8c92fc3209f8d3531093cf8f7a2ac02dbd085235
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 "libnr/nr-point-matrix-ops.h"
34 //#include "libnr/nr-rect.h"
35 //#include "libnr/nr-matrix.h"
36 #include "libnr/nr-matrix-ops.h"
37 //#include "libnr/nr-matrix-scale-ops.h"
38 //#include "libnr/nr-matrix-translate-ops.h"
39 #include "libnr/nr-scale-translate-ops.h"
40 //#include "libnr/nr-translate-scale-ops.h"
41 //#include "libnr/nr-matrix-fns.h"
42 //#include "libnr/nr-pixblock.h"
43 #include <2geom/pathvector.h>
44 #include <2geom/rect.h>
45 #include <2geom/bezier-curve.h>
46 #include <2geom/hvlinesegment.h>
47 #include "helper/geom.h"
48 #include "helper/geom-curves.h"
49 //#include "display/canvas-bpath.h"
50 #include "sp-item.h"
52 //#include "glib.h"
53 //#include "gtk/gtkdialog.h"
54 //#include "gtk/gtkbox.h"
55 //#include "gtk/gtkstock.h"
57 //#include "glibmm/i18n.h"
58 //#include "enums.h"
59 //#include "document.h"
60 #include "style.h"
61 //#include "sp-paint-server.h"
62 #include "inkscape_version.h"
64 //#include "libnrtype/FontFactory.h"
65 //#include "libnrtype/font-instance.h"
66 //#include "libnrtype/font-style-to-pos.h"
68 #define WIN32_LEAN_AND_MEAN
69 #include <windows.h>
71 #include "win32.h"
72 #include "emf-win32-print.h"
74 #include "unit-constants.h"
76 //#include "extension/extension.h"
77 #include "extension/system.h"
78 #include "extension/print.h"
80 //#include "io/sys.h"
82 //#include "macros.h"
84 namespace Inkscape {
85 namespace Extension {
86 namespace Internal {
88 static float dwDPI = 2540;
91 PrintEmfWin32::PrintEmfWin32 (void):
92 hdc(NULL),
93 hbrush(NULL),
94 hbrushOld(NULL),
95 hpen(NULL),
96 stroke_and_fill(false),
97 fill_only(false),
98 simple_shape(false)
99 {
100 }
103 PrintEmfWin32::~PrintEmfWin32 (void)
104 {
105 if (hdc) {
106 HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
107 if ( metafile ) {
108 DeleteEnhMetaFile( metafile );
109 }
110 DeleteDC( hdc );
111 }
113 /* restore default signal handling for SIGPIPE */
114 #if !defined(_WIN32) && !defined(__WIN32__)
115 (void) signal(SIGPIPE, SIG_DFL);
116 #endif
117 return;
118 }
121 unsigned int
122 PrintEmfWin32::setup (Inkscape::Extension::Print * /*mod*/)
123 {
124 return TRUE;
125 }
128 unsigned int
129 PrintEmfWin32::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
130 {
131 gchar const *utf8_fn = mod->get_param_string("destination");
133 gsize bytesRead = 0;
134 gsize bytesWritten = 0;
135 GError* error = NULL;
136 gchar *local_fn =
137 g_filename_from_utf8( utf8_fn, -1, &bytesRead, &bytesWritten, &error );
139 if (local_fn == NULL) {
140 return 1;
141 }
143 CHAR *ansi_uri = (CHAR *) local_fn;
144 gunichar2 *unicode_fn = g_utf8_to_utf16( local_fn, -1, NULL, NULL, NULL );
145 WCHAR *unicode_uri = (WCHAR *) unicode_fn;
147 // width and height in px
148 _width = sp_document_width(doc);
149 _height = sp_document_height(doc);
151 NRRect d;
152 bool pageBoundingBox;
153 pageBoundingBox = mod->get_param_bool("pageBoundingBox");
154 if (pageBoundingBox) {
155 d.x0 = d.y0 = 0;
156 d.x1 = _width;
157 d.y1 = _height;
158 } else {
159 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
160 sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
161 }
163 d.x0 *= IN_PER_PX;
164 d.y0 *= IN_PER_PX;
165 d.x1 *= IN_PER_PX;
166 d.y1 *= IN_PER_PX;
168 float dwInchesX = (d.x1 - d.x0);
169 float dwInchesY = (d.y1 - d.y0);
171 // dwInchesX x dwInchesY in .01mm units
172 SetRect( &rc, 0, 0, (int) ceil(dwInchesX*2540), (int) ceil(dwInchesY*2540) );
174 // Get a Reference DC
175 HDC hScreenDC = GetDC( NULL );
177 // Get the physical characteristics of the reference DC
178 int PixelsX = GetDeviceCaps( hScreenDC, HORZRES );
179 int PixelsY = GetDeviceCaps( hScreenDC, VERTRES );
180 int MMX = GetDeviceCaps( hScreenDC, HORZSIZE );
181 int MMY = GetDeviceCaps( hScreenDC, VERTSIZE );
183 CHAR buff[1024];
184 ZeroMemory(buff, sizeof(buff));
185 snprintf(buff, sizeof(buff)-1, "Inkscape %s (%s)", INKSCAPE_VERSION, __DATE__);
186 INT len = strlen(buff);
187 CHAR *p1 = strrchr(ansi_uri, '\\');
188 CHAR *p2 = strrchr(ansi_uri, '/');
189 CHAR *p = MAX(p1, p2);
190 if (p)
191 p++;
192 else
193 p = ansi_uri;
194 snprintf(buff+len+1, sizeof(buff)-len-2, "%s", p);
196 // Create the Metafile
197 if (PrintWin32::is_os_wide()) {
198 WCHAR wbuff[1024];
199 ZeroMemory(wbuff, sizeof(wbuff));
200 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buff, sizeof(buff)/sizeof(buff[0]), wbuff, sizeof(wbuff)/sizeof(wbuff[0]));
201 hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, wbuff );
202 }
203 else {
204 hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, buff );
205 }
207 // Release the reference DC
208 ReleaseDC( NULL, hScreenDC );
210 // Did we get a good metafile?
211 if (hdc == NULL)
212 {
213 g_free(local_fn);
214 g_free(unicode_fn);
215 return 1;
216 }
218 // Anisotropic mapping mode
219 SetMapMode( hdc, MM_ANISOTROPIC );
221 // Set the Windows extent
222 int windowextX = (int) ceil(dwInchesX*dwDPI);
223 int windowextY = (int) ceil(dwInchesY*dwDPI);
224 SetWindowExtEx( hdc, windowextX, windowextY, NULL );
226 // Set the viewport extent to reflect
227 // dwInchesX" x dwInchesY" in device units
228 int viewportextX = (int)((float)dwInchesX*25.4f*(float)PixelsX/(float)MMX);
229 int viewportextY = (int)((float)dwInchesY*25.4f*(float)PixelsY/(float)MMY);
230 SetViewportExtEx( hdc, viewportextX, viewportextY, NULL );
232 if (1) {
233 snprintf(buff, sizeof(buff)-1, "Screen=%dx%dpx, %dx%dmm", PixelsX, PixelsY, MMX, MMY);
234 GdiComment(hdc, strlen(buff), (BYTE*) buff);
236 snprintf(buff, sizeof(buff)-1, "Drawing=%.1lfx%.1lfpx, %.1lfx%.1lfmm", _width, _height, dwInchesX * MM_PER_IN, dwInchesY * MM_PER_IN);
237 GdiComment(hdc, strlen(buff), (BYTE*) buff);
238 }
240 SetRect( &rc, 0, 0, (int) ceil(dwInchesX*dwDPI), (int) ceil(dwInchesY*dwDPI) );
242 g_free(local_fn);
243 g_free(unicode_fn);
245 m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, sp_document_height(doc)));
247 return 0;
248 }
251 unsigned int
252 PrintEmfWin32::finish (Inkscape::Extension::Print * /*mod*/)
253 {
254 if (!hdc) return 0;
256 flush_fill(); // flush any pending fills
258 HENHMETAFILE metafile = CloseEnhMetaFile( hdc );
259 if ( metafile ) {
260 DeleteEnhMetaFile( metafile );
261 }
262 DeleteDC( hdc );
264 hdc = NULL;
266 return 0;
267 }
270 unsigned int
271 PrintEmfWin32::comment (Inkscape::Extension::Print * /*module*/,
272 const char * /*comment*/)
273 {
274 if (!hdc) return 0;
276 flush_fill(); // flush any pending fills
278 return 0;
279 }
282 int
283 PrintEmfWin32::create_brush(SPStyle const *style)
284 {
285 float rgb[3];
287 if (style) {
288 float opacity = SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
289 if (opacity <= 0.0)
290 return 1;
292 sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
293 hbrush = CreateSolidBrush( RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]) );
294 hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
296 SetPolyFillMode( hdc,
297 style->fill_rule.computed == 0 ? WINDING :
298 style->fill_rule.computed == 2 ? ALTERNATE : ALTERNATE );
299 } else { // if (!style)
300 hbrush = CreateSolidBrush( RGB(255, 255, 255) );
301 hbrushOld = (HBRUSH) SelectObject( hdc, hbrush );
302 SetPolyFillMode( hdc, ALTERNATE );
303 }
305 return 0;
306 }
309 void
310 PrintEmfWin32::destroy_brush()
311 {
312 SelectObject( hdc, hbrushOld );
313 if (hbrush)
314 DeleteObject( hbrush );
315 hbrush = NULL;
316 hbrushOld = NULL;
317 }
320 void
321 PrintEmfWin32::create_pen(SPStyle const *style, const Geom::Matrix &transform)
322 {
323 if (style) {
324 float rgb[3];
326 sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
328 LOGBRUSH lb;
329 ZeroMemory(&lb, sizeof(lb));
330 lb.lbStyle = BS_SOLID;
331 lb.lbColor = RGB( 255*rgb[0], 255*rgb[1], 255*rgb[2] );
333 int linestyle = PS_SOLID;
334 int linecap = 0;
335 int linejoin = 0;
336 DWORD n_dash = 0;
337 DWORD *dash = NULL;
339 using Geom::X;
340 using Geom::Y;
342 Geom::Point zero(0, 0);
343 Geom::Point one(1, 1);
344 Geom::Point p0(zero * transform);
345 Geom::Point p1(one * transform);
346 Geom::Point p(p1 - p0);
348 double scale = sqrt( (p[X]*p[X]) + (p[Y]*p[Y]) ) / sqrt(2);
350 DWORD linewidth = MAX( 1, (DWORD) (scale * style->stroke_width.computed * IN_PER_PX * dwDPI) );
352 if (style->stroke_linecap.computed == 0) {
353 linecap = PS_ENDCAP_FLAT;
354 }
355 else if (style->stroke_linecap.computed == 1) {
356 linecap = PS_ENDCAP_ROUND;
357 }
358 else if (style->stroke_linecap.computed == 2) {
359 linecap = PS_ENDCAP_SQUARE;
360 }
362 if (style->stroke_linejoin.computed == 0) {
363 linejoin = PS_JOIN_MITER;
364 }
365 else if (style->stroke_linejoin.computed == 1) {
366 linejoin = PS_JOIN_ROUND;
367 }
368 else if (style->stroke_linejoin.computed == 2) {
369 linejoin = PS_JOIN_BEVEL;
370 }
372 if (style->stroke_dash.n_dash &&
373 style->stroke_dash.dash )
374 {
375 int i = 0;
376 while (linestyle != PS_USERSTYLE &&
377 (i < style->stroke_dash.n_dash)) {
378 if (style->stroke_dash.dash[i] > 0.00000001)
379 linestyle = PS_USERSTYLE;
380 i++;
381 }
383 if (linestyle == PS_USERSTYLE) {
384 n_dash = style->stroke_dash.n_dash;
385 dash = new DWORD[n_dash];
386 for (i = 0; i < style->stroke_dash.n_dash; i++) {
387 dash[i] = (DWORD) (style->stroke_dash.dash[i] * IN_PER_PX * dwDPI);
388 }
389 }
390 }
392 hpen = ExtCreatePen(
393 PS_GEOMETRIC | linestyle | linecap | linejoin,
394 linewidth,
395 &lb,
396 n_dash,
397 dash );
399 if ( !hpen && linestyle == PS_USERSTYLE ) {
400 hpen = ExtCreatePen(
401 PS_GEOMETRIC | PS_SOLID | linecap | linejoin,
402 linewidth,
403 &lb,
404 0,
405 NULL );
406 }
408 if ( !hpen ) {
409 hpen = CreatePen(
410 PS_SOLID,
411 linewidth,
412 lb.lbColor );
413 }
415 hpenOld = (HPEN) SelectObject( hdc, hpen );
417 if (linejoin == PS_JOIN_MITER) {
418 float oldmiterlimit;
419 float miterlimit = style->stroke_miterlimit.value;
421 miterlimit = miterlimit * 10.0 / 4.0;
422 if (miterlimit < 1)
423 miterlimit = 10.0;
425 miterlimit = miterlimit * IN_PER_PX * dwDPI;
427 SetMiterLimit(
428 hdc,
429 miterlimit,
430 &oldmiterlimit );
431 }
433 if (n_dash) {
434 delete[] dash;
435 }
436 }
437 else { // if (!style)
438 hpen = CreatePen( PS_SOLID, 1, RGB(0, 0, 0) );
439 hpenOld = (HPEN) SelectObject( hdc, hpen );
440 }
441 }
444 void
445 PrintEmfWin32::destroy_pen()
446 {
447 SelectObject( hdc, hpenOld );
448 if (hpen)
449 DeleteObject( hpen );
450 hpen = NULL;
451 }
454 void
455 PrintEmfWin32::flush_fill()
456 {
457 if (!fill_pathv.empty()) {
458 stroke_and_fill = false;
459 fill_only = true;
460 print_pathv(fill_pathv, fill_transform);
461 fill_only = false;
462 if (!simple_shape)
463 FillPath( hdc );
464 destroy_brush();
465 fill_pathv.clear();
466 }
467 }
469 unsigned int
470 PrintEmfWin32::bind(Inkscape::Extension::Print * /*mod*/, Geom::Matrix const *transform, float /*opacity*/)
471 {
472 Geom::Matrix tr = *transform;
474 if (m_tr_stack.size()) {
475 Geom::Matrix tr_top = m_tr_stack.top();
476 m_tr_stack.push(tr * tr_top);
477 } else {
478 m_tr_stack.push(tr);
479 }
481 return 1;
482 }
484 unsigned int
485 PrintEmfWin32::release(Inkscape::Extension::Print * /*mod*/)
486 {
487 m_tr_stack.pop();
488 return 1;
489 }
491 unsigned int
492 PrintEmfWin32::fill(Inkscape::Extension::Print * /*mod*/,
493 Geom::PathVector const &pathv, Geom::Matrix const * /*transform*/, SPStyle const *style,
494 NRRect const * /*pbox*/, NRRect const * /*dbox*/, NRRect const * /*bbox*/)
495 {
496 if (!hdc) return 0;
498 Geom::Matrix tf = m_tr_stack.top();
500 flush_fill(); // flush any pending fills
502 if (style->fill.isColor()) {
503 if (create_brush(style))
504 return 0;
505 } else {
506 // create_brush(NULL);
507 return 0;
508 }
510 fill_pathv.clear();
511 std::copy(pathv.begin(), pathv.end(), std::back_inserter(fill_pathv));
512 fill_transform = tf;
514 // postpone fill in case of stroke-and-fill
516 return 0;
517 }
520 unsigned int
521 PrintEmfWin32::stroke (Inkscape::Extension::Print * /*mod*/,
522 Geom::PathVector const &pathv, const Geom::Matrix * /*transform*/, const SPStyle *style,
523 const NRRect * /*pbox*/, const NRRect * /*dbox*/, const NRRect * /*bbox*/)
524 {
525 if (!hdc) return 0;
527 Geom::Matrix tf = m_tr_stack.top();
529 stroke_and_fill = ( pathv == fill_pathv );
531 if (!stroke_and_fill) {
532 flush_fill(); // flush any pending fills
533 }
535 if (style->stroke.isColor()) {
536 create_pen(style, tf);
537 } else {
538 // create_pen(NULL, tf);
539 return 0;
540 }
542 print_pathv(pathv, tf);
544 if (stroke_and_fill) {
545 if (!simple_shape)
546 StrokeAndFillPath( hdc );
547 destroy_brush();
548 fill_pathv.clear();
549 } else {
550 if (!simple_shape)
551 StrokePath( hdc );
552 }
554 destroy_pen();
556 return 0;
557 }
560 bool
561 PrintEmfWin32::print_simple_shape(Geom::PathVector const &pathv, const Geom::Matrix &transform)
562 {
563 Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
565 int nodes = 0;
566 int moves = 0;
567 int lines = 0;
568 int curves = 0;
570 for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
571 {
572 moves++;
573 nodes++;
575 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
576 {
577 nodes++;
579 if ( is_straight_curve(*cit) ) {
580 lines++;
581 }
582 else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit)) {
583 cubic = cubic;
584 curves++;
585 }
586 }
587 }
589 if (!nodes)
590 return false;
592 POINT *lpPoints = new POINT[moves + lines + curves*3];
593 int i = 0;
595 /**
596 * For all Subpaths in the <path>
597 */
598 for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
599 {
600 using Geom::X;
601 using Geom::Y;
603 Geom::Point p0 = pit->initialPoint();
605 p0[X] = (p0[X] * IN_PER_PX * dwDPI);
606 p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
608 LONG const x0 = (LONG) round(p0[X]);
609 LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
611 lpPoints[i].x = x0;
612 lpPoints[i].y = y0;
613 i = i + 1;
615 /**
616 * For all segments in the subpath
617 */
618 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
619 {
620 if ( is_straight_curve(*cit) )
621 {
622 //Geom::Point p0 = cit->initialPoint();
623 Geom::Point p1 = cit->finalPoint();
625 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
626 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
627 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
628 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
630 //LONG const x0 = (LONG) round(p0[X]);
631 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
632 LONG const x1 = (LONG) round(p1[X]);
633 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
635 lpPoints[i].x = x1;
636 lpPoints[i].y = y1;
637 i = i + 1;
638 }
639 else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
640 {
641 std::vector<Geom::Point> points = cubic->points();
642 //Geom::Point p0 = points[0];
643 Geom::Point p1 = points[1];
644 Geom::Point p2 = points[2];
645 Geom::Point p3 = points[3];
647 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
648 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
649 p2[X] = (p2[X] * IN_PER_PX * dwDPI);
650 p3[X] = (p3[X] * IN_PER_PX * dwDPI);
651 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
652 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
653 p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
654 p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
656 //LONG const x0 = (LONG) round(p0[X]);
657 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
658 LONG const x1 = (LONG) round(p1[X]);
659 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
660 LONG const x2 = (LONG) round(p2[X]);
661 LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
662 LONG const x3 = (LONG) round(p3[X]);
663 LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
665 POINT pt[3];
666 pt[0].x = x1;
667 pt[0].y = y1;
668 pt[1].x = x2;
669 pt[1].y = y2;
670 pt[2].x = x3;
671 pt[2].y = y3;
673 lpPoints[i].x = x1;
674 lpPoints[i].y = y1;
675 lpPoints[i+1].x = x2;
676 lpPoints[i+1].y = y2;
677 lpPoints[i+2].x = x3;
678 lpPoints[i+2].y = y3;
679 i = i + 3;
680 }
681 }
682 }
684 bool done = false;
685 bool closed = (lpPoints[0].x == lpPoints[i-1].x) && (lpPoints[0].y == lpPoints[i-1].y);
686 bool polygon = false;
687 bool polyline = false;
688 bool rectangle = false;
689 bool ellipse = false;
691 if (moves == 1 && moves+lines == nodes && closed) {
692 polygon = true;
693 if (nodes==5) {
694 if (lpPoints[0].x == lpPoints[3].x && lpPoints[1].x == lpPoints[2].x &&
695 lpPoints[0].y == lpPoints[1].y && lpPoints[2].y == lpPoints[3].y)
696 {
697 rectangle = true;
698 }
699 }
700 }
701 else if (moves == 1 && moves+lines == nodes) {
702 polyline = true;
703 }
704 else if (moves == 1 && nodes == 5 && moves+curves == nodes && closed) {
705 if (lpPoints[0].x == lpPoints[1].x && lpPoints[1].x == lpPoints[11].x &&
706 lpPoints[5].x == lpPoints[6].x && lpPoints[6].x == lpPoints[7].x &&
707 lpPoints[2].x == lpPoints[10].x && lpPoints[3].x == lpPoints[9].x && lpPoints[4].x == lpPoints[8].x &&
708 lpPoints[2].y == lpPoints[3].y && lpPoints[3].y == lpPoints[4].y &&
709 lpPoints[8].y == lpPoints[9].y && lpPoints[9].y == lpPoints[10].y &&
710 lpPoints[5].y == lpPoints[1].y && lpPoints[6].y == lpPoints[0].y && lpPoints[7].y == lpPoints[11].y)
711 {
712 ellipse = true;
713 }
714 }
716 if (polygon || polyline || ellipse) {
717 HPEN hpenTmp = NULL;
718 HPEN hpenOld = NULL;
719 HBRUSH hbrushTmp = NULL;
720 HBRUSH hbrushOld = NULL;
722 if (!stroke_and_fill) {
723 if (fill_only) {
724 hpenTmp = (HPEN) GetStockObject(NULL_PEN);
725 hpenOld = (HPEN) SelectObject( hdc, hpenTmp );
726 }
727 else { // if (stroke_only)
728 hbrushTmp = (HBRUSH) GetStockObject(NULL_BRUSH);
729 hbrushOld = (HBRUSH) SelectObject( hdc, hbrushTmp );
730 }
731 }
733 if (polygon) {
734 if (rectangle)
735 Rectangle( hdc, lpPoints[0].x, lpPoints[0].y, lpPoints[2].x, lpPoints[2].y );
736 else
737 Polygon( hdc, lpPoints, nodes );
738 }
739 else if (polyline) {
740 Polyline( hdc, lpPoints, nodes );
741 }
742 else if (ellipse) {
743 Ellipse( hdc, lpPoints[6].x, lpPoints[3].y, lpPoints[0].x, lpPoints[9].y);
744 }
746 done = true;
748 if (hpenOld)
749 SelectObject( hdc, hpenOld );
750 if (hpenTmp)
751 DeleteObject( hpenTmp );
752 if (hbrushOld)
753 SelectObject( hdc, hbrushOld );
754 if (hbrushTmp)
755 DeleteObject( hbrushTmp );
756 }
758 delete[] lpPoints;
760 return done;
761 }
763 unsigned int
764 PrintEmfWin32::print_pathv(Geom::PathVector const &pathv, const Geom::Matrix &transform)
765 {
766 simple_shape = print_simple_shape(pathv, transform);
768 if (simple_shape)
769 return TRUE;
771 Geom::PathVector pv = pathv_to_linear_and_cubic_beziers( pathv * transform );
773 BeginPath( hdc );
775 /**
776 * For all Subpaths in the <path>
777 */
778 for (Geom::PathVector::const_iterator pit = pv.begin(); pit != pv.end(); ++pit)
779 {
780 using Geom::X;
781 using Geom::Y;
783 Geom::Point p0 = pit->initialPoint();
785 p0[X] = (p0[X] * IN_PER_PX * dwDPI);
786 p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
788 LONG const x0 = (LONG) round(p0[X]);
789 LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
791 MoveToEx( hdc, x0, y0, NULL );
793 /**
794 * For all segments in the subpath
795 */
796 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit)
797 {
798 if ( is_straight_curve(*cit) )
799 {
800 //Geom::Point p0 = cit->initialPoint();
801 Geom::Point p1 = cit->finalPoint();
803 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
804 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
805 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
806 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
808 //LONG const x0 = (LONG) round(p0[X]);
809 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
810 LONG const x1 = (LONG) round(p1[X]);
811 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
813 LineTo( hdc, x1, y1 );
814 }
815 else if (Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit))
816 {
817 std::vector<Geom::Point> points = cubic->points();
818 //Geom::Point p0 = points[0];
819 Geom::Point p1 = points[1];
820 Geom::Point p2 = points[2];
821 Geom::Point p3 = points[3];
823 //p0[X] = (p0[X] * IN_PER_PX * dwDPI);
824 p1[X] = (p1[X] * IN_PER_PX * dwDPI);
825 p2[X] = (p2[X] * IN_PER_PX * dwDPI);
826 p3[X] = (p3[X] * IN_PER_PX * dwDPI);
827 //p0[Y] = (p0[Y] * IN_PER_PX * dwDPI);
828 p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
829 p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
830 p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
832 //LONG const x0 = (LONG) round(p0[X]);
833 //LONG const y0 = (LONG) round(rc.bottom-p0[Y]);
834 LONG const x1 = (LONG) round(p1[X]);
835 LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
836 LONG const x2 = (LONG) round(p2[X]);
837 LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
838 LONG const x3 = (LONG) round(p3[X]);
839 LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
841 POINT pt[3];
842 pt[0].x = x1;
843 pt[0].y = y1;
844 pt[1].x = x2;
845 pt[1].y = y2;
846 pt[2].x = x3;
847 pt[2].y = y3;
849 PolyBezierTo( hdc, pt, 3 );
850 }
851 else
852 {
853 g_warning("logical error, because pathv_to_linear_and_cubic_beziers was used");
854 }
855 }
857 if (pit->end_default() == pit->end_closed()) {
858 CloseFigure( hdc );
859 }
860 }
862 EndPath( hdc );
864 return TRUE;
865 }
868 bool
869 PrintEmfWin32::textToPath(Inkscape::Extension::Print * ext)
870 {
871 return ext->get_param_bool("textToPath");
872 }
874 unsigned int
875 PrintEmfWin32::text(Inkscape::Extension::Print * /*mod*/, char const *text, Geom::Point p,
876 SPStyle const *const style)
877 {
878 if (!hdc) return 0;
880 HFONT hfont = NULL;
882 #ifdef USE_PANGO_WIN32
883 /*
884 font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value, font_style_to_pos(*style));
885 if (tf) {
886 LOGFONT *lf = pango_win32_font_logfont(tf->pFont);
887 tf->Unref();
888 hfont = CreateFontIndirect(lf);
889 g_free(lf);
890 }
891 */
892 #endif
894 if (!hfont) {
895 if (PrintWin32::is_os_wide()) {
896 LOGFONTW *lf = (LOGFONTW*)g_malloc(sizeof(LOGFONTW));
897 g_assert(lf != NULL);
899 lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
900 lf->lfWidth = 0;
901 lf->lfEscapement = 0;
902 lf->lfOrientation = 0;
903 lf->lfWeight =
904 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
905 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
906 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
907 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
908 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
909 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
910 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
911 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
912 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
913 FW_NORMAL;
914 lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
915 lf->lfUnderline = style->text_decoration.underline;
916 lf->lfStrikeOut = style->text_decoration.line_through;
917 lf->lfCharSet = DEFAULT_CHARSET;
918 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
919 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
920 lf->lfQuality = DEFAULT_QUALITY;
921 lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
923 gunichar2 *unicode_name = g_utf8_to_utf16( style->text->font_family.value, -1, NULL, NULL, NULL );
924 wcsncpy(lf->lfFaceName, (wchar_t*) unicode_name, LF_FACESIZE-1);
925 g_free(unicode_name);
927 hfont = CreateFontIndirectW(lf);
929 g_free(lf);
930 }
931 else {
932 LOGFONTA *lf = (LOGFONTA*)g_malloc(sizeof(LOGFONTA));
933 g_assert(lf != NULL);
935 lf->lfHeight = style->font_size.computed * IN_PER_PX * dwDPI;
936 lf->lfWidth = 0;
937 lf->lfEscapement = 0;
938 lf->lfOrientation = 0;
939 lf->lfWeight =
940 style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
941 style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
942 style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
943 style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
944 style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
945 style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
946 style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
947 style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
948 style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
949 FW_NORMAL;
950 lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
951 lf->lfUnderline = style->text_decoration.underline;
952 lf->lfStrikeOut = style->text_decoration.line_through;
953 lf->lfCharSet = DEFAULT_CHARSET;
954 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
955 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
956 lf->lfQuality = DEFAULT_QUALITY;
957 lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
959 strncpy(lf->lfFaceName, (char*) style->text->font_family.value, LF_FACESIZE-1);
961 hfont = CreateFontIndirectA(lf);
963 g_free(lf);
964 }
965 }
967 HFONT hfontOld = (HFONT) SelectObject(hdc, hfont);
969 float rgb[3];
970 sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
971 SetTextColor(hdc, RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]));
973 // Text alignment:
974 // - (x,y) coordinates received by this filter are those of the point where the text
975 // actually starts, and already takes into account the text object's alignment;
976 // - for this reason, the EMF text alignment must always be TA_BASELINE|TA_LEFT.
977 SetTextAlign(hdc, TA_BASELINE | TA_LEFT);
979 // Transparent text background
980 SetBkMode(hdc, TRANSPARENT);
982 Geom::Matrix tf = m_tr_stack.top();
984 p = p * tf;
985 p[Geom::X] = (p[Geom::X] * IN_PER_PX * dwDPI);
986 p[Geom::Y] = (p[Geom::Y] * IN_PER_PX * dwDPI);
988 LONG const xpos = (LONG) round(p[Geom::X]);
989 LONG const ypos = (LONG) round(rc.bottom-p[Geom::Y]);
991 if (PrintWin32::is_os_wide()) {
992 gunichar2 *unicode_text = g_utf8_to_utf16( text, -1, NULL, NULL, NULL );
993 TextOutW(hdc, xpos, ypos, (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
994 }
995 else {
996 TextOutA(hdc, xpos, ypos, (CHAR*)text, strlen((char*)text));
997 }
999 SelectObject(hdc, hfontOld);
1000 DeleteObject(hfont);
1002 return 0;
1003 }
1005 void
1006 PrintEmfWin32::init (void)
1007 {
1008 Inkscape::Extension::Extension * ext;
1010 /* EMF print */
1011 ext = Inkscape::Extension::build_from_mem(
1012 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
1013 "<name>Enhanced Metafile Print</name>\n"
1014 "<id>org.inkscape.print.emf.win32</id>\n"
1015 "<param name=\"destination\" type=\"string\"></param>\n"
1016 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
1017 "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
1018 "<print/>\n"
1019 "</inkscape-extension>", new PrintEmfWin32());
1021 return;
1022 }
1025 } /* namespace Internal */
1026 } /* namespace Extension */
1027 } /* namespace Inkscape */
1029 #endif /* WIN32 */
1031 /*
1032 Local Variables:
1033 mode:c++
1034 c-file-style:"stroustrup"
1035 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1036 indent-tabs-mode:nil
1037 fill-column:99
1038 End:
1039 */
1040 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :