Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfd.c
Line
Count
Source
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Path drawing procedures for pdfwrite driver */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gxdevice.h"
22
#include "gxfixed.h"
23
#include "gxgstate.h"
24
#include "gxpaint.h"
25
#include "gxcoord.h"
26
#include "gxdevmem.h"
27
#include "gxcolor2.h"
28
#include "gxhldevc.h"
29
#include "gsstate.h"
30
#include "gxstate.h"
31
#include "gserrors.h"
32
#include "gsptype2.h"
33
#include "gsshade.h"
34
#include "gzpath.h"
35
#include "gzcpath.h"
36
#include "gdevpdfx.h"
37
#include "gdevpdfg.h"
38
#include "gdevpdfo.h"
39
#include "gsutil.h"
40
#include "gdevpdtf.h"
41
#include "gdevpdts.h"
42
#include "gxdevsop.h"
43
44
/* ---------------- Drawing ---------------- */
45
46
/* Fill a rectangle. */
47
int
48
gdev_pdf_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
49
                        gx_color_index color)
50
252M
{
51
252M
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
52
252M
    int code;
53
54
252M
    if (pdev->Eps2Write) {
55
252M
        float x0, y0, x1, y1;
56
252M
        gs_rect *Box;
57
58
252M
        if (!pdev->accumulating_charproc) {
59
250M
            Box = &pdev->BBox;
60
250M
            x0 = x / (pdev->HWResolution[0] / 72.0);
61
250M
            y0 = y / (pdev->HWResolution[1] / 72.0);
62
250M
            x1 = x0 + (w / (pdev->HWResolution[0] / 72.0));
63
250M
            y1 = y0 + (h / (pdev->HWResolution[1] / 72.0));
64
250M
        }
65
2.04M
        else {
66
2.04M
            Box = &pdev->charproc_BBox;
67
2.04M
            x0 = (float)x / 100;
68
2.04M
            y0 = (float)y / 100;
69
2.04M
            x1 = x0 + (w / 100);
70
2.04M
            y1 = y0 + (h / 100);
71
2.04M
        }
72
73
252M
        if (Box->p.x > x0)
74
27.2k
            Box->p.x = x0;
75
252M
        if (Box->p.y > y0)
76
21.0k
            Box->p.y = y0;
77
252M
        if (Box->q.x < x1)
78
2.41M
            Box->q.x = x1;
79
252M
        if (Box->q.y < y1)
80
1.61M
            Box->q.y = y1;
81
252M
        if (pdev->AccumulatingBBox)
82
251M
            return 0;
83
252M
    }
84
544k
    code = pdf_open_page(pdev, PDF_IN_STREAM);
85
544k
    if (code < 0)
86
0
        return code;
87
    /* Make sure we aren't being clipped. */
88
544k
    code = pdf_put_clip_path(pdev, NULL);
89
544k
    if (code < 0)
90
0
        return code;
91
544k
    pdf_set_pure_color(pdev, color, &pdev->saved_fill_color,
92
544k
                       &pdev->fill_used_process_color,
93
544k
                       &psdf_set_fill_color_commands);
94
544k
    if (!pdev->HaveStrokeColor)
95
511k
        pdev->saved_stroke_color = pdev->saved_fill_color;
96
544k
    pprintd4(pdev->strm, "%d %d %d %d re f\n", x, y, w, h);
97
544k
    return 0;
98
544k
}
99
100
/* ---------------- Path drawing ---------------- */
101
102
/* ------ Vector device implementation ------ */
103
104
static int
105
pdf_setlinewidth(gx_device_vector * vdev, double width)
106
133k
{
107
    /* Acrobat Reader doesn't accept negative line widths. */
108
133k
    return psdf_setlinewidth(vdev, fabs(width));
109
133k
}
110
111
static bool
112
pdf_can_handle_hl_color(gx_device_vector * vdev, const gs_gstate * pgs,
113
                 const gx_drawing_color * pdc)
114
2.19M
{
115
2.19M
    return pgs != NULL;
116
2.19M
}
117
118
static int
119
pdf_setfillcolor(gx_device_vector * vdev, const gs_gstate * pgs,
120
                 const gx_drawing_color * pdc)
121
1.14M
{
122
1.14M
    gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
123
1.14M
    bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pgs, pdc);
124
1.14M
    const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL);
125
126
1.14M
    if (!pdev->HaveStrokeColor) {
127
        /* opdfread.ps assumes same color for stroking and non-stroking operations. */
128
425k
        int code = pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_stroke_color,
129
425k
                                    &pdev->stroke_used_process_color,
130
425k
                                    &psdf_set_stroke_color_commands);
131
425k
        if (code < 0)
132
5.81k
            return code;
133
425k
    }
134
1.13M
    return pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_fill_color,
135
1.13M
                                 &pdev->fill_used_process_color,
136
1.13M
                                 &psdf_set_fill_color_commands);
137
1.14M
}
138
139
static int
140
pdf_setstrokecolor(gx_device_vector * vdev, const gs_gstate * pgs,
141
                   const gx_drawing_color * pdc)
142
134k
{
143
134k
    gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
144
134k
    bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pgs, pdc);
145
134k
    const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL);
146
147
134k
    if (!pdev->HaveStrokeColor) {
148
        /* opdfread.ps assumes same color for stroking and non-stroking operations. */
149
65.9k
        int code = pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_fill_color,
150
65.9k
                                 &pdev->fill_used_process_color,
151
65.9k
                                 &psdf_set_fill_color_commands);
152
65.9k
        if (code < 0)
153
125
            return code;
154
65.9k
    }
155
134k
    return pdf_set_drawing_color(pdev, pgs_for_hl_color, pdc, &pdev->saved_stroke_color,
156
134k
                                 &pdev->stroke_used_process_color,
157
134k
                                 &psdf_set_stroke_color_commands);
158
134k
}
159
160
static int
161
pdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1,
162
           gx_path_type_t type)
163
770k
{
164
770k
    gx_device_pdf *pdev = (gx_device_pdf *)vdev;
165
770k
    fixed xmax = int2fixed(32766), ymax = int2fixed(32766);
166
770k
    int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0);
167
770k
    fixed xmin = (pdev->sbstack_depth > bottom ? -xmax : 0);
168
770k
    fixed ymin = (pdev->sbstack_depth > bottom ? -ymax : 0);
169
170
    /*
171
     * If we're doing a stroke operation, expand the checking box by the
172
     * stroke width.
173
     */
174
770k
    if (type & gx_path_type_stroke) {
175
234k
        double w = vdev->state.line_params.half_width;
176
234k
        double xw = w * (fabs(vdev->state.ctm.xx) + fabs(vdev->state.ctm.yx));
177
234k
        int d = float2fixed(xw) + fixed_1;
178
179
234k
        xmin -= d;
180
234k
        xmax += d;
181
234k
        ymin -= d;
182
234k
        ymax += d;
183
234k
    }
184
770k
    if (pdev->PDFA == 1) {
185
        /* Check values, and decide how to proceed based on PDFACompatibilitylevel */
186
0
        if (x0 < xmin || y0 < ymin || x1 - x0 > xmax || y1 - y0 >ymax) {
187
0
            switch(pdev->PDFACompatibilityPolicy) {
188
0
                case 0:
189
0
                    emprintf(pdev->memory,
190
0
                         "Required co-ordinate outside valid range for PDF/A-1, reverting to normal PDF output.\n");
191
0
                    pdev->AbortPDFAX = true;
192
0
                    pdev->PDFA = 0;
193
0
                    break;
194
0
                case 1:
195
0
                    emprintf(pdev->memory,
196
0
                         "Required co-ordinate outside valid range for PDF/A-1, clamping to valid range, output may be incorrect.\n");
197
                    /*
198
                     * Clamp coordinates to avoid tripping over Acrobat Reader's limit
199
                     * of 32K on user coordinate values, which was adopted by the PDF/A-1 spec.
200
                     */
201
0
                    if (x0 < xmin)
202
0
                        x0 = xmin;
203
204
0
                    if (y0 < ymin)
205
0
                        y0 = ymin;
206
207
                    /* We used to clamp x1 and y1 here, but actually, because this is a rectangle
208
                     * we don't need to do that, we need to clamp the *difference* between x0,x1
209
                     * and y0,y1 to keep it inside the Acrobat 4 or 5 limits.
210
                     */
211
0
                    if (x1 - x0 > xmax)
212
0
                        x1 = x0 + xmax;
213
0
                    if (y1 - y0 > ymax)
214
0
                        y1 = y0 + ymax;
215
0
                    break;
216
0
                default:
217
0
                case 2:
218
0
                    emprintf(pdev->memory,
219
0
                         "Required co-ordinate outside valid range for PDF/A-1, aborting.\n");
220
0
                    return_error(gs_error_limitcheck);
221
0
                    break;
222
0
            }
223
0
        }
224
0
    }
225
770k
    return psdf_dorect(vdev, x0, y0, x1, y1, type);
226
770k
}
227
228
static int
229
pdf_endpath(gx_device_vector * vdev, gx_path_type_t type)
230
2.00M
{
231
2.00M
    return 0;     /* always handled by caller */
232
2.00M
}
233
234
const gx_device_vector_procs pdf_vector_procs = {
235
        /* Page management */
236
    NULL,
237
        /* Imager state */
238
    pdf_setlinewidth,
239
    psdf_setlinecap,
240
    psdf_setlinejoin,
241
    psdf_setmiterlimit,
242
    psdf_setdash,
243
    psdf_setflat,
244
    psdf_setlogop,
245
        /* Other state */
246
    pdf_can_handle_hl_color,
247
    pdf_setfillcolor,
248
    pdf_setstrokecolor,
249
        /* Paths */
250
    psdf_dopath,
251
    pdf_dorect,
252
    psdf_beginpath,
253
    psdf_moveto,
254
    psdf_lineto,
255
    psdf_curveto,
256
    psdf_closepath,
257
    pdf_endpath
258
};
259
260
/* ------ Utilities ------ */
261
262
/* Store a copy of clipping path. */
263
int
264
pdf_remember_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
265
344k
{
266
344k
    int code = 0;
267
    /* Used for skipping redundant clip paths. SF bug #624168. */
268
344k
    if (pdev->clip_path != 0) {
269
117k
        gx_path_free(pdev->clip_path, "pdf clip path");
270
117k
    }
271
344k
    if (pcpath == 0) {
272
207k
        pdev->clip_path = 0;
273
207k
        return 0;
274
207k
    }
275
136k
    pdev->clip_path = gx_path_alloc(pdev->pdf_memory, "pdf clip path");
276
136k
    if (pdev->clip_path == 0)
277
0
        return_error(gs_error_VMerror);
278
279
136k
    code = gx_cpath_to_path((gx_clip_path *)pcpath, pdev->clip_path);
280
136k
    if (code < 0)
281
0
        return code;
282
283
    /* gx_cpath_to_path above ends up going through gx_path_assign_preserve
284
     * which specifically states that the segments of the paths (in this case pcpath
285
     * and pdev->clip_path) must have been allocated with the same allocator.
286
     * If that's not true (eg pdfi running inside GS) then we need to 'unshare'
287
     * the path. Otherwise we mauy end up with pcpath being freed and discarded
288
     * while the pdfwrite devcie still thinks it has a pointer to it.
289
     */
290
136k
    if (pcpath->path.memory != pdev->pdf_memory)
291
136k
        code = gx_path_unshare(pdev->clip_path);
292
293
136k
    return code;
294
136k
}
295
296
/* Check if same clipping path. */
297
static int
298
pdf_is_same_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
299
356k
{
300
    /* Used for skipping redundant clip paths. SF bug #624168. */
301
356k
    gs_cpath_enum cenum;
302
356k
    gs_path_enum penum;
303
356k
    gs_fixed_point vs0[3], vs1[3];
304
356k
    int code, pe_op;
305
306
356k
    if ((pdev->clip_path != 0) != (pcpath != 0))
307
223k
        return 0;
308
    /* Both clip paths are empty, so the same */
309
133k
    if (pdev->clip_path == 0)
310
0
        return 1;
311
133k
    code = gx_path_enum_init(&penum, pdev->clip_path);
312
133k
    if (code < 0)
313
0
        return code;
314
133k
    code = gx_cpath_enum_init(&cenum, (gx_clip_path *)pcpath);
315
133k
    if (code < 0)
316
0
        return code;
317
    /* This flags a warning in Coverity, uninitialised variable cenum.first_visit */
318
    /* This is because gx_cpath_enum_init doesn't initialise first_visit, but the */
319
    /* variable can be used in enum_next. However, this is not truly used this    */
320
    /* way. The enum_init sets the 'state' to 'scan', and the first thing that happens */
321
    /* in enum_next when state is 'scan' is to set first_visit. */
322
305k
    while ((code = gx_cpath_enum_next(&cenum, vs0)) > 0) {
323
287k
        pe_op = gx_path_enum_next(&penum, vs1);
324
287k
        if (pe_op < 0)
325
0
            return pe_op;
326
287k
        if (pe_op != code)
327
790
            return 0;
328
287k
        switch (pe_op) {
329
0
            case gs_pe_curveto:
330
0
                if (vs0[1].x != vs1[1].x || vs0[1].y != vs1[1].y ||
331
0
                    vs0[2].x != vs1[2].x || vs0[2].y != vs1[2].y)
332
0
                    return 0;
333
                /* fall through */
334
140k
            case gs_pe_moveto:
335
274k
            case gs_pe_lineto:
336
274k
            case gs_pe_gapto:
337
274k
                if (vs0[0].x != vs1[0].x || vs0[0].y != vs1[0].y)
338
114k
                    return 0;
339
287k
        }
340
287k
    }
341
18.1k
    if (code < 0)
342
0
        return code;
343
18.1k
    code = gx_path_enum_next(&penum, vs1);
344
18.1k
    if (code < 0)
345
0
        return code;
346
18.1k
    return (code == 0);
347
18.1k
}
348
349
int
350
pdf_check_soft_mask(gx_device_pdf * pdev, gs_gstate * pgs)
351
9.11M
{
352
9.11M
    int code = 0;
353
354
9.11M
    if (pgs && pdev->state.soft_mask_id != pgs->soft_mask_id) {
355
    /*
356
     * The contents must be open already, so the following will only exit
357
     * text or string context.
358
     */
359
462
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
360
462
    if (code < 0)
361
0
        return code;
362
462
    if (pdev->vgstack_depth > pdev->vgstack_bottom) {
363
302
        code = pdf_restore_viewer_state(pdev, pdev->strm);
364
302
        if (code < 0)
365
0
            return code;
366
302
    }
367
462
    }
368
9.11M
    return code;
369
9.11M
}
370
371
/* Test whether we will need to put the clipping path. */
372
bool
373
pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
374
10.7M
{
375
10.7M
    if (pcpath == NULL) {
376
21.1k
        if (pdev->clip_path_id == pdev->no_clip_path_id)
377
18.6k
            return false;
378
10.7M
    } else {
379
10.7M
        if (pdev->clip_path_id == pcpath->id)
380
1.06M
            return false;
381
9.71M
        if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
382
9.71M
                                        int2fixed(pdev->width),
383
9.71M
                                        int2fixed(pdev->height)))
384
9.50M
            if (pdev->clip_path_id == pdev->no_clip_path_id)
385
9.49M
                return false;
386
220k
        if (pdf_is_same_clip_path(pdev, pcpath) > 0) {
387
18.0k
            pdev->clip_path_id = pcpath->id;
388
18.0k
            return false;
389
18.0k
        }
390
220k
    }
391
204k
    return true;
392
10.7M
}
393
394
static int pdf_write_path(gx_device_pdf * pdev, gs_path_enum *cenum, gdev_vector_dopath_state_t *state, gx_path *path, int is_clip_enum,
395
                               gx_path_type_t type, const gs_matrix *pmat)
396
1.91M
{
397
1.91M
    int pe_op;
398
1.91M
    gdev_vector_path_seg_record segments[5] = {0};
399
1.91M
    int i, seg_index = 0, is_rect = 1, buffering = 0, initial_m = 0, segs = 0, code, matrix_optimisable = 0;
400
1.91M
    gx_path_rectangular_type rtype = prt_none;
401
1.91M
    gs_fixed_rect rbox;
402
1.91M
    gx_device_vector *vdev = (gx_device_vector *)pdev;
403
1.91M
    gs_fixed_point line_start = {0,0};
404
1.91M
    gs_point p, q;
405
1.91M
    bool do_close = (type & (gx_path_type_clip | gx_path_type_stroke | gx_path_type_always_close)) != 0;
406
1.91M
    bool stored_moveto = false;
407
408
1.91M
    gdev_vector_dopath_init(state, (gx_device_vector *)pdev,
409
1.91M
                            type, pmat);
410
1.91M
    if (is_clip_enum)
411
50.9k
        code = gx_cpath_enum_init((gs_cpath_enum *)cenum, (gx_clip_path *)path);
412
1.86M
    else {
413
1.86M
        code = gx_path_enum_init(cenum, path);
414
1.86M
        rtype = gx_path_is_rectangular(path, &rbox);
415
1.86M
    }
416
1.91M
    if (code < 0)
417
0
        return code;
418
419
1.91M
    if((pmat == 0 || is_xxyy(pmat) || is_xyyx(pmat)) &&
420
1.89M
        (state->scale_mat.xx == 1.0 && state->scale_mat.yy == 1.0 &&
421
1.89M
        is_xxyy(&state->scale_mat) && is_fzero2(state->scale_mat.tx, state->scale_mat.ty))) {
422
1.82M
         matrix_optimisable = 1;
423
1.82M
         buffering = 1;
424
1.82M
    }
425
    /*
426
     * if the path type is stroke, we only recognize closed
427
     * rectangles; otherwise, we recognize all rectangles.
428
     * Note that for stroking with a transformation, we can't use dorect,
429
     * which requires (untransformed) device coordinates.
430
     */
431
1.91M
    if (rtype != prt_none &&
432
685k
        (!(type & gx_path_type_stroke) || rtype == prt_closed) &&
433
685k
          matrix_optimisable == 1)
434
675k
    {
435
675k
        gs_point p, q;
436
437
675k
        gs_point_transform_inverse((double)rbox.p.x, (double)rbox.p.y,
438
675k
                                   &state->scale_mat, &p);
439
675k
        gs_point_transform_inverse((double)rbox.q.x, (double)rbox.q.y,
440
675k
                                   &state->scale_mat, &q);
441
675k
        code = vdev_proc(vdev, dorect)(vdev, (fixed)p.x, (fixed)p.y,
442
675k
                                       (fixed)q.x, (fixed)q.y, type);
443
675k
        if (code >= 0) {
444
675k
            if (code == 0)
445
675k
                return 1;
446
0
            return code;
447
675k
        }
448
        /* If the dorect proc failed, use a general path. */
449
675k
    }
450
451
    /* The following is an optimisation for space. If we see a closed subpath
452
     * which is a rectangle, emit it as a 're' instead of writing the individual
453
     * segments of the path. Note that 're' always applies the width before the
454
     * height when constructing the path, so we must take care to ensure that
455
     * we get the path direction correct. We do ths by detecting whether the path
456
     * moves first in the x or y directoin, if it moves in the y direction first,
457
     * we simply move one vertex round the rectangle, ie we start at point 2.
458
     */
459
12.1M
    do {
460
12.1M
        if (is_clip_enum)
461
417k
            segments[seg_index].op = pe_op = gx_cpath_enum_next((gs_cpath_enum *)cenum, segments[seg_index].vs);
462
11.7M
        else
463
11.7M
            segments[seg_index].op = pe_op = gx_path_enum_next(cenum, segments[seg_index].vs);
464
465
12.1M
        if (segs == 0 && pe_op > 0)
466
1.23M
            segs = 1;
467
468
12.1M
        switch(pe_op) {
469
1.79M
            case gs_pe_moveto:
470
1.79M
            case gs_pe_gapto:
471
1.79M
                if (!buffering) {
472
327k
                    stored_moveto = true;
473
327k
                    line_start = segments[0].vs[0];
474
327k
                    seg_index = -1;
475
1.47M
                } else {
476
1.98M
                    for (i=0;i<seg_index;i++) {
477
515k
                        gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
478
515k
                    }
479
1.47M
                    line_start = segments[seg_index].vs[0];
480
1.47M
                    segments[0] = segments[seg_index];
481
1.47M
                    seg_index = 0;
482
1.47M
                    initial_m = 1;
483
1.47M
                }
484
1.79M
                break;
485
4.50M
            case gs_pe_lineto:
486
4.50M
                if (!buffering) {
487
2.39M
                    if (stored_moveto) {
488
201k
                        gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start);
489
201k
                        stored_moveto = false;
490
201k
                    }
491
4.79M
                    for (i=0;i<=seg_index;i++)
492
2.39M
                        gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
493
2.39M
                    seg_index = -1;
494
2.39M
                } else {
495
2.10M
                    if (!initial_m) {
496
0
                        for (i=0;i<=seg_index;i++) {
497
0
                            gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
498
0
                        }
499
0
                        buffering = 0;
500
0
                        seg_index = -1;
501
0
                    }
502
2.10M
                    if (type & gx_path_type_optimize && seg_index > 0) {
503
2.10M
                        if (segments[seg_index - 1].op == gs_pe_lineto) {
504
841k
                            if (segments[seg_index].vs[0].x == segments[seg_index - 1].vs[0].x && segments[seg_index].vs[0].x == line_start.x) {
505
104k
                                if (segments[seg_index - 1].vs[0].y > line_start.y && segments[seg_index].vs[0].y >= segments[seg_index - 1].vs[0].y) {
506
43.4k
                                    segments[seg_index - 1].vs[0].y = segments[seg_index].vs[0].y;
507
43.4k
                                    seg_index--;
508
61.4k
                                } else {
509
61.4k
                                    if (segments[seg_index - 1].vs[0].y < line_start.y && segments[seg_index].vs[0].y <= segments[seg_index - 1].vs[0].y) {
510
43.2k
                                        segments[seg_index - 1].vs[0].y = segments[seg_index].vs[0].y;
511
43.2k
                                        seg_index--;
512
43.2k
                                    } else
513
18.2k
                                        line_start = segments[seg_index - 1].vs[0];
514
61.4k
                                }
515
736k
                            } else {
516
736k
                                if (segments[seg_index].vs[0].y == segments[seg_index - 1].vs[0].y && segments[seg_index].vs[0].y == line_start.y) {
517
6.40k
                                    if (segments[seg_index - 1].vs[0].x > line_start.x && segments[seg_index].vs[0].x > segments[seg_index - 1].vs[0].x) {
518
560
                                        segments[seg_index - 1].vs[0].x = segments[seg_index].vs[0].x;
519
560
                                        seg_index--;
520
5.84k
                                    } else {
521
5.84k
                                        if (segments[seg_index - 1].vs[0].x < line_start.x && segments[seg_index].vs[0].x < segments[seg_index - 1].vs[0].x) {
522
589
                                            segments[seg_index - 1].vs[0].x = segments[seg_index].vs[0].x;
523
589
                                            seg_index--;
524
589
                                        } else
525
5.25k
                                            line_start = segments[seg_index - 1].vs[0];
526
5.84k
                                    }
527
6.40k
                                } else
528
730k
                                    line_start = segments[seg_index - 1].vs[0];
529
736k
                            }
530
841k
                        }
531
2.10M
                    }
532
2.10M
                }
533
4.50M
                break;
534
4.32M
            case gs_pe_curveto:
535
4.32M
                if (stored_moveto) {
536
122k
                    gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start);
537
122k
                    stored_moveto = false;
538
122k
                }
539
9.06M
                for (i=0;i<=seg_index;i++) {
540
4.74M
                    gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
541
4.74M
                }
542
4.32M
                line_start = segments[seg_index].vs[2];
543
4.32M
                seg_index = -1;
544
4.32M
                buffering = 0;
545
4.32M
                break;
546
407k
            case gs_pe_closepath:
547
407k
                if (!buffering || seg_index < 4) {
548
199k
                    if (stored_moveto && ((type & gx_path_type_stroke) && !(type & gx_path_type_fill)))
549
222
                        gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start);
550
199k
                    stored_moveto = false;
551
199k
                    if (!do_close) {
552
147k
                        i = seg_index;
553
148k
                        while (i > 0 && segments[i - 1].op == gs_pe_moveto) {
554
1.27k
                            segments[i - 1] = segments[i];
555
1.27k
                            i--;
556
1.27k
                        }
557
147k
                        seg_index = i;
558
425k
                        for (i=0;i<seg_index;i++)
559
277k
                            gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
560
561
147k
                        seg_index = 0;
562
147k
                        if (is_clip_enum)
563
0
                            segments[seg_index].op = pe_op = gx_cpath_enum_next((gs_cpath_enum *)cenum, segments[seg_index].vs);
564
147k
                        else
565
147k
                            segments[seg_index].op = pe_op = gx_path_enum_next(cenum, segments[seg_index].vs);
566
567
147k
                        if (pe_op > 0) {
568
29.2k
                            gdev_vector_dopath_segment(state, gs_pe_closepath, segments[0].vs);
569
29.2k
                            if (pe_op == gs_pe_moveto) {
570
29.2k
                                if (matrix_optimisable)
571
29.2k
                                    buffering = 1;
572
0
                                else
573
0
                                    buffering = 0;
574
29.2k
                                seg_index = 0;
575
29.2k
                                initial_m = 1;
576
29.2k
                                line_start = segments[0].vs[0];
577
29.2k
                            } else {
578
0
                                gdev_vector_dopath_segment(state, segments[0].op, segments[0].vs);
579
0
                                buffering = 0;
580
0
                                seg_index = -1;
581
0
                            }
582
29.2k
                        }
583
147k
                    } else {
584
131k
                        for (i=0;i<=seg_index;i++)
585
79.9k
                            gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
586
51.3k
                        if (matrix_optimisable)
587
37.4k
                            buffering = 1;
588
13.9k
                        else
589
13.9k
                            buffering = 0;
590
51.3k
                        seg_index = -1;
591
51.3k
                    }
592
208k
                } else {
593
208k
                    is_rect = 1;
594
510k
                    for (i=1;i<seg_index;i++) {
595
413k
                        if (segments[i - 1].vs[0].x != segments[i].vs[0].x) {
596
256k
                            if (segments[i - 1].vs[0].y != segments[i].vs[0].y) {
597
98.0k
                                is_rect = 0;
598
98.0k
                                break;
599
158k
                            } else {
600
158k
                                if (segments[i].vs[0].x != segments[i + 1].vs[0].x || segments[i].vs[0].y == segments[i + 1].vs[0].x){
601
3.69k
                                    is_rect = 0;
602
3.69k
                                    break;
603
3.69k
                                }
604
158k
                            }
605
256k
                        } else {
606
156k
                            if (segments[i - 1].vs[0].y == segments[i].vs[0].y) {
607
3.58k
                                is_rect = 0;
608
3.58k
                                break;
609
153k
                            } else {
610
153k
                                if (segments[i].vs[0].y != segments[i + 1].vs[0].y || segments[i].vs[0].x == segments[i + 1].vs[0].x){
611
7.06k
                                    is_rect = 0;
612
7.06k
                                    break;
613
7.06k
                                }
614
153k
                            }
615
156k
                        }
616
413k
                    }
617
208k
                    if (segments[0].vs[0].x != segments[seg_index].vs[0].x || segments[0].vs[0].y != segments[seg_index].vs[0].y)
618
4.08k
                        is_rect = 0;
619
620
                    /* If we would have to alter the starting point, and we are dashing a stroke, then don't treat
621
                     * this as a rectangle. Changing the start vertex will alter the dash pattern.
622
                     */
623
208k
                    if (segments[0].vs[0].x == segments[1].vs[0].x && (type & gx_path_type_dashed_stroke))
624
0
                        is_rect = 0;
625
626
208k
                    if (is_rect == 1) {
627
95.5k
                        gs_fixed_point *pt = &segments[0].vs[0];
628
95.5k
                        fixed width, height;
629
630
95.5k
                        if (segments[0].vs[0].x == segments[1].vs[0].x) {
631
43.2k
                            pt = &segments[1].vs[0];
632
43.2k
                            width = segments[2].vs[0].x - segments[1].vs[0].x;
633
43.2k
                            height = segments[0].vs[0].y - segments[1].vs[0].y;
634
52.2k
                        } else {
635
52.2k
                            width = segments[1].vs[0].x - segments[0].vs[0].x;
636
52.2k
                            height = segments[2].vs[0].y - segments[1].vs[0].y;
637
52.2k
                        }
638
639
95.5k
                        gs_point_transform_inverse((double)pt->x, (double)pt->y,
640
95.5k
                                   &state->scale_mat, &p);
641
95.5k
                        gs_point_transform_inverse((double)width, (double)height,
642
95.5k
                                   &state->scale_mat, &q);
643
95.5k
                        code = vdev_proc(vdev, dorect)(vdev, (fixed)p.x, (fixed)p.y,
644
95.5k
                                       (fixed)p.x + (fixed)q.x, (fixed)p.y + (fixed)q.y, type);
645
95.5k
                        if (code < 0)
646
0
                            return code;
647
95.5k
                        seg_index = -1;
648
113k
                    } else {
649
678k
                        for (i=0;i<=seg_index;i++) {
650
565k
                            gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
651
565k
                        }
652
113k
                        buffering = 0;
653
113k
                        seg_index = -1;
654
113k
                    }
655
208k
                }
656
407k
                break;
657
1.11M
            default:
658
2.29M
                for (i=0;i<seg_index;i++)
659
1.17M
                    gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
660
1.11M
                if (stored_moveto && ((type & gx_path_type_stroke) && !(type & gx_path_type_fill)))
661
250
                    gdev_vector_dopath_segment(state, gs_pe_moveto, &line_start);
662
1.11M
                seg_index = -1;
663
1.11M
                buffering = 0;
664
1.11M
                break;
665
12.1M
        }
666
12.1M
        seg_index++;
667
12.1M
        if (seg_index > 4) {
668
309k
            for (i=0;i<seg_index;i++) {
669
257k
                gdev_vector_dopath_segment(state, segments[i].op, segments[i].vs);
670
257k
            }
671
51.5k
            seg_index = 0;
672
51.5k
            buffering = 0;
673
51.5k
        }
674
12.1M
    } while (pe_op > 0);
675
676
1.23M
    if (pe_op < 0)
677
0
        return pe_op;
678
679
1.23M
    code = vdev_proc(vdev, endpath)(vdev, type);
680
1.23M
    return (code < 0 ? code : segs);
681
1.23M
}
682
683
/* Put a single element of a clipping path list. */
684
static int
685
pdf_put_clip_path_list_elem(gx_device_pdf * pdev, gx_cpath_path_list *e,
686
        gs_path_enum *cenum, gdev_vector_dopath_state_t *state,
687
        gs_fixed_point vs[3])
688
8.75k
{
689
8.75k
    int segments = 0;
690
691
    /* This function was previously recursive and reversed the order of subpaths. This
692
     * could lead to a C exec stack overflow on sufficiently complex clipping paths
693
     * (such as those produced by pdfwrite as a fallback for certain kinds of images).
694
     * Writing the subpaths in the forward order avoids the problem, is probably
695
     * slightly faster and uses less memory. Bug #706523.
696
     */
697
55.6k
    while (e) {
698
46.9k
        segments = pdf_write_path(pdev, cenum, state, &e->path, 0, gx_path_type_clip | gx_path_type_optimize, NULL);
699
46.9k
        if (segments < 0)
700
0
            return segments;
701
46.9k
        if (segments)
702
46.9k
            pprints1(pdev->strm, "%s n\n", (e->rule <= 0 ? "W" : "W*"));
703
46.9k
        e = e->next;
704
46.9k
    }
705
8.75k
    return 0;
706
8.75k
}
707
708
/* Put a clipping path on the output file. */
709
int
710
pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
711
2.91M
{
712
2.91M
    int code;
713
2.91M
    stream *s = pdev->strm;
714
2.91M
    gs_id new_id;
715
716
    /* Check for no update needed. */
717
2.91M
    if (pcpath == NULL) {
718
565k
        if (pdev->clip_path_id == pdev->no_clip_path_id)
719
565k
            return 0;
720
8
        new_id = pdev->no_clip_path_id;
721
2.34M
    } else {
722
2.34M
        if (pdev->clip_path_id == pcpath->id)
723
529k
            return 0;
724
1.81M
        new_id = pcpath->id;
725
1.81M
        if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
726
1.81M
                                        int2fixed(pdev->width),
727
1.81M
                                        int2fixed(pdev->height))
728
1.81M
            ) {
729
1.68M
            if (pdev->clip_path_id == pdev->no_clip_path_id)
730
1.68M
                return 0;
731
57
            new_id = pdev->no_clip_path_id;
732
57
        }
733
136k
        code = pdf_is_same_clip_path(pdev, pcpath);
734
136k
        if (code < 0)
735
0
            return code;
736
136k
        if (code) {
737
0
            pdev->clip_path_id = new_id;
738
0
            return 0;
739
0
        }
740
136k
    }
741
    /*
742
     * The contents must be open already, so the following will only exit
743
     * text or string context.
744
     */
745
136k
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
746
136k
    if (code < 0)
747
0
        return code;
748
    /* Use Q to unwind the old clipping path. */
749
136k
    if (pdev->vgstack_depth > pdev->vgstack_bottom) {
750
574
        code = pdf_restore_viewer_state(pdev, s);
751
574
        if (code < 0)
752
0
            return code;
753
574
    }
754
136k
    if (new_id != pdev->no_clip_path_id) {
755
136k
        gs_fixed_rect rect;
756
757
        /* Use q to allow the new clipping path to unwind.  */
758
136k
        code = pdf_save_viewer_state(pdev, s);
759
136k
        if (code < 0)
760
0
            return code;
761
        /* path_valid states that the clip path is a simple path. If the clip is an intersection of
762
         * two paths, then path_valid is false. The problem is that the rectangle list is the
763
         * scan-converted result of the clip, and ths is at the device resolution. Its possible
764
         * that the intersection of the clips, at device resolution, is rectangular but the
765
         * two paths are not, and that at a different resolution, nor is the intersection.
766
         * So we *only* want to write a rectangle, if the clip is rectangular, and its the
767
         * result of a simple rectangle. Otherwise we want to write the paths that create
768
         * the clip. However, see below about the path_list.
769
         */
770
136k
        if (pcpath->path_valid && cpath_is_rectangle(pcpath, &rect)) {
771
            /* Use unrounded coordinates. */
772
76.9k
            pprintg4(s, "%g %g %g %g re",
773
76.9k
                fixed2float(rect.p.x), fixed2float(rect.p.y),
774
76.9k
                fixed2float(rect.q.x - rect.p.x),
775
76.9k
                fixed2float(rect.q.y - rect.p.y));
776
76.9k
            pprints1(s, " %s n\n", (pcpath->rule <= 0 ? "W" : "W*"));
777
76.9k
        } else {
778
59.7k
            gdev_vector_dopath_state_t state;
779
59.7k
            gs_fixed_point vs[3];
780
781
            /* the first comment below is (now) incorrect. Previously in gx_clip_to_rectangle()
782
             * we would create a rectangular clip, without using a path to do so. This results
783
             * in a rectangular clip, where path_valid is false. However, we did *not* clear
784
             * the path_list! So if there had previously been a clip path set, by setting paths,
785
             * we did not clear it. This is not sensible, and caused massive confusion for this code
786
             * so it has been altered to clear path_list, indicating that there is a clip,
787
             * the path is not valid, and that it was not created using arbitrary paths.
788
             * In this case we just emit the rectangle as well (there should be only one).
789
             */
790
59.7k
            if (pcpath->path_list == NULL) {
791
                /*
792
                 * We think this should be never executed.
793
                 * This obsolete branch writes a clip path intersection
794
                 * as a set of rectangles computed by
795
                 * gx_cpath_intersect_path_slow.
796
                 * Those rectangles use coordinates rounded to pixels,
797
                 * therefore the precision may be unsatisfactory -
798
                 * see Bug 688407.
799
                 */
800
50.9k
                gs_cpath_enum cenum;
801
802
                /*
803
                 * We have to break 'const' here because the clip path
804
                 * enumeration logic uses some internal mark bits.
805
                 * This is very unfortunate, but until we can come up with
806
                 * a better algorithm, it's necessary.
807
                 */
808
50.9k
                code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)pcpath, 1, gx_path_type_clip | gx_path_type_optimize, NULL);
809
50.9k
                if (code < 0)
810
0
                    return code;
811
50.9k
                pprints1(s, "%s n\n", (pcpath->rule <= 0 ? "W" : "W*"));
812
50.9k
            } else {
813
8.75k
                gs_path_enum cenum;
814
815
8.75k
                code = pdf_put_clip_path_list_elem(pdev, pcpath->path_list, &cenum, &state, vs);
816
8.75k
                if (code < 0)
817
0
                    return code;
818
8.75k
            }
819
59.7k
        }
820
136k
    }
821
136k
    pdev->clip_path_id = new_id;
822
136k
    return pdf_remember_clip_path(pdev,
823
136k
            (pdev->clip_path_id == pdev->no_clip_path_id ? NULL : pcpath));
824
136k
}
825
826
/*
827
 * Compute the scaling to ensure that user coordinates for a path are within
828
 * PDF/A-1 valid range.  Return true if scaling was needed.  In this case, the
829
 * CTM will be multiplied by *pscale, and all coordinates will be divided by
830
 * *pscale.
831
 */
832
static bool
833
make_rect_scaling(const gx_device_pdf *pdev, const gs_fixed_rect *bbox,
834
                  double prescale, double *pscale)
835
1.82M
{
836
1.82M
    double bmin, bmax;
837
838
1.82M
    if (pdev->PDFA != 1) {
839
1.82M
        *pscale = 1;
840
1.82M
        return false;
841
1.82M
    }
842
843
0
    bmin = min(fixed2float(bbox->p.x) / pdev->scale.x, fixed2float(bbox->p.y) / pdev->scale.y) * prescale;
844
0
    bmax = max(fixed2float(bbox->q.x) / pdev->scale.x, fixed2float(bbox->q.y) / pdev->scale.y) * prescale;
845
0
    if (bmin <= int2fixed(-MAX_USER_COORD) ||
846
0
        bmax > int2fixed(MAX_USER_COORD)
847
0
        ) {
848
        /* Rescale the path. */
849
0
        *pscale = max(bmin / int2fixed(-MAX_USER_COORD),
850
0
                      bmax / int2fixed(MAX_USER_COORD));
851
0
        return true;
852
0
    } else {
853
0
        *pscale = 1;
854
0
        return false;
855
0
    }
856
0
}
857
858
/*
859
 * Prepare a fill with a color anc a clipping path.
860
 * Return 1 if there is nothing to paint.
861
 * Changes *box to the clipping box.
862
 */
863
static int
864
prepare_fill_with_clip(gx_device_pdf *pdev, const gs_gstate * pgs,
865
              gs_fixed_rect *box, bool have_path,
866
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
867
1.34M
{
868
1.34M
    bool new_clip;
869
1.34M
    int code;
870
871
    /*
872
     * Check for an empty clipping path.
873
     */
874
1.34M
    if (pcpath) {
875
1.32M
        gs_fixed_rect cbox;
876
877
1.32M
        gx_cpath_outer_box(pcpath, &cbox);
878
1.32M
        if (cbox.p.x >= cbox.q.x || cbox.p.y >= cbox.q.y)
879
27.8k
            return 1;    /* empty clipping path */
880
1.30M
        *box = cbox;
881
1.30M
    }
882
1.31M
    code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs);
883
1.31M
    if (code < 0)
884
0
        return code;
885
886
1.31M
    new_clip = pdf_must_put_clip_path(pdev, pcpath);
887
1.31M
    if (have_path || pdev->context == PDF_IN_NONE || new_clip) {
888
1.15M
        if (new_clip)
889
52.6k
            code = pdf_unclip(pdev);
890
1.10M
        else
891
1.10M
            code = pdf_open_page(pdev, PDF_IN_STREAM);
892
1.15M
        if (code < 0)
893
0
            return code;
894
1.15M
    }
895
1.31M
    code = pdf_prepare_fill(pdev, pgs, false);
896
1.31M
    if (code < 0)
897
3.16k
        return code;
898
1.30M
    return pdf_put_clip_path(pdev, pcpath);
899
1.31M
}
900
901
/* -------------A local image converter device. -----------------------------*/
902
903
public_st_pdf_lcvd_t();
904
905
static int
906
lcvd_copy_color_shifted(gx_device * dev,
907
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
908
                      int x, int y, int w, int h)
909
0
{
910
0
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
911
0
    int code;
912
0
    int dw = cvd->mdev.width;
913
0
    int dh = cvd->mdev.height;
914
915
0
    cvd->mdev.width -= cvd->mdev.mapped_x;
916
0
    cvd->mdev.height -= cvd->mdev.mapped_y;
917
918
0
    code = cvd->std_copy_color((gx_device *)&cvd->mdev, base, sourcex, sraster, id,
919
0
        x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, w, h);
920
921
0
    cvd->mdev.width = dw;
922
0
    cvd->mdev.height = dh;
923
924
0
    return code;
925
0
}
926
927
static int
928
lcvd_copy_mono_shifted(gx_device * dev,
929
               const byte * base, int sourcex, int sraster, gx_bitmap_id id,
930
                      int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
931
4.49k
{
932
4.49k
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
933
4.49k
    int code;
934
4.49k
    int dw = cvd->mdev.width;
935
4.49k
    int dh = cvd->mdev.height;
936
937
4.49k
    cvd->mdev.width -= cvd->mdev.mapped_x;
938
4.49k
    cvd->mdev.height -= cvd->mdev.mapped_y;
939
940
4.49k
    code = cvd->std_copy_mono((gx_device *)&cvd->mdev, base, sourcex, sraster, id,
941
4.49k
                              x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, w, h,
942
4.49k
                              zero, one);
943
944
4.49k
    cvd->mdev.width = dw;
945
4.49k
    cvd->mdev.height = dh;
946
947
4.49k
    return code;
948
4.49k
}
949
950
static int
951
lcvd_fill_rectangle_shifted(gx_device *dev, int x, int y, int width, int height, gx_color_index color)
952
12.7M
{
953
12.7M
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
954
12.7M
    int code;
955
12.7M
    int w = cvd->mdev.width;
956
12.7M
    int h = cvd->mdev.height;
957
958
12.7M
    if (cvd->pass == 2)
959
2.74M
        return 0;
960
961
10.0M
    cvd->mdev.width -= cvd->mdev.mapped_x;
962
10.0M
    cvd->mdev.height -= cvd->mdev.mapped_y;
963
964
10.0M
    code = cvd->std_fill_rectangle((gx_device *)&cvd->mdev,
965
10.0M
        x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color);
966
967
10.0M
    cvd->mdev.width = w;
968
10.0M
    cvd->mdev.height = h;
969
970
10.0M
    return code;
971
12.7M
}
972
static int
973
lcvd_fill_rectangle_shifted2(gx_device *dev, int x, int y, int width, int height, gx_color_index color)
974
2.19M
{
975
2.19M
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
976
2.19M
    int code = 0;
977
978
2.19M
    if (cvd->pass != 1)
979
240k
    {
980
240k
        if (cvd->mask) {
981
240k
            code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask,
982
240k
                x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, (gx_color_index)1);
983
240k
            if (code < 0)
984
0
                goto fail;
985
240k
        }
986
240k
    }
987
2.19M
    if (cvd->pass != 2)
988
2.01M
    {
989
2.01M
        int w = cvd->mdev.width;
990
2.01M
        int h = cvd->mdev.height;
991
2.01M
        cvd->mdev.width -= cvd->mdev.mapped_x;
992
2.01M
        cvd->mdev.height -= cvd->mdev.mapped_y;
993
994
2.01M
        code = cvd->std_fill_rectangle((gx_device *)&cvd->mdev,
995
2.01M
            x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color);
996
2.01M
        cvd->mdev.width = w;
997
2.01M
        cvd->mdev.height = h;
998
2.01M
    }
999
1000
2.19M
fail:
1001
1002
2.19M
    return code;
1003
2.19M
}
1004
static void
1005
lcvd_get_clipping_box_shifted_from_mdev(gx_device *dev, gs_fixed_rect *pbox)
1006
3.13k
{
1007
3.13k
    fixed ofs;
1008
3.13k
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
1009
3.13k
    int w = cvd->mdev.width;
1010
3.13k
    int h = cvd->mdev.height;
1011
1012
3.13k
    cvd->mdev.width -= cvd->mdev.mapped_x;
1013
3.13k
    cvd->mdev.height -= cvd->mdev.mapped_y;
1014
1015
3.13k
    cvd->std_get_clipping_box((gx_device *)&cvd->mdev, pbox);
1016
3.13k
    cvd->mdev.width = w;
1017
3.13k
    cvd->mdev.height = h;
1018
3.13k
    ofs = int2fixed(cvd->mdev.mapped_x);
1019
3.13k
    pbox->p.x += ofs;
1020
3.13k
    pbox->q.x += ofs;
1021
3.13k
    ofs = int2fixed(cvd->mdev.mapped_y);
1022
3.13k
    pbox->p.y += ofs;
1023
3.13k
    pbox->q.y += ofs;
1024
3.13k
}
1025
static int
1026
lcvd_dev_spec_op(gx_device *pdev1, int dev_spec_op,
1027
                void *data, int size)
1028
13.1M
{
1029
13.1M
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)pdev1;
1030
13.1M
    int code, w, h;
1031
1032
13.1M
    switch (dev_spec_op) {
1033
1.31M
        case gxdso_pattern_shading_area:
1034
1.31M
            return 1; /* Request shading area. */
1035
0
        case gxdso_pattern_can_accum:
1036
0
        case gxdso_pattern_start_accum:
1037
0
        case gxdso_pattern_finish_accum:
1038
0
        case gxdso_pattern_load:
1039
21
        case gxdso_pattern_is_cpath_accum:
1040
21
        case gxdso_pattern_shfill_doesnt_need_path:
1041
21
        case gxdso_pattern_handles_clip_path:
1042
21
        case gxdso_copy_color_is_fast:
1043
21
            return 0;
1044
13.1M
    }
1045
1046
11.8M
    w = cvd->mdev.width;
1047
11.8M
    h = cvd->mdev.height;
1048
11.8M
    cvd->mdev.width -= cvd->mdev.mapped_x;
1049
11.8M
    cvd->mdev.height -= cvd->mdev.mapped_y;
1050
1051
11.8M
    code = gx_default_dev_spec_op(pdev1, dev_spec_op, data, size);
1052
1053
11.8M
    cvd->mdev.width = w;
1054
11.8M
    cvd->mdev.height = h;
1055
1056
11.8M
    return code;
1057
13.1M
}
1058
static int
1059
lcvd_close_device_with_writing(gx_device *pdev)
1060
57
{
1061
    /* Assuming 'mdev' is being closed before 'mask' - see gx_image3_end_image. */
1062
57
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)pdev;
1063
57
    int code, code1;
1064
1065
57
    code = pdf_dump_converted_image(cvd->pdev, cvd, 0);
1066
57
    code1 = cvd->std_close_device((gx_device *)&cvd->mdev);
1067
57
    return code < 0 ? code : code1;
1068
57
}
1069
1070
static int
1071
write_image(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m, int for_pattern)
1072
1.28k
{
1073
1.28k
    gs_image_t image;
1074
1.28k
    pdf_image_writer writer;
1075
1.28k
    const int sourcex = 0;
1076
1.28k
    int code;
1077
1078
1.28k
    if (m != NULL)
1079
0
        pdf_put_matrix(pdev, NULL, m, " cm\n");
1080
1.28k
    code = pdf_copy_color_data(pdev, mdev->base, sourcex,
1081
1.28k
                mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height,
1082
1.28k
                &image, &writer, for_pattern);
1083
1.28k
    if (code == 1)
1084
1.28k
        code = 0; /* Empty image. */
1085
0
    else if (code == 0)
1086
0
        code = pdf_do_image(pdev, writer.pres, NULL, true);
1087
1.28k
    return code;
1088
1.28k
}
1089
static int
1090
write_mask(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m)
1091
0
{
1092
0
    const int sourcex = 0;
1093
0
    gs_id save_clip_id = pdev->clip_path_id;
1094
0
    bool save_skip_color = pdev->skip_colors;
1095
0
    int code;
1096
1097
0
    if (m != NULL)
1098
0
        pdf_put_matrix(pdev, NULL, m, " cm\n");
1099
0
    pdev->clip_path_id = pdev->no_clip_path_id;
1100
0
    pdev->skip_colors = true;
1101
0
    code = gdev_pdf_copy_mono((gx_device *)pdev, mdev->base, sourcex,
1102
0
                mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height,
1103
0
                gx_no_color_index, (gx_color_index)0);
1104
0
    pdev->clip_path_id = save_clip_id;
1105
0
    pdev->skip_colors = save_skip_color;
1106
0
    return code;
1107
0
}
1108
1109
static void
1110
max_subimage_width(int width, byte *base, int x0, int64_t count1, int *x1, int64_t *count)
1111
6.63k
{
1112
6.63k
    int64_t c = 0, c1 = count1 - 1;
1113
6.63k
    int x = x0;
1114
6.63k
    byte p = 1; /* The inverse of the previous bit. */
1115
6.63k
    byte r;     /* The inverse of the current  bit. */
1116
6.63k
    byte *q = base + (x / 8), m = 0x80 >> (x % 8);
1117
1118
5.26M
    for (; x < width; x++) {
1119
5.26M
        r = !(*q & m);
1120
5.26M
        if (p != r) {
1121
62.1k
            if (c >= c1) {
1122
14
                if (!r)
1123
8
                    goto ex; /* stop before the upgrade. */
1124
14
            }
1125
62.1k
            c++;
1126
62.1k
        }
1127
5.26M
        p = r;
1128
5.26M
        m >>= 1;
1129
5.26M
        if (!m) {
1130
655k
            m = 0x80;
1131
655k
            q++;
1132
655k
        }
1133
5.26M
    }
1134
6.62k
    if (p)
1135
5.27k
        c++; /* Account the last downgrade. */
1136
6.63k
ex:
1137
6.63k
    *count = c;
1138
6.63k
    *x1 = x;
1139
6.63k
}
1140
1141
static int
1142
cmpbits(const byte *base, const byte *base2, int w)
1143
83.1k
{
1144
83.1k
    int code;
1145
1146
83.1k
    code = memcmp(base, base2, w>>3);
1147
83.1k
    if (code)
1148
13.0k
        return code;
1149
70.1k
    base += w>>3;
1150
70.1k
    base2 += w>>3;
1151
70.1k
    w &= 7;
1152
70.1k
    if (w == 0)
1153
56.7k
        return 0;
1154
13.3k
    return ((*base ^ *base2) & (0xff00>>w));
1155
70.1k
}
1156
1157
static void
1158
compute_subimage(int width, int height, int raster, byte *base,
1159
                 int x0, int y0, int64_t MaxClipPathSize, int *x1, int *y1)
1160
118
{
1161
    /* Returns a semiopen range : [x0:x1)*[y0:y1). */
1162
118
    if (x0 != 0) {
1163
0
        int64_t count;
1164
1165
        /* A partial single scanline. */
1166
0
        max_subimage_width(width, base + y0 * raster, x0, MaxClipPathSize / 4, x1, &count);
1167
0
        *y1 = y0;
1168
118
    } else {
1169
118
        int xx, y = y0, yy;
1170
118
        int64_t count, count1 = MaxClipPathSize / 4;
1171
1172
6.74k
        for(; y < height && count1 > 0; ) {
1173
6.63k
            max_subimage_width(width, base + y * raster, 0, count1, &xx, &count);
1174
6.63k
            if (xx < width) {
1175
8
                if (y == y0) {
1176
                    /* Partial single scanline. */
1177
0
                    *y1 = y + 1;
1178
0
                    *x1 = xx;
1179
0
                    return;
1180
8
                } else {
1181
                    /* Full lines before this scanline. */
1182
8
                    break;
1183
8
                }
1184
8
            }
1185
6.62k
            count1 -= count;
1186
6.62k
            yy = y + 1;
1187
41.7k
            for (; yy < height; yy++)
1188
41.5k
                if (cmpbits(base + raster * y, base + raster * yy, width))
1189
6.52k
                    break;
1190
6.62k
            y = yy;
1191
1192
6.62k
        }
1193
118
        *y1 = y;
1194
118
        *x1 = width;
1195
118
    }
1196
118
}
1197
1198
static int
1199
image_line_to_clip(gx_device_pdf *pdev, byte *base, int x0, int x1, int y0, int y1, bool started)
1200
6.62k
{   /* returns the number of segments or error code. */
1201
6.62k
    int x = x0, xx;
1202
6.62k
    byte *q = base + (x / 8), m = 0x80 >> (x % 8);
1203
6.62k
    int64_t c = 0;
1204
1205
38.2k
    for (;;) {
1206
        /* Look for upgrade : */
1207
3.62M
        for (; x < x1; x++) {
1208
3.61M
            if (*q & m)
1209
31.6k
                break;
1210
3.58M
            m >>= 1;
1211
3.58M
            if (!m) {
1212
446k
                m = 0x80;
1213
446k
                q++;
1214
446k
            }
1215
3.58M
        }
1216
38.2k
        if (x == x1)
1217
6.62k
            return c;
1218
31.6k
        xx = x;
1219
        /* Look for downgrade : */
1220
1.70M
        for (; x < x1; x++) {
1221
1.70M
            if (!(*q & m))
1222
30.3k
                break;
1223
1.67M
            m >>= 1;
1224
1.67M
            if (!m) {
1225
208k
                m = 0x80;
1226
208k
                q++;
1227
208k
            }
1228
1.67M
        }
1229
        /* Found the interval [xx:x). */
1230
31.6k
        if (!started) {
1231
111
            stream_puts(pdev->strm, "n\n");
1232
111
            started = true;
1233
111
        }
1234
31.6k
        pprintld2(pdev->strm, "%ld %ld m ", xx, y0);
1235
31.6k
        pprintld2(pdev->strm, "%ld %ld l ", x, y0);
1236
31.6k
        pprintld2(pdev->strm, "%ld %ld l ", x, y1);
1237
31.6k
        pprintld2(pdev->strm, "%ld %ld l h\n", xx, y1);
1238
31.6k
        c += 4;
1239
31.6k
    }
1240
0
    return c;
1241
6.62k
}
1242
1243
static int
1244
mask_to_clip(gx_device_pdf *pdev, int width, int height,
1245
             int raster, byte *base, int x0, int y0, int x1, int y1)
