Code

Pot and Dutch translation update
[inkscape.git] / src / extension / internal / emf-win32-print.cpp
1 /** @file
2  * @brief Enhanced Metafile printing
3  */
4 /* Authors:
5  *   Ulf Erikson <ulferikson@users.sf.net>
6  *
7  * Copyright (C) 2006-2009 Authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
11 /*
12  * References:
13  *  - How to Create & Play Enhanced Metafiles in Win32
14  *      http://support.microsoft.com/kb/q145999/
15  *  - INFO: Windows Metafile Functions & Aldus Placeable Metafiles
16  *      http://support.microsoft.com/kb/q66949/
17  *  - Metafile Functions
18  *      http://msdn.microsoft.com/library/en-us/gdi/metafile_0whf.asp
19  *  - Metafile Structures
20  *      http://msdn.microsoft.com/library/en-us/gdi/metafile_5hkj.asp
21  */
23 #ifdef WIN32
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 //#include <string.h>
30 //#include <signal.h>
31 //#include <errno.h>
33 #include <2geom/pathvector.h>
34 #include <2geom/rect.h>
35 #include <2geom/bezier-curve.h>
36 #include <2geom/hvlinesegment.h>
37 #include "helper/geom.h"
38 #include "helper/geom-curves.h"
39 //#include "display/canvas-bpath.h"
40 #include "sp-item.h"
42 //#include "glib.h"
43 //#include "gtk/gtkdialog.h"
44 //#include "gtk/gtkbox.h"
45 //#include "gtk/gtkstock.h"
47 //#include "glibmm/i18n.h"
48 //#include "enums.h"
49 //#include "document.h"
50 #include "style.h"
51 //#include "sp-paint-server.h"
52 #include "inkscape-version.h"
54 //#include "libnrtype/FontFactory.h"
55 //#include "libnrtype/font-instance.h"
56 //#include "libnrtype/font-style-to-pos.h"
58 #define WIN32_LEAN_AND_MEAN
59 #include <windows.h>
61 #include "win32.h"
62 #include "emf-win32-print.h"
64 #include "unit-constants.h"
66 //#include "extension/extension.h"
67 #include "extension/system.h"
68 #include "extension/print.h"
70 //#include "io/sys.h"
72 //#include "macros.h"
74 namespace Inkscape {
75 namespace Extension {
76 namespace Internal {
78 static float dwDPI = 2540;
81 PrintEmfWin32::PrintEmfWin32 (void):
82     hdc(NULL),
83     hbrush(NULL),
84     hbrushOld(NULL),
85     hpen(NULL),
86     stroke_and_fill(false),
87     fill_only(false),
88     simple_shape(false)
89 {
90 }
93 PrintEmfWin32::~PrintEmfWin32 (void)
94 {
95     if (hdc) {
96         HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
97         if ( metafile ) {
98             DeleteEnhMetaFile( metafile );
99         }
100         DeleteDC( hdc );
101     }
103     /* restore default signal handling for SIGPIPE */
104 #if !defined(_WIN32) && !defined(__WIN32__)
105     (void) signal(SIGPIPE, SIG_DFL);
106 #endif
107     return;
111 unsigned int
112 PrintEmfWin32::setup (Inkscape::Extension::Print * /*mod*/)
114     return TRUE;
118 unsigned int
119 PrintEmfWin32::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
121     gchar const *utf8_fn = mod->get_param_string("destination");
123     gsize bytesRead = 0;
124     gsize bytesWritten = 0;
125     GError* error = NULL;
126     gchar *local_fn =
127         g_filename_from_utf8( utf8_fn, -1,  &bytesRead,  &bytesWritten, &error );
129     if (local_fn == NULL) {
130         return 1;
131     }
133     CHAR *ansi_uri = (CHAR *) local_fn;
134     gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
135     WCHAR *unicode_uri = (WCHAR *) unicode_fn;
137     // width and height in px
138     _width = sp_document_width(doc);
139     _height = sp_document_height(doc);
141     NRRect d;
142     bool pageBoundingBox;
143     pageBoundingBox = mod->get_param_bool("pageBoundingBox");
144     if (pageBoundingBox) {
145         d.x0 = d.y0 = 0;
146         d.x1 = _width;
147         d.y1 = _height;
148     } else {
149         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
150         sp_item_invoke_bbox(doc_item, &d, sp_item_i2d_affine(doc_item), TRUE);
151     }
153     d.x0 *= IN_PER_PX;
154     d.y0 *= IN_PER_PX;
155     d.x1 *= IN_PER_PX;
156     d.y1 *= IN_PER_PX;
158     float dwInchesX = (d.x1 - d.x0);
159     float dwInchesY = (d.y1 - d.y0);
161     // dwInchesX x dwInchesY in .01mm units
162     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*2540), (int) ceil(dwInchesY*2540) );
164     // Get a Reference DC
165     HDC hScreenDC = GetDC( NULL );
167     // Get the physical characteristics of the reference DC
168     int PixelsX = GetDeviceCaps( hScreenDC, HORZRES );
169     int PixelsY = GetDeviceCaps( hScreenDC, VERTRES );
170     int MMX = GetDeviceCaps( hScreenDC, HORZSIZE );
171     int MMY = GetDeviceCaps( hScreenDC, VERTSIZE );
173     CHAR buff[1024];
174     ZeroMemory(buff, sizeof(buff));
175     snprintf(buff, sizeof(buff)-1, "Inkscape %s (%s)", Inkscape::version_string, __DATE__);
176     INT len = strlen(buff);
177     CHAR *p1 = strrchr(ansi_uri, '\\');
178     CHAR *p2 = strrchr(ansi_uri, '/');
179     CHAR *p = MAX(p1, p2);
180     if (p)
181         p++;
182     else
183         p = ansi_uri;
184     snprintf(buff+len+1, sizeof(buff)-len-2, "%s", p);
185     
186     // Create the Metafile
187     if (PrintWin32::is_os_wide()) {
188         WCHAR wbuff[1024];
189         ZeroMemory(wbuff, sizeof(wbuff));
190         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buff, sizeof(buff)/sizeof(buff[0]), wbuff, sizeof(wbuff)/sizeof(wbuff[0]));
191         hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, wbuff );
192     }
193     else {
194         hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, buff );
195     }
197     // Release the reference DC
198     ReleaseDC( NULL, hScreenDC );
200     // Did we get a good metafile?
201     if (hdc == NULL)
202     {
203         g_free(local_fn);
204         g_free(unicode_fn);
205         return 1;
206     }
208     // Anisotropic mapping mode
209     SetMapMode( hdc, MM_ANISOTROPIC );
211     // Set the Windows extent
212     int windowextX = (int) ceil(dwInchesX*dwDPI);
213     int windowextY = (int) ceil(dwInchesY*dwDPI);
214     SetWindowExtEx( hdc, windowextX, windowextY, NULL );
216     // Set the viewport extent to reflect
217     // dwInchesX" x dwInchesY" in device units
218     int viewportextX = (int)((float)dwInchesX*25.4f*(float)PixelsX/(float)MMX);
219     int viewportextY = (int)((float)dwInchesY*25.4f*(float)PixelsY/(float)MMY);
220     SetViewportExtEx( hdc, viewportextX, viewportextY, NULL );
222     if (1) {
223         snprintf(buff, sizeof(buff)-1, "Screen=%dx%dpx, %dx%dmm", PixelsX, PixelsY, MMX, MMY);
224         GdiComment(hdc, strlen(buff), (BYTE*) buff);
226         snprintf(buff, sizeof(buff)-1, "Drawing=%.1lfx%.1lfpx, %.1lfx%.1lfmm", _width, _height, dwInchesX * MM_PER_IN, dwInchesY * MM_PER_IN);
227         GdiComment(hdc, strlen(buff), (BYTE*) buff);
228     }
230     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*dwDPI), (int) ceil(dwInchesY*dwDPI) );
232     g_free(local_fn);
233     g_free(unicode_fn);
235     m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, sp_document_height(doc)));
237     return 0;
241 unsigned int
242 PrintEmfWin32::finish (Inkscape::Extension::Print * /*mod*/)
244     if (!hdc) return 0;
246     flush_fill(); // flush any pending fills
248     HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
249     if ( metafile ) {
250         DeleteEnhMetaFile( metafile );
251     }
252     DeleteDC( hdc );
254     hdc = NULL;
256     return 0;
260 unsigned int
261 PrintEmfWin32::comment (Inkscape::Extension::Print * /*module*/,
262                         const char * /*comment*/)
264     if (!hdc) return 0;
266     flush_fill(); // flush any pending fills
268     return 0;
272 int
273 PrintEmfWin32::create_brush(SPStyle const *style)
275     float rgb[3];
277     if (style) {
278         float opacity = SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
279         if (opacity <= 0.0)
280             return 1;
282         sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
283         hbrush = CreateSolidBrush( RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]) );
284         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
286         SetPolyFillMode( hdc,
287                          style->fill_rule.computed == 0 ? WINDING :
288                          style->fill_rule.computed == 2 ? ALTERNATE : ALTERNATE );
289     } else { // if (!style)
290         hbrush = CreateSolidBrush( RGB(255, 255, 255) );
291         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
292         SetPolyFillMode( hdc, ALTERNATE );
293     }
295     return 0;
299 void
300 PrintEmfWin32::destroy_brush()
302     SelectObject( hdc, hbrushOld );
303     if (hbrush)
304         DeleteObject( hbrush );
305     hbrush = NULL;
306     hbrushOld = NULL;
310 void
311 PrintEmfWin32::create_pen(SPStyle const *style, const Geom::Matrix &transform)
313     if (style) {
314         float rgb[3];
316         sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
318         LOGBRUSH lb;
319         ZeroMemory(&lb, sizeof(lb));
320         lb.lbStyle = BS_SOLID;
321         lb.lbColor = RGB( 255*rgb[0], 255*rgb[1], 255*rgb[2] );
323         int linestyle = PS_SOLID;
324         int linecap = 0;
325         int linejoin = 0;
326         DWORD n_dash = 0;
327         DWORD *dash = NULL;
329         using Geom::X;
330         using Geom::Y;
332         Geom::Point zero(0, 0);
333         Geom::Point one(1, 1);
334         Geom::Point p0(zero * transform);
335         Geom::Point p1(one * transform);
336         Geom::Point p(p1 - p0);
338         double scale = sqrt( (p[X]*p[X]) + (p[Y]*p[Y]) ) / sqrt(2);
340         DWORD linewidth = MAX( 1, (DWORD) (scale * style->stroke_width.computed * IN_PER_PX * dwDPI) );
342         if (style->stroke_linecap.computed == 0) {
343             linecap = PS_ENDCAP_FLAT;
344         }
345         else if (style->stroke_linecap.computed == 1) {
346             linecap = PS_ENDCAP_ROUND;
347         }
348         else if (style->stroke_linecap.computed == 2) {
349             linecap = PS_ENDCAP_SQUARE;
350         }
352         if (style->stroke_linejoin.computed == 0) {
353             linejoin = PS_JOIN_MITER;
354         }
355         else if (style->stroke_linejoin.computed == 1) {
356             linejoin = PS_JOIN_ROUND;
357         }
358         else if (style->stroke_linejoin.computed == 2) {
359             linejoin = PS_JOIN_BEVEL;
360         }
362         if (style->stroke_dash.n_dash   &&
363             style->stroke_dash.dash       )
364         {
365             int i = 0;
366             while (linestyle != PS_USERSTYLE &&
367                    (i < style->stroke_dash.n_dash)) {
368                 if (style->stroke_dash.dash[i] > 0.00000001)
369                     linestyle = PS_USERSTYLE;
370                 i++;
371             }
373             if (linestyle == PS_USERSTYLE) {
374                 n_dash = style->stroke_dash.n_dash;
375                 dash = new DWORD[n_dash];
376                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
377                     dash[i] = (DWORD) (style->stroke_dash.dash[i] * IN_PER_PX * dwDPI);
378                 }
379             }
380         }
382         hpen = ExtCreatePen(
383             PS_GEOMETRIC | linestyle | linecap | linejoin,
384             linewidth,
385             &lb,
386             n_dash,
387             dash );
389         if ( !hpen && linestyle == PS_USERSTYLE ) {
390             hpen = ExtCreatePen(
391                 PS_GEOMETRIC | PS_SOLID | linecap | linejoin,
392                 linewidth,
393                 &lb,
394                 0,
395                 NULL );
396         }
398         if ( !hpen ) {
399             hpen = CreatePen(
400                 PS_SOLID,
401                 linewidth,
402                 lb.lbColor );
403         }
405         hpenOld = (HPEN) SelectObject( hdc, hpen );
407         if (linejoin == PS_JOIN_MITER) {
408             float oldmiterlimit;
409             float miterlimit = style->stroke_miterlimit.value;
411             miterlimit = miterlimit * 10.0 / 4.0;
412             if (miterlimit < 1)
413                 miterlimit = 10.0;
415             miterlimit = miterlimit * IN_PER_PX * dwDPI;
417             SetMiterLimit(
418                 hdc,
419                 miterlimit,
420                 &oldmiterlimit );
421         }
423         if (n_dash) {
424             delete[] dash;
425         }
426     }
427     else { // if (!style)
428         hpen = CreatePen( PS_SOLID, 1, RGB(0, 0, 0) );
429         hpenOld = (HPEN) SelectObject( hdc, hpen );
430     }
434 void
435 PrintEmfWin32::destroy_pen()
437     SelectObject( hdc, hpenOld );
438     if (hpen)
439         DeleteObject( hpen );
440     hpen = NULL;
444 void
445 PrintEmfWin32::flush_fill()
447     if (!fill_pathv.empty()) {
448         stroke_and_fill = false;
449         fill_only = true;
450         print_pathv(fill_pathv, fill_transform);
451         fill_only = false;
452         if (!simple_shape)
453             FillPath( hdc );
454         destroy_brush();
455         fill_pathv.clear();
456     }
459 unsigned int
460 PrintEmfWin32::bind(Inkscape::Extension::Print * /*mod*/, Geom::Matrix const *transform, float /*opacity*/)
462     Geom::Matrix tr = *transform;
463     
464     if (m_tr_stack.size()) {
465         Geom::Matrix tr_top = m_tr_stack.top();
466         m_tr_stack.push(tr * tr_top);
467     } else {
468         m_tr_stack.push(tr);
469     }
471     return 1;
474 unsigned int
475 PrintEmfWin32::release(Inkscape::Extension::Print * /*mod*/)
477     m_tr_stack.pop();
478     return 1;
481 unsigned int
482 PrintEmfWin32::fill(Inkscape::Extension::Print * /*mod*/,
483                     Geom::PathVector const &pathv, Geom::Matrix const * /*transform*/, SPStyle const *style,
484                     NRRect const * /*pbox*/, NRRect const * /*dbox*/, NRRect const * /*bbox*/)
486     if (!hdc) return 0;
488     Geom::Matrix tf = m_tr_stack.top();
490     flush_fill(); // flush any pending fills
492     if (style->fill.isColor()) {
493         if (create_brush(style))
494             return 0;
495     } else {
496         // create_brush(NULL);
497         return 0;
498     }
500     fill_pathv.clear();
501     std::copy(pathv.begin(), pathv.end(), std::back_inserter(fill_pathv));
502     fill_transform = tf;
504     // postpone fill in case of stroke-and-fill
506     return 0;
510 unsigned int
511 PrintEmfWin32::stroke (Inkscape::Extension::Print * /*mod*/,
512                        Geom::PathVector const &pathv, const Geom::Matrix * /*transform*/, const SPStyle *style,
513                        const NRRect * /*pbox*/, const NRRect * /*dbox*/, const NRRect * /*bbox*/)
515     if (!hdc) return 0;
517     Geom::Matrix tf = m_tr_stack.top();
519     stroke_and_fill = ( pathv == fill_pathv );
521     if (!stroke_and_fill) {
522         flush_fill(); // flush any pending fills
523     }
525     if (style->stroke.isColor()) {
526         create_pen(style, tf);
527     } else {
528         // create_pen(NULL, tf);
529         return 0;
530     }
532     print_pathv(pathv, tf);
534     if (stroke_and_fill) {
535         if (!simple_shape)
536             StrokeAndFillPath( hdc );
537         destroy_brush();
538         fill_pathv.clear();
539     } else {
540         if (!simple_shape)
541             StrokePath( hdc );
542     }
544     destroy_pen();
546     return 0;
550 bool
551 PrintEmfWin32::print_simple_shape(Geom::PathVector const &pathv, const Geom::Matrix &transform)
553     Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
554     
555     int nodes = 0;
556     int moves = 0;
557     int lines = 0;
558     int curves = 0;
560     for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
561     {
562         moves++;
563         nodes++;
564         
565         for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
566         {
567             nodes++;
568             
569             if ( is_straight_curve(*cit) ) {
570                 lines++;
571             }
572             else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit)) {
573                 cubic = cubic;
574                 curves++;
575             }
576         }
577     }
579     if (!nodes)
580         return false;
581     
582     POINT *lpPoints = new POINT[moves + lines + curves*3];
583     int i = 0;
585     /**
586      * For all Subpaths in the <path>
587      */      
588     for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
589     {
590         using Geom::X;
591         using Geom::Y;
593         Geom::Point p0 = pit->initialPoint();
595         p0[X] = (p0[X] * IN_PER_PX * dwDPI);
596         p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
597                 
598         LONG const x0 = (LONG) round(p0[X]);
599         LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
601         lpPoints[i].x = x0;
602         lpPoints[i].y = y0;
603         i = i + 1;
605         /**
606          * For all segments in the subpath
607          */
608         for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
609         {
610             if ( is_straight_curve(*cit) )
611             {
612                 //Geom::Point p0 = cit->initialPoint();
613                 Geom::Point p1 = cit->finalPoint();
615                 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
616                 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
617                 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
618                 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
619                 
620                 //LONG const x0 = (LONG) round(p0[X]);
621                 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
622                 LONG const x1 = (LONG) round(p1[X]);
623                 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
625                 lpPoints[i].x = x1;
626                 lpPoints[i].y = y1;
627                 i = i + 1;
628             }
629             else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
630             {
631                 std::vector<Geom::Point> points = cubic->points();
632                 //Geom::Point p0 = points[0];
633                 Geom::Point p1 = points[1];
634                 Geom::Point p2 = points[2];
635                 Geom::Point p3 = points[3];
637                 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
638                 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
639                 p2[X] = (p2[X] * IN_PER_PX * dwDPI);
640                 p3[X] = (p3[X] * IN_PER_PX * dwDPI);
641                 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
642                 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
643                 p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
644                 p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
645                 
646                 //LONG const x0 = (LONG) round(p0[X]);
647                 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
648                 LONG const x1 = (LONG) round(p1[X]);
649                 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
650                 LONG const x2 = (LONG) round(p2[X]);
651                 LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
652                 LONG const x3 = (LONG) round(p3[X]);
653                 LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
655                 POINT pt[3];
656                 pt[0].x = x1;
657                 pt[0].y = y1;
658                 pt[1].x = x2;
659                 pt[1].y = y2;
660                 pt[2].x = x3;
661                 pt[2].y = y3;
663                 lpPoints[i].x = x1;
664                 lpPoints[i].y = y1;
665                 lpPoints[i+1].x = x2;
666                 lpPoints[i+1].y = y2;
667                 lpPoints[i+2].x = x3;
668                 lpPoints[i+2].y = y3;
669                 i = i + 3;
670             }
671         }
672     }
674     bool done = false;
675     bool closed = (lpPoints[0].x == lpPoints[i-1].x) && (lpPoints[0].y == lpPoints[i-1].y);
676     bool polygon = false;
677     bool rectangle = false;
678     bool ellipse = false;
679     
680     if (moves == 1 && moves+lines == nodes && closed) {
681         polygon = true;
682         if (nodes==5) {
683             if (lpPoints[0].x == lpPoints[3].x && lpPoints[1].x == lpPoints[2].x &&
684                 lpPoints[0].y == lpPoints[1].y && lpPoints[2].y == lpPoints[3].y)
685             {
686                 rectangle = true;
687             }
688         }
689     }
690     else if (moves == 1 && nodes == 5 && moves+curves == nodes && closed) {
691         if (lpPoints[0].x == lpPoints[1].x && lpPoints[1].x == lpPoints[11].x &&
692             lpPoints[5].x == lpPoints[6].x && lpPoints[6].x == lpPoints[7].x &&
693             lpPoints[2].x == lpPoints[10].x && lpPoints[3].x == lpPoints[9].x && lpPoints[4].x == lpPoints[8].x &&
694             lpPoints[2].y == lpPoints[3].y && lpPoints[3].y == lpPoints[4].y &&
695             lpPoints[8].y == lpPoints[9].y && lpPoints[9].y == lpPoints[10].y &&
696             lpPoints[5].y == lpPoints[1].y && lpPoints[6].y == lpPoints[0].y && lpPoints[7].y == lpPoints[11].y)
697         {
698             ellipse = true;
699         }
700     }
702     if (polygon || ellipse) {
703         HPEN hpenTmp = NULL;
704         HPEN hpenOld = NULL;
705         HBRUSH hbrushTmp = NULL;
706         HBRUSH hbrushOld = NULL;
708         if (!stroke_and_fill) {
709             if (fill_only) {
710                 hpenTmp = (HPEN) GetStockObject(NULL_PEN);
711                 hpenOld = (HPEN) SelectObject( hdc, hpenTmp );
712             }
713             else { // if (stroke_only)
714                 hbrushTmp = (HBRUSH) GetStockObject(NULL_BRUSH);
715                 hbrushOld = (HBRUSH) SelectObject( hdc, hbrushTmp );
716             }
717         }
719         if (polygon) {
720             if (rectangle)
721                 Rectangle( hdc, lpPoints[0].x, lpPoints[0].y, lpPoints[2].x, lpPoints[2].y );
722             else
723                 Polygon( hdc, lpPoints, nodes );
724         }
725         else if (ellipse) {
726             Ellipse( hdc, lpPoints[6].x, lpPoints[3].y, lpPoints[0].x, lpPoints[9].y);
727         }
728         
729         done = true;
731         if (hpenOld)
732             SelectObject( hdc, hpenOld );
733         if (hpenTmp)
734             DeleteObject( hpenTmp );
735         if (hbrushOld)
736             SelectObject( hdc, hbrushOld );
737         if (hbrushTmp)
738             DeleteObject( hbrushTmp );
739     }
741     delete[] lpPoints;
742     
743     return done;
746 unsigned int
747 PrintEmfWin32::print_pathv(Geom::PathVector const &pathv, const Geom::Matrix &transform)
749     simple_shape = print_simple_shape(pathv, transform);
751     if (simple_shape)
752         return TRUE;
754     Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
755     
756     BeginPath( hdc );
758     /**
759      * For all Subpaths in the <path>
760      */      
761     for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
762     {
763         using Geom::X;
764         using Geom::Y;
766         Geom::Point p0 = pit->initialPoint();
768         p0[X] = (p0[X] * IN_PER_PX * dwDPI);
769         p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
770                 
771         LONG const x0 = (LONG) round(p0[X]);
772         LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
774         MoveToEx( hdc, x0, y0, NULL );
776         /**
777          * For all segments in the subpath
778          */
779         for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
780         {
781             if ( is_straight_curve(*cit) )
782             {
783                 //Geom::Point p0 = cit->initialPoint();
784                 Geom::Point p1 = cit->finalPoint();
786                 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
787                 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
788                 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
789                 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
790                 
791                 //LONG const x0 = (LONG) round(p0[X]);
792                 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
793                 LONG const x1 = (LONG) round(p1[X]);
794                 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
796                 LineTo( hdc, x1, y1 );
797             }
798             else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
799             {
800                 std::vector<Geom::Point> points = cubic->points();
801                 //Geom::Point p0 = points[0];
802                 Geom::Point p1 = points[1];
803                 Geom::Point p2 = points[2];
804                 Geom::Point p3 = points[3];
806                 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
807                 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
808                 p2[X] = (p2[X] * IN_PER_PX * dwDPI);
809                 p3[X] = (p3[X] * IN_PER_PX * dwDPI);
810                 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
811                 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
812                 p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
813                 p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
814                 
815                 //LONG const x0 = (LONG) round(p0[X]);
816                 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
817                 LONG const x1 = (LONG) round(p1[X]);
818                 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
819                 LONG const x2 = (LONG) round(p2[X]);
820                 LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
821                 LONG const x3 = (LONG) round(p3[X]);
822                 LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
824                 POINT pt[3];
825                 pt[0].x = x1;
826                 pt[0].y = y1;
827                 pt[1].x = x2;
828                 pt[1].y = y2;
829                 pt[2].x = x3;
830                 pt[2].y = y3;
832                 PolyBezierTo( hdc, pt, 3 );
833             }
834             else
835             {
836                 g_warning("logical error, because pathv_to_linear_and_cubic_beziers was used");
837             }
838         }
840         if (pit->end_default() == pit->end_closed()) {
841             CloseFigure( hdc );
842         }
843     }
845     EndPath( hdc );
847     return TRUE;
851 bool
852 PrintEmfWin32::textToPath(Inkscape::Extension::Print * ext)
854     return ext->get_param_bool("textToPath");
857 unsigned int
858 PrintEmfWin32::text(Inkscape::Extension::Print * /*mod*/, char const *text, Geom::Point p,
859                     SPStyle const *const style)
861     if (!hdc) return 0;
863     HFONT hfont = NULL;
864     
865 #ifdef USE_PANGO_WIN32
866 /*
867     font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
868     if (tf) {
869         LOGFONT *lf = pango_win32_font_logfont(tf->pFont);
870         tf->Unref();
871         hfont = CreateFontIndirect(lf);
872         g_free(lf);
873     }
874 */
875 #endif
877     if (!hfont) {
878         if (PrintWin32::is_os_wide()) {
879             LOGFONTW *lf = (LOGFONTW*)g_malloc(sizeof(LOGFONTW));
880             g_assert(lf != NULL);
881             
882             lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
883             lf->lfWidth = 0;
884             lf->lfEscapement = 0;
885             lf->lfOrientation = 0;
886             lf->lfWeight =
887                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
888                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
889                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
890                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
891                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
892                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
893                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
894                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
895                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
896                 FW_NORMAL;
897             lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
898             lf->lfUnderline = style->text_decoration.underline;
899             lf->lfStrikeOut = style->text_decoration.line_through;
900             lf->lfCharSet = DEFAULT_CHARSET;
901             lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
902             lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
903             lf->lfQuality = DEFAULT_QUALITY;
904             lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
905             
906             gunichar2 *unicode_name = g_utf8_to_utf16( style->text->font_family.value, -1, NULL, NULL, NULL );
907             wcsncpy(lf->lfFaceName, (wchar_t*) unicode_name, LF_FACESIZE-1);
908             g_free(unicode_name);
909             
910             hfont = CreateFontIndirectW(lf);
911             
912             g_free(lf);
913         }
914         else {
915             LOGFONTA *lf = (LOGFONTA*)g_malloc(sizeof(LOGFONTA));
916             g_assert(lf != NULL);
917             
918             lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
919             lf->lfWidth = 0;
920             lf->lfEscapement = 0;
921             lf->lfOrientation = 0;
922             lf->lfWeight =
923                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
924                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
925                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
926                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
927                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
928                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
929                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
930                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
931                 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
932                 FW_NORMAL;
933             lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
934             lf->lfUnderline = style->text_decoration.underline;
935             lf->lfStrikeOut = style->text_decoration.line_through;
936             lf->lfCharSet = DEFAULT_CHARSET;
937             lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
938             lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
939             lf->lfQuality = DEFAULT_QUALITY;
940             lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
941             
942             strncpy(lf->lfFaceName, (char*) style->text->font_family.value, LF_FACESIZE-1);
944             hfont = CreateFontIndirectA(lf);
945             
946             g_free(lf);
947         }
948     }
949     
950     HFONT hfontOld = (HFONT) SelectObject(hdc, hfont);
952     float rgb[3];
953     sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
954     SetTextColor(hdc, RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]));
956     // Text alignment:
957     //   - (x,y) coordinates received by this filter are those of the point where the text
958     //     actually starts, and already takes into account the text object's alignment;
959     //   - for this reason, the EMF text alignment must always be TA_BASELINE|TA_LEFT.
960     SetTextAlign(hdc, TA_BASELINE | TA_LEFT);
962     // Transparent text background
963     SetBkMode(hdc, TRANSPARENT);
965     Geom::Matrix tf = m_tr_stack.top();
967     p = p * tf;
968     p[Geom::X] = (p[Geom::X] * IN_PER_PX * dwDPI);
969     p[Geom::Y] = (p[Geom::Y] * IN_PER_PX * dwDPI);
971     LONG const xpos = (LONG) round(p[Geom::X]);
972     LONG const ypos = (LONG) round(rc.bottom-p[Geom::Y]);
974     if (PrintWin32::is_os_wide()) {
975         gunichar2 *unicode_text = g_utf8_to_utf16( text, -1, NULL, NULL, NULL );
976         TextOutW(hdc, xpos, ypos, (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
977     }
978     else {
979         TextOutA(hdc, xpos, ypos, (CHAR*)text, strlen((char*)text));
980     }
982     SelectObject(hdc, hfontOld);
983     DeleteObject(hfont);
984     
985     return 0;
988 void
989 PrintEmfWin32::init (void)
991     Inkscape::Extension::Extension * ext;
993     /* EMF print */
994     ext = Inkscape::Extension::build_from_mem(
995         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
996         "<name>Enhanced Metafile Print</name>\n"
997         "<id>org.inkscape.print.emf.win32</id>\n"
998         "<param name=\"destination\" type=\"string\"></param>\n"
999         "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
1000         "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
1001         "<print/>\n"
1002         "</inkscape-extension>", new PrintEmfWin32());
1004     return;
1008 }  /* namespace Internal */
1009 }  /* namespace Extension */
1010 }  /* namespace Inkscape */
1012 #endif /* WIN32 */
1014 /*
1015   Local Variables:
1016   mode:c++
1017   c-file-style:"stroustrup"
1018   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1019   indent-tabs-mode:nil
1020   fill-column:99
1021   End:
1022 */
1023 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :