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
4 * Copyright © 2007, 2008 Adrian Johnson
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.
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
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Adrian Johnson.
34 * Adrian Johnson <ajohnson@redneon.com>
35 * Vladimir Vukicevic <vladimir@pobox.com>
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
43 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
44 # define _WIN32_WINNT 0x0500
49 #include "cairo-default-context-private.h"
50 #include "cairo-error-private.h"
51 #include "cairo-paginated-private.h"
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"
64 #if !defined(POSTSCRIPT_IDENTIFY)
65 # define POSTSCRIPT_IDENTIFY 0x1015
68 #if !defined(PSIDENT_GDICENTRIC)
69 # define PSIDENT_GDICENTRIC 0x0000
72 #if !defined(GET_PS_FEATURESETTING)
73 # define GET_PS_FEATURESETTING 0x1019
76 #if !defined(FEATURESETTING_PSLEVEL)
77 # define FEATURESETTING_PSLEVEL 0x0002
80 #if !defined(GRADIENT_FILL_RECT_H)
81 # define GRADIENT_FILL_RECT_H 0x00
84 #if !defined(CHECKJPEGFORMAT)
85 # define CHECKJPEGFORMAT 0x1017
88 #if !defined(CHECKPNGFORMAT)
89 # define CHECKPNGFORMAT 0x1018
92 #define PELS_72DPI ((LONG)(72. / 0.0254))
94 static const char *_cairo_win32_printing_supported_mime_types[] =
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;
105 _cairo_win32_printing_surface_init_ps_mode (cairo_win32_printing_surface_t *surface)
108 INT ps_feature, ps_level;
110 word = PSIDENT_GDICENTRIC;
111 if (ExtEscape (surface->win32.dc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), (char *)&word, 0, (char *)NULL) <= 0)
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)
120 surface->win32.flags |= CAIRO_WIN32_SURFACE_CAN_RECT_GRADIENT;
124 _cairo_win32_printing_surface_init_image_support (cairo_win32_printing_surface_t *surface)
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;
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;
137 /* When creating an EMF file, ExtTextOut with ETO_GLYPH_INDEX does not
138 * work unless the GDI function GdiInitializeLanguagePack() has been
141 * http://m-a-tech.blogspot.com/2009/04/emf-buffer-idiocracy.html
143 * The only information I could find on the how to use this
144 * undocumented function is the use in:
146 * http://src.chromium.org/viewvc/chrome/trunk/src/chrome/renderer/render_process.cc?view=markup
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)
155 _cairo_win32_printing_surface_init_language_pack (cairo_win32_printing_surface_t *surface)
157 typedef BOOL (WINAPI *gdi_init_lang_pack_func_t)(int);
158 gdi_init_lang_pack_func_t gdi_init_lang_pack;
161 if (GetModuleHandleW (L"LPK.DLL"))
164 module = GetModuleHandleW (L"GDI32.DLL");
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);
173 static cairo_int_status_t
174 analyze_surface_pattern_transparency (cairo_surface_pattern_t *pattern)
176 cairo_image_surface_t *image;
178 cairo_int_status_t status;
179 cairo_image_transparency_t transparency;
181 status = _cairo_surface_acquire_source_image (pattern->surface,
187 transparency = _cairo_image_analyze_transparency (image);
188 switch (transparency) {
189 case CAIRO_IMAGE_UNKNOWN:
191 case CAIRO_IMAGE_IS_OPAQUE:
192 status = CAIRO_STATUS_SUCCESS;
195 case CAIRO_IMAGE_HAS_BILEVEL_ALPHA:
196 case CAIRO_IMAGE_HAS_ALPHA:
197 status = CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY;
201 _cairo_surface_release_source_image (pattern->surface, image, image_extra);
207 surface_pattern_supported (const cairo_surface_pattern_t *pattern)
209 if (_cairo_surface_is_recording (pattern->surface))
212 if (cairo_surface_get_type (pattern->surface) != CAIRO_SURFACE_TYPE_WIN32 &&
213 pattern->surface->backend->acquire_source_image == NULL)
222 pattern_supported (cairo_win32_printing_surface_t *surface, const cairo_pattern_t *pattern)
224 if (pattern->type == CAIRO_PATTERN_TYPE_SOLID)
227 if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE)
228 return surface_pattern_supported ((const cairo_surface_pattern_t *) pattern);
230 if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR)
231 return surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_RECT_GRADIENT;
236 static cairo_int_status_t
237 _cairo_win32_printing_surface_analyze_operation (cairo_win32_printing_surface_t *surface,
239 const cairo_pattern_t *pattern)
241 if (! pattern_supported (surface, pattern))
242 return CAIRO_INT_STATUS_UNSUPPORTED;
244 if (!(op == CAIRO_OPERATOR_SOURCE ||
245 op == CAIRO_OPERATOR_OVER ||
246 op == CAIRO_OPERATOR_CLEAR))
247 return CAIRO_INT_STATUS_UNSUPPORTED;
249 if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
250 cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern;
252 if ( _cairo_surface_is_recording (surface_pattern->surface))
253 return CAIRO_INT_STATUS_ANALYZE_RECORDING_SURFACE_PATTERN;
256 if (op == CAIRO_OPERATOR_SOURCE ||
257 op == CAIRO_OPERATOR_CLEAR)
258 return CAIRO_STATUS_SUCCESS;
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.
270 if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
271 cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern;
273 return analyze_surface_pattern_transparency (surface_pattern);
276 if (_cairo_pattern_is_opaque (pattern, NULL))
277 return CAIRO_STATUS_SUCCESS;
279 return CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY;
283 _cairo_win32_printing_surface_operation_supported (cairo_win32_printing_surface_t *surface,
285 const cairo_pattern_t *pattern)
287 if (_cairo_win32_printing_surface_analyze_operation (surface, op, pattern) != CAIRO_INT_STATUS_UNSUPPORTED)
294 _cairo_win32_printing_surface_init_clear_color (cairo_win32_printing_surface_t *surface,
295 cairo_solid_pattern_t *color)
297 if (surface->content == CAIRO_CONTENT_COLOR_ALPHA)
298 _cairo_pattern_init_solid (color, CAIRO_COLOR_WHITE);
300 _cairo_pattern_init_solid (color, CAIRO_COLOR_BLACK);
304 _cairo_win32_printing_surface_flatten_transparency (cairo_win32_printing_surface_t *surface,
305 const cairo_color_t *color)
308 BYTE red, green, blue;
310 red = color->red_short >> 8;
311 green = color->green_short >> 8;
312 blue = color->blue_short >> 8;
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);
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;
323 /* Blend into black */
324 red = (color->red_short >> 8);
325 green = (color->green_short >> 8);
326 blue = (color->blue_short >> 8);
329 c = RGB (red, green, blue);
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)
338 cairo_solid_pattern_t *pattern = (cairo_solid_pattern_t *) source;
341 color = _cairo_win32_printing_surface_flatten_transparency (surface,
343 surface->brush = CreateSolidBrush (color);
345 return _cairo_win32_print_gdi_error ("_cairo_win32_surface_select_solid_brush(CreateSolidBrush)");
346 surface->old_brush = SelectObject (surface->win32.dc, surface->brush);
348 return CAIRO_STATUS_SUCCESS;
352 _cairo_win32_printing_surface_done_solid_brush (cairo_win32_printing_surface_t *surface)
354 if (surface->old_brush) {
355 SelectObject (surface->win32.dc, surface->old_brush);
356 DeleteObject (surface->brush);
357 surface->old_brush = NULL;
361 static cairo_status_t
362 _cairo_win32_printing_surface_get_ctm_clip_box (cairo_win32_printing_surface_t *surface,
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);
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");
376 return CAIRO_STATUS_SUCCESS;
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)
384 cairo_status_t status;
386 GetClipBox (surface->win32.dc, &clip);
387 status = _cairo_win32_printing_surface_select_solid_brush (surface, pattern);
391 FillRect (surface->win32.dc, &clip, surface->brush);
392 _cairo_win32_printing_surface_done_solid_brush (surface);
394 return CAIRO_STATUS_SUCCESS;
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)
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;
409 int x_tile, y_tile, left, right, top, bottom;
411 cairo_recording_surface_t *recording_surface = (cairo_recording_surface_t *) pattern->surface;
414 extend = cairo_pattern_get_extend (&pattern->base);
416 p2d = pattern->base.matrix;
417 status = cairo_matrix_invert (&p2d);
418 /* _cairo_pattern_set_matrix guarantees invertibility */
419 assert (status == CAIRO_STATUS_SUCCESS);
421 old_ctm = surface->ctm;
422 old_has_ctm = surface->has_ctm;
423 cairo_matrix_multiply (&p2d, &p2d, &surface->ctm);
425 SaveDC (surface->win32.dc);
426 _cairo_matrix_to_win32_xform (&p2d, &xform);
428 status = _cairo_recording_surface_get_bbox (recording_surface, &bbox, NULL);
432 _cairo_box_round_to_rectangle (&bbox, &recording_extents);
434 status = _cairo_win32_printing_surface_get_ctm_clip_box (surface, &clip);
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));
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);
459 for (y_tile = top; y_tile < bottom; y_tile++) {
460 for (x_tile = left; x_tile < right; x_tile++) {
464 SaveDC (surface->win32.dc);
466 cairo_matrix_translate (&m,
467 x_tile*recording_extents.width,
468 y_tile*recording_extents.height);
469 if (extend == CAIRO_EXTEND_REFLECT) {
471 cairo_matrix_translate (&m, recording_extents.width, 0);
472 cairo_matrix_scale (&m, -1, 1);
475 cairo_matrix_translate (&m, 0, recording_extents.height);
476 cairo_matrix_scale (&m, 1, -1);
480 surface->has_ctm = !_cairo_matrix_is_identity (&surface->ctm);
482 /* Set clip path around bbox of the pattern. */
483 BeginPath (surface->win32.dc);
487 cairo_matrix_transform_point (&surface->ctm, &x, &y);
488 MoveToEx (surface->win32.dc, (int) x, (int) y, NULL);
490 x = recording_extents.width;
492 cairo_matrix_transform_point (&surface->ctm, &x, &y);
493 LineTo (surface->win32.dc, (int) x, (int) y);
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);
501 y = recording_extents.height;
502 cairo_matrix_transform_point (&surface->ctm, &x, &y);
503 LineTo (surface->win32.dc, (int) x, (int) y);
505 CloseFigure (surface->win32.dc);
506 EndPath (surface->win32.dc);
507 SelectClipPath (surface->win32.dc, RGN_AND);
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);
522 surface->content = old_content;
523 surface->ctm = old_ctm;
524 surface->has_ctm = old_has_ctm;
525 RestoreDC (surface->win32.dc, -1);
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)
537 const unsigned char *mime_data;
538 unsigned long mime_data_length;
539 cairo_int_status_t status;
542 if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_CHECK_JPEG))
543 return CAIRO_INT_STATUS_UNSUPPORTED;
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;
550 status = _cairo_image_info_get_jpeg_info (info, mime_data, mime_data_length);
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;
560 return CAIRO_INT_STATUS_UNSUPPORTED;
563 *length = mime_data_length;
565 return CAIRO_STATUS_SUCCESS;
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)
575 const unsigned char *mime_data;
576 unsigned long mime_data_length;
578 cairo_int_status_t status;
581 if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_CHECK_PNG))
582 return CAIRO_INT_STATUS_UNSUPPORTED;
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;
589 status = _cairo_image_info_get_png_info (info, mime_data, mime_data_length);
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;
599 return CAIRO_INT_STATUS_UNSUPPORTED;
602 *length = mime_data_length;
604 return CAIRO_STATUS_SUCCESS;
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)
611 cairo_status_t status;
612 cairo_extend_t extend;
613 cairo_image_surface_t *image;
615 cairo_image_surface_t *opaque_image = NULL;
620 int x_tile, y_tile, left, right, top, bottom;
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;
629 /* If we can't use StretchDIBits with this surface, we can't do anything
632 if (!(surface->win32.flags & CAIRO_WIN32_SURFACE_CAN_STRETCHDIB))
633 return CAIRO_INT_STATUS_UNSUPPORTED;
635 if (surface->content == CAIRO_CONTENT_COLOR_ALPHA)
636 background_color = CAIRO_COLOR_WHITE;
638 background_color = CAIRO_COLOR_BLACK;
640 extend = cairo_pattern_get_extend (&pattern->base);
642 status = _cairo_surface_acquire_source_image (pattern->surface,
643 &image, &image_extra);
647 if (image->base.status) {
648 status = image->base.status;
652 if (image->width == 0 || image->height == 0) {
653 status = CAIRO_STATUS_SUCCESS;
658 status = _cairo_win32_printing_surface_check_jpeg (surface,
663 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
665 status = _cairo_win32_printing_surface_check_png (surface,
671 if (_cairo_status_is_error (status))
674 use_mime = (status == CAIRO_STATUS_SUCCESS);
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;
681 opaque_surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
684 if (opaque_surface->status) {
685 status = opaque_surface->status;
686 goto CLEANUP_OPAQUE_IMAGE;
689 _cairo_pattern_init_solid (&background_pattern,
691 status = _cairo_surface_paint (opaque_surface,
692 CAIRO_OPERATOR_SOURCE,
693 &background_pattern.base,
696 goto CLEANUP_OPAQUE_IMAGE;
698 _cairo_pattern_init_for_surface (&image_pattern, &image->base);
699 status = _cairo_surface_paint (opaque_surface,
703 _cairo_pattern_fini (&image_pattern.base);
705 goto CLEANUP_OPAQUE_IMAGE;
707 opaque_image = (cairo_image_surface_t *) opaque_surface;
709 opaque_image = image;
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;
724 m = pattern->base.matrix;
725 status = cairo_matrix_invert (&m);
726 /* _cairo_pattern_set_matrix guarantees invertibility */
727 assert (status == CAIRO_STATUS_SUCCESS);
729 cairo_matrix_multiply (&m, &m, &surface->gdi_ctm);
730 SaveDC (surface->win32.dc);
731 _cairo_matrix_to_win32_xform (&m, &xform);
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;
738 oldmode = SetStretchBltMode(surface->win32.dc, HALFTONE);
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);
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,
759 opaque_image->height,
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,
769 status = _cairo_win32_print_gdi_error ("_cairo_win32_printing_surface_paint(StretchDIBits)");
770 goto CLEANUP_OPAQUE_IMAGE;
774 SetStretchBltMode(surface->win32.dc, oldmode);
775 RestoreDC (surface->win32.dc, -1);
777 CLEANUP_OPAQUE_IMAGE:
778 if (opaque_image != image)
779 cairo_surface_destroy (&opaque_image->base);
781 _cairo_surface_release_source_image (pattern->surface, image, image_extra);
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)
790 if (_cairo_surface_is_recording (pattern->surface)) {
791 return _cairo_win32_printing_surface_paint_recording_pattern (surface,
794 return _cairo_win32_printing_surface_paint_image_pattern (surface,
800 vertex_set_color (TRIVERTEX *vert, cairo_color_stop_t *color)
802 /* MSDN says that the range here is 0x0000 .. 0xff00;
803 * that may well be a typo, but just chop the low bits
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;
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)
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;
827 extend = cairo_pattern_get_extend (&pattern->base.base);
828 SaveDC (surface->win32.dc);
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);
835 cairo_matrix_multiply (&mat, &surface->ctm, &mat);
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);
845 d = sqrt (xd*xd + yd*yd);
848 cairo_matrix_init (&rot,
852 cairo_matrix_multiply (&mat, &rot, &mat);
854 _cairo_matrix_to_win32_xform (&mat, &xform);
856 if (!SetWorldTransform (surface->win32.dc, &xform))
857 return _cairo_win32_print_gdi_error ("_win32_printing_surface_paint_linear_pattern:SetWorldTransform2");
859 GetClipBox (surface->win32.dc, &clip);
861 if (extend == CAIRO_EXTEND_REPEAT || extend == CAIRO_EXTEND_REFLECT) {
862 range_start = floor (clip.left / d);
863 range_stop = ceil (clip.right / d);
868 num_ranges = range_stop - range_start;
869 num_stops = pattern->base.n_stops;
870 num_rects = num_stops - 1;
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));
876 for (i = 0; i < num_ranges*num_rects; i++) {
877 vert[i*2].y = (LONG) clip.top;
878 if (i%num_rects == 0) {
880 if (extend == CAIRO_EXTEND_REFLECT && (range_start+(i/num_rects))%2)
882 vert[i*2].x = (LONG)(d*(range_start + i/num_rects));
883 vertex_set_color (&vert[i*2], &pattern->base.stops[stop].color);
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;
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);
899 rect[i].UpperLeft = i*2;
900 rect[i].LowerRight = i*2 + 1;
902 total_verts = 2*num_ranges*num_rects;
903 total_rects = num_ranges*num_rects;
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;
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;
942 if (!GradientFill (surface->win32.dc,
945 GRADIENT_FILL_RECT_H))
946 return _cairo_win32_print_gdi_error ("_win32_printing_surface_paint_linear_pattern:GradientFill");
950 RestoreDC (surface->win32.dc, -1);
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)
959 cairo_status_t status;
961 switch (pattern->type) {
962 case CAIRO_PATTERN_TYPE_SOLID:
963 status = _cairo_win32_printing_surface_paint_solid_pattern (surface, pattern);
968 case CAIRO_PATTERN_TYPE_SURFACE:
969 status = _cairo_win32_printing_surface_paint_surface_pattern (surface,
970 (cairo_surface_pattern_t *) pattern);
975 case CAIRO_PATTERN_TYPE_LINEAR:
976 status = _cairo_win32_printing_surface_paint_linear_pattern (surface, (cairo_linear_pattern_t *) pattern);
981 case CAIRO_PATTERN_TYPE_RADIAL:
982 return CAIRO_INT_STATUS_UNSUPPORTED;
985 case CAIRO_PATTERN_TYPE_MESH:
989 return CAIRO_STATUS_SUCCESS;
992 typedef struct _win32_print_path_info {
993 cairo_win32_printing_surface_t *surface;
996 static cairo_status_t
997 _cairo_win32_printing_surface_path_move_to (void *closure,
998 const cairo_point_t *point)
1000 win32_path_info_t *path_info = closure;
1002 if (path_info->surface->has_ctm) {
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);
1010 MoveToEx (path_info->surface->win32.dc,
1011 _cairo_fixed_integer_part (point->x),
1012 _cairo_fixed_integer_part (point->y),
1016 return CAIRO_STATUS_SUCCESS;
1019 static cairo_status_t
1020 _cairo_win32_printing_surface_path_line_to (void *closure,
1021 const cairo_point_t *point)
1023 win32_path_info_t *path_info = closure;
1025 path_info->surface->path_empty = FALSE;
1026 if (path_info->surface->has_ctm) {
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);
1034 LineTo (path_info->surface->win32.dc,
1035 _cairo_fixed_integer_part (point->x),
1036 _cairo_fixed_integer_part (point->y));
1039 return CAIRO_STATUS_SUCCESS;
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)
1048 win32_path_info_t *path_info = closure;
1051 path_info->surface->path_empty = FALSE;
1052 if (path_info->surface->has_ctm) {
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;
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;
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;
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);
1080 PolyBezierTo (path_info->surface->win32.dc, points, 3);
1082 return CAIRO_STATUS_SUCCESS;
1085 static cairo_status_t
1086 _cairo_win32_printing_surface_path_close_path (void *closure)
1088 win32_path_info_t *path_info = closure;
1090 CloseFigure (path_info->surface->win32.dc);
1092 return CAIRO_STATUS_SUCCESS;
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)
1099 win32_path_info_t path_info;
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,
1110 static cairo_int_status_t
1111 _cairo_win32_printing_surface_show_page (void *abstract_surface)
1113 cairo_win32_printing_surface_t *surface = abstract_surface;
1115 /* Undo both SaveDC's that we did in start_page */
1116 RestoreDC (surface->win32.dc, -2);
1118 return CAIRO_STATUS_SUCCESS;
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,
1126 cairo_antialias_t antialias)
1128 cairo_win32_printing_surface_t *surface = cairo_container_of (clipper,
1129 cairo_win32_printing_surface_t,
1131 cairo_status_t status;
1133 if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE)
1134 return CAIRO_STATUS_SUCCESS;
1137 RestoreDC (surface->win32.dc, -1);
1138 SaveDC (surface->win32.dc);
1140 return CAIRO_STATUS_SUCCESS;
1143 BeginPath (surface->win32.dc);
1144 status = _cairo_win32_printing_surface_emit_path (surface, path);
1145 EndPath (surface->win32.dc);
1147 switch (fill_rule) {
1148 case CAIRO_FILL_RULE_WINDING:
1149 SetPolyFillMode (surface->win32.dc, WINDING);
1151 case CAIRO_FILL_RULE_EVEN_ODD:
1152 SetPolyFillMode (surface->win32.dc, ALTERNATE);
1158 SelectClipPath (surface->win32.dc, RGN_AND);
1164 _cairo_win32_printing_surface_get_font_options (void *abstract_surface,
1165 cairo_font_options_t *options)
1167 _cairo_font_options_init_default (options);
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);
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)
1181 cairo_win32_printing_surface_t *surface = abstract_surface;
1182 cairo_solid_pattern_t clear;
1183 cairo_status_t status;
1185 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
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;
1195 if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE)
1196 return _cairo_win32_printing_surface_analyze_operation (surface, op, source);
1198 assert (_cairo_win32_printing_surface_operation_supported (surface, op, source));
1200 return _cairo_win32_printing_surface_paint_pattern (surface, source);
1204 _cairo_win32_line_cap (cairo_line_cap_t 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;
1220 _cairo_win32_line_join (cairo_line_join_t 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;
1236 _cairo_matrix_factor_out_scale (cairo_matrix_t *m, double *scale)
1241 if (fabs (m->xy) > s)
1243 if (fabs (m->yx) > s)
1245 if (fabs (m->yy) > s)
1249 cairo_matrix_scale (m, s, s);
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,
1261 cairo_antialias_t antialias,
1262 const cairo_clip_t *clip)
1264 cairo_win32_printing_surface_t *surface = abstract_surface;
1265 cairo_int_status_t status;
1274 cairo_solid_pattern_t clear;
1278 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
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;
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;
1293 return _cairo_win32_printing_surface_analyze_operation (surface, op, source);
1296 assert (_cairo_win32_printing_surface_operation_supported (surface, op, source));
1297 assert (!(style->num_dashes > 0 && style->dash_offset != 0.0));
1299 cairo_matrix_multiply (&mat, stroke_ctm, &surface->ctm);
1300 _cairo_matrix_factor_out_scale (&mat, &scale);
1302 pen_style = PS_GEOMETRIC;
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]);
1311 pen_style |= PS_SOLID;
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;
1319 color = _cairo_win32_printing_surface_flatten_transparency (surface,
1322 /* Color not used as the pen will only be used by WidenPath() */
1323 color = RGB (0,0,0);
1325 brush.lbStyle = BS_SOLID;
1326 brush.lbColor = color;
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,
1336 return _cairo_win32_print_gdi_error ("_win32_surface_stroke:ExtCreatePen");
1337 obj = SelectObject (surface->win32.dc, pen);
1339 return _cairo_win32_print_gdi_error ("_win32_surface_stroke:SelectObject");
1341 BeginPath (surface->win32.dc);
1342 status = _cairo_win32_printing_surface_emit_path (surface, path);
1343 EndPath (surface->win32.dc);
1348 * Switch to user space to set line parameters
1350 SaveDC (surface->win32.dc);
1352 _cairo_matrix_to_win32_xform (&mat, &xform);
1356 if (!ModifyWorldTransform (surface->win32.dc, &xform, MWT_LEFTMULTIPLY))
1357 return _cairo_win32_print_gdi_error ("_win32_surface_stroke:SetWorldTransform");
1359 if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1360 StrokePath (surface->win32.dc);
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");
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);
1373 RestoreDC (surface->win32.dc, -1);
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,
1387 cairo_antialias_t antialias,
1388 const cairo_clip_t *clip)
1390 cairo_win32_printing_surface_t *surface = abstract_surface;
1391 cairo_int_status_t status;
1392 cairo_solid_pattern_t clear;
1394 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
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;
1404 if (surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE)
1405 return _cairo_win32_printing_surface_analyze_operation (surface, op, source);
1407 assert (_cairo_win32_printing_surface_operation_supported (surface, op, source));
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);
1414 switch (fill_rule) {
1415 case CAIRO_FILL_RULE_WINDING:
1416 SetPolyFillMode (surface->win32.dc, WINDING);
1418 case CAIRO_FILL_RULE_EVEN_ODD:
1419 SetPolyFillMode (surface->win32.dc, ALTERNATE);
1425 if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1426 status = _cairo_win32_printing_surface_select_solid_brush (surface, source);
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);
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,
1451 cairo_scaled_font_t *scaled_font,
1452 const cairo_clip_t *clip)
1455 cairo_glyph_t *unicode_glyphs;
1456 cairo_scaled_font_subsets_glyph_t subset_glyph;
1458 cairo_bool_t sequence_is_unicode;
1459 cairo_status_t status = CAIRO_STATUS_SUCCESS;
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.
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
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,
1479 &scaled_font->options);
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);
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,
1496 unicode_glyphs[i].index = subset_glyph.unicode;
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))
1506 status = _cairo_win32_surface_emit_glyphs (&surface->win32,
1508 sequence_is_unicode ? &unicode_glyphs[first] : &glyphs[first],
1511 ! sequence_is_unicode);
1513 if (i < num_glyphs - 1)
1514 sequence_is_unicode = unicode_glyphs[i + 1].index <= 0xffff;
1520 if (surface->has_ctm)
1521 cairo_scaled_font_destroy (scaled_font);
1523 free (unicode_glyphs);
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,
1534 cairo_scaled_font_t *scaled_font,
1535 const cairo_clip_t *clip)
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;
1542 cairo_matrix_t old_ctm;
1543 cairo_bool_t old_has_ctm;
1544 cairo_solid_pattern_t clear;
1546 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
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;
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.
1563 * If we are printing a bitmap font, use fallback images to
1564 * ensure the font is not substituted.
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;
1571 return _cairo_win32_printing_surface_analyze_operation (surface, op, source);
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
1581 for (i = 0; i < num_glyphs; i++) {
1582 status = _cairo_scaled_glyph_lookup (scaled_font,
1584 CAIRO_SCALED_GLYPH_INFO_PATH,
1590 return _cairo_win32_printing_surface_analyze_operation (surface, op, source);
1593 if (source->type == CAIRO_PATTERN_TYPE_SOLID) {
1594 cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) source;
1597 color = _cairo_win32_printing_surface_flatten_transparency (surface,
1599 opaque = cairo_pattern_create_rgb (GetRValue (color) / 255.0,
1600 GetGValue (color) / 255.0,
1601 GetBValue (color) / 255.0);
1603 return opaque->status;
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)
1611 return _cairo_win32_printing_surface_emit_win32_glyphs (surface,
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,
1630 CAIRO_SCALED_GLYPH_INFO_PATH,
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);
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);
1647 SetPolyFillMode (surface->win32.dc, WINDING);
1648 FillPath (surface->win32.dc);
1649 _cairo_win32_printing_surface_done_solid_brush (surface);
1651 SelectClipPath (surface->win32.dc, RGN_AND);
1652 status = _cairo_win32_printing_surface_paint_pattern (surface, source);
1655 RestoreDC (surface->win32.dc, -1);
1658 cairo_pattern_destroy (opaque);
1663 static const char **
1664 _cairo_win32_printing_surface_get_supported_mime_types (void *abstract_surface)
1666 return _cairo_win32_printing_supported_mime_types;
1669 static cairo_status_t
1670 _cairo_win32_printing_surface_finish (void *abstract_surface)
1672 cairo_win32_printing_surface_t *surface = abstract_surface;
1674 if (surface->font_subsets != NULL)
1675 _cairo_scaled_font_subsets_destroy (surface->font_subsets);
1677 return CAIRO_STATUS_SUCCESS;
1680 static cairo_surface_t *
1681 _cairo_win32_printing_surface_create_similar (void *abstract_surface,
1682 cairo_content_t content,
1686 cairo_rectangle_t extents;
1688 extents.x = extents.y = 0;
1689 extents.width = width;
1690 extents.height = height;
1691 return cairo_recording_surface_create (content, &extents);
1694 static cairo_int_status_t
1695 _cairo_win32_printing_surface_start_page (void *abstract_surface)
1697 cairo_win32_printing_surface_t *surface = abstract_surface;
1699 double x_res, y_res;
1700 cairo_matrix_t inverse_ctm;
1701 cairo_status_t status;
1703 SaveDC (surface->win32.dc); /* Save application context first, before doing MWT */
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
1711 * transform the device context DC by the pattern to device matrix
1712 * replay the recording surface
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.
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.
1727 * For printing device contexts where 1 unit is 1 dpi, switching
1728 * the GDI CTM to identity maximises the possible resolution of
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.
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.
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;
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");
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);
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);
1776 SaveDC (surface->win32.dc); /* Then save Cairo's known-good clip state, so the clip path can be reset */
1778 return CAIRO_STATUS_SUCCESS;
1782 _cairo_win32_printing_surface_set_paginated_mode (void *abstract_surface,
1783 cairo_paginated_mode_t paginated_mode)
1785 cairo_win32_printing_surface_t *surface = abstract_surface;
1787 surface->paginated_mode = paginated_mode;
1791 _cairo_win32_printing_surface_supports_fine_grained_fallbacks (void *abstract_surface)
1797 * cairo_win32_printing_surface_create:
1798 * @hdc: the DC to create a surface for
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.
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.
1810 * Return value: the newly created surface
1815 cairo_win32_printing_surface_create (HDC hdc)
1817 cairo_win32_printing_surface_t *surface;
1818 cairo_surface_t *paginated;
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));
1826 if (_cairo_win32_save_initial_clip (hdc, surface) != CAIRO_STATUS_SUCCESS) {
1828 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1832 _cairo_surface_clipper_init (&surface->clipper,
1833 _cairo_win32_printing_surface_clipper_intersect_clip_path);
1835 surface->win32.format = CAIRO_FORMAT_RGB24;
1836 surface->win32.base.content = CAIRO_CONTENT_COLOR_ALPHA;
1838 surface->win32.dc = hdc;
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) {
1845 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
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;
1854 surface->win32.flags = _cairo_win32_flags_for_dc (surface->win32.dc);
1855 surface->win32.flags |= CAIRO_WIN32_SURFACE_FOR_PRINTING;
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,
1863 CAIRO_CONTENT_COLOR_ALPHA);
1865 paginated = _cairo_paginated_surface_create (&surface->win32.base,
1866 CAIRO_CONTENT_COLOR_ALPHA,
1867 &cairo_win32_surface_paginated_backend);
1869 /* paginated keeps the only reference to surface now, drop ours */
1870 cairo_surface_destroy (&surface->win32.base);
1875 static const cairo_surface_backend_t cairo_win32_printing_surface_backend = {
1876 CAIRO_SURFACE_TYPE_WIN32_PRINTING,
1877 _cairo_win32_printing_surface_finish,
1879 _cairo_default_context_create,
1881 _cairo_win32_printing_surface_create_similar,
1882 NULL, /* create similar image */
1883 NULL, /* map to image */
1884 NULL, /* unmap image */
1886 _cairo_surface_default_source,
1887 NULL, /* acquire_source_image */
1888 NULL, /* release_source_image */
1889 NULL, /* snapshot */
1891 NULL, /* copy_page */
1892 _cairo_win32_printing_surface_show_page,
1894 _cairo_win32_surface_get_extents,
1895 _cairo_win32_printing_surface_get_font_options,
1898 NULL, /* mark_dirty_rectangle */
1900 _cairo_win32_printing_surface_paint,
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,
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,