1246
118
{
1247
118
    int y, yy, code = 0;
1248
118
    bool has_segments = false;
1249
1250
6.74k
    for (y = y0; y < y1 && code >= 0;) {
1251
6.62k
        yy = y + 1;
1252
6.62k
        if (x0 == 0) {
1253
41.7k
        for (; yy < y1; yy++)
1254
41.5k
            if (cmpbits(base + raster * y, base + raster * yy, width))
1255
6.50k
                break;
1256
6.62k
        }
1257
6.62k
        code = image_line_to_clip(pdev, base + raster * y, x0, x1, y, yy, has_segments);
1258
6.62k
        if (code > 0)
1259
6.41k
            has_segments = true;
1260
6.62k
        y = yy;
1261
6.62k
    }
1262
118
    if (has_segments)
1263
111
        stream_puts(pdev->strm, "W n\n");
1264
118
    return code < 0 ? code : has_segments ? 1 : 0;
1265
118
}
1266
1267
static int
1268
write_subimage(gx_device_pdf *pdev, gx_device_memory *mdev, int x, int y, int x1, int y1, int for_pattern)
1269
111
{
1270
111
    gs_image_t image;
1271
111
    pdf_image_writer writer;
1272
    /* expand in 1 pixel to provide a proper color interpolation */
1273
111
    int X = max(0, x - 1);
1274
111
    int Y = max(0, y - 1);
1275
111
    int X1 = min(mdev->width, x1 + 1);
1276
111
    int Y1 = min(mdev->height, y1 + 1);
1277
111
    int code;
1278
1279
111
    code = pdf_copy_color_data(pdev, mdev->base + mdev->raster * Y, X,
1280
111
                mdev->raster, gx_no_bitmap_id,
1281
111
                X, Y, X1 - X, Y1 - Y,
1282
111
                &image, &writer, for_pattern);
1283
111
    if (code < 0)
1284
0
        return code;
1285
111
    if (!writer.pres)
1286
111
        return 0; /* inline image. */
1287
0
    return pdf_do_image(pdev, writer.pres, NULL, true);
1288
111
}
1289
1290
static int
1291
write_image_with_clip(gx_device_pdf *pdev, pdf_lcvd_t *cvd, int for_pattern)
1292
105
{
1293
105
    int x = 0, y = 0;
1294
105
    int code, code1;
1295
1296
105
    if (cvd->write_matrix)
1297
78
        pdf_put_matrix(pdev, NULL, &cvd->m, " cm q\n");
1298
118
    for(;;) {
1299
118
        int x1, y1;
1300
1301
118
        compute_subimage(cvd->mask->width, cvd->mask->height,
1302
118
                         cvd->mask->raster, cvd->mask->base,
1303
118
                         x, y, max(pdev->MaxClipPathSize, 100), &x1, &y1);
1304
118
        code = mask_to_clip(pdev,
1305
118
                         cvd->mask->width, cvd->mask->height,
1306
118
                         cvd->mask->raster, cvd->mask->base,
1307
118
                         x, y, x1, y1);
1308
118
        if (code < 0)
1309
0
            return code;
1310
118
        if (code > 0) {
1311
111
            code1 = write_subimage(pdev, &cvd->mdev, x, y, x1, y1, for_pattern);
1312
111
            if (code1 < 0)
1313
0
                return code1;
1314
111
        }
1315
118
        if (x1 >= cvd->mdev.width && y1 >= cvd->mdev.height)
1316
105
            break;
1317
13
        if (code > 0)
1318
13
            stream_puts(pdev->strm, "Q q\n");
1319
13
        if (x1 == cvd->mask->width) {
1320
13
            x = 0;
1321
13
            y = y1;
1322
13
        } else {
1323
0
            x = x1;
1324
0
            y = y1;
1325
0
        }
1326
13
    }
1327
105
    if (cvd->write_matrix)
1328
78
        stream_puts(pdev->strm, "Q\n");
1329
105
    return 0;
1330
105
}
1331
1332
int
1333
pdf_dump_converted_image(gx_device_pdf *pdev, pdf_lcvd_t *cvd, int for_pattern)
1334
1.56k
{
1335
1.56k
    int code = 0;
1336
1337
1.56k
    if (cvd->pass == 1)
1338
0
        return 0;
1339
1340
1.56k
    cvd->mdev.width -= cvd->mdev.mapped_x;
1341
1.56k
    cvd->mdev.height -= cvd->mdev.mapped_y;
1342
1343
1.56k
    if (!cvd->path_is_empty || cvd->has_background) {
1344
1.28k
        if (!cvd->has_background)
1345
1.28k
            stream_puts(pdev->strm, "W n\n");
1346
1.28k
        code = write_image(pdev, &cvd->mdev, (cvd->write_matrix ? &cvd->m : NULL), for_pattern);
1347
1.28k
        cvd->path_is_empty = true;
1348
1.28k
    } else if (!cvd->mask_is_empty && pdev->PatternImagemask) {
1349
        /* Convert to imagemask with a pattern color. */
1350
        /* See also use_image_as_pattern in gdevpdfi.c . */
1351
0
        gs_gstate s;
1352
0
        gs_pattern1_instance_t inst;
1353
0
        gs_id id = gs_next_ids(cvd->mdev.memory, 1);
1354
0
        cos_value_t v;
1355
0
        const pdf_resource_t *pres;
1356
1357
0
        memset(&s, 0, sizeof(s));
1358
0
        s.ctm.xx = cvd->m.xx;
1359
0
        s.ctm.xy = cvd->m.xy;
1360
0
        s.ctm.yx = cvd->m.yx;
1361
0
        s.ctm.yy = cvd->m.yy;
1362
0
        s.ctm.tx = cvd->m.tx;
1363
0
        s.ctm.ty = cvd->m.ty;
1364
0
        memset(&inst, 0, sizeof(inst));
1365
0
        inst.saved = (gs_gstate *)&s; /* HACK : will use s.ctm only. */
1366
0
        inst.templat.PaintType = 1;
1367
0
        inst.templat.TilingType = 1;
1368
0
        inst.templat.BBox.p.x = inst.templat.BBox.p.y = 0;
1369
0
        inst.templat.BBox.q.x = cvd->mdev.width;
1370
0
        inst.templat.BBox.q.y = cvd->mdev.height;
1371
0
        inst.templat.XStep = (float)cvd->mdev.width;
1372
0
        inst.templat.YStep = (float)cvd->mdev.height;
1373
1374
0
        {
1375
0
            pattern_accum_param_s param;
1376
0
            param.pinst = (void *)&inst;
1377
0
            param.graphics_state = (void *)&s;
1378
0
            param.pinst_id = inst.id;
1379
1380
0
            code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
1381
0
                gxdso_pattern_start_accum, &param, sizeof(pattern_accum_param_s));
1382
0
        }
1383
1384
0
        if (code >= 0) {
1385
0
            stream_puts(pdev->strm, "W n\n");
1386
0
            code = write_image(pdev, &cvd->mdev, NULL, for_pattern);
1387
0
        }
1388
0
        pres = pdev->accumulating_substream_resource;
1389
0
        if (pres == NULL)
1390
0
            code = gs_note_error(gs_error_unregistered);
1391
0
        if (code >= 0) {
1392
0
            pattern_accum_param_s param;
1393
0
            param.pinst = (void *)&inst;
1394
0
            param.graphics_state = (void *)&s;
1395
0
            param.pinst_id = inst.id;
1396
1397
0
            code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
1398
0
                gxdso_pattern_finish_accum, &param, id);
1399
0
        }
1400
0
        if (code >= 0)
1401
0
            code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
1402
0
                gxdso_pattern_load, &id, sizeof(gs_id));
1403
0
        if (code >= 0)
1404
0
            code = pdf_cs_Pattern_colored(pdev, &v);
1405
0
        if (code >= 0) {
1406
0
            cos_value_write(&v, pdev);
1407
0
            pprinti64d1(pdev->strm, " cs /R%"PRId64" scn ", pdf_resource_id(pres));
1408
0
        }
1409
0
        if (code >= 0)
1410
0
            code = write_mask(pdev, cvd->mask, (cvd->write_matrix ? &cvd->m : NULL));
1411
0
        cvd->mask_is_empty = true;
1412
282
    } else if (!cvd->mask_is_empty && !pdev->PatternImagemask) {
1413
        /* Convert to image with a clipping path. */
1414
105
        stream_puts(pdev->strm, "q\n");
1415
105
        code = write_image_with_clip(pdev, cvd, for_pattern);
1416
105
        stream_puts(pdev->strm, "Q\n");
1417
177
    } else if (cvd->filled_trap){
1418
0
        stream_puts(pdev->strm, "q\n");
1419
0
        code = write_image_with_clip(pdev, cvd, for_pattern);
1420
0
        stream_puts(pdev->strm, "Q\n");
1421
0
    }
1422
1.56k
    cvd->filled_trap = false;
1423
1.56k
    cvd->mdev.width += cvd->mdev.mapped_x;
1424
1.56k
    cvd->mdev.height += cvd->mdev.mapped_y;
1425
1.56k
    if (code > 0)
1426
0
        code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev,
1427
0
                0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0);
1428
1429
1.56k
    return code;
1430
1.56k
}
1431
static int
1432
lcvd_fill_trapezoid(gx_device * dev, const gs_fixed_edge * left,
1433
    const gs_fixed_edge * right, fixed ybot, fixed ytop, bool swap_axes,
1434
    const gx_device_color * pdevc, gs_logical_operation_t lop)
1435
164k
{
1436
164k
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
1437
1438
164k
    if (cvd->mask != NULL)
1439
63.3k
        cvd->filled_trap = true;
1440
164k
    return gx_default_fill_trapezoid(dev, left, right, ybot, ytop, swap_axes, pdevc, lop);
1441
164k
}
1442
1443
static int
1444
lcvd_handle_fill_path_as_shading_coverage(gx_device *dev,
1445
    const gs_gstate *pgs, gx_path *ppath,
1446
    const gx_fill_params *params,
1447
    const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)
1448
1.31M
{
1449
1.31M
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
1450
1.31M
    gx_device_pdf *pdev = (gx_device_pdf *)cvd->mdev.target;
1451
1.31M
    int code;
1452
1453
1.31M
    if (cvd->pass == 1)
1454
1.31M
        return 0;
1455
6.79k
    if (cvd->has_background)
1456
12
        return 0;
1457
6.78k
    if (gx_path_is_null(ppath)) {
1458
        /* use the mask. */
1459
4.10k
        if (!cvd->path_is_empty) {
1460
27
            code = pdf_dump_converted_image(pdev, cvd, 2);
1461
27
            if (code < 0)
1462
0
                return code;
1463
27
            stream_puts(pdev->strm, "Q q\n");
1464
27
            dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted2;
1465
27
        }
1466
4.10k
        if (cvd->mask && (!cvd->mask_is_clean || !cvd->path_is_empty)) {
1467
9
            code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask,
1468
9
                        0, 0, cvd->mask->width, cvd->mask->height, (gx_color_index)0);
1469
9
            if (code < 0)
1470
0
                return code;
1471
9
            cvd->mask_is_clean = true;
1472
9
        }
1473
4.10k
        cvd->path_is_empty = true;
1474
4.10k
        if (cvd->mask)
1475
4.10k
            cvd->mask_is_empty = false;
1476
4.10k
    } else {
1477
2.67k
        gs_matrix m;
1478
2.67k
        gs_path_enum cenum;
1479
2.67k
        gdev_vector_dopath_state_t state;
1480
1481
2.67k
        gs_make_translation(cvd->path_offset.x, cvd->path_offset.y, &m);
1482
        /* use the clipping. */
1483
2.67k
        if (!cvd->mask_is_empty) {
1484
27
            code = pdf_dump_converted_image(pdev, cvd, 2);
1485
27
            if (code < 0)
1486
0
                return code;
1487
27
            stream_puts(pdev->strm, "Q q\n");
1488
27
            dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted;
1489
27
            cvd->mask_is_empty = true;
1490
27
        }
1491
2.67k
        code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_fill | gx_path_type_optimize, &m);
1492
2.67k
        if (code < 0)
1493
0
            return code;
1494
2.67k
        stream_puts(pdev->strm, "h\n");
1495
2.67k
        cvd->path_is_empty = false;
1496
2.67k
    }
1497
6.78k
    return 0;
1498
6.78k
}
1499
1500
static int
1501
lcvd_transform_pixel_region(gx_device *dev, transform_pixel_region_reason reason, transform_pixel_region_data *data)
1502
0
{
1503
0
    transform_pixel_region_data local_data;
1504
0
    gx_dda_fixed_point local_pixels, local_rows;
1505
0
    gs_int_rect local_clip;
1506
0
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
1507
0
    int ret;
1508
0
    dev_t_proc_fill_rectangle((*fill_rectangle), gx_device);
1509
0
    dev_t_proc_copy_color((*copy_color), gx_device);
1510
0
    int w = cvd->mdev.width;
1511
0
    int h = cvd->mdev.height;
1512
1513
0
    cvd->mdev.width -= cvd->mdev.mapped_x;
1514
0
    cvd->mdev.height -= cvd->mdev.mapped_y;
1515
1516
0
    if (reason == transform_pixel_region_begin) {
1517
0
        local_data = *data;
1518
0
        local_pixels = *local_data.u.init.pixels;
1519
0
        local_rows = *local_data.u.init.rows;
1520
0
        local_clip = *local_data.u.init.clip;
1521
0
        local_data.u.init.pixels = &local_pixels;
1522
0
        local_data.u.init.rows = &local_rows;
1523
0
        local_data.u.init.clip = &local_clip;
1524
0
        local_pixels.x.state.Q -= int2fixed(cvd->mdev.mapped_x);
1525
0
        local_pixels.y.state.Q -= int2fixed(cvd->mdev.mapped_y);
1526
0
        local_rows.x.state.Q -= int2fixed(cvd->mdev.mapped_x);
1527
0
        local_rows.y.state.Q -= int2fixed(cvd->mdev.mapped_y);
1528
0
        local_clip.p.x -= cvd->mdev.mapped_x;
1529
0
        local_clip.p.y -= cvd->mdev.mapped_y;
1530
0
        local_clip.q.x -= cvd->mdev.mapped_x;
1531
0
        local_clip.q.y -= cvd->mdev.mapped_y;
1532
0
        ret = cvd->std_transform_pixel_region(dev, reason, &local_data);
1533
0
        data->state = local_data.state;
1534
0
        cvd->mdev.width = w;
1535
0
        cvd->mdev.height = h;
1536
0
        return ret;
1537
0
    }
1538
0
    copy_color = dev_proc(&cvd->mdev, copy_color);
1539
0
    fill_rectangle = dev_proc(&cvd->mdev, fill_rectangle);
1540
0
    dev_proc(&cvd->mdev, copy_color) = cvd->std_copy_color;
1541
0
    dev_proc(&cvd->mdev, fill_rectangle) = cvd->std_fill_rectangle;
1542
0
    ret = cvd->std_transform_pixel_region(dev, reason, data);
1543
0
    dev_proc(&cvd->mdev, copy_color) = copy_color;
1544
0
    dev_proc(&cvd->mdev, fill_rectangle) = fill_rectangle;
1545
0
    cvd->mdev.width = w;
1546
0
    cvd->mdev.height = h;
1547
0
    return ret;
1548
0
}
1549
1550
int
1551
pdf_setup_masked_image_converter(gx_device_pdf *pdev, gs_memory_t *mem, const gs_matrix *m, pdf_lcvd_t **pcvd,
1552
                                 bool need_mask, int x, int y, int w, int h, bool write_on_close)
1553
1.69k
{
1554
1.69k
    int code;
1555
1.69k
    gx_device_memory *mask = 0;
1556
1.69k
    pdf_lcvd_t *cvd = *pcvd;
1557
1558
1.69k
    if (cvd == NULL) {
1559
78
        cvd = gs_alloc_struct(mem, pdf_lcvd_t, &st_pdf_lcvd_t, "pdf_setup_masked_image_converter");
1560
78
        if (cvd == NULL)
1561
0
            return_error(gs_error_VMerror);
1562
78
        *pcvd = cvd;
1563
78
    }
1564
1.69k
    cvd->pdev = pdev;
1565
1.69k
    gs_make_mem_device(&cvd->mdev, gdev_mem_device_for_bits(pdev->color_info.depth),
1566
1.69k
                mem, 0, (gx_device *)pdev);
1567
    /* x and y are always non-negative here. */
1568
1.69k
    if (x < 0 || y < 0)
1569
0
        return_error(gs_error_Fatal);
1570
1.69k
    cvd->mdev.width  = w; /* The size we want to allocate for */
1571
1.69k
    cvd->mdev.height = h;
1572
1.69k
    cvd->mdev.mapped_x = x;
1573
1.69k
    cvd->mdev.mapped_y = y;
1574
1.69k
    cvd->mdev.bitmap_memory = mem;
1575
1.69k
    cvd->mdev.color_info = pdev->color_info;
1576
1.69k
    cvd->path_is_empty = true;
1577
1.69k
    cvd->mask_is_empty = true;
1578
1.69k
    cvd->mask_is_clean = false;
1579
1.69k
    cvd->filled_trap = false;
1580
1.69k
    cvd->has_background = false;
1581
1.69k
    cvd->pass = 0;
1582
1.69k
    cvd->mask = 0;
1583
1.69k
    cvd->write_matrix = true;
1584
1.69k
    code = (*dev_proc(&cvd->mdev, open_device))((gx_device *)&cvd->mdev);
1585
1.69k
    if (code < 0)
1586
0
        return code; /* FIXME: free cvd? */
1587
1.69k
    code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev,
1588
1.69k
                0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0);
1589
1.69k
    if (code < 0)
1590
0
        return code; /* FIXME: free cvd? */
1591
1.69k
    if (need_mask) {
1592
203
        mask = gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory, "pdf_setup_masked_image_converter");
1593
203
        if (mask == NULL)
1594
0
            return_error(gs_error_VMerror); /* FIXME: free cvd? */
1595
203
        cvd->mask = mask;
1596
203
        gs_make_mem_mono_device(mask, mem, (gx_device *)pdev);
1597
203
        mask->width = cvd->mdev.width;
1598
203
        mask->height = cvd->mdev.height;
1599
203
        mask->raster = gx_device_raster((gx_device *)mask, 1);
1600
203
        mask->bitmap_memory = mem;
1601
203
        code = (*dev_proc(mask, open_device))((gx_device *)mask);
1602
203
        if (code < 0)
1603
0
            return code; /* FIXME: free cvd? */
1604
203
        if (write_on_close) {
1605
57
            code = (*dev_proc(mask, fill_rectangle))((gx_device *)mask,
1606
57
                        0, 0, mask->width, mask->height, (gx_color_index)0);
1607
57
            if (code < 0)
1608
0
                return code; /* FIXME: free cvd? */
1609
57
        }
1610
203
    }
1611
1.69k
    cvd->std_copy_color = dev_proc(&cvd->mdev, copy_color);
1612
1.69k
    cvd->std_copy_mono = dev_proc(&cvd->mdev, copy_mono);
1613
1.69k
    cvd->std_fill_rectangle = dev_proc(&cvd->mdev, fill_rectangle);
1614
1.69k
    cvd->std_close_device = dev_proc(&cvd->mdev, close_device);
1615
1.69k
    cvd->std_get_clipping_box = dev_proc(&cvd->mdev, get_clipping_box);
1616
1.69k
    cvd->std_transform_pixel_region = dev_proc(&cvd->mdev, transform_pixel_region);
1617
1.69k
    if (!write_on_close) {
1618
        /* Type 3 images will write to the mask directly. */
1619
1.64k
        dev_proc(&cvd->mdev, fill_rectangle) = (need_mask ? lcvd_fill_rectangle_shifted2
1620
1.64k
                                                          : lcvd_fill_rectangle_shifted);
1621
1.64k
    } else {
1622
57
        dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted;
1623
57
    }
1624
1.69k
    dev_proc(&cvd->mdev, get_clipping_box) = lcvd_get_clipping_box_shifted_from_mdev;
1625
1.69k
    dev_proc(&cvd->mdev, copy_color) = lcvd_copy_color_shifted;
1626
1.69k
    dev_proc(&cvd->mdev, copy_mono) = lcvd_copy_mono_shifted;
1627
1.69k
    dev_proc(&cvd->mdev, dev_spec_op) = lcvd_dev_spec_op;
1628
1.69k
    dev_proc(&cvd->mdev, fill_path) = lcvd_handle_fill_path_as_shading_coverage;
1629
1.69k
    dev_proc(&cvd->mdev, transform_pixel_region) = lcvd_transform_pixel_region;
1630
1.69k
    dev_proc(&cvd->mdev, fill_trapezoid) = lcvd_fill_trapezoid;
1631
1.69k
    cvd->m = *m;
1632
1.69k
    if (write_on_close) {
1633
57
        cvd->mdev.is_open = true;
1634
57
        if (mask)
1635
57
            mask->is_open = true;
1636
57
        dev_proc(&cvd->mdev, close_device) = lcvd_close_device_with_writing;
1637
57
    }
1638
1.69k
    cvd->mdev.width  = w + x; /* The size we appear to the world as */
1639
1.69k
    cvd->mdev.height = h + y;
1640
1.69k
    return 0;
1641
1.69k
}
1642
1643
void
1644
pdf_remove_masked_image_converter(gx_device_pdf *pdev, pdf_lcvd_t *cvd, bool need_mask)
1645
1.62k
{
1646
1.62k
    cvd->mdev.width  -= cvd->mdev.mapped_x;
1647
1.62k
    cvd->mdev.height -= cvd->mdev.mapped_y;
1648
1.62k
    (*dev_proc(&cvd->mdev, close_device))((gx_device *)&cvd->mdev);
1649
1.62k
    if (cvd->mask) {
1650
125
        (*dev_proc(cvd->mask, close_device))((gx_device *)cvd->mask);
1651
125
        gs_free_object(cvd->mask->memory, cvd->mask, "pdf_remove_masked_image_converter");
1652
125
    }
1653
1.62k
}
1654
1655
/* ------ Driver procedures ------ */
1656
1657
/* Fill a path. */
1658
int
1659
gdev_pdf_fill_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath,
1660
                   const gx_fill_params * params,
1661
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
1662
4.35M
{
1663
4.35M
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
1664
4.35M
    int code;
1665
    /*
1666
     * HACK: we fill an empty path in order to set the clipping path
1667
     * and the color for writing text.  If it weren't for this, we
1668
     * could detect and skip empty paths before putting out the clip
1669
     * path or the color.  We also clip with an empty path in order
1670
     * to advance currentpoint for show operations without actually
1671
     * drawing anything.
1672
     */
1673
4.35M
    bool have_path;
1674
4.35M
    gs_fixed_rect box = {{0, 0}, {0, 0}}, box1;
1675
4.35M
    gs_rect box2;
1676
1677
4.35M
    if (pdev->Eps2Write) {
1678
3.35M
        gx_path_bbox(ppath, &box1);
1679
3.35M
        if (box1.p.x != 0 || box1.p.y != 0 || box1.q.x != 0 || box1.q.y != 0){
1680
3.31M
            if (pcpath != 0)
1681
3.31M
                rect_intersect(box1, pcpath->outer_box);
1682
            /* convert fixed point co-ordinates to floating point and account for resolution */
1683
3.31M
            box2.p.x = fixed2int(box1.p.x) / (pdev->HWResolution[0] / 72.0);
1684
3.31M
            box2.p.y = fixed2int(box1.p.y) / (pdev->HWResolution[1] / 72.0);
1685
3.31M
            box2.q.x = fixed2int(box1.q.x) / (pdev->HWResolution[0] / 72.0);
1686
3.31M
            box2.q.y = fixed2int(box1.q.y) / (pdev->HWResolution[1] / 72.0);
1687
            /* Finally compare the new BBox of the path with the existing EPS BBox */
1688
3.31M
            if (box2.p.x < pdev->BBox.p.x)
1689
4.45k
                pdev->BBox.p.x = box2.p.x;
1690
3.31M
            if (box2.p.y < pdev->BBox.p.y)
1691
10.0k
                pdev->BBox.p.y = box2.p.y;
1692
3.31M
            if (box2.q.x > pdev->BBox.q.x)
1693
7.44k
                pdev->BBox.q.x = box2.q.x;
1694
3.31M
            if (box2.q.y > pdev->BBox.q.y)
1695
5.66k
                pdev->BBox.q.y = box2.q.y;
1696
3.31M
        }
1697
3.35M
        if (pdev->AccumulatingBBox)
1698
2.99M
            return 0;
1699
3.35M
    }
1700
1.36M
    have_path = !gx_path_is_void(ppath);
1701
1.36M
    if (!have_path && !pdev->vg_initial_set) {
1702
        /* See lib/gs_pdfwr.ps about "initial graphic state". */
1703
39.3k
        pdf_prepare_initial_viewer_state(pdev, pgs);
1704
39.3k
        pdf_reset_graphics(pdev);
1705
39.3k
        return 0;
1706
39.3k
    }
1707
1.32M
    if (have_path) {
1708
1.14M
        code = gx_path_bbox(ppath, &box);
1709
1.14M
        if (code < 0)
1710
0
            return code;
1711
1.14M
    }
1712
1.32M
    box1 = box;
1713
1714
1.32M
    code = prepare_fill_with_clip(pdev, pgs, &box, have_path, pdcolor, pcpath);
1715
1.32M
    if (code == gs_error_rangecheck) {
1716
        /* Fallback to the default implermentation for handling
1717
           a transparency with CompatibilityLevel<=1.3 . */
1718
3.16k
        return gx_default_fill_path((gx_device *)pdev, pgs, ppath, params, pdcolor, pcpath);
1719
3.16k
    }
1720
1.32M
    if (code < 0)
1721
0
        return code;
1722
1.32M
    if (code == 1)
1723
27.8k
        return 0; /* Nothing to paint. */
1724
1.29M
    if (!have_path)
1725
170k
        return 0;
1726
1.12M
    code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor);
1727
1.12M
    if (code == gs_error_rangecheck) {
1728
15.3k
        const bool convert_to_image = ((pdev->CompatibilityLevel <= 1.2 ||
1729
9.54k
                pdev->params.ColorConversionStrategy != ccs_LeaveColorUnchanged) &&
1730
5.81k
                gx_dc_is_pattern2_color(pdcolor));
1731
1732
15.3k
        if (!convert_to_image) {
1733
            /* Fallback to the default implermentation for handling
1734
            a shading with CompatibilityLevel<=1.2 . */
1735
13.5k
            return gx_default_fill_path(dev, pgs, ppath, params, pdcolor, pcpath);
1736
13.5k
        } else {
1737
            /* Convert a shading into a bitmap
1738
               with CompatibilityLevel<=1.2 . */
1739
1.86k
            pdf_lcvd_t cvd, *pcvd = &cvd;
1740
1.86k
            int sx, sy;
1741
1.86k
            gs_fixed_rect bbox, bbox1;
1742
1.86k
            bool need_mask = gx_dc_pattern2_can_overlap(pdcolor);
1743
1.86k
            gs_matrix m, save_ctm = ctm_only(pgs), ms, msi, mm;
1744
1.86k
            gs_int_point rect_size;
1745
            /* double scalex = 1.9, scaley = 1.4; debug purpose only. */
1746
1.86k
            double scale, scalex, scaley;
1747
1.86k
            int log2_scale_x = 0, log2_scale_y = 0;
1748
1.86k
            gx_drawing_color dc = *pdcolor;
1749
1.86k
            gs_pattern2_instance_t pi = *(gs_pattern2_instance_t *)dc.ccolor.pattern;
1750
1.86k
            gs_gstate *pgs2 = gs_gstate_copy(pi.saved, gs_gstate_memory(pi.saved));
1751
1752
1.86k
            if (pgs2 == NULL)
1753
0
                return_error(gs_error_VMerror);
1754
1.86k
            dc.ccolor.pattern = (gs_pattern_instance_t *)&pi;
1755
1.86k
            pi.saved = pgs2;
1756
1.86k
            code = gx_path_bbox(ppath, &bbox);
1757
1.86k
            if (code < 0)
1758
0
                goto image_exit;
1759
1.86k
            rect_intersect(bbox, box);
1760
1.86k
            code = gx_dc_pattern2_get_bbox(pdcolor, &bbox1);
1761
1.86k
            if (code < 0)
1762
0
                goto image_exit;
1763
1.86k
            if (code)
1764
1.86k
                rect_intersect(bbox, bbox1);
1765
1.86k
            if (bbox.p.x >= bbox.q.x || bbox.p.y >= bbox.q.y) {
1766
244
                code = 0;
1767
244
                goto image_exit;
1768
244
            }
1769
1.62k
            sx = fixed2int(bbox.p.x);
1770
1.62k
            sy = fixed2int(bbox.p.y);
1771
1.62k
            gs_make_identity(&m);
1772
1.62k
            rect_size.x = fixed2int(bbox.q.x + fixed_half) - sx;
1773
1.62k
            rect_size.y = fixed2int(bbox.q.y + fixed_half) - sy;
1774
1.62k
            if (rect_size.x == 0 || rect_size.y == 0)
1775
0
                goto image_exit;
1776
1.62k
            m.tx = (float)sx;
1777
1.62k
            m.ty = (float)sy;
1778
1.62k
            cvd.path_offset.x = sx;
1779
1.62k
            cvd.path_offset.y = sy;
1780
1.62k
            scale = (double)rect_size.x * rect_size.y * pdev->color_info.num_components /
1781
1.62k
                    pdev->MaxShadingBitmapSize;
1782
1.62k
            if (scale > 1) {
1783
                /* This section (together with the call to 'path_scale' below)
1784
                   sets up a downscaling when converting the shading into bitmap.
1785
                   We used floating point numbers to debug it, but in production
1786
                   we prefer to deal only with integers being powers of 2
1787
                   in order to avoid possible distorsions when scaling paths.
1788
                */
1789
176
                log2_scale_x = log2_scale_y = ilog2((int)ceil(sqrt(scale)));
1790
176
                if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale)
1791
159
                    log2_scale_y++;
1792
176
                if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale)
1793
95
                    log2_scale_x++;
1794
176
                scalex = (double)(1 << log2_scale_x);
1795
176
                scaley = (double)(1 << log2_scale_y);
1796
176
                rect_size.x = (int)floor(rect_size.x / scalex + 0.5);
1797
176
                rect_size.y = (int)floor(rect_size.y / scaley + 0.5);
1798
176
                gs_make_scaling(1.0 / scalex, 1.0 / scaley, &ms);
1799
176
                gs_make_scaling(scalex, scaley, &msi);
1800
176
                gs_matrix_multiply(&msi, &m, &m);
1801
176
                gs_matrix_multiply(&ctm_only(pgs), &ms, &mm);
1802
176
                gs_setmatrix((gs_gstate *)pgs, &mm);
1803
176
                gs_matrix_multiply(&ctm_only(pgs2), &ms, &mm);
1804
176
                gs_setmatrix((gs_gstate *)pgs2, &mm);
1805
176
                sx = fixed2int(bbox.p.x / (int)scalex);
1806
176
                sy = fixed2int(bbox.p.y / (int)scaley);
1807
176
                cvd.path_offset.x = sx; /* m.tx / scalex */
1808
176
                cvd.path_offset.y = sy;
1809
176
            }
1810
1.62k
            if (sx < 0 || sy < 0)
1811
0
                return 0;
1812
1813
1.62k
            code = pdf_setup_masked_image_converter(pdev, pdev->memory, &m, &pcvd, need_mask, sx, sy,
1814
1.62k
                            rect_size.x, rect_size.y, false);
1815
1.62k
            if (code >= 0) {
1816
1.62k
                gs_path_enum cenum;
1817
1.62k
                gdev_vector_dopath_state_t state;
1818
1819
1.62k
                pcvd->has_background = gx_dc_pattern2_has_background(pdcolor);
1820
1.62k
                stream_puts(pdev->strm, "q\n");
1821
1.62k
                code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_clip | gx_path_type_optimize, NULL);
