Subversion Repositories Scribus

Rev

Rev 14952 | Details | Compare with Previous | Last modification | View Log | RSS feed

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