Code

change NR::Matrix to Geom:: for many sp_item_xxx_affine functions
[inkscape.git] / src / extension / internal / emf-win32-print.cpp
1 /** \file
2  * Enhanced Metafile Printing.
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 <string.h>
31 #include <signal.h>
32 #include <errno.h>
34 #include "libnr/n-art-bpath.h"
35 #include "libnr/nr-point-matrix-ops.h"
36 #include "libnr/nr-rect.h"
37 #include "libnr/nr-matrix.h"
38 #include "libnr/nr-matrix-ops.h" 
39 #include "libnr/nr-matrix-scale-ops.h"
40 #include "libnr/nr-matrix-translate-ops.h"
41 #include "libnr/nr-scale-translate-ops.h"
42 #include "libnr/nr-translate-scale-ops.h"
43 #include "libnr/nr-matrix-fns.h"
44 #include "libnr/nr-path.h"
45 #include "libnr/nr-pixblock.h"
46 #include "display/canvas-bpath.h"
47 #include "sp-item.h"
49 #include "glib.h"
50 #include "gtk/gtkdialog.h"
51 #include "gtk/gtkbox.h"
52 #include "gtk/gtkstock.h"
54 #include "glibmm/i18n.h"
55 #include "enums.h"
56 #include "document.h"
57 #include "style.h"
58 #include "sp-paint-server.h"
59 #include "inkscape_version.h"
61 #include "libnrtype/FontFactory.h"
62 #include "libnrtype/font-instance.h"
63 #include "libnrtype/font-style-to-pos.h"
65 #include "win32.h"
66 #include "emf-win32-print.h"
68 #include "unit-constants.h"
70 #include "extension/extension.h"
71 #include "extension/system.h"
72 #include "extension/print.h"
74 #include "io/sys.h"
76 #include "macros.h"
78 #define WIN32_LEAN_AND_MEAN
79 #include <windows.h>
81 namespace Inkscape {
82 namespace Extension {
83 namespace Internal {
85 static float dwDPI = 2540;
88 PrintEmfWin32::PrintEmfWin32 (void):
89     hdc(NULL),
90     hbrush(NULL),
91     hbrushOld(NULL),
92     hpen(NULL),
93     fill_path(NULL),
94     stroke_and_fill(false),
95     fill_only(false),
96     simple_shape(false)
97 {
98 }
101 PrintEmfWin32::~PrintEmfWin32 (void)
103     if (hdc) {
104         HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
105         if ( metafile ) {
106             DeleteEnhMetaFile( metafile );
107         }
108         DeleteDC( hdc );
109     }
111     /* restore default signal handling for SIGPIPE */
112 #if !defined(_WIN32) && !defined(__WIN32__)
113     (void) signal(SIGPIPE, SIG_DFL);
114 #endif
115         return;
119 unsigned int
120 PrintEmfWin32::setup (Inkscape::Extension::Print *mod)
122     return TRUE;
126 unsigned int
127 PrintEmfWin32::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
129     gchar const *utf8_fn = mod->get_param_string("destination");
131     gsize bytesRead = 0;
132     gsize bytesWritten = 0;
133     GError* error = NULL;
134     gchar *local_fn =
135         g_filename_from_utf8( utf8_fn, -1,  &bytesRead,  &bytesWritten, &error );
137     if (local_fn == NULL) {
138         return 1;
139     }
141     CHAR *ansi_uri = (CHAR *) local_fn;
142     gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
143     WCHAR *unicode_uri = (WCHAR *) unicode_fn;
145     // width and height in px
146     _width = sp_document_width(doc);
147     _height = sp_document_height(doc);
149     NRRect d;
150     bool pageBoundingBox;
151     pageBoundingBox = mod->get_param_bool("pageBoundingBox");
152     if (pageBoundingBox) {
153         d.x0 = d.y0 = 0;
154         d.x1 = _width;
155         d.y1 = _height;
156     } else {
157         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
158         sp_item_invoke_bbox(doc_item, &d, from_2geom(sp_item_i2r_affine(doc_item)), TRUE);
159     }
161     d.x0 *= IN_PER_PX;
162     d.y0 *= IN_PER_PX;
163     d.x1 *= IN_PER_PX;
164     d.y1 *= IN_PER_PX;
166     float dwInchesX = (d.x1 - d.x0);
167     float dwInchesY = (d.y1 - d.y0);
169     // dwInchesX x dwInchesY in .01mm units
170     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*2540), (int) ceil(dwInchesY*2540) );
172     // Get a Reference DC
173     HDC hScreenDC = GetDC( NULL );
175     // Create the Metafile
176     if (PrintWin32::is_os_wide())
177         hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, NULL );
178     else
179         hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, NULL );
181     // Release the reference DC
182     ReleaseDC( NULL, hScreenDC );
184     // Did we get a good metafile?
185     if (hdc == NULL)
186     {
187         g_free(local_fn);
188         g_free(unicode_fn);
189         return 1;
190     }
192     // Anisotropic mapping mode
193     SetMapMode( hdc, MM_ANISOTROPIC );
195     // Set the Windows extent
196     SetWindowExtEx( hdc, (int) (dwInchesX*dwDPI), (int) (dwInchesY*dwDPI), NULL );
198     // Set the viewport extent to reflect
199     // dwInchesX" x dwInchesY" in device units
200     SetViewportExtEx( hdc,
201                       (int) ((float) dwInchesX*25.4f*PX_PER_MM),
202                       (int) ((float) dwInchesY*25.4f*PX_PER_MM),
203                       NULL );
205     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*dwDPI), (int) ceil(dwInchesY*dwDPI) );
207     g_free(local_fn);
208     g_free(unicode_fn);
210     m_tr_stack.push( NR::scale(1, -1) * NR::translate(0, sp_document_height(doc)));
211     return 0;
215 unsigned int
216 PrintEmfWin32::finish (Inkscape::Extension::Print *mod)
218     if (!hdc) return 0;
220     flush_fill(); // flush any pending fills
222     HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
223     if ( metafile ) {
224         DeleteEnhMetaFile( metafile );
225     }
226     DeleteDC( hdc );
228     hdc = NULL;
230     return 0;
234 unsigned int
235 PrintEmfWin32::comment (Inkscape::Extension::Print * module,
236                                 const char *comment)
238     if (!hdc) return 0;
240     flush_fill(); // flush any pending fills
242     return 0;
246 int
247 PrintEmfWin32::create_brush(SPStyle const *style)
249     float rgb[3];
251     if (style) {
252         float opacity = SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
253         if (opacity <= 0.0)
254             return 1;
256         sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
257         hbrush = CreateSolidBrush( RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]) );
258         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
260         SetPolyFillMode( hdc,
261                          style->fill_rule.computed == 0 ? WINDING :
262                          style->fill_rule.computed == 2 ? ALTERNATE : ALTERNATE );
263     } else { // if (!style)
264         hbrush = CreateSolidBrush( RGB(255, 255, 255) );
265         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
266         SetPolyFillMode( hdc, ALTERNATE );
267     }
269     return 0;
273 void
274 PrintEmfWin32::destroy_brush()
276     SelectObject( hdc, hbrushOld );
277     if (hbrush)
278         DeleteObject( hbrush );
279     hbrush = NULL;
280     hbrushOld = NULL;
284 void
285 PrintEmfWin32::create_pen(SPStyle const *style, const NR::Matrix *transform)
287     if (style) {
288         float rgb[3];
290         sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
292         LOGBRUSH lb = {0};
293         lb.lbStyle = BS_SOLID;
294         lb.lbColor = RGB( 255*rgb[0], 255*rgb[1], 255*rgb[2] );
296         int linestyle = PS_SOLID;
297         int linecap = 0;
298         int linejoin = 0;
299         DWORD n_dash = 0;
300         DWORD *dash = NULL;
301         float oldmiterlimit;
303         using NR::X;
304         using NR::Y;
306         NR::Matrix tf = *transform;
308         NR::Point zero(0, 0);
309         NR::Point one(1, 1);
310         NR::Point p0(zero * tf);
311         NR::Point p1(one * tf);
312         NR::Point p(p1 - p0);
314         double scale = sqrt( (p[X]*p[X]) + (p[Y]*p[Y]) ) / sqrt(2);
316         DWORD linewidth = MAX( 1, (DWORD) (scale * style->stroke_width.computed * IN_PER_PX * dwDPI) );
318         if (style->stroke_linecap.computed == 0) {
319             linecap = PS_ENDCAP_FLAT;
320         }
321         else if (style->stroke_linecap.computed == 1) {
322             linecap = PS_ENDCAP_ROUND;
323         }
324         else if (style->stroke_linecap.computed == 2) {
325             linecap = PS_ENDCAP_SQUARE;
326         }
328         if (style->stroke_linejoin.computed == 0) {
329             linejoin = PS_JOIN_MITER;
330         }
331         else if (style->stroke_linejoin.computed == 1) {
332             linejoin = PS_JOIN_ROUND;
333         }
334         else if (style->stroke_linejoin.computed == 2) {
335             linejoin = PS_JOIN_BEVEL;
336         }
338         if (style->stroke_dash.n_dash   &&
339             style->stroke_dash.dash       )
340         {
341             int i = 0;
342             while (linestyle != PS_USERSTYLE &&
343                    (i < style->stroke_dash.n_dash)) {
344                 if (style->stroke_dash.dash[i] > 0.00000001)
345                     linestyle = PS_USERSTYLE;
346                 i++;
347             }
349             if (linestyle == PS_USERSTYLE) {
350                 n_dash = style->stroke_dash.n_dash;
351                 dash = new DWORD[n_dash];
352                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
353                     dash[i] = (DWORD) (style->stroke_dash.dash[i] * IN_PER_PX * dwDPI);
354                 }
355             }
356         }
358         hpen = ExtCreatePen(
359             PS_GEOMETRIC | linestyle | linecap | linejoin,
360             linewidth,
361             &lb,
362             n_dash,
363             dash );
365         if ( !hpen && linestyle == PS_USERSTYLE ) {
366             hpen = ExtCreatePen(
367                 PS_GEOMETRIC | PS_SOLID | linecap | linejoin,
368                 linewidth,
369                 &lb,
370                 0,
371                 NULL );
372         }
374         if ( !hpen ) {
375             hpen = CreatePen(
376                 PS_SOLID,
377                 linewidth,
378                 lb.lbColor );
379         }
381         hpenOld = (HPEN) SelectObject( hdc, hpen );
383         if (linejoin == PS_JOIN_MITER) {
384             float miterlimit = style->stroke_miterlimit.value;
385             if (miterlimit < 1)
386                 miterlimit = 4.0;
387             SetMiterLimit(
388                 hdc,
389                 miterlimit * IN_PER_PX * dwDPI,
390                 &oldmiterlimit );
391         }
393         if (n_dash) {
394             delete[] dash;
395         }
396     }
397     else { // if (!style)
398         hpen = CreatePen( PS_SOLID, 1, RGB(0, 0, 0) );
399         hpenOld = (HPEN) SelectObject( hdc, hpen );
400     }
404 void
405 PrintEmfWin32::destroy_pen()
407     SelectObject( hdc, hpenOld );
408     if (hpen)
409         DeleteObject( hpen );
410     hpen = NULL;
414 void
415 PrintEmfWin32::flush_fill()
417     if (fill_path) {
418         stroke_and_fill = false;
419         fill_only = true;
420         print_bpath(fill_path, &fill_transform, &fill_pbox);
421         fill_only = false;
422         if (!simple_shape)
423             FillPath( hdc );
424         destroy_brush();
425         delete[] fill_path;
426         fill_path = NULL;
427     }
431 NArtBpath *
432 PrintEmfWin32::copy_bpath(const NArtBpath *bp)
434     NArtBpath *tmp = (NArtBpath *) bp;
435     int num = 1;
436     
437     while (tmp->code != NR_END) {
438         num++;
439         tmp += 1;
440     }
442     tmp = new NArtBpath[num];
443     while (num--) {
444         tmp[num] = bp[num];
445     }
447     return tmp;
451 int
452 PrintEmfWin32::cmp_bpath(const NArtBpath *bp1, const NArtBpath *bp2)
454     if (!bp1 || !bp2) {
455         return 1;
456     }
457     
458     while (bp1->code != NR_END && bp2->code != NR_END) {
459         if (bp1->code != bp2->code) {
460             return 1;
461         }
463         if ( fabs(bp1->x1 - bp2->x1) > 0.00000001 ||
464              fabs(bp1->y1 - bp2->y1) > 0.00000001 ||
465              fabs(bp1->x2 - bp2->x2) > 0.00000001 ||
466              fabs(bp1->y2 - bp2->y2) > 0.00000001 ||
467              fabs(bp1->x3 - bp2->x3) > 0.00000001 ||
468              fabs(bp1->y3 - bp2->y3) > 0.00000001 )
469         {
470             return 1;
471         }
472         
473         bp1 += 1;
474         bp2 += 1;
475     }
476     
477     return bp1->code != NR_END || bp2->code != NR_END;
480 unsigned int
481 PrintEmfWin32::bind(Inkscape::Extension::Print *mod, NR::Matrix const *transform, float opacity)
483     NR::Matrix tr = *transform;
484     
485     if (m_tr_stack.size()) {
486         NR::Matrix tr_top = m_tr_stack.top();
487         m_tr_stack.push(tr * tr_top);
488     } else {
489         m_tr_stack.push(tr);
490     }
492     return 1;
495 unsigned int
496 PrintEmfWin32::release(Inkscape::Extension::Print *mod)
498     m_tr_stack.pop();
499     return 1;
502 unsigned int
503 PrintEmfWin32::fill(Inkscape::Extension::Print *mod,
504                const_NRBPath const *bpath, NR::Matrix const *transform, SPStyle const *style,
505                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
507     if (!hdc) return 0;
509     NR::Matrix tf = m_tr_stack.top();
511     flush_fill(); // flush any pending fills
513     if (style->fill.isColor()) {
514         if (create_brush(style))
515             return 0;
516     } else {
517         // create_brush(NULL);
518         return 0;
519     }
521     fill_path = copy_bpath( bpath->path );
522     fill_transform = tf;
523     fill_pbox = *pbox;
525     // postpone fill in case of stroke-and-fill
527     return 0;
531 unsigned int
532 PrintEmfWin32::stroke (Inkscape::Extension::Print *mod,
533                   const const_NRBPath *bpath, const NR::Matrix *transform, const SPStyle *style,
534                   const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
536     if (!hdc) return 0;
538     NR::Matrix tf = m_tr_stack.top();
540     stroke_and_fill = ( cmp_bpath( bpath->path, fill_path ) == 0 );
542     if (!stroke_and_fill) {
543         flush_fill(); // flush any pending fills
544     }
546     if (style->stroke.isColor()) {
547         create_pen(style, &tf);
548     } else {
549         // create_pen(NULL, &tf);
550         return 0;
551     }
553     print_bpath(bpath->path, &tf, pbox);
555     if (stroke_and_fill) {
556         if (!simple_shape)
557             StrokeAndFillPath( hdc );
558         destroy_brush();
559         delete[] fill_path;
560         fill_path = NULL;
561     } else {
562         if (!simple_shape)
563             StrokePath( hdc );
564     }
566     destroy_pen();
568     return 0;
572 bool
573 PrintEmfWin32::print_simple_shape(const NArtBpath *bpath, const NR::Matrix *transform, NRRect const *pbox)
575     NR::Matrix tf = *transform;
576     const NArtBpath *bp = bpath;
577     
578     int nodes = 0;
579     int moves = 0;
580     int lines = 0;
581     int curves = 0;
583     while (bp->code != NR_END) {
584         nodes++;
585         switch (bp->code) {
586             case NR_MOVETO:
587             case NR_MOVETO_OPEN:
588                 moves++;
589                 break;
590             case NR_LINETO:
591                 lines++;
592                 break;
593             case NR_CURVETO:
594                 curves++;
595                 break;
596         }
597         bp += 1;
598     }
600     if (!nodes)
601         return false;
602     
603     POINT *lpPoints = new POINT[moves + lines + curves*3];
604     int i = 0;
605     bp = bpath;
606     while (bp->code != NR_END)
607     {
608         using NR::X;
609         using NR::Y;
611         NR::Point p1(bp->c(1) * tf);
612         NR::Point p2(bp->c(2) * tf);
613         NR::Point p3(bp->c(3) * tf);
615         p1[X] = (p1[X] * IN_PER_PX * dwDPI);
616         p2[X] = (p2[X] * IN_PER_PX * dwDPI);
617         p3[X] = (p3[X] * IN_PER_PX * dwDPI);
618         p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
619         p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
620         p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
622         LONG const x1 = (LONG) round(p1[X]);
623         LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
624         LONG const x2 = (LONG) round(p2[X]);
625         LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
626         LONG const x3 = (LONG) round(p3[X]);
627         LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
629         switch (bp->code) {
630             case NR_MOVETO:
631             case NR_MOVETO_OPEN:
632             case NR_LINETO:
633                 lpPoints[i].x = x3;
634                 lpPoints[i].y = y3;
635                 i = i + 1;
636                 break;
637             case NR_CURVETO:
638                 lpPoints[i].x = x1;
639                 lpPoints[i].y = y1;
640                 lpPoints[i+1].x = x2;
641                 lpPoints[i+1].y = y2;
642                 lpPoints[i+2].x = x3;
643                 lpPoints[i+2].y = y3;
644                 i = i + 3;
645                 break;
646         }
647         
648         bp += 1;
649     }
651     bool done = false;
652     bool circular = (lpPoints[0].x == lpPoints[i-1].x) && (lpPoints[0].y == lpPoints[i-1].y);
653     bool polygon = false;
654     bool ellipse = false;
655     
656     if (moves == 1 && moves+lines == nodes && circular) {
657         polygon = true;
658     }
659     else if (moves == 1 && nodes == 5 && moves+curves == nodes && circular) {
660         if (lpPoints[0].x == lpPoints[1].x && lpPoints[1].x == lpPoints[11].x &&
661             lpPoints[5].x == lpPoints[6].x && lpPoints[6].x == lpPoints[7].x &&
662             lpPoints[2].x == lpPoints[10].x && lpPoints[3].x == lpPoints[9].x && lpPoints[4].x == lpPoints[8].x &&
663             lpPoints[2].y == lpPoints[3].y && lpPoints[3].y == lpPoints[4].y &&
664             lpPoints[8].y == lpPoints[9].y && lpPoints[9].y == lpPoints[10].y &&
665             lpPoints[5].y == lpPoints[1].y && lpPoints[6].y == lpPoints[0].y && lpPoints[7].y == lpPoints[11].y)
666         {
667             ellipse = true;
668         }
669     }
671     if (polygon || ellipse) {
672         HPEN hpenTmp = NULL;
673         HPEN hpenOld = NULL;
674         HBRUSH hbrushTmp = NULL;
675         HBRUSH hbrushOld = NULL;
677         if (!stroke_and_fill) {
678             if (fill_only) {
679                 hpenTmp = (HPEN) GetStockObject(NULL_PEN);
680                 hpenOld = (HPEN) SelectObject( hdc, hpenTmp );
681             }
682             else { // if (stroke_only)
683                 hbrushTmp = (HBRUSH) GetStockObject(NULL_BRUSH);
684                 hbrushOld = (HBRUSH) SelectObject( hdc, hbrushTmp );
685             }
686         }
688         if (polygon) {
689             Polygon( hdc, lpPoints, nodes );
690         }
691         else if (ellipse) {
692             Ellipse( hdc, lpPoints[6].x, lpPoints[3].y, lpPoints[0].x, lpPoints[9].y);
693         }
694         
695         done = true;
697         if (hpenOld)
698             SelectObject( hdc, hpenOld );
699         if (hpenTmp)
700             DeleteObject( hpenTmp );
701         if (hbrushOld)
702             SelectObject( hdc, hbrushOld );
703         if (hbrushTmp)
704             DeleteObject( hbrushTmp );
705     }
707     delete[] lpPoints;
708     
709     return done;
712 unsigned int
713 PrintEmfWin32::print_bpath(const NArtBpath *bp, const NR::Matrix *transform, NRRect const *pbox)
715     unsigned int closed;
716     NR::Matrix tf = *transform;
718     simple_shape = print_simple_shape(bp, &tf, pbox);
720     if (simple_shape)
721         return TRUE;
722     
723     BeginPath( hdc );
724     closed = FALSE;
725     while (bp->code != NR_END) {
726         using NR::X;
727         using NR::Y;
729         NR::Point p1(bp->c(1) * tf);
730         NR::Point p2(bp->c(2) * tf);
731         NR::Point p3(bp->c(3) * tf);
733         p1[X] = (p1[X] * IN_PER_PX * dwDPI);
734         p2[X] = (p2[X] * IN_PER_PX * dwDPI);
735         p3[X] = (p3[X] * IN_PER_PX * dwDPI);
736         p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
737         p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
738         p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
740         LONG const x1 = (LONG) round(p1[X]);
741         LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
742         LONG const x2 = (LONG) round(p2[X]);
743         LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
744         LONG const x3 = (LONG) round(p3[X]);
745         LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
747         switch (bp->code) {
748             case NR_MOVETO:
749                 if (closed) {
750                     CloseFigure( hdc );
751                 }
752                 closed = TRUE;
753                 MoveToEx( hdc, x3, y3, NULL );
754                 break;
755             case NR_MOVETO_OPEN:
756                 if (closed) {
757                     CloseFigure( hdc );
758                 }
759                 closed = FALSE;
760                 MoveToEx( hdc, x3, y3, NULL );
761                 break;
762             case NR_LINETO:
763                 LineTo( hdc, x3, y3 );
764                 break;
765             case NR_CURVETO:
766             {
767                 POINT pt[3];
768                 pt[0].x = x1;
769                 pt[0].y = y1;
770                 pt[1].x = x2;
771                 pt[1].y = y2;
772                 pt[2].x = x3;
773                 pt[2].y = y3;
775                 PolyBezierTo( hdc, pt, 3 );
776                 break;
777             }
778             default:
779                 break;
780         }
781         bp += 1;
782     }
783     if (closed) {
784         CloseFigure( hdc );
785     }
786     EndPath( hdc );
788     return closed;
792 bool
793 PrintEmfWin32::textToPath(Inkscape::Extension::Print * ext)
795     return ext->get_param_bool("textToPath");
798 unsigned int
799 PrintEmfWin32::text(Inkscape::Extension::Print *mod, char const *text, NR::Point p,
800               SPStyle const *const style)
802     if (!hdc) return 0;
804     HFONT hfont = NULL;
805     
806 #ifdef USE_PANGO_WIN32
807 /*
808     font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
809     if (tf) {
810         LOGFONT *lf = pango_win32_font_logfont(tf->pFont);
811         tf->Unref();
812         hfont = CreateFontIndirect(lf);
813         g_free(lf);
814     }
815 */
816 #endif
818     if (!hfont) {
819         if (PrintWin32::is_os_wide()) {
820             LOGFONTW *lf = (LOGFONTW*)g_malloc(sizeof(LOGFONTW));
821             g_assert(lf != NULL);
822             
823             lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
824             lf->lfWidth = 0;
825             lf->lfEscapement = 0;
826             lf->lfOrientation = 0;
827             lf->lfWeight =
828                 style->font_weight.value == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
829                 style->font_weight.value == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
830                 style->font_weight.value == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
831                 style->font_weight.value == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
832                 style->font_weight.value == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
833                 style->font_weight.value == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
834                 style->font_weight.value == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
835                 style->font_weight.value == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
836                 style->font_weight.value == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
837                 style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL ? FW_NORMAL :
838                 style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD ? FW_BOLD :
839                 style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER ? FW_EXTRALIGHT :
840                 style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER ? FW_EXTRABOLD :
841                 FW_NORMAL;
842             lf->lfItalic = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
843             lf->lfUnderline = style->text_decoration.underline;
844             lf->lfStrikeOut = style->text_decoration.line_through;
845             lf->lfCharSet = DEFAULT_CHARSET;
846             lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
847             lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
848             lf->lfQuality = DEFAULT_QUALITY;
849             lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
850             
851             gunichar2 *unicode_name = g_utf8_to_utf16( style->text->font_family.value, -1, NULL, NULL, NULL );
852             wcsncpy(lf->lfFaceName, (wchar_t*) unicode_name, LF_FACESIZE-1);
853             g_free(unicode_name);
854             
855             hfont = CreateFontIndirectW(lf);
856             
857             g_free(lf);
858         }
859         else {
860             LOGFONTA *lf = (LOGFONTA*)g_malloc(sizeof(LOGFONTA));
861             g_assert(lf != NULL);
862             
863             lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
864             lf->lfWidth = 0;
865             lf->lfEscapement = 0;
866             lf->lfOrientation = 0;
867             lf->lfWeight =
868                 style->font_weight.value == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
869                 style->font_weight.value == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
870                 style->font_weight.value == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
871                 style->font_weight.value == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
872                 style->font_weight.value == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
873                 style->font_weight.value == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
874                 style->font_weight.value == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
875                 style->font_weight.value == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
876                 style->font_weight.value == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
877                 style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL ? FW_NORMAL :
878                 style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD ? FW_BOLD :
879                 style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER ? FW_EXTRALIGHT :
880                 style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER ? FW_EXTRABOLD :
881                 FW_NORMAL;
882             lf->lfItalic = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
883             lf->lfUnderline = style->text_decoration.underline;
884             lf->lfStrikeOut = style->text_decoration.line_through;
885             lf->lfCharSet = DEFAULT_CHARSET;
886             lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
887             lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
888             lf->lfQuality = DEFAULT_QUALITY;
889             lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
890             
891             strncpy(lf->lfFaceName, (char*) style->text->font_family.value, LF_FACESIZE-1);
893             hfont = CreateFontIndirectA(lf);
894             
895             g_free(lf);
896         }
897     }
898     
899     HFONT hfontOld = (HFONT) SelectObject(hdc, hfont);
901     float rgb[3];
902     sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
903     SetTextColor(hdc, RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]));
905     int align =
906         style->text_align.value == SP_CSS_TEXT_ALIGN_RIGHT ? TA_RIGHT :
907         style->text_align.value == SP_CSS_TEXT_ALIGN_CENTER ? TA_CENTER : TA_LEFT;
908     SetTextAlign(hdc, TA_BASELINE | align);
909     SetBkMode(hdc, TRANSPARENT);
911     NR::Matrix tf = m_tr_stack.top();
913     p = p * tf;
914     p[NR::X] = (p[NR::X] * IN_PER_PX * dwDPI);
915     p[NR::Y] = (p[NR::Y] * IN_PER_PX * dwDPI);
917     LONG const xpos = (LONG) round(p[NR::X]);
918     LONG const ypos = (LONG) round(rc.bottom-p[NR::Y]);
920     if (PrintWin32::is_os_wide()) {
921         gunichar2 *unicode_text = g_utf8_to_utf16( text, -1, NULL, NULL, NULL );
922         TextOutW(hdc, xpos, ypos, (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
923     }
924     else {
925         TextOutA(hdc, xpos, ypos, (CHAR*)text, strlen((char*)text));
926     }
928     SelectObject(hdc, hfontOld);
929     DeleteObject(hfont);
930     
931     return 0;
934 void
935 PrintEmfWin32::init (void)
937     Inkscape::Extension::Extension * ext;
939     /* EMF print */
940     ext = Inkscape::Extension::build_from_mem(
941         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
942         "<name>Enhanced Metafile Print</name>\n"
943         "<id>org.inkscape.print.emf.win32</id>\n"
944         "<param name=\"destination\" type=\"string\"></param>\n"
945         "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
946         "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
947         "<print/>\n"
948         "</inkscape-extension>", new PrintEmfWin32());
950     return;
954 }  /* namespace Internal */
955 }  /* namespace Extension */
956 }  /* namespace Inkscape */
958 #endif /* WIN32 */
960 /*
961   Local Variables:
962   mode:c++
963   c-file-style:"stroustrup"
964   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
965   indent-tabs-mode:nil
966   fill-column:99
967   End:
968 */
969 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :