Code

remove const assignment error
[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/n-art-bpath.h"
39 #include "libnr/nr-point-matrix-ops.h"
40 #include "gtk/gtk.h"
41 #include "print.h"
42 #include "glibmm/i18n.h"
43 #include "extension/extension.h"
44 #include "extension/system.h"
45 #include "extension/print.h"
46 #include "extension/db.h"
47 #include "extension/output.h"
48 #include "document.h"
49 #include "display/nr-arena.h"
50 #include "display/nr-arena-item.h"
52 #include "libnr/nr-rect.h"
53 #include "libnr/nr-matrix.h"
54 #include "libnr/nr-pixblock.h"
56 #include <stdio.h>
57 #include <string.h>
59 #include <vector>
60 #include <string>
62 #include "io/sys.h"
64 #include "unit-constants.h"
66 #include "clear-n_.h"
69 #define PRINT_EMF_WIN32 "org.inkscape.print.emf.win32"
71 #ifndef PS_JOIN_MASK
72 #define PS_JOIN_MASK (PS_JOIN_BEVEL|PS_JOIN_MITER|PS_JOIN_ROUND)
73 #endif
76 namespace Inkscape {
77 namespace Extension {
78 namespace Internal {
81 EmfWin32::EmfWin32 (void) // The null constructor
82 {
83     return;
84 }
87 EmfWin32::~EmfWin32 (void) //The destructor
88 {
89     return;
90 }
93 bool
94 EmfWin32::check (Inkscape::Extension::Extension * module)
95 {
96     if (NULL == Inkscape::Extension::db.get(PRINT_EMF_WIN32))
97         return FALSE;
98     return TRUE;
99 }
102 static void
103 emf_print_document_to_file(SPDocument *doc, gchar const *filename)
105     Inkscape::Extension::Print *mod;
106     SPPrintContext context;
107     gchar const *oldconst;
108     gchar *oldoutput;
109     unsigned int ret;
111     sp_document_ensure_up_to_date(doc);
113     mod = Inkscape::Extension::get_print(PRINT_EMF_WIN32);
114     oldconst = mod->get_param_string("destination");
115     oldoutput = g_strdup(oldconst);
116     mod->set_param_string("destination", (gchar *)filename);
118 /* Start */
119     context.module = mod;
120     /* fixme: This has to go into module constructor somehow */
121     /* Create new arena */
122     mod->base = SP_ITEM(sp_document_root(doc));
123     mod->arena = NRArena::create();
124     mod->dkey = sp_item_display_key_new(1);
125     mod->root = sp_item_invoke_show(mod->base, mod->arena, mod->dkey, SP_ITEM_SHOW_DISPLAY);
126     /* Print document */
127     ret = mod->begin(doc);
128     if (ret) {
129         throw Inkscape::Extension::Output::save_failed();
130     }
131     sp_item_invoke_print(mod->base, &context);
132     ret = mod->finish();
133     /* Release arena */
134     sp_item_invoke_hide(mod->base, mod->dkey);
135     mod->base = NULL;
136     nr_arena_item_unref(mod->root);
137     mod->root = NULL;
138     nr_object_unref((NRObject *) mod->arena);
139     mod->arena = NULL;
140 /* end */
142     mod->set_param_string("destination", oldoutput);
143     g_free(oldoutput);
145     return;
149 void
150 EmfWin32::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri)
152     Inkscape::Extension::Extension * ext;
154     ext = Inkscape::Extension::db.get(PRINT_EMF_WIN32);
155     if (ext == NULL)
156         return;
158     bool old_textToPath  = ext->get_param_bool("textToPath");
159     bool new_val         = mod->get_param_bool("textToPath");
160     ext->set_param_bool("textToPath", new_val);
162     gchar * final_name;
163     final_name = g_strdup_printf("%s", uri);
164     emf_print_document_to_file(doc, final_name);
165     g_free(final_name);
167     ext->set_param_bool("textToPath", old_textToPath);
169     return;
174 typedef struct {
175     int type;
176     ENHMETARECORD *lpEMFR;
177 } EMF_OBJECT, *PEMF_OBJECT;
179 typedef struct emf_callback_data {
180     Glib::ustring *outsvg;
181     Glib::ustring *path;
182     struct SPStyle style;
183     class SPTextStyle tstyle;
184     bool stroke_set;
185     bool fill_set;
186     double xDPI, yDPI;
187     bool pathless_stroke;
188     bool inpath;
190     SIZEL sizeWnd;
191     SIZEL sizeView;
192     float PixelsX;
193     float PixelsY;
194     float MMX;
195     float MMY;
196     float dwInchesX;
197     float dwInchesY;
198     POINTL winorg;
199     POINTL vieworg;
200     double ScaleX, ScaleY;
201     COLORREF textColor;
202     bool textColorSet;
203     DWORD textAlign;
204     XFORM worldTransform;
205     unsigned int id;
207     int n_obj;
208     PEMF_OBJECT emf_obj;
209 } EMF_CALLBACK_DATA, *PEMF_CALLBACK_DATA;
212 static void
213 output_style(PEMF_CALLBACK_DATA d, int iType)
215     SVGOStringStream tmp_id;
216     SVGOStringStream tmp_style;
217     char tmp[1024] = {0};
219     float fill_rgb[3];
220     sp_color_get_rgb_floatv( &(d->style.fill.value.color), fill_rgb );
221     
222     float stroke_rgb[3];
223     sp_color_get_rgb_floatv(&(d->style.stroke.value.color), stroke_rgb);
225     tmp_id << "\n\tid=\"" << (d->id++) << "\"";
226     *(d->outsvg) += tmp_id.str().c_str();
227     *(d->outsvg) += "\n\tstyle=\"";
228     if (iType == EMR_STROKEPATH || !d->fill_set) {
229         tmp_style << "fill:none;";
230     } else {
231         snprintf(tmp, 1023,
232                  "fill:#%02x%02x%02x;",
233                  SP_COLOR_F_TO_U(fill_rgb[0]),
234                  SP_COLOR_F_TO_U(fill_rgb[1]),
235                  SP_COLOR_F_TO_U(fill_rgb[2]));
236         tmp_style << tmp;
237         snprintf(tmp, 1023,
238                  "fill-rule:%s;",
239                  d->style.fill_rule.value == 0 ? "evenodd" : "nonzero");
240         tmp_style << tmp;
241         tmp_style << "fill-opacity:1;";
243         if (d->fill_set && d->stroke_set && d->style.stroke_width.value == 1 &&
244             fill_rgb[0]==stroke_rgb[0] && fill_rgb[1]==stroke_rgb[1] && fill_rgb[2]==stroke_rgb[2])
245         {
246             d->stroke_set = false;
247         }
248     }
250     if (iType == EMR_FILLPATH || !d->stroke_set) {
251         tmp_style << "stroke:none;";
252     } else {
253         snprintf(tmp, 1023,
254                  "stroke:#%02x%02x%02x;",
255                  SP_COLOR_F_TO_U(stroke_rgb[0]),
256                  SP_COLOR_F_TO_U(stroke_rgb[1]),
257                  SP_COLOR_F_TO_U(stroke_rgb[2]));
258         tmp_style << tmp;
260         tmp_style << "stroke-width:" <<
261             MAX( 0.001, d->style.stroke_width.value ) << "px;";
263         tmp_style << "stroke-linecap:" <<
264             (d->style.stroke_linecap.computed == 0 ? "butt" :
265              d->style.stroke_linecap.computed == 1 ? "round" :
266              d->style.stroke_linecap.computed == 2 ? "square" :
267              "unknown") << ";";
269         tmp_style << "stroke-linejoin:" <<
270             (d->style.stroke_linejoin.computed == 0 ? "miter" :
271              d->style.stroke_linejoin.computed == 1 ? "round" :
272              d->style.stroke_linejoin.computed == 2 ? "bevel" :
273              "unknown") << ";";
275         if (d->style.stroke_linejoin.computed == 0) {
276             tmp_style << "stroke-miterlimit:" <<
277                 MAX( 0.01, d->style.stroke_miterlimit.value ) << ";";
278         }
280         if (d->style.stroke_dasharray_set &&
281             d->style.stroke_dash.n_dash && d->style.stroke_dash.dash)
282         {
283             tmp_style << "stroke-dasharray:";
284             for (int i=0; i<d->style.stroke_dash.n_dash; i++) {
285                 if (i)
286                     tmp_style << ",";
287                 tmp_style << d->style.stroke_dash.dash[i];
288             }
289             tmp_style << ";";
290             tmp_style << "stroke-dashoffset:0;";
291         } else {
292             tmp_style << "stroke-dasharray:none;";
293         }
294         tmp_style << "stroke-opacity:1;";
295     }
296     tmp_style << "\" ";
298     *(d->outsvg) += tmp_style.str().c_str();
302 static double
303 _pix_x_to_point(PEMF_CALLBACK_DATA d, double px)
305     double tmp = px - d->winorg.x;
306     tmp *= d->ScaleX ? (double) PX_PER_IN / d->ScaleX : 1.0;
307     tmp += d->vieworg.x;
308     return tmp;
311 static double
312 _pix_y_to_point(PEMF_CALLBACK_DATA d, double px)
314     double tmp = px - d->winorg.y;
315     tmp *= d->ScaleY ? (double) PX_PER_IN / d->ScaleY : 1.0;
316     tmp += d->vieworg.y;
317     return tmp;
321 static double
322 pix_to_x_point(PEMF_CALLBACK_DATA d, double px, double py)
324     double ppx = _pix_x_to_point(d, px);
325     double ppy = _pix_y_to_point(d, py);
327     double x = ppx * d->worldTransform.eM11 + ppy * d->worldTransform.eM21 + d->worldTransform.eDx;
328     
329     return x;
332 static double
333 pix_to_y_point(PEMF_CALLBACK_DATA d, double px, double py)
335     double ppx = _pix_x_to_point(d, px);
336     double ppy = _pix_y_to_point(d, py);
338     double y = ppx * d->worldTransform.eM12 + ppy * d->worldTransform.eM22 + d->worldTransform.eDy;
339     
340     return y;
344 static double
345 _pix_size_to_point(PEMF_CALLBACK_DATA d, double px)
347     double tmp = px;
348     tmp *= d->ScaleX ? (double) PX_PER_IN / d->ScaleX : 1.0;
349     return tmp;
353 static double
354 pix_to_size_point(PEMF_CALLBACK_DATA d, double px)
356     double dx = pix_to_x_point(d, px, 0);
357     double dy = pix_to_y_point(d, px, 0);
358     double tmp = sqrt(dx * dx + dy * dy);
359     return tmp;
363 static void
364 select_pen(PEMF_CALLBACK_DATA d, int index)
366     PEMRCREATEPEN pEmr = NULL;
368     if (index >= 0 && index < d->n_obj)
369         pEmr = (PEMRCREATEPEN) d->emf_obj[index].lpEMFR;
371     if (!pEmr)
372         return;
374     switch (pEmr->lopn.lopnStyle & PS_STYLE_MASK) {
375         case PS_DASH:
376         case PS_DOT:
377         case PS_DASHDOT:
378         case PS_DASHDOTDOT:
379         {
380             int i = 0;
381             int penstyle = (pEmr->lopn.lopnStyle & PS_STYLE_MASK);
382             d->style.stroke_dash.n_dash =
383                 penstyle == PS_DASHDOTDOT ? 6 : penstyle == PS_DASHDOT ? 4 : 2;
384             if (d->style.stroke_dash.dash)
385                 delete[] d->style.stroke_dash.dash;
386             d->style.stroke_dash.dash = new double[d->style.stroke_dash.n_dash];
387             if (penstyle==PS_DASH || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
388                 d->style.stroke_dash.dash[i++] = 3;
389                 d->style.stroke_dash.dash[i++] = 1;
390             }
391             if (penstyle==PS_DOT || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
392                 d->style.stroke_dash.dash[i++] = 1;
393                 d->style.stroke_dash.dash[i++] = 1;
394             }
395             if (penstyle==PS_DASHDOTDOT) {
396                 d->style.stroke_dash.dash[i++] = 1;
397                 d->style.stroke_dash.dash[i++] = 1;
398             }
399             
400             d->style.stroke_dasharray_set = 1;
401             break;
402         }
403         
404         case PS_SOLID:
405         default:
406         {
407             d->style.stroke_dasharray_set = 0;
408             break;
409         }
410     }
412     switch (pEmr->lopn.lopnStyle & PS_ENDCAP_MASK) {
413         case PS_ENDCAP_ROUND:
414         {
415             d->style.stroke_linecap.computed = 1;
416             break;
417         }
418         case PS_ENDCAP_SQUARE:
419         {
420             d->style.stroke_linecap.computed = 2;
421             break;
422         }
423         case PS_ENDCAP_FLAT:
424         default:
425         {
426             d->style.stroke_linecap.computed = 0;
427             break;
428         }
429     }
431     switch (pEmr->lopn.lopnStyle & PS_JOIN_MASK) {
432         case PS_JOIN_BEVEL:
433         {
434             d->style.stroke_linejoin.computed = 2;
435             break;
436         }
437         case PS_JOIN_MITER:
438         {
439             d->style.stroke_linejoin.computed = 0;
440             break;
441         }
442         case PS_JOIN_ROUND:
443         default:
444         {
445             d->style.stroke_linejoin.computed = 1;
446             break;
447         }
448     }
450     d->stroke_set = true;
452     if (pEmr->lopn.lopnStyle == PS_NULL) {
453         d->style.stroke_width.value = 0;
454         d->stroke_set = false;
455     } else if (pEmr->lopn.lopnWidth.x) {
456         d->style.stroke_width.value = pix_to_size_point( d, pEmr->lopn.lopnWidth.x );
457     } else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
458         //d->style.stroke_width.value = 1.0;
459         d->style.stroke_width.value = pix_to_size_point( d, 1 );
460     }
462     double r, g, b;
463     r = SP_COLOR_U_TO_F( GetRValue(pEmr->lopn.lopnColor) );
464     g = SP_COLOR_U_TO_F( GetGValue(pEmr->lopn.lopnColor) );
465     b = SP_COLOR_U_TO_F( GetBValue(pEmr->lopn.lopnColor) );
466     d->style.stroke.value.color.set( r, g, b );
470 static void
471 select_extpen(PEMF_CALLBACK_DATA d, int index)
473     PEMREXTCREATEPEN pEmr = NULL;
475     if (index >= 0 && index < d->n_obj)
476         pEmr = (PEMREXTCREATEPEN) d->emf_obj[index].lpEMFR;
478     if (!pEmr)
479         return;
481     switch (pEmr->elp.elpPenStyle & PS_STYLE_MASK) {
482         case PS_USERSTYLE:
483         {
484             if (pEmr->elp.elpNumEntries) {
485                 d->style.stroke_dash.n_dash = pEmr->elp.elpNumEntries;
486                 if (d->style.stroke_dash.dash)
487                     delete[] d->style.stroke_dash.dash;
488                 d->style.stroke_dash.dash = new double[pEmr->elp.elpNumEntries];
489                 for (unsigned int i=0; i<pEmr->elp.elpNumEntries; i++) {
490                     d->style.stroke_dash.dash[i] = pix_to_size_point( d, pEmr->elp.elpStyleEntry[i] );
491                 }
492                 d->style.stroke_dasharray_set = 1;
493             } else {
494                 d->style.stroke_dasharray_set = 0;
495             }
496             break;
497         }
499         case PS_DASH:
500         case PS_DOT:
501         case PS_DASHDOT:
502         case PS_DASHDOTDOT:
503         {
504             int i = 0;
505             int penstyle = (pEmr->elp.elpPenStyle & PS_STYLE_MASK);
506             d->style.stroke_dash.n_dash =
507                 penstyle == PS_DASHDOTDOT ? 6 : penstyle == PS_DASHDOT ? 4 : 2;
508             if (d->style.stroke_dash.dash)
509                 delete[] d->style.stroke_dash.dash;
510             d->style.stroke_dash.dash = new double[d->style.stroke_dash.n_dash];
511             if (penstyle==PS_DASH || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
512                 d->style.stroke_dash.dash[i++] = 3;
513                 d->style.stroke_dash.dash[i++] = 2;
514             }
515             if (penstyle==PS_DOT || penstyle==PS_DASHDOT || penstyle==PS_DASHDOTDOT) {
516                 d->style.stroke_dash.dash[i++] = 1;
517                 d->style.stroke_dash.dash[i++] = 2;
518             }
519             if (penstyle==PS_DASHDOTDOT) {
520                 d->style.stroke_dash.dash[i++] = 1;
521                 d->style.stroke_dash.dash[i++] = 2;
522             }
523             
524             d->style.stroke_dasharray_set = 1;
525             break;
526         }
527         
528         case PS_SOLID:
529         default:
530         {
531             d->style.stroke_dasharray_set = 0;
532             break;
533         }
534     }
536     switch (pEmr->elp.elpPenStyle & PS_ENDCAP_MASK) {
537         case PS_ENDCAP_ROUND:
538         {
539             d->style.stroke_linecap.computed = 1;
540             break;
541         }
542         case PS_ENDCAP_SQUARE:
543         {
544             d->style.stroke_linecap.computed = 2;
545             break;
546         }
547         case PS_ENDCAP_FLAT:
548         default:
549         {
550             d->style.stroke_linecap.computed = 0;
551             break;
552         }
553     }
555     switch (pEmr->elp.elpPenStyle & PS_JOIN_MASK) {
556         case PS_JOIN_BEVEL:
557         {
558             d->style.stroke_linejoin.computed = 2;
559             break;
560         }
561         case PS_JOIN_MITER:
562         {
563             d->style.stroke_linejoin.computed = 0;
564             break;
565         }
566         case PS_JOIN_ROUND:
567         default:
568         {
569             d->style.stroke_linejoin.computed = 1;
570             break;
571         }
572     }
574     d->stroke_set = true;
576     if (pEmr->elp.elpPenStyle == PS_NULL) {
577         d->style.stroke_width.value = 0;
578         d->stroke_set = false;
579     } else if (pEmr->elp.elpWidth) {
580         d->style.stroke_width.value = pix_to_size_point( d, pEmr->elp.elpWidth );
581     } else { // this stroke should always be rendered as 1 pixel wide, independent of zoom level (can that be done in SVG?)
582         //d->style.stroke_width.value = 1.0;
583         d->style.stroke_width.value = pix_to_size_point( d, 1 );
584     }
586     double r, g, b;
587     r = SP_COLOR_U_TO_F( GetRValue(pEmr->elp.elpColor) );
588     g = SP_COLOR_U_TO_F( GetGValue(pEmr->elp.elpColor) );
589     b = SP_COLOR_U_TO_F( GetBValue(pEmr->elp.elpColor) );
591     d->style.stroke.value.color.set( r, g, b );
595 static void
596 select_brush(PEMF_CALLBACK_DATA d, int index)
598     PEMRCREATEBRUSHINDIRECT pEmr = NULL;
600     if (index >= 0 && index < d->n_obj)
601         pEmr = (PEMRCREATEBRUSHINDIRECT) d->emf_obj[index].lpEMFR;
603     if (!pEmr)
604         return;
606     if (pEmr->lb.lbStyle == BS_SOLID) {
607         double r, g, b;
608         r = SP_COLOR_U_TO_F( GetRValue(pEmr->lb.lbColor) );
609         g = SP_COLOR_U_TO_F( GetGValue(pEmr->lb.lbColor) );
610         b = SP_COLOR_U_TO_F( GetBValue(pEmr->lb.lbColor) );
611         d->style.fill.value.color.set( r, g, b );
612     }
614     d->fill_set = true;
618 static void
619 select_font(PEMF_CALLBACK_DATA d, int index)
621     PEMREXTCREATEFONTINDIRECTW pEmr = NULL;
623     if (index >= 0 && index < d->n_obj)
624         pEmr = (PEMREXTCREATEFONTINDIRECTW) d->emf_obj[index].lpEMFR;
626     if (!pEmr)
627         return;
629     d->style.font_size.computed = pix_to_size_point( d, pEmr->elfw.elfLogFont.lfHeight );
630     d->style.font_weight.value =
631         pEmr->elfw.elfLogFont.lfWeight == FW_THIN ? SP_CSS_FONT_WEIGHT_100 :
632         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRALIGHT ? SP_CSS_FONT_WEIGHT_200 :
633         pEmr->elfw.elfLogFont.lfWeight == FW_LIGHT ? SP_CSS_FONT_WEIGHT_300 :
634         pEmr->elfw.elfLogFont.lfWeight == FW_NORMAL ? SP_CSS_FONT_WEIGHT_400 :
635         pEmr->elfw.elfLogFont.lfWeight == FW_MEDIUM ? SP_CSS_FONT_WEIGHT_500 :
636         pEmr->elfw.elfLogFont.lfWeight == FW_SEMIBOLD ? SP_CSS_FONT_WEIGHT_600 :
637         pEmr->elfw.elfLogFont.lfWeight == FW_BOLD ? SP_CSS_FONT_WEIGHT_700 :
638         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRABOLD ? SP_CSS_FONT_WEIGHT_800 :
639         pEmr->elfw.elfLogFont.lfWeight == FW_HEAVY ? SP_CSS_FONT_WEIGHT_900 :
640         pEmr->elfw.elfLogFont.lfWeight == FW_NORMAL ? SP_CSS_FONT_WEIGHT_NORMAL :
641         pEmr->elfw.elfLogFont.lfWeight == FW_BOLD ? SP_CSS_FONT_WEIGHT_BOLD :
642         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRALIGHT ? SP_CSS_FONT_WEIGHT_LIGHTER :
643         pEmr->elfw.elfLogFont.lfWeight == FW_EXTRABOLD ? SP_CSS_FONT_WEIGHT_BOLDER :
644         FW_NORMAL;
645     d->style.font_style.value = (pEmr->elfw.elfLogFont.lfItalic ? SP_CSS_FONT_STYLE_ITALIC : SP_CSS_FONT_STYLE_NORMAL);
646     d->style.text_decoration.underline = pEmr->elfw.elfLogFont.lfUnderline;
647     d->style.text_decoration.line_through = pEmr->elfw.elfLogFont.lfStrikeOut;
648     if (d->tstyle.font_family.value)
649         g_free(d->tstyle.font_family.value);
650     d->tstyle.font_family.value =
651         (gchar *) g_utf16_to_utf8( (gunichar2*) pEmr->elfw.elfLogFont.lfFaceName, -1, NULL, NULL, NULL );
654 static void
655 delete_object(PEMF_CALLBACK_DATA d, int index)
657     if (index >= 0 && index < d->n_obj) {
658         d->emf_obj[index].type = 0;
659         if (d->emf_obj[index].lpEMFR)
660             free(d->emf_obj[index].lpEMFR);
661         d->emf_obj[index].lpEMFR = NULL;
662     }
666 static void
667 insert_object(PEMF_CALLBACK_DATA d, int index, int type, ENHMETARECORD *pObj)
669     if (index >= 0 && index < d->n_obj) {
670         delete_object(d, index);
671         d->emf_obj[index].type = type;
672         d->emf_obj[index].lpEMFR = pObj;
673     }
676 static void
677 assert_empty_path(PEMF_CALLBACK_DATA d, const char *fun)
679     if (!d->path->empty()) {
680         g_debug("emf-win32-inout: assert_empty_path failed for %s\n", fun);
682         *(d->outsvg) += "<!--\n";
683         *(d->outsvg) += "    <path \t";
684         output_style(d, EMR_STROKEPATH);
685         if (strstr(d->path->c_str(), "d=\"") == NULL) {
686             *(d->outsvg) += "d=\"";
687             *(d->outsvg) += "\n\t";
688         }
689         *(d->outsvg) += *(d->path);
690         *(d->outsvg) += " \" /> \n";
691         *(d->outsvg) += "-->\n";
693         *(d->path) = "";
694     }
698 static int CALLBACK
699 myEnhMetaFileProc(HDC hDC, HANDLETABLE *lpHTable, ENHMETARECORD *lpEMFR, int nObj, LPARAM lpData)
701     PEMF_CALLBACK_DATA d;
702     SVGOStringStream tmp_outsvg;
703     SVGOStringStream tmp_path;
704     SVGOStringStream tmp_str;
706     d = (PEMF_CALLBACK_DATA) lpData;
708     if (d->pathless_stroke) {
709         if (lpEMFR->iType!=EMR_POLYBEZIERTO && lpEMFR->iType!=EMR_POLYBEZIERTO16 &&
710             lpEMFR->iType!=EMR_POLYLINETO && lpEMFR->iType!=EMR_POLYLINETO16 &&
711             lpEMFR->iType!=EMR_LINETO && lpEMFR->iType!=EMR_ARCTO &&
712             lpEMFR->iType!=EMR_SETBKCOLOR && lpEMFR->iType!=EMR_SETROP2 &&
713             lpEMFR->iType!=EMR_SETBKMODE)
714         {
715             *(d->outsvg) += "    <path ";
716             output_style(d, EMR_STROKEPATH);
717             *(d->outsvg) += "\n\t";
718             *(d->outsvg) += *(d->path);
719             *(d->outsvg) += " \" /> \n";
720             *(d->path) = "";
721             d->pathless_stroke = false;
722         }
723     }
725     switch (lpEMFR->iType)
726     {
727         case EMR_HEADER:
728         {
729             ENHMETAHEADER *pEmr = (ENHMETAHEADER *) lpEMFR;
730             tmp_outsvg << "<svg\n";
732             d->xDPI = 2540;
733             d->yDPI = 2540;
735             d->PixelsX = pEmr->rclFrame.right - pEmr->rclFrame.left;
736             d->PixelsY = pEmr->rclFrame.bottom - pEmr->rclFrame.top;
738             d->MMX = d->PixelsX / 100.0;
739             d->MMY = d->PixelsY / 100.0;
741             tmp_outsvg <<
742                 "  width=\"" << d->MMX << "mm\"\n" <<
743                 "  height=\"" << d->MMY << "mm\">\n";
744             tmp_outsvg << "<g>\n";
746             if (pEmr->nHandles) {
747                 d->n_obj = pEmr->nHandles;
748                 d->emf_obj = new EMF_OBJECT[d->n_obj];
749                 
750                 // Init the new emf_obj list elements to null, provided the
751                 // dynamic allocation succeeded.
752                 if ( d->emf_obj != NULL )
753                 {
754                     for( unsigned int i=0; i < d->n_obj; ++i )
755                         d->emf_obj[i].lpEMFR = NULL;
756                 } //if
758             } else {
759                 d->emf_obj = NULL;
760             }
762             break;
763         }
764         case EMR_POLYBEZIER:
765         {
766             PEMRPOLYBEZIER pEmr = (PEMRPOLYBEZIER) lpEMFR;
767             DWORD i,j;
769             if (pEmr->cptl<4)
770                 break;
772             if (!d->inpath) {
773                 assert_empty_path(d, "EMR_POLYBEZIER");
775                 *(d->outsvg) += "    <path ";
776                 output_style(d, EMR_STROKEPATH);
777                 *(d->outsvg) += "\n\td=\"";
778             }
780             tmp_str <<
781                 "\n\tM " <<
782                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
783                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y) << " ";
785             for (i=1; i<pEmr->cptl; ) {
786                 tmp_str << "\n\tC ";
787                 for (j=0; j<3 && i<pEmr->cptl; j++,i++) {
788                     tmp_str <<
789                         pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
790                         pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
791                 }
792             }
794             if (d->inpath) {
795                 tmp_path << tmp_str.str().c_str();
796             }
797             else {
798                 *(d->outsvg) += tmp_str.str().c_str();
799                 *(d->outsvg) += " \" /> \n";
800             }
802             break;
803         }
804         case EMR_POLYGON:
805         {
806             EMRPOLYGON *pEmr = (EMRPOLYGON *) lpEMFR;
807             DWORD i;
809             if (pEmr->cptl < 2)
810                 break;
812             assert_empty_path(d, "EMR_POLYGON");
814             *(d->outsvg) += "    <path ";
815             output_style(d, EMR_STROKEANDFILLPATH);
816             *(d->outsvg) += "\n\td=\"";
818             tmp_str <<
819                 "\n\tM " <<
820                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
821                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " ";
823             for (i=1; i<pEmr->cptl; i++) {
824                 tmp_str <<
825                     "\n\tL " <<
826                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
827                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
828             }
830             *(d->outsvg) += tmp_str.str().c_str();
831             *(d->outsvg) += " z \" /> \n";
833             break;
834         }
835         case EMR_POLYLINE:
836         {
837             EMRPOLYLINE *pEmr = (EMRPOLYLINE *) lpEMFR;
838             DWORD i;
840             if (pEmr->cptl<2)
841                 break;
843             if (!d->inpath) {
844                 assert_empty_path(d, "EMR_POLYLINE");
846                 *(d->outsvg) += "    <path ";
847                 output_style(d, EMR_STROKEPATH);
848                 *(d->outsvg) += "\n\td=\"";
849             }
851             tmp_str <<
852                 "\n\tM " <<
853                 pix_to_x_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " " <<
854                 pix_to_y_point( d, pEmr->aptl[0].x, pEmr->aptl[0].y ) << " ";
856             for (i=1; i<pEmr->cptl; i++) {
857                 tmp_str <<
858                     "\n\tL " <<
859                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
860                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
861             }
863             if (d->inpath) {
864                 tmp_path << tmp_str.str().c_str();
865             }
866             else {
867                 *(d->outsvg) += tmp_str.str().c_str();
868                 *(d->outsvg) += " \" /> \n";
869             }
871             break;
872         }
873         case EMR_POLYBEZIERTO:
874         {
875             PEMRPOLYBEZIERTO pEmr = (PEMRPOLYBEZIERTO) lpEMFR;
876             DWORD i,j;
878             if (d->path->empty()) {
879                 d->pathless_stroke = true;
880                 *(d->path) = "d=\"";
881             }
883             for (i=0; i<pEmr->cptl;) {
884                 tmp_path << "\n\tC ";
885                 for (j=0; j<3 && i<pEmr->cptl; j++,i++) {
886                     tmp_path <<
887                         pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
888                         pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
889                 }
890             }
892             break;
893         }
894         case EMR_POLYLINETO:
895         {
896             PEMRPOLYLINETO pEmr = (PEMRPOLYLINETO) lpEMFR;
897             DWORD i;
899             if (d->path->empty()) {
900                 d->pathless_stroke = true;
901                 *(d->path) = "d=\"";
902             }
904             for (i=0; i<pEmr->cptl;i++) {
905                 tmp_path <<
906                     "\n\tL " <<
907                     pix_to_x_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " " <<
908                     pix_to_y_point( d, pEmr->aptl[i].x, pEmr->aptl[i].y ) << " ";
909             }
911             break;
912         }
913         case EMR_POLYPOLYLINE:
914         case EMR_POLYPOLYGON:
915         {
916             PEMRPOLYPOLYGON pEmr = (PEMRPOLYPOLYGON) lpEMFR;
917             unsigned int n, i, j;
919             if (!d->inpath) {
920                 assert_empty_path(d, lpEMFR->iType == EMR_POLYPOLYGON ? "EMR_POLYPOLYGON" : "EMR_POLYPOLYLINE");
922                 *(d->outsvg) += "    <path ";
923                 output_style(d, lpEMFR->iType==EMR_POLYPOLYGON ? EMR_STROKEANDFILLPATH : EMR_STROKEPATH);
924                 *(d->outsvg) += "\n\td=\"";
925             }
927             POINTL *aptl = (POINTL *) &pEmr->aPolyCounts[pEmr->nPolys];
929             i = 0;
930             for (n=0; n<pEmr->nPolys && i<pEmr->cptl; n++) {
931                 SVGOStringStream poly_path;
933                 poly_path << "\n\tM " <<
934                     pix_to_x_point( d, aptl[i].x, aptl[i].y ) << " " <<
935                     pix_to_y_point( d, aptl[i].x, aptl[i].y ) << " ";
936                 i++;
938                 for (j=1; j<pEmr->aPolyCounts[n] && i<pEmr->cptl; j++) {
939                     poly_path << "\n\tL " <<
940                         pix_to_x_point( d, aptl[i].x, aptl[i].y ) << " " <<
941                         pix_to_y_point( d, aptl[i].x, aptl[i].y ) << " ";
942                     i++;
943                 }
945                 tmp_str << poly_path.str().c_str();
946                 if (lpEMFR->iType == EMR_POLYPOLYGON)
947                     tmp_str << " z";
948                 tmp_str << " \n";
949             }
951             if (d->inpath) {
952                 tmp_path << tmp_str.str().c_str();
953             }
954             else {
955                 *(d->outsvg) += tmp_str.str().c_str();
956                 *(d->outsvg) += " \" /> \n";
957             }
959             break;
960         }
961         case EMR_SETWINDOWEXTEX:
962         {
963             PEMRSETWINDOWEXTEX pEmr = (PEMRSETWINDOWEXTEX) lpEMFR;
965             *(d->outsvg) += "<!-- EMR_SETWINDOWEXTEX -->\n";
967             d->sizeWnd = pEmr->szlExtent;
968             d->PixelsX = d->sizeWnd.cx;
969             d->PixelsY = d->sizeWnd.cy;
971             d->ScaleX = d->xDPI / (100*d->MMX / d->PixelsX);
972             d->ScaleY = d->yDPI / (100*d->MMY / d->PixelsY);
974             break;
975         }
976         case EMR_SETWINDOWORGEX:
977         {
978             PEMRSETWINDOWORGEX pEmr = (PEMRSETWINDOWORGEX) lpEMFR;
979             d->winorg = pEmr->ptlOrigin;
980             break;
981         }
982         case EMR_SETVIEWPORTEXTEX:
983         {
984             PEMRSETVIEWPORTEXTEX pEmr = (PEMRSETVIEWPORTEXTEX) lpEMFR;
986             *(d->outsvg) += "<!-- EMR_SETVIEWPORTEXTEX -->\n";
988             d->sizeView = pEmr->szlExtent;
990             if (d->sizeWnd.cx && d->sizeWnd.cy) {
991                 /*
992                 HDC hScreenDC = GetDC( NULL );
994                 float scrPixelsX = (float)GetDeviceCaps( hScreenDC, HORZRES );
995                 float scrPixelsY = (float)GetDeviceCaps( hScreenDC, VERTRES );
996                 float scrMMX = (float)GetDeviceCaps( hScreenDC, HORZSIZE );
997                 float scrMMY = (float)GetDeviceCaps( hScreenDC, VERTSIZE );
999                 ReleaseDC( NULL, hScreenDC );
1001                 d->dwInchesX = d->sizeView.cx / (25.4f*scrPixelsX/scrMMX);
1002                 d->dwInchesY = d->sizeView.cy / (25.4f*scrPixelsY/scrMMY);
1003                 d->xDPI = d->sizeWnd.cx / d->dwInchesX;
1004                 d->yDPI = d->sizeWnd.cy / d->dwInchesY;
1005                 */
1006                 if (1) {
1007                     d->xDPI = 2540;
1008                     d->yDPI = 2540;
1009                     d->dwInchesX = d->PixelsX / d->xDPI;
1010                     d->dwInchesY = d->PixelsY / d->yDPI;
1011                     d->ScaleX = d->xDPI;
1012                     d->ScaleY = d->yDPI;
1013                 }
1015                 d->MMX = d->dwInchesX * MM_PER_IN;
1016                 d->MMY = d->dwInchesY * MM_PER_IN;
1017             }
1019             break;
1020         }
1021         case EMR_SETVIEWPORTORGEX:
1022         {
1023             PEMRSETVIEWPORTORGEX pEmr = (PEMRSETVIEWPORTORGEX) lpEMFR;
1024             d->vieworg = pEmr->ptlOrigin;
1025             break;
1026         }
1027         case EMR_SETBRUSHORGEX:
1028             *(d->outsvg) += "<!-- EMR_SETBRUSHORGEX -->\n";
1029             break;
1030         case EMR_EOF:
1031         {
1032             assert_empty_path(d, "EMR_EOF");
1033             tmp_outsvg << "</g>\n";
1034             tmp_outsvg << "</svg>\n";
1035             break;
1036         }
1037         case EMR_SETPIXELV:
1038             *(d->outsvg) += "<!-- EMR_SETPIXELV -->\n";
1039             break;
1040         case EMR_SETMAPPERFLAGS:
1041             *(d->outsvg) += "<!-- EMR_SETMAPPERFLAGS -->\n";
1042             break;
1043         case EMR_SETMAPMODE:
1044             *(d->outsvg) += "<!-- EMR_SETMAPMODE -->\n";
1045             break;
1046         case EMR_SETBKMODE:
1047             *(d->outsvg) += "<!-- EMR_SETBKMODE -->\n";
1048             break;
1049         case EMR_SETPOLYFILLMODE:
1050         {
1051             PEMRSETPOLYFILLMODE pEmr = (PEMRSETPOLYFILLMODE) lpEMFR;
1052             d->style.fill_rule.value =
1053                 (pEmr->iMode == ALTERNATE ? 0 :
1054                  pEmr->iMode == WINDING ? 1 : 0);
1055             break;
1056         }
1057         case EMR_SETROP2:
1058             *(d->outsvg) += "<!-- EMR_SETROP2 -->\n";
1059             break;
1060         case EMR_SETSTRETCHBLTMODE:
1061             *(d->outsvg) += "<!-- EMR_SETSTRETCHBLTMODE -->\n";
1062             break;
1063         case EMR_SETTEXTALIGN:
1064         {
1065             PEMRSETTEXTALIGN pEmr = (PEMRSETTEXTALIGN) lpEMFR;
1066             d->textAlign = pEmr->iMode;
1067             break;
1068         }
1069         case EMR_SETCOLORADJUSTMENT:
1070             *(d->outsvg) += "<!-- EMR_SETCOLORADJUSTMENT -->\n";
1071             break;
1072         case EMR_SETTEXTCOLOR:
1073         {
1074             PEMRSETTEXTCOLOR pEmr = (PEMRSETTEXTCOLOR) lpEMFR;
1075             d->textColor = pEmr->crColor;
1076             d->textColorSet = true;
1077             break;
1078         }
1079         case EMR_SETBKCOLOR:
1080             *(d->outsvg) += "<!-- EMR_SETBKCOLOR -->\n";
1081             break;
1082         case EMR_OFFSETCLIPRGN:
1083             *(d->outsvg) += "<!-- EMR_OFFSETCLIPRGN -->\n";
1084             break;
1085         case EMR_MOVETOEX:
1086         {
1087             PEMRMOVETOEX pEmr = (PEMRMOVETOEX) lpEMFR;
1089             if (d->path->empty()) {
1090                 d->pathless_stroke = true;
1091                 *(d->path) = "d=\"";
1092             }
1094             tmp_path <<
1095                 "\n\tM " <<
1096                 pix_to_x_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " " <<
1097                 pix_to_y_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " ";
1098             break;
1099         }
1100         case EMR_SETMETARGN:
1101             *(d->outsvg) += "<!-- EMR_SETMETARGN -->\n";
1102             break;
1103         case EMR_EXCLUDECLIPRECT:
1104             *(d->outsvg) += "<!-- EMR_EXCLUDECLIPRECT -->\n";
1105             break;
1106         case EMR_INTERSECTCLIPRECT:
1107             *(d->outsvg) += "<!-- EMR_INTERSECTCLIPRECT -->\n";
1108             break;
1109         case EMR_SCALEVIEWPORTEXTEX:
1110             *(d->outsvg) += "<!-- EMR_SCALEVIEWPORTEXTEX -->\n";
1111             break;
1112         case EMR_SCALEWINDOWEXTEX:
1113             *(d->outsvg) += "<!-- EMR_SCALEWINDOWEXTEX -->\n";
1114             break;
1115         case EMR_SAVEDC:
1116             *(d->outsvg) += "<!-- EMR_SAVEDC -->\n";
1117             break;
1118         case EMR_RESTOREDC:
1119             *(d->outsvg) += "<!-- EMR_RESTOREDC -->\n";
1120             break;
1121         case EMR_SETWORLDTRANSFORM:
1122         {
1123             PEMRSETWORLDTRANSFORM pEmr = (PEMRSETWORLDTRANSFORM) lpEMFR;
1124             d->worldTransform = pEmr->xform;
1125             break;
1126         }
1127         case EMR_MODIFYWORLDTRANSFORM:
1128         {
1129             PEMRMODIFYWORLDTRANSFORM pEmr = (PEMRMODIFYWORLDTRANSFORM) lpEMFR;
1130             switch (pEmr->iMode)
1131             {
1132                 case MWT_IDENTITY:
1133                     d->worldTransform.eM11 = 1.0;
1134                     d->worldTransform.eM12 = 0.0;
1135                     d->worldTransform.eM21 = 0.0;
1136                     d->worldTransform.eM22 = 1.0;
1137                     d->worldTransform.eDx  = 0.0;
1138                     d->worldTransform.eDy  = 0.0;
1139                     break;
1140                 case MWT_LEFTMULTIPLY:
1141                 {
1142 //                    d->worldTransform = pEmr->xform * worldTransform;
1144                     float a11 = pEmr->xform.eM11;
1145                     float a12 = pEmr->xform.eM12;
1146                     float a13 = 0.0;
1147                     float a21 = pEmr->xform.eM21;
1148                     float a22 = pEmr->xform.eM22;
1149                     float a23 = 0.0;
1150                     float a31 = pEmr->xform.eDx;
1151                     float a32 = pEmr->xform.eDy;
1152                     float a33 = 1.0;
1154                     float b11 = d->worldTransform.eM11;
1155                     float b12 = d->worldTransform.eM12;
1156                     //float b13 = 0.0;
1157                     float b21 = d->worldTransform.eM21;
1158                     float b22 = d->worldTransform.eM22;
1159                     //float b23 = 0.0;
1160                     float b31 = d->worldTransform.eDx;
1161                     float b32 = d->worldTransform.eDy;
1162                     //float b33 = 1.0;
1164                     float c11 = a11*b11 + a12*b21 + a13*b31;;
1165                     float c12 = a11*b12 + a12*b22 + a13*b32;;
1166                     //float c13 = a11*b13 + a12*b23 + a13*b33;;
1167                     float c21 = a21*b11 + a22*b21 + a23*b31;;
1168                     float c22 = a21*b12 + a22*b22 + a23*b32;;
1169                     //float c23 = a21*b13 + a22*b23 + a23*b33;;
1170                     float c31 = a31*b11 + a32*b21 + a33*b31;;
1171                     float c32 = a31*b12 + a32*b22 + a33*b32;;
1172                     //float c33 = a31*b13 + a32*b23 + a33*b33;;
1174                     d->worldTransform.eM11 = c11;;
1175                     d->worldTransform.eM12 = c12;;
1176                     d->worldTransform.eM21 = c21;;
1177                     d->worldTransform.eM22 = c22;;
1178                     d->worldTransform.eDx = c31;
1179                     d->worldTransform.eDy = c32;
1180                     
1181                     break;
1182                 }
1183                 case MWT_RIGHTMULTIPLY:
1184                 {
1185 //                    d->worldTransform = worldTransform * pEmr->xform;
1187                     float a11 = d->worldTransform.eM11;
1188                     float a12 = d->worldTransform.eM12;
1189                     float a13 = 0.0;
1190                     float a21 = d->worldTransform.eM21;
1191                     float a22 = d->worldTransform.eM22;
1192                     float a23 = 0.0;
1193                     float a31 = d->worldTransform.eDx;
1194                     float a32 = d->worldTransform.eDy;
1195                     float a33 = 1.0;
1197                     float b11 = pEmr->xform.eM11;
1198                     float b12 = pEmr->xform.eM12;
1199                     //float b13 = 0.0;
1200                     float b21 = pEmr->xform.eM21;
1201                     float b22 = pEmr->xform.eM22;
1202                     //float b23 = 0.0;
1203                     float b31 = pEmr->xform.eDx;
1204                     float b32 = pEmr->xform.eDy;
1205                     //float b33 = 1.0;
1207                     float c11 = a11*b11 + a12*b21 + a13*b31;;
1208                     float c12 = a11*b12 + a12*b22 + a13*b32;;
1209                     //float c13 = a11*b13 + a12*b23 + a13*b33;;
1210                     float c21 = a21*b11 + a22*b21 + a23*b31;;
1211                     float c22 = a21*b12 + a22*b22 + a23*b32;;
1212                     //float c23 = a21*b13 + a22*b23 + a23*b33;;
1213                     float c31 = a31*b11 + a32*b21 + a33*b31;;
1214                     float c32 = a31*b12 + a32*b22 + a33*b32;;
1215                     //float c33 = a31*b13 + a32*b23 + a33*b33;;
1217                     d->worldTransform.eM11 = c11;;
1218                     d->worldTransform.eM12 = c12;;
1219                     d->worldTransform.eM21 = c21;;
1220                     d->worldTransform.eM22 = c22;;
1221                     d->worldTransform.eDx = c31;
1222                     d->worldTransform.eDy = c32;
1224                     break;
1225                 }
1226 //                case MWT_SET:
1227                 default:
1228                     d->worldTransform = pEmr->xform;
1229                     break;
1230             }
1231             break;
1232         }
1233         case EMR_SELECTOBJECT:
1234         {
1235             PEMRSELECTOBJECT pEmr = (PEMRSELECTOBJECT) lpEMFR;
1236             unsigned int index = pEmr->ihObject;
1238             if (index >= ENHMETA_STOCK_OBJECT) {
1239                 index -= ENHMETA_STOCK_OBJECT;
1240                 switch (index) {
1241                     case NULL_BRUSH:
1242                         d->fill_set = false;
1243                         break;
1244                     case BLACK_BRUSH:
1245                     case DKGRAY_BRUSH:
1246                     case GRAY_BRUSH:
1247                     case LTGRAY_BRUSH:
1248                     case WHITE_BRUSH:
1249                     {
1250                         float val = 0;
1251                         switch (index) {
1252                             case BLACK_BRUSH:
1253                                 val = 0.0 / 255.0;
1254                                 break;
1255                             case DKGRAY_BRUSH:
1256                                 val = 64.0 / 255.0;
1257                                 break;
1258                             case GRAY_BRUSH:
1259                                 val = 128.0 / 255.0;
1260                                 break;
1261                             case LTGRAY_BRUSH:
1262                                 val = 192.0 / 255.0;
1263                                 break;
1264                             case WHITE_BRUSH:
1265                                 val = 255.0 / 255.0;
1266                                 break;
1267                         }
1268                         d->style.fill.value.color.set( val, val, val );
1270                         d->fill_set = true;
1271                         break;
1272                     }
1273                     case NULL_PEN:
1274                         d->stroke_set = false;
1275                         break;
1276                     case BLACK_PEN:
1277                     case WHITE_PEN:
1278                     {
1279                         float val = index == BLACK_PEN ? 0 : 1;
1280                         d->style.stroke_dasharray_set = 0;
1281                         d->style.stroke_width.value = 1.0;
1282                         d->style.stroke.value.color.set( val, val, val );
1284                         d->stroke_set = true;
1286                         break;
1287                     }
1288                 }
1289             } else {
1290                 if ( /*index >= 0 &&*/ index < (unsigned int) d->n_obj) {
1291                     switch (d->emf_obj[index].type)
1292                     {
1293                         case EMR_CREATEPEN:
1294                             select_pen(d, index);
1295                             break;
1296                         case EMR_CREATEBRUSHINDIRECT:
1297                             select_brush(d, index);
1298                             break;
1299                         case EMR_EXTCREATEPEN:
1300                             select_extpen(d, index);
1301                             break;
1302                         case EMR_EXTCREATEFONTINDIRECTW:
1303                             select_font(d, index);
1304                             break;
1305                     }
1306                 }
1307             }
1308             break;
1309         }
1310         case EMR_CREATEPEN:
1311         {
1312             PEMRCREATEPEN pEmr = (PEMRCREATEPEN) lpEMFR;
1313             int index = pEmr->ihPen;
1315             EMRCREATEPEN *pPen =
1316                 (EMRCREATEPEN *) malloc( sizeof(EMRCREATEPEN) );
1317             pPen->lopn = pEmr->lopn;
1318             insert_object(d, index, EMR_CREATEPEN, (ENHMETARECORD *) pPen);
1320             break;
1321         }
1322         case EMR_CREATEBRUSHINDIRECT:
1323         {
1324             PEMRCREATEBRUSHINDIRECT pEmr = (PEMRCREATEBRUSHINDIRECT) lpEMFR;
1325             int index = pEmr->ihBrush;
1327             EMRCREATEBRUSHINDIRECT *pBrush =
1328                 (EMRCREATEBRUSHINDIRECT *) malloc( sizeof(EMRCREATEBRUSHINDIRECT) );
1329             pBrush->lb = pEmr->lb;
1330             insert_object(d, index, EMR_CREATEBRUSHINDIRECT, (ENHMETARECORD *) pBrush);
1332             break;
1333         }
1334         case EMR_DELETEOBJECT:
1335             break;
1336         case EMR_ANGLEARC:
1337             *(d->outsvg) += "<!-- EMR_ANGLEARC -->\n";
1338             break;
1339         case EMR_ELLIPSE:
1340         {
1341             PEMRELLIPSE pEmr = (PEMRELLIPSE) lpEMFR;
1342             RECTL rclBox = pEmr->rclBox;
1344             double l = pix_to_x_point( d, pEmr->rclBox.left, pEmr->rclBox.top );
1345             double t = pix_to_y_point( d, pEmr->rclBox.left, pEmr->rclBox.top );
1346             double r = pix_to_x_point( d, pEmr->rclBox.right, pEmr->rclBox.bottom );
1347             double b = pix_to_y_point( d, pEmr->rclBox.right, pEmr->rclBox.bottom );
1349             double cx = (l + r) / 2.0;
1350             double cy = (t + b) / 2.0;
1351             double rx = fabs(l - r) / 2.0;
1352             double ry = fabs(t - b) / 2.0;
1354             SVGOStringStream tmp_ellipse;
1355             tmp_ellipse << "cx=\"" << cx << "\" ";
1356             tmp_ellipse << "cy=\"" << cy << "\" ";
1357             tmp_ellipse << "rx=\"" << rx << "\" ";
1358             tmp_ellipse << "ry=\"" << ry << "\" ";
1360             assert_empty_path(d, "EMR_ELLIPSE");
1362             *(d->outsvg) += "    <ellipse ";
1363             output_style(d, lpEMFR->iType);
1364             *(d->outsvg) += "\n\t";
1365             *(d->outsvg) += tmp_ellipse.str().c_str();
1366             *(d->outsvg) += "/> \n";
1367             *(d->path) = "";
1368             break;
1369         }
1370         case EMR_RECTANGLE:
1371         {
1372             PEMRRECTANGLE pEmr = (PEMRRECTANGLE) lpEMFR;
1373             RECTL rc = pEmr->rclBox;
1375             double l = pix_to_x_point( d, rc.left, rc.top );
1376             double t = pix_to_y_point( d, rc.left, rc.top );
1377             double r = pix_to_x_point( d, rc.right, rc.bottom );
1378             double b = pix_to_y_point( d, rc.right, rc.bottom );
1380             SVGOStringStream tmp_rectangle;
1381             tmp_rectangle << "d=\"";
1382             tmp_rectangle << "\n\tM " << l << " " << t << " ";
1383             tmp_rectangle << "\n\tL " << r << " " << t << " ";
1384             tmp_rectangle << "\n\tL " << r << " " << b << " ";
1385             tmp_rectangle << "\n\tL " << l << " " << b << " ";
1386             tmp_rectangle << "\n\tz";
1388             assert_empty_path(d, "EMR_RECTANGLE");
1390             *(d->outsvg) += "    <path ";
1391             output_style(d, lpEMFR->iType);
1392             *(d->outsvg) += "\n\t";
1393             *(d->outsvg) += tmp_rectangle.str().c_str();
1394             *(d->outsvg) += " \" /> \n";
1395             *(d->path) = "";
1396             break;
1397         }
1398         case EMR_ROUNDRECT:
1399             *(d->outsvg) += "<!-- EMR_ROUNDRECT -->\n";
1400             break;
1401         case EMR_ARC:
1402             *(d->outsvg) += "<!-- EMR_ARC -->\n";
1403             break;
1404         case EMR_CHORD:
1405             *(d->outsvg) += "<!-- EMR_CHORD -->\n";
1406             break;
1407         case EMR_PIE:
1408             *(d->outsvg) += "<!-- EMR_PIE -->\n";
1409             break;
1410         case EMR_SELECTPALETTE:
1411             *(d->outsvg) += "<!-- EMR_SELECTPALETTE -->\n";
1412             break;
1413         case EMR_CREATEPALETTE:
1414             *(d->outsvg) += "<!-- EMR_CREATEPALETTE -->\n";
1415             break;
1416         case EMR_SETPALETTEENTRIES:
1417             *(d->outsvg) += "<!-- EMR_SETPALETTEENTRIES -->\n";
1418             break;
1419         case EMR_RESIZEPALETTE:
1420             *(d->outsvg) += "<!-- EMR_RESIZEPALETTE -->\n";
1421             break;
1422         case EMR_REALIZEPALETTE:
1423             *(d->outsvg) += "<!-- EMR_REALIZEPALETTE -->\n";
1424             break;
1425         case EMR_EXTFLOODFILL:
1426             *(d->outsvg) += "<!-- EMR_EXTFLOODFILL -->\n";
1427             break;
1428         case EMR_LINETO:
1429         {
1430             PEMRLINETO pEmr = (PEMRLINETO) lpEMFR;
1432             if (d->path->empty()) {
1433                 d->pathless_stroke = true;
1434                 *(d->path) = "d=\"";
1435             }
1437             tmp_path <<
1438                 "\n\tL " <<
1439                 pix_to_x_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " " <<
1440                 pix_to_y_point( d, pEmr->ptl.x, pEmr->ptl.y ) << " ";
1441             break;
1442         }
1443         case EMR_ARCTO:
1444             *(d->outsvg) += "<!-- EMR_ARCTO -->\n";
1445             break;
1446         case EMR_POLYDRAW:
1447             *(d->outsvg) += "<!-- EMR_POLYDRAW -->\n";
1448             break;
1449         case EMR_SETARCDIRECTION:
1450             *(d->outsvg) += "<!-- EMR_SETARCDIRECTION -->\n";
1451             break;
1452         case EMR_SETMITERLIMIT:
1453         {
1454             PEMRSETMITERLIMIT pEmr = (PEMRSETMITERLIMIT) lpEMFR;
1455             d->style.stroke_miterlimit.value = pix_to_size_point( d, pEmr->eMiterLimit );
1457             if (d->style.stroke_miterlimit.value < 1)
1458                 d->style.stroke_miterlimit.value = 1.0;
1460             break;
1461         }
1462         case EMR_BEGINPATH:
1463         {
1464             tmp_path << "d=\"";
1465             *(d->path) = "";
1466             d->inpath = true;
1467             break;
1468         }
1469         case EMR_ENDPATH:
1470         {
1471             tmp_path << "\"";
1472             d->inpath = false;
1473             break;
1474         }
1475         case EMR_CLOSEFIGURE:
1476         {
1477             tmp_path << "\n\tz";
1478             break;
1479         }
1480         case EMR_FILLPATH:
1481         case EMR_STROKEANDFILLPATH:
1482         case EMR_STROKEPATH:
1483         {
1484             *(d->outsvg) += "    <path ";
1485             output_style(d, lpEMFR->iType);
1486             *(d->outsvg) += "\n\t";
1487             *(d->outsvg) += *(d->path);
1488             *(d->outsvg) += " /> \n";
1489             *(d->path) = "";
1490             break;
1491         }
1492         case EMR_FLATTENPATH:
1493             *(d->outsvg) += "<!-- EMR_FLATTENPATH -->\n";
1494             break;
1495         case EMR_WIDENPATH:
1496             *(d->outsvg) += "<!-- EMR_WIDENPATH -->\n";
1497             break;
1498         case EMR_SELECTCLIPPATH:
1499             *(d->outsvg) += "<!-- EMR_SELECTCLIPPATH -->\n";
1500             break;
1501         case EMR_ABORTPATH:
1502             *(d->outsvg) += "<!-- EMR_ABORTPATH -->\n";
1503             break;
1504         case EMR_GDICOMMENT:
1505             *(d->outsvg) += "<!-- EMR_GDICOMMENT -->\n";
1506             break;
1507         case EMR_FILLRGN:
1508             *(d->outsvg) += "<!-- EMR_FILLRGN -->\n";
1509             break;
1510         case EMR_FRAMERGN:
1511             *(d->outsvg) += "<!-- EMR_FRAMERGN -->\n";
1512             break;
1513         case EMR_INVERTRGN:
1514             *(d->outsvg) += "<!-- EMR_INVERTRGN -->\n";
1515             break;
1516         case EMR_PAINTRGN:
1517             *(d->outsvg) += "<!-- EMR_PAINTRGN -->\n";
1518             break;
1519         case EMR_EXTSELECTCLIPRGN:
1520             *(d->outsvg) += "<!-- EMR_EXTSELECTCLIPRGN -->\n";
1521             break;
1522         case EMR_BITBLT:
1523             *(d->outsvg) += "<!-- EMR_BITBLT -->\n";
1524             break;
1525         case EMR_STRETCHBLT:
1526             *(d->outsvg) += "<!-- EMR_STRETCHBLT -->\n";
1527             break;
1528         case EMR_MASKBLT:
1529             *(d->outsvg) += "<!-- EMR_MASKBLT -->\n";
1530             break;
1531         case EMR_PLGBLT:
1532             *(d->outsvg) += "<!-- EMR_PLGBLT -->\n";
1533             break;
1534         case EMR_SETDIBITSTODEVICE:
1535             *(d->outsvg) += "<!-- EMR_SETDIBITSTODEVICE -->\n";
1536             break;
1537         case EMR_STRETCHDIBITS:
1538             *(d->outsvg) += "<!-- EMR_STRETCHDIBITS -->\n";
1539             break;
1540         case EMR_EXTCREATEFONTINDIRECTW:
1541         {
1542             PEMREXTCREATEFONTINDIRECTW pEmr = (PEMREXTCREATEFONTINDIRECTW) lpEMFR;
1543             int index = pEmr->ihFont;
1545             EMREXTCREATEFONTINDIRECTW *pFont =
1546                 (EMREXTCREATEFONTINDIRECTW *) malloc( sizeof(EMREXTCREATEFONTINDIRECTW) );
1547             pFont->elfw = pEmr->elfw;
1548             insert_object(d, index, EMR_EXTCREATEFONTINDIRECTW, (ENHMETARECORD *) pFont);
1550             break;
1551         }
1552         case EMR_EXTTEXTOUTA:
1553         {
1554             *(d->outsvg) += "<!-- EMR_EXTTEXTOUTA -->\n";
1555             break;
1556         }
1557         case EMR_EXTTEXTOUTW:
1558         {
1559             PEMREXTTEXTOUTW pEmr = (PEMREXTTEXTOUTW) lpEMFR;
1561             double x1 = pEmr->emrtext.ptlReference.x;
1562             double y1 = pEmr->emrtext.ptlReference.y;
1563             
1564             if (!(d->textAlign & TA_BOTTOM))
1565                 y1 += fabs(d->style.font_size.computed);
1566             
1567             double x = pix_to_x_point(d, x1, y1);
1568             double y = pix_to_y_point(d, x1, y1);
1570             wchar_t *wide_text = (wchar_t *) ((char *) pEmr + pEmr->emrtext.offString);
1572             gchar *ansi_text =
1573                 (gchar *) g_utf16_to_utf8( (gunichar2 *) wide_text, pEmr->emrtext.nChars, NULL, NULL, NULL );
1575             if (ansi_text) {
1576                 gchar *p = ansi_text;
1577                 while (*p) {
1578                     if (*p < 32 || *p >= 127) {
1579                         g_free(ansi_text);
1580                         ansi_text = g_strdup("");
1581                         break;
1582                     }
1583                     p++;
1584                 }
1586                 SVGOStringStream ts;
1588                 gchar *escaped_text = g_markup_escape_text(ansi_text, -1);
1590                 float text_rgb[3];
1591                 sp_color_get_rgb_floatv( &(d->style.fill.value.color), text_rgb );
1593                 if (!d->textColorSet) {
1594                     d->textColor = RGB(SP_COLOR_F_TO_U(text_rgb[0]),
1595                                        SP_COLOR_F_TO_U(text_rgb[1]),
1596                                        SP_COLOR_F_TO_U(text_rgb[2]));
1597                 }
1599                 char tmp[128];
1600                 snprintf(tmp, 127,
1601                          "fill:#%02x%02x%02x;",
1602                          GetRValue(d->textColor),
1603                          GetGValue(d->textColor),
1604                          GetBValue(d->textColor));
1606                 bool i = (d->style.font_style.value == SP_CSS_FONT_STYLE_ITALIC);
1607                 //bool o = (d->style.font_style.value == SP_CSS_FONT_STYLE_OBLIQUE);
1608                 bool b = (d->style.font_weight.value == SP_CSS_FONT_WEIGHT_BOLD) ||
1609                     (d->style.font_weight.value >= SP_CSS_FONT_WEIGHT_500 && d->style.font_weight.value <= SP_CSS_FONT_WEIGHT_900);
1610                 int lcr = ((d->textAlign & TA_CENTER) == TA_CENTER) ? 2 : ((d->textAlign & TA_RIGHT) == TA_RIGHT) ? 1 : 0;
1612                 assert_empty_path(d, "EMR_EXTTEXTOUTW");
1614                 ts << "    <text\n";
1615                 ts << "        id=\"" << (d->id++) << "\"\n";
1616                 ts << "        xml:space=\"preserve\"\n";
1617                 ts << "        x=\"" << x << "\"\n";
1618                 ts << "        y=\"" << y << "\"\n";
1619                 ts << "        style=\""
1620                    << "font-size:" << fabs(d->style.font_size.computed) << "px;"
1621                    << tmp
1622                    << "font-style:" << (i ? "italic" : "normal") << ";"
1623                    << "font-weight:" << (b ? "bold" : "normal") << ";"
1624                    << "text-align:" << (lcr==2 ? "center" : lcr==1 ? "end" : "start") << ";"
1625                    << "text-anchor:" << (lcr==2 ? "middle" : lcr==1 ? "end" : "start") << ";"
1626                    << "font-family:" << d->tstyle.font_family.value << ";"
1627                    << "\"\n";
1628                 ts << "    >";
1629                 ts << escaped_text;
1630                 ts << "</text>\n";
1631                 
1632                 *(d->outsvg) += ts.str().c_str();
1633                 
1634                 g_free(escaped_text);
1635                 g_free(ansi_text);
1636             }
1637             
1638             break;
1639         }
1640         case EMR_POLYBEZIER16:
1641         {
1642             PEMRPOLYBEZIER16 pEmr = (PEMRPOLYBEZIER16) lpEMFR;
1643             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1644             DWORD i,j;
1646             if (pEmr->cpts<4)
1647                 break;
1649             if (!d->inpath) {
1650                 assert_empty_path(d, "EMR_POLYBEZIER16");
1652                 *(d->outsvg) += "    <path ";
1653                 output_style(d, EMR_STROKEPATH);
1654                 *(d->outsvg) += "\n\td=\"";
1655             }
1657             tmp_str <<
1658                 "\n\tM " <<
1659                 pix_to_x_point( d, apts[0].x, apts[0].y ) << " " <<
1660                 pix_to_y_point( d, apts[0].x, apts[0].y ) << " ";
1662             for (i=1; i<pEmr->cpts; ) {
1663                 tmp_str << "\n\tC ";
1664                 for (j=0; j<3 && i<pEmr->cpts; j++,i++) {
1665                     tmp_str <<
1666                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1667                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1668                 }
1669             }
1671             if (d->inpath) {
1672                 tmp_path << tmp_str.str().c_str();
1673             }
1674             else {
1675                 *(d->outsvg) += tmp_str.str().c_str();
1676                 *(d->outsvg) += " \" /> \n";
1677             }
1679             break;
1680         }
1681         case EMR_POLYGON16:
1682         {
1683             PEMRPOLYGON16 pEmr = (PEMRPOLYGON16) lpEMFR;
1684             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1685             SVGOStringStream tmp_poly;
1686             unsigned int i;
1687             unsigned int first = 0;
1689             assert_empty_path(d, "EMR_POLYGON16");
1691             *(d->outsvg) += "    <path ";
1692             output_style(d, EMR_STROKEANDFILLPATH);
1693             *(d->outsvg) += "\n\td=\"";
1694             
1695             // skip the first point?
1696             tmp_poly << "\n\tM " <<
1697                 pix_to_x_point( d, apts[first].x, apts[first].y ) << " " <<
1698                 pix_to_y_point( d, apts[first].x, apts[first].y ) << " ";
1700             for (i=first+1; i<pEmr->cpts; i++) {
1701                 tmp_poly << "\n\tL " <<
1702                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1703                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1704             }
1706             *(d->outsvg) += tmp_poly.str().c_str();
1707             *(d->outsvg) += " z \" /> \n";
1709             break;
1710         }
1711         case EMR_POLYLINE16:
1712         {
1713             EMRPOLYLINE16 *pEmr = (EMRPOLYLINE16 *) lpEMFR;
1714             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1715             DWORD i;
1717             if (pEmr->cpts<2)
1718                 break;
1720             if (!d->inpath) {
1721                 assert_empty_path(d, "EMR_POLYLINE16");
1723                 *(d->outsvg) += "    <path ";
1724                 output_style(d, EMR_STROKEPATH);
1725                 *(d->outsvg) += "\n\td=\"";
1726             }
1728             tmp_str <<
1729                 "\n\tM " <<
1730                 pix_to_x_point( d, apts[0].x, apts[0].y ) << " " <<
1731                 pix_to_y_point( d, apts[0].x, apts[0].y ) << " ";
1733             for (i=1; i<pEmr->cpts; i++) {
1734                 tmp_str <<
1735                     "\n\tL " <<
1736                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1737                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1738             }
1740             if (d->inpath) {
1741                 tmp_path << tmp_str.str().c_str();
1742             }
1743             else {
1744                 *(d->outsvg) += tmp_str.str().c_str();
1745                 *(d->outsvg) += " \" /> \n";
1746             }
1748             break;
1749         }
1750         case EMR_POLYBEZIERTO16:
1751         {
1752             PEMRPOLYBEZIERTO16 pEmr = (PEMRPOLYBEZIERTO16) lpEMFR;
1753             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1754             DWORD i,j;
1756             if (d->path->empty()) {
1757                 d->pathless_stroke = true;
1758                 *(d->path) = "d=\"";
1759             }
1761             for (i=0; i<pEmr->cpts;) {
1762                 tmp_path << "\n\tC ";
1763                 for (j=0; j<3 && i<pEmr->cpts; j++,i++) {
1764                     tmp_path <<
1765                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1766                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1767                 }
1768             }
1770             break;
1771         }
1772         case EMR_POLYLINETO16:
1773         {
1774             PEMRPOLYLINETO16 pEmr = (PEMRPOLYLINETO16) lpEMFR;
1775             POINTS *apts = (POINTS *) pEmr->apts; // Bug in MinGW wingdi.h ?
1776             DWORD i;
1778             if (d->path->empty()) {
1779                 d->pathless_stroke = true;
1780                 *(d->path) = "d=\"";
1781             }
1783             for (i=0; i<pEmr->cpts;i++) {
1784                 tmp_path <<
1785                     "\n\tL " <<
1786                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1787                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1788             }
1790             break;
1791         }
1792         case EMR_POLYPOLYLINE16:
1793         case EMR_POLYPOLYGON16:
1794         {
1795             PEMRPOLYPOLYGON16 pEmr = (PEMRPOLYPOLYGON16) lpEMFR;
1796             unsigned int n, i, j;
1798             if (!d->inpath) {
1799                 assert_empty_path(d, lpEMFR->iType == EMR_POLYPOLYGON16 ? "EMR_POLYPOLYGON16" : "EMR_POLYPOLYLINE16");
1801                 *(d->outsvg) += "    <path ";
1802                 output_style(d, lpEMFR->iType==EMR_POLYPOLYGON16 ? EMR_STROKEANDFILLPATH : EMR_STROKEPATH);
1803                 *(d->outsvg) += "\n\td=\"";
1804             }
1806             POINTS *apts = (POINTS *) &pEmr->aPolyCounts[pEmr->nPolys];
1808             i = 0;
1809             for (n=0; n<pEmr->nPolys && i<pEmr->cpts; n++) {
1810                 SVGOStringStream poly_path;
1812                 poly_path << "\n\tM " <<
1813                     pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1814                     pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1815                 i++;
1817                 for (j=1; j<pEmr->aPolyCounts[n] && i<pEmr->cpts; j++) {
1818                     poly_path << "\n\tL " <<
1819                         pix_to_x_point( d, apts[i].x, apts[i].y ) << " " <<
1820                         pix_to_y_point( d, apts[i].x, apts[i].y ) << " ";
1821                     i++;
1822                 }
1824                 tmp_str << poly_path.str().c_str();
1825                 if (lpEMFR->iType == EMR_POLYPOLYGON16)
1826                     tmp_str << " z";
1827                 tmp_str << " \n";
1828             }
1830             if (d->inpath) {
1831                 tmp_path << tmp_str.str().c_str();
1832             }
1833             else {
1834                 *(d->outsvg) += tmp_str.str().c_str();
1835                 *(d->outsvg) += " \" /> \n";
1836             }
1838             break;
1839         }
1840         case EMR_POLYDRAW16:
1841             *(d->outsvg) += "<!-- EMR_POLYDRAW16 -->\n";
1842             break;
1843         case EMR_CREATEMONOBRUSH:
1844             *(d->outsvg) += "<!-- EMR_CREATEMONOBRUSH -->\n";
1845             break;
1846         case EMR_CREATEDIBPATTERNBRUSHPT:
1847             *(d->outsvg) += "<!-- EMR_CREATEDIBPATTERNBRUSHPT -->\n";
1848             break;
1849         case EMR_EXTCREATEPEN:
1850         {
1851             PEMREXTCREATEPEN pEmr = (PEMREXTCREATEPEN) lpEMFR;
1852             int index = pEmr->ihPen;
1854             EMREXTCREATEPEN *pPen =
1855                 (EMREXTCREATEPEN *) malloc( sizeof(EMREXTCREATEPEN) +
1856                                             sizeof(DWORD) * pEmr->elp.elpNumEntries );
1857             pPen->ihPen = pEmr->ihPen;
1858             pPen->offBmi = pEmr->offBmi;
1859             pPen->cbBmi = pEmr->cbBmi;
1860             pPen->offBits = pEmr->offBits;
1861             pPen->cbBits = pEmr->cbBits;
1862             pPen->elp = pEmr->elp;
1863             for (unsigned int i=0; i<pEmr->elp.elpNumEntries; i++) {
1864                 pPen->elp.elpStyleEntry[i] = pEmr->elp.elpStyleEntry[i];
1865             }
1866             insert_object(d, index, EMR_EXTCREATEPEN, (ENHMETARECORD *) pPen);
1868             break;
1869         }
1870         case EMR_POLYTEXTOUTA:
1871             *(d->outsvg) += "<!-- EMR_POLYTEXTOUTA -->\n";
1872             break;
1873         case EMR_POLYTEXTOUTW:
1874             *(d->outsvg) += "<!-- EMR_POLYTEXTOUTW -->\n";
1875             break;
1876         case EMR_SETICMMODE:
1877             *(d->outsvg) += "<!-- EMR_SETICMMODE -->\n";
1878             break;
1879         case EMR_CREATECOLORSPACE:
1880             *(d->outsvg) += "<!-- EMR_CREATECOLORSPACE -->\n";
1881             break;
1882         case EMR_SETCOLORSPACE:
1883             *(d->outsvg) += "<!-- EMR_SETCOLORSPACE -->\n";
1884             break;
1885         case EMR_DELETECOLORSPACE:
1886             *(d->outsvg) += "<!-- EMR_DELETECOLORSPACE -->\n";
1887             break;
1888         case EMR_GLSRECORD:
1889             *(d->outsvg) += "<!-- EMR_GLSRECORD -->\n";
1890             break;
1891         case EMR_GLSBOUNDEDRECORD:
1892             *(d->outsvg) += "<!-- EMR_GLSBOUNDEDRECORD -->\n";
1893             break;
1894         case EMR_PIXELFORMAT:
1895             *(d->outsvg) += "<!-- EMR_PIXELFORMAT -->\n";
1896             break;
1897     }
1898     
1899     *(d->outsvg) += tmp_outsvg.str().c_str();
1900     *(d->path) += tmp_path.str().c_str();
1902     return 1;
1905 static int CALLBACK
1906 myMetaFileProc(HDC hDC, HANDLETABLE *lpHTable, METARECORD *lpMFR, int nObj, LPARAM lpData)
1908     g_warning("Unable to import Windows Meta File.\n");
1909     return 0;
1912 // Aldus Placeable Header ===================================================
1913 // Since we are a 32bit app, we have to be sure this structure compiles to
1914 // be identical to a 16 bit app's version. To do this, we use the #pragma
1915 // to adjust packing, we use a WORD for the hmf handle, and a SMALL_RECT
1916 // for the bbox rectangle.
1917 #pragma pack( push )
1918 #pragma pack( 2 )
1919 typedef struct
1921         DWORD           dwKey;
1922         WORD            hmf;
1923         SMALL_RECT      bbox;
1924         WORD            wInch;
1925         DWORD           dwReserved;
1926         WORD            wCheckSum;
1927 } APMHEADER, *PAPMHEADER;
1928 #pragma pack( pop )
1931 SPDocument *
1932 EmfWin32::open( Inkscape::Extension::Input *mod, const gchar *uri )
1934     EMF_CALLBACK_DATA d;
1936     memset(&d, 0, sizeof(d));
1938     d.worldTransform.eM11 = 1.0;
1939     d.worldTransform.eM12 = 0.0;
1940     d.worldTransform.eM21 = 0.0;
1941     d.worldTransform.eM22 = 1.0;
1942     d.worldTransform.eDx  = 0.0;
1943     d.worldTransform.eDy  = 0.0;
1944     
1945     gsize bytesRead = 0;
1946     gsize bytesWritten = 0;
1947     GError* error = NULL;
1948     gchar *local_fn =
1949         g_filename_from_utf8( uri, -1,  &bytesRead,  &bytesWritten, &error );
1951     if (local_fn == NULL) {
1952         return NULL;
1953     }
1955     d.outsvg = new Glib::ustring("");
1956     d.path = new Glib::ustring("");
1958     CHAR *ansi_uri = (CHAR *) local_fn;
1959     gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
1960     WCHAR *unicode_uri = (WCHAR *) unicode_fn;
1962     DWORD filesize = 0;
1963     HANDLE fp = NULL;
1965     HMETAFILE hmf;
1966     HENHMETAFILE hemf;
1968     if (PrintWin32::is_os_wide()) {
1969         fp = CreateFileW(unicode_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1970     }
1971     else {
1972         fp = CreateFileA(ansi_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1973     }
1975     if ( fp != INVALID_HANDLE_VALUE ) {
1976         filesize = GetFileSize(fp, NULL);
1977         CloseHandle(fp);
1978     }
1980     // Try open as Enhanced Metafile
1981     if (PrintWin32::is_os_wide())
1982         hemf = GetEnhMetaFileW(unicode_uri);
1983     else
1984         hemf = GetEnhMetaFileA(ansi_uri);
1986     if (!hemf) {
1987         // Try open as Windows Metafile
1988         if (PrintWin32::is_os_wide())
1989             hmf = GetMetaFileW(unicode_uri);
1990         else
1991             hmf = GetMetaFileA(ansi_uri);
1993         METAFILEPICT mp;
1994         HDC hDC;
1996         if (!hmf) {
1997             if (PrintWin32::is_os_wide()) {
1998                 WCHAR szTemp[MAX_PATH];
2000                 DWORD dw = GetShortPathNameW( unicode_uri, szTemp, MAX_PATH );
2001                 if (dw) {
2002                     hmf = GetMetaFileW( szTemp );
2003                 }
2004             } else {
2005                 CHAR szTemp[MAX_PATH];
2007                 DWORD dw = GetShortPathNameA( ansi_uri, szTemp, MAX_PATH );
2008                 if (dw) {
2009                     hmf = GetMetaFileA( szTemp );
2010                 }
2011             }
2012         }
2014         if (hmf) {
2015             DWORD nSize = GetMetaFileBitsEx( hmf, 0, NULL );
2017             if (!nSize)
2018                 nSize = filesize;
2019             
2020             if (nSize) {
2021                 BYTE *lpvData = new BYTE[nSize];
2022                 if (lpvData) {
2023                     DWORD dw = GetMetaFileBitsEx( hmf, nSize, lpvData );
2024                     if (dw) {
2025                         // Fill out a METAFILEPICT structure
2026                         mp.mm = MM_ANISOTROPIC;
2027                         mp.xExt = 1000;
2028                         mp.yExt = 1000;
2029                         mp.hMF = NULL;
2030                         // Get a reference DC
2031                         hDC = GetDC( NULL );
2032                         // Make an enhanced metafile from the windows metafile
2033                         hemf = SetWinMetaFileBits( nSize, lpvData, hDC, &mp );
2034                         // Clean up
2035                         ReleaseDC( NULL, hDC );
2036                         DeleteMetaFile( hmf );
2037                         hmf = NULL;
2038                     }
2039                     else {
2040                         // EnumMetaFile
2041                     }
2042                     delete[] lpvData;
2043                 }
2044                 else {
2045                     DeleteMetaFile( hmf );
2046                     hmf = NULL;
2047                 }
2048             }
2049             else {
2050                 DeleteMetaFile( hmf );
2051                 hmf = NULL;
2052             }
2053         }
2054         else {
2055             // Try open as Aldus Placeable Metafile
2056             HANDLE hFile;
2057             if (PrintWin32::is_os_wide())
2058                 hFile = CreateFileW( unicode_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
2059             else
2060                 hFile = CreateFileA( ansi_uri, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
2062             if (hFile != INVALID_HANDLE_VALUE) {
2063                 DWORD nSize = GetFileSize( hFile, NULL );
2064                 if (nSize) {
2065                     BYTE *lpvData = new BYTE[nSize];
2066                     if (lpvData) {
2067                         DWORD dw = ReadFile( hFile, lpvData, nSize, &nSize, NULL );
2068                         if (dw) {
2069                             if ( ((PAPMHEADER)lpvData)->dwKey == 0x9ac6cdd7l ) {
2070                                 // Fill out a METAFILEPICT structure
2071                                 mp.mm = MM_ANISOTROPIC;
2072                                 mp.xExt = ((PAPMHEADER)lpvData)->bbox.Right - ((PAPMHEADER)lpvData)->bbox.Left;
2073                                 mp.xExt = ( mp.xExt * 2540l ) / (DWORD)(((PAPMHEADER)lpvData)->wInch);
2074                                 mp.yExt = ((PAPMHEADER)lpvData)->bbox.Bottom - ((PAPMHEADER)lpvData)->bbox.Top;
2075                                 mp.yExt = ( mp.yExt * 2540l ) / (DWORD)(((PAPMHEADER)lpvData)->wInch);
2076                                 mp.hMF = NULL;
2077                                 // Get a reference DC
2078                                 hDC = GetDC( NULL );
2079                                 // Create an enhanced metafile from the bits
2080                                 hemf = SetWinMetaFileBits( nSize, lpvData+sizeof(APMHEADER), hDC, &mp );
2081                                 // Clean up
2082                                 ReleaseDC( NULL, hDC );
2083                             }
2084                         }
2085                         delete[] lpvData;
2086                     }
2087                 }
2088                 CloseHandle( hFile );
2089             }
2090         }
2091     }
2093     if ((!hemf && !hmf) || !d.outsvg || !d.path) {
2094         if (d.outsvg)
2095             delete d.outsvg;
2096         if (d.path)
2097             delete d.path;
2098         if (local_fn)
2099             g_free(local_fn);
2100         if (unicode_fn)
2101             g_free(unicode_fn);
2102         if (hemf)
2103             DeleteEnhMetaFile(hemf);
2104         if (hmf)
2105             DeleteMetaFile(hmf);
2106         return NULL;
2107     }
2109     if (hemf) {
2110         EnumEnhMetaFile(NULL, hemf, myEnhMetaFileProc, (LPVOID) &d, NULL);
2111         DeleteEnhMetaFile(hemf);
2112     }
2113     else {
2114         EnumMetaFile(NULL, hmf, myMetaFileProc, (LPARAM) &d);
2115         DeleteMetaFile(hmf);
2116     }
2118 //    std::cout << "SVG Output: " << std::endl << *(d.outsvg) << std::endl;
2120     SPDocument *doc = sp_document_new_from_mem(d.outsvg->c_str(), d.outsvg->length(), TRUE);
2122     delete d.outsvg;
2123     delete d.path;
2125     if (d.emf_obj) {
2126         int i;
2127         for (i=0; i<d.n_obj; i++)
2128             delete_object(&d, i);
2129         delete[] d.emf_obj;
2130     }
2131     
2132     if (d.style.stroke_dash.dash)
2133         delete[] d.style.stroke_dash.dash;
2135     if  (local_fn)
2136         g_free(local_fn);
2137     if  (unicode_fn)
2138         g_free(unicode_fn);
2140     return doc;
2144 void
2145 EmfWin32::init (void)
2147     Inkscape::Extension::Extension * ext;
2149     /* EMF in */
2150     ext = Inkscape::Extension::build_from_mem(
2151         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2152             "<name>" N_("EMF Input") "</name>\n"
2153             "<id>org.inkscape.input.emf.win32</id>\n"
2154             "<input>\n"
2155                 "<extension>.emf</extension>\n"
2156                 "<mimetype>image/x-emf</mimetype>\n"
2157                 "<filetypename>" N_("Enhanced Metafiles (*.emf)") "</filetypename>\n"
2158                 "<filetypetooltip>" N_("Enhanced Metafiles") "</filetypetooltip>\n"
2159                 "<output_extension>org.inkscape.output.emf.win32</output_extension>\n"
2160             "</input>\n"
2161         "</inkscape-extension>", new EmfWin32());
2163     /* WMF in */
2164     ext = Inkscape::Extension::build_from_mem(
2165         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2166             "<name>" N_("WMF Input") "</name>\n"
2167             "<id>org.inkscape.input.wmf.win32</id>\n"
2168             "<input>\n"
2169                 "<extension>.wmf</extension>\n"
2170                 "<mimetype>image/x-wmf</mimetype>\n"
2171                 "<filetypename>" N_("Windows Metafiles (*.wmf)") "</filetypename>\n"
2172                 "<filetypetooltip>" N_("Windows Metafiles") "</filetypetooltip>\n"
2173                 "<output_extension>org.inkscape.output.emf.win32</output_extension>\n"
2174             "</input>\n"
2175         "</inkscape-extension>", new EmfWin32());
2177     /* EMF out */
2178     ext = Inkscape::Extension::build_from_mem(
2179         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2180             "<name>" N_("EMF Output") "</name>\n"
2181             "<id>org.inkscape.output.emf.win32</id>\n"
2182             "<param name=\"textToPath\" gui-text=\"" N_("Convert texts to paths") "\" type=\"boolean\">true</param>\n"
2183             "<output>\n"
2184                 "<extension>.emf</extension>\n"
2185                 "<mimetype>image/x-emf</mimetype>\n"
2186                 "<filetypename>" N_("Enhanced Metafile (*.emf)") "</filetypename>\n"
2187                 "<filetypetooltip>" N_("Enhanced Metafile") "</filetypetooltip>\n"
2188             "</output>\n"
2189         "</inkscape-extension>", new EmfWin32());
2191     return;
2195 } } }  /* namespace Inkscape, Extension, Implementation */
2198 #endif /* WIN32 */
2201 /*
2202   Local Variables:
2203   mode:cpp
2204   c-file-style:"stroustrup"
2205   c-file-offsets:((innamespace . 0)(inline-open . 0))
2206   indent-tabs-mode:nil
2207   fill-column:99
2208   End:
2209 */
2210 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :