CairoOutputDev.cc 110 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1
2
3
4
5
6
7
8
//========================================================================
//
// CairoOutputDev.cc
//
// Copyright 2003 Glyph & Cog, LLC
// Copyright 2004 Red Hat, Inc
//
//========================================================================
9
10
11
12
13

//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
14
15
16
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
17
18
// Copyright (C) 2005-2008 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
19
// Copyright (C) 2005, 2009, 2012, 2017-2021 Albert Astals Cid <aacid@kde.org>
20
// Copyright (C) 2005 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
Albert Astals Cid's avatar
Albert Astals Cid committed
21
// Copyright (C) 2006-2011, 2013, 2014, 2017, 2018 Carlos Garcia Campos <carlosgc@gnome.org>
22
// Copyright (C) 2008 Carl Worth <cworth@cworth.org>
Albert Astals Cid's avatar
Albert Astals Cid committed
23
// Copyright (C) 2008-2018, 2021 Adrian Johnson <ajohnson@redneon.com>
24
// Copyright (C) 2008 Michael Vrable <mvrable@cs.ucsd.edu>
Albert Astals Cid's avatar
Albert Astals Cid committed
25
// Copyright (C) 2008, 2009 Chris Wilson <chris@chris-wilson.co.uk>
26
// Copyright (C) 2008, 2012 Hib Eris <hib@hiberis.nl>
27
// Copyright (C) 2009, 2010 David Benjamin <davidben@mit.edu>
28
// Copyright (C) 2011-2014 Thomas Freitag <Thomas.Freitag@alfa.de>
Albert Astals Cid's avatar
Albert Astals Cid committed
29
// Copyright (C) 2012 Patrick Pfeifer <p2000@mailinator.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
30
// Copyright (C) 2012, 2015, 2016 Jason Crain <jason@aquaticape.us>
Albert Astals Cid's avatar
0.31.0    
Albert Astals Cid committed
31
// Copyright (C) 2015 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
32
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
33
// Copyright (C) 2018, 2020 Adam Reichold <adam.reichold@t-online.de>
Albert Astals Cid's avatar
Albert Astals Cid committed
34
// Copyright (C) 2019, 2020 Marek Kasik <mkasik@redhat.com>
35
// Copyright (C) 2020 Michal <sudolskym@gmail.com>
36
// Copyright (C) 2020 Oliver Sander <oliver.sander@tu-dresden.de>
Albert Astals Cid's avatar
Albert Astals Cid committed
37
// Copyright (C) 2021 Uli Schlachter <psychon@znc.in>
Albert Astals Cid's avatar
Albert Astals Cid committed
38
// Copyright (C) 2021 Christian Persch <chpe@src.gnome.org>
39
40
41
42
43
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
Kristian Høgsberg's avatar
Kristian Høgsberg committed
44
45
46

#include <config.h>

47
#include <cstdint>
48
49
50
#include <cstring>
#include <cmath>
#include <cassert>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
51
52
53
54
55
56
#include <cairo.h>

#include "goo/gfile.h"
#include "GlobalParams.h"
#include "Error.h"
#include "Object.h"
57
#include "Gfx.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
58
59
#include "GfxState.h"
#include "GfxFont.h"
60
#include "Page.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
61
62
#include "Link.h"
#include "FontEncodingTables.h"
63
#include "PDFDocEncoding.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
64
65
66
67
#include <fofi/FoFiTrueType.h>
#include <splash/SplashBitmap.h>
#include "CairoOutputDev.h"
#include "CairoFontEngine.h"
68
#include "CairoRescaleBox.h"
69
#include "UnicodeMap.h"
70
#include "JBIG2Stream.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
71
72
//------------------------------------------------------------------------

73
// #define LOG_CAIRO
Kristian Høgsberg's avatar
Kristian Høgsberg committed
74

75
76
77
78
79
// To limit memory usage and improve performance when printing, limit
// cairo images to this size. 8192 is sufficient for an A2 sized
// 300ppi image.
#define MAX_PRINT_IMAGE_SIZE 8192

Kristian Høgsberg's avatar
Kristian Høgsberg committed
80
#ifdef LOG_CAIRO
Albert Astals Cid's avatar
Albert Astals Cid committed
81
#    define LOG(x) (x)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
82
#else
Albert Astals Cid's avatar
Albert Astals Cid committed
83
#    define LOG(x)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
84
85
#endif

Albert Astals Cid's avatar
Albert Astals Cid committed
86
87
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
Adrian Johnson's avatar
Adrian Johnson committed
88

Albert Astals Cid's avatar
Albert Astals Cid committed
89
90
91
92
//------------------------------------------------------------------------
// CairoImage
//------------------------------------------------------------------------

Albert Astals Cid's avatar
Albert Astals Cid committed
93
94
95
96
97
98
99
CairoImage::CairoImage(double x1A, double y1A, double x2A, double y2A)
{
    image = nullptr;
    x1 = x1A;
    y1 = y1A;
    x2 = x2A;
    y2 = y2A;
Albert Astals Cid's avatar
Albert Astals Cid committed
100
101
}

Albert Astals Cid's avatar
Albert Astals Cid committed
102
103
104
105
CairoImage::~CairoImage()
{
    if (image)
        cairo_surface_destroy(image);
Albert Astals Cid's avatar
Albert Astals Cid committed
106
}
Kristian Høgsberg's avatar
Kristian Høgsberg committed
107

Albert Astals Cid's avatar
Albert Astals Cid committed
108
109
110
111
112
void CairoImage::setImage(cairo_surface_t *i)
{
    if (image)
        cairo_surface_destroy(image);
    image = cairo_surface_reference(i);
113
114
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
115
116
117
118
//------------------------------------------------------------------------
// CairoOutputDev
//------------------------------------------------------------------------

119
120
121
122
123
124
125
// We cannot tie the lifetime of an FT_Library object to that of
// CairoOutputDev, since any FT_Faces created with it may end up with a
// reference by Cairo which can be held long after the CairoOutputDev is
// deleted.  The simplest way to avoid problems is to never tear down the
// FT_Library instance; to avoid leaks, just use a single global instance
// initialized the first time it is needed.
FT_Library CairoOutputDev::ft_lib;
126
std::once_flag CairoOutputDev::ft_lib_once_flag;
127

Albert Astals Cid's avatar
Albert Astals Cid committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
CairoOutputDev::CairoOutputDev()
{
    doc = nullptr;

    std::call_once(ft_lib_once_flag, FT_Init_FreeType, &ft_lib);

    fontEngine = nullptr;
    fontEngine_owner = false;
    glyphs = nullptr;
    fill_pattern = nullptr;
    fill_color.r = fill_color.g = fill_color.b = 0;
    stroke_pattern = nullptr;
    stroke_color.r = stroke_color.g = stroke_color.b = 0;
    stroke_opacity = 1.0;
    fill_opacity = 1.0;
    textClipPath = nullptr;
    strokePathClip = nullptr;
    cairo = nullptr;
    currentFont = nullptr;
147
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 14, 0)
Albert Astals Cid's avatar
Albert Astals Cid committed
148
    prescaleImages = false;
149
#else
Albert Astals Cid's avatar
Albert Astals Cid committed
150
    prescaleImages = true;
151
#endif
Albert Astals Cid's avatar
Albert Astals Cid committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    printing = true;
    use_show_text_glyphs = false;
    inUncoloredPattern = false;
    inType3Char = false;
    t3_glyph_has_bbox = false;
    text_matrix_valid = true;

    groupColorSpaceStack = nullptr;
    maskStack = nullptr;
    group = nullptr;
    mask = nullptr;
    shape = nullptr;
    cairo_shape = nullptr;
    knockoutCount = 0;

    textPage = nullptr;
    actualText = nullptr;

    // the SA parameter supposedly defaults to false, but Acrobat
    // apparently hardwires it to true
    stroke_adjust = true;
    align_stroke_coords = false;
    adjusted_stroke_width = false;
    xref = nullptr;
}

