1 /** @file
2 * @brief Enhanced Metafile printing
3 */
4 /* Authors:
5 * Ulf Erikson <ulferikson@users.sf.net>
6 *
7 * Copyright (C) 2006-2008 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;
108 }
111 unsigned int
112 PrintEmfWin32::setup (Inkscape::Extension::Print * /*mod*/)
113 {
114 return TRUE;
115 }
118 unsigned int
119 PrintEmfWin32::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
120 {
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);
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;
238 }
241 unsigned int
242 PrintEmfWin32::finish (Inkscape::Extension::Print * /*mod*/)
243 {
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;
257 }
260 unsigned int
261 PrintEmfWin32::comment (Inkscape::Extension::Print * /*module*/,
262 const char * /*comment*/)
263 {
264 if (!hdc) return 0;
266 flush_fill(); // flush any pending fills
268 return 0;
269 }
272 int
273 PrintEmfWin32::create_brush(SPStyle const *style)
274 {
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;
296 }
299 void
300 PrintEmfWin32::destroy_brush()
301 {
302 SelectObject( hdc, hbrushOld );
303 if (hbrush)
304 DeleteObject( hbrush );
305 hbrush = NULL;
306 hbrushOld = NULL;
307 }
310 void
311 PrintEmfWin32::create_pen(SPStyle const *style, const Geom::Matrix &transform)
312 {
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 }
431 }
434 void
435 PrintEmfWin32::destroy_pen()
436 {
437 SelectObject( hdc, hpenOld );
438 if (hpen)
439 DeleteObject( hpen );
440 hpen = NULL;
441 }
444 void
445 PrintEmfWin32::flush_fill()
446 {
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 }
457 }
459 unsigned int
460 PrintEmfWin32::bind(Inkscape::Extension::Print * /*mod*/, Geom::Matrix const *transform, float /*opacity*/)
461 {
462 Geom::Matrix tr = *transform;
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;
472 }
474 unsigned int
475 PrintEmfWin32::release(Inkscape::Extension::Print * /*mod*/)
476 {
477 m_tr_stack.pop();
478 return 1;
479 }
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*/)
485 {
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;
507 }
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*/)
514 {
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;
547 }
550 bool
551 PrintEmfWin32::print_simple_shape(Geom::PathVector const &pathv, const Geom::Matrix &transform)
552 {
553 Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
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++;
565 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
566 {
567 nodes++;
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;
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);
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);
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);
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 polyline = false;
678 bool rectangle = false;
679 bool ellipse = false;
681 if (moves == 1 && moves+lines == nodes && closed) {
682 polygon = true;
683 if (nodes==5) {
684 if (lpPoints[0].x == lpPoints[3].x && lpPoints[1].x == lpPoints[2].x &&
685 lpPoints[0].y == lpPoints[1].y && lpPoints[2].y == lpPoints[3].y)
686 {
687 rectangle = true;
688 }
689 }
690 }
691 else if (moves == 1 && moves+lines == nodes) {
692 polyline = true;
693 }
694 else if (moves == 1 && nodes == 5 && moves+curves == nodes && closed) {
695 if (lpPoints[0].x == lpPoints[1].x && lpPoints[1].x == lpPoints[11].x &&
696 lpPoints[5].x == lpPoints[6].x && lpPoints[6].x == lpPoints[7].x &&
697 lpPoints[2].x == lpPoints[10].x && lpPoints[3].x == lpPoints[9].x && lpPoints[4].x == lpPoints[8].x &&
698 lpPoints[2].y == lpPoints[3].y && lpPoints[3].y == lpPoints[4].y &&
699 lpPoints[8].y == lpPoints[9].y && lpPoints[9].y == lpPoints[10].y &&
700 lpPoints[5].y == lpPoints[1].y && lpPoints[6].y == lpPoints[0].y && lpPoints[7].y == lpPoints[11].y)
701 {
702 ellipse = true;
703 }
704 }
706 if (polygon || polyline || ellipse) {
707 HPEN hpenTmp = NULL;
708 HPEN hpenOld = NULL;
709 HBRUSH hbrushTmp = NULL;
710 HBRUSH hbrushOld = NULL;
712 if (!stroke_and_fill) {
713 if (fill_only) {
714 hpenTmp = (HPEN) GetStockObject(NULL_PEN);
715 hpenOld = (HPEN) SelectObject( hdc, hpenTmp );
716 }
717 else { // if (stroke_only)
718 hbrushTmp = (HBRUSH) GetStockObject(NULL_BRUSH);
719 hbrushOld = (HBRUSH) SelectObject( hdc, hbrushTmp );
720 }
721 }
723 if (polygon) {
724 if (rectangle)
725 Rectangle( hdc, lpPoints[0].x, lpPoints[0].y, lpPoints[2].x, lpPoints[2].y );
726 else
727 Polygon( hdc, lpPoints, nodes );
728 }
729 else if (polyline) {
730 Polyline( hdc, lpPoints, nodes );
731 }
732 else if (ellipse) {
733 Ellipse( hdc, lpPoints[6].x, lpPoints[3].y, lpPoints[0].x, lpPoints[9].y);
734 }
736 done = true;
738 if (hpenOld)
739 SelectObject( hdc, hpenOld );
740 if (hpenTmp)
741 DeleteObject( hpenTmp );
742 if (hbrushOld)
743 SelectObject( hdc, hbrushOld );
744 if (hbrushTmp)
745 DeleteObject( hbrushTmp );
746 }
748 delete[] lpPoints;
750 return done;
751 }
753 unsigned int
754 PrintEmfWin32::print_pathv(Geom::PathVector const &pathv, const Geom::Matrix &transform)
755 {
756 simple_shape = print_simple_shape(pathv, transform);
758 if (simple_shape)
759 return TRUE;
761 Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
763 BeginPath( hdc );
765 /**
766 * For all Subpaths in the <path>
767 */
768 for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
769 {
770 using Geom::X;
771 using Geom::Y;
773 Geom::Point p0 = pit->initialPoint();
775 p0[X] = (p0[X] * IN_PER_PX * dwDPI);
776 p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
778 LONG const x0 = (LONG) round(p0[X]);
779 LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
781 MoveToEx( hdc, x0, y0, NULL );
783 /**
784 * For all segments in the subpath
785 */
786 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
787 {
788 if ( is_straight_curve(*cit) )
789 {
790 //Geom::Point p0 = cit->initialPoint();
791 Geom::Point p1 = cit->finalPoint();
793 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
794 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
795 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
796 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
798 //LONG const x0 = (LONG) round(p0[X]);
799 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
800 LONG const x1 = (LONG) round(p1[X]);
801 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
803 LineTo( hdc, x1, y1 );
804 }
805 else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
806 {
807 std::vector<Geom::Point> points = cubic->points();
808 //Geom::Point p0 = points[0];
809 Geom::Point p1 = points[1];
810 Geom::Point p2 = points[2];
811 Geom::Point p3 = points[3];
813 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
814 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
815 p2[X] = (p2[X] * IN_PER_PX * dwDPI);
816 p3[X] = (p3[X] * IN_PER_PX * dwDPI);
817 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
818 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
819 p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
820 p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
822 //LONG const x0 = (LONG) round(p0[X]);
823 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
824 LONG const x1 = (LONG) round(p1[X]);
825 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
826 LONG const x2 = (LONG) round(p2[X]);
827 LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
828 LONG const x3 = (LONG) round(p3[X]);
829 LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
831 POINT pt[3];
832 pt[0].x = x1;
833 pt[0].y = y1;
834 pt[1].x = x2;
835 pt[1].y = y2;
836 pt[2].x = x3;
837 pt[2].y = y3;
839 PolyBezierTo( hdc, pt, 3 );
840 }
841 else
842 {
843 g_warning("logical error, because pathv_to_linear_and_cubic_beziers was used");
844 }
845 }
847 if (pit->end_default() == pit->end_closed()) {
848 CloseFigure( hdc );
849 }
850 }
852 EndPath( hdc );
854 return TRUE;
855 }
858 bool
859 PrintEmfWin32::textToPath(Inkscape::Extension::Print * ext)
860 {
861 return ext->get_param_bool("textToPath");
862 }
864 unsigned int
865 PrintEmfWin32::text(Inkscape::Extension::Print * /*mod*/, char const *text, Geom::Point p,
866 SPStyle const *const style)
867 {
868 if (!hdc) return 0;
870 HFONT hfont = NULL;
872 #ifdef USE_PANGO_WIN32
873 /*
874 font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
875 if (tf) {
876 LOGFONT *lf = pango_win32_font_logfont(tf->pFont);
877 tf->Unref();
878 hfont = CreateFontIndirect(lf);
879 g_free(lf);
880 }
881 */
882 #endif
884 if (!hfont) {
885 if (PrintWin32::is_os_wide()) {
886 LOGFONTW *lf = (LOGFONTW*)g_malloc(sizeof(LOGFONTW));
887 g_assert(lf != NULL);
889 lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
890 lf->lfWidth = 0;
891 lf->lfEscapement = 0;
892 lf->lfOrientation = 0;
893 lf->lfWeight =
894 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
895 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
896 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
897 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
898 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
899 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
900 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
901 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
902 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
903 FW_NORMAL;
904 lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
905 lf->lfUnderline = style->text_decoration.underline;
906 lf->lfStrikeOut = style->text_decoration.line_through;
907 lf->lfCharSet = DEFAULT_CHARSET;
908 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
909 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
910 lf->lfQuality = DEFAULT_QUALITY;
911 lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
913 gunichar2 *unicode_name = g_utf8_to_utf16( style->text->font_family.value, -1, NULL, NULL, NULL );
914 wcsncpy(lf->lfFaceName, (wchar_t*) unicode_name, LF_FACESIZE-1);
915 g_free(unicode_name);
917 hfont = CreateFontIndirectW(lf);
919 g_free(lf);
920 }
921 else {
922 LOGFONTA *lf = (LOGFONTA*)g_malloc(sizeof(LOGFONTA));
923 g_assert(lf != NULL);
925 lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
926 lf->lfWidth = 0;
927 lf->lfEscapement = 0;
928 lf->lfOrientation = 0;
929 lf->lfWeight =
930 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
931 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
932 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
933 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
934 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
935 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
936 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
937 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
938 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
939 FW_NORMAL;
940 lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
941 lf->lfUnderline = style->text_decoration.underline;
942 lf->lfStrikeOut = style->text_decoration.line_through;
943 lf->lfCharSet = DEFAULT_CHARSET;
944 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
945 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
946 lf->lfQuality = DEFAULT_QUALITY;
947 lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
949 strncpy(lf->lfFaceName, (char*) style->text->font_family.value, LF_FACESIZE-1);
951 hfont = CreateFontIndirectA(lf);
953 g_free(lf);
954 }
955 }
957 HFONT hfontOld = (HFONT) SelectObject(hdc, hfont);
959 float rgb[3];
960 sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
961 SetTextColor(hdc, RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]));
963 // Text alignment:
964 // - (x,y) coordinates received by this filter are those of the point where the text
965 // actually starts, and already takes into account the text object's alignment;
966 // - for this reason, the EMF text alignment must always be TA_BASELINE|TA_LEFT.
967 SetTextAlign(hdc, TA_BASELINE | TA_LEFT);
969 // Transparent text background
970 SetBkMode(hdc, TRANSPARENT);
972 Geom::Matrix tf = m_tr_stack.top();
974 p = p * tf;
975 p[Geom::X] = (p[Geom::X] * IN_PER_PX * dwDPI);
976 p[Geom::Y] = (p[Geom::Y] * IN_PER_PX * dwDPI);
978 LONG const xpos = (LONG) round(p[Geom::X]);
979 LONG const ypos = (LONG) round(rc.bottom-p[Geom::Y]);
981 if (PrintWin32::is_os_wide()) {
982 gunichar2 *unicode_text = g_utf8_to_utf16( text, -1, NULL, NULL, NULL );
983 TextOutW(hdc, xpos, ypos, (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
984 }
985 else {
986 TextOutA(hdc, xpos, ypos, (CHAR*)text, strlen((char*)text));
987 }
989 SelectObject(hdc, hfontOld);
990 DeleteObject(hfont);
992 return 0;
993 }
995 void
996 PrintEmfWin32::init (void)
997 {
998 Inkscape::Extension::Extension * ext;
1000 /* EMF print */
1001 ext = Inkscape::Extension::build_from_mem(
1002 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
1003 "<name>Enhanced Metafile Print</name>\n"
1004 "<id>org.inkscape.print.emf.win32</id>\n"
1005 "<param name=\"destination\" type=\"string\"></param>\n"
1006 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
1007 "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
1008 "<print/>\n"
1009 "</inkscape-extension>", new PrintEmfWin32());
1011 return;
1012 }
1015 } /* namespace Internal */
1016 } /* namespace Extension */
1017 } /* namespace Inkscape */
1019 #endif /* WIN32 */
1021 /*
1022 Local Variables:
1023 mode:c++
1024 c-file-style:"stroustrup"
1025 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1026 indent-tabs-mode:nil
1027 fill-column:99
1028 End:
1029 */
1030 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :