1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
3 * Copyright © 2004,2006 Red Hat, Inc.
4 * Copyright © 2007, Adrian Johnson
6 * Permission to use, copy, modify, distribute, and sell this software
7 * and its documentation for any purpose is hereby granted without
8 * fee, provided that the above copyright notice appear in all copies
9 * and that both that copyright notice and this permission notice
10 * appear in supporting documentation, and that the name of
11 * Red Hat, Inc. not be used in advertising or publicity pertaining to
12 * distribution of the software without specific, written prior
13 * permission. Red Hat, Inc. makes no representations about the
14 * suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
17 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
20 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
23 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Authors: Carl D. Worth <cworth@cworth.org>
26 * Adrian Johnson <ajohnson@redneon.com>
29 #include "cairo-boilerplate-private.h"
30 #include "cairo-malloc-private.h"
32 #if CAIRO_CAN_TEST_WIN32_PRINTING_SURFACE
34 #include <cairo-win32.h>
35 #include <cairo-paginated-surface-private.h>
39 #if !defined(POSTSCRIPT_IDENTIFY)
40 # define POSTSCRIPT_IDENTIFY 0x1015
43 #if !defined(PSIDENT_GDICENTRIC)
44 # define PSIDENT_GDICENTRIC 0x0000
47 #if !defined(GET_PS_FEATURESETTING)
48 # define GET_PS_FEATURESETTING 0x1019
51 #if !defined(FEATURESETTING_PSLEVEL)
52 # define FEATURESETTING_PSLEVEL 0x0002
56 _cairo_win32_print_gdi_error (const char *context
)
59 DWORD last_error
= GetLastError ();
61 if (!FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
62 FORMAT_MESSAGE_FROM_SYSTEM
,
65 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
68 fprintf (stderr
, "%s: Unknown GDI error", context
);
70 fprintf (stderr
, "%s: %S", context
, (wchar_t *)lpMsgBuf
);
77 /* We should switch off of last_status, but we'd either return
78 * CAIRO_STATUS_NO_MEMORY or CAIRO_STATUS_UNKNOWN_ERROR and there
79 * is no CAIRO_STATUS_UNKNOWN_ERROR.
81 return CAIRO_STATUS_NO_MEMORY
;
84 static cairo_user_data_key_t win32_closure_key
;
86 typedef struct _win32_target_closure
{
90 cairo_surface_t
*target
;
94 } win32_target_closure_t
;
97 printer_is_postscript_level_3 (HDC dc
)
100 INT ps_feature
, ps_level
;
102 word
= PSIDENT_GDICENTRIC
;
103 if (ExtEscape (dc
, POSTSCRIPT_IDENTIFY
, sizeof(DWORD
), (char *)&word
, 0, (char *)NULL
) <= 0)
106 ps_feature
= FEATURESETTING_PSLEVEL
;
107 if (ExtEscape (dc
, GET_PS_FEATURESETTING
, sizeof(INT
),
108 (char *)&ps_feature
, sizeof(INT
), (char *)&ps_level
) <= 0)
118 create_printer_dc (win32_target_closure_t
*ptc
)
122 int x_dpi
, y_dpi
, left_margin
, top_margin
, page_height
, printable_height
;
126 GetDefaultPrinter (NULL
, &size
);
127 printer_name
= _cairo_malloc (size
);
129 if (printer_name
== NULL
)
132 if (GetDefaultPrinter (printer_name
, &size
) == 0) {
137 /* printf("\nPrinting to : %s\n", printer_name); */
138 ptc
->dc
= CreateDC (NULL
, printer_name
, NULL
, NULL
);
141 if (!printer_is_postscript_level_3 (ptc
->dc
)) {
142 printf("The default printer driver must be a color PostScript level 3 printer\n");
147 /* The printer device units on win32 are 1 unit == 1 dot and the
148 * origin is the start of the printable area. We transform the
149 * coordinate space to 1 unit is 1 point as expected by the
150 * tests. As the page size is larger than the test surface, the
151 * origin is translated down so that the each test is drawn at the
152 * bottom left corner of the page. This is because the bottom left
153 * corner of the PNG image that ghostscript creates is positioned
154 * at origin of the PS coordinates (ie the bottom left of the
155 * page). The left and bottom margins are stored in
156 * win32_target_closure as size of the PNG image needs to be
157 * increased because the test output is offset from the bottom
158 * left by the non printable margins. After the PNG is created the
159 * margins will be chopped off so the image matches the reference
162 printable_height
= GetDeviceCaps (ptc
->dc
, VERTRES
);
163 x_dpi
= GetDeviceCaps (ptc
->dc
, LOGPIXELSX
);
164 y_dpi
= GetDeviceCaps (ptc
->dc
, LOGPIXELSY
);
165 left_margin
= GetDeviceCaps (ptc
->dc
, PHYSICALOFFSETX
);
166 top_margin
= GetDeviceCaps (ptc
->dc
, PHYSICALOFFSETY
);
167 page_height
= GetDeviceCaps (ptc
->dc
, PHYSICALHEIGHT
);
169 SetGraphicsMode (ptc
->dc
, GM_ADVANCED
);
170 xform
.eM11
= x_dpi
/72.0;
173 xform
.eM22
= y_dpi
/72.0;
175 xform
.eDy
= printable_height
- ptc
->height
*y_dpi
/72.0;
176 if (!SetWorldTransform (ptc
->dc
, &xform
)) {
177 _cairo_win32_print_gdi_error ("cairo-boilerplate-win32-printing:SetWorldTransform");
181 ptc
->left_margin
= 72.0*left_margin
/x_dpi
;
182 ptc
->bottom_margin
= 72.0*(page_height
- printable_height
- top_margin
)/y_dpi
;
185 static cairo_surface_t
*
186 _cairo_boilerplate_win32_printing_create_surface (const char *name
,
187 cairo_content_t content
,
192 cairo_boilerplate_mode_t mode
,
195 win32_target_closure_t
*ptc
;
196 cairo_surface_t
*surface
;
199 if (content
== CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED
)
200 content
= CAIRO_CONTENT_COLOR_ALPHA
;
202 *closure
= ptc
= xmalloc (sizeof (win32_target_closure_t
));
204 xasprintf (&ptc
->filename
, "%s.out.ps", name
);
205 xunlink (ptc
->filename
);
207 memset (&di
, 0, sizeof (DOCINFO
));
208 di
.cbSize
= sizeof (DOCINFO
);
209 di
.lpszDocName
= ptc
->filename
;
210 di
.lpszOutput
= ptc
->filename
;
213 ptc
->height
= height
;
215 create_printer_dc (ptc
);
216 if (ptc
->dc
== NULL
) {
217 printf("\nFailed to create printer\n");
218 free (ptc
->filename
);
222 StartDoc (ptc
->dc
, &di
);
224 surface
= cairo_win32_printing_surface_create (ptc
->dc
);
225 if (cairo_surface_status (surface
)) {
226 free (ptc
->filename
);
230 cairo_surface_set_fallback_resolution (surface
, 72., 72.);
232 if (content
== CAIRO_CONTENT_COLOR
) {
233 ptc
->target
= surface
;
234 surface
= cairo_surface_create_similar (ptc
->target
,
241 if (cairo_surface_set_user_data (surface
,
244 NULL
) != CAIRO_STATUS_SUCCESS
) {
245 cairo_surface_destroy (surface
);
246 if (ptc
->target
!= NULL
)
247 cairo_surface_destroy (ptc
->target
);
248 free (ptc
->filename
);
256 static cairo_status_t
257 _cairo_boilerplate_win32_printing_surface_write_to_png (cairo_surface_t
*surface
,
258 const char *filename
)
260 win32_target_closure_t
*ptc
= cairo_surface_get_user_data (surface
, &win32_closure_key
);
262 cairo_surface_t
*src_image
, *dst_image
;
264 cairo_status_t status
;
267 /* Both surface and ptc->target were originally created at the
268 * same dimensions. We want a 1:1 copy here, so we first clear any
269 * device offset and scale on surface.
271 * In a more realistic use case of device offsets, the target of
272 * this copying would be of a different size than the source, and
273 * the offset would be desirable during the copy operation. */
274 double x_offset
, y_offset
;
275 double x_scale
, y_scale
;
276 cairo_surface_get_device_offset (surface
, &x_offset
, &y_offset
);
277 cairo_surface_get_device_scale (surface
, &x_scale
, &y_scale
);
278 cairo_surface_set_device_offset (surface
, 0, 0);
279 cairo_surface_set_device_scale (surface
, 1, 1);
281 cr
= cairo_create (ptc
->target
);
282 cairo_set_source_surface (cr
, surface
, 0, 0);
284 cairo_show_page (cr
);
286 cairo_surface_set_device_offset (surface
, x_offset
, y_offset
);
287 cairo_surface_set_device_scale (surface
, x_scale
, y_scale
);
289 cairo_surface_finish (surface
);
290 surface
= ptc
->target
;
293 cairo_surface_finish (surface
);
296 sprintf (command
, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s",
297 ptc
->width
+ ptc
->left_margin
, ptc
->height
+ ptc
->bottom_margin
, filename
, ptc
->filename
);
299 if (system (command
) != 0)
300 return CAIRO_STATUS_WRITE_ERROR
;
302 /* Create a new image from the ghostscript image that has the
303 * left and bottom margins removed */
305 src_image
= cairo_image_surface_create_from_png (filename
);
306 status
= cairo_surface_status (src_image
);
310 dst_image
= cairo_image_surface_create (CAIRO_FORMAT_RGB24
,
313 cr
= cairo_create (dst_image
);
314 cairo_set_operator (cr
, CAIRO_OPERATOR_SOURCE
);
315 cairo_set_source_surface (cr
, src_image
, -ptc
->left_margin
, 0);
319 cairo_surface_write_to_png (dst_image
, filename
);
320 status
= cairo_surface_status (dst_image
);
324 cairo_surface_destroy (src_image
);
325 cairo_surface_destroy (dst_image
);
327 return CAIRO_STATUS_SUCCESS
;
330 static cairo_surface_t
*
331 _cairo_boilerplate_win32_printing_get_image_surface (cairo_surface_t
*surface
,
336 win32_target_closure_t
*ptc
= cairo_surface_get_user_data (surface
,
339 cairo_status_t status
;
341 /* XXX test paginated interface */
343 return cairo_boilerplate_surface_create_in_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH
);
345 xasprintf (&filename
, "%s.png", ptc
->filename
);
346 status
= _cairo_boilerplate_win32_printing_surface_write_to_png (surface
, filename
);
348 return cairo_boilerplate_surface_create_in_error (status
);
350 surface
= cairo_boilerplate_get_image_surface_from_png (filename
,
353 ptc
->target
== NULL
);
362 _cairo_boilerplate_win32_printing_cleanup (void *closure
)
364 win32_target_closure_t
*ptc
= closure
;
367 cairo_surface_destroy (ptc
->target
);
368 free (ptc
->filename
);
373 static const cairo_boilerplate_target_t targets
[] = {
375 "win32-printing", "win32", ".ps", NULL
,
376 CAIRO_SURFACE_TYPE_WIN32_PRINTING
,
377 CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED
, 0,
378 "cairo_win32_printing_surface_create",
379 _cairo_boilerplate_win32_printing_create_surface
,
380 cairo_surface_create_similar
,
382 _cairo_boilerplate_win32_printing_get_image_surface
,
383 _cairo_boilerplate_win32_printing_surface_write_to_png
,
384 _cairo_boilerplate_win32_printing_cleanup
,
385 NULL
, NULL
, FALSE
, TRUE
, TRUE
388 "win32-printing", "win32", ".ps", NULL
,
389 CAIRO_SURFACE_TYPE_RECORDING
, CAIRO_CONTENT_COLOR
, 0,
390 "cairo_win32_printing_surface_create",
391 _cairo_boilerplate_win32_printing_create_surface
,
392 cairo_surface_create_similar
,
394 _cairo_boilerplate_win32_printing_get_image_surface
,
395 _cairo_boilerplate_win32_printing_surface_write_to_png
,
396 _cairo_boilerplate_win32_printing_cleanup
,
397 NULL
, NULL
, FALSE
, TRUE
, TRUE
400 CAIRO_BOILERPLATE (win32_printing
, targets
)
404 CAIRO_NO_BOILERPLATE (win32_printing
)