CairoOutputDev::~CairoOutputDev()
{
    if (fontEngine_owner && fontEngine) {
        delete fontEngine;
    }
183
184
185
186
    if (textClipPath) {
        cairo_path_destroy(textClipPath);
        textClipPath = nullptr;
    }
Albert Astals Cid's avatar
Albert Astals Cid committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

    if (cairo)
        cairo_destroy(cairo);
    cairo_pattern_destroy(stroke_pattern);
    cairo_pattern_destroy(fill_pattern);
    if (group)
        cairo_pattern_destroy(group);
    if (mask)
        cairo_pattern_destroy(mask);
    if (shape)
        cairo_pattern_destroy(shape);
    if (textPage)
        textPage->decRefCnt();
    if (actualText)
        delete actualText;
202
}
Kristian Høgsberg's avatar
Kristian Høgsberg committed
203

204
void CairoOutputDev::setCairo(cairo_t *c)
205
{
Albert Astals Cid's avatar
Albert Astals Cid committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    if (cairo != nullptr) {
        cairo_status_t status = cairo_status(cairo);
        if (status) {
            error(errInternal, -1, "cairo context error: {0:s}\n", cairo_status_to_string(status));
        }
        cairo_destroy(cairo);
        assert(!cairo_shape);
    }
    if (c != nullptr) {
        cairo = cairo_reference(c);
        /* save the initial matrix so that we can use it for type3 fonts. */
        // XXX: is this sufficient? could we miss changes to the matrix somehow?
        cairo_get_matrix(cairo, &orig_matrix);
    } else {
        cairo = nullptr;
        cairo_shape = nullptr;
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
223
224
}

225
226
void CairoOutputDev::setTextPage(TextPage *text)
{
Albert Astals Cid's avatar
Albert Astals Cid committed
227
228
229
230
231
232
233
234
235
236
237
238
    if (textPage)
        textPage->decRefCnt();
    if (actualText)
        delete actualText;
    if (text) {
        textPage = text;
        textPage->incRefCnt();
        actualText = new ActualText(text);
    } else {
        textPage = nullptr;
        actualText = nullptr;
    }
239
240
}

241
void CairoOutputDev::copyAntialias(cairo_t *cr, cairo_t *source_cr)
242
{
243
    cairo_set_antialias(cr, cairo_get_antialias(source_cr));
244

245
246
    cairo_font_options_t *font_options = cairo_font_options_create();
    cairo_get_font_options(source_cr, font_options);
Albert Astals Cid's avatar
Albert Astals Cid committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    cairo_set_font_options(cr, font_options);
    cairo_font_options_destroy(font_options);
}

void CairoOutputDev::startDoc(PDFDoc *docA, CairoFontEngine *parentFontEngine)
{
    doc = docA;
    if (parentFontEngine) {
        fontEngine = parentFontEngine;
    } else {
        if (fontEngine) {
            delete fontEngine;
        }
        fontEngine = new CairoFontEngine(ft_lib);
        fontEngine_owner = true;
    }
    xref = doc->getXRef();
}

void CairoOutputDev::startPage(int pageNum, GfxState *state, XRef *xrefA)
{
    /* set up some per page defaults */
269
    cairo_pattern_destroy(fill_pattern);
Albert Astals Cid's avatar
Albert Astals Cid committed
270
    cairo_pattern_destroy(stroke_pattern);
271

Albert Astals Cid's avatar
Albert Astals Cid committed
272
273
274
275
276
277
278
279
280
281
    fill_pattern = cairo_pattern_create_rgb(0., 0., 0.);
    fill_color.r = fill_color.g = fill_color.b = 0;
    stroke_pattern = cairo_pattern_reference(fill_pattern);
    stroke_color.r = stroke_color.g = stroke_color.b = 0;

    if (textPage)
        textPage->startPage(state);
    if (xrefA != nullptr) {
        xref = xrefA;
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
282
283
}

Albert Astals Cid's avatar
Albert Astals Cid committed
284
285
286
287
288
289
290
void CairoOutputDev::endPage()
{
    if (textPage) {
        textPage->endPage();
        textPage->coalesce(true, 0, false);
    }
}
291

Albert Astals Cid's avatar
Albert Astals Cid committed
292
293
294
295
296
297
void CairoOutputDev::saveState(GfxState *state)
{
    LOG(printf("save\n"));
    cairo_save(cairo);
    if (cairo_shape)
        cairo_save(cairo_shape);
298

Albert Astals Cid's avatar
Albert Astals Cid committed
299
300
301
302
303
    MaskStack *ms = new MaskStack;
    ms->mask = cairo_pattern_reference(mask);
    ms->mask_matrix = mask_matrix;
    ms->next = maskStack;
    maskStack = ms;
304

Albert Astals Cid's avatar
Albert Astals Cid committed
305
306
    if (strokePathClip)
        strokePathClip->ref_count++;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
307
308
}

Albert Astals Cid's avatar
Albert Astals Cid committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
void CairoOutputDev::restoreState(GfxState *state)
{
    LOG(printf("restore\n"));
    cairo_restore(cairo);
    if (cairo_shape)
        cairo_restore(cairo_shape);

    text_matrix_valid = true;

    /* These aren't restored by cairo_restore() since we keep them in
     * the output device. */
    updateFillColor(state);
    updateStrokeColor(state);
    updateFillOpacity(state);
    updateStrokeOpacity(state);
    updateBlendMode(state);

    MaskStack *ms = maskStack;
    if (ms) {
        if (mask)
            cairo_pattern_destroy(mask);
        mask = ms->mask;
        mask_matrix = ms->mask_matrix;
        maskStack = ms->next;
        delete ms;
    }
335

Albert Astals Cid's avatar
Albert Astals Cid committed
336
337
338
339
340
341
342
343
    if (strokePathClip && --strokePathClip->ref_count == 0) {
        delete strokePathClip->path;
        if (strokePathClip->dashes)
            gfree(strokePathClip->dashes);
        gfree(strokePathClip);
        strokePathClip = nullptr;
    }
}
344

Albert Astals Cid's avatar
Albert Astals Cid committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
void CairoOutputDev::updateAll(GfxState *state)
{
    updateLineDash(state);
    updateLineJoin(state);
    updateLineCap(state);
    updateLineWidth(state);
    updateFlatness(state);
    updateMiterLimit(state);
    updateFillColor(state);
    updateStrokeColor(state);
    updateFillOpacity(state);
    updateStrokeOpacity(state);
    updateBlendMode(state);
    needFontUpdate = true;
    if (textPage)
        textPage->updateFont(state);
}

void CairoOutputDev::setDefaultCTM(const double *ctm)
{
    cairo_matrix_t matrix;
    matrix.xx = ctm[0];
    matrix.yx = ctm[1];
    matrix.xy = ctm[2];
    matrix.yy = ctm[3];
    matrix.x0 = ctm[4];
    matrix.y0 = ctm[5];

    cairo_transform(cairo, &matrix);
    if (cairo_shape)
        cairo_transform(cairo_shape, &matrix);
376

Albert Astals Cid's avatar
Albert Astals Cid committed
377
    OutputDev::setDefaultCTM(ctm);
378
379
}

Albert Astals Cid's avatar
Albert Astals Cid committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
void CairoOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
{
    cairo_matrix_t matrix, invert_matrix;
    matrix.xx = m11;
    matrix.yx = m12;
    matrix.xy = m21;
    matrix.yy = m22;
    matrix.x0 = m31;
    matrix.y0 = m32;

    /* Make sure the matrix is invertible before setting it.
     * cairo will blow up if we give it a matrix that's not
     * invertible, so we need to check before passing it
     * to cairo_transform. Ignoring it is likely to give better
     * results than not rendering anything at all. See #14398
     *
     * Ideally, we could do the cairo_transform
     * and then check if anything went wrong and fix it then
     * instead of having to invert the matrix. */
    invert_matrix = matrix;
    if (cairo_matrix_invert(&invert_matrix)) {
        error(errSyntaxWarning, -1, "matrix not invertible\n");
        return;
    }

    cairo_transform(cairo, &matrix);
    if (cairo_shape)
        cairo_transform(cairo_shape, &matrix);
    updateLineDash(state);
    updateLineJoin(state);
    updateLineCap(state);
    updateLineWidth(state);
}
413

Albert Astals Cid's avatar
Albert Astals Cid committed
414
415
416
417
418
void CairoOutputDev::updateLineDash(GfxState *state)
{
    double *dashPattern;
    int dashLength;
    double dashStart;
419

Albert Astals Cid's avatar
Albert Astals Cid committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    state->getLineDash(&dashPattern, &dashLength, &dashStart);
    cairo_set_dash(cairo, dashPattern, dashLength, dashStart);
    if (cairo_shape)
        cairo_set_dash(cairo_shape, dashPattern, dashLength, dashStart);
}

void CairoOutputDev::updateFlatness(GfxState *state)
{
    // cairo_set_tolerance (cairo, state->getFlatness());
}

void CairoOutputDev::updateLineJoin(GfxState *state)
{
    switch (state->getLineJoin()) {
    case 0:
        cairo_set_line_join(cairo, CAIRO_LINE_JOIN_MITER);
        break;
    case 1:
        cairo_set_line_join(cairo, CAIRO_LINE_JOIN_ROUND);
        break;
    case 2:
        cairo_set_line_join(cairo, CAIRO_LINE_JOIN_BEVEL);
        break;
    }
    if (cairo_shape)
        cairo_set_line_join(cairo_shape, cairo_get_line_join(cairo));
}

void CairoOutputDev::updateLineCap(GfxState *state)
{
    switch (state->getLineCap()) {
    case 0:
        cairo_set_line_cap(cairo, CAIRO_LINE_CAP_BUTT);
        break;
    case 1:
        cairo_set_line_cap(cairo, CAIRO_LINE_CAP_ROUND);
        break;
    case 2:
        cairo_set_line_cap(cairo, CAIRO_LINE_CAP_SQUARE);
        break;
    }
    if (cairo_shape)
        cairo_set_line_cap(cairo_shape, cairo_get_line_cap(cairo));
}

void CairoOutputDev::updateMiterLimit(GfxState *state)
{
    cairo_set_miter_limit(cairo, state->getMiterLimit());
    if (cairo_shape)
        cairo_set_miter_limit(cairo_shape, state->getMiterLimit());
}

void CairoOutputDev::updateLineWidth(GfxState *state)
{
    LOG(printf("line width: %f\n", state->getLineWidth()));
    adjusted_stroke_width = false;
    double width = state->getLineWidth();
    if (stroke_adjust && !printing) {
        double x, y;
        x = y = width;

        /* find out line width in device units */
        cairo_user_to_device_distance(cairo, &x, &y);
        if (fabs(x) <= 1.0 && fabs(y) <= 1.0) {
            /* adjust width to at least one device pixel */
            x = y = 1.0;
            cairo_device_to_user_distance(cairo, &x, &y);
            width = MIN(fabs(x), fabs(y));
            adjusted_stroke_width = true;
        }
    } else if (width == 0.0) {
        /* Cairo does not support 0 line width == 1 device pixel. Find out
         * how big pixels (device unit) are in the x and y
         * directions. Choose the smaller of the two as our line width.
         */
        double x = 1.0, y = 1.0;
        if (printing) {
            // assume printer pixel size is 1/600 inch
            x = 72.0 / 600;
            y = 72.0 / 600;
        }
        cairo_device_to_user_distance(cairo, &x, &y);
        width = MIN(fabs(x), fabs(y));
    }
    cairo_set_line_width(cairo, width);
    if (cairo_shape)
        cairo_set_line_width(cairo_shape, cairo_get_line_width(cairo));
}

void CairoOutputDev::updateFillColor(GfxState *state)
{
    GfxRGB color = fill_color;

    if (inUncoloredPattern)
        return;

    state->getFillRGB(&fill_color);
    if (cairo_pattern_get_type(fill_pattern) != CAIRO_PATTERN_TYPE_SOLID || color.r != fill_color.r || color.g != fill_color.g || color.b != fill_color.b) {
        cairo_pattern_destroy(fill_pattern);
        fill_pattern = cairo_pattern_create_rgba(colToDbl(fill_color.r), colToDbl(fill_color.g), colToDbl(fill_color.b), fill_opacity);

        LOG(printf("fill color: %d %d %d\n", fill_color.r, fill_color.g, fill_color.b));
    }
}

void CairoOutputDev::updateStrokeColor(GfxState *state)
{
    GfxRGB color = stroke_color;

    if (inUncoloredPattern)
        return;

    state->getStrokeRGB(&stroke_color);
    if (cairo_pattern_get_type(fill_pattern) != CAIRO_PATTERN_TYPE_SOLID || color.r != stroke_color.r || color.g != stroke_color.g || color.b != stroke_color.b) {
        cairo_pattern_destroy(stroke_pattern);
        stroke_pattern = cairo_pattern_create_rgba(colToDbl(stroke_color.r), colToDbl(stroke_color.g), colToDbl(stroke_color.b), stroke_opacity);

        LOG(printf("stroke color: %d %d %d\n", stroke_color.r, stroke_color.g, stroke_color.b));
    }
}

void CairoOutputDev::updateFillOpacity(GfxState *state)
{
    double opacity = fill_opacity;

    if (inUncoloredPattern)
        return;

    fill_opacity = state->getFillOpacity();
    if (opacity != fill_opacity) {
        cairo_pattern_destroy(fill_pattern);
        fill_pattern = cairo_pattern_create_rgba(colToDbl(fill_color.r), colToDbl(fill_color.g), colToDbl(fill_color.b), fill_opacity);

        LOG(printf("fill opacity: %f\n", fill_opacity));
    }
}

void CairoOutputDev::updateStrokeOpacity(GfxState *state)
{
    double opacity = stroke_opacity;

    if (inUncoloredPattern)
        return;

    stroke_opacity = state->getStrokeOpacity();
    if (opacity != stroke_opacity) {
        cairo_pattern_destroy(stroke_pattern);
        stroke_pattern = cairo_pattern_create_rgba(colToDbl(stroke_color.r), colToDbl(stroke_color.g), colToDbl(stroke_color.b), stroke_opacity);

        LOG(printf("stroke opacity: %f\n", stroke_opacity));
    }
}

void CairoOutputDev::updateFillColorStop(GfxState *state, double offset)
{
    if (inUncoloredPattern)
        return;

    state->getFillRGB(&fill_color);

    // If stroke pattern is set then the current fill is clipped
    // to a stroke path.  In that case, the stroke opacity has to be used
    // rather than the fill opacity.
    // See https://gitlab.freedesktop.org/poppler/poppler/issues/178
    auto opacity = (state->getStrokePattern()) ? state->getStrokeOpacity() : state->getFillOpacity();

    cairo_pattern_add_color_stop_rgba(fill_pattern, offset, colToDbl(fill_color.r), colToDbl(fill_color.g), colToDbl(fill_color.b), opacity);
    LOG(printf("fill color stop: %f (%d, %d, %d, %d)\n", offset, fill_color.r, fill_color.g, fill_color.b, dblToCol(opacity)));
}

void CairoOutputDev::updateBlendMode(GfxState *state)
{
    switch (state->getBlendMode()) {
    default:
    case gfxBlendNormal:
        cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
        break;
    case gfxBlendMultiply:
        cairo_set_operator(cairo, CAIRO_OPERATOR_MULTIPLY);
        break;
    case gfxBlendScreen:
        cairo_set_operator(cairo, CAIRO_OPERATOR_SCREEN);
        break;
    case gfxBlendOverlay:
        cairo_set_operator(cairo, CAIRO_OPERATOR_OVERLAY);
        break;
    case gfxBlendDarken:
        cairo_set_operator(cairo, CAIRO_OPERATOR_DARKEN);
        break;
    case gfxBlendLighten:
        cairo_set_operator(cairo, CAIRO_OPERATOR_LIGHTEN);
        break;
    case gfxBlendColorDodge:
        cairo_set_operator(cairo, CAIRO_OPERATOR_COLOR_DODGE);
        break;
    case gfxBlendColorBurn:
        cairo_set_operator(cairo, CAIRO_OPERATOR_COLOR_BURN);
        break;
    case gfxBlendHardLight:
        cairo_set_operator(cairo, CAIRO_OPERATOR_HARD_LIGHT);
        break;
    case gfxBlendSoftLight:
        cairo_set_operator(cairo, CAIRO_OPERATOR_SOFT_LIGHT);
        break;
    case gfxBlendDifference:
        cairo_set_operator(cairo, CAIRO_OPERATOR_DIFFERENCE);
        break;
    case gfxBlendExclusion:
        cairo_set_operator(cairo, CAIRO_OPERATOR_EXCLUSION);
        break;
    case gfxBlendHue:
        cairo_set_operator(cairo, CAIRO_OPERATOR_HSL_HUE);
        break;
    case gfxBlendSaturation:
        cairo_set_operator(cairo, CAIRO_OPERATOR_HSL_SATURATION);
        break;
    case gfxBlendColor:
        cairo_set_operator(cairo, CAIRO_OPERATOR_HSL_COLOR);
        break;
    case gfxBlendLuminosity:
        cairo_set_operator(cairo, CAIRO_OPERATOR_HSL_LUMINOSITY);
        break;
    }
    LOG(printf("blend mode: %d\n", (int)state->getBlendMode()));
}

void CairoOutputDev::updateFont(GfxState *state)
{
    cairo_font_face_t *font_face;
    cairo_matrix_t matrix, invert_matrix;

    LOG(printf("updateFont() font=%s\n", state->getFont()->getName()->c_str()));

    needFontUpdate = false;

    // FIXME: use cairo font engine?
    if (textPage)
        textPage->updateFont(state);

    currentFont = fontEngine->getFont(state->getFont(), doc, printing, xref);

    if (!currentFont)
        return;

    font_face = currentFont->getFontFace();
    cairo_set_font_face(cairo, font_face);

    use_show_text_glyphs = state->getFont()->hasToUnicodeCMap() && cairo_surface_has_show_text_glyphs(cairo_get_target(cairo));

    double fontSize = state->getFontSize();
    const double *m = state->getTextMat();
    /* NOTE: adjusting by a constant is hack. The correct solution
     * is probably to use user-fonts and compute the scale on a per
     * glyph basis instead of for the entire font */
    double w = currentFont->getSubstitutionCorrection(state->getFont());
    matrix.xx = m[0] * fontSize * state->getHorizScaling() * w;
    matrix.yx = m[1] * fontSize * state->getHorizScaling() * w;
    matrix.xy = -m[2] * fontSize;
    matrix.yy = -m[3] * fontSize;
    matrix.x0 = 0;
    matrix.y0 = 0;

    LOG(printf("font matrix: %f %f %f %f\n", matrix.xx, matrix.yx, matrix.xy, matrix.yy));

    /* Make sure the font matrix is invertible before setting it.  cairo
     * will blow up if we give it a matrix that's not invertible, so we
     * need to check before passing it to cairo_set_font_matrix. Ignoring it
     * is likely to give better results than not rendering anything at
     * all. See #18254.
     */
    invert_matrix = matrix;
    if (cairo_matrix_invert(&invert_matrix)) {
        error(errSyntaxWarning, -1, "font matrix not invertible");
        text_matrix_valid = false;
        return;
    }

    cairo_set_font_matrix(cairo, &matrix);
    text_matrix_valid = true;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
699
700
}

701
702
703
704
705
706
/* Tolerance in pixels for checking if strokes are horizontal or vertical
 * lines in device space */
#define STROKE_COORD_TOLERANCE 0.5

/* Align stroke coordinate i if the point is the start or end of a
 * horizontal or vertical line */
707
void CairoOutputDev::alignStrokeCoords(const GfxSubpath *subpath, int i, double *x, double *y)
708
{
Albert Astals Cid's avatar
Albert Astals Cid committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
    double x1, y1, x2, y2;
    bool align = false;

    x1 = subpath->getX(i);
    y1 = subpath->getY(i);
    cairo_user_to_device(cairo, &x1, &y1);

    // Does the current coord and prev coord form a horiz or vert line?
    if (i > 0 && !subpath->getCurve(i - 1)) {
        x2 = subpath->getX(i - 1);
        y2 = subpath->getY(i - 1);
        cairo_user_to_device(cairo, &x2, &y2);
        if (fabs(x2 - x1) < STROKE_COORD_TOLERANCE || fabs(y2 - y1) < STROKE_COORD_TOLERANCE)
            align = true;
    }

    // Does the current coord and next coord form a horiz or vert line?
    if (i < subpath->getNumPoints() - 1 && !subpath->getCurve(i + 1)) {
        x2 = subpath->getX(i + 1);
        y2 = subpath->getY(i + 1);
        cairo_user_to_device(cairo, &x2, &y2);
        if (fabs(x2 - x1) < STROKE_COORD_TOLERANCE || fabs(y2 - y1) < STROKE_COORD_TOLERANCE)
            align = true;
    }

    *x = subpath->getX(i);
    *y = subpath->getY(i);
    if (align) {
        /* see http://www.cairographics.org/FAQ/#sharp_lines */
        cairo_user_to_device(cairo, x, y);
        *x = floor(*x) + 0.5;
        *y = floor(*y) + 0.5;
        cairo_device_to_user(cairo, x, y);
    }
743
744
}

745
746
#undef STROKE_COORD_TOLERANCE

Albert Astals Cid's avatar
Albert Astals Cid committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
void CairoOutputDev::doPath(cairo_t *c, GfxState *state, const GfxPath *path)
{
    int i, j;
    double x, y;
    cairo_new_path(c);
    for (i = 0; i < path->getNumSubpaths(); ++i) {
        const GfxSubpath *subpath = path->getSubpath(i);
        if (subpath->getNumPoints() > 0) {
            if (align_stroke_coords) {
                alignStrokeCoords(subpath, 0, &x, &y);
            } else {
                x = subpath->getX(0);
                y = subpath->getY(0);
            }
            cairo_move_to(c, x, y);
            j = 1;
            while (j < subpath->getNumPoints()) {
                if (subpath->getCurve(j)) {
                    if (align_stroke_coords) {
                        alignStrokeCoords(subpath, j + 2, &x, &y);
                    } else {
                        x = subpath->getX(j + 2);
                        y = subpath->getY(j + 2);
                    }
                    cairo_curve_to(c, subpath->getX(j), subpath->getY(j), subpath->getX(j + 1), subpath->getY(j + 1), x, y);

                    j += 3;
                } else {
                    if (align_stroke_coords) {
                        alignStrokeCoords(subpath, j, &x, &y);
                    } else {
                        x = subpath->getX(j);
                        y = subpath->getY(j);
                    }
                    cairo_line_to(c, x, y);
                    ++j;
                }
            }
            if (subpath->isClosed()) {
                LOG(printf("close\n"));
                cairo_close_path(c);
            }
        }
    }
}

void CairoOutputDev::stroke(GfxState *state)
{
    if (inType3Char) {
        GfxGray gray;
        state->getFillGray(&gray);
        if (colToDbl(gray) > 0.5)
            return;
    }

    if (adjusted_stroke_width)
        align_stroke_coords = true;
    doPath(cairo, state, state->getPath());
    align_stroke_coords = false;
    cairo_set_source(cairo, stroke_pattern);
    LOG(printf("stroke\n"));
808
    if (strokePathClip) {
Albert Astals Cid's avatar
Albert Astals Cid committed
809
810
811
812
813
814
815
816
817
818
819
820
        cairo_push_group(cairo);
        cairo_stroke(cairo);
        cairo_pop_group_to_source(cairo);
        fillToStrokePathClip(state);
    } else {
        cairo_stroke(cairo);
    }
    if (cairo_shape) {
        doPath(cairo_shape, state, state->getPath());
        cairo_stroke(cairo_shape);
    }
}
821

Albert Astals Cid's avatar
Albert Astals Cid committed
822
823
824
825
826
827
828
829
void CairoOutputDev::fill(GfxState *state)
{
    if (inType3Char) {
        GfxGray gray;
        state->getFillGray(&gray);
        if (colToDbl(gray) > 0.5)
            return;
    }
830

Albert Astals Cid's avatar
Albert Astals Cid committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
    doPath(cairo, state, state->getPath());
    cairo_set_fill_rule(cairo, CAIRO_FILL_RULE_WINDING);
    cairo_set_source(cairo, fill_pattern);
    LOG(printf("fill\n"));
    // XXX: how do we get the path
    if (mask) {
        cairo_save(cairo);
        cairo_clip(cairo);
        if (strokePathClip) {
            cairo_push_group(cairo);
            fillToStrokePathClip(state);
            cairo_pop_group_to_source(cairo);
        }
        cairo_set_matrix(cairo, &mask_matrix);
        cairo_mask(cairo, mask);
        cairo_restore(cairo);
    } else if (strokePathClip) {
        fillToStrokePathClip(state);
    } else {
        cairo_fill(cairo);
    }
    if (cairo_shape) {
        cairo_set_fill_rule(cairo_shape, CAIRO_FILL_RULE_WINDING);
        doPath(cairo_shape, state, state->getPath());
        cairo_fill(cairo_shape);
    }
}
858

Albert Astals Cid's avatar
Albert Astals Cid committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
void CairoOutputDev::eoFill(GfxState *state)
{
    doPath(cairo, state, state->getPath());
    cairo_set_fill_rule(cairo, CAIRO_FILL_RULE_EVEN_ODD);
    cairo_set_source(cairo, fill_pattern);
    LOG(printf("fill-eo\n"));

    if (mask) {
        cairo_save(cairo);
        cairo_clip(cairo);
        cairo_set_matrix(cairo, &mask_matrix);
        cairo_mask(cairo, mask);
        cairo_restore(cairo);
    } else {
        cairo_fill(cairo);
    }
    if (cairo_shape) {
        cairo_set_fill_rule(cairo_shape, CAIRO_FILL_RULE_EVEN_ODD);
        doPath(cairo_shape, state, state->getPath());
        cairo_fill(cairo_shape);
    }
}
881

882
bool CairoOutputDev::tilingPatternFill(GfxState *state, Gfx *gfxA, Catalog *cat, GfxTilingPattern *tPat, const double *mat, int x0, int y0, int x1, int y1, double xStep, double yStep)
Albert Astals Cid's avatar
Albert Astals Cid committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
{
    PDFRectangle box;
    Gfx *gfx;
    cairo_pattern_t *pattern;
    cairo_surface_t *surface;
    cairo_matrix_t matrix;
    cairo_matrix_t pattern_matrix;
    cairo_t *old_cairo;
    double xMin, yMin, xMax, yMax;
    double width, height;
    double scaleX, scaleY;
    int surface_width, surface_height;
    StrokePathClip *strokePathTmp;
    bool adjusted_stroke_width_tmp;
    cairo_pattern_t *maskTmp;
898
899
900
901
902
    const double *bbox = tPat->getBBox();
    const double *pmat = tPat->getMatrix();
    const int paintType = tPat->getPaintType();
    Dict *resDict = tPat->getResDict();
    Object *str = tPat->getContentStream();
Albert Astals Cid's avatar
Albert Astals Cid committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932

    width = bbox[2] - bbox[0];
    height = bbox[3] - bbox[1];

    if (xStep != width || yStep != height)
        return false;
    /* TODO: implement the other cases here too */

    // Find the width and height of the transformed pattern
    cairo_get_matrix(cairo, &matrix);
    cairo_matrix_init(&pattern_matrix, mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]);
    cairo_matrix_multiply(&matrix, &matrix, &pattern_matrix);

    double widthX = width, widthY = 0;
    cairo_matrix_transform_distance(&matrix, &widthX, &widthY);
    surface_width = ceil(sqrt(widthX * widthX + widthY * widthY));

    double heightX = 0, heightY = height;
    cairo_matrix_transform_distance(&matrix, &heightX, &heightY);
    surface_height = ceil(sqrt(heightX * heightX + heightY * heightY));
    scaleX = surface_width / width;
    scaleY = surface_height / height;

    surface = cairo_surface_create_similar(cairo_get_target(cairo), CAIRO_CONTENT_COLOR_ALPHA, surface_width, surface_height);
    if (cairo_surface_status(surface))
        return false;

    old_cairo = cairo;
    cairo = cairo_create(surface);
    cairo_surface_destroy(surface);
933
    copyAntialias(cairo, old_cairo);
Albert Astals Cid's avatar
Albert Astals Cid committed
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978

    box.x1 = bbox[0];
    box.y1 = bbox[1];
    box.x2 = bbox[2];
    box.y2 = bbox[3];
    cairo_scale(cairo, scaleX, scaleY);
    cairo_translate(cairo, -box.x1, -box.y1);

    strokePathTmp = strokePathClip;
    strokePathClip = nullptr;
    adjusted_stroke_width_tmp = adjusted_stroke_width;
    maskTmp = mask;
    mask = nullptr;
    gfx = new Gfx(doc, this, resDict, &box, nullptr, nullptr, nullptr, gfxA);
    if (paintType == 2)
        inUncoloredPattern = true;
    gfx->display(str);
    if (paintType == 2)
        inUncoloredPattern = false;
    delete gfx;
    strokePathClip = strokePathTmp;
    adjusted_stroke_width = adjusted_stroke_width_tmp;
    mask = maskTmp;

    pattern = cairo_pattern_create_for_surface(cairo_get_target(cairo));
    cairo_destroy(cairo);
    cairo = old_cairo;
    if (cairo_pattern_status(pattern))
        return false;

    // Cairo can fail if the pattern translation is too large. Fix by making the
    // translation smaller.
    const double det = pmat[0] * pmat[3] - pmat[1] * pmat[2];

    // Find the number of repetitions of pattern we need to shift by. Transform
    // the translation component of pmat (pmat[4] and pmat[5]) into the pattern's
    // coordinate system by multiplying by inverse of pmat, then divide by
    // pattern size (xStep and yStep).
    const double xoffset = round((pmat[3] * pmat[4] - pmat[2] * pmat[5]) / (xStep * det));
    const double yoffset = -round((pmat[1] * pmat[4] - pmat[0] * pmat[5]) / (yStep * det));

    if (!std::isfinite(xoffset) || !std::isfinite(yoffset)) {
        error(errSyntaxWarning, -1, "CairoOutputDev: Singular matrix in tilingPatternFill");
        return false;
    }
979

Albert Astals Cid's avatar
Albert Astals Cid committed
980
981
982
    // Shift pattern_matrix by multiples of the pattern size.
    pattern_matrix.x0 -= xoffset * pattern_matrix.xx * xStep + yoffset * pattern_matrix.xy * yStep;
    pattern_matrix.y0 -= xoffset * pattern_matrix.yx * xStep + yoffset * pattern_matrix.yy * yStep;
983

Albert Astals Cid's avatar
Albert Astals Cid committed
984
985
    state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
    cairo_rectangle(cairo, xMin, yMin, xMax - xMin, yMax - yMin);
986

Albert Astals Cid's avatar
Albert Astals Cid committed
987
988
989
    cairo_matrix_init_scale(&matrix, scaleX, scaleY);
    cairo_matrix_translate(&matrix, -box.x1, -box.y1);
    cairo_pattern_set_matrix(pattern, &matrix);
990

Albert Astals Cid's avatar
Albert Astals Cid committed
991
992
993
994
995
996
997
998
    cairo_transform(cairo, &pattern_matrix);
    cairo_set_source(cairo, pattern);
    cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
    if (strokePathClip) {
        fillToStrokePathClip(state);
    } else {
        cairo_fill(cairo);
    }
999

Albert Astals Cid's avatar
Albert Astals Cid committed
1000
    cairo_pattern_destroy(pattern);
1001

Albert Astals Cid's avatar
Albert Astals Cid committed
1002
    return true;
1003
1004
}

1005
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
1006
bool CairoOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading)
1007
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
    // Function shaded fills are subdivided to rectangles that are the
    // following size in device space.  Note when printing this size is
    // in points.
    const int subdivide_pixels = 10;

    double x_begin, x_end, x1, x2;
    double y_begin, y_end, y1, y2;
    double x_step;
    double y_step;
    GfxColor color;
    GfxRGB rgb;
    cairo_matrix_t mat;

    const double *matrix = shading->getMatrix();
    mat.xx = matrix[0];
    mat.yx = matrix[1];
    mat.xy = matrix[2];
    mat.yy = matrix[3];
    mat.x0 = matrix[4];
    mat.y0 = matrix[5];
    if (cairo_matrix_invert(&mat)) {
        error(errSyntaxWarning, -1, "matrix not invertible\n");
        return false;
1031
1032
    }

Albert Astals Cid's avatar
Albert Astals Cid committed
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
    // get cell size in pattern space
    x_step = y_step = subdivide_pixels;
    cairo_matrix_transform_distance(&mat, &x_step, &y_step);

    cairo_pattern_destroy(fill_pattern);
    fill_pattern = cairo_pattern_create_mesh();
    cairo_pattern_set_matrix(fill_pattern, &mat);
    shading->getDomain(&x_begin, &y_begin, &x_end, &y_end);

    for (x1 = x_begin; x1 < x_end; x1 += x_step) {
        x2 = x1 + x_step;
        if (x2 > x_end)
            x2 = x_end;

        for (y1 = y_begin; y1 < y_end; y1 += y_step) {
            y2 = y1 + y_step;
            if (y2 > y_end)
                y2 = y_end;

            cairo_mesh_pattern_begin_patch(fill_pattern);
            cairo_mesh_pattern_move_to(fill_pattern, x1, y1);
            cairo_mesh_pattern_line_to(fill_pattern, x2, y1);
            cairo_mesh_pattern_line_to(fill_pattern, x2, y2);
            cairo_mesh_pattern_line_to(fill_pattern, x1, y2);

            shading->getColor(x1, y1, &color);
            shading->getColorSpace()->getRGB(&color, &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, 0, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));

            shading->getColor(x2, y1, &color);
            shading->getColorSpace()->getRGB(&color, &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, 1, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));

            shading->getColor(x2, y2, &color);
            shading->getColorSpace()->getRGB(&color, &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, 2, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));

            shading->getColor(x1, y2, &color);
            shading->getColorSpace()->getRGB(&color, &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, 3, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));

            cairo_mesh_pattern_end_patch(fill_pattern);
        }
    }

    double xMin, yMin, xMax, yMax;
    // get the clip region bbox
    state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
    state->moveTo(xMin, yMin);
    state->lineTo(xMin, yMax);
    state->lineTo(xMax, yMax);
    state->lineTo(xMax, yMin);
    state->closePath();
    fill(state);
    state->clearPath();

    return true;
