Coverage Report

Created: 2026-09-14 07:34

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