1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* Cairo - a vector graphics library with display and print output
3  *
4  * Copyright © 2007, 2008 Adrian Johnson
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  * The Original Code is the cairo graphics library.
30  *
31  * The Initial Developer of the Original Code is Adrian Johnson.
32  *
33  * Contributor(s):
34  *      Adrian Johnson <ajohnson@redneon.com>
35  *      Vladimir Vukicevic <vladimir@pobox.com>
36  */
37 
38 #define WIN32_LEAN_AND_MEAN
39 /* We require Windows 2000 features such as ETO_PDY */
40 #if !defined(WINVER) || (WINVER < 0x0500)
41 # define WINVER 0x0500
42 #endif
43 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
44 # define _WIN32_WINNT 0x0500
45 #endif
46 
47 #include "cairoint.h"
48 
49 #include "cairo-default-context-private.h"
50 #include "cairo-error-private.h"
51 #include "cairo-paginated-private.h"
52 
53 #include "cairo-clip-private.h"
54 #include "cairo-composite-rectangles-private.h"
55 #include "cairo-win32-private.h"
56 #include "cairo-recording-surface-inline.h"
57 #include "cairo-scaled-font-subsets-private.h"
58 #include "cairo-image-info-private.h"
59 #include "cairo-image-surface-inline.h"
60 #include "cairo-image-surface-private.h"
61 #include "cairo-surface-backend-private.h"
62 #include "cairo-surface-clipper-private.h"
63 #include "cairo-surface-snapshot-inline.h"
64 #include "cairo-surface-subsurface-private.h"
65 
66 #include <windows.h>
67 
68 #if !defined(POSTSCRIPT_IDENTIFY)
69 # define POSTSCRIPT_IDENTIFY 0x1015
70 #endif
71 
72 #if !defined(PSIDENT_GDICENTRIC)
73 # define PSIDENT_GDICENTRIC 0x0000
74 #endif
75 
76 #if !defined(GET_PS_FEATURESETTING)
77 # define GET_PS_FEATURESETTING 0x1019
78 #endif
79 
80 #if !defined(FEATURESETTING_PSLEVEL)
81 # define FEATURESETTING_PSLEVEL 0x0002
82 #endif
83 
84 #if !defined(GRADIENT_FILL_RECT_H)
85 # define GRADIENT_FILL_RECT_H 0x00
86 #endif
87 
88 #if !defined(CHECKJPEGFORMAT)
89 # define CHECKJPEGFORMAT 0x1017
90 #endif
91 
92 #if !defined(CHECKPNGFORMAT)
93 # define CHECKPNGFORMAT 0x1018
94 #endif
95 
96 #define PELS_72DPI  ((LONG)(72. / 0.0254))
97 
98 static const char *_cairo_win32_printing_supported_mime_types[] =
99 {
100     CAIRO_MIME_TYPE_JPEG,
101     CAIRO_MIME_TYPE_PNG,
102     NULL
103 };
104 
105 static const cairo_surface_backend_t cairo_win32_printing_surface_backend;
106 static const cairo_paginated_surface_backend_t cairo_win32_surface_paginated_backend;
107 
108 static void
_cairo_win32_printing_surface_init_ps_mode(cairo_win32_printing_surface_t * surface)109 _cairo_win32_printing_surface_init_ps_mode (cairo_win32_printing_surface_t *surface)
110 {
111     DWORD word;
112     INT ps_feature, ps_level;
113 
114     word = PSIDENT_GDICENTRIC;
115     if (ExtEscape (surface->win32.dc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), (char *)&word, 0, (char *)NULL) <= 0)
116 	return;
117 
118     ps_feature = FEATURESETTING_PSLEVEL;
119     if (ExtEscape (surface->win32.dc, GET_PS_FEATURESETTING, sizeof(INT),
120 		   (char *)&ps_feature, sizeof(INT), (char *)&ps_level) <= 0)
121 	return;
122 
123     if (ps_level >= 3)
124 	surface->win32.flags |= CAIRO_WIN32_SURFACE_CAN_RECT_GRADIENT;
125 }
126 
127 static void
_cairo_win32_printing_surface_init_image_support(cairo_win32_printing_surface_t * surface)128 _cairo_win32_printing_surface_init_image_support (cairo_win32_printing_surface_t *surface)
129 {
130     DWORD word;
131 
132     word = CHECKJPEGFORMAT;
133     if (ExtEscape(surface->win32.dc, QUERYESCSUPPORT, sizeof(word), (char *)&word, 0, (char *)NULL) > 0)
134 	surface->win32.flags |= CAIRO_WIN32_SURFACE_CAN_CHECK_JPEG;
135 
136     word = CHECKPNGFORMAT;
137     if (ExtEscape(surface->win32.dc, QUERYESCSUPPORT, sizeof(word), (char *)&word, 0, (char *)NULL) > 0)
138 	surface->win32.flags |= CAIRO_WIN32_SURFACE_CAN_CHECK_PNG;
139 }
140 
141 /* When creating an EMF file, ExtTextOut with ETO_GLYPH_INDEX does not
142  * work unless the GDI function GdiInitializeLanguagePack() has been
143  * called.
144  *
145  *   http://m-a-tech.blogspot.com/2009/04/emf-buffer-idiocracy.html
146  *
147  * The only information I could find on the how to use this
148  * undocumented function is the use in:
149  *
150  * http://src.chromium.org/viewvc/chrome/trunk/src/chrome/renderer/render_process.cc?view=markup
151  *
152  * to solve the same problem. The above code first checks if LPK.DLL
153  * is already loaded. If it is not it calls
154  * GdiInitializeLanguagePack() using the prototype
155  *   BOOL GdiInitializeLanguagePack (int)
156  * and argument 0.
157  */
158 static void
_cairo_win32_printing_surface_init_language_pack(cairo_win32_printing_surface_t * surface)159 _cairo_win32_printing_surface_init_language_pack (cairo_win32_printing_surface_t *surface)
160 {
161     typedef BOOL (WINAPI *gdi_init_lang_pack_func_t)(int);
162     gdi_init_lang_pack_func_t gdi_init_lang_pack;
163     HMODULE module;
164 
165     if (GetModuleHandleW (L"LPK.DLL"))
166 	return;
167 
168     module = GetModuleHandleW (L"GDI32.DLL");
169     if (module) {
170 	gdi_init_lang_pack = (gdi_init_lang_pack_func_t)
171 	    GetProcAddress (module, "GdiInitializeLanguagePack");
172 	if (gdi_init_lang_pack)
173 	    gdi_init_lang_pack (0);
174     }
175 }
176 
177 /**
178  * _cairo_win32_printing_surface_acquire_image_pattern:
179  * @surface: the win32 printing surface
180  * @pattern: A #cairo_pattern_t of type SURFACE or RASTER_SOURCE to use as the source
181  * @extents: extents of the operation that is using this source
182  * @image_pattern: returns pattern containing acquired image. The matrix (adjusted for
183  * the device offset of raster source) is copied from the pattern.
184  * @width: returns width of the pattern
185  * @height: returns height of  pattern
186  * @image_extra: returns image extra for image type surface
187  *
188  * Acquire source surface or raster source pattern.
189  **/
190 static cairo_status_t
_cairo_win32_printing_surface_acquire_image_pattern(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents,cairo_surface_pattern_t * image_pattern,int * width,int * height,void ** image_extra)191 _cairo_win32_printing_surface_acquire_image_pattern (
192     cairo_win32_printing_surface_t  *surface,
193     const cairo_pattern_t           *pattern,
194     const cairo_rectangle_int_t     *extents,
195     cairo_surface_pattern_t         *image_pattern,
196     int                             *width,
197     int                             *height,
198     void                           **image_extra)
199 {
200     cairo_status_t          status;
201     cairo_image_surface_t  *image;
202     cairo_matrix_t tm;
203     double x = 0;
204     double y = 0;
205 
206     switch (pattern->type) {
207     case CAIRO_PATTERN_TYPE_SURFACE: {
208 	cairo_surface_t *surf = ((cairo_surface_pattern_t *) pattern)->surface;
209 
210 	status =  _cairo_surface_acquire_source_image (surf, &image, image_extra);
211 	if (unlikely (status))
212 	    return status;
213 
214 	*width = image->width;
215 	*height = image->height;
216     } break;
217 
218     case CAIRO_PATTERN_TYPE_RASTER_SOURCE: {
219 	cairo_surface_t *surf;
220 	cairo_box_t box;
221 	cairo_rectangle_int_t rect;
222 	cairo_raster_source_pattern_t *raster;
223 
224 	/* get the operation extents in pattern space */
225 	_cairo_box_from_rectangle (&box, extents);
226 	_cairo_matrix_transform_bounding_box_fixed (&pattern->matrix, &box, NULL);
227 	_cairo_box_round_to_rectangle (&box, &rect);
228 	surf = _cairo_raster_source_pattern_acquire (pattern, &surface->win32.base, &rect);
229 	if (!surf)
230 	    return CAIRO_INT_STATUS_UNSUPPORTED;
231 
232 	assert (_cairo_surface_is_image (surf));
233 	image = (cairo_image_surface_t *) surf;
234 	cairo_surface_get_device_offset (surf, &x, &y);
235 
236 	raster = (cairo_raster_source_pattern_t *) pattern;
237 	*width = raster->extents.width;
238 	*height = raster->extents.height;
239     } break;
240 
241     case CAIRO_PATTERN_TYPE_SOLID:
242     case CAIRO_PATTERN_TYPE_LINEAR:
243     case CAIRO_PATTERN_TYPE_RADIAL:
244     case CAIRO_PATTERN_TYPE_MESH:
245     default:
246 	ASSERT_NOT_REACHED;
247 	break;
248     }
249 
250     _cairo_pattern_init_for_surface (image_pattern, &image->base);
251     image_pattern->base.extend = pattern->extend;
252     cairo_matrix_init_translate (&tm, x, y);
253     status = cairo_matrix_invert (&tm);
254     /* translation matrices are invertibile */
255     assert (status == CAIRO_STATUS_SUCCESS);
256 
257     image_pattern->base.matrix = pattern->matrix;
258     cairo_matrix_multiply (&image_pattern->base.matrix, &image_pattern->base.matrix, &tm);
259 
260     return CAIRO_STATUS_SUCCESS;
261 }
262 
263 static void
_cairo_win32_printing_surface_release_image_pattern(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern,cairo_surface_pattern_t * image_pattern,void * image_extra)264 _cairo_win32_printing_surface_release_image_pattern (cairo_win32_printing_surface_t *surface,
265 						     const cairo_pattern_t          *pattern,
266 						     cairo_surface_pattern_t        *image_pattern,
267 						     void                           *image_extra)
268 {
269     cairo_surface_t *surf = image_pattern->surface;
270 
271     _cairo_pattern_fini (&image_pattern->base);
272     switch (pattern->type) {
273     case CAIRO_PATTERN_TYPE_SURFACE: {
274 	cairo_surface_pattern_t *surf_pat = (cairo_surface_pattern_t *) pattern;
275 	cairo_image_surface_t *image = (cairo_image_surface_t *) surf;
276 	_cairo_surface_release_source_image (surf_pat->surface, image, image_extra);
277     } break;
278 
279     case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
280 	_cairo_raster_source_pattern_release (pattern, surf);
281 	break;
282 
283     case CAIRO_PATTERN_TYPE_SOLID:
284     case CAIRO_PATTERN_TYPE_LINEAR:
285     case CAIRO_PATTERN_TYPE_RADIAL:
286     case CAIRO_PATTERN_TYPE_MESH:
287     default:
288 	ASSERT_NOT_REACHED;
289 	break;
290     }
291 }
292 
293 static cairo_int_status_t
analyze_surface_pattern_transparency(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents)294 analyze_surface_pattern_transparency (cairo_win32_printing_surface_t *surface,
295 				      const cairo_pattern_t          *pattern,
296 				      const cairo_rectangle_int_t    *extents)
297 {
298     cairo_surface_pattern_t image_pattern;
299     cairo_image_surface_t  *image;
300     void		   *image_extra;
301     cairo_int_status_t      status;
302     cairo_image_transparency_t transparency;
303     int pattern_width, pattern_height;
304 
305     status = _cairo_win32_printing_surface_acquire_image_pattern (surface,
306 								  pattern,
307 								  extents,
308 								  &image_pattern,
309 								  &pattern_width,
310 								  &pattern_height,
311 								  &image_extra);
312     if (status)
313 	return status;
314 
315     image = (cairo_image_surface_t *)(image_pattern.surface);
316     transparency = _cairo_image_analyze_transparency (image);
317     switch (transparency) {
318     case CAIRO_IMAGE_UNKNOWN:
319 	ASSERT_NOT_REACHED;
320     case CAIRO_IMAGE_IS_OPAQUE:
321 	status = CAIRO_STATUS_SUCCESS;
322 	break;
323 
324     case CAIRO_IMAGE_HAS_BILEVEL_ALPHA:
325     case CAIRO_IMAGE_HAS_ALPHA:
326 	status = CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY;
327 	break;
328     }
329 
330     _cairo_win32_printing_surface_release_image_pattern (surface, pattern, &image_pattern, image_extra);
331 
332     return status;
333 }
334 
335 static cairo_bool_t
surface_pattern_supported(const cairo_surface_pattern_t * pattern)336 surface_pattern_supported (const cairo_surface_pattern_t *pattern)
337 {
338     if (_cairo_surface_is_recording (pattern->surface))
339 	return TRUE;
340 
341     if (pattern->surface->backend->acquire_source_image == NULL)
342     {
343 	return FALSE;
344     }
345 
346     return TRUE;
347 }
348 
349 static cairo_bool_t
pattern_supported(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern)350 pattern_supported (cairo_win32_printing_surface_t *surface, const cairo_pattern_t *pattern)
351 {
352     switch (pattern->type) {
353     case CAIRO_PATTERN_TYPE_SOLID:
354 	return TRUE;
355 
356     case CAIRO_PATTERN_TYPE_LINEAR:
357 	return surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_RECT_GRADIENT;
358 
359     case CAIRO_PATTERN_TYPE_RADIAL:
360     case CAIRO_PATTERN_TYPE_MESH:
361 	return FALSE;
362 
363     case CAIRO_PATTERN_TYPE_SURFACE:
364 	return surface_pattern_supported ((cairo_surface_pattern_t *) pattern);
365 
366     case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
367 	return TRUE;
368 
369     default:
370 	ASSERT_NOT_REACHED;
371 	return FALSE;
372     }
373 }
374 
375 static cairo_int_status_t
_cairo_win32_printing_surface_analyze_operation(cairo_win32_printing_surface_t * surface,cairo_operator_t op,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents)376 _cairo_win32_printing_surface_analyze_operation (cairo_win32_printing_surface_t *surface,
377                                                  cairo_operator_t                op,
378                                                  const cairo_pattern_t          *pattern,
379 						 const cairo_rectangle_int_t    *extents)
380 {
381     if (! pattern_supported (surface, pattern))
382 	return CAIRO_INT_STATUS_UNSUPPORTED;
383 
384     if (!(op == CAIRO_OPERATOR_SOURCE ||
385 	  op == CAIRO_OPERATOR_OVER ||
386 	  op == CAIRO_OPERATOR_CLEAR))
387 	return CAIRO_INT_STATUS_UNSUPPORTED;
388 
389     if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
390 	cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern;
391 
392 	if (surface_pattern->surface->type == CAIRO_SURFACE_TYPE_RECORDING)
393 	    return CAIRO_INT_STATUS_ANALYZE_RECORDING_SURFACE_PATTERN;
394     }
395 
396     if (op == CAIRO_OPERATOR_SOURCE ||
397 	op == CAIRO_OPERATOR_CLEAR)
398 	return CAIRO_STATUS_SUCCESS;
399 
400     /* CAIRO_OPERATOR_OVER is only supported for opaque patterns. If
401      * the pattern contains transparency, we return
402      * CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY to the analysis
403      * surface. If the analysis surface determines that there is
404      * anything drawn under this operation, a fallback image will be
405      * used. Otherwise the operation will be replayed during the
406      * render stage and we blend the transarency into the white
407      * background to convert the pattern to opaque.
408      */
409 
410     if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE || pattern->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE)
411 	return analyze_surface_pattern_transparency (surface, pattern, extents);
412 
413     if (_cairo_pattern_is_opaque (pattern, NULL))
414 	return CAIRO_STATUS_SUCCESS;
415     else
416 	return CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY;
417 }
418 
419 static cairo_bool_t
_cairo_win32_printing_surface_operation_supported(cairo_win32_printing_surface_t * surface,cairo_operator_t op,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents)420 _cairo_win32_printing_surface_operation_supported (cairo_win32_printing_surface_t *surface,
421                                                    cairo_operator_t                op,
422                                                    const cairo_pattern_t          *pattern,
423 						   const cairo_rectangle_int_t    *extents)
424 {
425     if (_cairo_win32_printing_surface_analyze_operation (surface, op, pattern, extents) != CAIRO_INT_STATUS_UNSUPPORTED)
426 	return TRUE;
427     else
428 	return FALSE;
429 }
430 
431 static void
_cairo_win32_printing_surface_init_clear_color(cairo_win32_printing_surface_t * surface,cairo_solid_pattern_t * color)432 _cairo_win32_printing_surface_init_clear_color (cairo_win32_printing_surface_t *surface,
433 						cairo_solid_pattern_t *color)
434 {
435     if (surface->content == CAIRO_CONTENT_COLOR_ALPHA)
436 	_cairo_pattern_init_solid (color, CAIRO_COLOR_WHITE);
437     else
438 	_cairo_pattern_init_solid (color, CAIRO_COLOR_BLACK);
439 }
440 
441 static COLORREF
_cairo_win32_printing_surface_flatten_transparency(cairo_win32_printing_surface_t * surface,const cairo_color_t * color)442 _cairo_win32_printing_surface_flatten_transparency (cairo_win32_printing_surface_t *surface,
443 						    const cairo_color_t   *color)
444 {
445     COLORREF c;
446     BYTE red, green, blue;
447 
448     red   = color->red_short   >> 8;
449     green = color->green_short >> 8;
450     blue  = color->blue_short  >> 8;
451 
452     if (!CAIRO_COLOR_IS_OPAQUE(color)) {
453 	if (surface->content == CAIRO_CONTENT_COLOR_ALPHA) {
454 	    /* Blend into white */
455 	    uint8_t one_minus_alpha = 255 - (color->alpha_short >> 8);
456 
457 	    red   = (color->red_short   >> 8) + one_minus_alpha;
458 	    green = (color->green_short >> 8) + one_minus_alpha;
459 	    blue  = (color->blue_short  >> 8) + one_minus_alpha;
460 	} else {
461 	    /* Blend into black */
462 	    red   = (color->red_short   >> 8);
463 	    green = (color->green_short >> 8);
464 	    blue  = (color->blue_short  >> 8);
465 	}
466     }
467     c = RGB (red, green, blue);
468 
469     return c;
470 }
471 
472 static cairo_status_t
_cairo_win32_printing_surface_select_solid_brush(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * source)473 _cairo_win32_printing_surface_select_solid_brush (cairo_win32_printing_surface_t *surface,
474                                                   const cairo_pattern_t *source)
475 {
476     cairo_solid_pattern_t *pattern = (cairo_solid_pattern_t *) source;
477     COLORREF color;
478 
479     color = _cairo_win32_printing_surface_flatten_transparency (surface,
480 								&pattern->color);
481     surface->brush = CreateSolidBrush (color);
482     if (!surface->brush)
483 	return _cairo_win32_print_gdi_error ("_cairo_win32_surface_select_solid_brush(CreateSolidBrush)");
484     surface->old_brush = SelectObject (surface->win32.dc, surface->brush);
485 
486     return CAIRO_STATUS_SUCCESS;
487 }
488 
489 static void
_cairo_win32_printing_surface_done_solid_brush(cairo_win32_printing_surface_t * surface)490 _cairo_win32_printing_surface_done_solid_brush (cairo_win32_printing_surface_t *surface)
491 {
492     if (surface->old_brush) {
493 	SelectObject (surface->win32.dc, surface->old_brush);
494 	DeleteObject (surface->brush);
495 	surface->old_brush = NULL;
496     }
497 }
498 
499 static cairo_status_t
_cairo_win32_printing_surface_get_ctm_clip_box(cairo_win32_printing_surface_t * surface,RECT * clip)500 _cairo_win32_printing_surface_get_ctm_clip_box (cairo_win32_printing_surface_t *surface,
501 						RECT                  *clip)
502 {
503     XFORM xform;
504 
505     _cairo_matrix_to_win32_xform (&surface->ctm, &xform);
506     if (!ModifyWorldTransform (surface->win32.dc, &xform, MWT_LEFTMULTIPLY))
507 	return _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_get_clip_box:ModifyWorldTransform");
508     GetClipBox (surface->win32.dc, clip);
509 
510     _cairo_matrix_to_win32_xform (&surface->gdi_ctm, &xform);
511     if (!SetWorldTransform (surface->win32.dc, &xform))
512 	return _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_get_clip_box:SetWorldTransform");
513 
514     return CAIRO_STATUS_SUCCESS;
515 }
516 
517 static cairo_status_t
_cairo_win32_printing_surface_paint_solid_pattern(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern)518 _cairo_win32_printing_surface_paint_solid_pattern (cairo_win32_printing_surface_t *surface,
519                                                    const cairo_pattern_t *pattern)
520 {
521     RECT clip;
522     cairo_status_t status;
523 
524     GetClipBox (surface->win32.dc, &clip);
525     status = _cairo_win32_printing_surface_select_solid_brush (surface, pattern);
526     if (status)
527 	return status;
528 
529     FillRect (surface->win32.dc, &clip, surface->brush);
530     _cairo_win32_printing_surface_done_solid_brush (surface);
531 
532     return CAIRO_STATUS_SUCCESS;
533 }
534 
535 static cairo_status_t
_cairo_win32_printing_surface_paint_recording_pattern(cairo_win32_printing_surface_t * surface,cairo_surface_pattern_t * pattern)536 _cairo_win32_printing_surface_paint_recording_pattern (cairo_win32_printing_surface_t   *surface,
537 						       cairo_surface_pattern_t *pattern)
538 {
539     cairo_content_t old_content;
540     cairo_matrix_t old_ctm;
541     cairo_bool_t old_has_ctm;
542     cairo_rectangle_int_t recording_extents;
543     cairo_int_status_t status;
544     cairo_extend_t extend;
545     cairo_matrix_t p2d;
546     XFORM xform;
547     int x_tile, y_tile, left, right, top, bottom;
548     RECT clip;
549     cairo_recording_surface_t *recording_surface = (cairo_recording_surface_t *) pattern->surface;
550     cairo_box_t bbox;
551     cairo_surface_t *free_me = NULL;
552     cairo_bool_t is_subsurface;
553 
554     extend = cairo_pattern_get_extend (&pattern->base);
555 
556     p2d = pattern->base.matrix;
557     status = cairo_matrix_invert (&p2d);
558     /* _cairo_pattern_set_matrix guarantees invertibility */
559     assert (status == CAIRO_INT_STATUS_SUCCESS);
560 
561     old_ctm = surface->ctm;
562     old_has_ctm = surface->has_ctm;
563     cairo_matrix_multiply (&p2d, &p2d, &surface->ctm);
564     surface->ctm = p2d;
565     SaveDC (surface->win32.dc);
566     _cairo_matrix_to_win32_xform (&p2d, &xform);
567 
568     if (_cairo_surface_is_snapshot (&recording_surface->base)) {
569 	free_me = _cairo_surface_snapshot_get_target (&recording_surface->base);
570 	recording_surface = (cairo_recording_surface_t *) free_me;
571     }
572 
573     if (recording_surface->base.backend->type == CAIRO_SURFACE_TYPE_SUBSURFACE) {
574 	cairo_surface_subsurface_t *sub = (cairo_surface_subsurface_t *) recording_surface;
575 
576 	recording_surface = (cairo_recording_surface_t *) (sub->target);
577 	recording_extents = sub->extents;
578 	is_subsurface = TRUE;
579     } else {
580 	status = _cairo_recording_surface_get_bbox (recording_surface, &bbox, NULL);
581 	if (status)
582 	    goto err;
583 
584 	_cairo_box_round_to_rectangle (&bbox, &recording_extents);
585     }
586 
587     status = _cairo_win32_printing_surface_get_ctm_clip_box (surface, &clip);
588     if (status)
589 	goto err;
590 
591     if (extend == CAIRO_EXTEND_REPEAT || extend == CAIRO_EXTEND_REFLECT) {
592 	left = floor (clip.left / _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x));
593 	right = ceil (clip.right / _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x));
594 	top = floor (clip.top / _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y));
595 	bottom = ceil (clip.bottom / _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y));
596     } else {
597 	left = 0;
598 	right = 1;
599 	top = 0;
600 	bottom = 1;
601     }
602 
603     old_content = surface->content;
604     if (recording_surface->base.content == CAIRO_CONTENT_COLOR) {
605 	surface->content = CAIRO_CONTENT_COLOR;
606 	status = _cairo_win32_printing_surface_paint_solid_pattern (surface,
607 								    &_cairo_pattern_black.base);
608 	if (status)
609 	    goto err;
610     }
611 
612     for (y_tile = top; y_tile < bottom; y_tile++) {
613 	for (x_tile = left; x_tile < right; x_tile++) {
614 	    cairo_matrix_t m;
615 	    double x, y;
616 
617 	    SaveDC (surface->win32.dc);
618 	    m = p2d;
619 	    cairo_matrix_translate (&m,
620 				    x_tile*recording_extents.width,
621 				    y_tile*recording_extents.height);
622 	    if (extend == CAIRO_EXTEND_REFLECT) {
623 		if (x_tile % 2) {
624 		    cairo_matrix_translate (&m, recording_extents.width, 0);
625 		    cairo_matrix_scale (&m, -1, 1);
626 		}
627 		if (y_tile % 2) {
628 		    cairo_matrix_translate (&m, 0, recording_extents.height);
629 		    cairo_matrix_scale (&m, 1, -1);
630 		}
631 	    }
632 	    surface->ctm = m;
633 	    surface->has_ctm = !_cairo_matrix_is_identity (&surface->ctm);
634 
635 	    /* Set clip path around bbox of the pattern. */
636 	    BeginPath (surface->win32.dc);
637 
638 	    x = 0;
639 	    y = 0;
640 	    cairo_matrix_transform_point (&surface->ctm, &x, &y);
641 	    MoveToEx (surface->win32.dc, (int) x, (int) y, NULL);
642 
643 	    x = recording_extents.width;
644 	    y = 0;
645 	    cairo_matrix_transform_point (&surface->ctm, &x, &y);
646 	    LineTo (surface->win32.dc, (int) x, (int) y);
647 
648 	    x = recording_extents.width;
649 	    y = recording_extents.height;
650 	    cairo_matrix_transform_point (&surface->ctm, &x, &y);
651 	    LineTo (surface->win32.dc, (int) x, (int) y);
652 
653 	    x = 0;
654 	    y = recording_extents.height;
655 	    cairo_matrix_transform_point (&surface->ctm, &x, &y);
656 	    LineTo (surface->win32.dc, (int) x, (int) y);
657 
658 	    CloseFigure (surface->win32.dc);
659 	    EndPath (surface->win32.dc);
660 	    SelectClipPath (surface->win32.dc, RGN_AND);
661 
662 	    SaveDC (surface->win32.dc); /* Allow clip path to be reset during replay */
663 	    status = _cairo_recording_surface_replay_region (&recording_surface->base,
664 							     is_subsurface ? &recording_extents : NULL,
665 							     &surface->win32.base,
666 							     CAIRO_RECORDING_REGION_NATIVE);
667 	    assert (status != CAIRO_INT_STATUS_UNSUPPORTED);
668 	    /* Restore both the clip save and our earlier path SaveDC */
669 	    RestoreDC (surface->win32.dc, -2);
670 
671 	    if (status)
672 		goto err;
673 	}
674     }
675 
676     surface->content = old_content;
677     surface->ctm = old_ctm;
678     surface->has_ctm = old_has_ctm;
679     RestoreDC (surface->win32.dc, -1);
680 
681   err:
682     cairo_surface_destroy (free_me);
683     return status;
684 }
685 
686 static cairo_int_status_t
_cairo_win32_printing_surface_check_jpeg(cairo_win32_printing_surface_t * surface,cairo_surface_t * source,const unsigned char ** data,unsigned long * length,cairo_image_info_t * info)687 _cairo_win32_printing_surface_check_jpeg (cairo_win32_printing_surface_t   *surface,
688 					  cairo_surface_t         *source,
689 					  const unsigned char    **data,
690 					  unsigned long           *length,
691 					  cairo_image_info_t      *info)
692 {
693     const unsigned char *mime_data;
694     unsigned long mime_data_length;
695     cairo_int_status_t status;
696     DWORD result;
697 
698     if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_CHECK_JPEG))
699 	return CAIRO_INT_STATUS_UNSUPPORTED;
700 
701     cairo_surface_get_mime_data (source, CAIRO_MIME_TYPE_JPEG,
702 				 &mime_data, &mime_data_length);
703     if (mime_data == NULL)
704 	return CAIRO_INT_STATUS_UNSUPPORTED;
705 
706     status = _cairo_image_info_get_jpeg_info (info, mime_data, mime_data_length);
707     if (status)
708 	return status;
709 
710     result = 0;
711     if (ExtEscape(surface->win32.dc, CHECKJPEGFORMAT, mime_data_length, (char *) mime_data,
712 		  sizeof(result), (char *) &result) <= 0)
713 	return CAIRO_INT_STATUS_UNSUPPORTED;
714 
715     if (result != 1)
716 	return CAIRO_INT_STATUS_UNSUPPORTED;
717 
718     *data = mime_data;
719     *length = mime_data_length;
720 
721     return CAIRO_STATUS_SUCCESS;
722 }
723 
724 static cairo_int_status_t
_cairo_win32_printing_surface_check_png(cairo_win32_printing_surface_t * surface,cairo_surface_t * source,const unsigned char ** data,unsigned long * length,cairo_image_info_t * info)725 _cairo_win32_printing_surface_check_png (cairo_win32_printing_surface_t   *surface,
726 					 cairo_surface_t         *source,
727 					 const unsigned char    **data,
728 					 unsigned long           *length,
729 					 cairo_image_info_t      *info)
730 {
731     const unsigned char *mime_data;
732     unsigned long mime_data_length;
733 
734     cairo_int_status_t status;
735     DWORD result;
736 
737     if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_CHECK_PNG))
738 	return CAIRO_INT_STATUS_UNSUPPORTED;
739 
740     cairo_surface_get_mime_data (source, CAIRO_MIME_TYPE_PNG,
741 				 &mime_data, &mime_data_length);
742     if (mime_data == NULL)
743 	return CAIRO_INT_STATUS_UNSUPPORTED;
744 
745     status = _cairo_image_info_get_png_info (info, mime_data, mime_data_length);
746     if (status)
747 	return status;
748 
749     result = 0;
750     if (ExtEscape(surface->win32.dc, CHECKPNGFORMAT, mime_data_length, (char *) mime_data,
751 		  sizeof(result), (char *) &result) <= 0)
752 	return CAIRO_INT_STATUS_UNSUPPORTED;
753 
754     if (result != 1)
755 	return CAIRO_INT_STATUS_UNSUPPORTED;
756 
757     *data = mime_data;
758     *length = mime_data_length;
759 
760     return CAIRO_STATUS_SUCCESS;
761 }
762 
763 static cairo_status_t
_cairo_win32_printing_surface_paint_image_pattern(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents)764 _cairo_win32_printing_surface_paint_image_pattern (cairo_win32_printing_surface_t *surface,
765 						   const cairo_pattern_t          *pattern,
766 						   const cairo_rectangle_int_t    *extents)
767 {
768     cairo_int_status_t status;
769     cairo_surface_pattern_t image_pattern;
770     cairo_image_surface_t *image;
771     void *image_extra;
772     cairo_image_surface_t *opaque_image = NULL;
773     BITMAPINFO bi;
774     cairo_matrix_t m;
775     int oldmode;
776     XFORM xform;
777     int x_tile, y_tile, left, right, top, bottom;
778     int pattern_width, pattern_height;
779     RECT clip;
780     const cairo_color_t *background_color;
781     const unsigned char *mime_data;
782     unsigned long mime_size;
783     cairo_image_info_t mime_info;
784     cairo_bool_t use_mime;
785     DWORD mime_type;
786 
787     /* If we can't use StretchDIBits with this surface, we can't do anything
788      * here.
789      */
790     if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_STRETCHDIB))
791 	return CAIRO_INT_STATUS_UNSUPPORTED;
792 
793     if (surface->content == CAIRO_CONTENT_COLOR_ALPHA)
794 	background_color = CAIRO_COLOR_WHITE;
795     else
796 	background_color = CAIRO_COLOR_BLACK;
797 
798     status = _cairo_win32_printing_surface_acquire_image_pattern (surface,
799 								  pattern,
800 								  extents,
801 								  &image_pattern,
802 								  &pattern_width,
803 								  &pattern_height,
804 								  &image_extra);
805     if (status)
806 	return status;
807 
808     image = (cairo_image_surface_t *)(image_pattern.surface);
809     if (image->base.status) {
810 	status = image->base.status;
811 	goto CLEANUP_IMAGE;
812     }
813 
814     if (image->width == 0 || image->height == 0) {
815 	status = CAIRO_STATUS_SUCCESS;
816 	goto CLEANUP_IMAGE;
817     }
818 
819     mime_type = BI_JPEG;
820     status = _cairo_win32_printing_surface_check_jpeg (surface,
821 						       image_pattern.surface,
822 						       &mime_data,
823 						       &mime_size,
824 						       &mime_info);
825     if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
826 	mime_type = BI_PNG;
827 	status = _cairo_win32_printing_surface_check_png (surface,
828 							  image_pattern.surface,
829 							  &mime_data,
830 							  &mime_size,
831 							  &mime_info);
832     }
833     if (_cairo_int_status_is_error (status))
834 	return status;
835 
836     use_mime = (status == CAIRO_INT_STATUS_SUCCESS);
837 
838     if (!use_mime && image->format != CAIRO_FORMAT_RGB24) {
839 	cairo_surface_t *opaque_surface;
840 	cairo_surface_pattern_t image_pattern;
841 	cairo_solid_pattern_t background_pattern;
842 
843 	opaque_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
844 						     image->width,
845 						     image->height);
846 	if (opaque_surface->status) {
847 	    status = opaque_surface->status;
848 	    goto CLEANUP_OPAQUE_IMAGE;
849 	}
850 
851 	_cairo_pattern_init_solid (&background_pattern,
852 				   background_color);
853 	status = _cairo_surface_paint (opaque_surface,
854 				       CAIRO_OPERATOR_SOURCE,
855 				       &background_pattern.base,
856 				       NULL);
857 	if (status)
858 	    goto CLEANUP_OPAQUE_IMAGE;
859 
860 	_cairo_pattern_init_for_surface (&image_pattern, &image->base);
861 	status = _cairo_surface_paint (opaque_surface,
862 				       CAIRO_OPERATOR_OVER,
863 				       &image_pattern.base,
864 				       NULL);
865 	_cairo_pattern_fini (&image_pattern.base);
866 	if (status)
867 	    goto CLEANUP_OPAQUE_IMAGE;
868 
869 	opaque_image = (cairo_image_surface_t *) opaque_surface;
870     } else {
871 	opaque_image = image;
872     }
873 
874     bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
875     bi.bmiHeader.biWidth = use_mime ? mime_info.width : opaque_image->width;
876     bi.bmiHeader.biHeight = use_mime ? - mime_info.height : -opaque_image->height;
877     bi.bmiHeader.biSizeImage = use_mime ? mime_size : 0;
878     bi.bmiHeader.biXPelsPerMeter = PELS_72DPI;
879     bi.bmiHeader.biYPelsPerMeter = PELS_72DPI;
880     bi.bmiHeader.biPlanes = 1;
881     bi.bmiHeader.biBitCount = 32;
882     bi.bmiHeader.biCompression = use_mime ? mime_type : BI_RGB;
883     bi.bmiHeader.biClrUsed = 0;
884     bi.bmiHeader.biClrImportant = 0;
885 
886     m = image_pattern.base.matrix;
887     status = cairo_matrix_invert (&m);
888     /* _cairo_pattern_set_matrix guarantees invertibility */
889     assert (status == CAIRO_INT_STATUS_SUCCESS);
890 
891     cairo_matrix_multiply (&m, &m, &surface->ctm);
892     cairo_matrix_multiply (&m, &m, &surface->gdi_ctm);
893     SaveDC (surface->win32.dc);
894     _cairo_matrix_to_win32_xform (&m, &xform);
895 
896     if (! SetWorldTransform (surface->win32.dc, &xform)) {
897 	status = _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_paint_image_pattern");
898 	goto CLEANUP_OPAQUE_IMAGE;
899     }
900 
901     oldmode = SetStretchBltMode(surface->win32.dc, HALFTONE);
902 
903     GetClipBox (surface->win32.dc, &clip);
904     if (pattern->extend == CAIRO_EXTEND_REPEAT || pattern->extend == CAIRO_EXTEND_REFLECT) {
905 	left = floor ( clip.left / (double) opaque_image->width);
906 	right = ceil (clip.right / (double) opaque_image->width);
907 	top = floor (clip.top / (double) opaque_image->height);
908 	bottom = ceil (clip.bottom / (double) opaque_image->height);
909     } else {
910 	left = 0;
911 	right = 1;
912 	top = 0;
913 	bottom = 1;
914     }
915 
916     for (y_tile = top; y_tile < bottom; y_tile++) {
917 	for (x_tile = left; x_tile < right; x_tile++) {
918 	    if (!StretchDIBits (surface->win32.dc,
919 				x_tile*opaque_image->width,
920 				y_tile*opaque_image->height,
921 				opaque_image->width,
922 				opaque_image->height,
923 				0,
924 				0,
925 				use_mime ? mime_info.width : opaque_image->width,
926 				use_mime ? mime_info.height : opaque_image->height,
927 				use_mime ? mime_data : opaque_image->data,
928 				&bi,
929 				DIB_RGB_COLORS,
930 				SRCCOPY))
931 	    {
932 		status = _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_paint(StretchDIBits)");
933 		goto CLEANUP_OPAQUE_IMAGE;
934 	    }
935 	}
936     }
937     SetStretchBltMode(surface->win32.dc, oldmode);
938     RestoreDC (surface->win32.dc, -1);
939 
940 CLEANUP_OPAQUE_IMAGE:
941     if (opaque_image != image)
942 	cairo_surface_destroy (&opaque_image->base);
943 CLEANUP_IMAGE:
944     _cairo_win32_printing_surface_release_image_pattern (surface, pattern, &image_pattern, image_extra);
945 
946     return status;
947 }
948 
949 static void
vertex_set_color(TRIVERTEX * vert,cairo_color_stop_t * color)950 vertex_set_color (TRIVERTEX *vert, cairo_color_stop_t *color)
951 {
952     /* MSDN says that the range here is 0x0000 .. 0xff00;
953      * that may well be a typo, but just chop the low bits
954      * here. */
955     vert->Alpha = 0xff00;
956     vert->Red   = color->red_short & 0xff00;
957     vert->Green = color->green_short & 0xff00;
958     vert->Blue  = color->blue_short & 0xff00;
959 }
960 
961 static cairo_int_status_t
_cairo_win32_printing_surface_paint_linear_pattern(cairo_win32_printing_surface_t * surface,cairo_linear_pattern_t * pattern)962 _cairo_win32_printing_surface_paint_linear_pattern (cairo_win32_printing_surface_t *surface,
963                                                     cairo_linear_pattern_t *pattern)
964 {
965     TRIVERTEX *vert;
966     GRADIENT_RECT *rect;
967     RECT clip;
968     XFORM xform;
969     int i, num_stops;
970     cairo_matrix_t mat, rot;
971     double p1x, p1y, p2x, p2y, xd, yd, d, sn, cs;
972     cairo_extend_t extend;
973     int range_start, range_stop, num_ranges, num_rects, stop;
974     int total_verts, total_rects;
975     cairo_status_t status;
976 
977     extend = cairo_pattern_get_extend (&pattern->base.base);
978     SaveDC (surface->win32.dc);
979 
980     mat = pattern->base.base.matrix;
981     status = cairo_matrix_invert (&mat);
982     /* _cairo_pattern_set_matrix guarantees invertibility */
983     assert (status == CAIRO_STATUS_SUCCESS);
984 
985     cairo_matrix_multiply (&mat, &surface->ctm, &mat);
986 
987     p1x = pattern->pd1.x;
988     p1y = pattern->pd1.y;
989     p2x = pattern->pd2.x;
990     p2y = pattern->pd2.y;
991     cairo_matrix_translate (&mat, p1x, p1y);
992 
993     xd = p2x - p1x;
994     yd = p2y - p1y;
995     d = sqrt (xd*xd + yd*yd);
996     sn = yd/d;
997     cs = xd/d;
998     cairo_matrix_init (&rot,
999 		       cs, sn,
1000 		       -sn, cs,
1001 		        0, 0);
1002     cairo_matrix_multiply (&mat, &rot, &mat);
1003 
1004     _cairo_matrix_to_win32_xform (&mat, &xform);
1005 
1006     if (!SetWorldTransform (surface->win32.dc, &xform))
1007 	return _cairo_win32_print_gdi_error ("_win32_printing_surface_paint_linear_pattern:SetWorldTransform2");
1008 
1009     GetClipBox (surface->win32.dc, &clip);
1010 
1011     if (extend == CAIRO_EXTEND_REPEAT || extend == CAIRO_EXTEND_REFLECT) {
1012 	range_start = floor (clip.left / d);
1013 	range_stop = ceil (clip.right / d);
1014     } else {
1015 	range_start = 0;
1016 	range_stop = 1;
1017     }
1018     num_ranges = range_stop - range_start;
1019     num_stops = pattern->base.n_stops;
1020     num_rects = num_stops - 1;
1021 
1022     /* Add an extra four points and two rectangles for EXTEND_PAD */
1023     vert = _cairo_malloc (sizeof (TRIVERTEX) * (num_rects*2*num_ranges + 4));
1024     rect = _cairo_malloc (sizeof (GRADIENT_RECT) * (num_rects*num_ranges + 2));
1025 
1026     for (i = 0; i < num_ranges*num_rects; i++) {
1027 	vert[i*2].y = (LONG) clip.top;
1028 	if (i%num_rects == 0) {
1029 	    stop = 0;
1030 	    if (extend == CAIRO_EXTEND_REFLECT && (range_start+(i/num_rects))%2)
1031 		stop = num_rects;
1032 	    vert[i*2].x = (LONG)(d*(range_start + i/num_rects));
1033 	    vertex_set_color (&vert[i*2], &pattern->base.stops[stop].color);
1034 	} else {
1035 	    vert[i*2].x = vert[i*2-1].x;
1036 	    vert[i*2].Red = vert[i*2-1].Red;
1037 	    vert[i*2].Green = vert[i*2-1].Green;
1038 	    vert[i*2].Blue = vert[i*2-1].Blue;
1039 	    vert[i*2].Alpha = vert[i*2-1].Alpha;
1040 	}
1041 
1042 	stop = i%num_rects + 1;
1043 	vert[i*2+1].x = (LONG)(d*(range_start + i/num_rects + pattern->base.stops[stop].offset));
1044 	vert[i*2+1].y = (LONG) clip.bottom;
1045 	if (extend == CAIRO_EXTEND_REFLECT && (range_start+(i/num_rects))%2)
1046 	    stop = num_rects - stop;
1047 	vertex_set_color (&vert[i*2+1], &pattern->base.stops[stop].color);
1048 
1049 	rect[i].UpperLeft = i*2;
1050 	rect[i].LowerRight = i*2 + 1;
1051     }
1052     total_verts = 2*num_ranges*num_rects;
1053     total_rects = num_ranges*num_rects;
1054 
1055     if (extend == CAIRO_EXTEND_PAD) {
1056 	vert[i*2].x = vert[i*2-1].x;
1057 	vert[i*2].y = (LONG) clip.top;
1058 	vert[i*2].Red = vert[i*2-1].Red;
1059 	vert[i*2].Green = vert[i*2-1].Green;
1060 	vert[i*2].Blue = vert[i*2-1].Blue;
1061 	vert[i*2].Alpha = 0xff00;
1062 	vert[i*2+1].x = clip.right;
1063 	vert[i*2+1].y = (LONG) clip.bottom;
1064 	vert[i*2+1].Red = vert[i*2-1].Red;
1065 	vert[i*2+1].Green = vert[i*2-1].Green;
1066 	vert[i*2+1].Blue = vert[i*2-1].Blue;
1067 	vert[i*2+1].Alpha = 0xff00;
1068 	rect[i].UpperLeft = i*2;
1069 	rect[i].LowerRight = i*2 + 1;
1070 
1071 	i++;
1072 
1073 	vert[i*2].x = clip.left;
1074 	vert[i*2].y = (LONG) clip.top;
1075 	vert[i*2].Red = vert[0].Red;
1076 	vert[i*2].Green = vert[0].Green;
1077 	vert[i*2].Blue = vert[0].Blue;
1078 	vert[i*2].Alpha = 0xff00;
1079 	vert[i*2+1].x = vert[0].x;
1080 	vert[i*2+1].y = (LONG) clip.bottom;
1081 	vert[i*2+1].Red = vert[0].Red;
1082 	vert[i*2+1].Green = vert[0].Green;
1083 	vert[i*2+1].Blue = vert[0].Blue;
1084 	vert[i*2+1].Alpha = 0xff00;
1085 	rect[i].UpperLeft = i*2;
1086 	rect[i].LowerRight = i*2 + 1;
1087 
1088 	total_verts += 4;
1089 	total_rects += 2;
1090     }
1091 
1092     if (!GradientFill (surface->win32.dc,
1093 		       vert, total_verts,
1094 		       rect, total_rects,
1095 		       GRADIENT_FILL_RECT_H))
1096 	return _cairo_win32_print_gdi_error ("_win32_printing_surface_paint_linear_pattern:GradientFill");
1097 
1098     free (rect);
1099     free (vert);
1100     RestoreDC (surface->win32.dc, -1);
1101 
1102     return 0;
1103 }
1104 
1105 static cairo_int_status_t
_cairo_win32_printing_surface_paint_pattern(cairo_win32_printing_surface_t * surface,const cairo_pattern_t * pattern,const cairo_rectangle_int_t * extents)1106 _cairo_win32_printing_surface_paint_pattern (cairo_win32_printing_surface_t *surface,
1107                                              const cairo_pattern_t          *pattern,
1108 					     const cairo_rectangle_int_t    *extents)
1109 {
1110     cairo_status_t status;
1111 
1112     switch (pattern->type) {
1113     case CAIRO_PATTERN_TYPE_SOLID:
1114 	status = _cairo_win32_printing_surface_paint_solid_pattern (surface, pattern);
1115 	if (status)
1116 	    return status;
1117 	break;
1118 
1119     case CAIRO_PATTERN_TYPE_SURFACE: {
1120 	cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern;
1121 
1122 	if ( _cairo_surface_is_recording (surface_pattern->surface))
1123 	    status = _cairo_win32_printing_surface_paint_recording_pattern (surface, surface_pattern);
1124 	else
1125 	    status = _cairo_win32_printing_surface_paint_image_pattern (surface, pattern, extents);
1126 
1127 	if (status)
1128 	    return status;
1129 	break;
1130     }
1131     case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
1132 	status = _cairo_win32_printing_surface_paint_image_pattern (surface, pattern, extents);
1133 	if (status)
1134 	    return status;
1135 	break;
1136 
1137     case CAIRO_PATTERN_TYPE_LINEAR:
1138 	status = _cairo_win32_printing_surface_paint_linear_pattern (surface, (cairo_linear_pattern_t *) pattern);
1139 	if (status)
1140 	    return status;
1141 	break;
1142 
1143     case CAIRO_PATTERN_TYPE_RADIAL:
1144 	return CAIRO_INT_STATUS_UNSUPPORTED;
1145 	break;
1146 
1147     case CAIRO_PATTERN_TYPE_MESH:
1148 	ASSERT_NOT_REACHED;
1149     }
1150 
1151     return CAIRO_STATUS_SUCCESS;
1152 }
1153 
1154 typedef struct _win32_print_path_info {
1155     cairo_win32_printing_surface_t *surface;
1156 } win32_path_info_t;
1157 
1158 static cairo_status_t
_cairo_win32_printing_surface_path_move_to(void * closure,const cairo_point_t * point)1159 _cairo_win32_printing_surface_path_move_to (void *closure,
1160 					    const cairo_point_t *point)
1161 {
1162     win32_path_info_t *path_info = closure;
1163 
1164     if (path_info->surface->has_ctm) {
1165 	double x, y;
1166 
1167 	x = _cairo_fixed_to_double (point->x);
1168 	y = _cairo_fixed_to_double (point->y);
1169 	cairo_matrix_transform_point (&path_info->surface->ctm, &x, &y);
1170 	MoveToEx (path_info->surface->win32.dc, (int) x, (int) y, NULL);
1171     } else {
1172 	MoveToEx (path_info->surface->win32.dc,
1173 		  _cairo_fixed_integer_part (point->x),
1174 		  _cairo_fixed_integer_part (point->y),
1175 		  NULL);
1176     }
1177 
1178     return CAIRO_STATUS_SUCCESS;
1179 }
1180 
1181 static cairo_status_t
_cairo_win32_printing_surface_path_line_to(void * closure,const cairo_point_t * point)1182 _cairo_win32_printing_surface_path_line_to (void *closure,
1183 					    const cairo_point_t *point)
1184 {
1185     win32_path_info_t *path_info = closure;
1186 
1187     path_info->surface->path_empty = FALSE;
1188     if (path_info->surface->has_ctm) {
1189 	double x, y;
1190 
1191 	x = _cairo_fixed_to_double (point->x);
1192 	y = _cairo_fixed_to_double (point->y);
1193 	cairo_matrix_transform_point (&path_info->surface->ctm, &x, &y);
1194 	LineTo (path_info->surface->win32.dc, (int) x, (int) y);
1195     } else {
1196 	LineTo (path_info->surface->win32.dc,
1197 		_cairo_fixed_integer_part (point->x),
1198 		_cairo_fixed_integer_part (point->y));
1199     }
1200 
1201     return CAIRO_STATUS_SUCCESS;
1202 }
1203 
1204 static cairo_status_t
_cairo_win32_printing_surface_path_curve_to(void * closure,const cairo_point_t * b,const cairo_point_t * c,const cairo_point_t * d)1205 _cairo_win32_printing_surface_path_curve_to (void          *closure,
1206                                              const cairo_point_t *b,
1207                                              const cairo_point_t *c,
1208                                              const cairo_point_t *d)
1209 {
1210     win32_path_info_t *path_info = closure;
1211     POINT points[3];
1212 
1213     path_info->surface->path_empty = FALSE;
1214     if (path_info->surface->has_ctm) {
1215 	double x, y;
1216 
1217 	x = _cairo_fixed_to_double (b->x);
1218 	y = _cairo_fixed_to_double (b->y);
1219 	cairo_matrix_transform_point (&path_info->surface->ctm, &x, &y);
1220 	points[0].x = (LONG) x;
1221 	points[0].y = (LONG) y;
1222 
1223 	x = _cairo_fixed_to_double (c->x);
1224 	y = _cairo_fixed_to_double (c->y);
1225 	cairo_matrix_transform_point (&path_info->surface->ctm, &x, &y);
1226 	points[1].x = (LONG) x;
1227 	points[1].y = (LONG) y;
1228 
1229 	x = _cairo_fixed_to_double (d->x);
1230 	y = _cairo_fixed_to_double (d->y);
1231 	cairo_matrix_transform_point (&path_info->surface->ctm, &x, &y);
1232 	points[2].x = (LONG) x;
1233 	points[2].y = (LONG) y;
1234     } else {
1235 	points[0].x = _cairo_fixed_integer_part (b->x);
1236 	points[0].y = _cairo_fixed_integer_part (b->y);
1237 	points[1].x = _cairo_fixed_integer_part (c->x);
1238 	points[1].y = _cairo_fixed_integer_part (c->y);
1239 	points[2].x = _cairo_fixed_integer_part (d->x);
1240 	points[2].y = _cairo_fixed_integer_part (d->y);
1241     }
1242     PolyBezierTo (path_info->surface->win32.dc, points, 3);
1243 
1244     return CAIRO_STATUS_SUCCESS;
1245 }
1246 
1247 static cairo_status_t
_cairo_win32_printing_surface_path_close_path(void * closure)1248 _cairo_win32_printing_surface_path_close_path (void *closure)
1249 {
1250     win32_path_info_t *path_info = closure;
1251 
1252     CloseFigure (path_info->surface->win32.dc);
1253 
1254     return CAIRO_STATUS_SUCCESS;
1255 }
1256 
1257 static cairo_status_t
_cairo_win32_printing_surface_emit_path(cairo_win32_printing_surface_t * surface,const cairo_path_fixed_t * path)1258 _cairo_win32_printing_surface_emit_path (cairo_win32_printing_surface_t    *surface,
1259 					 const cairo_path_fixed_t *path)
1260 {
1261     win32_path_info_t path_info;
1262 
1263     path_info.surface = surface;
1264     return _cairo_path_fixed_interpret (path,
1265 					_cairo_win32_printing_surface_path_move_to,
1266 					_cairo_win32_printing_surface_path_line_to,
1267 					_cairo_win32_printing_surface_path_curve_to,
1268 					_cairo_win32_printing_surface_path_close_path,
1269 					&path_info);
1270 }
1271 
1272 static cairo_int_status_t
_cairo_win32_printing_surface_show_page(void * abstract_surface)1273 _cairo_win32_printing_surface_show_page (void *abstract_surface)
1274 {
1275     cairo_win32_printing_surface_t *surface = abstract_surface;
1276 
1277     /* Undo both SaveDC's that we did in start_page */
1278     RestoreDC (surface->win32.dc, -2);
1279 
1280     /* Invalidate extents since the size of the next page is not known at
1281      * this point.
1282      */
1283     surface->extents_valid = FALSE;
1284 
1285     return CAIRO_STATUS_SUCCESS;
1286 }
1287 
1288 static cairo_status_t
_cairo_win32_printing_surface_clipper_intersect_clip_path(cairo_surface_clipper_t * clipper,cairo_path_fixed_t * path,cairo_fill_rule_t fill_rule,double tolerance,cairo_antialias_t antialias)1289 _cairo_win32_printing_surface_clipper_intersect_clip_path (cairo_surface_clipper_t *clipper,
1290                                                    cairo_path_fixed_t *path,
1291                                                    cairo_fill_rule_t   fill_rule,
1292                                                    double	       tolerance,
1293                                                    cairo_antialias_t   antialias)
1294 {
1295     cairo_win32_printing_surface_t *surface = cairo_container_of (clipper,
1296 							 cairo_win32_printing_surface_t,
1297 							 clipper);
1298     cairo_status_t status;
1299 
1300     if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE)
1301 	return CAIRO_STATUS_SUCCESS;
1302 
1303     if (path == NULL) {
1304 	RestoreDC (surface->win32.dc, -1);
1305 	SaveDC (surface->win32.dc);
1306 
1307 	return CAIRO_STATUS_SUCCESS;
1308     }
1309 
1310     BeginPath (surface->win32.dc);
1311     status = _cairo_win32_printing_surface_emit_path (surface, path);
1312     EndPath (surface->win32.dc);
1313 
1314     switch (fill_rule) {
1315     case CAIRO_FILL_RULE_WINDING:
1316 	SetPolyFillMode (surface->win32.dc, WINDING);
1317 	break;
1318     case CAIRO_FILL_RULE_EVEN_ODD:
1319 	SetPolyFillMode (surface->win32.dc, ALTERNATE);
1320 	break;
1321     default:
1322 	ASSERT_NOT_REACHED;
1323     }
1324 
1325     SelectClipPath (surface->win32.dc, RGN_AND);
1326 
1327     return status;
1328 }
1329 
1330 static cairo_bool_t
_cairo_win32_printing_surface_get_extents(void * abstract_surface,cairo_rectangle_int_t * rectangle)1331 _cairo_win32_printing_surface_get_extents (void		          *abstract_surface,
1332 					   cairo_rectangle_int_t  *rectangle)
1333 {
1334     cairo_win32_printing_surface_t *surface = abstract_surface;
1335 
1336     if (surface->extents_valid)
1337 	*rectangle = surface->win32.extents;
1338 
1339     return surface->extents_valid;
1340 }
1341 
1342 static void
_cairo_win32_printing_surface_get_font_options(void * abstract_surface,cairo_font_options_t * options)1343 _cairo_win32_printing_surface_get_font_options (void                  *abstract_surface,
1344                                                 cairo_font_options_t  *options)
1345 {
1346     _cairo_font_options_init_default (options);
1347 
1348     cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
1349     cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
1350     cairo_font_options_set_antialias (options, CAIRO_ANTIALIAS_GRAY);
1351     _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
1352 }
1353 
1354 static cairo_int_status_t
_cairo_win32_printing_surface_paint(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_clip_t * clip)1355 _cairo_win32_printing_surface_paint (void			*abstract_surface,
1356                                      cairo_operator_t		 op,
1357                                      const cairo_pattern_t	*source,
1358 				     const cairo_clip_t      *clip)
1359 {
1360     cairo_win32_printing_surface_t *surface = abstract_surface;
1361     cairo_solid_pattern_t clear;
1362     cairo_composite_rectangles_t extents;
1363     cairo_status_t status;
1364 
1365     status = _cairo_composite_rectangles_init_for_paint (&extents,
1366 							 &surface->win32.base,
1367 							 op, source, clip);
1368     if (unlikely (status))
1369 	return status;
1370 
1371     status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
1372     if (unlikely (status))
1373 	goto cleanup_composite;
1374 
1375     if (op == CAIRO_OPERATOR_CLEAR) {
1376 	_cairo_win32_printing_surface_init_clear_color (surface, &clear);
1377 	source = (cairo_pattern_t*) &clear;
1378 	op = CAIRO_OPERATOR_SOURCE;
1379     }
1380 
1381     if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE) {
1382 	status = _cairo_win32_printing_surface_analyze_operation (surface, op, source, &extents.bounded);
1383 	goto cleanup_composite;
1384     }
1385 
1386     assert (_cairo_win32_printing_surface_operation_supported (surface, op, source, &extents.bounded));
1387 
1388     status = _cairo_win32_printing_surface_paint_pattern (surface, source, &extents.bounded);
1389 
1390   cleanup_composite:
1391     _cairo_composite_rectangles_fini (&extents);
1392     return status;
1393 }
1394 
1395 static int
_cairo_win32_line_cap(cairo_line_cap_t cap)1396 _cairo_win32_line_cap (cairo_line_cap_t cap)
1397 {
1398     switch (cap) {
1399     case CAIRO_LINE_CAP_BUTT:
1400 	return PS_ENDCAP_FLAT;
1401     case CAIRO_LINE_CAP_ROUND:
1402 	return PS_ENDCAP_ROUND;
1403     case CAIRO_LINE_CAP_SQUARE:
1404 	return PS_ENDCAP_SQUARE;
1405     default:
1406 	ASSERT_NOT_REACHED;
1407 	return 0;
1408     }
1409 }
1410 
1411 static int
_cairo_win32_line_join(cairo_line_join_t join)1412 _cairo_win32_line_join (cairo_line_join_t join)
1413 {
1414     switch (join) {
1415     case CAIRO_LINE_JOIN_MITER:
1416 	return PS_JOIN_MITER;
1417     case CAIRO_LINE_JOIN_ROUND:
1418 	return PS_JOIN_ROUND;
1419     case CAIRO_LINE_JOIN_BEVEL:
1420 	return PS_JOIN_BEVEL;
1421     default:
1422 	ASSERT_NOT_REACHED;
1423 	return 0;
1424     }
1425 }
1426 
1427 static void
_cairo_matrix_factor_out_scale(cairo_matrix_t * m,double * scale)1428 _cairo_matrix_factor_out_scale (cairo_matrix_t *m, double *scale)
1429 {
1430     double s;
1431 
1432     s = fabs (m->xx);
1433     if (fabs (m->xy) > s)
1434 	s = fabs (m->xy);
1435     if (fabs (m->yx) > s)
1436 	s = fabs (m->yx);
1437     if (fabs (m->yy) > s)
1438 	s = fabs (m->yy);
1439     *scale = s;
1440     s = 1.0/s;
1441     cairo_matrix_scale (m, s, s);
1442 }
1443 
1444 static cairo_int_status_t
_cairo_win32_printing_surface_stroke(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_path_fixed_t * path,const cairo_stroke_style_t * style,const cairo_matrix_t * stroke_ctm,const cairo_matrix_t * stroke_ctm_inverse,double tolerance,cairo_antialias_t antialias,const cairo_clip_t * clip)1445 _cairo_win32_printing_surface_stroke (void			*abstract_surface,
1446                                       cairo_operator_t		 op,
1447                                       const cairo_pattern_t	*source,
1448                                       const cairo_path_fixed_t	*path,
1449                                       const cairo_stroke_style_t *style,
1450                                       const cairo_matrix_t	*stroke_ctm,
1451                                       const cairo_matrix_t	*stroke_ctm_inverse,
1452                                       double			tolerance,
1453                                       cairo_antialias_t		antialias,
1454 				      const cairo_clip_t    *clip)
1455 {
1456     cairo_win32_printing_surface_t *surface = abstract_surface;
1457     cairo_int_status_t status;
1458     HPEN pen;
1459     LOGBRUSH brush;
1460     COLORREF color;
1461     XFORM xform;
1462     DWORD pen_style;
1463     DWORD *dash_array;
1464     HGDIOBJ obj;
1465     unsigned int i;
1466     cairo_solid_pattern_t clear;
1467     cairo_matrix_t mat;
1468     double scale;
1469     cairo_composite_rectangles_t extents;
1470 
1471     status = _cairo_composite_rectangles_init_for_stroke (&extents,
1472 							  &surface->win32.base,
1473 							  op, source,
1474 							  path, style, stroke_ctm,
1475 							  clip);
1476     if (unlikely (status))
1477 	return status;
1478 
1479     /* use the more accurate extents */
1480     {
1481 	cairo_rectangle_int_t r;
1482 	cairo_box_t b;
1483 
1484 	status = _cairo_path_fixed_stroke_extents (path, style,
1485 						   stroke_ctm, stroke_ctm_inverse,
1486 						   tolerance,
1487 						   &r);
1488 	if (unlikely (status))
1489 	    goto cleanup_composite;
1490 
1491 	_cairo_box_from_rectangle (&b, &r);
1492 	status = _cairo_composite_rectangles_intersect_mask_extents (&extents, &b);
1493 	if (unlikely (status))
1494 	    goto cleanup_composite;
1495     }
1496 
1497     status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
1498     if (unlikely (status))
1499 	goto cleanup_composite;
1500 
1501     if (op == CAIRO_OPERATOR_CLEAR) {
1502 	_cairo_win32_printing_surface_init_clear_color (surface, &clear);
1503 	source = (cairo_pattern_t*) &clear;
1504 	op = CAIRO_OPERATOR_SOURCE;
1505     }
1506 
1507     if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE) {
1508 	/* Win32 does not support a dash offset. */
1509 	if (style->num_dashes > 0 && style->dash_offset != 0.0)
1510 	    status = CAIRO_INT_STATUS_UNSUPPORTED;
1511 	else
1512 	    status = _cairo_win32_printing_surface_analyze_operation (surface, op, source, &extents.bounded);
1513 
1514 	goto cleanup_composite;
1515     }
1516 
1517     assert (_cairo_win32_printing_surface_operation_supported (surface, op, source, &extents.bounded));
1518     assert (!(style->num_dashes > 0 && style->dash_offset != 0.0));
1519 
1520     cairo_matrix_multiply (&mat, stroke_ctm, &surface->ctm);
1521     _cairo_matrix_factor_out_scale (&mat, &scale);
1522 
1523     pen_style = PS_GEOMETRIC;
1524     dash_array = NULL;
1525     if (style->num_dashes) {
1526 	pen_style |= PS_USERSTYLE;
1527 	dash_array = calloc (sizeof (DWORD), style->num_dashes);
1528 	for (i = 0; i < style->num_dashes; i++) {
1529 	    dash_array[i] = (DWORD) (scale * style->dash[i]);
1530 	}
1531     } else {
1532 	pen_style |= PS_SOLID;
1533     }
1534 
1535     SetMiterLimit (surface->win32.dc, (FLOAT) (style->miter_limit), NULL);
1536     if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1537 	cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) source;
1538 
1539 
1540 	color = _cairo_win32_printing_surface_flatten_transparency (surface,
1541 								    &solid->color);
1542     } else {
1543 	/* Color not used as the pen will only be used by WidenPath() */
1544 	color = RGB (0,0,0);
1545     }
1546     brush.lbStyle = BS_SOLID;
1547     brush.lbColor = color;
1548     brush.lbHatch = 0;
1549     pen_style |= _cairo_win32_line_cap (style->line_cap);
1550     pen_style |= _cairo_win32_line_join (style->line_join);
1551     pen = ExtCreatePen(pen_style,
1552 		       scale * style->line_width,
1553 		       &brush,
1554 		       style->num_dashes,
1555 		       dash_array);
1556     if (pen == NULL) {
1557 	status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:ExtCreatePen");
1558 	goto cleanup_composite;
1559     }
1560 
1561     obj = SelectObject (surface->win32.dc, pen);
1562     if (obj == NULL) {
1563 	status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:SelectObject");
1564 	goto cleanup_composite;
1565     }
1566 
1567     BeginPath (surface->win32.dc);
1568     status = _cairo_win32_printing_surface_emit_path (surface, path);
1569     EndPath (surface->win32.dc);
1570     if (unlikely (status))
1571 	goto cleanup_composite;
1572 
1573     /*
1574      * Switch to user space to set line parameters
1575      */
1576     SaveDC (surface->win32.dc);
1577 
1578     _cairo_matrix_to_win32_xform (&mat, &xform);
1579     xform.eDx = 0.0f;
1580     xform.eDy = 0.0f;
1581 
1582     if (!ModifyWorldTransform (surface->win32.dc, &xform, MWT_LEFTMULTIPLY)) {
1583 	status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:SetWorldTransform");
1584 	goto cleanup_composite;
1585     }
1586 
1587     if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1588 	StrokePath (surface->win32.dc);
1589     } else {
1590 	if (!WidenPath (surface->win32.dc)) {
1591 	    status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:WidenPath");
1592 	    goto cleanup_composite;
1593 	}
1594 	if (!SelectClipPath (surface->win32.dc, RGN_AND)) {
1595 	    status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:SelectClipPath");
1596 	    goto cleanup_composite;
1597 	}
1598 
1599 	/* Return to device space to paint the pattern */
1600 	_cairo_matrix_to_win32_xform (&surface->gdi_ctm, &xform);
1601 	if (!SetWorldTransform (surface->win32.dc, &xform)) {
1602 	    status = _cairo_win32_print_gdi_error ("_win32_surface_stroke:ModifyWorldTransform");
1603 	    goto cleanup_composite;
1604 	}
1605 	status = _cairo_win32_printing_surface_paint_pattern (surface, source, &extents.bounded);
1606     }
1607     RestoreDC (surface->win32.dc, -1);
1608     DeleteObject (pen);
1609     free (dash_array);
1610 
1611 cleanup_composite:
1612     _cairo_composite_rectangles_fini (&extents);
1613     return status;
1614 }
1615 
1616 static cairo_int_status_t
_cairo_win32_printing_surface_fill(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_path_fixed_t * path,cairo_fill_rule_t fill_rule,double tolerance,cairo_antialias_t antialias,const cairo_clip_t * clip)1617 _cairo_win32_printing_surface_fill (void		        *abstract_surface,
1618 				    cairo_operator_t		 op,
1619 				    const cairo_pattern_t	*source,
1620 				    const cairo_path_fixed_t	*path,
1621 				    cairo_fill_rule_t		 fill_rule,
1622 				    double			 tolerance,
1623 				    cairo_antialias_t		 antialias,
1624 				    const cairo_clip_t		*clip)
1625 {
1626     cairo_win32_printing_surface_t *surface = abstract_surface;
1627     cairo_int_status_t status;
1628     cairo_solid_pattern_t clear;
1629     cairo_composite_rectangles_t extents;
1630 
1631     status = _cairo_composite_rectangles_init_for_fill (&extents,
1632 							&surface->win32.base,
1633 							op, source, path,
1634 							clip);
1635     if (unlikely (status))
1636 	return status;
1637 
1638     /* use the more accurate extents */
1639     {
1640 	cairo_rectangle_int_t r;
1641 	cairo_box_t b;
1642 
1643 	_cairo_path_fixed_fill_extents (path,
1644 					fill_rule,
1645 					tolerance,
1646 					&r);
1647 
1648 	_cairo_box_from_rectangle (&b, &r);
1649 	status = _cairo_composite_rectangles_intersect_mask_extents (&extents, &b);
1650 	if (unlikely (status))
1651 	    goto cleanup_composite;
1652     }
1653 
1654     status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
1655     if (unlikely (status))
1656 	goto cleanup_composite;
1657 
1658     if (op == CAIRO_OPERATOR_CLEAR) {
1659 	_cairo_win32_printing_surface_init_clear_color (surface, &clear);
1660 	source = (cairo_pattern_t*) &clear;
1661 	op = CAIRO_OPERATOR_SOURCE;
1662     }
1663 
1664     if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE) {
1665 	status = _cairo_win32_printing_surface_analyze_operation (surface, op, source, &extents.bounded);
1666 	goto cleanup_composite;
1667     }
1668 
1669     assert (_cairo_win32_printing_surface_operation_supported (surface, op, source, &extents.bounded));
1670 
1671     surface->path_empty = TRUE;
1672     BeginPath (surface->win32.dc);
1673     status = _cairo_win32_printing_surface_emit_path (surface, path);
1674     EndPath (surface->win32.dc);
1675 
1676     switch (fill_rule) {
1677     case CAIRO_FILL_RULE_WINDING:
1678 	SetPolyFillMode (surface->win32.dc, WINDING);
1679 	break;
1680     case CAIRO_FILL_RULE_EVEN_ODD:
1681 	SetPolyFillMode (surface->win32.dc, ALTERNATE);
1682 	break;
1683     default:
1684 	ASSERT_NOT_REACHED;
1685     }
1686 
1687     if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1688 	status = _cairo_win32_printing_surface_select_solid_brush (surface, source);
1689 	if (unlikely (status))
1690 	    goto cleanup_composite;
1691 
1692 	FillPath (surface->win32.dc);
1693 	_cairo_win32_printing_surface_done_solid_brush (surface);
1694     } else if (surface->path_empty == FALSE) {
1695 	SaveDC (surface->win32.dc);
1696 	SelectClipPath (surface->win32.dc, RGN_AND);
1697 	status = _cairo_win32_printing_surface_paint_pattern (surface, source, &extents.bounded);
1698 	RestoreDC (surface->win32.dc, -1);
1699     }
1700 
1701     fflush(stderr);
1702 
1703 cleanup_composite:
1704     _cairo_composite_rectangles_fini (&extents);
1705     return status;
1706 }
1707 
1708 
1709 static cairo_int_status_t
_cairo_win32_printing_surface_emit_win32_glyphs(cairo_win32_printing_surface_t * surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_glyph_t * glyphs,int num_glyphs,cairo_scaled_font_t * scaled_font,const cairo_clip_t * clip)1710 _cairo_win32_printing_surface_emit_win32_glyphs (cairo_win32_printing_surface_t	*surface,
1711 						 cairo_operator_t	 op,
1712 						 const cairo_pattern_t  *source,
1713 						 cairo_glyph_t		 *glyphs,
1714 						 int			 num_glyphs,
1715 						 cairo_scaled_font_t	*scaled_font,
1716 						 const cairo_clip_t	*clip)
1717 {
1718     cairo_matrix_t ctm;
1719     cairo_glyph_t  *unicode_glyphs;
1720     cairo_scaled_font_subsets_glyph_t subset_glyph;
1721     int i, first;
1722     cairo_bool_t sequence_is_unicode;
1723     cairo_status_t status = CAIRO_STATUS_SUCCESS;
1724 
1725     /* Where possible reverse the glyph indices back to unicode
1726      * characters. Strings of glyphs that could not be reversed to
1727      * unicode will be printed with ETO_GLYPH_INDEX.
1728      *
1729      * As _cairo_win32_scaled_font_index_to_ucs4() is a slow
1730      * operation, the font subsetting function
1731      * _cairo_scaled_font_subsets_map_glyph() is used to obtain
1732      * the unicode value because it caches the reverse mapping in
1733      * the subsets.
1734      */
1735 
1736     if (surface->has_ctm) {
1737 	for (i = 0; i < num_glyphs; i++)
1738 	    cairo_matrix_transform_point (&surface->ctm, &glyphs[i].x, &glyphs[i].y);
1739 	cairo_matrix_multiply (&ctm, &scaled_font->ctm, &surface->ctm);
1740 	scaled_font = cairo_scaled_font_create (scaled_font->font_face,
1741 						&scaled_font->font_matrix,
1742 						&ctm,
1743 						&scaled_font->options);
1744     }
1745 
1746     unicode_glyphs = _cairo_malloc_ab (num_glyphs, sizeof (cairo_glyph_t));
1747     if (unicode_glyphs == NULL)
1748 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1749 
1750     memcpy (unicode_glyphs, glyphs, num_glyphs * sizeof (cairo_glyph_t));
1751     for (i = 0; i < num_glyphs; i++) {
1752 	status = _cairo_scaled_font_subsets_map_glyph (surface->font_subsets,
1753 						       scaled_font,
1754 						       glyphs[i].index,
1755 						       NULL, 0,
1756 						       &subset_glyph);
1757 	if (status)
1758 	    goto fail;
1759 
1760 	unicode_glyphs[i].index = subset_glyph.unicode;
1761     }
1762 
1763     i = 0;
1764     first = 0;
1765     sequence_is_unicode = unicode_glyphs[0].index <= 0xffff;
1766     while (i < num_glyphs) {
1767 	if (i == num_glyphs - 1 ||
1768 	    ((unicode_glyphs[i + 1].index < 0xffff) != sequence_is_unicode))
1769 	{
1770 	    status = _cairo_win32_surface_emit_glyphs (&surface->win32,
1771 						       source,
1772 						       sequence_is_unicode ? &unicode_glyphs[first] : &glyphs[first],
1773 						       i - first + 1,
1774 						       scaled_font,
1775 						       ! sequence_is_unicode);
1776 	    first = i + 1;
1777 	    if (i < num_glyphs - 1)
1778 		sequence_is_unicode = unicode_glyphs[i + 1].index <= 0xffff;
1779 	}
1780 	i++;
1781     }
1782 
1783 fail:
1784     if (surface->has_ctm)
1785 	cairo_scaled_font_destroy (scaled_font);
1786 
1787     free (unicode_glyphs);
1788 
1789     return status;
1790 }
1791 
1792 static cairo_int_status_t
_cairo_win32_printing_surface_show_glyphs(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_glyph_t * glyphs,int num_glyphs,cairo_scaled_font_t * scaled_font,const cairo_clip_t * clip)1793 _cairo_win32_printing_surface_show_glyphs (void                 *abstract_surface,
1794                                            cairo_operator_t	 op,
1795                                            const cairo_pattern_t *source,
1796                                            cairo_glyph_t        *glyphs,
1797                                            int			 num_glyphs,
1798                                            cairo_scaled_font_t  *scaled_font,
1799 					   const cairo_clip_t	*clip)
1800 {
1801     cairo_win32_printing_surface_t *surface = abstract_surface;
1802     cairo_status_t status = CAIRO_STATUS_SUCCESS;
1803     cairo_scaled_glyph_t *scaled_glyph;
1804     cairo_pattern_t *opaque = NULL;
1805     int i;
1806     cairo_matrix_t old_ctm;
1807     cairo_bool_t old_has_ctm;
1808     cairo_solid_pattern_t clear;
1809     cairo_composite_rectangles_t extents;
1810     cairo_bool_t overlap;
1811 
1812     status = _cairo_composite_rectangles_init_for_glyphs (&extents,
1813 							  &surface->win32.base,
1814 							  op, source,
1815 							  scaled_font,
1816 							  glyphs, num_glyphs,
1817 							  clip,
1818 							  &overlap);
1819     if (unlikely (status))
1820 	return status;
1821 
1822     status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
1823     if (unlikely (status))
1824 	goto cleanup_composite;
1825 
1826     if (op == CAIRO_OPERATOR_CLEAR) {
1827 	_cairo_win32_printing_surface_init_clear_color (surface, &clear);
1828 	source = (cairo_pattern_t*) &clear;
1829 	op = CAIRO_OPERATOR_SOURCE;
1830     }
1831 
1832     if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE) {
1833 	/* When printing bitmap fonts to a printer DC, Windows may
1834 	 * substitute an outline font for bitmap font. As the win32
1835 	 * font backend always uses a screen DC when obtaining the
1836 	 * font metrics the metrics of the substituted font will not
1837 	 * match the metrics that the win32 font backend returns.
1838 	 *
1839 	 * If we are printing a bitmap font, use fallback images to
1840 	 * ensure the font is not substituted.
1841 	 */
1842 #if CAIRO_HAS_WIN32_FONT
1843 	if (cairo_scaled_font_get_type (scaled_font) == CAIRO_FONT_TYPE_WIN32) {
1844 	    if (_cairo_win32_scaled_font_is_bitmap (scaled_font)) {
1845 		status = CAIRO_INT_STATUS_UNSUPPORTED;
1846 		goto cleanup_composite;
1847 	    } else {
1848 		status = _cairo_win32_printing_surface_analyze_operation (surface, op, source, &extents.bounded);
1849 		goto cleanup_composite;
1850 	    }
1851 	}
1852 #endif
1853 
1854 	/* For non win32 fonts we need to check that each glyph has a
1855 	 * path available. If a path is not available,
1856 	 * _cairo_scaled_glyph_lookup() will return
1857 	 * CAIRO_INT_STATUS_UNSUPPORTED and a fallback image will be
1858 	 * used.
1859 	 */
1860         _cairo_scaled_font_freeze_cache (scaled_font);
1861 	for (i = 0; i < num_glyphs; i++) {
1862 	    status = _cairo_scaled_glyph_lookup (scaled_font,
1863 						 glyphs[i].index,
1864 						 CAIRO_SCALED_GLYPH_INFO_PATH,
1865 						 &scaled_glyph);
1866 	    if (status)
1867                 break;
1868 	}
1869         _cairo_scaled_font_thaw_cache (scaled_font);
1870 	if (unlikely (status))
1871 	    goto cleanup_composite;
1872 
1873 	status = _cairo_win32_printing_surface_analyze_operation (surface, op, source, &extents.bounded);
1874 	goto cleanup_composite;
1875     }
1876 
1877     if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1878 	cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) source;
1879 	COLORREF color;
1880 
1881 	color = _cairo_win32_printing_surface_flatten_transparency (surface,
1882 								    &solid->color);
1883 	opaque = cairo_pattern_create_rgb (GetRValue (color) / 255.0,
1884 					   GetGValue (color) / 255.0,
1885 					   GetBValue (color) / 255.0);
1886 	if (unlikely (opaque->status)) {
1887 	    status = opaque->status;
1888 	    goto cleanup_composite;
1889 	}
1890 	source = opaque;
1891     }
1892 
1893 #if CAIRO_HAS_WIN32_FONT
1894     if (cairo_scaled_font_get_type (scaled_font) == CAIRO_FONT_TYPE_WIN32 &&
1895 	source->type == CAIRO_PATTERN_TYPE_SOLID)
1896     {
1897 	status = _cairo_win32_printing_surface_emit_win32_glyphs (surface,
1898 								  op,
1899 								  source,
1900 								  glyphs,
1901 								  num_glyphs,
1902 								  scaled_font,
1903 								  clip);
1904 	goto cleanup_composite;
1905     }
1906 #endif
1907 
1908     SaveDC (surface->win32.dc);
1909     old_ctm = surface->ctm;
1910     old_has_ctm = surface->has_ctm;
1911     surface->has_ctm = TRUE;
1912     surface->path_empty = TRUE;
1913     _cairo_scaled_font_freeze_cache (scaled_font);
1914     BeginPath (surface->win32.dc);
1915     for (i = 0; i < num_glyphs; i++) {
1916 	status = _cairo_scaled_glyph_lookup (scaled_font,
1917 					     glyphs[i].index,
1918 					     CAIRO_SCALED_GLYPH_INFO_PATH,
1919 					     &scaled_glyph);
1920 	if (status)
1921 	    break;
1922 	surface->ctm = old_ctm;
1923 	cairo_matrix_translate (&surface->ctm, glyphs[i].x, glyphs[i].y);
1924 	status = _cairo_win32_printing_surface_emit_path (surface, scaled_glyph->path);
1925     }
1926     EndPath (surface->win32.dc);
1927     _cairo_scaled_font_thaw_cache (scaled_font);
1928     surface->ctm = old_ctm;
1929     surface->has_ctm = old_has_ctm;
1930     if (status == CAIRO_STATUS_SUCCESS && surface->path_empty == FALSE) {
1931 	if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1932 	    status = _cairo_win32_printing_surface_select_solid_brush (surface, source);
1933 	    if (unlikely (status))
1934 		goto cleanup_composite;
1935 
1936 	    SetPolyFillMode (surface->win32.dc, WINDING);
1937 	    FillPath (surface->win32.dc);
1938 	    _cairo_win32_printing_surface_done_solid_brush (surface);
1939 	} else {
1940 	    SelectClipPath (surface->win32.dc, RGN_AND);
1941 	    status = _cairo_win32_printing_surface_paint_pattern (surface, source, &extents.bounded);
1942 	}
1943     }
1944     RestoreDC (surface->win32.dc, -1);
1945 
1946     if (opaque)
1947 	cairo_pattern_destroy (opaque);
1948 
1949 cleanup_composite:
1950     _cairo_composite_rectangles_fini (&extents);
1951     return status;
1952 }
1953 
1954 static const char **
_cairo_win32_printing_surface_get_supported_mime_types(void * abstract_surface)1955 _cairo_win32_printing_surface_get_supported_mime_types (void	  *abstract_surface)
1956 {
1957     return _cairo_win32_printing_supported_mime_types;
1958 }
1959 
1960 static cairo_status_t
_cairo_win32_printing_surface_finish(void * abstract_surface)1961 _cairo_win32_printing_surface_finish (void *abstract_surface)
1962 {
1963     cairo_win32_printing_surface_t *surface = abstract_surface;
1964 
1965     if (surface->font_subsets != NULL)
1966 	_cairo_scaled_font_subsets_destroy (surface->font_subsets);
1967 
1968     return CAIRO_STATUS_SUCCESS;
1969 }
1970 
1971 static cairo_surface_t *
_cairo_win32_printing_surface_create_similar(void * abstract_surface,cairo_content_t content,int width,int height)1972 _cairo_win32_printing_surface_create_similar (void		*abstract_surface,
1973 					      cairo_content_t	 content,
1974 					      int		 width,
1975 					      int		 height)
1976 {
1977     cairo_rectangle_t extents;
1978 
1979     extents.x = extents.y = 0;
1980     extents.width  = width;
1981     extents.height = height;
1982     return cairo_recording_surface_create (content, &extents);
1983 }
1984 
1985 static cairo_int_status_t
_cairo_win32_printing_surface_start_page(void * abstract_surface)1986 _cairo_win32_printing_surface_start_page (void *abstract_surface)
1987 {
1988     cairo_win32_printing_surface_t *surface = abstract_surface;
1989     XFORM xform;
1990     double x_res, y_res;
1991     cairo_matrix_t inverse_ctm;
1992     cairo_status_t status;
1993     RECT rect;
1994 
1995     /* Since the page size may be changed after _show_page() and before the
1996      * next drawing command, the extents are set in _start_page() and invalidated
1997      * in _show_page(). The paginated surface will obtain the extents immediately
1998      * after calling _show_page() and before any drawing commands. At this point
1999      * the next page will not have been setup on the DC so we return invalid
2000      * extents and the paginated surface will create an unbounded recording surface.
2001      * Prior to replay of the record surface, the paginated surface will call
2002      * _start_page and we setup the correct extents.
2003      *
2004      * Note that we always set the extents x,y to 0 so prevent replay from translating
2005      * the coordinates of objects. Windows will clip anything outside of the page clip
2006      * area.
2007      */
2008     GetClipBox(surface->win32.dc, &rect);
2009     surface->win32.extents.x = 0;
2010     surface->win32.extents.y = 0;
2011     surface->win32.extents.width = rect.right;
2012     surface->win32.extents.height = rect.bottom;
2013     surface->extents_valid = TRUE;
2014 
2015     SaveDC (surface->win32.dc); /* Save application context first, before doing MWT */
2016 
2017     /* As the logical coordinates used by GDI functions (eg LineTo)
2018      * are integers we need to do some additional work to prevent
2019      * rounding errors. For example the obvious way to paint a recording
2020      * pattern is to:
2021      *
2022      *   SaveDC()
2023      *   transform the device context DC by the pattern to device matrix
2024      *   replay the recording surface
2025      *   RestoreDC()
2026      *
2027      * The problem here is that if the pattern to device matrix is
2028      * [100 0 0 100 0 0], coordinates in the recording pattern such as
2029      * (1.56, 2.23) which correspond to (156, 223) in device space
2030      * will be rounded to (100, 200) due to (1.56, 2.23) being
2031      * truncated to integers.
2032      *
2033      * This is solved by saving the current GDI CTM in surface->ctm,
2034      * switch the GDI CTM to identity, and transforming all
2035      * coordinates by surface->ctm before passing them to GDI. When
2036      * painting a recording pattern, surface->ctm is transformed by the
2037      * pattern to device matrix.
2038      *
2039      * For printing device contexts where 1 unit is 1 dpi, switching
2040      * the GDI CTM to identity maximises the possible resolution of
2041      * coordinates.
2042      *
2043      * If the device context is an EMF file, using an identity
2044      * transform often provides insufficient resolution. The workaround
2045      * is to set the GDI CTM to a scale < 1 eg [1.0/16 0 0 1/0/16 0 0]
2046      * and scale the cairo CTM by [16 0 0 16 0 0]. The
2047      * SetWorldTransform function call to scale the GDI CTM by 1.0/16
2048      * will be recorded in the EMF followed by all the graphics
2049      * functions by their coordinateds multiplied by 16.
2050      *
2051      * To support allowing the user to set a GDI CTM with scale < 1,
2052      * we avoid switching to an identity CTM if the CTM xx and yy is < 1.
2053      */
2054     SetGraphicsMode (surface->win32.dc, GM_ADVANCED);
2055     GetWorldTransform(surface->win32.dc, &xform);
2056     if (xform.eM11 < 1 && xform.eM22 < 1) {
2057 	cairo_matrix_init_identity (&surface->ctm);
2058 	surface->gdi_ctm.xx = xform.eM11;
2059 	surface->gdi_ctm.xy = xform.eM21;
2060 	surface->gdi_ctm.yx = xform.eM12;
2061 	surface->gdi_ctm.yy = xform.eM22;
2062 	surface->gdi_ctm.x0 = xform.eDx;
2063 	surface->gdi_ctm.y0 = xform.eDy;
2064     } else {
2065 	surface->ctm.xx = xform.eM11;
2066 	surface->ctm.xy = xform.eM21;
2067 	surface->ctm.yx = xform.eM12;
2068 	surface->ctm.yy = xform.eM22;
2069 	surface->ctm.x0 = xform.eDx;
2070 	surface->ctm.y0 = xform.eDy;
2071 	cairo_matrix_init_identity (&surface->gdi_ctm);
2072 	if (!ModifyWorldTransform (surface->win32.dc, NULL, MWT_IDENTITY))
2073 	    return _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_start_page:ModifyWorldTransform");
2074     }
2075 
2076     surface->has_ctm = !_cairo_matrix_is_identity (&surface->ctm);
2077     surface->has_gdi_ctm = !_cairo_matrix_is_identity (&surface->gdi_ctm);
2078     inverse_ctm = surface->ctm;
2079     status = cairo_matrix_invert (&inverse_ctm);
2080     if (status)
2081 	return status;
2082 
2083     x_res = GetDeviceCaps (surface->win32.dc, LOGPIXELSX);
2084     y_res = GetDeviceCaps (surface->win32.dc, LOGPIXELSY);
2085     cairo_matrix_transform_distance (&inverse_ctm, &x_res, &y_res);
2086     _cairo_surface_set_resolution (&surface->win32.base, x_res, y_res);
2087 
2088     SaveDC (surface->win32.dc); /* Then save Cairo's known-good clip state, so the clip path can be reset */
2089 
2090     return CAIRO_STATUS_SUCCESS;
2091 }
2092 
2093 static cairo_int_status_t
_cairo_win32_printing_surface_set_paginated_mode(void * abstract_surface,cairo_paginated_mode_t paginated_mode)2094 _cairo_win32_printing_surface_set_paginated_mode (void *abstract_surface,
2095                                                   cairo_paginated_mode_t paginated_mode)
2096 {
2097     cairo_win32_printing_surface_t *surface = abstract_surface;
2098 
2099     surface->paginated_mode = paginated_mode;
2100 
2101     return CAIRO_STATUS_SUCCESS;
2102 }
2103 
2104 static cairo_bool_t
_cairo_win32_printing_surface_supports_fine_grained_fallbacks(void * abstract_surface)2105 _cairo_win32_printing_surface_supports_fine_grained_fallbacks (void *abstract_surface)
2106 {
2107     return TRUE;
2108 }
2109 
2110 /**
2111  * cairo_win32_printing_surface_create:
2112  * @hdc: the DC to create a surface for
2113  *
2114  * Creates a cairo surface that targets the given DC.  The DC will be
2115  * queried for its initial clip extents, and this will be used as the
2116  * size of the cairo surface.  The DC should be a printing DC;
2117  * antialiasing will be ignored, and GDI will be used as much as
2118  * possible to draw to the surface.
2119  *
2120  * The returned surface will be wrapped using the paginated surface to
2121  * provide correct complex rendering behaviour; cairo_surface_show_page() and
2122  * associated methods must be used for correct output.
2123  *
2124  * Return value: the newly created surface
2125  *
2126  * Since: 1.6
2127  **/
2128 cairo_surface_t *
cairo_win32_printing_surface_create(HDC hdc)2129 cairo_win32_printing_surface_create (HDC hdc)
2130 {
2131     cairo_win32_printing_surface_t *surface;
2132     cairo_surface_t *paginated;
2133 
2134     surface = _cairo_malloc (sizeof (cairo_win32_printing_surface_t));
2135     if (surface == NULL)
2136 	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
2137 
2138 #if 0
2139     if (_cairo_win32_save_initial_clip (hdc, surface) != CAIRO_STATUS_SUCCESS) {
2140 	free (surface);
2141 	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
2142     }
2143 #endif
2144 
2145     _cairo_surface_clipper_init (&surface->clipper,
2146 				 _cairo_win32_printing_surface_clipper_intersect_clip_path);
2147 
2148     surface->win32.format = CAIRO_FORMAT_RGB24;
2149     surface->content = CAIRO_CONTENT_COLOR_ALPHA;
2150 
2151     surface->win32.dc = hdc;
2152     surface->extents_valid = FALSE;
2153 
2154     surface->brush = NULL;
2155     surface->old_brush = NULL;
2156     surface->font_subsets = _cairo_scaled_font_subsets_create_scaled ();
2157     if (surface->font_subsets == NULL) {
2158 	free (surface);
2159 	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
2160     }
2161 
2162     surface->win32.flags = _cairo_win32_flags_for_dc (surface->win32.dc, CAIRO_FORMAT_RGB24);
2163     surface->win32.flags |= CAIRO_WIN32_SURFACE_FOR_PRINTING;
2164 
2165     _cairo_win32_printing_surface_init_ps_mode (surface);
2166     _cairo_win32_printing_surface_init_image_support (surface);
2167     _cairo_win32_printing_surface_init_language_pack (surface);
2168     _cairo_surface_init (&surface->win32.base,
2169 			 &cairo_win32_printing_surface_backend,
2170 			 NULL, /* device */
2171                          CAIRO_CONTENT_COLOR_ALPHA,
2172 			 TRUE); /* is_vector */
2173 
2174     paginated = _cairo_paginated_surface_create (&surface->win32.base,
2175 						 CAIRO_CONTENT_COLOR_ALPHA,
2176 						 &cairo_win32_surface_paginated_backend);
2177 
2178     /* paginated keeps the only reference to surface now, drop ours */
2179     cairo_surface_destroy (&surface->win32.base);
2180 
2181     return paginated;
2182 }
2183 
2184 static const cairo_surface_backend_t cairo_win32_printing_surface_backend = {
2185     CAIRO_SURFACE_TYPE_WIN32_PRINTING,
2186     _cairo_win32_printing_surface_finish,
2187 
2188     _cairo_default_context_create,
2189 
2190     _cairo_win32_printing_surface_create_similar,
2191     NULL, /* create similar image */
2192     NULL, /* map to image */
2193     NULL, /* unmap image */
2194 
2195     _cairo_surface_default_source,
2196     NULL, /* acquire_source_image */
2197     NULL, /* release_source_image */
2198     NULL, /* snapshot */
2199 
2200     NULL, /* copy_page */
2201     _cairo_win32_printing_surface_show_page,
2202 
2203     _cairo_win32_printing_surface_get_extents,
2204     _cairo_win32_printing_surface_get_font_options,
2205 
2206     NULL, /* flush */
2207     NULL, /* mark_dirty_rectangle */
2208 
2209     _cairo_win32_printing_surface_paint,
2210     NULL, /* mask */
2211     _cairo_win32_printing_surface_stroke,
2212     _cairo_win32_printing_surface_fill,
2213     NULL, /* fill/stroke */
2214     _cairo_win32_printing_surface_show_glyphs,
2215     NULL, /* has_show_text_glyphs */
2216     NULL, /* show_text_glyphs */
2217     _cairo_win32_printing_surface_get_supported_mime_types,
2218 };
2219 
2220 static const cairo_paginated_surface_backend_t cairo_win32_surface_paginated_backend = {
2221     _cairo_win32_printing_surface_start_page,
2222     _cairo_win32_printing_surface_set_paginated_mode,
2223     NULL, /* set_bounding_box */
2224     NULL, /* _cairo_win32_printing_surface_has_fallback_images, */
2225     _cairo_win32_printing_surface_supports_fine_grained_fallbacks,
2226 };
2227