1822
1.62k
                if (code >= 0) {
1823
1.62k
                    stream_puts(pdev->strm, (params->rule < 0 ? "W n\n" : "W* n\n"));
1824
1.62k
                    pdf_put_matrix(pdev, NULL, &cvd.m, " cm q\n");
1825
1.62k
                    cvd.write_matrix = false;
1826
1.62k
                    if (!pcvd->has_background) {
1827
1.62k
                        pcvd->pass = 1;
1828
1.62k
                        code = gs_shading_do_fill_rectangle(pi.templat.Shading,
1829
1.62k
                             NULL, (gx_device *)&cvd.mdev, pgs2, !pi.shfill);
1830
1.62k
                        pcvd->pass = 2;
1831
1.62k
                        pcvd->mask_is_empty = true;
1832
1.62k
                        pcvd->path_is_empty = true;
1833
1.62k
                        pcvd->filled_trap = 0;
1834
1.62k
                    }
1835
1.62k
                    if (code >= 0) {
1836
1.43k
                        code = gs_shading_do_fill_rectangle(pi.templat.Shading,
1837
1.43k
                             NULL, (gx_device *)&cvd.mdev, pgs2, !pi.shfill);
1838
1.43k
                    }
1839
1.62k
                    if (code >= 0)
1840
1.43k
                        code = pdf_dump_converted_image(pdev, &cvd, 2);
1841
1.62k
                }
1842
1.62k
                stream_puts(pdev->strm, "Q Q\n");
1843
1.62k
                pdf_remove_masked_image_converter(pdev, &cvd, need_mask);
1844
1.62k
            }
1845
1.62k
            gs_setmatrix((gs_gstate *)pgs, &save_ctm);
1846
1.86k
image_exit:
1847
1.86k
            gs_gstate_free(pgs2);
1848
1.86k
            return code;
1849
1.62k
        }
1850
15.3k
    }
1851
1.10M
    if (code < 0)
1852
0
        return code;
1853
1.10M
    {
1854
1.10M
        stream *s = pdev->strm;
1855
1.10M
        double scale;
1856
1.10M
        gs_matrix smat, *psmat = NULL;
1857
1.10M
        gs_path_enum cenum;
1858
1.10M
        gdev_vector_dopath_state_t state;
1859
1860
1.10M
        if (pcpath) {
1861
1.10M
            rect_intersect(box1, box);
1862
1.10M
            if (box1.p.x > box1.q.x || box1.p.y > box1.q.y)
1863
216k
                return 0;   /* outside the clipping path */
1864
1.10M
        }
1865
891k
        if (params->flatness != pdev->state.flatness) {
1866
18.6k
            pprintg1(s, "%g i\n", params->flatness);
1867
18.6k
            pdev->state.flatness = params->flatness;
1868
18.6k
        }
1869
891k
        if (make_rect_scaling(pdev, &box1, 1.0, &scale)) {
1870
0
            gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale,
1871
0
                            &smat);
1872
0
            pdf_put_matrix(pdev, "q ", &smat, "cm\n");
1873
0
            psmat = &smat;
1874
0
        }
1875
891k
        code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_fill | gx_path_type_optimize, NULL);
1876
891k
        if (code < 0)
1877
0
            return code;
1878
1879
891k
        stream_puts(s, (params->rule < 0 ? "f\n" : "f*\n"));
1880
891k
        if (psmat != NULL)
1881
0
            stream_puts(pdev->strm, "Q\n");
1882
891k
    }
1883
0
    return 0;
1884
891k
}
1885
1886
/* Stroke a path. */
1887
int
1888
gdev_pdf_stroke_path(gx_device * dev, const gs_gstate * pgs,
1889
                     gx_path * ppath, const gx_stroke_params * params,
1890
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
1891
1.10M
{
1892
1.10M
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
1893
1.10M
    stream *s;
1894
1.10M
    int code;
1895
1.10M
    double scale, path_scale;
1896
1.10M
    bool set_ctm;
1897
1.10M
    gs_matrix mat;
1898
1.10M
    double prescale = 1;
1899
1.10M
    gs_fixed_rect bbox;
1900
1.10M
    gs_path_enum cenum;
1901
1.10M
    gdev_vector_dopath_state_t state;
1902
1903
1.10M
    if (gx_path_is_void(ppath))
1904
164k
        return 0;    /* won't mark the page */
1905
944k
    code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs);
1906
944k
    if (code < 0)
1907
0
        return code;
1908
944k
   if (pdf_must_put_clip_path(pdev, pcpath))
1909
70.8k
        code = pdf_unclip(pdev);
1910
873k
    else if ((pdev->last_charpath_op & TEXT_DO_FALSE_CHARPATH) && ppath->current_subpath &&
1911
0
        (ppath->last_charpath_segment == ppath->current_subpath->last) && !pdev->ForOPDFRead) {
1912
0
        bool hl_color = pdf_can_handle_hl_color((gx_device_vector *)pdev, pgs, pdcolor);
1913
0
        const gs_gstate *pgs_for_hl_color = (hl_color ? pgs : NULL);
1914
1915
0
        if (pdf_modify_text_render_mode(pdev->text->text_state, 1)) {
1916
            /* Set the colour for the stroke */
1917
0
            code = pdf_reset_color(pdev, pgs_for_hl_color, pdcolor, &pdev->saved_stroke_color,
1918
0
                        &pdev->stroke_used_process_color, &psdf_set_stroke_color_commands);
1919
0
            if (code == 0) {
1920
0
                s = pdev->strm;
1921
                /* Text is emitted scaled so that the CTM is an identity matrix, the line width
1922
                 * needs to be scaled to match otherwise we will get the default, or the current
1923
                 * width scaled by the CTM before the text, either of which would be wrong.
1924
                 */
1925
0
                scale = 72 / pdev->HWResolution[0];
1926
0
                scale *= fabs(pgs->ctm.xx);
1927
0
                pprintg1(s, "%g w\n", (pgs->line_params.half_width * 2) * (float)scale);
1928
                /* Some trickery here. We have altered the colour, text render mode and linewidth,
1929
                 * we don't want those to persist. By switching to a stream context we will flush the
1930
                 * pending text. This has the beneficial side effect of executing a grestore. So
1931
                 * everything works out neatly.
1932
                 */
1933
0
                code = pdf_open_page(pdev, PDF_IN_STREAM);
1934
0
                return(code);
1935
0
            }
1936
0
        }
1937
        /* Can only get here if any of the above steps fail, in which case we proceed to
1938
         * emit the charpath as a normal path, and stroke it.
1939
         */
1940
0
        code = pdf_open_page(pdev, PDF_IN_STREAM);
1941
0
    } else
1942
873k
        code = pdf_open_page(pdev, PDF_IN_STREAM);
1943
944k
    if (code < 0)
1944
0
        return code;
1945
944k
    code = pdf_prepare_stroke(pdev, pgs, false);
1946
944k
    if (code == gs_error_rangecheck) {
1947
        /* Fallback to the default implermentation for handling
1948
           a transparency with CompatibilityLevel<=1.3 . */
1949
401
        return gx_default_stroke_path((gx_device *)dev, pgs, ppath, params, pdcolor, pcpath);
1950
401
    }
1951
944k
    if (code < 0)
1952
0
        return code;
1953
944k
    code = pdf_put_clip_path(pdev, pcpath);
1954
944k
    if (code < 0)
1955
0
        return code;
1956
    /*
1957
     * If the CTM is not uniform, stroke width depends on angle.
1958
     * We'd like to avoid resetting the CTM, so we check for uniform
1959
     * CTMs explicitly.  Note that in PDF, unlike PostScript, it is
1960
     * the CTM at the time of the stroke operation, not the CTM at
1961
     * the time the path was constructed, that is used for transforming
1962
     * the points of the path; so if we have to reset the CTM, we must
1963
     * do it before constructing the path, and inverse-transform all
1964
     * the coordinates.
1965
     */
1966
944k
    set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev,
1967
944k
                                               pgs, &scale, &mat);
1968
944k
    if (set_ctm && ((pgs->ctm.xx == 0 && pgs->ctm.xy == 0) ||
1969
92.9k
                    (pgs->ctm.yx == 0 && pgs->ctm.yy == 0))) {
1970
        /* Acrobat Reader 5 and Adobe Reader 6 issues
1971
           the "Wrong operand type" error with matrices, which have 3 zero coefs.
1972
           Besides that, we found that Acrobat Reader 4, Acrobat Reader 5
1973
           and Adobe Reader 6 all store the current path in user space
1974
           and apply CTM in the time of stroking - See the bug 687901.
1975
           Therefore a precise conversion of Postscript to PDF isn't possible in this case.
1976
           Adobe viewers render a line with a constant width instead.
1977
           At last, with set_ctm == true we need the inverse matrix in
1978
           gdev_vector_dopath. Therefore we exclude projection matrices
1979
           (see bug 688363). */
1980
1.40k
        set_ctm = false;
1981
1.40k
        scale = fabs(pgs->ctm.xx + pgs->ctm.xy + pgs->ctm.yx + pgs->ctm.yy) /* Using the non-zero coeff. */
1982
1.40k
                / sqrt(2); /* Empirically from Adobe. */
1983
1.40k
    }
1984
944k
    if (set_ctm && pdev->PDFA == 1) {
1985
        /*
1986
         * We want a scaling factor that will bring the largest reasonable
1987
         * user coordinate within bounds.  We choose a factor based on the
1988
         * minor axis of the transformation.  Thanks to Raph Levien for
1989
         * the following formula.
1990
         */
1991
0
        double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy;
1992
0
        double u = fabs(a * d - b * c);
1993
0
        double v = a * a + b * b + c * c + d * d;
1994
0
        double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5;
1995
1996
0
        prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor);
1997
0
    }
1998
944k
    gx_path_bbox(ppath, &bbox);
1999
944k
    {
2000
        /* Check whether a painting appears inside the clipping box.
2001
           Doing so after writing the clipping path due to /SP pdfmark
2002
           uses a special hack with painting outside the clipping box
2003
           for synchronizing the clipping path (see lib/gs_pdfwr.ps).
2004
           That hack appeared because there is no way to pass
2005
           the gs_gstate through gdev_pdf_put_params,
2006
           which pdfmark is implemented with.
2007
        */
2008
944k
        gs_fixed_rect clip_box, stroke_bbox = bbox;
2009
944k
        gs_point d0, d1;
2010
944k
        gs_fixed_point p0, p1;
2011
944k
        fixed bbox_expansion_x, bbox_expansion_y;
2012
2013
944k
        gs_distance_transform(pgs->line_params.half_width, 0, &ctm_only(pgs), &d0);
2014
944k
        gs_distance_transform(0, pgs->line_params.half_width, &ctm_only(pgs), &d1);
2015
944k
        p0.x = float2fixed(any_abs(d0.x));
2016
944k
        p0.y = float2fixed(any_abs(d0.y));
2017
944k
        p1.x = float2fixed(any_abs(d1.x));
2018
944k
        p1.y = float2fixed(any_abs(d1.y));
2019
944k
        bbox_expansion_x = max(p0.x, p1.x) + fixed_1 * 2;
2020
944k
        bbox_expansion_y = max(p0.y, p1.y) + fixed_1 * 2;
2021
944k
        stroke_bbox.p.x -= bbox_expansion_x;
2022
944k
        stroke_bbox.p.y -= bbox_expansion_y;
2023
944k
        stroke_bbox.q.x += bbox_expansion_x;
2024
944k
        stroke_bbox.q.y += bbox_expansion_y;
2025
944k
        gx_cpath_outer_box(pcpath, &clip_box);
2026
944k
        rect_intersect(stroke_bbox, clip_box);
2027
944k
        if (stroke_bbox.q.x < stroke_bbox.p.x || stroke_bbox.q.y < stroke_bbox.p.y)
2028
31.5k
            return 0;
2029
944k
    }
2030
912k
    if (make_rect_scaling(pdev, &bbox, prescale, &path_scale)) {
2031
0
        scale /= path_scale;
2032
0
        if (set_ctm)
2033
0
            gs_matrix_scale(&mat, path_scale, path_scale, &mat);
2034
0
        else {
2035
0
            gs_make_scaling(path_scale, path_scale, &mat);
2036
0
            set_ctm = true;
2037
0
        }
2038
0
    }
2039
912k
    code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pgs, params,
2040
912k
                                      pdcolor, scale);
2041
912k
    if (code < 0)
2042
189
        return gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
2043
189
                                      pcpath);
2044
912k
    if (!pdev->HaveStrokeColor)
2045
418k
        pdev->saved_fill_color = pdev->saved_stroke_color;
2046
912k
    if (set_ctm)
2047
84.6k
        pdf_put_matrix(pdev, "q ", &mat, "cm\n");
2048
912k
    if (pgs->line_params.dash.offset != 0 || pgs->line_params.dash.pattern_size != 0)
2049
9.12k
        code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize | gx_path_type_dashed_stroke, (set_ctm ? &mat : (const gs_matrix *)0));
2050
903k
    else
2051
903k
        code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize, (set_ctm ? &mat : (const gs_matrix *)0));
2052
912k
    if (code < 0)
2053
0
        return code;
2054
912k
    s = pdev->strm;
2055
912k
    stream_puts(s, "S");
2056
912k
    stream_puts(s, (set_ctm ? " Q\n" : "\n"));
2057
912k
    if (pdev->Eps2Write) {
2058
186k
        pdev->AccumulatingBBox++;
2059
186k
        code = gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
2060
186k
                                      pcpath);
2061
186k
        pdev->AccumulatingBBox--;
2062
186k
        if (code < 0)
2063
1
            return code;
2064
186k
    }
2065
912k
    return 0;
2066
912k
}
2067
2068
int
2069
gdev_pdf_fill_stroke_path(gx_device *dev, const gs_gstate *pgs, gx_path *ppath,
2070
                          const gx_fill_params *fill_params, const gx_drawing_color *pdcolor_fill,
2071
                          const gx_stroke_params *stroke_params, const gx_drawing_color *pdcolor_stroke,
2072
    const gx_clip_path *pcpath)
2073
16.3k
{
2074
16.3k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
2075
16.3k
    int code;
2076
16.3k
    bool new_clip;
2077
16.3k
    bool have_path;
2078
2079
16.3k
    have_path = !gx_path_is_void(ppath);
2080
16.3k
    if (!have_path) {
2081
12.2k
        if (!pdev->vg_initial_set) {
2082
            /* See lib/gs_pdfwr.ps about "initial graphic state". */
2083
0
            pdf_prepare_initial_viewer_state(pdev, pgs);
2084
0
            pdf_reset_graphics(pdev);
2085
0
            return 0;
2086
0
        }
2087
12.2k
    }
2088
2089
    /* PostScript doesn't have a fill+stroke primitive, so break it into two operations
2090
     * PDF 1.2 only has a single overprint setting, we can't be certainto match that
2091
     * Because our inpu tcould be from a higher level. So be sure and break it into
2092
     * 2 operations.
2093
     */
2094
16.3k
    if (pdev->ForOPDFRead || pdev->CompatibilityLevel < 1.3) {
2095
10.4k
        code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath);
2096
10.4k
        if (code < 0)
2097
0
            return code;
2098
10.4k
        gs_swapcolors_quick(pgs);
2099
10.4k
        code = gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath);
