2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / cairo / boilerplate / cairo-boilerplate-win32-printing.c
bloba5f21fff5e7efae1ab4591a9d1f8782ba7af401a
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 /* We require Windows 2000 features such as GetDefaultPrinter() */
30 #if !defined(WINVER) || (WINVER < 0x0500)
31 # define WINVER 0x0500
32 #endif
33 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
34 # define _WIN32_WINNT 0x0500
35 #endif
37 #include "cairo-boilerplate.h"
38 #include "cairo-boilerplate-win32-private.h"
40 #include <cairo-win32.h>
41 #include <cairo-win32-private.h>
42 #include <cairo-paginated-surface-private.h>
44 #include <windows.h>
46 #if !defined(POSTSCRIPT_IDENTIFY)
47 # define POSTSCRIPT_IDENTIFY 0x1015
48 #endif
50 #if !defined(PSIDENT_GDICENTRIC)
51 # define PSIDENT_GDICENTRIC 0x0000
52 #endif
54 #if !defined(GET_PS_FEATURESETTING)
55 # define GET_PS_FEATURESETTING 0x1019
56 #endif
58 #if !defined(FEATURESETTING_PSLEVEL)
59 # define FEATURESETTING_PSLEVEL 0x0002
60 #endif
62 cairo_user_data_key_t win32_closure_key;
64 typedef struct _win32_target_closure
66 char *filename;
67 int width;
68 int height;
69 cairo_surface_t *target;
70 HDC dc;
71 int left_margin;
72 int bottom_margin;
73 } win32_target_closure_t;
75 static cairo_bool_t
76 printer_is_postscript_level_3 (HDC dc)
78 DWORD word;
79 INT ps_feature, ps_level;
81 word = PSIDENT_GDICENTRIC;
82 if (ExtEscape (dc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), (char *)&word, 0, (char *)NULL) <= 0)
83 return FALSE;
85 ps_feature = FEATURESETTING_PSLEVEL;
86 if (ExtEscape (dc, GET_PS_FEATURESETTING, sizeof(INT),
87 (char *)&ps_feature, sizeof(INT), (char *)&ps_level) <= 0)
88 return FALSE;
90 if (ps_level >= 3)
91 return TRUE;
93 return FALSE;
96 static void
97 create_printer_dc (win32_target_closure_t *ptc)
99 char *printer_name;
100 DWORD size;
101 int x_dpi, y_dpi, left_margin, top_margin, page_height, printable_height;
102 XFORM xform;
104 ptc->dc = NULL;
105 GetDefaultPrinter (NULL, &size);
106 printer_name = malloc (size);
108 if (printer_name == NULL)
109 return;
111 if (GetDefaultPrinter (printer_name, &size) == 0) {
112 free (printer_name);
113 return;
116 /* printf("\nPrinting to : %s\n", printer_name); */
117 ptc->dc = CreateDC (NULL, printer_name, NULL, NULL);
118 free (printer_name);
120 if (!printer_is_postscript_level_3 (ptc->dc)) {
121 printf("The default printer driver must be a color PostScript level 3 printer\n");
122 ptc->dc = NULL;
123 return;
126 /* The printer device units on win32 are 1 unit == 1 dot and the
127 * origin is the start of the printable area. We transform the
128 * cordinate space to 1 unit is 1 point as expected by the
129 * tests. As the page size is larger than the test surface, the
130 * origin is translated down so that the each test is drawn at the
131 * bottom left corner of the page. This is because the bottom left
132 * corner of the PNG image that ghostscript creates is positioned
133 * at origin of the PS coordinates (ie the bottom left of the
134 * page). The left and bottom margins are stored in
135 * win32_target_closure as size of the PNG image needs to be
136 * increased because the test output is offset from the bottom
137 * left by the non printable margins. After the PNG is created the
138 * margins will be chopped off so the image matches the reference
139 * image.
141 printable_height = GetDeviceCaps (ptc->dc, VERTRES);
142 x_dpi = GetDeviceCaps (ptc->dc, LOGPIXELSX);
143 y_dpi = GetDeviceCaps (ptc->dc, LOGPIXELSY);
144 left_margin = GetDeviceCaps (ptc->dc, PHYSICALOFFSETX);
145 top_margin = GetDeviceCaps (ptc->dc, PHYSICALOFFSETY);
146 page_height = GetDeviceCaps (ptc->dc, PHYSICALHEIGHT);
148 SetGraphicsMode (ptc->dc, GM_ADVANCED);
149 xform.eM11 = x_dpi/72.0;
150 xform.eM12 = 0;
151 xform.eM21 = 0;
152 xform.eM22 = y_dpi/72.0;
153 xform.eDx = 0;
154 xform.eDy = printable_height - ptc->height*y_dpi/72.0;
155 if (!SetWorldTransform (ptc->dc, &xform)) {
156 _cairo_win32_print_gdi_error ("cairo-boilerplate-win32-printing:SetWorldTransform");
157 return;
160 ptc->left_margin = 72.0*left_margin/x_dpi;
161 ptc->bottom_margin = 72.0*(page_height - printable_height - top_margin)/y_dpi;
164 cairo_surface_t *
165 _cairo_boilerplate_win32_printing_create_surface (const char *name,
166 cairo_content_t content,
167 int width,
168 int height,
169 int max_width,
170 int max_height,
171 cairo_boilerplate_mode_t mode,
172 int id,
173 void **closure)
175 win32_target_closure_t *ptc;
176 cairo_surface_t *surface;
177 DOCINFO di;
179 if (content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED)
180 content = CAIRO_CONTENT_COLOR_ALPHA;
182 *closure = ptc = xmalloc (sizeof (win32_target_closure_t));
184 xasprintf (&ptc->filename, "%s-out.ps", name);
185 xunlink (ptc->filename);
187 memset (&di, 0, sizeof (DOCINFO));
188 di.cbSize = sizeof (DOCINFO);
189 di.lpszDocName = ptc->filename;
190 di.lpszOutput = ptc->filename;
192 ptc->width = width;
193 ptc->height = height;
195 create_printer_dc (ptc);
196 if (ptc->dc == NULL) {
197 printf("\nFailed to create printer\n");
198 free (ptc->filename);
199 free (ptc);
200 return NULL;
202 StartDoc (ptc->dc, &di);
203 StartPage (ptc->dc);
204 surface = cairo_win32_printing_surface_create (ptc->dc);
205 if (cairo_surface_status (surface)) {
206 free (ptc->filename);
207 free (ptc);
208 return NULL;
210 cairo_surface_set_fallback_resolution (surface, 72., 72.);
212 if (content == CAIRO_CONTENT_COLOR) {
213 ptc->target = surface;
214 surface = cairo_surface_create_similar (ptc->target,
215 CAIRO_CONTENT_COLOR,
216 width, height);
217 } else {
218 ptc->target = NULL;
221 if (cairo_surface_set_user_data (surface,
222 &win32_closure_key,
223 ptc,
224 NULL) != CAIRO_STATUS_SUCCESS) {
225 cairo_surface_destroy (surface);
226 if (ptc->target != NULL)
227 cairo_surface_destroy (ptc->target);
228 free (ptc->filename);
229 free (ptc);
230 return NULL;
233 return surface;
236 cairo_status_t
237 _cairo_boilerplate_win32_printing_surface_write_to_png (cairo_surface_t *surface, const char *filename)
239 win32_target_closure_t *ptc = cairo_surface_get_user_data (surface, &win32_closure_key);
240 char command[4096];
241 cairo_surface_t *src_image, *dst_image;
242 cairo_t *cr;
243 cairo_status_t status;
245 /* Both surface and ptc->target were originally created at the
246 * same dimensions. We want a 1:1 copy here, so we first clear any
247 * device offset on surface.
249 * In a more realistic use case of device offsets, the target of
250 * this copying would be of a different size than the source, and
251 * the offset would be desirable during the copy operation. */
252 cairo_surface_set_device_offset (surface, 0, 0);
254 if (ptc->target) {
255 cairo_t *cr;
256 cr = cairo_create (ptc->target);
257 cairo_set_source_surface (cr, surface, 0, 0);
258 cairo_paint (cr);
259 cairo_show_page (cr);
260 cairo_destroy (cr);
262 cairo_surface_finish (surface);
263 surface = ptc->target;
266 cairo_surface_finish (surface);
267 EndPage (ptc->dc);
268 EndDoc (ptc->dc);
269 sprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s",
270 ptc->width + ptc->left_margin, ptc->height + ptc->bottom_margin, filename, ptc->filename);
272 if (system (command) != 0)
273 return CAIRO_STATUS_WRITE_ERROR;
275 /* Create a new image from the ghostscript image that has the
276 * left and bottom margins removed */
278 src_image = cairo_image_surface_create_from_png (filename);
279 status = cairo_surface_status (src_image);
280 if (status)
281 return status;
283 dst_image = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
284 ptc->width,
285 ptc->height);
286 cr = cairo_create (dst_image);
287 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
288 cairo_set_source_surface (cr, src_image, -ptc->left_margin, 0);
289 cairo_paint (cr);
290 cairo_destroy (cr);
292 cairo_surface_write_to_png (dst_image, filename);
293 status = cairo_surface_status (dst_image);
294 if (status)
295 return status;
297 cairo_surface_destroy (src_image);
298 cairo_surface_destroy (dst_image);
300 return CAIRO_STATUS_SUCCESS;
303 cairo_surface_t *
304 _cairo_boilerplate_win32_printing_get_image_surface (cairo_surface_t *surface,
305 int page,
306 int width,
307 int height)
309 win32_target_closure_t *ptc = cairo_surface_get_user_data (surface,
310 &win32_closure_key);
311 char *filename;
312 cairo_status_t status;
314 /* XXX test paginated interface */
315 if (page != 0)
316 return cairo_boilerplate_surface_create_in_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
318 xasprintf (&filename, "%s.png", ptc->filename);
319 status = _cairo_boilerplate_win32_printing_surface_write_to_png (surface, filename);
320 if (status)
321 return cairo_boilerplate_surface_create_in_error (status);
323 surface = cairo_boilerplate_get_image_surface_from_png (filename,
324 width,
325 height,
326 ptc->target == NULL);
328 remove (filename);
329 free (filename);
331 return surface;
334 void
335 _cairo_boilerplate_win32_printing_cleanup (void *closure)
337 win32_target_closure_t *ptc = closure;
339 if (ptc->target)
340 cairo_surface_destroy (ptc->target);
341 free (ptc->filename);
342 free (ptc);
343 DeleteDC (ptc->dc);