Code

9b1b77f160e08effff9b78c2ea28c6e1059044e5
[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 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-fns.h"
39 #include "libnr/nr-path.h"
40 #include "libnr/nr-pixblock.h"
41 #include "display/canvas-bpath.h"
42 #include "sp-item.h"
44 #include "glib.h"
45 #include "gtk/gtkdialog.h"
46 #include "gtk/gtkbox.h"
47 #include "gtk/gtkstock.h"
49 #include "glibmm/i18n.h"
50 #include "enums.h"
51 #include "document.h"
52 #include "style.h"
53 #include "sp-paint-server.h"
54 #include "inkscape_version.h"
56 #include "win32.h"
57 #include "emf-win32-print.h"
59 #include "unit-constants.h"
61 #include "extension/extension.h"
62 #include "extension/system.h"
63 #include "extension/print.h"
65 #include "io/sys.h"
67 #include "macros.h"
69 #define WIN32_LEAN_AND_MEAN
70 #include <windows.h>
72 namespace Inkscape {
73 namespace Extension {
74 namespace Internal {
76 static float dwDPI = 2540;
79 PrintEmfWin32::PrintEmfWin32 (void):
80     hdc(NULL),
81     hbrush(NULL),
82     hpen(NULL),
83     fill_path(NULL)
84 {
85 }
88 PrintEmfWin32::~PrintEmfWin32 (void)
89 {
90     if (hdc) {
91         HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
92         if ( metafile ) {
93             DeleteEnhMetaFile( metafile );
94         }
95         DeleteDC( hdc );
96     }
98     /* restore default signal handling for SIGPIPE */
99 #if !defined(_WIN32) && !defined(__WIN32__)
100     (void) signal(SIGPIPE, SIG_DFL);
101 #endif
102         return;
106 unsigned int
107 PrintEmfWin32::setup (Inkscape::Extension::Print *mod)
109     return TRUE;
113 unsigned int
114 PrintEmfWin32::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
116     gchar const *utf8_fn = mod->get_param_string("destination");
118     gsize bytesRead = 0;
119     gsize bytesWritten = 0;
120     GError* error = NULL;
121     gchar *local_fn =
122         g_filename_from_utf8( utf8_fn, -1,  &bytesRead,  &bytesWritten, &error );
124     if (local_fn == NULL) {
125         return 1;
126     }
128     CHAR *ansi_uri = (CHAR *) local_fn;
129     gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
130     WCHAR *unicode_uri = (WCHAR *) unicode_fn;
132     // width and height in px
133     _width = sp_document_width(doc);
134     _height = sp_document_height(doc);
136     NRRect d;
137     bool pageBoundingBox;
138     pageBoundingBox = mod->get_param_bool("pageBoundingBox");
139     if (pageBoundingBox) {
140         d.x0 = d.y0 = 0;
141         d.x1 = _width;
142         d.y1 = _height;
143     } else {
144         SPItem* doc_item = SP_ITEM(sp_document_root(doc));
145         sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
146     }
148     d.x0 *= IN_PER_PX;
149     d.y0 *= IN_PER_PX;
150     d.x1 *= IN_PER_PX;
151     d.y1 *= IN_PER_PX;
153     float dwInchesX = (d.x1 - d.x0);
154     float dwInchesY = (d.y1 - d.y0);
156     // dwInchesX x dwInchesY in .01mm units
157     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*2540), (int) ceil(dwInchesY*2540) );
159     // Get a Reference DC
160     HDC hScreenDC = GetDC( NULL );
162     // Get the physical characteristics of the reference DC
163     float PixelsX = (float) GetDeviceCaps( hScreenDC, HORZRES );
164     float PixelsY = (float) GetDeviceCaps( hScreenDC, VERTRES );
165     float MMX = (float) GetDeviceCaps( hScreenDC, HORZSIZE );
166     float MMY = (float) GetDeviceCaps( hScreenDC, VERTSIZE );
168     // Create the Metafile
169     if (PrintWin32::is_os_wide())
170         hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, NULL );
171     else
172         hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, NULL );
174     // Release the reference DC
175     ReleaseDC( NULL, hScreenDC );
177     // Did we get a good metafile?
178     if (hdc == NULL)
179     {
180         g_free(local_fn);
181         g_free(unicode_fn);
182         return 1;
183     }
185     // Anisotropic mapping mode
186     SetMapMode( hdc, MM_ANISOTROPIC );
188     // Set the Windows extent
189     SetWindowExtEx( hdc, (int) (dwInchesX*dwDPI), (int) (dwInchesY*dwDPI), NULL );
191     // Set the viewport extent to reflect
192     // dwInchesX" x dwInchesY" in device units
193     SetViewportExtEx( hdc,
194                       (int) ((float) dwInchesX*25.4f*PixelsX/MMX),
195                       (int) ((float) dwInchesY*25.4f*PixelsY/MMY),
196                       NULL );
198     SetRect( &rc, 0, 0, (int) ceil(dwInchesX*dwDPI), (int) ceil(dwInchesY*dwDPI) );
200     g_free(local_fn);
201     g_free(unicode_fn);
203     return 0;
207 unsigned int
208 PrintEmfWin32::finish (Inkscape::Extension::Print *mod)
210     if (!hdc) return 0;
212     flush_fill(); // flush any pending fills
214     HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
215     if ( metafile ) {
216         DeleteEnhMetaFile( metafile );
217     }
218     DeleteDC( hdc );
220     hdc = NULL;
222     return 0;
226 unsigned int
227 PrintEmfWin32::comment (Inkscape::Extension::Print * module,
228                                 const char *comment)
230     if (!hdc) return 0;
232     flush_fill(); // flush any pending fills
234     return 0;
238 void
239 PrintEmfWin32::create_brush(SPStyle const *style)
241     float rgb[3];
243     if (style) {
244         sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
245         hbrush = CreateSolidBrush( RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]) );
246         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
248         SetPolyFillMode( hdc,
249                          style->fill_rule.computed == 0 ? WINDING :
250                          style->fill_rule.computed == 2 ? ALTERNATE : ALTERNATE );
251     } else { // if (!style)
252         hbrush = CreateSolidBrush( RGB(255, 255, 255) );
253         hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
254         SetPolyFillMode( hdc, ALTERNATE );
255     }
259 void
260 PrintEmfWin32::destroy_brush()
262     SelectObject( hdc, hbrushOld );
263     if (hbrush)
264         DeleteObject( hbrush );
265     hbrush = NULL;
269 void
270 PrintEmfWin32::create_pen(SPStyle const *style)
272     if (style) {
273         float rgb[3];
275         sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
277         LOGBRUSH lb = {0};
278         lb.lbStyle = BS_SOLID;
279         lb.lbColor = RGB( 255*rgb[0], 255*rgb[1], 255*rgb[2] );
281         int linestyle = PS_SOLID;
282         int linecap = 0;
283         int linejoin = 0;
284         DWORD n_dash = 0;
285         DWORD *dash = NULL;
286         float oldmiterlimit;
288         DWORD linewidth = MAX( 1, (DWORD) (style->stroke_width.computed * IN_PER_PX * dwDPI) );
290         if (style->stroke_linecap.computed == 0) {
291             linecap = PS_ENDCAP_FLAT;
292         }
293         else if (style->stroke_linecap.computed == 1) {
294             linecap = PS_ENDCAP_ROUND;
295         }
296         else if (style->stroke_linecap.computed == 2) {
297             linecap = PS_ENDCAP_SQUARE;
298         }
300         if (style->stroke_linejoin.computed == 0) {
301             linejoin = PS_JOIN_MITER;
302         }
303         else if (style->stroke_linejoin.computed == 1) {
304             linejoin = PS_JOIN_ROUND;
305         }
306         else if (style->stroke_linejoin.computed == 2) {
307             linejoin = PS_JOIN_BEVEL;
308         }
310         if (style->stroke_dash.n_dash   &&
311             style->stroke_dash.dash       )
312         {
313             int i = 0;
314             while (linestyle != PS_USERSTYLE &&
315                    (i < style->stroke_dash.n_dash)) {
316                 if (style->stroke_dash.dash[i] > 0.00000001)
317                     linestyle = PS_USERSTYLE;
318                 i++;
319             }
321             if (linestyle == PS_USERSTYLE) {
322                 n_dash = style->stroke_dash.n_dash;
323                 dash = new DWORD[n_dash];
324                 for (i = 0; i < style->stroke_dash.n_dash; i++) {
325                     dash[i] = (DWORD) (style->stroke_dash.dash[i] * IN_PER_PX * dwDPI);
326                 }
327             }
328         }
330         hpen = ExtCreatePen(
331             PS_GEOMETRIC | linestyle | linecap | linejoin,
332             linewidth,
333             &lb,
334             n_dash,
335             dash );
337         if ( !hpen && linestyle == PS_USERSTYLE ) {
338             hpen = ExtCreatePen(
339                 PS_GEOMETRIC | PS_SOLID | linecap | linejoin,
340                 linewidth,
341                 &lb,
342                 0,
343                 NULL );
344         }
346         if ( !hpen ) {
347             hpen = CreatePen(
348                 PS_SOLID,
349                 linewidth,
350                 lb.lbColor );
351         }
353         hpenOld = (HPEN) SelectObject( hdc, hpen );
355         if (linejoin == PS_JOIN_MITER) {
356             float miterlimit = style->stroke_miterlimit.value;
357             if (miterlimit < 1)
358                 miterlimit = 4.0;
359             SetMiterLimit(
360                 hdc,
361                 miterlimit * IN_PER_PX * dwDPI,
362                 &oldmiterlimit );
363         }
365         if (n_dash) {
366             delete[] dash;
367         }
368     }
369     else { // if (!style)
370         hpen = CreatePen( PS_SOLID, 1, RGB(0, 0, 0) );
371         hpenOld = (HPEN) SelectObject( hdc, hpen );
372     }
376 void
377 PrintEmfWin32::destroy_pen()
379     SelectObject( hdc, hpenOld );
380     if (hpen)
381         DeleteObject( hpen );
382     hpen = NULL;
386 void
387 PrintEmfWin32::flush_fill()
389     if (fill_path) {
390         print_bpath(fill_path, &fill_transform, &fill_pbox);
391         FillPath( hdc );
392         destroy_brush();
393         delete[] fill_path;
394         fill_path = NULL;
395     }
399 NArtBpath *
400 PrintEmfWin32::copy_bpath(const NArtBpath *bp)
402     NArtBpath *tmp = (NArtBpath *) bp;
403     int num = 1;
404     
405     while (tmp->code != NR_END) {
406         num++;
407         tmp += 1;
408     }
410     tmp = new NArtBpath[num];
411     while (num--) {
412         tmp[num] = bp[num];
413     }
415     return tmp;
419 int
420 PrintEmfWin32::cmp_bpath(const NArtBpath *bp1, const NArtBpath *bp2)
422     if (!bp1 || !bp2) {
423         return 1;
424     }
425     
426     while (bp1->code != NR_END && bp2->code != NR_END) {
427         if (bp1->code != bp2->code) {
428             return 1;
429         }
431         if ( fabs(bp1->x1 - bp2->x1) > 0.00000001 ||
432              fabs(bp1->y1 - bp2->y1) > 0.00000001 ||
433              fabs(bp1->x2 - bp2->x2) > 0.00000001 ||
434              fabs(bp1->y2 - bp2->y2) > 0.00000001 ||
435              fabs(bp1->x3 - bp2->x3) > 0.00000001 ||
436              fabs(bp1->y3 - bp2->y3) > 0.00000001 )
437         {
438             return 1;
439         }
440         
441         bp1 += 1;
442         bp2 += 1;
443     }
444     
445     return bp1->code != NR_END || bp2->code != NR_END;
449 unsigned int
450 PrintEmfWin32::fill(Inkscape::Extension::Print *mod,
451                NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,
452                NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
454     if (!hdc) return 0;
456     flush_fill(); // flush any pending fills
458     if (style->fill.type == SP_PAINT_TYPE_COLOR) {
459         create_brush(style);
460     } else {
461         // create_brush(NULL);
462         return 0;
463     }
465     fill_path = copy_bpath( bpath->path );
466     fill_transform = *transform;
467     fill_pbox = *pbox;
469     // postpone fill in case of stroke-and-fill
471     return 0;
475 unsigned int
476 PrintEmfWin32::stroke (Inkscape::Extension::Print *mod,
477                   const NRBPath *bpath, const NRMatrix *transform, const SPStyle *style,
478                   const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
480     if (!hdc) return 0;
482     bool stroke_and_fill = ( cmp_bpath( bpath->path, fill_path ) == 0 );
484     if (!stroke_and_fill) {
485         flush_fill(); // flush any pending fills
486     }
488     if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
489         create_pen(style);
490     } else {
491         // create_pen(NULL);
492         return 0;
493     }
495     print_bpath(bpath->path, transform, pbox);
497     if (stroke_and_fill) {
498         StrokeAndFillPath( hdc );
499         destroy_brush();
500         delete[] fill_path;
501         fill_path = NULL;
502     } else {
503         StrokePath( hdc );
504     }
506     destroy_pen();
508     return 0;
512 unsigned int
513 PrintEmfWin32::print_bpath(const NArtBpath *bp, const NRMatrix *transform, NRRect const *pbox)
515     unsigned int closed;
516     NR::Matrix tf = *transform;
518     BeginPath( hdc );
519     closed = FALSE;
520     while (bp->code != NR_END) {
521         using NR::X;
522         using NR::Y;
524         NR::Point p1(bp->c(1) * tf);
525         NR::Point p2(bp->c(2) * tf);
526         NR::Point p3(bp->c(3) * tf);
528         p1[X] = (p1[X] * IN_PER_PX * dwDPI);
529         p2[X] = (p2[X] * IN_PER_PX * dwDPI);
530         p3[X] = (p3[X] * IN_PER_PX * dwDPI);
531         p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
532         p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
533         p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
535         LONG const x1 = (LONG) round(p1[X]);
536         LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
537         LONG const x2 = (LONG) round(p2[X]);
538         LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
539         LONG const x3 = (LONG) round(p3[X]);
540         LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
542         switch (bp->code) {
543             case NR_MOVETO:
544                 if (closed) {
545                     CloseFigure( hdc );
546                 }
547                 closed = TRUE;
548                 MoveToEx( hdc, x3, y3, NULL );
549                 break;
550             case NR_MOVETO_OPEN:
551                 if (closed) {
552                     CloseFigure( hdc );
553                 }
554                 closed = FALSE;
555                 MoveToEx( hdc, x3, y3, NULL );
556                 break;
557             case NR_LINETO:
558                 LineTo( hdc, x3, y3 );
559                 break;
560             case NR_CURVETO:
561             {
562                 POINT pt[3];
563                 pt[0].x = x1;
564                 pt[0].y = y1;
565                 pt[1].x = x2;
566                 pt[1].y = y2;
567                 pt[2].x = x3;
568                 pt[2].y = y3;
570                 PolyBezierTo( hdc, pt, 3 );
571                 break;
572             }
573             default:
574                 break;
575         }
576         bp += 1;
577     }
578     if (closed) {
579         CloseFigure( hdc );
580     }
581     EndPath( hdc );
583     return closed;
587 bool
588 PrintEmfWin32::textToPath(Inkscape::Extension::Print * ext)
590     return ext->get_param_bool("textToPath");
594 void
595 PrintEmfWin32::init (void)
597     Inkscape::Extension::Extension * ext;
599     /* EMF print */
600     ext = Inkscape::Extension::build_from_mem(
601         "<inkscape-extension>\n"
602         "<name>Enhanced Metafile Print</name>\n"
603         "<id>org.inkscape.print.emf.win32</id>\n"
604         "<param name=\"destination\" type=\"string\"></param>\n"
605         "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
606         "<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"
607         "<print/>\n"
608         "</inkscape-extension>", new PrintEmfWin32());
610     return;
614 }  /* namespace Internal */
615 }  /* namespace Extension */
616 }  /* namespace Inkscape */
618 #endif /* WIN32 */
620 /*
621   Local Variables:
622   mode:c++
623   c-file-style:"stroustrup"
624   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
625   indent-tabs-mode:nil
626   fill-column:99
627   End:
628 */
629 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :