Coverage Report

Created: 2026-11-12 07:09

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