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