1090
1091
1092
}
#endif /* CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) */

Albert Astals Cid's avatar
Albert Astals Cid committed
1093
1094
1095
1096
bool CairoOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading, double tMin, double tMax)
{
    double x0, y0, x1, y1;
    double dx, dy;
1097

Albert Astals Cid's avatar
Albert Astals Cid committed
1098
1099
1100
    shading->getCoords(&x0, &y0, &x1, &y1);
    dx = x1 - x0;
    dy = y1 - y0;
1101

Albert Astals Cid's avatar
Albert Astals Cid committed
1102
1103
1104
1105
1106
1107
    cairo_pattern_destroy(fill_pattern);
    fill_pattern = cairo_pattern_create_linear(x0 + tMin * dx, y0 + tMin * dy, x0 + tMax * dx, y0 + tMax * dy);
    if (!shading->getExtend0() && !shading->getExtend1())
        cairo_pattern_set_extend(fill_pattern, CAIRO_EXTEND_NONE);
    else
        cairo_pattern_set_extend(fill_pattern, CAIRO_EXTEND_PAD);
1108

Albert Astals Cid's avatar
Albert Astals Cid committed
1109
    LOG(printf("axial-sh\n"));
1110

Albert Astals Cid's avatar
Albert Astals Cid committed
1111
1112
1113
    // TODO: use the actual stops in the shading in the case
    // of linear interpolation (Type 2 Exponential functions with N=1)
    return false;
1114
1115
}

