Coverage Report

Created: 2026-04-01 07:17

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