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 /* We require Windows 2000 features such as GetDefaultPrinter() */
30 #if !defined(WINVER) || (WINVER < 0x0500)
31 # define WINVER 0x0500
33 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
34 # define _WIN32_WINNT 0x0500
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>
46 #if !defined(POSTSCRIPT_IDENTIFY)
47 # define POSTSCRIPT_IDENTIFY 0x1015
50 #if !defined(PSIDENT_GDICENTRIC)
51 # define PSIDENT_GDICENTRIC 0x0000
54 #if !defined(GET_PS_FEATURESETTING)
55 # define GET_PS_FEATURESETTING 0x1019
58 #if !defined(FEATURESETTING_PSLEVEL)
59 # define FEATURESETTING_PSLEVEL 0x0002
62 cairo_user_data_key_t win32_closure_key
;
64 typedef struct _win32_target_closure
69 cairo_surface_t
*target
;
73 } win32_target_closure_t
;
76 printer_is_postscript_level_3 (HDC dc
)
79 INT ps_feature
, ps_level
;
81 word
= PSIDENT_GDICENTRIC
;
82 if (ExtEscape (dc
, POSTSCRIPT_IDENTIFY
, sizeof(DWORD
), (char *)&word
, 0, (char *)NULL
) <= 0)
85 ps_feature
= FEATURESETTING_PSLEVEL
;
86 if (ExtEscape (dc
, GET_PS_FEATURESETTING
, sizeof(INT
),
87 (char *)&ps_feature
, sizeof(INT
), (char *)&ps_level
) <= 0)
97 create_printer_dc (win32_target_closure_t
*ptc
)
101 int x_dpi
, y_dpi
, left_margin
, top_margin
, page_height
, printable_height
;
105 GetDefaultPrinter (NULL
, &size
);
106 printer_name
= malloc (size
);
108 if (printer_name
== NULL
)
111 if (GetDefaultPrinter (printer_name
, &size
) == 0) {
116 /* printf("\nPrinting to : %s\n", printer_name); */
117 ptc
->dc
= CreateDC (NULL
, printer_name
, NULL
, NULL
);
120 if (!printer_is_postscript_level_3 (ptc
->dc
)) {
121 printf("The default printer driver must be a color PostScript level 3 printer\n");
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
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;
152 xform
.eM22
= y_dpi
/72.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");
160 ptc
->left_margin
= 72.0*left_margin
/x_dpi
;
161 ptc
->bottom_margin
= 72.0*(page_height
- printable_height
- top_margin
)/y_dpi
;
165 _cairo_boilerplate_win32_printing_create_surface (const char *name
,
166 cairo_content_t content
,
171 cairo_boilerplate_mode_t mode
,
175 win32_target_closure_t
*ptc
;
176 cairo_surface_t
*surface
;
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
;
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
);
202 StartDoc (ptc
->dc
, &di
);
204 surface
= cairo_win32_printing_surface_create (ptc
->dc
);
205 if (cairo_surface_status (surface
)) {
206 free (ptc
->filename
);
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
,
221 if (cairo_surface_set_user_data (surface
,
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
);
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
);
241 cairo_surface_t
*src_image
, *dst_image
;
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);
256 cr
= cairo_create (ptc
->target
);
257 cairo_set_source_surface (cr
, surface
, 0, 0);
259 cairo_show_page (cr
);
262 cairo_surface_finish (surface
);
263 surface
= ptc
->target
;
266 cairo_surface_finish (surface
);
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
);
283 dst_image
= cairo_image_surface_create (CAIRO_FORMAT_RGB24
,
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);
292 cairo_surface_write_to_png (dst_image
, filename
);
293 status
= cairo_surface_status (dst_image
);
297 cairo_surface_destroy (src_image
);
298 cairo_surface_destroy (dst_image
);
300 return CAIRO_STATUS_SUCCESS
;
304 _cairo_boilerplate_win32_printing_get_image_surface (cairo_surface_t
*surface
,
309 win32_target_closure_t
*ptc
= cairo_surface_get_user_data (surface
,
312 cairo_status_t status
;
314 /* XXX test paginated interface */
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
);
321 return cairo_boilerplate_surface_create_in_error (status
);
323 surface
= cairo_boilerplate_get_image_surface_from_png (filename
,
326 ptc
->target
== NULL
);
335 _cairo_boilerplate_win32_printing_cleanup (void *closure
)
337 win32_target_closure_t
*ptc
= closure
;
340 cairo_surface_destroy (ptc
->target
);
341 free (ptc
->filename
);