Merge branch 'mingw11' into 'master'
[cairo.git] / boilerplate / cairo-boilerplate-win32-printing.c
blobe8fcdcef591a2b62d06f2a7f21040ab0de927d67
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /*
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>
37 #include <windows.h>
39 #if !defined(POSTSCRIPT_IDENTIFY)
40 # define POSTSCRIPT_IDENTIFY 0x1015
41 #endif
43 #if !defined(PSIDENT_GDICENTRIC)
44 # define PSIDENT_GDICENTRIC 0x0000
45 #endif
47 #if !defined(GET_PS_FEATURESETTING)
48 # define GET_PS_FEATURESETTING 0x1019
49 #endif
51 #if !defined(FEATURESETTING_PSLEVEL)
52 # define FEATURESETTING_PSLEVEL 0x0002
53 #endif
55 static cairo_status_t
56 _cairo_win32_print_gdi_error (const char *context)
58 void *lpMsgBuf;
59 DWORD last_error = GetLastError ();
61 if (!FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
62 FORMAT_MESSAGE_FROM_SYSTEM,
63 NULL,
64 last_error,
65 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
66 (LPWSTR) &lpMsgBuf,
67 0, NULL)) {
68 fprintf (stderr, "%s: Unknown GDI error", context);
69 } else {
70 fprintf (stderr, "%s: %S", context, (wchar_t *)lpMsgBuf);
72 LocalFree (lpMsgBuf);
75 fflush (stderr);
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 {
87 char *filename;
88 int width;
89 int height;
90 cairo_surface_t *target;
91 HDC dc;
92 int left_margin;
93 int bottom_margin;
94 } win32_target_closure_t;
96 static cairo_bool_t
97 printer_is_postscript_level_3 (HDC dc)
99 DWORD word;
100 INT ps_feature, ps_level;
102 word = PSIDENT_GDICENTRIC;
103 if (ExtEscape (dc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), (char *)&word, 0, (char *)NULL) <= 0)
104 return FALSE;
106 ps_feature = FEATURESETTING_PSLEVEL;
107 if (ExtEscape (dc, GET_PS_FEATURESETTING, sizeof(INT),
108 (char *)&ps_feature, sizeof(INT), (char *)&ps_level) <= 0)
109 return FALSE;
111 if (ps_level >= 3)
112 return TRUE;
114 return FALSE;
117 static void
118 create_printer_dc (win32_target_closure_t *ptc)
120 char *printer_name;
121 DWORD size;
122 int x_dpi, y_dpi, left_margin, top_margin, page_height, printable_height;
123 XFORM xform;
125 ptc->dc = NULL;
126 GetDefaultPrinter (NULL, &size);
127 printer_name = _cairo_malloc (size);
129 if (printer_name == NULL)
130 return;
132 if (GetDefaultPrinter (printer_name, &size) == 0) {
133 free (printer_name);
134 return;
137 /* printf("\nPrinting to : %s\n", printer_name); */
138 ptc->dc = CreateDC (NULL, printer_name, NULL, NULL);
139 free (printer_name);
141 if (!printer_is_postscript_level_3 (ptc->dc)) {
142 printf("The default printer driver must be a color PostScript level 3 printer\n");
143 ptc->dc = NULL;
144 return;
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
160 * image.
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;
171 xform.eM12 = 0;
172 xform.eM21 = 0;
173 xform.eM22 = y_dpi/72.0;
174 xform.eDx = 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");
178 return;
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,
188 double width,
189 double height,
190 double max_width,
191 double max_height,
192 cairo_boilerplate_mode_t mode,
193 void **closure)
195 win32_target_closure_t *ptc;
196 cairo_surface_t *surface;
197 DOCINFO di;
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;
212 ptc->width = width;
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);
219 free (ptc);
220 return NULL;
222 StartDoc (ptc->dc, &di);
223 StartPage (ptc->dc);
224 surface = cairo_win32_printing_surface_create (ptc->dc);
225 if (cairo_surface_status (surface)) {
226 free (ptc->filename);
227 free (ptc);
228 return NULL;
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,
235 CAIRO_CONTENT_COLOR,
236 width, height);
237 } else {
238 ptc->target = NULL;
241 if (cairo_surface_set_user_data (surface,
242 &win32_closure_key,
243 ptc,
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);
249 free (ptc);
250 return NULL;
253 return surface;
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);
261 char command[4096];
262 cairo_surface_t *src_image, *dst_image;
263 cairo_t *cr;
264 cairo_status_t status;
266 if (ptc->target) {
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);
280 cairo_t *cr;
281 cr = cairo_create (ptc->target);
282 cairo_set_source_surface (cr, surface, 0, 0);
283 cairo_paint (cr);
284 cairo_show_page (cr);
285 cairo_destroy (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);
294 EndPage (ptc->dc);
295 EndDoc (ptc->dc);
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);
307 if (status)
308 return status;
310 dst_image = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
311 ptc->width,
312 ptc->height);
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);
316 cairo_paint (cr);
317 cairo_destroy (cr);
319 cairo_surface_write_to_png (dst_image, filename);
320 status = cairo_surface_status (dst_image);
321 if (status)
322 return status;
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,
332 int page,
333 int width,
334 int height)
336 win32_target_closure_t *ptc = cairo_surface_get_user_data (surface,
337 &win32_closure_key);
338 char *filename;
339 cairo_status_t status;
341 /* XXX test paginated interface */
342 if (page != 0)
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);
347 if (status)
348 return cairo_boilerplate_surface_create_in_error (status);
350 surface = cairo_boilerplate_get_image_surface_from_png (filename,
351 width,
352 height,
353 ptc->target == NULL);
355 remove (filename);
356 free (filename);
358 return surface;
361 static void
362 _cairo_boilerplate_win32_printing_cleanup (void *closure)
364 win32_target_closure_t *ptc = closure;
366 if (ptc->target)
367 cairo_surface_destroy (ptc->target);
368 free (ptc->filename);
369 free (ptc);
370 DeleteDC (ptc->dc);
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,
381 NULL, NULL,
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,
393 NULL, NULL,
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)
402 #else
404 CAIRO_NO_BOILERPLATE (win32_printing)
406 #endif