emf-inout.cpp revision 453a8671e6992250324846d0b76f02801221c760
2801N/A#ifdef HAVE_CONFIG_H
2801N/A#include <png.h> //This must precede text_reassemble.h or it blows up in pngconf.h when compiling
#include "sp-root.h"
#include "sp-path.h"
#include "style.h"
#include "print.h"
#include "display/drawing-item.h"
#include "clear-n_.h"
#include "document.h"
#include "emf-print.h"
#include "emf-inout.h"
#ifndef U_PS_JOIN_MASK
namespace Inkscape {
namespace Extension {
namespace Internal {
static bool clipset = false;
gcc -Wall -o testpng testpng.c -lpng
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
size_t x, y;
inverse of gethexcolor() in emf-print.cpp
return(out);
return FALSE;
return TRUE;
unsigned int ret;
if (ret) {
bool new_FixPPTPatternAsHatch = mod->get_param_bool("FixPPTPatternAsHatch"); // force all patterns as standard EMF hatch
ext->set_param_bool("FixPPTCharPos",new_FixPPTCharPos); // Remember to add any new ones to PrintEmf::init or a mysterious failure will result!
/* given the transformation matrix from worldTranform return the scale in the matrix part. Assumes that the
double scale =
if(scale <= 0.0)scale=1.0; /* something is dreadfully wrong with the matrix, but do not crash over it */
return(scale);
/* given the transformation matrix from worldTranform and the current x,y position in inkscape coordinates,
generate an SVG transform that gives the same amount of rotation, no scaling, and maps x,y back onto x,y. This is used for
if(useoffset){
double newx = x * d->dc[d->level].worldTransform.eM11/scale + y * d->dc[d->level].worldTransform.eM21/scale;
double newy = x * d->dc[d->level].worldTransform.eM12/scale + y * d->dc[d->level].worldTransform.eM22/scale;
switch(hatchType){
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
switch(hatchType){
case U_HS_HORIZONTAL:
case U_HS_VERTICAL:
case U_HS_FDIAGONAL:
case U_HS_BDIAGONAL:
case U_HS_CROSS:
case U_HS_DIAGCROSS:
case U_HS_SOLIDCLR:
case U_HS_DITHEREDCLR:
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
switch(hatchType){
case U_HS_HORIZONTAL:
case U_HS_VERTICAL:
case U_HS_CROSS:
case U_HS_SOLIDCLR:
case U_HS_DITHEREDCLR:
case U_HS_SOLIDTEXTCLR:
case U_HS_DITHEREDTEXTCLR:
case U_HS_SOLIDBKCLR:
case U_HS_DITHEREDBKCLR:
case U_HS_FDIAGONAL:
case U_HS_BDIAGONAL:
case U_HS_DIAGCROSS:
int dibparams;
if( !cbBits ||
!cbBmi ||
pEmr,
&px,
&numCt,
&width,
&height,
if(!DIB_to_RGBA(
&mempng,
base64String = g_strdup("iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAA3NCSVQICAjb4U/gAAAALElEQVQImQXBQQ2AMAAAsUJQMSWI2H8qME1yMshojwrvGB8XcHKvR1XtOTc/8HENumHCsOMAAAAASUVORK5CYII=");
been loaded? The image names contain no identifying information, they are just numbered sequentially.
XXXX is the rotation in radians x 1000000 and truncated. That is then stored in BASE64 as the "image".
if(current_rotation(d) >= 0.00001 || current_rotation(d) <= -0.00001){ /* some rotation, allow a little rounding error around 0 degrees */
if(!idx){
if(d->dwRop3){
switch(d->dwRop3){
case U_SRCINVERT:
case U_DSTINVERT:
case U_BLACKNESS:
case U_SRCERASE:
case U_NOTSRCCOPY:
case U_NOTSRCERASE:
case U_PATCOPY:
case U_WHITENESS:
case U_SRCAND:
case U_MERGECOPY:
case U_MERGEPAINT:
case U_PATPAINT:
switch(d->dwRop2){
case U_R2_BLACK:
case U_R2_NOTMERGEPEN:
case U_R2_MASKNOTPEN:
case U_R2_NOTCOPYPEN:
case U_R2_MASKPENNOT:
case U_R2_NOT:
case U_R2_XORPEN:
case U_R2_NOTMASKPEN:
case U_R2_NOTXORPEN:
case U_R2_NOP:
case U_R2_MERGENOTPEN:
case U_R2_COPYPEN:
case U_R2_MASKPEN:
case U_R2_MERGEPENNOT:
case U_R2_MERGEPEN:
case U_R2_WHITE:
// *(d->outsvg) += tmp_id.str().c_str();
case DRAW_PATTERN:
case DRAW_IMAGE:
case DRAW_PAINT:
// if the stroke is the same as the fill, and the right size not to change the end size of the object, do not do it separately
case DRAW_PATTERN:
case DRAW_IMAGE:
case DRAW_PAINT:
if (clipset)
clipset = false;
double tmp;
tmp = ((((double) (px - d->dc[d->level].winorg.x))*scale) + d->dc[d->level].vieworg.x) * d->D2PscaleX;
tmp -= d->ulCornerOutX; //The EMF boundary rectangle can be anywhere, place its upper left corner in the Inkscape upper left corner
return(tmp);
double tmp;
tmp = ((((double) (py - d->dc[d->level].winorg.y))*scale) * d->E2IdirY + d->dc[d->level].vieworg.y) * d->D2PscaleY;
tmp -= d->ulCornerOutY; //The EMF boundary rectangle can be anywhere, place its upper left corner in the Inkscape upper left corner
return(tmp);
double wpx = px * d->dc[d->level].worldTransform.eM11 + py * d->dc[d->level].worldTransform.eM21 + d->dc[d->level].worldTransform.eDx;
double wpy = px * d->dc[d->level].worldTransform.eM12 + py * d->dc[d->level].worldTransform.eM22 + d->dc[d->level].worldTransform.eDy;
double ppx = fabs(px * (d->dc[d->level].ScaleInX ? d->dc[d->level].ScaleInX : 1.0) * d->D2PscaleX * current_scale(d));
return ppx;
if (!pEmr){ return; }
case U_PS_DASH:
case U_PS_DOT:
case U_PS_DASHDOT:
case U_PS_DASHDOTDOT:
if (d->dc[d->level].style.stroke_dash.dash && (d->level==0 || (d->level>0 && d->dc[d->level].style.stroke_dash.dash!=d->dc[d->level-1].style.stroke_dash.dash)))
case U_PS_SOLID:
case U_PS_ENDCAP_FLAT:
case U_PS_JOIN_ROUND:
} else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
//d->dc[d->level].style.stroke_width.value = 1.0;
if (!pEmr)
case U_PS_USERSTYLE:
if (d->dc[d->level].style.stroke_dash.dash && (d->level==0 || (d->level>0 && d->dc[d->level].style.stroke_dash.dash!=d->dc[d->level-1].style.stroke_dash.dash)))
case U_PS_DASH:
case U_PS_DOT:
case U_PS_DASHDOT:
case U_PS_DASHDOTDOT:
if (d->dc[d->level].style.stroke_dash.dash && (d->level==0 || (d->level>0 && d->dc[d->level].style.stroke_dash.dash!=d->dc[d->level-1].style.stroke_dash.dash)))
case U_PS_SOLID:
case U_PS_ENDCAP_ROUND:
case U_PS_ENDCAP_SQUARE:
case U_PS_ENDCAP_FLAT:
case U_PS_JOIN_BEVEL:
case U_PS_JOIN_MITER:
case U_PS_JOIN_ROUND:
if (pEmr->elp.elpPenStyle == U_PS_NULL) { // draw nothing, but fill out all the values with something
} else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
//d->dc[d->level].style.stroke_width.value = 1.0;
d->dc[d->level].stroke_recidx = index; // used if the hatch needs to be redone due to bkMode, textmode, etc. changes
else if(pEmr->elp.elpBrushStyle == U_BS_DIBPATTERN || pEmr->elp.elpBrushStyle == U_BS_DIBPATTERNPT){
d->dc[d->level].stroke_idx = add_image(d, pEmr, pEmr->cbBits, pEmr->cbBmi, *(uint32_t *) &(pEmr->elp.elpColor), pEmr->offBits, pEmr->offBmi);
d->dc[d->level].fill_recidx = index; // used if the hatch needs to be redone due to bkMode, textmode, etc. changes
tidx = add_image(d, (void *) pEmr, pEmr->cbBits, pEmr->cbBmi, pEmr->iUsage, pEmr->offBits, pEmr->offBmi);
if (!pEmr)return;
/* The logfont information always starts with a U_LOGFONT structure but the U_EMREXTCREATEFONTINDIRECTW
is defined as U_LOGFONT_PANOSE so it can handle one of those if that is actually present. Currently only logfont
d->dc[d->level].style.font_style.value = (pEmr->elfw.elfLogFont.lfItalic ? SP_CSS_FONT_STYLE_ITALIC : SP_CSS_FONT_STYLE_NORMAL);
if(ctmp){
if(*ctmp){
d->dc[d->level].font_name = strdup("Arial"); // Default font, EMF spec says device can pick whatever it wants
d->dc[d->level].style.baseline_shift.value = round((double)((pEmr->elfw.elfLogFont.lfEscapement + 3600) % 3600)) / 10.0; // use baseline_shift instead of text_transform to avoid overflow
// We are keeping a copy of the EMR rather than just a structure. Currently that is not necessary as the entire
// reord by record, and we might need to do that again at some point in the future if we start running into EMF
/* Identify probable Adobe Illustrator produced EMF files, which do strange things with the scaling.
int ret=0;
char *ptr;
if(pEmr->nDescription)string = U_Utf16leToUtf8((uint16_t *)((char *) pEmr + pEmr->offDescription), pEmr->nDescription, NULL);
if(string){
return(ret);
return res;
int dibparams;
if(!cbBits ||
!cbBmi ||
pEmr,
&px,
&numCt,
&width,
&height,
if(!DIB_to_RGBA(
&mempng,
tmp_image << "iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAA3NCSVQICAjb4U/gAAAALElEQVQImQXBQQ2AMAAAsUJQMSWI2H8qME1yMshojwrvGB8XcHKvR1XtOTc/8HENumHCsOMAAAAASUVORK5CYII=";
uint32_t tbkMode = U_TRANSPARENT; // holds proposed change to bkMode, if text is involved saving these to the DC must wait until the text is written
tsp.boff = 0.0; /* offset to baseline from LL corner of bounding rectangle, changes with fs and taln*/
while(OK){
//std::cout << "record type: " << lpEMFR->iType << " length: " << lpEMFR->nSize << " offset: " << off <<std::endl;
//std::cout << "tri->dirty:"<< d->tri->dirty << " emr_mask: " << std::hex << emr_mask << std::dec << std::endl;
// incompatible change to text drawing detected (color or background change) forces out existing text
if ((d->dc[d->level].dirty & DIRTY_TEXT) || ((emr_mask != 0xFFFFFFFF) && (emr_mask & U_DRAW_TEXT) && d->tri->dirty)){
// U_COLORREF and TRCOLORREF are exactly the same in memory, but the compiler needs some convincing...
if(tbkMode == U_TRANSPARENT){ (void) trinfo_load_bk(d->tri, BKCLR_NONE, *(TRCOLORREF *) &tbkColor); }
//std::cout << "BEFORE DRAW logic d->mask: " << std::hex << d->mask << " emr_mask: " << emr_mask << std::dec << std::endl;
((d->mask & U_DRAW_ONLYTO) && !(emr_mask & U_DRAW_ONLYTO) )// *TO records can only be followed by other *TO records
d->mask = 0;
d->drawtype = 0;
// std::cout << "AFTER DRAW logic d->mask: " << std::hex << d->mask << " emr_mask: " << emr_mask << std::dec << std::endl;
case U_EMR_HEADER:
if (d->pDesc) {
tmp_outdef << " xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n"; // needed for sodipodi:role
if ((pEmr->szlMillimeters.cx + pEmr->szlMillimeters.cy) && ( pEmr->szlDevice.cx + pEmr->szlDevice.cy)){
tmp_outdef <<
for( int i=0; i < d->n_obj; ++i )
case U_EMR_POLYBEZIER:
uint32_t i,j;
tmp_str <<
case U_EMR_POLYGON:
uint32_t i;
tmp_str <<
tmp_str <<
case U_EMR_POLYLINE:
uint32_t i;
tmp_str <<
tmp_str <<
case U_EMR_POLYBEZIERTO:
uint32_t i,j;
tmp_path <<
case U_EMR_POLYLINETO:
uint32_t i;
tmp_path <<
case U_EMR_POLYPOLYLINE:
case U_EMR_POLYPOLYGON:
case U_EMR_SETWINDOWEXTEX:
d->dc[d->level].ScaleInX = (double) d->dc[d->level].sizeView.cx / (double) d->dc[d->level].sizeWnd.cx;
d->dc[d->level].ScaleInY = (double) d->dc[d->level].sizeView.cy / (double) d->dc[d->level].sizeWnd.cy;
case U_EMR_SETWINDOWORGEX:
case U_EMR_SETVIEWPORTEXTEX:
d->dc[d->level].ScaleInX = (double) d->dc[d->level].sizeView.cx / (double) d->dc[d->level].sizeWnd.cx;
d->dc[d->level].ScaleInY = (double) d->dc[d->level].sizeView.cy / (double) d->dc[d->level].sizeWnd.cy;
case U_EMR_SETVIEWPORTORGEX:
case U_EMR_EOF:
OK=0;
case U_EMR_SETMAPMODE:
case U_MM_TEXT:
/* For all of the following the indicated scale this will be encoded in WindowExtEx/ViewportExtex
case U_MM_ANISOTROPIC:
case U_EMR_SETBKMODE:
case U_EMR_SETPOLYFILLMODE:
case U_EMR_SETROP2:
case U_EMR_SETSTRETCHBLTMODE:
case U_EMR_SETTEXTALIGN:
case U_EMR_SETCOLORADJUSTMENT:
case U_EMR_SETTEXTCOLOR:
case U_EMR_SETBKCOLOR:
case U_EMR_MOVETOEX:
tmp_path <<
case U_EMR_INTERSECTCLIPRECT:
clipset = true;
if ((rc.left == rc_old.left) && (rc.top == rc_old.top) && (rc.right == rc_old.right) && (rc.bottom == rc_old.bottom))
case U_EMR_SAVEDC:
d->dc[d->level + 1].font_name = strdup(d->dc[d->level].font_name); // or memory access problems because font name pointer duplicated
case U_EMR_RESTOREDC:
if (d->dc[old_level].style.stroke_dash.dash && (old_level==0 || (old_level>0 && d->dc[old_level].style.stroke_dash.dash!=d->dc[old_level-1].style.stroke_dash.dash))){
old_level--;
case U_EMR_SETWORLDTRANSFORM:
case U_MWT_IDENTITY:
case U_MWT_LEFTMULTIPLY:
case U_MWT_RIGHTMULTIPLY:
case U_EMR_SELECTOBJECT:
switch (index) {
case U_NULL_BRUSH:
case U_BLACK_BRUSH:
case U_DKGRAY_BRUSH:
case U_GRAY_BRUSH:
case U_LTGRAY_BRUSH:
case U_WHITE_BRUSH:
float val = 0;
switch (index) {
case U_BLACK_BRUSH:
case U_DKGRAY_BRUSH:
case U_GRAY_BRUSH:
case U_LTGRAY_BRUSH:
case U_WHITE_BRUSH:
case U_NULL_PEN:
case U_BLACK_PEN:
case U_WHITE_PEN:
case U_EMR_CREATEPEN:
case U_EMR_CREATEMONOBRUSH:
case U_EMR_EXTCREATEPEN:
case U_EMR_CREATEPEN:
case U_EMR_DELETEOBJECT:
case U_EMR_ANGLEARC:
case U_EMR_ELLIPSE:
double cx = pix_to_x_point( d, (rclBox.left + rclBox.right)/2.0, (rclBox.bottom + rclBox.top)/2.0 );
double cy = pix_to_y_point( d, (rclBox.left + rclBox.right)/2.0, (rclBox.bottom + rclBox.top)/2.0 );
case U_EMR_RECTANGLE:
case U_EMR_ROUNDRECT:
case U_EMR_ARC:
int f1;
if(!stat){
case U_EMR_CHORD:
int f1;
case U_EMR_PIE:
int f1;
case U_EMR_LINETO:
tmp_path <<
case U_EMR_ARCTO:
int f1;
case U_EMR_SETARCDIRECTION:
case U_EMR_SETMITERLIMIT:
case U_EMR_BEGINPATH:
case U_EMR_ENDPATH:
d->mask &= (0xFFFFFFFF - U_DRAW_ONLYTO); // clear the OnlyTo bit (it might not have been set), prevents any further path extension
case U_EMR_CLOSEFIGURE:
case U_EMR_FILLPATH:
if(!(d->mask & U_DRAW_CLOSED)){ // Close a path not explicitly closed by an EMRCLOSEFIGURE, otherwise fill makes no sense
case U_EMR_STROKEANDFILLPATH:
if(!(d->mask & U_DRAW_CLOSED)){ // Close a path not explicitly closed by an EMRCLOSEFIGURE, otherwise fill makes no sense
case U_EMR_STROKEPATH:
case U_EMR_ABORTPATH:
d->drawtype = 0;
case U_EMR_COMMENT:
if ( *szTxt) {
szTxt++;
case U_EMR_EXTSELECTCLIPRGN:
clipset = false;
case U_EMR_BITBLT:
d->mask |= U_DRAW_CLOSED; // Bitblit is not really open or closed, but we need it to fill, and this is the flag for that
int sh = 0;
case U_EMR_STRETCHBLT:
case U_EMR_MASKBLT:
int sh = 0;
case U_EMR_STRETCHDIBITS:
case U_EMR_EXTTEXTOUTA:
case U_EMR_EXTTEXTOUTW:
case U_EMR_SMALLTEXTOUT:
int roff = sizeof(U_EMRSMALLTEXTOUT); //offset to the start of the variable fields, only used with U_EMR_SMALLTEXTOUT
int cChars;
cChars = 0;
if(!dup_wt)dup_wt = U_Latin1ToUtf32le((char *) pEmr + pEmr->emrtext.offString, pEmr->emrtext.nChars, NULL);
dup_wt = U_Utf16leToUtf32le((uint16_t *)((char *) pEmr + pEmr->emrtext.offString), pEmr->emrtext.nChars, NULL);
msdepua(dup_wt); //convert everything in Microsoft's private use area. For Symbol, Wingdings, Dingbats
char *ansi_text;
// Empty string or starts with an invalid escape/control sequence, which is bogus text. Throw it out before g_markup_escape_text can make things worse
if (ansi_text) {
case SP_CSS_FONT_STYLE_ITALIC:
case SP_CSS_FONT_STYLE_NORMAL:
// EMF textalignment is a bit strange: 0x6 is center, 0x2 is right, 0x0 is left, the value 0x4 is also drawn left
ALIRIGHT);
ALITOP));
if( (fOptions & U_ETO_RTLREADING) || (d->dc[d->level].textAlign & U_TA_RTLREADING) ){ tsp.ldir = LDIR_RL; }
tsp.ori = d->dc[d->level].style.baseline_shift.value; // For now orientation is always the same as escapement
tsp.string = (uint8_t *) U_strdup(escaped_text); // this will be free'd much later at a trinfo_clear().
// when font name includes narrow it may not be set to "condensed". Narrow fonts do not work well anyway though
case U_EMR_POLYBEZIER16:
uint32_t i,j;
case U_EMR_POLYGON16:
unsigned int first = 0;
case U_EMR_POLYLINE16:
uint32_t i;
case U_EMR_POLYBEZIERTO16:
uint32_t i,j;
case U_EMR_POLYLINETO16:
uint32_t i;
case U_EMR_POLYPOLYLINE16:
case U_EMR_POLYPOLYGON16:
case U_EMR_CREATEMONOBRUSH:
case U_EMR_EXTCREATEPEN:
case U_EMR_SETICMMODE:
// When testing, uncomment the following to place a comment for each processed EMR record in the SVG
// *(d->outsvg) += dbg_str.str().c_str();
(void) emr_properties(U_EMR_INVALID); // force the release of the lookup table memory, returned value is irrelevant
typedef struct _SMALL_RECT {
d.dc[0].font_name = strdup("Arial"); // Default font, EMF spec says device can pick whatever it wants
return NULL;
d.mask = 0;
d.drawtype = 0;
d.dwRop3 = 0;
// set up the size default for patterns in defs. This might not be referenced if there are no patterns defined in the drawing.
char *contents;
SPDocument *doc = SPDocument::createNewDocFromMem(d.outsvg->c_str(), strlen(d.outsvg->c_str()), TRUE);
delete d.outsvg;
delete d.path;
delete d.outdef;
delete d.defs;
if (d.emf_obj) {
for (i=0; i<d.n_obj; i++)
delete_object(&d, i);
delete[] d.emf_obj;
for(int i=0; i<=d.level;i++){
return doc;
"<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">true</param>\n"
"<param name=\"TnrToSymbol\" gui-text=\"" N_("Map Unicode to Symbol font") "\" type=\"boolean\">true</param>\n"
"<param name=\"TnrToWingdings\" gui-text=\"" N_("Map Unicode to Wingdings") "\" type=\"boolean\">true</param>\n"
"<param name=\"TnrToZapfDingbats\" gui-text=\"" N_("Map Unicode to Zapf Dingbats") "\" type=\"boolean\">true</param>\n"
"<param name=\"UsePUA\" gui-text=\"" N_("Use MS Unicode PUA (0xF020-0xF0FF) for converted characters") "\" type=\"boolean\">false</param>\n"
"<param name=\"FixPPTCharPos\" gui-text=\"" N_("Compensate for PPT font bug") "\" type=\"boolean\">false</param>\n"
"<param name=\"FixPPTDashLine\" gui-text=\"" N_("Convert dashed/dotted lines to single lines") "\" type=\"boolean\">false</param>\n"
"<param name=\"FixPPTGrad2Polys\" gui-text=\"" N_("Convert gradients to colored polygon series") "\" type=\"boolean\">false</param>\n"
"<param name=\"FixPPTPatternAsHatch\" gui-text=\"" N_("Map all fill patterns to standard EMF hatches") "\" type=\"boolean\">false</param>\n"
"<param name=\"FixImageRot\" gui-text=\"" N_("Ignore image rotations") "\" type=\"boolean\">false</param>\n"