2100
10.4k
        gs_swapcolors_quick(pgs);
2101
10.4k
        return code;
2102
10.4k
    } else {
2103
5.91k
        bool set_ctm;
2104
5.91k
        gs_matrix mat;
2105
5.91k
        double scale, path_scale;
2106
5.91k
        double prescale = 1;
2107
5.91k
        gs_fixed_rect bbox;
2108
5.91k
        gs_path_enum cenum;
2109
5.91k
        gdev_vector_dopath_state_t state;
2110
5.91k
        stream *s = pdev->strm;
2111
        /*
2112
         * Check for an empty clipping path.
2113
         */
2114
5.91k
        if (pcpath) {
2115
5.91k
            gs_fixed_rect cbox;
2116
2117
5.91k
            gx_cpath_outer_box(pcpath, &cbox);
2118
5.91k
            if (cbox.p.x >= cbox.q.x || cbox.p.y >= cbox.q.y)
2119
178
                return 1;   /* empty clipping path */
2120
5.91k
        }
2121
5.73k
        code = pdf_check_soft_mask(pdev, (gs_gstate *)pgs);
2122
5.73k
        if (code < 0)
2123
0
            return code;
2124
2125
5.73k
        new_clip = pdf_must_put_clip_path(pdev, pcpath);
2126
5.73k
        if (have_path || pdev->context == PDF_IN_NONE || new_clip) {
2127
1.79k
            if (new_clip)
2128
746
                code = pdf_unclip(pdev);
2129
1.04k
            else
2130
1.04k
                code = pdf_open_page(pdev, PDF_IN_STREAM);
2131
1.79k
            if (code < 0)
2132
0
                return code;
2133
1.79k
        }
2134
5.73k
        code = pdf_prepare_fill_stroke(pdev, pgs, false);
2135
5.73k
        if (code < 0)
2136
0
            return code;
2137
2138
5.73k
        code = pdf_put_clip_path(pdev, pcpath);
2139
5.73k
        if (code < 0)
2140
0
            return code;
2141
        /*
2142
         * If the CTM is not uniform, stroke width depends on angle.
2143
         * We'd like to avoid resetting the CTM, so we check for uniform
2144
         * CTMs explicitly.  Note that in PDF, unlike PostScript, it is
2145
         * the CTM at the time of the stroke operation, not the CTM at
2146
         * the time the path was constructed, that is used for transforming
2147
         * the points of the path; so if we have to reset the CTM, we must
2148
         * do it before constructing the path, and inverse-transform all
2149
         * the coordinates.
2150
         */
2151
5.73k
        set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev,
2152
5.73k
                                                   pgs, &scale, &mat);
2153
5.73k
        if (set_ctm && ((pgs->ctm.xx == 0 && pgs->ctm.xy == 0) ||
2154
1.12k
                        (pgs->ctm.yx == 0 && pgs->ctm.yy == 0))) {
2155
            /* Acrobat Reader 5 and Adobe Reader 6 issues
2156
               the "Wrong operand type" error with matrices, which have 3 zero coefs.
2157
               Besides that, we found that Acrobat Reader 4, Acrobat Reader 5
2158
               and Adobe Reader 6 all store the current path in user space
2159
               and apply CTM in the time of stroking - See the bug 687901.
2160
               Therefore a precise conversion of Postscript to PDF isn't possible in this case.
2161
               Adobe viewers render a line with a constant width instead.
2162
               At last, with set_ctm == true we need the inverse matrix in
2163
               gdev_vector_dopath. Therefore we exclude projection matrices
2164
               (see bug 688363). */
2165
39
            set_ctm = false;
2166
39
            scale = fabs(pgs->ctm.xx + pgs->ctm.xy + pgs->ctm.yx + pgs->ctm.yy) /* Using the non-zero coeff. */
2167
39
                    / sqrt(2); /* Empirically from Adobe. */
2168
39
        }
2169
5.73k
        if (pdev->PDFA == 1 && set_ctm) {
2170
            /*
2171
             * We want a scaling factor that will bring the largest reasonable
2172
             * user coordinate within bounds.  We choose a factor based on the
2173
             * minor axis of the transformation.  Thanks to Raph Levien for
2174
             * the following formula.
2175
             */
2176
0
            double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy;
2177
0
            double u = fabs(a * d - b * c);
2178
0
            double v = a * a + b * b + c * c + d * d;
2179
0
            double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5;
2180
2181
0
            prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor);
2182
0
        }
2183
5.73k
        gx_path_bbox(ppath, &bbox);
2184
5.73k
        {
2185
            /* Check whether a painting appears inside the clipping box.
2186
               Doing so after writing the clipping path due to /SP pdfmark
2187
               uses a special hack with painting outside the clipping box
2188
               for synchronizing the clipping path (see lib/gs_pdfwr.ps).
2189
               That hack appeared because there is no way to pass
2190
               the gs_gstate through gdev_pdf_put_params,
2191
               which pdfmark is implemented with.
2192
            */
2193
5.73k
            gs_fixed_rect clip_box, stroke_bbox = bbox;
2194
5.73k
            gs_point d0, d1;
2195
5.73k
            gs_fixed_point p0, p1;
2196
5.73k
            fixed bbox_expansion_x, bbox_expansion_y;
2197
2198
5.73k
            gs_distance_transform(pgs->line_params.half_width, 0, &ctm_only(pgs), &d0);
2199
5.73k
            gs_distance_transform(0, pgs->line_params.half_width, &ctm_only(pgs), &d1);
2200
5.73k
            p0.x = float2fixed(any_abs(d0.x));
2201
5.73k
            p0.y = float2fixed(any_abs(d0.y));
2202
5.73k
            p1.x = float2fixed(any_abs(d1.x));
2203
5.73k
            p1.y = float2fixed(any_abs(d1.y));
2204
5.73k
            bbox_expansion_x = max(p0.x, p1.x) + fixed_1 * 2;
2205
5.73k
            bbox_expansion_y = max(p0.y, p1.y) + fixed_1 * 2;
2206
5.73k
            stroke_bbox.p.x -= bbox_expansion_x;
2207
5.73k
            stroke_bbox.p.y -= bbox_expansion_y;
2208
5.73k
            stroke_bbox.q.x += bbox_expansion_x;
2209
5.73k
            stroke_bbox.q.y += bbox_expansion_y;
2210
5.73k
            gx_cpath_outer_box(pcpath, &clip_box);
2211
5.73k
            rect_intersect(stroke_bbox, clip_box);
2212
5.73k
            if (stroke_bbox.q.x < stroke_bbox.p.x || stroke_bbox.q.y < stroke_bbox.p.y)
2213
842
                return 0;
2214
5.73k
        }
2215
4.89k
        if (make_rect_scaling(pdev, &bbox, prescale, &path_scale)) {
2216
0
            scale /= path_scale;
2217
0
            if (set_ctm)
2218
0
                gs_matrix_scale(&mat, path_scale, path_scale, &mat);
2219
0
            else {
2220
0
                gs_make_scaling(path_scale, path_scale, &mat);
2221
0
                set_ctm = true;
2222
0
            }
2223
0
        }
2224
2225
4.89k
        code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor_fill);
2226
4.89k
        if (code == gs_error_rangecheck) {
2227
            /* rangecheck means we revert to the equivalent to the default implementation */
2228
189
            code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath);
2229
189
            if (code < 0)
2230
0
                return code;
2231
            /* Swap colors to make sure the pgs colorspace is correct for stroke */
2232
189
            gs_swapcolors_quick(pgs);
2233
189
            code = gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath);
2234
189
            gs_swapcolors_quick(pgs);
2235
189
            return code;
2236
189
        }
2237
2238
        /* Swap colors to make sure the pgs colorspace is correct for stroke */
2239
4.70k
        gs_swapcolors_quick(pgs);
2240
4.70k
        code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pgs, stroke_params,
2241
4.70k
                                          pdcolor_stroke, scale);
2242
4.70k
        gs_swapcolors_quick(pgs);
2243
4.70k
        if (code < 0) {
2244
0
            code = gdev_pdf_fill_path(dev, pgs, ppath, fill_params, pdcolor_fill, pcpath);
2245
0
            if (code < 0)
2246
0
                return code;
2247
0
            return gdev_pdf_stroke_path(dev, pgs, ppath, stroke_params, pdcolor_stroke, pcpath);
2248
0
        }
2249
4.70k
        if (!pdev->HaveStrokeColor)
2250
0
            pdev->saved_fill_color = pdev->saved_stroke_color;
2251
4.70k
        if (set_ctm)
2252
903
            pdf_put_matrix(pdev, "q ", &mat, "cm\n");
2253
4.70k
        if (pgs->line_params.dash.offset != 0 || pgs->line_params.dash.pattern_size != 0)
2254
6
            code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize | gx_path_type_dashed_stroke, (set_ctm ? &mat : (const gs_matrix *)0));
2255
4.70k
        else
2256
4.70k
            code = pdf_write_path(pdev, (gs_path_enum *)&cenum, &state, (gx_path *)ppath, 0, gx_path_type_stroke | gx_path_type_optimize, (set_ctm ? &mat : (const gs_matrix *)0));
2257
4.70k
        if (code < 0)
2258
0
            return code;
2259
4.70k
        s = pdev->strm;
2260
4.70k
        stream_puts(s, (fill_params->rule < 0 ? "B\n" : "B*\n"));
2261
4.70k
        stream_puts(s, (set_ctm ? " Q\n" : "\n"));
2262
4.70k
    }
2263
4.70k
    return 0;
2264
16.3k
}
2265
2266
/*
2267
   The fill_rectangle_hl_color device method.
2268
   See gxdevcli.h about return codes.
2269
 */
2270
int
2271
gdev_pdf_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect,
2272
    const gs_gstate *pgs, const gx_drawing_color *pdcolor,
2273
    const gx_clip_path *pcpath)
2274
17.1k
{
2275
17.1k
    int code;
2276
17.1k
    gs_fixed_rect box1 = *rect, box = box1;
2277
17.1k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
2278
17.1k
    double scale;
2279
17.1k
    gs_matrix smat, *psmat = NULL;
2280
17.1k
    const bool convert_to_image = (pdev->CompatibilityLevel <= 1.2 &&
2281
10.5k
            gx_dc_is_pattern2_color(pdcolor));
2282
2283
17.1k
    if (rect->p.x == rect->q.x)
2284
462
        return 0;
2285
16.6k
    if (!convert_to_image) {
2286
16.6k
        code = prepare_fill_with_clip(pdev, pgs, &box, true, pdcolor, pcpath);
2287
16.6k
        if (code < 0)
2288
0
            return code;
2289
16.6k
        if (code == 1)
2290
0
            return 0; /* Nothing to paint. */
2291
16.6k
        code = pdf_setfillcolor((gx_device_vector *)pdev, pgs, pdcolor);
2292
16.6k
        if (code < 0)
2293
0
            return code;
2294
16.6k
        if (pcpath)
2295
16.6k
            rect_intersect(box1, box);
2296
16.6k
        if (box1.p.x > box1.q.x || box1.p.y > box1.q.y)
2297
0
            return 0;   /* outside the clipping path */
2298
16.6k
        if (make_rect_scaling(pdev, &box1, 1.0, &scale)) {
2299
0
            gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale, &smat);
2300
0
            pdf_put_matrix(pdev, "q ", &smat, "cm\n");
2301
0
            psmat = &smat;
2302
0
        }
2303
16.6k
        pprintg4(pdev->strm, "%g %g %g %g re f\n",
2304
16.6k
                fixed2float(box1.p.x) / scale, fixed2float(box1.p.y) / scale,
2305
16.6k
                fixed2float(box1.q.x - box1.p.x) / scale, fixed2float(box1.q.y - box1.p.y) / scale);
2306
16.6k
        if (psmat != NULL)
2307
0
            stream_puts(pdev->strm, "Q\n");
2308
16.6k
        if (pdev->Eps2Write) {
2309
10.5k
            gs_rect *Box;
2310
2311
10.5k
            if (!pdev->accumulating_charproc)
2312
10.5k
                Box = &pdev->BBox;
2313
0
            else
2314
0
                Box = &pdev->charproc_BBox;
2315
2316
10.5k
            if (fixed2float(box1.p.x) / (pdev->HWResolution[0] / 72.0) < Box->p.x)
2317
849
                Box->p.x = fixed2float(box1.p.x) / (pdev->HWResolution[0] / 72.0);
2318
10.5k
            if (fixed2float(box1.p.y) / (pdev->HWResolution[1] / 72.0) < Box->p.y)
2319
836
                Box->p.y = fixed2float(box1.p.y) / (pdev->HWResolution[1] / 72.0);
2320
10.5k
            if (fixed2float(box1.q.x) / (pdev->HWResolution[0] / 72.0) > Box->q.x)
2321
1.26k
                Box->q.x = fixed2float(box1.q.x) / (pdev->HWResolution[0] / 72.0);
2322
10.5k
            if (fixed2float(box1.q.y) / (pdev->HWResolution[1] / 72.0) > Box->q.y)
2323
1.03k
                Box->q.y = fixed2float(box1.q.y) / (pdev->HWResolution[1] / 72.0);
2324
10.5k
        }
2325
16.6k
        return 0;
2326
16.6k
    } else {
2327
0
        gx_fill_params params;
2328
0
        gx_path path;
2329
2330
0
        params.rule = 1; /* Not important because the path is a rectange. */
2331
0
        params.adjust.x = params.adjust.y = 0;
2332
0
        params.flatness = pgs->flatness;
2333
0
        gx_path_init_local(&path, pgs->memory);
2334
0
        code = gx_path_add_rectangle(&path, rect->p.x, rect->p.y, rect->q.x, rect->q.y);
2335
0
        if (code < 0)
2336
0
            return code;
2337
0
        code = gdev_pdf_fill_path(dev, pgs, &path, &params, pdcolor, pcpath);
2338
0
        if (code < 0)
2339
0
            return code;
2340
0
        gx_path_free(&path, "gdev_pdf_fill_rectangle_hl_color");
2341
0
        return code;
2342
2343
0
    }
2344
16.6k
}
2345
2346
int
2347
gdev_pdf_fillpage(gx_device *dev, gs_gstate * pgs, gx_device_color *pdevc)
2348
300k
{
2349
300k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
2350
300k
    int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0);
2351
2352
300k
    if (gx_dc_pure_color(pdevc) == pdev->white && !is_in_page(pdev) && pdev->sbstack_depth <= bottom) {
2353
        /* PDF doesn't need to erase the page if its plain white */
2354
288k
        return 0;
2355
288k
    }
2356
12.4k
    else
2357
12.4k
        return gx_default_fillpage(dev, pgs, pdevc);
2358
300k
}