metafile-print.cpp revision 440e7182af0764dabf2219f14c5b8ed58949953e
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith * @brief Metafile printing - common routines
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith * Krzysztof KosiĆski <tweenk.pl@gmail.com>
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith * Copyright (C) 2013 Authors
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith * Released under GNU GPL, read the file 'COPYING' for more information
a4142717644b885998f4de2b27be4e8648315decMarkus EngelPrintMetafile::FontfixMap PrintMetafile::_ppt_fixable_fonts;
a4142717644b885998f4de2b27be4e8648315decMarkus Engel // restore default signal handling for SIGPIPE
a4142717644b885998f4de2b27be4e8648315decMarkus Engelbool PrintMetafile::textToPath(Inkscape::Extension::Print *ext)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithunsigned int PrintMetafile::bind(Inkscape::Extension::Print * /*mod*/, Geom::Affine const &transform, float /*opacity*/)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithunsigned int PrintMetafile::release(Inkscape::Extension::Print * /*mod*/)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithbool PrintMetafile::_load_ppt_fontfix_data() //this is not called by any other source files
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith static bool ppt_fontfix_available = false;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith if (_ppt_fontfix_read) return ppt_fontfix_available;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // add default entry
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith _ppt_fixable_fonts.insert(std::make_pair(Glib::ustring(""), FontfixParams()));
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith std::string fontfix_path = Glib::build_filename(INKSCAPE_EXTENSIONDIR, "fontfix.conf");
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith std::ifstream fontfix_file(fontfix_path.c_str(), std::ios::in);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith g_warning("Unable to open PowerPoint fontfix file: %s\n"
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith "PowerPoint ungrouping compensation in WMF/EMF export will not be available.",
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith return (ppt_fontfix_available = false);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith char *oldlocale = g_strdup(setlocale(LC_NUMERIC, NULL));
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // not a comment, get the 4 values from the line
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith int elements = sscanf(instr.c_str(), "%lf %lf %lf %127[^\n]",
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith ¶ms.f1, ¶ms.f2, ¶ms.f3, &fontname[0]);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith g_warning("Malformed line in %s: %s\n", fontfix_path.c_str(), instr.c_str());
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith _ppt_fixable_fonts.insert(std::make_pair(Glib::ustring(fontname), params));
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith return (ppt_fontfix_available = true);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// Finds font fix parameters for the given fontname.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithvoid PrintMetafile::_lookup_ppt_fontfix(Glib::ustring const &fontname, FontfixParams ¶ms)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith if (!_ppt_fontfix_read) _load_ppt_fontfix_data();
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith FontfixMap::iterator f = _ppt_fixable_fonts.find(fontname);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn SmithU_COLORREF PrintMetafile::_gethexcolor(uint32_t color)
253a831f0fda7107ad98c474e21b71e90592d0f2Alex Valavanis// Translate Inkscape weights to EMF weights.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithuint32_t PrintMetafile::_translate_weight(unsigned inkweight)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // 400 is tested first, as it is the most common case
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith case SP_CSS_FONT_WEIGHT_200: return U_FW_EXTRALIGHT;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith case SP_CSS_FONT_WEIGHT_600: return U_FW_SEMIBOLD;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith case SP_CSS_FONT_WEIGHT_800: return U_FW_EXTRABOLD;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith default: return U_FW_NORMAL;
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith/* opacity weighting of two colors as float. v1 is the color, op is its opacity, v2 is the background color */
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithinline float opweight(float v1, float v2, float op)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn SmithU_COLORREF PrintMetafile::avg_stop_color(SPGradient *gr)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith sp_color_get_rgb_floatv(&gr->vector.stops[0 ].color, rgbs);
253a831f0fda7107ad98c474e21b71e90592d0f2Alex Valavanis sp_color_get_rgb_floatv(&gr->vector.stops[last].color, rgbe);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith /* Replace opacity at start & stop with that fraction background color, then average those two for final color. */
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * ((opweight(rgbs[0], gv.rgb[0], ops) + opweight(rgbe[0], gv.rgb[0], ope)) / 2.0),
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * ((opweight(rgbs[1], gv.rgb[1], ops) + opweight(rgbe[1], gv.rgb[1], ope)) / 2.0),
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * ((opweight(rgbs[2], gv.rgb[2], ops) + opweight(rgbe[2], gv.rgb[2], ope)) / 2.0)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn SmithU_COLORREF PrintMetafile::weight_opacity(U_COLORREF c1)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * opweight((float)c1.Red / 255.0, gv.rgb[0], opa),
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * opweight((float)c1.Green / 255.0, gv.rgb[1], opa),
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith 255 * opweight((float)c1.Blue / 255.0, gv.rgb[2], opa)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith/* t between 0 and 1, values outside that range use the nearest limit */
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn SmithU_COLORREF PrintMetafile::weight_colors(U_COLORREF c1, U_COLORREF c2, double t)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith#define clrweight(a,b,t) ((1-t)*((double) a) + (t)*((double) b))
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith result.Reserved = clrweight(c1.Reserved, c2.Reserved, t);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // now handle the opacity, mix the RGB with background at the weighted opacity
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// Extract hatchType, hatchColor from a name like
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// EMFhatch<hatchType>_<hatchColor>
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// Where the first one is a number and the second a color in hex.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// hatchType and hatchColor have been set with defaults before this is called.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithvoid PrintMetafile::hatch_classify(char *name, int *hatchType, U_COLORREF *hatchColor, U_COLORREF *bkColor)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // name should be EMFhatch or WMFhatch but *MFhatch will be accepted
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith return; // not anything we can parse
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith if (*name != '_' || val > U_HS_DITHEREDBKCLR) { // wrong syntax, cannot classify
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith if (2 != sscanf(name, "%X_%X", &hcolor, &bcolor)) { // not a pattern with background
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith *hatchType = -1; // not a pattern, cannot classify
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith /* Everything > U_HS_SOLIDCLR is solid, just specify the color in the brush rather than messing around with background or textcolor */
bf9ec3e969ba6b11cbbc613545aedc63cc886973Matthew Petroff// Recurse down from a brush pattern, try to figure out what it is.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// If an image is found set a pointer to the epixbuf, else set that to NULL
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// If a pattern is found with a name like [EW]MFhatch3_3F7FFF return hatchType=3, hatchColor=3F7FFF (as a uint32_t),
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith// otherwise hatchType is set to -1 and hatchColor is not defined.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithvoid PrintMetafile::brush_classify(SPObject *parent, int depth, Inkscape::Pixbuf **epixbuf, int *hatchType, U_COLORREF *hatchColor, U_COLORREF *bkColor)
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // first look along the pattern chain, if there is one
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith for (SPPattern *pat_i = SP_PATTERN(parent); pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith strncpy(temp, pat_i->getAttribute("id"), sizeof(temp)-1); // Some names may be longer than [EW]MFhatch#_######
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith hatch_classify(temp, hatchType, hatchColor, bkColor);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith // still looking? Look at this pattern's children, if there are any
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith while (child && !(*epixbuf) && (*hatchType == -1)) {
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith brush_classify(child, depth, epixbuf, hatchType, hatchColor, bkColor);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith } else { // some inkscape rearrangements pass through nodes between pattern and image which are not classified as either.
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith while (child && !(*epixbuf) && (*hatchType == -1)) {
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith brush_classify(child, depth, epixbuf, hatchType, hatchColor, bkColor);
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smith//swap R/B in 4 byte pixel
2f5f997e354e7f4a02b6818bdc68fbece5cb237dJohn Smithvoid PrintMetafile::swapRBinRGBA(char *px, int pixels)
char tmp;
g_error("Fatal programming error, hold_gradient() in metafile-print.cpp called with invalid draw mode");
http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
Geom::PathVector PrintMetafile::center_ellipse_as_SVG_PathV(Geom::Point ctr, double rx, double ry, double F)
using Geom::X;
using Geom::Y;
sprintf(text, " M %f,%f A %f %f %f 0 0 %f %f A %f %f %f 0 0 %f %f z", x1, y1, rx, ry, F * 360. / (2.*M_PI), x2, y2, rx, ry, F * 360. / (2.*M_PI), x1, y1);
return outres;
Geom::PathVector PrintMetafile::center_elliptical_ring_as_SVG_PathV(Geom::Point ctr, double rx1, double ry1, double rx2, double ry2, double F)
using Geom::X;
using Geom::Y;
sprintf(text, " M %f,%f A %f %f %f 0 1 %f %f A %f %f %f 0 1 %f %f z M %f,%f A %f %f %f 0 0 %f %f A %f %f %f 0 0 %f %f z",
return outres;
Geom::PathVector PrintMetafile::center_elliptical_hole_as_SVG_PathV(Geom::Point ctr, double rx, double ry, double F)
using Geom::X;
using Geom::Y;
sprintf(text, " M %f,%f A %f %f %f 0 0 %f %f A %f %f %f 0 0 %f %f z M 50000,50000 50000,-50000 -50000,-50000 -50000,50000 z",
return outres;
ctr "center" of rectangle (might not actually be in the center with respect to leading/trailing edges
Geom::PathVector PrintMetafile::rect_cutter(Geom::Point ctr, Geom::Point pos, Geom::Point neg, Geom::Point width)
return outres;
return fr;