diff --git a/src/extension/internal/emf-win32-print.cpp b/src/extension/internal/emf-win32-print.cpp
index 3d18e73b4113b702441a062a49209a3ba0b1ada1..6e0a7bf0b6e13bd59de1e24c36813617ee62ad47 100644 (file)
* Authors:
* Ulf Erikson <ulferikson@users.sf.net>
*
- * Copyright (C) 2006 Authors
+ * Copyright (C) 2006-2008 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "libnr/nr-point-matrix-ops.h"
#include "libnr/nr-rect.h"
#include "libnr/nr-matrix.h"
+#include "libnr/nr-matrix-ops.h"
+#include "libnr/nr-matrix-scale-ops.h"
+#include "libnr/nr-matrix-translate-ops.h"
+#include "libnr/nr-scale-translate-ops.h"
+#include "libnr/nr-translate-scale-ops.h"
#include "libnr/nr-matrix-fns.h"
#include "libnr/nr-path.h"
#include "libnr/nr-pixblock.h"
+#include <libnr/n-art-bpath-2geom.h>
#include "display/canvas-bpath.h"
#include "sp-item.h"
hbrush(NULL),
hbrushOld(NULL),
hpen(NULL),
- fill_path(NULL)
+ fill_path(NULL),
+ stroke_and_fill(false),
+ fill_only(false),
+ simple_shape(false)
{
}
#if !defined(_WIN32) && !defined(__WIN32__)
(void) signal(SIGPIPE, SIG_DFL);
#endif
- return;
+ return;
}
HDC hScreenDC = GetDC( NULL );
// Get the physical characteristics of the reference DC
- float PixelsX = (float) GetDeviceCaps( hScreenDC, HORZRES );
- float PixelsY = (float) GetDeviceCaps( hScreenDC, VERTRES );
- float MMX = (float) GetDeviceCaps( hScreenDC, HORZSIZE );
- float MMY = (float) GetDeviceCaps( hScreenDC, VERTSIZE );
-
- // Create the Metafile
- if (PrintWin32::is_os_wide())
- hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, NULL );
+ int PixelsX = GetDeviceCaps( hScreenDC, HORZRES );
+ int PixelsY = GetDeviceCaps( hScreenDC, VERTRES );
+ int MMX = GetDeviceCaps( hScreenDC, HORZSIZE );
+ int MMY = GetDeviceCaps( hScreenDC, VERTSIZE );
+
+ CHAR buff[1024];
+ ZeroMemory(buff, sizeof(buff));
+ snprintf(buff, sizeof(buff)-1, "Inkscape %s (%s)", INKSCAPE_VERSION, __DATE__);
+ INT len = strlen(buff);
+ CHAR *p1 = strrchr(ansi_uri, '\\');
+ CHAR *p2 = strrchr(ansi_uri, '/');
+ CHAR *p = MAX(p1, p2);
+ if (p)
+ p++;
else
- hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, NULL );
+ p = ansi_uri;
+ snprintf(buff+len+1, sizeof(buff)-len-2, "%s", p);
+
+ // Create the Metafile
+ if (PrintWin32::is_os_wide()) {
+ WCHAR wbuff[1024];
+ ZeroMemory(wbuff, sizeof(wbuff));
+ MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buff, sizeof(buff)/sizeof(buff[0]), wbuff, sizeof(wbuff)/sizeof(wbuff[0]));
+ hdc = CreateEnhMetaFileW( hScreenDC, unicode_uri, &rc, wbuff );
+ }
+ else {
+ hdc = CreateEnhMetaFileA( hScreenDC, ansi_uri, &rc, buff );
+ }
// Release the reference DC
ReleaseDC( NULL, hScreenDC );
SetMapMode( hdc, MM_ANISOTROPIC );
// Set the Windows extent
- SetWindowExtEx( hdc, (int) (dwInchesX*dwDPI), (int) (dwInchesY*dwDPI), NULL );
+ int windowextX = (int) ceil(dwInchesX*dwDPI);
+ int windowextY = (int) ceil(dwInchesY*dwDPI);
+ SetWindowExtEx( hdc, windowextX, windowextY, NULL );
// Set the viewport extent to reflect
// dwInchesX" x dwInchesY" in device units
- SetViewportExtEx( hdc,
- (int) ((float) dwInchesX*25.4f*PixelsX/MMX),
- (int) ((float) dwInchesY*25.4f*PixelsY/MMY),
- NULL );
+ int viewportextX = (int)((float)dwInchesX*25.4f*(float)PixelsX/(float)MMX);
+ int viewportextY = (int)((float)dwInchesY*25.4f*(float)PixelsY/(float)MMY);
+ SetViewportExtEx( hdc, viewportextX, viewportextY, NULL );
+
+ if (1) {
+ snprintf(buff, sizeof(buff)-1, "Screen=%dx%dpx, %dx%dmm", PixelsX, PixelsY, MMX, MMY);
+ GdiComment(hdc, strlen(buff), (BYTE*) buff);
+
+ snprintf(buff, sizeof(buff)-1, "Drawing=%.1lfx%.1lfpx, %.1lfx%.1lfmm", _width, _height, dwInchesX * MM_PER_IN, dwInchesY * MM_PER_IN);
+ GdiComment(hdc, strlen(buff), (BYTE*) buff);
+ }
SetRect( &rc, 0, 0, (int) ceil(dwInchesX*dwDPI), (int) ceil(dwInchesY*dwDPI) );
g_free(local_fn);
g_free(unicode_fn);
+ m_tr_stack.push( NR::scale(1, -1) * NR::translate(0, sp_document_height(doc)));
+
return 0;
}
PrintEmfWin32::flush_fill()
{
if (fill_path) {
+ stroke_and_fill = false;
+ fill_only = true;
print_bpath(fill_path, &fill_transform, &fill_pbox);
- FillPath( hdc );
+ fill_only = false;
+ if (!simple_shape)
+ FillPath( hdc );
destroy_brush();
delete[] fill_path;
fill_path = NULL;
unsigned int
PrintEmfWin32::bind(Inkscape::Extension::Print *mod, NR::Matrix const *transform, float opacity)
{
- text_transform = *transform;
- return 0;
+ NR::Matrix tr = *transform;
+
+ if (m_tr_stack.size()) {
+ NR::Matrix tr_top = m_tr_stack.top();
+ m_tr_stack.push(tr * tr_top);
+ } else {
+ m_tr_stack.push(tr);
+ }
+
+ return 1;
}
unsigned int
PrintEmfWin32::release(Inkscape::Extension::Print *mod)
{
- return 0;
+ m_tr_stack.pop();
+ return 1;
}
unsigned int
PrintEmfWin32::fill(Inkscape::Extension::Print *mod,
- NRBPath const *bpath, NR::Matrix const *transform, SPStyle const *style,
+ Geom::PathVector const &pathv, NR::Matrix const *transform, SPStyle const *style,
NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
{
if (!hdc) return 0;
+ NR::Matrix tf = m_tr_stack.top();
+
flush_fill(); // flush any pending fills
if (style->fill.isColor()) {
return 0;
}
- fill_path = copy_bpath( bpath->path );
- fill_transform = *transform;
+ NArtBpath * bpath = BPath_from_2GeomPath(pathv);
+ fill_path = copy_bpath( bpath );
+ g_free(bpath);
+ fill_transform = tf;
fill_pbox = *pbox;
// postpone fill in case of stroke-and-fill
unsigned int
PrintEmfWin32::stroke (Inkscape::Extension::Print *mod,
- const NRBPath *bpath, const NR::Matrix *transform, const SPStyle *style,
+ Geom::PathVector const &pathv, const NR::Matrix *transform, const SPStyle *style,
const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
{
if (!hdc) return 0;
- bool stroke_and_fill = ( cmp_bpath( bpath->path, fill_path ) == 0 );
+ NR::Matrix tf = m_tr_stack.top();
+
+ NArtBpath * bpath = BPath_from_2GeomPath(pathv);
+
+ stroke_and_fill = ( cmp_bpath( bpath, fill_path ) == 0 );
if (!stroke_and_fill) {
flush_fill(); // flush any pending fills
}
if (style->stroke.isColor()) {
- create_pen(style, transform);
+ create_pen(style, &tf);
} else {
- // create_pen(NULL, transform);
+ // create_pen(NULL, &tf);
return 0;
}
- print_bpath(bpath->path, transform, pbox);
+ print_bpath(bpath, &tf, pbox);
if (stroke_and_fill) {
- StrokeAndFillPath( hdc );
+ if (!simple_shape)
+ StrokeAndFillPath( hdc );
destroy_brush();
delete[] fill_path;
fill_path = NULL;
} else {
- StrokePath( hdc );
+ if (!simple_shape)
+ StrokePath( hdc );
}
+ g_free(bpath);
destroy_pen();
return 0;
}
+bool
+PrintEmfWin32::print_simple_shape(const NArtBpath *bpath, const NR::Matrix *transform, NRRect const *pbox)
+{
+ NR::Matrix tf = *transform;
+ const NArtBpath *bp = bpath;
+
+ int nodes = 0;
+ int moves = 0;
+ int lines = 0;
+ int curves = 0;
+
+ while (bp->code != NR_END) {
+ nodes++;
+ switch (bp->code) {
+ case NR_MOVETO:
+ case NR_MOVETO_OPEN:
+ moves++;
+ break;
+ case NR_LINETO:
+ lines++;
+ break;
+ case NR_CURVETO:
+ curves++;
+ break;
+ }
+ bp += 1;
+ }
+
+ if (!nodes)
+ return false;
+
+ POINT *lpPoints = new POINT[moves + lines + curves*3];
+ int i = 0;
+ bp = bpath;
+ while (bp->code != NR_END)
+ {
+ using NR::X;
+ using NR::Y;
+
+ NR::Point p1(bp->c(1) * tf);
+ NR::Point p2(bp->c(2) * tf);
+ NR::Point p3(bp->c(3) * tf);
+
+ p1[X] = (p1[X] * IN_PER_PX * dwDPI);
+ p2[X] = (p2[X] * IN_PER_PX * dwDPI);
+ p3[X] = (p3[X] * IN_PER_PX * dwDPI);
+ p1[Y] = (p1[Y] * IN_PER_PX * dwDPI);
+ p2[Y] = (p2[Y] * IN_PER_PX * dwDPI);
+ p3[Y] = (p3[Y] * IN_PER_PX * dwDPI);
+
+ LONG const x1 = (LONG) round(p1[X]);
+ LONG const y1 = (LONG) round(rc.bottom-p1[Y]);
+ LONG const x2 = (LONG) round(p2[X]);
+ LONG const y2 = (LONG) round(rc.bottom-p2[Y]);
+ LONG const x3 = (LONG) round(p3[X]);
+ LONG const y3 = (LONG) round(rc.bottom-p3[Y]);
+
+ switch (bp->code) {
+ case NR_MOVETO:
+ case NR_MOVETO_OPEN:
+ case NR_LINETO:
+ lpPoints[i].x = x3;
+ lpPoints[i].y = y3;
+ i = i + 1;
+ break;
+ case NR_CURVETO:
+ lpPoints[i].x = x1;
+ lpPoints[i].y = y1;
+ lpPoints[i+1].x = x2;
+ lpPoints[i+1].y = y2;
+ lpPoints[i+2].x = x3;
+ lpPoints[i+2].y = y3;
+ i = i + 3;
+ break;
+ }
+
+ bp += 1;
+ }
+
+ bool done = false;
+ bool closed = (lpPoints[0].x == lpPoints[i-1].x) && (lpPoints[0].y == lpPoints[i-1].y);
+ bool polygon = false;
+ bool polyline = false;
+ bool rectangle = false;
+ bool ellipse = false;
+
+ if (moves == 1 && moves+lines == nodes && closed) {
+ polygon = true;
+ if (nodes==5) {
+ if (lpPoints[0].x == lpPoints[3].x && lpPoints[1].x == lpPoints[2].x &&
+ lpPoints[0].y == lpPoints[1].y && lpPoints[2].y == lpPoints[3].y)
+ {
+ rectangle = true;
+ }
+ }
+ }
+ else if (moves == 1 && moves+lines == nodes) {
+ polyline = true;
+ }
+ else if (moves == 1 && nodes == 5 && moves+curves == nodes && closed) {
+ if (lpPoints[0].x == lpPoints[1].x && lpPoints[1].x == lpPoints[11].x &&
+ lpPoints[5].x == lpPoints[6].x && lpPoints[6].x == lpPoints[7].x &&
+ lpPoints[2].x == lpPoints[10].x && lpPoints[3].x == lpPoints[9].x && lpPoints[4].x == lpPoints[8].x &&
+ lpPoints[2].y == lpPoints[3].y && lpPoints[3].y == lpPoints[4].y &&
+ lpPoints[8].y == lpPoints[9].y && lpPoints[9].y == lpPoints[10].y &&
+ lpPoints[5].y == lpPoints[1].y && lpPoints[6].y == lpPoints[0].y && lpPoints[7].y == lpPoints[11].y)
+ {
+ ellipse = true;
+ }
+ }
+
+ if (polygon || polyline || ellipse) {
+ HPEN hpenTmp = NULL;
+ HPEN hpenOld = NULL;
+ HBRUSH hbrushTmp = NULL;
+ HBRUSH hbrushOld = NULL;
+
+ if (!stroke_and_fill) {
+ if (fill_only) {
+ hpenTmp = (HPEN) GetStockObject(NULL_PEN);
+ hpenOld = (HPEN) SelectObject( hdc, hpenTmp );
+ }
+ else { // if (stroke_only)
+ hbrushTmp = (HBRUSH) GetStockObject(NULL_BRUSH);
+ hbrushOld = (HBRUSH) SelectObject( hdc, hbrushTmp );
+ }
+ }
+
+ if (polygon) {
+ if (rectangle)
+ Rectangle( hdc, lpPoints[0].x, lpPoints[0].y, lpPoints[2].x, lpPoints[2].y );
+ else
+ Polygon( hdc, lpPoints, nodes );
+ }
+ else if (polyline) {
+ Polyline( hdc, lpPoints, nodes );
+ }
+ else if (ellipse) {
+ Ellipse( hdc, lpPoints[6].x, lpPoints[3].y, lpPoints[0].x, lpPoints[9].y);
+ }
+
+ done = true;
+
+ if (hpenOld)
+ SelectObject( hdc, hpenOld );
+ if (hpenTmp)
+ DeleteObject( hpenTmp );
+ if (hbrushOld)
+ SelectObject( hdc, hbrushOld );
+ if (hbrushTmp)
+ DeleteObject( hbrushTmp );
+ }
+
+ delete[] lpPoints;
+
+ return done;
+}
+
unsigned int
PrintEmfWin32::print_bpath(const NArtBpath *bp, const NR::Matrix *transform, NRRect const *pbox)
{
unsigned int closed;
NR::Matrix tf = *transform;
+ simple_shape = print_simple_shape(bp, &tf, pbox);
+
+ if (simple_shape)
+ return TRUE;
+
BeginPath( hdc );
closed = FALSE;
while (bp->code != NR_END) {
@@ -658,21 +883,17 @@ PrintEmfWin32::text(Inkscape::Extension::Print *mod, char const *text, NR::Point
lf->lfEscapement = 0;
lf->lfOrientation = 0;
lf->lfWeight =
- style->font_weight.value == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL ? FW_NORMAL :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD ? FW_BOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER ? FW_EXTRALIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER ? FW_EXTRABOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
FW_NORMAL;
- lf->lfItalic = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
+ lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
lf->lfUnderline = style->text_decoration.underline;
lf->lfStrikeOut = style->text_decoration.line_through;
lf->lfCharSet = DEFAULT_CHARSET;
@@ -698,21 +919,17 @@ PrintEmfWin32::text(Inkscape::Extension::Print *mod, char const *text, NR::Point
lf->lfEscapement = 0;
lf->lfOrientation = 0;
lf->lfWeight =
- style->font_weight.value == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL ? FW_NORMAL :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLD ? FW_BOLD :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_LIGHTER ? FW_EXTRALIGHT :
- style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER ? FW_EXTRABOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_100 ? FW_THIN :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_200 ? FW_EXTRALIGHT :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_300 ? FW_LIGHT :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_400 ? FW_NORMAL :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_500 ? FW_MEDIUM :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_600 ? FW_SEMIBOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_700 ? FW_BOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_800 ? FW_EXTRABOLD :
+ style->font_weight.computed == SP_CSS_FONT_WEIGHT_900 ? FW_HEAVY :
FW_NORMAL;
- lf->lfItalic = (style->font_style.value == SP_CSS_FONT_STYLE_ITALIC);
+ lf->lfItalic = (style->font_style.computed == SP_CSS_FONT_STYLE_ITALIC);
lf->lfUnderline = style->text_decoration.underline;
lf->lfStrikeOut = style->text_decoration.line_through;
lf->lfCharSet = DEFAULT_CHARSET;
@@ -728,37 +945,42 @@ PrintEmfWin32::text(Inkscape::Extension::Print *mod, char const *text, NR::Point
g_free(lf);
}
}
-
+
HFONT hfontOld = (HFONT) SelectObject(hdc, hfont);
float rgb[3];
sp_color_get_rgb_floatv( &style->fill.value.color, rgb );
SetTextColor(hdc, RGB(255*rgb[0], 255*rgb[1], 255*rgb[2]));
- int align =
- style->text_align.value == SP_CSS_TEXT_ALIGN_RIGHT ? TA_RIGHT :
- style->text_align.value == SP_CSS_TEXT_ALIGN_CENTER ? TA_CENTER : TA_LEFT;
- SetTextAlign(hdc, TA_BASELINE | align);
+ // Text alignment:
+ // - (x,y) coordinates received by this filter are those of the point where the text
+ // actually starts, and already takes into account the text object's alignment;
+ // - for this reason, the EMF text alignment must always be TA_BASELINE|TA_LEFT.
+ SetTextAlign(hdc, TA_BASELINE | TA_LEFT);
+
+ // Transparent text background
+ SetBkMode(hdc, TRANSPARENT);
- p = p * text_transform;
+ NR::Matrix tf = m_tr_stack.top();
+
+ p = p * tf;
p[NR::X] = (p[NR::X] * IN_PER_PX * dwDPI);
p[NR::Y] = (p[NR::Y] * IN_PER_PX * dwDPI);
+ LONG const xpos = (LONG) round(p[NR::X]);
+ LONG const ypos = (LONG) round(rc.bottom-p[NR::Y]);
+
if (PrintWin32::is_os_wide()) {
gunichar2 *unicode_text = g_utf8_to_utf16( text, -1, NULL, NULL, NULL );
- TextOutW(hdc, p[NR::X], p[NR::Y], (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
+ TextOutW(hdc, xpos, ypos, (WCHAR*)unicode_text, wcslen((wchar_t*)unicode_text));
}
else {
- TextOutA(hdc, p[NR::X], p[NR::Y], (CHAR*)text, strlen((char*)text));
+ TextOutA(hdc, xpos, ypos, (CHAR*)text, strlen((char*)text));
}
SelectObject(hdc, hfontOld);
DeleteObject(hfont);
- return 0;
-
-
-
return 0;
}
"<name>Enhanced Metafile Print</name>\n"
"<id>org.inkscape.print.emf.win32</id>\n"
"<param name=\"destination\" type=\"string\"></param>\n"
- "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
- "<param name=\"pageBoundingBox\" type=\"boolean\">TRUE</param>\n"
+ "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
+ "<param name=\"pageBoundingBox\" type=\"boolean\">true</param>\n"
"<print/>\n"
"</inkscape-extension>", new PrintEmfWin32());