1116
bool CairoOutputDev::axialShadedSupportExtend(GfxState *state, GfxAxialShading *shading)
1117
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1118
    return (shading->getExtend0() == shading->getExtend1());
1119
1120
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
bool CairoOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading, double sMin, double sMax)
{
    double x0, y0, r0, x1, y1, r1;
    double dx, dy, dr;
    cairo_matrix_t matrix;
    double scale;

    shading->getCoords(&x0, &y0, &r0, &x1, &y1, &r1);
    dx = x1 - x0;
    dy = y1 - y0;
    dr = r1 - r0;

    // Cairo/pixman do not work well with a very large or small scaled
    // matrix.  See cairo bug #81657.
    //
    // As a workaround, scale the pattern by the average of the vertical
    // and horizontal scaling of the current transformation matrix.
    cairo_get_matrix(cairo, &matrix);
    scale = (sqrt(matrix.xx * matrix.xx + matrix.yx * matrix.yx) + sqrt(matrix.xy * matrix.xy + matrix.yy * matrix.yy)) / 2;
    cairo_matrix_init_scale(&matrix, scale, scale);
Jason Crain's avatar
Jason Crain committed
1141

Albert Astals Cid's avatar
Albert Astals Cid committed
1142
1143
1144
1145
1146
1147
1148
    cairo_pattern_destroy(fill_pattern);
    fill_pattern = cairo_pattern_create_radial((x0 + sMin * dx) * scale, (y0 + sMin * dy) * scale, (r0 + sMin * dr) * scale, (x0 + sMax * dx) * scale, (y0 + sMax * dy) * scale, (r0 + sMax * dr) * scale);
    cairo_pattern_set_matrix(fill_pattern, &matrix);
    if (shading->getExtend0() && shading->getExtend1())
        cairo_pattern_set_extend(fill_pattern, CAIRO_EXTEND_PAD);
    else
        cairo_pattern_set_extend(fill_pattern, CAIRO_EXTEND_NONE);
1149

Albert Astals Cid's avatar
Albert Astals Cid committed
1150
    LOG(printf("radial-sh\n"));
1151

Albert Astals Cid's avatar
Albert Astals Cid committed
1152
    return false;
1153
1154
}

1155
bool CairoOutputDev::radialShadedSupportExtend(GfxState *state, GfxRadialShading *shading)
1156
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1157
    return (shading->getExtend0() == shading->getExtend1());
1158
1159
}

1160
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
1161
bool CairoOutputDev::gouraudTriangleShadedFill(GfxState *state, GfxGouraudTriangleShading *shading)
1162
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1163
1164
1165
1166
    double x0, y0, x1, y1, x2, y2;
    GfxColor color[3];
    int i, j;
    GfxRGB rgb;
1167

Albert Astals Cid's avatar
Albert Astals Cid committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
    cairo_pattern_destroy(fill_pattern);
    fill_pattern = cairo_pattern_create_mesh();

    for (i = 0; i < shading->getNTriangles(); i++) {
        if (shading->isParameterized()) {
            double color0, color1, color2;
            shading->getTriangle(i, &x0, &y0, &color0, &x1, &y1, &color1, &x2, &y2, &color2);
            shading->getParameterizedColor(color0, &color[0]);
            shading->getParameterizedColor(color1, &color[1]);
            shading->getParameterizedColor(color2, &color[2]);
        } else {
            shading->getTriangle(i, &x0, &y0, &color[0], &x1, &y1, &color[1], &x2, &y2, &color[2]);
        }
1181

Albert Astals Cid's avatar
Albert Astals Cid committed
1182
        cairo_mesh_pattern_begin_patch(fill_pattern);
1183

Albert Astals Cid's avatar
Albert Astals Cid committed
1184
1185
1186
        cairo_mesh_pattern_move_to(fill_pattern, x0, y0);
        cairo_mesh_pattern_line_to(fill_pattern, x1, y1);
        cairo_mesh_pattern_line_to(fill_pattern, x2, y2);
1187

Albert Astals Cid's avatar
Albert Astals Cid committed
1188
1189
1190
1191
        for (j = 0; j < 3; j++) {
            shading->getColorSpace()->getRGB(&color[j], &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, j, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));
        }
1192

Albert Astals Cid's avatar
Albert Astals Cid committed
1193
1194
        cairo_mesh_pattern_end_patch(fill_pattern);
    }
1195

Albert Astals Cid's avatar
Albert Astals Cid committed
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
    double xMin, yMin, xMax, yMax;
    // get the clip region bbox
    state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
    state->moveTo(xMin, yMin);
    state->lineTo(xMin, yMax);
    state->lineTo(xMax, yMax);
    state->lineTo(xMax, yMin);
    state->closePath();
    fill(state);
    state->clearPath();
1206

Albert Astals Cid's avatar
Albert Astals Cid committed
1207
    return true;
1208
1209
}

1210
bool CairoOutputDev::patchMeshShadedFill(GfxState *state, GfxPatchMeshShading *shading)
1211
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1212
    int i, j, k;
1213

Albert Astals Cid's avatar
Albert Astals Cid committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
    cairo_pattern_destroy(fill_pattern);
    fill_pattern = cairo_pattern_create_mesh();

    for (i = 0; i < shading->getNPatches(); i++) {
        const GfxPatch *patch = shading->getPatch(i);
        GfxColor color;
        GfxRGB rgb;

        cairo_mesh_pattern_begin_patch(fill_pattern);

        cairo_mesh_pattern_move_to(fill_pattern, patch->x[0][0], patch->y[0][0]);
        cairo_mesh_pattern_curve_to(fill_pattern, patch->x[0][1], patch->y[0][1], patch->x[0][2], patch->y[0][2], patch->x[0][3], patch->y[0][3]);

        cairo_mesh_pattern_curve_to(fill_pattern, patch->x[1][3], patch->y[1][3], patch->x[2][3], patch->y[2][3], patch->x[3][3], patch->y[3][3]);

        cairo_mesh_pattern_curve_to(fill_pattern, patch->x[3][2], patch->y[3][2], patch->x[3][1], patch->y[3][1], patch->x[3][0], patch->y[3][0]);

        cairo_mesh_pattern_curve_to(fill_pattern, patch->x[2][0], patch->y[2][0], patch->x[1][0], patch->y[1][0], patch->x[0][0], patch->y[0][0]);

        cairo_mesh_pattern_set_control_point(fill_pattern, 0, patch->x[1][1], patch->y[1][1]);
        cairo_mesh_pattern_set_control_point(fill_pattern, 1, patch->x[1][2], patch->y[1][2]);
        cairo_mesh_pattern_set_control_point(fill_pattern, 2, patch->x[2][2], patch->y[2][2]);
        cairo_mesh_pattern_set_control_point(fill_pattern, 3, patch->x[2][1], patch->y[2][1]);

        for (j = 0; j < 4; j++) {
            int u, v;

            switch (j) {
            case 0:
                u = 0;
                v = 0;
                break;
            case 1:
                u = 0;
                v = 1;
                break;
            case 2:
                u = 1;
                v = 1;
                break;
            case 3:
                u = 1;
                v = 0;
                break;
            }

            if (shading->isParameterized()) {
                shading->getParameterizedColor(patch->color[u][v].c[0], &color);
            } else {
                for (k = 0; k < shading->getColorSpace()->getNComps(); k++) {
                    // simply cast to the desired type; that's all what is needed.
                    color.c[k] = GfxColorComp(patch->color[u][v].c[k]);
                }
            }

            shading->getColorSpace()->getRGB(&color, &rgb);
            cairo_mesh_pattern_set_corner_color_rgb(fill_pattern, j, colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b));
        }
        cairo_mesh_pattern_end_patch(fill_pattern);
    }
1274

Albert Astals Cid's avatar
Albert Astals Cid committed
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
    double xMin, yMin, xMax, yMax;
    // get the clip region bbox
    state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
    state->moveTo(xMin, yMin);
    state->lineTo(xMin, yMax);
    state->lineTo(xMax, yMax);
    state->lineTo(xMax, yMin);
    state->closePath();
    fill(state);
    state->clearPath();
1285

Albert Astals Cid's avatar
Albert Astals Cid committed
1286
    return true;
1287
}
1288
#endif /* CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0) */
1289

Albert Astals Cid's avatar
Albert Astals Cid committed
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
void CairoOutputDev::clip(GfxState *state)
{
    doPath(cairo, state, state->getPath());
    cairo_set_fill_rule(cairo, CAIRO_FILL_RULE_WINDING);
    cairo_clip(cairo);
    LOG(printf("clip\n"));
    if (cairo_shape) {
        doPath(cairo_shape, state, state->getPath());
        cairo_set_fill_rule(cairo_shape, CAIRO_FILL_RULE_WINDING);
        cairo_clip(cairo_shape);
    }
1301
1302
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
void CairoOutputDev::eoClip(GfxState *state)
{
    doPath(cairo, state, state->getPath());
    cairo_set_fill_rule(cairo, CAIRO_FILL_RULE_EVEN_ODD);
    cairo_clip(cairo);
    LOG(printf("clip-eo\n"));
    if (cairo_shape) {
        doPath(cairo_shape, state, state->getPath());
        cairo_set_fill_rule(cairo_shape, CAIRO_FILL_RULE_EVEN_ODD);
        cairo_clip(cairo_shape);
    }
}

void CairoOutputDev::clipToStrokePath(GfxState *state)
1317
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
    LOG(printf("clip-to-stroke-path\n"));
    strokePathClip = (StrokePathClip *)gmalloc(sizeof(*strokePathClip));
    strokePathClip->path = state->getPath()->copy();
    cairo_get_matrix(cairo, &strokePathClip->ctm);
    strokePathClip->line_width = cairo_get_line_width(cairo);
    strokePathClip->dash_count = cairo_get_dash_count(cairo);
    if (strokePathClip->dash_count) {
        strokePathClip->dashes = (double *)gmallocn(sizeof(double), strokePathClip->dash_count);
        cairo_get_dash(cairo, strokePathClip->dashes, &strokePathClip->dash_offset);
    } else {
        strokePathClip->dashes = nullptr;
    }
    strokePathClip->cap = cairo_get_line_cap(cairo);
    strokePathClip->join = cairo_get_line_join(cairo);
    strokePathClip->miter = cairo_get_miter_limit(cairo);
    strokePathClip->ref_count = 1;
}
1335

Albert Astals Cid's avatar
Albert Astals Cid committed
1336
1337
1338
void CairoOutputDev::fillToStrokePathClip(GfxState *state)
{
    cairo_save(cairo);
1339

Albert Astals Cid's avatar
Albert Astals Cid committed
1340
1341
1342
1343
1344
1345
1346
1347
    cairo_set_matrix(cairo, &strokePathClip->ctm);
    cairo_set_line_width(cairo, strokePathClip->line_width);
    cairo_set_dash(cairo, strokePathClip->dashes, strokePathClip->dash_count, strokePathClip->dash_offset);
    cairo_set_line_cap(cairo, strokePathClip->cap);
    cairo_set_line_join(cairo, strokePathClip->join);
    cairo_set_miter_limit(cairo, strokePathClip->miter);
    doPath(cairo, state, strokePathClip->path);
    cairo_stroke(cairo);
1348

Albert Astals Cid's avatar
Albert Astals Cid committed
1349
    cairo_restore(cairo);
1350
1351
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1352
void CairoOutputDev::beginString(GfxState *state, const GooString *s)
1353
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
    int len = s->getLength();

    if (needFontUpdate)
        updateFont(state);

    if (!currentFont)
        return;

    glyphs = (cairo_glyph_t *)gmallocn(len, sizeof(cairo_glyph_t));
    glyphCount = 0;
1364
    if (use_show_text_glyphs) {
Albert Astals Cid's avatar
Albert Astals Cid committed
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
        clusters = (cairo_text_cluster_t *)gmallocn(len, sizeof(cairo_text_cluster_t));
        clusterCount = 0;
        utf8Max = len * 2; // start with twice the number of glyphs. we will realloc if we need more.
        utf8 = (char *)gmalloc(utf8Max);
        utf8Count = 0;
    }
}

void CairoOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, int nBytes, const Unicode *u, int uLen)
{
    if (currentFont) {
        glyphs[glyphCount].index = currentFont->getGlyph(code, u, uLen);
        glyphs[glyphCount].x = x - originX;
        glyphs[glyphCount].y = y - originY;
        glyphCount++;
        if (use_show_text_glyphs) {
            const UnicodeMap *utf8Map = globalParams->getUtf8Map();
            if (utf8Max - utf8Count < uLen * 6) {
                // utf8 encoded characters can be up to 6 bytes
                if (utf8Max > uLen * 6)
                    utf8Max *= 2;
                else
                    utf8Max += 2 * uLen * 6;
                utf8 = (char *)grealloc(utf8, utf8Max);
            }
            clusters[clusterCount].num_bytes = 0;
            for (int i = 0; i < uLen; i++) {
                int size = utf8Map->mapUnicode(u[i], utf8 + utf8Count, utf8Max - utf8Count);
                utf8Count += size;
                clusters[clusterCount].num_bytes += size;
            }
            clusters[clusterCount].num_glyphs = 1;
            clusterCount++;
        }
    }

    if (!textPage)
        return;
    actualText->addChar(state, x, y, dx, dy, code, nBytes, u, uLen);
1404
1405
1406
1407
}

void CairoOutputDev::endString(GfxState *state)
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
    int render;

    if (!currentFont)
        return;

    // endString can be called without a corresponding beginString. If this
    // happens glyphs will be null so don't draw anything, just return.
    // XXX: OutputDevs should probably not have to deal with this...
    if (!glyphs)
        return;

    // ignore empty strings and invisible text -- this is used by
    // Acrobat Capture
    render = state->getRender();
    if (render == 3 || glyphCount == 0 || !text_matrix_valid) {
        goto finish;
    }

    if (!(render & 1)) {
        LOG(printf("fill string\n"));
        cairo_set_source(cairo, fill_pattern);
        if (use_show_text_glyphs)
            cairo_show_text_glyphs(cairo, utf8, utf8Count, glyphs, glyphCount, clusters, clusterCount, (cairo_text_cluster_flags_t)0);
        else
            cairo_show_glyphs(cairo, glyphs, glyphCount);
        if (cairo_shape)
            cairo_show_glyphs(cairo_shape, glyphs, glyphCount);
1435
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1436

Albert Astals Cid's avatar
Albert Astals Cid committed
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
    // stroke
    if ((render & 3) == 1 || (render & 3) == 2) {
        LOG(printf("stroke string\n"));
        cairo_set_source(cairo, stroke_pattern);
        cairo_glyph_path(cairo, glyphs, glyphCount);
        cairo_stroke(cairo);
        if (cairo_shape) {
            cairo_glyph_path(cairo_shape, glyphs, glyphCount);
            cairo_stroke(cairo_shape);
        }
    }
1448

Albert Astals Cid's avatar
Albert Astals Cid committed
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
    // clip
    if ((render & 4)) {
        LOG(printf("clip string\n"));
        // append the glyph path to textClipPath.

        // set textClipPath as the currentPath
        if (textClipPath) {
            cairo_append_path(cairo, textClipPath);
            if (cairo_shape) {
                cairo_append_path(cairo_shape, textClipPath);
            }
            cairo_path_destroy(textClipPath);
        }

        // append the glyph path
        cairo_glyph_path(cairo, glyphs, glyphCount);

        // move the path back into textClipPath
        // and clear the current path
        textClipPath = cairo_copy_path(cairo);
        cairo_new_path(cairo);
        if (cairo_shape) {
            cairo_new_path(cairo_shape);
        }
1473
    }
1474

1475
finish:
Albert Astals Cid's avatar
Albert Astals Cid committed
1476
1477
1478
1479
1480
1481
1482
    gfree(glyphs);
    glyphs = nullptr;
    if (use_show_text_glyphs) {
        gfree(clusters);
        clusters = nullptr;
        gfree(utf8);
        utf8 = nullptr;
1483
    }
1484
}
1485

Albert Astals Cid's avatar
Albert Astals Cid committed
1486
bool CairoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, const Unicode *u, int uLen)
1487
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511

    cairo_save(cairo);
    cairo_matrix_t matrix;

    const double *ctm = state->getCTM();
    matrix.xx = ctm[0];
    matrix.yx = ctm[1];
    matrix.xy = ctm[2];
    matrix.yy = ctm[3];
    matrix.x0 = ctm[4];
    matrix.y0 = ctm[5];
    /* Restore the original matrix and then transform to matrix needed for the
     * type3 font. This is ugly but seems to work. Perhaps there is a better way to do it?*/
    cairo_set_matrix(cairo, &orig_matrix);
    cairo_transform(cairo, &matrix);
    if (cairo_shape) {
        cairo_save(cairo_shape);
        cairo_set_matrix(cairo_shape, &orig_matrix);
        cairo_transform(cairo_shape, &matrix);
    }
    cairo_pattern_destroy(stroke_pattern);
    cairo_pattern_reference(fill_pattern);
    stroke_pattern = fill_pattern;
    return false;
1512
1513
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1514
void CairoOutputDev::endType3Char(GfxState *state)
1515
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1516
1517
1518
1519
    cairo_restore(cairo);
    if (cairo_shape) {
        cairo_restore(cairo_shape);
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1520
1521
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1522
1523
1524
1525
void CairoOutputDev::type3D0(GfxState *state, double wx, double wy)
{
    t3_glyph_wx = wx;
    t3_glyph_wy = wy;
1526
1527
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
void CairoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
{
    t3_glyph_wx = wx;
    t3_glyph_wy = wy;
    t3_glyph_bbox[0] = llx;
    t3_glyph_bbox[1] = lly;
    t3_glyph_bbox[2] = urx;
    t3_glyph_bbox[3] = ury;
    t3_glyph_has_bbox = true;
1537
1538
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
void CairoOutputDev::beginTextObject(GfxState *state) { }

void CairoOutputDev::endTextObject(GfxState *state)
{
    if (textClipPath) {
        // clip the accumulated text path
        cairo_append_path(cairo, textClipPath);
        cairo_clip(cairo);
        if (cairo_shape) {
            cairo_append_path(cairo_shape, textClipPath);
            cairo_clip(cairo_shape);
        }
        cairo_path_destroy(textClipPath);
        textClipPath = nullptr;
    }
1554
1555
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1556
void CairoOutputDev::beginActualText(GfxState *state, const GooString *text)
1557
{
Albert Astals Cid's avatar
Albert Astals Cid committed
1558
1559
1560
    if (textPage)
        actualText->begin(state, text);
}
1561

Albert Astals Cid's avatar
Albert Astals Cid committed
1562
1563
1564
1565
void CairoOutputDev::endActualText(GfxState *state)
{
    if (textPage)
        actualText->end(state);
1566
1567
}

Albert Astals Cid's avatar
Albert Astals Cid committed
1568
1569
1570
1571
static inline int splashRound(SplashCoord x)
{
    return (int)floor(x + 0.5);
}
1572

Albert Astals Cid's avatar
Albert Astals Cid committed
1573
1574
1575
1576
static inline int splashCeil(SplashCoord x)
{
    return (int)ceil(x);
}
1577

Albert Astals Cid's avatar
Albert Astals Cid committed
1578
1579
1580
1581
static inline int splashFloor(SplashCoord x)
{
    return (int)floor(x);
}
1582

Albert Astals Cid's avatar
Albert Astals Cid committed
1583
1584
1585
1586
static cairo_surface_t *cairo_surface_create_similar_clip(cairo_t *cairo, cairo_content_t content)
{
    cairo_pattern_t *pattern;
    cairo_surface_t *surface = nullptr;
1587

Albert Astals Cid's avatar
Albert Astals Cid committed
1588
1589
1590
1591
1592
1593
1594
    cairo_push_group_with_content(cairo, content);
    pattern = cairo_pop_group(cairo);
    cairo_pattern_get_surface(pattern, &surface);
    cairo_surface_reference(surface);
    cairo_pattern_destroy(pattern);
    return surface;
}
1595

Albert Astals Cid's avatar
Albert Astals Cid committed
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
void CairoOutputDev::beginTransparencyGroup(GfxState * /*state*/, const double * /*bbox*/, GfxColorSpace *blendingColorSpace, bool /*isolated*/, bool knockout, bool forSoftMask)
{
    /* push color space */
    ColorSpaceStack *css = new ColorSpaceStack;
    css->cs = blendingColorSpace;
    css->knockout = knockout;
    cairo_get_matrix(cairo, &css->group_matrix);
    css->next = groupColorSpaceStack;
    groupColorSpaceStack = css;

    LOG(printf("begin transparency group. knockout: %s\n", knockout ? "yes" : "no"));

    if (knockout) {
        knockoutCount++;
        if (!cairo_shape) {
            /* create a surface for tracking the shape */
            cairo_surface_t *cairo_shape_surface = cairo_surface_create_similar_clip(cairo, CAIRO_CONTENT_ALPHA);
            cairo_shape = cairo_create(cairo_shape_surface);
            cairo_surface_destroy(cairo_shape_surface);
1615
            copyAntialias(cairo_shape, cairo);
Albert Astals Cid's avatar