Code

977b0eb64fb7f0a78266035a3a5ea721dc436b13
[inkscape.git] / src / extension / internal / emf-win32-inout.cpp
1 /** \file
2  * Enhanced Metafile Input and Output.
3  */
4 /*
5  * Authors:
6  *   Ulf Erikson <ulferikson@users.sf.net>
7  *
8  * Copyright (C) 2006-2008 Authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
12 /*
13  * References:
14  *  - How to Create & Play Enhanced Metafiles in Win32
15  *      http://support.microsoft.com/kb/q145999/
16  *  - INFO: Windows Metafile Functions & Aldus Placeable Metafiles
17  *      http://support.microsoft.com/kb/q66949/
18  *  - Metafile Functions
19  *      http://msdn.microsoft.com/library/en-us/gdi/metafile_0whf.asp
20  *  - Metafile Structures
21  *      http://msdn.microsoft.com/library/en-us/gdi/metafile_5hkj.asp
22  */
24 #ifdef WIN32
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "win32.h"
31 #include "emf-win32-print.h"
32 #include "emf-win32-inout.h"
33 #include "inkscape.h"
34 #include "sp-path.h"
35 #include "style.h"
36 #include "color.h"
37 #include "display/curve.h"
38 #include "libnr/nr-point-matrix-ops.h"
39 #include "gtk/gtk.h"
40 #include "print.h"
41 #include "glibmm/i18n.h"
42 #include "extension/extension.h"
43 #include "extension/system.h"
44 #include "extension/print.h"
45 #include "extension/db.h"
46 #include "extension/output.h"
47 #include "document.h"
48 #include "display/nr-arena.h"
49 #include "display/nr-arena-item.h"
51 #include "libnr/nr-rect.h"
52 #include "libnr/nr-matrix.h"
53 #include "libnr/nr-pixblock.h"
55 #include <stdio.h>
56 #include <string.h>
58 #include <vector>
59 #include <string>
61 #include "io/sys.h"
63 #include "unit-constants.h"
65 #include "clear-n_.h"
68 #define PRINT_EMF_WIN32 "org.inkscape.print.emf.win32"
70 #ifndef PS_JOIN_MASK
71 #define PS_JOIN_MASK (PS_JOIN_BEVEL|PS_JOIN_MITER|PS_JOIN_ROUND)
72 #endif
75 namespace Inkscape {
76 namespace Extension {
77 namespace Internal {
80 EmfWin32::EmfWin32 (void) // The null constructor
81 {
82     return;
83 }
86 EmfWin32::~EmfWin32 (void) //The destructor
87 {
88     return;
89 }
92 bool
93 EmfWin32::check (Inkscape::Extension::Extension * module)
94 {
95     if (NULL == Inkscape::Extension::db.get(PRINT_EMF_WIN32))
96         return FALSE;
97     return TRUE;
98 }
101 static void
102 emf_print_document_to_file(SPDocument *doc, gchar const *filename)
104     Inkscape::Extension::Print *mod;
105     SPPrintContext context;
106     gchar const *oldconst;
107     gchar *oldoutput;
108     unsigned int ret;
110     sp_document_ensure_up_to_date(doc);
112     mod = Inkscape::Extension::get_print(PRINT_EMF_WIN32);
113     oldconst = mod->get_param_string("destination");
114     oldoutput = g_strdup(oldconst);
115     mod->set_param_string("destination", (gchar *)filename);
117 /* Start */
118     context.module = mod;
119     /* fixme: This has to go into module constructor somehow */
120     /* Create new arena */
121     mod->base = SP_ITEM(sp_document_root(doc));
122     mod->arena = NRArena::create();
123     mod->dkey = sp_item_display_key_new(1);
124     mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
125     /* Print document */
126     ret = mod->begin(doc);
127     if (ret) {
128         throw Inkscape::Extension::Output::save_failed();
129     }
130     sp_item_invoke_print(mod->base, &context);
131     ret = mod->finish();
132     /* Release arena */
133     sp_item_invoke_hide(mod->base, mod->dkey);
134     mod->base = NULL;
135     nr_arena_item_unref(mod->root);
136     mod->root = NULL;
137     nr_object_unref((NRObject *) mod->arena);
138     mod->arena = NULL;
139 /* end */
141     mod->set_param_string("destination", oldoutput);
142     g_free(oldoutput);
144     return;
148 void
149 EmfWin32::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
151     Inkscape::Extension::Extension * ext;
153     ext = Inkscape::Extension::db.get(PRINT_EMF_WIN32);
154     if (ext == NULL)
155         return;
157     bool old_textToPath  = ext->get_param_bool("textToPath");
158     bool new_val         = mod->get_param_bool("textToPath");
159     ext->set_param_bool("textToPath", new_val);
161     gchar * final_name;
162     final_name = g_strdup_printf("%s", uri);
163     emf_print_document_to_file(doc, final_name);
164     g_free(final_name);
166     ext->set_param_bool("textToPath", old_textToPath);
168     return;
173 typedef struct {
174     int type;
175     ENHMETARECORD *lpEMFR;
176 } EMF_OBJECT, *PEMF_OBJECT;
178 typedef struct emf_callback_data {
179     Glib::ustring *outsvg;
180     Glib::ustring *path;
181     struct SPStyle style;
182     class SPTextStyle tstyle;
183     bool stroke_set;
184     bool fill_set;
185     double xDPI, yDPI;
186     bool pathless_stroke;
187     bool inpath;
189     SIZEL sizeWnd;
190     SIZEL sizeView;
191     float PixelsInX, PixelsInY;
192     float PixelsOutX, PixelsOutY;
193     float MMX;
194     float MMY;
195     float dwInchesX;
196     float dwInchesY;
197     POINTL winorg;
198     POINTL vieworg;
199     double ScaleInX, ScaleInY;
200     double ScaleOutX, ScaleOutY;
201     COLORREF textColor;
202     bool textColorSet;
203     DWORD textAlign;
204     XFORM worldTransform;
205     unsigned int id;
206     CHAR *pDesc;
208     int n_obj;
209     PEMF_OBJECT emf_obj;
210 } EMF_CALLBACK_DATA, *PEMF_CALLBACK_DATA;
213 static void
214 output_style(PEMF_CALLBACK_DATA d, int iType)
216     SVGOStringStream tmp_id;
217     SVGOStringStream tmp_style;
218     char tmp[1024] = {0};
220     float fill_rgb[3];
221     sp_color_get_rgb_floatv( &(d->style.fill.value.color), fill_rgb );
222     
223     float stroke_rgb[3];
224     sp_color_get_rgb_floatv(&(d->style.stroke.value.color), stroke_rgb);
226     tmp_id << "\n\tid=\"" << (d->id++) << "\"";
227     *(d->outsvg) += tmp_id.str().c_str();
228     *(d->outsvg) += "\n\tstyle=\"";
229     if (iType == EMR_STROKEPATH || !d->fill_set) {
230         tmp_style << "fill:none;";
231     } else {
232         snprintf(tmp, 1023,
233                  "fill:#%02x%02x%02x;",
234                  SP_COLOR_F_TO_U(fill_rgb[0]),
235                  SP_COLOR_F_TO_U(fill_rgb[1]),
236                  SP_COLOR_F_TO_U(fill_rgb[2]));
237         tmp_style << tmp;
238         snprintf(tmp, 1023,
239                  "fill-rule:%s;",
240                  d->style.fill_rule.value == 0 ? "evenodd" : "nonzero");
241         tmp_style << tmp;
242         tmp_style << "fill-opacity:1;";
244         if (d->fill_set && d->stroke_set && d->style.stroke_width.value == 1 &&
245             fill_rgb[0]==stroke_rgb[0] && fill_rgb[1]==stroke_rgb[1] && fill_rgb[2]==stroke_rgb[2])
246         {
247             d->stroke_set = false;
248         }
249     }
251     if (iType == EMR_FILLPATH || !d->stroke_set) {
252         tmp_style << "stroke:none;";
253     } else {
254         snprintf(tmp, 1023,
255                  "stroke:#%02x%02x%02x;",
256                  SP_COLOR_F_TO_U(stroke_rgb[0]),
257                  SP_COLOR_F_TO_U(stroke_rgb[1]),
258                  SP_COLOR_F_TO_U(stroke_rgb[2]));
259         tmp_style << tmp;
261         tmp_style << "stroke-width:" <<
262             MAX( 0.001, d->style.stroke_width.value ) << "px;";
264         tmp_style << "stroke-linecap:" <<
265             (d->style.stroke_linecap.computed == 0 ? "butt" :
266              d->style.stroke_linecap.computed == 1 ? "round" :
267              d->style.stroke_linecap.computed == 2 ? "square" :
268              "unknown") << ";";
270         tmp_style << "stroke-linejoin:" <<
271             (d->style.stroke_linejoin.computed == 0 ? "miter" :
272              d->style.stroke_linejoin.computed == 1 ? "round" :
273              d->style.stroke_linejoin.computed == 2 ? "bevel" :
274              "unknown") << ";";
276         if (d->style.stroke_linejoin.computed == 0) {
277             tmp_style << "stroke-miterlimit:" <<
278                 MAX( 0.01, d->style.stroke_miterlimit.value ) << ";";
279         }
281         if (d->style.stroke_dasharray_set &&
282             d->style.stroke_dash.n_dash && d->style.stroke_dash.dash)
283         {
284             tmp_style << "stroke-dasharray:";
285             for (int i=0; i<d->style.stroke_dash.n_dash; i++) {
286                 if (i)
287                     tmp_style << ",";
288                 tmp_style << d->style.stroke_dash.dash[i];
289             }
290             tmp_style << ";";
291             tmp_style << "stroke-dashoffset:0;";
292         } else {
293             tmp_style << "stroke-dasharray:none;";
294         }
295         tmp_style << "stroke-opacity:1;";
296     }
297     tmp_style << "\" ";
299     *(d->outsvg) += tmp_style.str().c_str();
303 static double
304 _pix_x_to_point(PEMF_CALLBACK_DATA d, double px)
306     double tmp = px - d->winorg.x;
307     tmp *= d->ScaleInX ? d->ScaleInX : 1.0;
308     tmp += d->vieworg.x;
309     return tmp;
312 static double
313 _pix_y_to_point(PEMF_CALLBACK_DATA d, double px)
315     double tmp = px - d->winorg.y;
316     tmp *= d->ScaleInY ? d->ScaleInY : 1.0;
317     tmp += d->vieworg.y;
318     return tmp;
322 static double
323 pix_to_x_point(PEMF_CALLBACK_DATA d, double px, double py)
325     double ppx = _pix_x_to_point(d, px);
326     double ppy = _pix_y_to_point(d, py);
328     double x = ppx * d->worldTransform.eM11 + ppy * d->worldTransform.eM21 + d->worldTransform.eDx;
329     x *= d->ScaleOutX ? d->ScaleOutX : DEVICESCALE;
330     
331     return x;
334 static double
335 pix_to_y_point(PEMF_CALLBACK_DATA d, double px, double py)
337     double ppx = _pix_x_to_point(d, px);
338     double ppy = _pix_y_to_point(d, py);
340     double y = ppx * d->worldTransform.eM12 + ppy * d->worldTransform.eM22 + d->worldTransform.eDy;
341     y *= d->ScaleOutY ? d->ScaleOutY : DEVICESCALE;
342     
343     return y;
346 static double
347 pix_to_size_point(PEMF_CALLBACK_DATA d, double px)
349     double dx = pix_to_x_point(d, px, 0);
350     double dy = pix_to_y_point(d, px, 0);
351     double tmp = sqrt(dx * dx + dy * dy);
352     return tmp;
356 static void
357 select_pen(PEMF_CALLBACK_DATA d, int index)
359     PEMRCREATEPEN pEmr = NULL;
361     if (index >= 0 && index < d->n_obj)
362         pEmr = (PEMRCREATEPEN) d->emf_obj[index].lpEMFR;
364     if (!pEmr)
365         return;
367     switch (pEmr->lopn.lopnStyle & PS_STYLE_MASK) {
368         case PS_DASH:
369         case PS_DOT:
370         case PS_DASHDOT:
371         case PS_DASHDOTDOT:
372         {
373             int i = 0;
374             int penstyle = (pEmr->lopn.lopnStyle & PS_STYLE_MASK);
375             d->style.stroke_dash.n_dash =
376                 penstyle == PS_DASHDOTDOT ? 6 : penstyle == PS_DASHDOT ? 4 : 2;
377             if (d->style.stroke_dash.dash)
378                 delete[] d->style.stroke_dash.dash;
379             d->style.stroke_dash.dash = new double[d->style.stroke_dash.n_dash];
380             if (penstyle==PS_DASH || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
381                 d->style.stroke_dash.dash[i++] = 3;
382                 d->style.stroke_dash.dash[i++] = 1;
383             }
384             if (penstyle==PS_DOT || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
385                 d->style.stroke_dash.dash[i++] = 1;
386                 d->style.stroke_dash.dash[i++] = 1;
387             }
388             if (penstyle==PS_DASHDOTDOT) {
389                 d->style.stroke_dash.dash[i++] = 1;
390                 d->style.stroke_dash.dash[i++] = 1;
391             }
392             
393             d->style.stroke_dasharray_set = 1;
394             break;
395         }
396         
397         case PS_SOLID:
398         default:
399         {
400             d->style.stroke_dasharray_set = 0;
401             break;
402         }
403     }
405     switch (pEmr->lopn.lopnStyle & PS_ENDCAP_MASK) {
406         case PS_ENDCAP_ROUND:
407         {
408             d->style.stroke_linecap.computed = 1;
409             break;
410         }
411         case PS_ENDCAP_SQUARE:
412         {
413             d->style.stroke_linecap.computed = 2;
414             break;
415         }
416         case PS_ENDCAP_FLAT:
417         default:
418         {
419             d->style.stroke_linecap.computed = 0;
420             break;
421         }
422     }
424     switch (pEmr->lopn.lopnStyle & PS_JOIN_MASK) {
425         case PS_JOIN_BEVEL:
426         {
427             d->style.stroke_linejoin.computed = 2;
428             break;
429         }
430         case PS_JOIN_MITER:
431         {
432             d->style.stroke_linejoin.computed = 0;
433             break;
434         }
435         case PS_JOIN_ROUND:
436         default:
437         {
438             d->style.stroke_linejoin.computed = 1;
439             break;
440         }
441     }
443     d->stroke_set = true;
445     if (pEmr->lopn.lopnStyle == PS_NULL) {
446         d->style.stroke_width.value = 0;
447         d->stroke_set = false;
448     } else if (pEmr->lopn.lopnWidth.x) {
449         d->style.stroke_width.value = pix_to_size_point( d, pEmr->lopn.lopnWidth.x );
450     } else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
451         //d->style.stroke_width.value = 1.0;
452         d->style.stroke_width.value = pix_to_size_point( d, 1 );
453     }
455     double r, g, b;
456     r = SP_COLOR_U_TO_F( GetRValue(pEmr->lopn.lopnColor) );
457     g = SP_COLOR_U_TO_F( GetGValue(pEmr->lopn.lopnColor) );
458     b = SP_COLOR_U_TO_F( GetBValue(pEmr->lopn.lopnColor) );
459     d->style.stroke.value.color.set( r, g, b );
463 static void
464 select_extpen(PEMF_CALLBACK_DATA d, int index)
466     PEMREXTCREATEPEN pEmr = NULL;
468     if (index >= 0 && index < d->n_obj)
469         pEmr = (PEMREXTCREATEPEN) d->emf_obj[index].lpEMFR;
471     if (!pEmr)
472         return;
474     switch (pEmr->elp.elpPenStyle & PS_STYLE_MASK) {
475         case PS_USERSTYLE:
476         {
477             if (pEmr->elp.elpNumEntries) {
478                 d->style.stroke_dash.n_dash = pEmr->elp.elpNumEntries;
479                 if (d->style.stroke_dash.dash)
480                     delete[] d->style.stroke_dash.dash;
481                 d->style.stroke_dash.dash = new double[pEmr->elp.elpNumEntries];
482                 for (unsigned int i=0; i<pEmr->elp.elpNumEntries; i++) {
483                     d->style.stroke_dash.dash[i] = pix_to_size_point( d, pEmr->elp.elpStyleEntry[i] );
484                 }
485                 d->style.stroke_dasharray_set = 1;
486             } else {
487                 d->style.stroke_dasharray_set = 0;
488             }
489             break;
490         }
492         case PS_DASH:
493         case PS_DOT:
494         case PS_DASHDOT:
495         case PS_DASHDOTDOT:
496         {
497             int i = 0;
498             int penstyle = (pEmr->elp.elpPenStyle & PS_STYLE_MASK);
499             d->style.stroke_dash.n_dash =
500                 penstyle == PS_DASHDOTDOT ? 6 : penstyle == PS_DASHDOT ? 4 : 2;
501             if (d->style.stroke_dash.dash)
502                 delete[] d->style.stroke_dash.dash;
503             d->style.stroke_dash.dash = new double[d->style.stroke_dash.n_dash];
504             if (penstyle==PS_DASH || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
505                 d->style.stroke_dash.dash[i++] = 3;
506                 d->style.stroke_dash.dash[i++] = 2;
507             }
508             if (penstyle==PS_DOT || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
509                 d->style.stroke_dash.dash[i++] = 1;
510                 d->style.stroke_dash.dash[i++] = 2;
511             }
512             if (penstyle==PS_DASHDOTDOT) {
513                 d->style.stroke_dash.dash[i++] = 1;
514                 d->style.stroke_dash.dash[i++] = 2;
515             }
516             
517             d->style.stroke_dasharray_set = 1;
518             break;
519         }
520         
521         case PS_SOLID:
522         default:
523         {
524             d->style.stroke_dasharray_set = 0;
525             break;
526         }
527     }
529     switch (pEmr->elp.elpPenStyle & PS_ENDCAP_MASK) {
530         case PS_ENDCAP_ROUND:
531         {
532             d->style.stroke_linecap.computed = 1;
533             break;
534         }
535         case PS_ENDCAP_SQUARE:
536         {
537             d->style.stroke_linecap.computed = 2;
538             break;
539         }
540         case PS_ENDCAP_FLAT:
541         default:
542         {
543             d->style.stroke_linecap.computed = 0;
544             break;
545         }
546     }
548     switch (pEmr->elp.elpPenStyle & PS_JOIN_MASK) {
549         case PS_JOIN_BEVEL:
550         {
551             d->style.stroke_linejoin.computed = 2;
552             break;
553         }
554         case PS_JOIN_MITER:
555         {
556             d->style.stroke_linejoin.computed = 0;
557             break;
558         }
559         case PS_JOIN_ROUND:
560         default:
561         {
562             d->style.stroke_linejoin.computed = 1;
563             break;
564         }
565     }
567     d->stroke_set = true;
569     if (pEmr->elp.elpPenStyle == PS_NULL) {
570         d->style.stroke_width.value = 0;
571         d->stroke_set = false;
572     } else if (pEmr->elp.elpWidth) {
573         d->style.stroke_width.value = pix_to_size_point( d, pEmr->elp.elpWidth );
574     } else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
575         //d->style.stroke_width.value = 1.0;
576         d->style.stroke_width.value = pix_to_size_point( d, 1 );
577     }
579     double r, g, b;
580     r = SP_COLOR_U_TO_F( GetRValue(pEmr->elp.elpColor) );
581     g = SP_COLOR_U_TO_F( GetGValue(pEmr->elp.elpColor) );
582     b = SP_COLOR_U_TO_F( GetBValue(pEmr->elp.elpColor) );
584     d->style.stroke.value.color.set( r, g, b );
588 static void
589 select_brush(PEMF_CALLBACK_DATA d, int index)
591     PEMRCREATEBRUSHINDIRECT pEmr = NULL;
593     if (index >= 0 && index < d->n_obj)
594         pEmr = (PEMRCREATEBRUSHINDIRECT) d->emf_obj[index].lpEMFR;
596     if (!pEmr)
597         return;
599     if (pEmr->lb.lbStyle == BS_SOLID) {
600         double r, g, b;
601         r = SP_COLOR_U_TO_F( GetRValue(pEmr->lb.lbColor) );
602         g = SP_COLOR_U_TO_F( GetGValue(pEmr->lb.lbColor) );
603         b = SP_COLOR_U_TO_F( GetBValue(pEmr->lb.lbColor) );
604         d->style.fill.value.color.set( r, g, b );
605     }
607     d->fill_set = true;
611 static void
612 select_font(PEMF_CALLBACK_DATA d, int index)
614     PEMREXTCREATEFONTINDIRECTW pEmr = NULL;
616     if (index >= 0 && index < d->n_obj)
617         pEmr = (PEMREXTCREATEFONTINDIRECTW) d->emf_obj[index].lpEMFR;
619     if (!pEmr)
620         return;
622     d->style.font_size.computed = pix_to_size_point( d, pEmr->elfw.elfLogFont.lfHeight );
623     d->style.font_weight.value =
624         pEmr->elfw.elfLogFont.lfWeight == FW_THIN ? SP_CSS_FONT_WEIGHT_100 :
625         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRALIGHT ? SP_CSS_FONT_WEIGHT_200 :
626         pEmr->elfw.elfLogFont.lfWeight == FW_LIGHT ? SP_CSS_FONT_WEIGHT_300 :
627         pEmr->elfw.elfLogFont.lfWeight == FW_NORMAL ? SP_CSS_FONT_WEIGHT_400 :
628         pEmr->elfw.elfLogFont.lfWeight == FW_MEDIUM ? SP_CSS_FONT_WEIGHT_500 :
629         pEmr->elfw.elfLogFont.lfWeight == FW_SEMIBOLD ? SP_CSS_FONT_WEIGHT_600 :
630         pEmr->elfw.elfLogFont.lfWeight == FW_BOLD ? SP_CSS_FONT_WEIGHT_700 :
631         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRABOLD ? SP_CSS_FONT_WEIGHT_800 :
632         pEmr->elfw.elfLogFont.lfWeight == FW_HEAVY ? SP_CSS_FONT_WEIGHT_900 :
633         pEmr->elfw.elfLogFont.lfWeight == FW_NORMAL ? SP_CSS_FONT_WEIGHT_NORMAL :
634         pEmr->elfw.elfLogFont.lfWeight == FW_BOLD ? SP_CSS_FONT_WEIGHT_BOLD :
635         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRALIGHT ? SP_CSS_FONT_WEIGHT_LIGHTER :
636         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRABOLD ? SP_CSS_FONT_WEIGHT_BOLDER :
637         FW_NORMAL;
638     d->style.font_style.value = (pEmr->elfw.elfLogFont.lfItalic ? SP_CSS_FONT_STYLE_ITALIC : SP_CSS_FONT_STYLE_NORMAL);
639     d->style.text_decoration.underline = pEmr->elfw.elfLogFont.lfUnderline;
640     d->style.text_decoration.line_through = pEmr->elfw.elfLogFont.lfStrikeOut;
641     if (d->tstyle.font_family.value)
642         g_free(d->tstyle.font_family.value);
643     d->tstyle.font_family.value =
644         (gchar *) g_utf16_to_utf8( (gunichar2*) pEmr->elfw.elfLogFont.lfFaceName, -1, NULL, NULL, NULL );
647 static void
648 delete_object(PEMF_CALLBACK_DATA d, int index)
650     if (index >= 0 && index < d->n_obj) {
651         d->emf_obj[index].type = 0;
652         if (d->emf_obj[index].lpEMFR)
653             free(d->emf_obj[index].lpEMFR);
654         d->emf_obj[index].lpEMFR = NULL;
655     }
659 static void
660 insert_object(PEMF_CALLBACK_DATA d, int index, int type, ENHMETARECORD *pObj)
662     if (index >= 0 && index < d->n_obj) {
663         delete_object(d, index);
664         d->emf_obj[index].type = type;
665         d->emf_obj[index].lpEMFR = pObj;
666     }
669 static void
670 assert_empty_path(PEMF_CALLBACK_DATA d, const char *fun)
672     if (!d->path->empty()) {
673         // g_debug("emf-win32-inout: assert_empty_path failed for %s\n", fun);
675         *(d->outsvg) += "<!--\n";
676         *(d->outsvg) += "    <path \t";
677         output_style(d, EMR_STROKEPATH);
678         if (strstr(d->path->c_str(), "d=\"") == NULL) {
679             *(d->outsvg) += "d=\"";
680             *(d->outsvg) += "\n\t";
681         }
682         *(d->outsvg) += *(d->path);
683         *(d->outsvg) += " \" /> \n";
684         *(d->outsvg) += "-->\n";
686         *(d->path) = "";
687     }
691 static int CALLBACK
692 myEnhMetaFileProc(HDC hDC, HANDLETABLE *lpHTable, ENHMETARECORD *lpEMFR, int nObj, LPARAM lpData)
694     PEMF_CALLBACK_DATA d;
695     SVGOStringStream tmp_outsvg;
696     SVGOStringStream tmp_path;
697     SVGOStringStream tmp_str;
698     SVGOStringStream dbg_str;
700     d = (PEMF_CALLBACK_DATA) lpData;
702     if (d->pathless_stroke) {
703         if (lpEMFR->iType!=EMR_POLYBEZIERTO && lpEMFR->iType!=EMR_POLYBEZIERTO16 &&
704             lpEMFR->iType!=EMR_POLYLINETO && lpEMFR->iType!=EMR_POLYLINETO16 &&
705             lpEMFR->iType!=EMR_LINETO && lpEMFR->iType!=EMR_ARCTO &&
706             lpEMFR->iType!=EMR_SETBKCOLOR && lpEMFR->iType!=EMR_SETROP2 &&
707             lpEMFR->iType!=EMR_SETBKMODE)
708         {
709             *(d->outsvg) += "    <path ";
710             output_style(d, EMR_STROKEPATH);
711             *(d->outsvg) += "\n\t";
712             *(d->outsvg) += *(d->path);
713             *(d->outsvg) += " \" /> \n";
714             *(d->path) = "";
715             d->pathless_stroke = false;
716         }
717     }
719     switch (lpEMFR->iType)
720     {
721         case EMR_HEADER:
722         {
723             dbg_str << "<!-- EMR_HEADER -->\n";
725             *(d->outsvg) += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
727             if (d->pDesc) {
728                 *(d->outsvg) += "<!-- ";
729                 *(d->outsvg) += d->pDesc;
730                 *(d->outsvg) += " -->\n";
731             }
733             ENHMETAHEADER *pEmr = (ENHMETAHEADER *) lpEMFR;
734             tmp_outsvg << "<svg\n";
735             tmp_outsvg << "  xmlns:svg=\"http://www.w3.org/2000/svg\"\n";
736             tmp_outsvg << "  xmlns=\"http://www.w3.org/2000/svg\"\n";
737             tmp_outsvg << "  version=\"1.0\"\n";
739             d->xDPI = 2540;
740             d->yDPI = 2540;
742             d->PixelsInX = pEmr->rclFrame.right - pEmr->rclFrame.left;
743             d->PixelsInY = pEmr->rclFrame.bottom - pEmr->rclFrame.top;
745             d->MMX = d->PixelsInX / 100.0;
746             d->MMY = d->PixelsInY / 100.0;
748             d->PixelsOutX = d->MMX * PX_PER_MM;
749             d->PixelsOutY = d->MMY * PX_PER_MM;
750             
751             tmp_outsvg <<
752                 "  width=\"" << d->MMX << "mm\"\n" <<
753                 "  height=\"" << d->MMY << "mm\"\n";
754             tmp_outsvg <<
755                 "  id=\"" << (d->id++) << "\">\n";
757             tmp_outsvg <<
758                 "<g\n" <<
759                 "  id=\"" << (d->id++) << "\">\n";
761             if (pEmr->nHandles) {
762                 d->n_obj = pEmr->nHandles;
763                 d->emf_obj = new EMF_OBJECT[d->n_obj];
764                 
765                 // Init the new emf_obj list elements to null, provided the
766                 // dynamic allocation succeeded.
767                 if ( d->emf_obj != NULL )
768                 {
769                     for( unsigned int i=0; i < d->n_obj; ++i )
770                         d->emf_obj[i].lpEMFR = NULL;
771                 } //if
773             } else {
774                 d->emf_obj = NULL;
775             }
777             break;
778         }
779         case EMR_POLYBEZIER:
780         {
781             dbg_str << "<!-- EMR_POLYBEZIER -->\n";
783             PEMRPOLYBEZIER pEmr = (PEMRPOLYBEZIER) lpEMFR;
784             DWORD i,j;
786             if (pEmr->cptl<4)
787                 break;
789             if (!d->inpath) {
790                 assert_empty_path(d, "EMR_POLYBEZIER");
792                 *(d->outsvg) += "    <path ";
793                 output_style(d, EMR_STROKEPATH);
794                 *(d->outsvg) += "\n\td=\"";
795             }
797             tmp_str <<
798                 "\n\tM " <<
799                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
800                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y) << " ";
802             for (i=1; i<pEmr->cptl; ) {
803                 tmp_str << "\n\tC ";
804                 for (j=0; j<3 && i<pEmr->cptl; j++,i++) {
805                     tmp_str <<
806                         pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
807                         pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
808                 }
809             }
811             if (d->inpath) {
812                 tmp_path << tmp_str.str().c_str();
813             }
814             else {
815                 *(d->outsvg) += tmp_str.str().c_str();
816                 *(d->outsvg) += " \" /> \n";
817             }
819             break;
820         }
821         case EMR_POLYGON:
822         {
823             dbg_str << "<!-- EMR_POLYGON -->\n";
825             EMRPOLYGON *pEmr = (EMRPOLYGON *) lpEMFR;
826             DWORD i;
828             if (pEmr->cptl < 2)
829                 break;
831             assert_empty_path(d, "EMR_POLYGON");
833             *(d->outsvg) += "    <path ";
834             output_style(d, EMR_STROKEANDFILLPATH);
835             *(d->outsvg) += "\n\td=\"";
837             tmp_str <<
838                 "\n\tM " <<
839                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
840                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " ";
842             for (i=1; i<pEmr->cptl; i++) {
843                 tmp_str <<
844                     "\n\tL " <<
845                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
846                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
847             }
849             *(d->outsvg) += tmp_str.str().c_str();
850             *(d->outsvg) += " z \" /> \n";
852             break;
853         }
854         case EMR_POLYLINE:
855         {
856             dbg_str << "<!-- EMR_POLYLINE -->\n";
858             EMRPOLYLINE *pEmr = (EMRPOLYLINE *) lpEMFR;
859             DWORD i;
861             if (pEmr->cptl<2)
862                 break;
864             if (!d->inpath) {
865                 assert_empty_path(d, "EMR_POLYLINE");
867                 *(d->outsvg) += "    <path ";
868                 output_style(d, EMR_STROKEPATH);
869                 *(d->outsvg) += "\n\td=\"";
870             }
872             tmp_str <<
873                 "\n\tM " <<
874                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
875                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " ";
877             for (i=1; i<pEmr->cptl; i++) {
878                 tmp_str <<
879                     "\n\tL " <<
880                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
881                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
882             }
884             if (d->inpath) {
885                 tmp_path << tmp_str.str().c_str();
886             }
887             else {
888                 *(d->outsvg) += tmp_str.str().c_str();
889                 *(d->outsvg) += " \" /> \n";
890             }
892             break;
893         }
894         case EMR_POLYBEZIERTO:
895         {
896             dbg_str << "<!-- EMR_POLYBEZIERTO -->\n";
898             PEMRPOLYBEZIERTO pEmr = (PEMRPOLYBEZIERTO) lpEMFR;
899             DWORD i,j;
901             if (d->path->empty()) {
902                 d->pathless_stroke = true;
903                 *(d->path) = "d=\"";
904             }
906             for (i=0; i<pEmr->cptl;) {
907                 tmp_path << "\n\tC ";
908                 for (j=0; j<3 && i<pEmr->cptl; j++,i++) {
909                     tmp_path <<
910                         pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
911                         pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
912                 }
913             }
915             break;
916         }
917         case EMR_POLYLINETO:
918         {
919             dbg_str << "<!-- EMR_POLYLINETO -->\n";
921             PEMRPOLYLINETO pEmr = (PEMRPOLYLINETO) lpEMFR;
922             DWORD i;
924             if (d->path->empty()) {
925                 d->pathless_stroke = true;
926                 *(d->path) = "d=\"";
927             }
929             for (i=0; i<pEmr->cptl;i++) {
930                 tmp_path <<
931                     "\n\tL " <<
932                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
933                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
934             }
936             break;
937         }
938         case EMR_POLYPOLYLINE:
939         case EMR_POLYPOLYGON:
940         {
941             if (lpEMFR->iType == EMR_POLYPOLYLINE)
942                 dbg_str << "<!-- EMR_POLYPOLYLINE -->\n";
943             if (lpEMFR->iType == EMR_POLYPOLYGON)
944                 dbg_str << "<!-- EMR_POLYPOLYGON -->\n";
946             PEMRPOLYPOLYGON pEmr = (PEMRPOLYPOLYGON) lpEMFR;
947             unsigned int n, i, j;
949             if (!d->inpath) {
950                 assert_empty_path(d, lpEMFR->iType == EMR_POLYPOLYGON ? "EMR_POLYPOLYGON" : "EMR_POLYPOLYLINE");
952                 *(d->outsvg) += "    <path ";
953                 output_style(d, lpEMFR->iType==EMR_POLYPOLYGON ? EMR_STROKEANDFILLPATH : EMR_STROKEPATH);
954                 *(d->outsvg) += "\n\td=\"";
955             }
957             POINTL *aptl = (POINTL *) &pEmr->aPolyCounts[pEmr->nPolys];
959             i = 0;
960             for (n=0; n<pEmr->nPolys && i<pEmr->cptl; n++) {
961                 SVGOStringStream poly_path;
963                 poly_path << "\n\tM " <<
964                     pix_to_x_point( d, aptl[i].x, aptl[i].y ) << " " <<
965                     pix_to_y_point( d, aptl[i].x, aptl[i].y ) << " ";
966                 i++;
968                 for (j=1; j<pEmr->aPolyCounts[n] && i<pEmr->cptl; j++) {
969                     poly_path << "\n\tL " <<
970                         pix_to_x_point( d, aptl[i].x, aptl[i].y ) << " " <<
971                         pix_to_y_point( d, aptl[i].x, aptl[i].y ) << " ";
972                     i++;
973                 }
975                 tmp_str << poly_path.str().c_str();
976                 if (lpEMFR->iType == EMR_POLYPOLYGON)
977                     tmp_str << " z";
978                 tmp_str << " \n";
979             }
981             if (d->inpath) {
982                 tmp_path << tmp_str.str().c_str();
983             }
984             else {
985                 *(d->outsvg) += tmp_str.str().c_str();
986                 *(d->outsvg) += " \" /> \n";
987             }
989             break;
990         }
991         case EMR_SETWINDOWEXTEX:
992         {
993             dbg_str << "<!-- EMR_SETWINDOWEXTEX -->\n";
995             PEMRSETWINDOWEXTEX pEmr = (PEMRSETWINDOWEXTEX) lpEMFR;
997             d->sizeWnd = pEmr->szlExtent;
999             if (!d->sizeWnd.cx || !d->sizeWnd.cy) {
1000                 d->sizeWnd = d->sizeView;
1001                 if (!d->sizeWnd.cx || !d->sizeWnd.cy) {
1002                     d->sizeWnd.cx = d->PixelsOutX;
1003                     d->sizeWnd.cy = d->PixelsOutY;
1004                 }
1005             }
1007             if (!d->sizeView.cx || !d->sizeView.cy) {
1008                 d->sizeView = d->sizeWnd;
1009             }
1011             d->PixelsInX = d->sizeWnd.cx;
1012             d->PixelsInY = d->sizeWnd.cy;
1013             
1014             if (d->PixelsInX && d->PixelsInY) {
1015                 d->ScaleInX = (double) d->sizeView.cx / (double) d->PixelsInX;
1016                 d->ScaleInY = (double) d->sizeView.cy / (double) d->PixelsInY;
1017             }
1018             else {
1019                 d->ScaleInX = 1;
1020                 d->ScaleInY = 1;
1021             }
1023             if (d->sizeView.cx && d->sizeView.cy) {
1024                 d->ScaleOutX = (double) d->PixelsOutX / (double) d->sizeView.cx;
1025                 d->ScaleOutY = (double) d->PixelsOutY / (double) d->sizeView.cy;
1026             }
1027             else {
1028                 d->ScaleOutX = DEVICESCALE;
1029                 d->ScaleOutY = DEVICESCALE;
1030             }
1032             break;
1033         }
1034         case EMR_SETWINDOWORGEX:
1035         {
1036             dbg_str << "<!-- EMR_SETWINDOWORGEX -->\n";
1038             PEMRSETWINDOWORGEX pEmr = (PEMRSETWINDOWORGEX) lpEMFR;
1039             d->winorg = pEmr->ptlOrigin;
1040             break;
1041         }
1042         case EMR_SETVIEWPORTEXTEX:
1043         {
1044             dbg_str << "<!-- EMR_SETVIEWPORTEXTEX -->\n";
1046             PEMRSETVIEWPORTEXTEX pEmr = (PEMRSETVIEWPORTEXTEX) lpEMFR;
1048             d->sizeView = pEmr->szlExtent;
1050             if (!d->sizeView.cx || !d->sizeView.cy) {
1051                 d->sizeView = d->sizeWnd;
1052                 if (!d->sizeView.cx || !d->sizeView.cy) {
1053                     d->sizeView.cx = d->PixelsOutX;
1054                     d->sizeView.cy = d->PixelsOutY;
1055                 }
1056             }
1058             if (!d->sizeWnd.cx || !d->sizeWnd.cy) {
1059                 d->sizeWnd = d->sizeView;
1060             }
1062             d->PixelsInX = d->sizeWnd.cx;
1063             d->PixelsInY = d->sizeWnd.cy;
1064             
1065             if (d->PixelsInX && d->PixelsInY) {
1066                 d->ScaleInX = (double) d->sizeView.cx / (double) d->PixelsInX;
1067                 d->ScaleInY = (double) d->sizeView.cy / (double) d->PixelsInY;
1068             }
1069             else {
1070                 d->ScaleInX = 1;
1071                 d->ScaleInY = 1;
1072             }
1074             if (d->sizeView.cx && d->sizeView.cy) {
1075                 d->ScaleOutX = (double) d->PixelsOutX / (double) d->sizeView.cx;
1076                 d->ScaleOutY = (double) d->PixelsOutY / (double) d->sizeView.cy;
1077             }
1078             else {
1079                 d->ScaleOutX = DEVICESCALE;
1080                 d->ScaleOutY = DEVICESCALE;
1081             }
1083             break;
1084         }
1085         case EMR_SETVIEWPORTORGEX:
1086         {
1087             dbg_str << "<!-- EMR_SETVIEWPORTORGEX -->\n";
1089             PEMRSETVIEWPORTORGEX pEmr = (PEMRSETVIEWPORTORGEX) lpEMFR;
1090             d->vieworg = pEmr->ptlOrigin;
1091             break;
1092         }
1093         case EMR_SETBRUSHORGEX:
1094             dbg_str << "<!-- EMR_SETBRUSHORGEX -->\n";
1095             break;
1096         case EMR_EOF:
1097         {
1098             dbg_str << "<!-- EMR_EOF -->\n";
1100             assert_empty_path(d, "EMR_EOF");
1101             tmp_outsvg << "</g>\n";
1102             tmp_outsvg << "</svg>\n";
1103             break;
1104         }
1105         case EMR_SETPIXELV:
1106             dbg_str << "<!-- EMR_SETPIXELV -->\n";
1107             break;
1108         case EMR_SETMAPPERFLAGS:
1109             dbg_str << "<!-- EMR_SETMAPPERFLAGS -->\n";
1110             break;
1111         case EMR_SETMAPMODE:
1112             dbg_str << "<!-- EMR_SETMAPMODE -->\n";
1113             break;
1114         case EMR_SETBKMODE:
1115             dbg_str << "<!-- EMR_SETBKMODE -->\n";
1116             break;
1117         case EMR_SETPOLYFILLMODE:
1118         {
1119             dbg_str << "<!-- EMR_SETPOLYFILLMODE -->\n";
1121             PEMRSETPOLYFILLMODE pEmr = (PEMRSETPOLYFILLMODE) lpEMFR;
1122             d->style.fill_rule.value =
1123                 (pEmr->iMode == ALTERNATE ? 0 :
1124                  pEmr->iMode == WINDING ? 1 : 0);
1125             break;
1126         }
1127         case EMR_SETROP2:
1128             dbg_str << "<!-- EMR_SETROP2 -->\n";
1129             break;
1130         case EMR_SETSTRETCHBLTMODE:
1131             dbg_str << "<!-- EMR_SETSTRETCHBLTMODE -->\n";
1132             break;
1133         case EMR_SETTEXTALIGN:
1134         {
1135             dbg_str << "<!-- EMR_SETTEXTALIGN -->\n";
1137             PEMRSETTEXTALIGN pEmr = (PEMRSETTEXTALIGN) lpEMFR;
1138             d->textAlign = pEmr->iMode;
1139             break;
1140         }
1141         case EMR_SETCOLORADJUSTMENT:
1142             dbg_str << "<!-- EMR_SETCOLORADJUSTMENT -->\n";
1143             break;
1144         case EMR_SETTEXTCOLOR:
1145         {
1146             dbg_str << "<!-- EMR_SETTEXTCOLOR -->\n";
1148             PEMRSETTEXTCOLOR pEmr = (PEMRSETTEXTCOLOR) lpEMFR;
1149             d->textColor = pEmr->crColor;
1150             d->textColorSet = true;
1151             break;
1152         }
1153         case EMR_SETBKCOLOR:
1154             dbg_str << "<!-- EMR_SETBKCOLOR -->\n";
1155             break;
1156         case EMR_OFFSETCLIPRGN:
1157             dbg_str << "<!-- EMR_OFFSETCLIPRGN -->\n";
1158             break;
1159         case EMR_MOVETOEX:
1160         {
1161             dbg_str << "<!-- EMR_MOVETOEX -->\n";
1163             PEMRMOVETOEX pEmr = (PEMRMOVETOEX) lpEMFR;
1165             if (d->path->empty()) {
1166                 d->pathless_stroke = true;
1167                 *(d->path) = "d=\"";
1168             }
1170             tmp_path <<
1171                 "\n\tM " <<
1172                 pix_to_x_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " " <<
1173                 pix_to_y_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " ";
1174             break;
1175         }
1176         case EMR_SETMETARGN:
1177             dbg_str << "<!-- EMR_SETMETARGN -->\n";
1178             break;
1179         case EMR_EXCLUDECLIPRECT:
1180             dbg_str << "<!-- EMR_EXCLUDECLIPRECT -->\n";
1181             break;
1182         case EMR_INTERSECTCLIPRECT:
1183             dbg_str << "<!-- EMR_INTERSECTCLIPRECT -->\n";
1184             break;
1185         case EMR_SCALEVIEWPORTEXTEX:
1186             dbg_str << "<!-- EMR_SCALEVIEWPORTEXTEX -->\n";
1187             break;
1188         case EMR_SCALEWINDOWEXTEX:
1189             dbg_str << "<!-- EMR_SCALEWINDOWEXTEX -->\n";
1190             break;
1191         case EMR_SAVEDC:
1192             dbg_str << "<!-- EMR_SAVEDC -->\n";
1193             break;
1194         case EMR_RESTOREDC:
1195             dbg_str << "<!-- EMR_RESTOREDC -->\n";
1196             break;
1197         case EMR_SETWORLDTRANSFORM:
1198         {
1199             dbg_str << "<!-- EMR_SETWORLDTRANSFORM -->\n";
1201             PEMRSETWORLDTRANSFORM pEmr = (PEMRSETWORLDTRANSFORM) lpEMFR;
1202             d->worldTransform = pEmr->xform;
1203             break;
1204         }
1205         case EMR_MODIFYWORLDTRANSFORM:
1206         {
1207             dbg_str << "<!-- EMR_MODIFYWORLDTRANSFORM -->\n";
1209             PEMRMODIFYWORLDTRANSFORM pEmr = (PEMRMODIFYWORLDTRANSFORM) lpEMFR;
1210             switch (pEmr->iMode)
1211             {
1212                 case MWT_IDENTITY:
1213                     d->worldTransform.eM11 = 1.0;
1214                     d->worldTransform.eM12 = 0.0;
1215                     d->worldTransform.eM21 = 0.0;
1216                     d->worldTransform.eM22 = 1.0;
1217                     d->worldTransform.eDx  = 0.0;
1218                     d->worldTransform.eDy  = 0.0;
1219                     break;
1220                 case MWT_LEFTMULTIPLY:
1221                 {
1222 //                    d->worldTransform = pEmr->xform * worldTransform;
1224                     float a11 = pEmr->xform.eM11;
1225                     float a12 = pEmr->xform.eM12;
1226                     float a13 = 0.0;
1227                     float a21 = pEmr->xform.eM21;
1228                     float a22 = pEmr->xform.eM22;
1229                     float a23 = 0.0;
1230                     float a31 = pEmr->xform.eDx;
1231                     float a32 = pEmr->xform.eDy;
1232                     float a33 = 1.0;
1234                     float b11 = d->worldTransform.eM11;
1235                     float b12 = d->worldTransform.eM12;
1236                     //float b13 = 0.0;
1237                     float b21 = d->worldTransform.eM21;
1238                     float b22 = d->worldTransform.eM22;
1239                     //float b23 = 0.0;
1240                     float b31 = d->worldTransform.eDx;
1241                     float b32 = d->worldTransform.eDy;
1242                     //float b33 = 1.0;
1244                     float c11 = a11*b11 + a12*b21 + a13*b31;;
1245                     float c12 = a11*b12 + a12*b22 + a13*b32;;
1246                     //float c13 = a11*b13 + a12*b23 + a13*b33;;
1247                     float c21 = a21*b11 + a22*b21 + a23*b31;;
1248                     float c22 = a21*b12 + a22*b22 + a23*b32;;
1249                     //float c23 = a21*b13 + a22*b23 + a23*b33;;
1250                     float c31 = a31*b11 + a32*b21 + a33*b31;;
1251                     float c32 = a31*b12 + a32*b22 + a33*b32;;
1252                     //float c33 = a31*b13 + a32*b23 + a33*b33;;
1254                     d->worldTransform.eM11 = c11;;
1255                     d->worldTransform.eM12 = c12;;
1256                     d->worldTransform.eM21 = c21;;
1257                     d->worldTransform.eM22 = c22;;
1258                     d->worldTransform.eDx = c31;
1259                     d->worldTransform.eDy = c32;
1260                     
1261                     break;
1262                 }
1263                 case MWT_RIGHTMULTIPLY:
1264                 {
1265 //                    d->worldTransform = worldTransform * pEmr->xform;
1267                     float a11 = d->worldTransform.eM11;
1268                     float a12 = d->worldTransform.eM12;
1269                     float a13 = 0.0;
1270                     float a21 = d->worldTransform.eM21;
1271                     float a22 = d->worldTransform.eM22;
1272                     float a23 = 0.0;
1273                     float a31 = d->worldTransform.eDx;
1274                     float a32 = d->worldTransform.eDy;
1275                     float a33 = 1.0;
1277                     float b11 = pEmr->xform.eM11;
1278                     float b12 = pEmr->xform.eM12;
1279                     //float b13 = 0.0;
1280                     float b21 = pEmr->xform.eM21;
1281                     float b22 = pEmr->xform.eM22;
1282                     //float b23 = 0.0;
1283                     float b31 = pEmr->xform.eDx;
1284                     float b32 = pEmr->xform.eDy;
1285                     //float b33 = 1.0;
1287                     float c11 = a11*b11 + a12*b21 + a13*b31;;
1288                     float c12 = a11*b12 + a12*b22 + a13*b32;;
1289                     //float c13 = a11*b13 + a12*b23 + a13*b33;;
1290                     float c21 = a21*b11 + a22*b21 + a23*b31;;
1291                     float c22 = a21*b12 + a22*b22 + a23*b32;;
1292                     //float c23 = a21*b13 + a22*b23 + a23*b33;;
1293                     float c31 = a31*b11 + a32*b21 + a33*b31;;
1294                     float c32 = a31*b12 + a32*b22 + a33*b32;;
1295                     //float c33 = a31*b13 + a32*b23 + a33*b33;;
1297                     d->worldTransform.eM11 = c11;;
1298                     d->worldTransform.eM12 = c12;;
1299                     d->worldTransform.eM21 = c21;;
1300                     d->worldTransform.eM22 = c22;;
1301                     d->worldTransform.eDx = c31;
1302                     d->worldTransform.eDy = c32;
1304                     break;
1305                 }
1306 //                case MWT_SET:
1307                 default:
1308                     d->worldTransform = pEmr->xform;
1309                     break;
1310             }
1311             break;
1312         }
1313         case EMR_SELECTOBJECT:
1314         {
1315             dbg_str << "<!-- EMR_SELECTOBJECT -->\n";
1317             PEMRSELECTOBJECT pEmr = (PEMRSELECTOBJECT) lpEMFR;
1318             unsigned int index = pEmr->ihObject;
1320             if (index >= ENHMETA_STOCK_OBJECT) {
1321                 index -= ENHMETA_STOCK_OBJECT;
1322                 switch (index) {
1323                     case NULL_BRUSH:
1324                         d->fill_set = false;
1325                         break;
1326                     case BLACK_BRUSH:
1327                     case DKGRAY_BRUSH:
1328                     case GRAY_BRUSH:
1329                     case LTGRAY_BRUSH:
1330                     case WHITE_BRUSH:
1331                     {
1332                         float val = 0;
1333                         switch (index) {
1334                             case BLACK_BRUSH:
1335                                 val = 0.0 / 255.0;
1336                                 break;
1337                             case DKGRAY_BRUSH:
1338                                 val = 64.0 / 255.0;
1339                                 break;
1340                             case GRAY_BRUSH:
1341                                 val = 128.0 / 255.0;
1342                                 break;
1343                             case LTGRAY_BRUSH:
1344                                 val = 192.0 / 255.0;
1345                                 break;
1346                             case WHITE_BRUSH:
1347                                 val = 255.0 / 255.0;
1348                                 break;
1349                         }
1350                         d->style.fill.value.color.set( val, val, val );
1352                         d->fill_set = true;
1353                         break;
1354                     }
1355                     case NULL_PEN:
1356                         d->stroke_set = false;
1357                         break;
1358                     case BLACK_PEN:
1359                     case WHITE_PEN:
1360                     {
1361                         float val = index == BLACK_PEN ? 0 : 1;
1362                         d->style.stroke_dasharray_set = 0;
1363                         d->style.stroke_width.value = 1.0;
1364                         d->style.stroke.value.color.set( val, val, val );
1366                         d->stroke_set = true;
1368                         break;
1369                     }
1370                 }
1371             } else {
1372                 if ( /*index >= 0 &&*/ index < (unsigned int) d->n_obj) {
1373                     switch (d->emf_obj[index].type)
1374                     {
1375                         case EMR_CREATEPEN:
1376                             select_pen(d, index);
1377                             break;
1378                         case EMR_CREATEBRUSHINDIRECT:
1379                             select_brush(d, index);
1380                             break;
1381                         case EMR_EXTCREATEPEN:
1382                             select_extpen(d, index);
1383                             break;
1384                         case EMR_EXTCREATEFONTINDIRECTW:
1385                             select_font(d, index);
1386                             break;
1387                     }
1388                 }
1389             }
1390             break;
1391         }
1392         case EMR_CREATEPEN:
1393         {
1394             dbg_str << "<!-- EMR_CREATEPEN -->\n";
1396             PEMRCREATEPEN pEmr = (PEMRCREATEPEN) lpEMFR;
1397             int index = pEmr->ihPen;
1399             EMRCREATEPEN *pPen =
1400                 (EMRCREATEPEN *) malloc( sizeof(EMRCREATEPEN) );
1401             pPen->lopn = pEmr->lopn;
1402             insert_object(d, index, EMR_CREATEPEN, (ENHMETARECORD *) pPen);
1404             break;
1405         }
1406         case EMR_CREATEBRUSHINDIRECT:
1407         {
1408             dbg_str << "<!-- EMR_CREATEBRUSHINDIRECT -->\n";
1410             PEMRCREATEBRUSHINDIRECT pEmr = (PEMRCREATEBRUSHINDIRECT) lpEMFR;
1411             int index = pEmr->ihBrush;
1413             EMRCREATEBRUSHINDIRECT *pBrush =
1414                 (EMRCREATEBRUSHINDIRECT *) malloc( sizeof(EMRCREATEBRUSHINDIRECT) );
1415             pBrush->lb = pEmr->lb;
1416             insert_object(d, index, EMR_CREATEBRUSHINDIRECT, (ENHMETARECORD *) pBrush);
1418             break;
1419         }
1420         case EMR_DELETEOBJECT:
1421             dbg_str << "<!-- EMR_DELETEOBJECT -->\n";
1422             break;
1423         case EMR_ANGLEARC:
1424             dbg_str << "<!-- EMR_ANGLEARC -->\n";
1425             break;
1426         case EMR_ELLIPSE:
1427         {
1428             dbg_str << "<!-- EMR_ELLIPSE -->\n";
1430             PEMRELLIPSE pEmr = (PEMRELLIPSE) lpEMFR;
1431             RECTL rclBox = pEmr->rclBox;
1433             double l = pix_to_x_point( d, pEmr->rclBox.left, pEmr->rclBox.top );
1434             double t = pix_to_y_point( d, pEmr->rclBox.left, pEmr->rclBox.top );
1435             double r = pix_to_x_point( d, pEmr->rclBox.right, pEmr->rclBox.bottom );
1436             double b = pix_to_y_point( d, pEmr->rclBox.right, pEmr->rclBox.bottom );
1438             double cx = (l + r) / 2.0;
1439             double cy = (t + b) / 2.0;
1440             double rx = fabs(l - r) / 2.0;
1441             double ry = fabs(t - b) / 2.0;
1443             SVGOStringStream tmp_ellipse;
1444             tmp_ellipse << "cx=\"" << cx << "\" ";
1445             tmp_ellipse << "cy=\"" << cy << "\" ";
1446             tmp_ellipse << "rx=\"" << rx << "\" ";
1447             tmp_ellipse << "ry=\"" << ry << "\" ";
1449             assert_empty_path(d, "EMR_ELLIPSE");
1451             *(d->outsvg) += "    <ellipse ";
1452             output_style(d, lpEMFR->iType);
1453             *(d->outsvg) += "\n\t";
1454             *(d->outsvg) += tmp_ellipse.str().c_str();
1455             *(d->outsvg) += "/> \n";
1456             *(d->path) = "";
1457             break;
1458         }
1459         case EMR_RECTANGLE:
1460         {
1461             dbg_str << "<!-- EMR_RECTANGLE -->\n";
1463             PEMRRECTANGLE pEmr = (PEMRRECTANGLE) lpEMFR;
1464             RECTL rc = pEmr->rclBox;
1466             double l = pix_to_x_point( d, rc.left, rc.top );
1467             double t = pix_to_y_point( d, rc.left, rc.top );
1468             double r = pix_to_x_point( d, rc.right, rc.bottom );
1469             double b = pix_to_y_point( d, rc.right, rc.bottom );
1471             SVGOStringStream tmp_rectangle;
1472             tmp_rectangle << "d=\"";
1473             tmp_rectangle << "\n\tM " << l << " " << t << " ";
1474             tmp_rectangle << "\n\tL " << r << " " << t << " ";
1475             tmp_rectangle << "\n\tL " << r << " " << b << " ";
1476             tmp_rectangle << "\n\tL " << l << " " << b << " ";
1477             tmp_rectangle << "\n\tz";
1479             assert_empty_path(d, "EMR_RECTANGLE");
1481             *(d->outsvg) += "    <path ";
1482             output_style(d, lpEMFR->iType);
1483             *(d->outsvg) += "\n\t";
1484             *(d->outsvg) += tmp_rectangle.str().c_str();
1485             *(d->outsvg) += " \" /> \n";
1486             *(d->path) = "";
1487             break;
1488         }
1489         case EMR_ROUNDRECT:
1490             dbg_str << "<!-- EMR_ROUNDRECT -->\n";
1491             break;
1492         case EMR_ARC:
1493             dbg_str << "<!-- EMR_ARC -->\n";
1494             break;
1495         case EMR_CHORD:
1496             dbg_str << "<!-- EMR_CHORD -->\n";
1497             break;
1498         case EMR_PIE:
1499             dbg_str << "<!-- EMR_PIE -->\n";
1500             break;
1501         case EMR_SELECTPALETTE:
1502             dbg_str << "<!-- EMR_SELECTPALETTE -->\n";
1503             break;
1504         case EMR_CREATEPALETTE:
1505             dbg_str << "<!-- EMR_CREATEPALETTE -->\n";
1506             break;
1507         case EMR_SETPALETTEENTRIES:
1508             dbg_str << "<!-- EMR_SETPALETTEENTRIES -->\n";
1509             break;
1510         case EMR_RESIZEPALETTE:
1511             dbg_str << "<!-- EMR_RESIZEPALETTE -->\n";
1512             break;
1513         case EMR_REALIZEPALETTE:
1514             dbg_str << "<!-- EMR_REALIZEPALETTE -->\n";
1515             break;
1516         case EMR_EXTFLOODFILL:
1517             dbg_str << "<!-- EMR_EXTFLOODFILL -->\n";
1518             break;
1519         case EMR_LINETO:
1520         {
1521             dbg_str << "<!-- EMR_LINETO -->\n";
1523             PEMRLINETO pEmr = (PEMRLINETO) lpEMFR;
1525             if (d->path->empty()) {
1526                 d->pathless_stroke = true;
1527                 *(d->path) = "d=\"";
1528             }
1530             tmp_path <<
1531                 "\n\tL " <<
1532                 pix_to_x_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " " <<
1533                 pix_to_y_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " ";
1534             break;
1535         }
1536         case EMR_ARCTO:
1537             dbg_str << "<!-- EMR_ARCTO -->\n";
1538             break;
1539         case EMR_POLYDRAW:
1540             dbg_str << "<!-- EMR_POLYDRAW -->\n";
1541             break;
1542         case EMR_SETARCDIRECTION:
1543             dbg_str << "<!-- EMR_SETARCDIRECTION -->\n";
1544             break;
1545         case EMR_SETMITERLIMIT:
1546         {
1547             dbg_str << "<!-- EMR_SETMITERLIMIT -->\n";
1549             PEMRSETMITERLIMIT pEmr = (PEMRSETMITERLIMIT) lpEMFR;
1550             d->style.stroke_miterlimit.value = pix_to_size_point( d, pEmr->eMiterLimit );
1552             if (d->style.stroke_miterlimit.value < 1)
1553                 d->style.stroke_miterlimit.value = 1.0;
1555             break;
1556         }
1557         case EMR_BEGINPATH:
1558         {
1559             dbg_str << "<!-- EMR_BEGINPATH -->\n";
1561             tmp_path << "d=\"";
1562             *(d->path) = "";
1563             d->inpath = true;
1564             break;
1565         }
1566         case EMR_ENDPATH:
1567         {
1568             dbg_str << "<!-- EMR_ENDPATH -->\n";
1570             tmp_path << "\"";
1571             d->inpath = false;
1572             break;
1573         }
1574         case EMR_CLOSEFIGURE:
1575         {
1576             dbg_str << "<!-- EMR_CLOSEFIGURE -->\n";
1578             tmp_path << "\n\tz";
1579             break;
1580         }
1581         case EMR_FILLPATH:
1582         case EMR_STROKEANDFILLPATH:
1583         case EMR_STROKEPATH:
1584         {
1585             if (lpEMFR->iType == EMR_FILLPATH)
1586                 dbg_str << "<!-- EMR_FILLPATH -->\n";
1587             if (lpEMFR->iType == EMR_STROKEANDFILLPATH)
1588                 dbg_str << "<!-- EMR_STROKEANDFILLPATH -->\n";
1589             if (lpEMFR->iType == EMR_STROKEPATH)
1590                 dbg_str << "<!-- EMR_STROKEPATH -->\n";
1592             *(d->outsvg) += "    <path ";
1593             output_style(d, lpEMFR->iType);
1594             *(d->outsvg) += "\n\t";
1595             *(d->outsvg) += *(d->path);
1596             *(d->outsvg) += " /> \n";
1597             *(d->path) = "";
1598             break;
1599         }
1600         case EMR_FLATTENPATH:
1601             dbg_str << "<!-- EMR_FLATTENPATH -->\n";
1602             break;
1603         case EMR_WIDENPATH:
1604             dbg_str << "<!-- EMR_WIDENPATH -->\n";
1605             break;
1606         case EMR_SELECTCLIPPATH:
1607             dbg_str << "<!-- EMR_SELECTCLIPPATH -->\n";
1608             break;
1609         case EMR_ABORTPATH:
1610             dbg_str << "<!-- EMR_ABORTPATH -->\n";
1611             break;
1612         case EMR_GDICOMMENT:
1613         {
1614             dbg_str << "<!-- EMR_GDICOMMENT -->\n";
1615             
1616             PEMRGDICOMMENT pEmr = (PEMRGDICOMMENT) lpEMFR;
1618             CHAR *szTxt = (CHAR *) pEmr->Data;
1620             for (DWORD i = 0; i < pEmr->cbData; i++) {
1621                 if ( *szTxt) {
1622                     if ( *szTxt >= ' ' && *szTxt < 'z' && *szTxt != '<' && *szTxt != '>' ) {
1623                         tmp_str << *szTxt;
1624                     }
1625                     szTxt++;
1626                 }
1627             }
1629             if (0 && strlen(tmp_str.str().c_str())) {
1630                 tmp_outsvg << "    <!-- \"";
1631                 tmp_outsvg << tmp_str.str().c_str();
1632                 tmp_outsvg << "\" -->\n";
1633             }
1634             
1635             break;
1636         }
1637         case EMR_FILLRGN:
1638             dbg_str << "<!-- EMR_FILLRGN -->\n";
1639             break;
1640         case EMR_FRAMERGN:
1641             dbg_str << "<!-- EMR_FRAMERGN -->\n";
1642             break;
1643         case EMR_INVERTRGN:
1644             dbg_str << "<!-- EMR_INVERTRGN -->\n";
1645             break;
1646         case EMR_PAINTRGN:
1647             dbg_str << "<!-- EMR_PAINTRGN -->\n";
1648             break;
1649         case EMR_EXTSELECTCLIPRGN:
1650             dbg_str << "<!-- EMR_EXTSELECTCLIPRGN -->\n";
1651             break;
1652         case EMR_BITBLT:
1653             dbg_str << "<!-- EMR_BITBLT -->\n";
1654             break;
1655         case EMR_STRETCHBLT:
1656             dbg_str << "<!-- EMR_STRETCHBLT -->\n";
1657             break;
1658         case EMR_MASKBLT:
1659             dbg_str << "<!-- EMR_MASKBLT -->\n";
1660             break;
1661         case EMR_PLGBLT:
1662             dbg_str << "<!-- EMR_PLGBLT -->\n";
1663             break;
1664         case EMR_SETDIBITSTODEVICE:
1665             dbg_str << "<!-- EMR_SETDIBITSTODEVICE -->\n";
1666             break;
1667         case EMR_STRETCHDIBITS:
1668             dbg_str << "<!-- EMR_STRETCHDIBITS -->\n";
1669             break;
1670         case EMR_EXTCREATEFONTINDIRECTW:
1671         {
1672             dbg_str << "<!-- EMR_EXTCREATEFONTINDIRECTW -->\n";
1674             PEMREXTCREATEFONTINDIRECTW pEmr = (PEMREXTCREATEFONTINDIRECTW) lpEMFR;
1675             int index = pEmr->ihFont;
1677             EMREXTCREATEFONTINDIRECTW *pFont =
1678                 (EMREXTCREATEFONTINDIRECTW *) malloc( sizeof(EMREXTCREATEFONTINDIRECTW) );
1679             pFont->elfw = pEmr->elfw;
1680             insert_object(d, index, EMR_EXTCREATEFONTINDIRECTW, (ENHMETARECORD *) pFont);
1682             break;
1683         }
1684         case EMR_EXTTEXTOUTA:
1685         {
1686             dbg_str << "<!-- EMR_EXTTEXTOUTA -->\n";
1687             break;
1688         }
1689         case EMR_EXTTEXTOUTW:
1690         {
1691             dbg_str << "<!-- EMR_EXTTEXTOUTW -->\n";
1693             PEMREXTTEXTOUTW pEmr = (PEMREXTTEXTOUTW) lpEMFR;
1695             double x1 = pEmr->emrtext.ptlReference.x;
1696             double y1 = pEmr->emrtext.ptlReference.y;
1697             
1698             if (!(d->textAlign & TA_BOTTOM))
1699                 y1 += fabs(d->style.font_size.computed);
1700             
1701             double x = pix_to_x_point(d, x1, y1);
1702             double y = pix_to_y_point(d, x1, y1);
1704             wchar_t *wide_text = (wchar_t *) ((char *) pEmr + pEmr->emrtext.offString);
1706             gchar *ansi_text =
1707                 (gchar *) g_utf16_to_utf8( (gunichar2 *) wide_text, pEmr->emrtext.nChars, NULL, NULL, NULL );
1709             if (ansi_text) {
1710                 gchar *p = ansi_text;
1711                 while (*p) {
1712                     if (*p < 32 || *p >= 127) {
1713                         g_free(ansi_text);
1714                         ansi_text = g_strdup("");
1715                         break;
1716                     }
1717                     p++;
1718                 }
1720                 SVGOStringStream ts;
1722                 gchar *escaped_text = g_markup_escape_text(ansi_text, -1);
1724                 float text_rgb[3];
1725                 sp_color_get_rgb_floatv( &(d->style.fill.value.color), text_rgb );
1727                 if (!d->textColorSet) {
1728                     d->textColor = RGB(SP_COLOR_F_TO_U(text_rgb[0]),
1729                                        SP_COLOR_F_TO_U(text_rgb[1]),
1730                                        SP_COLOR_F_TO_U(text_rgb[2]));
1731                 }
1733                 char tmp[128];
1734                 snprintf(tmp, 127,
1735                          "fill:#%02x%02x%02x;",
1736                          GetRValue(d->textColor),
1737                          GetGValue(d->textColor),
1738                          GetBValue(d->textColor));
1740                 bool i = (d->style.font_style.value == SP_CSS_FONT_STYLE_ITALIC);
1741                 //bool o = (d->style.font_style.value == SP_CSS_FONT_STYLE_OBLIQUE);
1742                 bool b = (d->style.font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) ||
1743                     (d->style.font_weight.value >= SP_CSS_FONT_WEIGHT_500 && d->style.font_weight.value <= SP_CSS_FONT_WEIGHT_900);
1744                 int lcr = ((d->textAlign & TA_CENTER) == TA_CENTER) ? 2 : ((d->textAlign & TA_RIGHT) == TA_RIGHT) ? 1 : 0;
1746                 assert_empty_path(d, "EMR_EXTTEXTOUTW");
1748                 ts << "    <text\n";
1749                 ts << "        id=\"" << (d->id++) << "\"\n";
1750                 ts << "        xml:space=\"preserve\"\n";
1751                 ts << "        x=\"" << x << "\"\n";
1752                 ts << "        y=\"" << y << "\"\n";
1753                 ts << "        style=\""
1754                    << "font-size:" << fabs(d->style.font_size.computed) << "px;"
1755                    << tmp
1756                    << "font-style:" << (i ? "italic" : "normal") << ";"
1757                    << "font-weight:" << (b ? "bold" : "normal") << ";"
1758                    << "text-align:" << (lcr==2 ? "center" : lcr==1 ? "end" : "start") << ";"
1759                    << "text-anchor:" << (lcr==2 ? "middle" : lcr==1 ? "end" : "start") << ";"
1760                    << "font-family:" << d->tstyle.font_family.value << ";"
1761                    << "\"\n";
1762                 ts << "    >";
1763                 ts << escaped_text;
1764                 ts << "</text>\n";
1765                 
1766                 *(d->outsvg) += ts.str().c_str();
1767                 
1768                 g_free(escaped_text);
1769                 g_free(ansi_text);
1770             }
1771             
1772             break;
1773         }
1774         case EMR_POLYBEZIER16:
1775         {
1776             dbg_str << "<!-- EMR_POLYBEZIER16 -->\n";
1778             PEMRPOLYBEZIER16 pEmr = (PEMRPOLYBEZIER16) lpEMFR;
1779             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1780             DWORD i,j;
1782             if (pEmr->cpts<4)
1783                 break;
1785             if (!d->inpath) {
1786                 assert_empty_path(d, "EMR_POLYBEZIER16");
1788                 *(d->outsvg) += "    <path ";
1789                 output_style(d, EMR_STROKEPATH);
1790                 *(d->outsvg) += "\n\td=\"";
1791             }
1793             tmp_str <<
1794                 "\n\tM " <<
1795                 pix_to_x_point( d, apts[0].x, apts[0].y ) << " " <<
1796                 pix_to_y_point( d, apts[0].x, apts[0].y ) << " ";
1798             for (i=1; i<pEmr->cpts; ) {
1799                 tmp_str << "\n\tC ";
1800                 for (j=0; j<3 && i<pEmr->cpts; j++,i++) {
1801                     tmp_str <<
1802                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1803                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1804                 }
1805             }
1807             if (d->inpath) {
1808                 tmp_path << tmp_str.str().c_str();
1809             }
1810             else {
1811                 *(d->outsvg) += tmp_str.str().c_str();
1812                 *(d->outsvg) += " \" /> \n";
1813             }
1815             break;
1816         }
1817         case EMR_POLYGON16:
1818         {
1819             dbg_str << "<!-- EMR_POLYGON16 -->\n";
1821             PEMRPOLYGON16 pEmr = (PEMRPOLYGON16) lpEMFR;
1822             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1823             SVGOStringStream tmp_poly;
1824             unsigned int i;
1825             unsigned int first = 0;
1827             assert_empty_path(d, "EMR_POLYGON16");
1829             *(d->outsvg) += "    <path ";
1830             output_style(d, EMR_STROKEANDFILLPATH);
1831             *(d->outsvg) += "\n\td=\"";
1832             
1833             // skip the first point?
1834             tmp_poly << "\n\tM " <<
1835                 pix_to_x_point( d, apts[first].x, apts[first].y ) << " " <<
1836                 pix_to_y_point( d, apts[first].x, apts[first].y ) << " ";
1838             for (i=first+1; i<pEmr->cpts; i++) {
1839                 tmp_poly << "\n\tL " <<
1840                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1841                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1842             }
1844             *(d->outsvg) += tmp_poly.str().c_str();
1845             *(d->outsvg) += " z \" /> \n";
1847             break;
1848         }
1849         case EMR_POLYLINE16:
1850         {
1851             dbg_str << "<!-- EMR_POLYLINE16 -->\n";
1853             EMRPOLYLINE16 *pEmr = (EMRPOLYLINE16 *) lpEMFR;
1854             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1855             DWORD i;
1857             if (pEmr->cpts<2)
1858                 break;
1860             if (!d->inpath) {
1861                 assert_empty_path(d, "EMR_POLYLINE16");
1863                 *(d->outsvg) += "    <path ";
1864                 output_style(d, EMR_STROKEPATH);
1865                 *(d->outsvg) += "\n\td=\"";
1866             }
1868             tmp_str <<
1869                 "\n\tM " <<
1870                 pix_to_x_point( d, apts[0].x, apts[0].y ) << " " <<
1871                 pix_to_y_point( d, apts[0].x, apts[0].y ) << " ";
1873             for (i=1; i<pEmr->cpts; i++) {
1874                 tmp_str <<
1875                     "\n\tL " <<
1876                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1877                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1878             }
1880             if (d->inpath) {
1881                 tmp_path << tmp_str.str().c_str();
1882             }
1883             else {
1884                 *(d->outsvg) += tmp_str.str().c_str();
1885                 *(d->outsvg) += " \" /> \n";
1886             }
1888             break;
1889         }
1890         case EMR_POLYBEZIERTO16:
1891         {
1892             dbg_str << "<!-- EMR_POLYBEZIERTO16 -->\n";
1894             PEMRPOLYBEZIERTO16 pEmr = (PEMRPOLYBEZIERTO16) lpEMFR;
1895             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1896             DWORD i,j;
1898             if (d->path->empty()) {
1899                 d->pathless_stroke = true;
1900                 *(d->path) = "d=\"";
1901             }
1903             for (i=0; i<pEmr->cpts;) {
1904                 tmp_path << "\n\tC ";
1905                 for (j=0; j<3 && i<pEmr->cpts; j++,i++) {
1906                     tmp_path <<
1907                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1908                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1909                 }
1910             }
1912             break;
1913         }
1914         case EMR_POLYLINETO16:
1915         {
1916             dbg_str << "<!-- EMR_POLYLINETO16 -->\n";
1918             PEMRPOLYLINETO16 pEmr = (PEMRPOLYLINETO16) lpEMFR;
1919             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1920             DWORD i;
1922             if (d->path->empty()) {
1923                 d->pathless_stroke = true;
1924                 *(d->path) = "d=\"";
1925             }
1927             for (i=0; i<pEmr->cpts;i++) {
1928                 tmp_path <<
1929                     "\n\tL " <<
1930                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1931                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1932             }
1934             break;
1935         }
1936         case EMR_POLYPOLYLINE16:
1937         case EMR_POLYPOLYGON16:
1938         {
1939             if (lpEMFR->iType == EMR_POLYPOLYLINE16)
1940                 dbg_str << "<!-- EMR_POLYPOLYLINE16 -->\n";
1941             if (lpEMFR->iType == EMR_POLYPOLYGON16)
1942                 dbg_str << "<!-- EMR_POLYPOLYGON16 -->\n";
1944             PEMRPOLYPOLYGON16 pEmr = (PEMRPOLYPOLYGON16) lpEMFR;
1945             unsigned int n, i, j;
1947             if (!d->inpath) {
1948                 assert_empty_path(d, lpEMFR->iType == EMR_POLYPOLYGON16 ? "EMR_POLYPOLYGON16" : "EMR_POLYPOLYLINE16");
1950                 *(d->outsvg) += "    <path ";
1951                 output_style(d, lpEMFR->iType==EMR_POLYPOLYGON16 ? EMR_STROKEANDFILLPATH : EMR_STROKEPATH);
1952                 *(d->outsvg) += "\n\td=\"";
1953             }
1955             POINTS *apts = (POINTS *) &pEmr->aPolyCounts[pEmr->nPolys];
1957             i = 0;
1958             for (n=0; n<pEmr->nPolys && i<pEmr->cpts; n++) {
1959                 SVGOStringStream poly_path;
1961                 poly_path << "\n\tM " <<
1962                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1963                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1964                 i++;
1966                 for (j=1; j<pEmr->aPolyCounts[n] && i<pEmr->cpts; j++) {
1967                     poly_path << "\n\tL " <<
1968                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1969                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1970                     i++;
1971                 }
1973                 tmp_str << poly_path.str().c_str();
1974                 if (lpEMFR->iType == EMR_POLYPOLYGON16)
1975                     tmp_str << " z";
1976                 tmp_str << " \n";
1977             }
1979             if (d->inpath) {
1980                 tmp_path << tmp_str.str().c_str();
1981             }
1982             else {
1983                 *(d->outsvg) += tmp_str.str().c_str();
1984                 *(d->outsvg) += " \" /> \n";
1985             }
1987             break;
1988         }
1989         case EMR_POLYDRAW16:
1990             dbg_str << "<!-- EMR_POLYDRAW16 -->\n";
1991             break;
1992         case EMR_CREATEMONOBRUSH:
1993             dbg_str << "<!-- EMR_CREATEMONOBRUSH -->\n";
1994             break;
1995         case EMR_CREATEDIBPATTERNBRUSHPT:
1996             dbg_str << "<!-- EMR_CREATEDIBPATTERNBRUSHPT -->\n";
1997             break;
1998         case EMR_EXTCREATEPEN:
1999         {
2000             dbg_str << "<!-- EMR_EXTCREATEPEN -->\n";
2002             PEMREXTCREATEPEN pEmr = (PEMREXTCREATEPEN) lpEMFR;
2003             int index = pEmr->ihPen;
2005             EMREXTCREATEPEN *pPen =
2006                 (EMREXTCREATEPEN *) malloc( sizeof(EMREXTCREATEPEN) +
2007                                             sizeof(DWORD) * pEmr->elp.elpNumEntries );
2008             pPen->ihPen = pEmr->ihPen;
2009             pPen->offBmi = pEmr->offBmi;
2010             pPen->cbBmi = pEmr->cbBmi;
2011             pPen->offBits = pEmr->offBits;
2012             pPen->cbBits = pEmr->cbBits;
2013             pPen->elp = pEmr->elp;
2014             for (unsigned int i=0; i<pEmr->elp.elpNumEntries; i++) {
2015                 pPen->elp.elpStyleEntry[i] = pEmr->elp.elpStyleEntry[i];
2016             }
2017             insert_object(d, index, EMR_EXTCREATEPEN, (ENHMETARECORD *) pPen);
2019             break;
2020         }
2021         case EMR_POLYTEXTOUTA:
2022             dbg_str << "<!-- EMR_POLYTEXTOUTA -->\n";
2023             break;
2024         case EMR_POLYTEXTOUTW:
2025             dbg_str << "<!-- EMR_POLYTEXTOUTW -->\n";
2026             break;
2027         case EMR_SETICMMODE:
2028             dbg_str << "<!-- EMR_SETICMMODE -->\n";
2029             break;
2030         case EMR_CREATECOLORSPACE:
2031             dbg_str << "<!-- EMR_CREATECOLORSPACE -->\n";
2032             break;
2033         case EMR_SETCOLORSPACE:
2034             dbg_str << "<!-- EMR_SETCOLORSPACE -->\n";
2035             break;
2036         case EMR_DELETECOLORSPACE:
2037             dbg_str << "<!-- EMR_DELETECOLORSPACE -->\n";
2038             break;
2039         case EMR_GLSRECORD:
2040             dbg_str << "<!-- EMR_GLSRECORD -->\n";
2041             break;
2042         case EMR_GLSBOUNDEDRECORD:
2043             dbg_str << "<!-- EMR_GLSBOUNDEDRECORD -->\n";
2044             break;
2045         case EMR_PIXELFORMAT:
2046             dbg_str << "<!-- EMR_PIXELFORMAT -->\n";
2047             break;
2048         default:
2049             dbg_str << "<!-- EMR_??? -->\n";
2050             break;
2051     }
2052     
2053 //    *(d->outsvg) += dbg_str.str().c_str();
2054     *(d->outsvg) += tmp_outsvg.str().c_str();
2055     *(d->path) += tmp_path.str().c_str();
2057     return 1;
2060 static int CALLBACK
2061 myMetaFileProc(HDC hDC, HANDLETABLE *lpHTable, METARECORD *lpMFR, int nObj, LPARAM lpData)
2063     g_warning("Unable to import Windows Meta File.\n");
2064     return 0;
2067 // Aldus Placeable Header ===================================================
2068 // Since we are a 32bit app, we have to be sure this structure compiles to
2069 // be identical to a 16 bit app's version. To do this, we use the #pragma
2070 // to adjust packing, we use a WORD for the hmf handle, and a SMALL_RECT
2071 // for the bbox rectangle.
2072 #pragma pack( push )
2073 #pragma pack( 2 )
2074 typedef struct
2076     DWORD       dwKey;
2077     WORD        hmf;
2078     SMALL_RECT  bbox;
2079     WORD        wInch;
2080     DWORD       dwReserved;
2081     WORD        wCheckSum;
2082 } APMHEADER, *PAPMHEADER;
2083 #pragma pack( pop )
2086 SPDocument *
2087 EmfWin32::open( Inkscape::Extension::Input *mod, const gchar *uri )
2089     EMF_CALLBACK_DATA d;
2091     memset(&d, 0, sizeof(d));
2093     d.worldTransform.eM11 = 1.0;
2094     d.worldTransform.eM12 = 0.0;
2095     d.worldTransform.eM21 = 0.0;
2096     d.worldTransform.eM22 = 1.0;
2097     d.worldTransform.eDx  = 0.0;
2098     d.worldTransform.eDy  = 0.0;
2099     
2100     gsize bytesRead = 0;
2101     gsize bytesWritten = 0;
2102     GError* error = NULL;
2103     gchar *local_fn =
2104         g_filename_from_utf8( uri, -1,  &bytesRead,  &bytesWritten, &error );
2106     if (local_fn == NULL) {
2107         return NULL;
2108     }
2110     d.outsvg = new Glib::ustring("");
2111     d.path = new Glib::ustring("");
2113     CHAR *ansi_uri = (CHAR *) local_fn;
2114     gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
2115     WCHAR *unicode_uri = (WCHAR *) unicode_fn;
2117     DWORD filesize = 0;
2118     HANDLE fp = NULL;
2120     HMETAFILE hmf;
2121     HENHMETAFILE hemf;
2123     if (PrintWin32::is_os_wide()) {
2124         fp = CreateFileW(unicode_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2125     }
2126     else {
2127         fp = CreateFileA(ansi_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2128     }
2130     if ( fp != INVALID_HANDLE_VALUE ) {
2131         filesize = GetFileSize(fp, NULL);
2132         CloseHandle(fp);
2133     }
2135     // Try open as Enhanced Metafile
2136     if (PrintWin32::is_os_wide())
2137         hemf = GetEnhMetaFileW(unicode_uri);
2138     else
2139         hemf = GetEnhMetaFileA(ansi_uri);
2141     if (!hemf) {
2142         // Try open as Windows Metafile
2143         if (PrintWin32::is_os_wide())
2144             hmf = GetMetaFileW(unicode_uri);
2145         else
2146             hmf = GetMetaFileA(ansi_uri);
2148         METAFILEPICT mp;
2149         HDC hDC;
2151         if (!hmf) {
2152             if (PrintWin32::is_os_wide()) {
2153                 WCHAR szTemp[MAX_PATH];
2155                 DWORD dw = GetShortPathNameW( unicode_uri, szTemp, MAX_PATH );
2156                 if (dw) {
2157                     hmf = GetMetaFileW( szTemp );
2158                 }
2159             } else {
2160                 CHAR szTemp[MAX_PATH];
2162                 DWORD dw = GetShortPathNameA( ansi_uri, szTemp, MAX_PATH );
2163                 if (dw) {
2164                     hmf = GetMetaFileA( szTemp );
2165                 }
2166             }
2167         }
2169         if (hmf) {
2170             DWORD nSize = GetMetaFileBitsEx( hmf, 0, NULL );
2172             if (!nSize)
2173                 nSize = filesize;
2174             
2175             if (nSize) {
2176                 BYTE *lpvData = new BYTE[nSize];
2177                 if (lpvData) {
2178                     DWORD dw = GetMetaFileBitsEx( hmf, nSize, lpvData );
2179                     if (dw) {
2180                         // Fill out a METAFILEPICT structure
2181                         mp.mm = MM_ANISOTROPIC;
2182                         mp.xExt = 1000;
2183                         mp.yExt = 1000;
2184                         mp.hMF = NULL;
2185                         // Get a reference DC
2186                         hDC = GetDC( NULL );
2187                         // Make an enhanced metafile from the windows metafile
2188                         hemf = SetWinMetaFileBits( nSize, lpvData, hDC, &mp );
2189                         // Clean up
2190                         ReleaseDC( NULL, hDC );
2191                         DeleteMetaFile( hmf );
2192                         hmf = NULL;
2193                     }
2194                     else {
2195                         // EnumMetaFile
2196                     }
2197                     delete[] lpvData;
2198                 }
2199                 else {
2200                     DeleteMetaFile( hmf );
2201                     hmf = NULL;
2202                 }
2203             }
2204             else {
2205                 DeleteMetaFile( hmf );
2206                 hmf = NULL;
2207             }
2208         }
2209         else {
2210             // Try open as Aldus Placeable Metafile
2211             HANDLE hFile;
2212             if (PrintWin32::is_os_wide())
2213                 hFile = CreateFileW( unicode_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
2214             else
2215                 hFile = CreateFileA( ansi_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
2217             if (hFile != INVALID_HANDLE_VALUE) {
2218                 DWORD nSize = GetFileSize( hFile, NULL );
2219                 if (nSize) {
2220                     BYTE *lpvData = new BYTE[nSize];
2221                     if (lpvData) {
2222                         DWORD dw = ReadFile( hFile, lpvData, nSize, &nSize, NULL );
2223                         if (dw) {
2224                             if ( ((PAPMHEADER)lpvData)->dwKey == 0x9ac6cdd7l ) {
2225                                 // Fill out a METAFILEPICT structure
2226                                 mp.mm = MM_ANISOTROPIC;
2227                                 mp.xExt = ((PAPMHEADER)lpvData)->bbox.Right - ((PAPMHEADER)lpvData)->bbox.Left;
2228                                 mp.xExt = ( mp.xExt * 2540l ) / (DWORD)(((PAPMHEADER)lpvData)->wInch);
2229                                 mp.yExt = ((PAPMHEADER)lpvData)->bbox.Bottom - ((PAPMHEADER)lpvData)->bbox.Top;
2230                                 mp.yExt = ( mp.yExt * 2540l ) / (DWORD)(((PAPMHEADER)lpvData)->wInch);
2231                                 mp.hMF = NULL;
2232                                 // Get a reference DC
2233                                 hDC = GetDC( NULL );
2234                                 // Create an enhanced metafile from the bits
2235                                 hemf = SetWinMetaFileBits( nSize, lpvData+sizeof(APMHEADER), hDC, &mp );
2236                                 // Clean up
2237                                 ReleaseDC( NULL, hDC );
2238                             }
2239                         }
2240                         delete[] lpvData;
2241                     }
2242                 }
2243                 CloseHandle( hFile );
2244             }
2245         }
2246     }
2248     if ((!hemf && !hmf) || !d.outsvg || !d.path) {
2249         if (d.outsvg)
2250             delete d.outsvg;
2251         if (d.path)
2252             delete d.path;
2253         if (local_fn)
2254             g_free(local_fn);
2255         if (unicode_fn)
2256             g_free(unicode_fn);
2257         if (hemf)
2258             DeleteEnhMetaFile(hemf);
2259         if (hmf)
2260             DeleteMetaFile(hmf);
2261         return NULL;
2262     }
2264     d.pDesc = NULL;
2266     if (hemf) {
2267         DWORD dwNeeded = GetEnhMetaFileDescriptionA( hemf, 0, NULL );
2268         if ( dwNeeded > 0 ) {
2269             d.pDesc = (CHAR *) malloc( dwNeeded + 1 );
2270             if ( GetEnhMetaFileDescription( hemf, dwNeeded, d.pDesc ) == 0 )
2271                 lstrcpy( d.pDesc, "" );
2272             if ( lstrlen( d.pDesc ) > 1 )
2273                 d.pDesc[lstrlen(d.pDesc)] = '#';
2274         }
2276         EnumEnhMetaFile(NULL, hemf, myEnhMetaFileProc, (LPVOID) &d, NULL);
2277         DeleteEnhMetaFile(hemf);
2278     }
2279     else {
2280         EnumMetaFile(NULL, hmf, myMetaFileProc, (LPARAM) &d);
2281         DeleteMetaFile(hmf);
2282     }
2283     
2284     if (d.pDesc)
2285         free( d.pDesc );
2287 //    std::cout << "SVG Output: " << std::endl << *(d.outsvg) << std::endl;
2289     SPDocument *doc = sp_document_new_from_mem(d.outsvg->c_str(), d.outsvg->length(), TRUE);
2291     delete d.outsvg;
2292     delete d.path;
2294     if (d.emf_obj) {
2295         int i;
2296         for (i=0; i<d.n_obj; i++)
2297             delete_object(&d, i);
2298         delete[] d.emf_obj;
2299     }
2300     
2301     if (d.style.stroke_dash.dash)
2302         delete[] d.style.stroke_dash.dash;
2304     if  (local_fn)
2305         g_free(local_fn);
2306     if  (unicode_fn)
2307         g_free(unicode_fn);
2309     return doc;
2313 void
2314 EmfWin32::init (void)
2316     Inkscape::Extension::Extension * ext;
2318     /* EMF in */
2319     ext = Inkscape::Extension::build_from_mem(
2320         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2321             "<name>" N_("EMF Input") "</name>\n"
2322             "<id>org.inkscape.input.emf.win32</id>\n"
2323             "<input>\n"
2324                 "<extension>.emf</extension>\n"
2325                 "<mimetype>image/x-emf</mimetype>\n"
2326                 "<filetypename>" N_("Enhanced Metafiles (*.emf)") "</filetypename>\n"
2327                 "<filetypetooltip>" N_("Enhanced Metafiles") "</filetypetooltip>\n"
2328                 "<output_extension>org.inkscape.output.emf.win32</output_extension>\n"
2329             "</input>\n"
2330         "</inkscape-extension>", new EmfWin32());
2332     /* WMF in */
2333     ext = Inkscape::Extension::build_from_mem(
2334         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2335             "<name>" N_("WMF Input") "</name>\n"
2336             "<id>org.inkscape.input.wmf.win32</id>\n"
2337             "<input>\n"
2338                 "<extension>.wmf</extension>\n"
2339                 "<mimetype>image/x-wmf</mimetype>\n"
2340                 "<filetypename>" N_("Windows Metafiles (*.wmf)") "</filetypename>\n"
2341                 "<filetypetooltip>" N_("Windows Metafiles") "</filetypetooltip>\n"
2342                 "<output_extension>org.inkscape.output.emf.win32</output_extension>\n"
2343             "</input>\n"
2344         "</inkscape-extension>", new EmfWin32());
2346     /* EMF out */
2347     ext = Inkscape::Extension::build_from_mem(
2348         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2349             "<name>" N_("EMF Output") "</name>\n"
2350             "<id>org.inkscape.output.emf.win32</id>\n"
2351             "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">true</param>\n"
2352             "<output>\n"
2353                 "<extension>.emf</extension>\n"
2354                 "<mimetype>image/x-emf</mimetype>\n"
2355                 "<filetypename>" N_("Enhanced Metafile (*.emf)") "</filetypename>\n"
2356                 "<filetypetooltip>" N_("Enhanced Metafile") "</filetypetooltip>\n"
2357             "</output>\n"
2358         "</inkscape-extension>", new EmfWin32());
2360     return;
2364 } } }  /* namespace Inkscape, Extension, Implementation */
2367 #endif /* WIN32 */
2370 /*
2371   Local Variables:
2372   mode:cpp
2373   c-file-style:"stroustrup"
2374   c-file-offsets:((innamespace . 0)(inline-open . 0))
2375   indent-tabs-mode:nil
2376   fill-column:99
2377   End:
2378 */
2379 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :