Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/gxclpath.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Higher-level path operations for band lists */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gpcheck.h"
22
#include "gserrors.h"
23
#include "gsptype2.h"
24
#include "gsptype1.h"
25
#include "gxdevice.h"
26
#include "gxdevmem.h"   /* must precede gxcldev.h */
27
#include "gxcldev.h"
28
#include "gxclpath.h"
29
#include "gxcolor2.h"
30
#include "gxdcolor.h"
31
#include "gxpcolor.h"
32
#include "gxpaint.h"    /* for gx_fill/stroke_params */
33
#include "gzpath.h"
34
#include "gzcpath.h"
35
#include "stream.h"
36
#include "gsserial.h"
37
#include "gxdevsop.h"
38
39
/* Statistics */
40
#ifdef COLLECT_STATS_CLIST
41
ulong stats_cmd_diffs[5];
42
#endif
43
44
/* Forward declarations */
45
static int cmd_put_path(gx_device_clist_writer * cldev,
46
                         gx_clist_state * pcls, const gx_path * ppath,
47
                         fixed ymin, fixed ymax, byte op,
48
                         bool implicit_close, segment_notes keep_notes);
49
50
/* ------ Utilities ------ */
51
52
/* Compute the colors used by a colored halftone. */
53
static gx_color_index
54
colored_halftone_color_usage(gx_device_clist_writer *cldev,
55
                             const gx_drawing_color *pdcolor)
56
1.65M
{
57
    /*
58
     * We only know how to compute an accurate color set for the
59
     * standard CMYK color mapping function.
60
     */
61
1.65M
    if (dev_proc(cldev, dev_spec_op)((gx_device *)cldev,
62
1.65M
                                     gxdso_is_std_cmyk_1bit, NULL, 0) <= 0)
63
1.65M
        return ((gx_color_index)1 << cldev->color_info.depth) - 1;  /* What about transparency?  Need to check this */
64
    /*
65
     * Note that c_base[0], and the low-order bit of plane_mask,
66
     * correspond to cyan: this requires reversing the bit order of
67
     * the plane mask.
68
     */
69
0
    return
70
0
        ((pdcolor->colors.colored.c_base[0] << 3) |
71
0
         (pdcolor->colors.colored.c_base[1] << 2) |
72
0
         (pdcolor->colors.colored.c_base[2] << 1) |
73
0
         (pdcolor->colors.colored.c_base[3]) |
74
0
         (byte_reverse_bits[pdcolor->colors.colored.plane_mask] >> 4));
75
1.65M
}
76
77
/*
78
 * Compute whether a drawing operation will require the slow (full-pixel)
79
 * RasterOp implementation.  If pdcolor is not NULL, it is the texture for
80
 * the RasterOp.
81
 */
82
bool
83
cmd_slow_rop(gx_device *dev, gs_logical_operation_t lop,
84
    const gx_drawing_color *pdcolor)
85
12.0M
{
86
12.0M
    gs_rop3_t rop = lop_rop(lop);
87
88
12.0M
    if (pdcolor != 0 && gx_dc_is_pure(pdcolor)) {
89
9.64M
        gx_color_index color = gx_dc_pure_color(pdcolor);
90
91
9.64M
        if (color == gx_device_black(dev))
92
8.57M
            rop = rop3_know_T_0(rop);
93
1.07M
        else if (color == gx_device_white(dev))
94
590k
            rop = rop3_know_T_1(rop);
95
9.64M
    }
96
12.0M
    return !(rop == rop3_0 || rop == rop3_1 ||
97
12.0M
             rop == rop3_S || rop == rop3_T);
98
12.0M
}
99
100
/* Write out the color for filling, stroking, or masking. */
101
/* We should be able to share this with clist_tile_rectangle, */
102
/* but I don't see how to do it without adding a level of procedure. */
103
/* If the pattern color is big, it can write to "all" bands. */
104
int
105
cmd_put_drawing_color(gx_device_clist_writer * cldev, gx_clist_state * pcls,
106
                      const gx_drawing_color * pdcolor, cmd_rects_enum_t *pre,
107
                      dc_devn_cl_type devn_type)
108
24.7M
{
109
24.7M
    const gx_device_halftone * pdht = pdcolor->type->get_dev_halftone(pdcolor);
110
24.7M
    int                        code, di;
111
24.7M
    uint                       dc_size = 0, req_size;
112
24.7M
    gx_device_color_saved *    psdc = &pcls->sdc;
113
24.7M
    byte *                     dp;
114
24.7M
    byte *                     dp0;
115
24.7M
    gs_int_point               color_phase;
116
24.7M
    int            buffer_space;
117
24.7M
    int            offset = 0;
118
24.7M
    int            left;
119
24.7M
    uint           portion_size, prefix_size;
120
24.7M
    int            req_size_final;
121
24.7M
    bool           is_pattern;
122
24.7M
    gs_id          pattern_id = gs_no_id;
123
24.7M
    bool           all_bands = (pre == NULL);
124
125
    /* see if the halftone must be inserted in the command list */
126
24.7M
    if ( pdht != NULL                          &&
127
24.7M
         pdht->id != cldev->device_halftone_id   ) {
128
2.35k
        if ((code = cmd_put_halftone(cldev, pdht)) < 0)
129
0
            return code;
130
2.35k
        psdc->type = gx_dc_type_none; /* force writing */
131
2.35k
    }
132
133
24.7M
    if (psdc->devn_type != devn_type) {
134
266k
        psdc->type = gx_dc_type_none; /* force writing if fill/stroke mismatch. */
135
266k
        psdc->devn_type = devn_type;
136
266k
    }
137
    /*
138
     * Get the device color type index and the required size.
139
     *
140
     * The complete cmd_opv_ext_put_drawing_color consists of:
141
     *  command code (2 bytes)
142
     *  tile index value or non tile color (1)
143
     *  device color type index (1)
144
     *  length of serialized device color (enc_u_sizew(dc_size))
145
     *  the serialized device color itself (dc_size)
146
     */
147
24.7M
    di = gx_get_dc_type_index(pdcolor);
148
24.7M
    code = pdcolor->type->write( pdcolor,
149
24.7M
                                 psdc,
150
24.7M
                                 (gx_device *)cldev,
151
24.7M
                                 0,
152
24.7M
                                 0,
153
24.7M
                                 &dc_size );
154
155
    /* if the returned value is > 0, no change in the color is necessary */
156
24.7M
    if (code > 0 && ((devn_type == devn_not_tile_fill) || (devn_type == devn_not_tile_stroke)))
157
21.4M
        return 0;
158
3.21M
    else if (code < 0 && code != gs_error_rangecheck)
159
66
        return code;
160
3.21M
    if (!all_bands && dc_size * pre->rect_nbands > 1024*1024 /* arbitrary */)
161
296
        all_bands = true;
162
3.21M
    is_pattern = gx_dc_is_pattern1_color(pdcolor);
163
3.21M
    if (is_pattern)
164
35.8k
        pattern_id = gs_dc_get_pattern_id(pdcolor);
165
3.21M
    if (all_bands) {
166
56.5k
        gx_clist_state * pcls1;
167
168
62.0k
        for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++) {
169
61.7k
            if (pcls1->pattern_id == pattern_id) {
170
56.2k
                pcls->pattern_id = gs_no_id; /* Force writing entire pattern. */
171
56.2k
                break;
172
56.2k
            }
173
61.7k
        }
174
56.5k
    }
175
3.21M
    left = dc_size;
176
177
3.21M
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
178
    /* see if phase information must be inserted in the command list */
179
3.21M
    if (pdcolor->type->get_phase(pdcolor, &color_phase) &&
180
3.21M
        (color_phase.x != pcls->screen_phase[gs_color_select_texture].x ||
181
2.62M
         color_phase.y != pcls->screen_phase[gs_color_select_texture].y || all_bands)) {
182
        /* Devc phase is the reverse of screen phase! */
183
296
        code = cmd_set_screen_phase_generic(cldev, pcls,
184
296
                                            -color_phase.x, -color_phase.y,
185
296
                                            gs_color_select_texture, all_bands);
186
296
        if (code < 0)
187
0
            return code;
188
296
    }
189
190
3.21M
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
191
3.21M
    if (is_pattern) {
192
35.8k
        pattern_id = gs_dc_get_pattern_id(pdcolor);
193
194
35.8k
        if (pattern_id != gs_no_id && pcls->pattern_id == pattern_id) {
195
            /* The pattern is known, write its id only.
196
               Note that gx_dc_pattern_write must process this case especially. */
197
            /* Note that id is gs_no_id when the pattern supplies an empty tile.
198
               In this case the full serialized pattern is shorter (left == 0),
199
               so go with it. */
200
47
            left = sizeof(pattern_id);
201
47
        }
202
35.8k
    }
203
204
3.21M
    do {
205
3.21M
        int extop;
206
3.21M
        prefix_size = 2 + 1 + (offset > 0 ? enc_u_sizew(offset) : 0);
207
3.21M
        req_size = left + prefix_size + enc_u_sizew(left);
208
3.21M
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
209
3.21M
        code = cmd_get_buffer_space(cldev, pcls, req_size);
210
3.21M
        if (code < 0)
211
0
            return code;
212
3.21M
        buffer_space = min(code, req_size);
213
3.21M
        portion_size = buffer_space - prefix_size - enc_u_sizew(left);
214
3.21M
        req_size_final = portion_size + prefix_size + enc_u_sizew(portion_size);
215
3.21M
        if (req_size_final > buffer_space)
216
0
            return_error(gs_error_unregistered); /* Must not happen. */
217
3.21M
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
218
3.21M
        switch (devn_type) {
219
3.02M
            case devn_not_tile_fill:
220
3.02M
                extop = cmd_opv_ext_put_fill_dcolor;
221
3.02M
                break;
222
192k
            case devn_not_tile_stroke:
223
192k
                extop = cmd_opv_ext_put_stroke_dcolor;
224
192k
                break;
225
0
            case devn_tile0:
226
0
                extop = cmd_opv_ext_put_tile_devn_color0;
227
0
                break;
228
0
            case devn_tile1:
229
0
                extop = cmd_opv_ext_put_tile_devn_color1;
230
0
                break;
231
0
            default:
232
0
                extop = cmd_opv_ext_put_fill_dcolor;
233
3.21M
        }
234
3.21M
        if (all_bands)
235
59.2k
            code = set_cmd_put_all_extended_op(&dp, cldev, extop, req_size_final);
236
3.15M
        else
237
3.15M
            code = set_cmd_put_extended_op(&dp, cldev, pcls, extop, req_size_final);
238
3.21M
        if (code < 0)
239
0
            return code;
240
3.21M
        dp0 = dp;
241
3.21M
        dp += 2;
242
3.21M
        *dp++ = di | (offset > 0 ? 0x80 : 0);
243
3.21M
        if (offset > 0)
244
3.21M
            enc_u_putw(offset, dp);
245
3.21M
        enc_u_putw(portion_size, dp);
246
3.21M
        code = pdcolor->type->write( pdcolor,
247
3.21M
                                     &pcls->sdc,
248
3.21M
                                     (gx_device *)cldev,
249
3.21M
                                     offset,
250
3.21M
                                     dp,
251
3.21M
                                     &portion_size);
252
3.21M
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
253
3.21M
        if (code < 0) {
254
0
            if (offset == 0)
255
0
                cldev->cnext = dp0;
256
0
            return code;
257
0
        }
258
3.21M
        offset += portion_size;
259
3.21M
        left -= portion_size;
260
3.21M
    } while (left);
261
262
    /* attempt to properly calculate color_usage */
263
3.21M
    pcls->color_usage.or |= cmd_drawing_color_usage(cldev, pdcolor);
264
265
    /* record the color we have just serialized color */
266
3.21M
    pdcolor->type->save_dc(pdcolor, &pcls->sdc);
267
3.21M
    if (pattern_id != gs_no_id) {
268
        /* Don't record empty tiles because they're not cached. */
269
18.4k
        pcls->pattern_id = pattern_id;
270
18.4k
    }
271
3.21M
    if (is_pattern) {
272
        /* HACK: since gx_dc_pattern_write identifies pattern by tile id,
273
           replace the client's pattern id with tile id in the saved color.  */
274
35.8k
        pcls->sdc.colors.pattern.id = pattern_id;
275
35.8k
        if (pattern_id &&
276
35.8k
            (gx_pattern1_get_transptr(pdcolor) != NULL ||
277
18.4k
             gx_pattern1_clist_has_trans(pdcolor))) {
278
            /* update either this band or all bands with the trans_bbox */
279
2.13k
            if (all_bands) {
280
19
                pcls->color_usage.trans_bbox.p.x = 0;
281
19
                pcls->color_usage.trans_bbox.q.x = cldev->width;  /* no other information available */
282
19
                pcls->color_usage.trans_bbox.p.y = 0;
283
19
                pcls->color_usage.trans_bbox.q.y = cldev->height;
284
19
                clist_update_trans_bbox(cldev, &(pcls->color_usage.trans_bbox));
285
2.11k
            } else {
286
2.11k
                pcls->color_usage.trans_bbox.p.x = 0;
287
2.11k
                pcls->color_usage.trans_bbox.q.x = cldev->width;  /* no other information available */
288
2.11k
                pcls->color_usage.trans_bbox.p.y = pre->y;
289
2.11k
                pcls->color_usage.trans_bbox.q.y = pre->yend;
290
2.11k
            }
291
2.13k
        }
292
35.8k
    }
293
3.21M
    if (is_pattern && all_bands) {
294
        /* Distribute the written pattern params to all bands.
295
           We know it is big, so it is not empty, so it has pattern_id and tile_phase.
296
         */
297
296
        gx_clist_state * pcls1;
298
299
5.84k
        for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++) {
300
5.54k
            pcls1->sdc = pcls->sdc;
301
5.54k
            pcls1->pattern_id = pcls->pattern_id;
302
5.54k
            pcls1->tile_phase.x = pcls->tile_phase.x;
303
5.54k
            pcls1->tile_phase.y = pcls->tile_phase.y;
304
5.54k
            pcls1->color_usage.or = pcls->color_usage.or;
305
5.54k
        }
306
296
    }
307
3.21M
    return code;
308
3.21M
}
309
310
/* Compute the colors used by a drawing color. */
311
/* If the device is using transparency, the pdf14 compositor may have */
312
/* altered the colorspace. If so, just flag all components used.      */
313
gx_color_usage_bits
314
cmd_drawing_color_usage(gx_device_clist_writer *cldev,
315
                        const gx_drawing_color * pdcolor)
316
3.31M
{
317
3.31M
    if (cldev->page_uses_transparency &&
318
3.31M
        (cldev->color_info.polarity != cldev->clist_color_info.polarity ||
319
366k
        (cldev->color_info.num_components != cldev->clist_color_info.num_components))) {
320
        /* we would have to transform the color which would impact performance */
321
21.9k
        return gx_color_usage_all(cldev);
322
21.9k
    }
323
324
3.29M
    if (gx_dc_is_pure(pdcolor))
325
625k
        return gx_color_index2usage((gx_device *)cldev, gx_dc_pure_color(pdcolor));
326
2.67M
    else if (gx_dc_is_binary_halftone(pdcolor))
327
981k
        return gx_color_index2usage((gx_device *)cldev,
328
981k
                                    gx_color_index2usage((gx_device *)cldev, gx_dc_binary_color0(pdcolor)) |
329
981k
                                    gx_color_index2usage((gx_device *)cldev, gx_dc_binary_color1(pdcolor)));
330
1.68M
    else if (gx_dc_is_colored_halftone(pdcolor))
331
1.65M
        return gx_color_index2usage((gx_device *)cldev, colored_halftone_color_usage(cldev, pdcolor));
332
35.0k
    else if (gx_dc_is_devn(pdcolor)) {
333
0
        gx_color_usage_bits bits = 0;
334
335
0
        gx_dc_devn_get_nonzero_comps(pdcolor, (gx_device *)cldev, &bits);
336
0
        return bits;
337
0
    }
338
35.0k
    else
339
35.0k
        return gx_color_usage_all(cldev);
340
3.29M
}
341
342
/* Clear (a) specific 'known' flag(s) for all bands. */
343
/* We must do this whenever the value of a 'known' parameter changes. */
344
void
345
cmd_clear_known(gx_device_clist_writer * cldev, uint known)
346
362k
{
347
362k
    uint unknown = ~known;
348
362k
    gx_clist_state *pcls = cldev->states;
349
362k
    int i;
350
351
21.2M
    for (i = cldev->nbands; --i >= 0; ++pcls)
352
20.9M
        pcls->known &= unknown;
353
362k
}
354
355
/* Check whether we need to change the clipping path in the device. */
356
bool
357
cmd_check_clip_path(gx_device_clist_writer * cldev, const gx_clip_path * pcpath)
358
5.10M
{
359
5.10M
    if (pcpath == NULL)
360
0
        return false;
361
    /* The clip path might have moved in memory, so even if the */
362
    /* ids match, update the pointer. */
363
5.10M
    cldev->clip_path = pcpath;
364
5.10M
    if (pcpath->id == cldev->clip_path_id)
365
4.87M
        return false;
366
232k
    cldev->clip_path_id = pcpath->id;
367
232k
    return true;
368
5.10M
}
369
370
/*
371
 * Check the graphics state elements that need to be up to date for filling
372
 * or stroking.
373
 */
374
#define FILL_KNOWN\
375
 (cj_ac_sa_known | flatness_known | op_bm_tk_known | ais_known |\
376
  fill_alpha_known | fill_adjust_known | stroke_alpha_known | clip_path_known)
377
static void
378
cmd_check_fill_known(gx_device_clist_writer* cdev, const gs_gstate* pgs,
379
    double flatness, const gs_fixed_point* padjust,
380
    const gx_clip_path* pcpath, uint* punknown)
381
962k
{
382
    /*
383
     * stroke_adjust is not needed for fills, and none of these are needed
384
     * if the path has no curves, but it's easier to update them all.
385
     */
386
962k
    if (state_neq(line_params.curve_join) || state_neq(accurate_curves) ||
387
962k
        state_neq(stroke_adjust)
388
962k
        ) {
389
296
        *punknown |= cj_ac_sa_known;
390
296
        state_update(line_params.curve_join);
391
296
        state_update(accurate_curves);
392
296
        state_update(stroke_adjust);
393
296
    }
394
962k
    if (cdev->gs_gstate.flatness != flatness) {
395
6.85k
        *punknown |= flatness_known;
396
6.85k
        cdev->gs_gstate.flatness = flatness;
397
6.85k
    }
398
    /*
399
     * Note: overprint and overprint_mode are implemented via a compositor
400
     * device, which is passed separately through the command list. Hence,
401
     * though both parameters are passed in the state as well, this usually
402
     * has no effect.
403
     */
404
962k
    if (state_neq(overprint) || state_neq(overprint_mode) ||
405
962k
        state_neq(blend_mode) || state_neq(text_knockout) ||
406
962k
        state_neq(stroke_overprint) || state_neq(renderingintent)) {
407
10.9k
        *punknown |= op_bm_tk_known;
408
10.9k
        state_update(overprint);
409
10.9k
        state_update(overprint_mode);
410
10.9k
        state_update(blend_mode);
411
10.9k
        state_update(text_knockout);
412
10.9k
        state_update(stroke_overprint);
413
10.9k
        state_update(renderingintent);
414
10.9k
    }
415
962k
    if (state_neq(alphaisshape)) {
416
152
        *punknown |= ais_known;
417
152
        state_update(alphaisshape);
418
152
    }
419
962k
    if (state_neq(strokeconstantalpha)) {
420
867
        *punknown |= stroke_alpha_known;
421
867
        state_update(strokeconstantalpha);
422
867
    }
423
962k
    if (cdev->gs_gstate.fillconstantalpha != pgs->fillconstantalpha) {
424
909
        *punknown |= fill_alpha_known;
425
909
        state_update(fillconstantalpha);
426
909
    }
427
962k
    if (cdev->gs_gstate.fill_adjust.x != padjust->x ||
428
962k
        cdev->gs_gstate.fill_adjust.y != padjust->y
429
962k
        ) {
430
1.64k
        *punknown |= fill_adjust_known;
431
1.64k
        cdev->gs_gstate.fill_adjust = *padjust;
432
1.64k
    }
433
962k
    if (cmd_check_clip_path(cdev, pcpath))
434
27.1k
        *punknown |= clip_path_known;
435
962k
}
436
437
/* Compute the written CTM length. */
438
int
439
cmd_write_ctm_return_length(gx_device_clist_writer * cldev, const gs_matrix *m)
440
319k
{
441
319k
    stream s;
442
443
319k
    s_init(&s, cldev->memory);
444
319k
    swrite_position_only(&s);
445
319k
    sput_matrix(&s, m);
446
319k
    return (uint)stell(&s);
447
319k
}
448
449
/* Compute the written CTM length. */
450
int
451
cmd_write_ctm_return_length_nodevice(const gs_matrix *m)
452
117k
{
453
117k
    stream s;
454
455
117k
    s_init(&s, NULL);
456
117k
    swrite_position_only(&s);
457
117k
    sput_matrix(&s, m);
458
117k
    return (uint)stell(&s);
459
117k
}
460
461
/* Write out CTM. */
462
int
463
cmd_write_ctm(const gs_matrix *m, byte *dp, int len)
464
437k
{
465
437k
    stream s;
466
467
437k
    s_init(&s, NULL);
468
437k
    swrite_string(&s, dp + 1, len);
469
437k
    sput_matrix(&s, m);
470
437k
    return 0;
471
437k
}
472
473
/* Write out values of any unknown parameters. */
474
int
475
cmd_write_unknown(gx_device_clist_writer * cldev, gx_clist_state * pcls,
476
                  uint must_know)
477
599k
{
478
599k
    uint unknown = ~pcls->known & must_know;
479
599k
    uint misc2_unknown = unknown & misc2_all_known;
480
599k
    byte *dp;
481
599k
    int code;
482
483
599k
    if (misc2_unknown) {
484
251k
        byte buf[
485
251k
                 1 +    /* cap_join: start_cap|join */
486
251k
                 1 +            /*           end_cap|dash_cap */
487
251k
                 1 +    /* cj_ac_sa */
488
251k
                 sizeof(float) +  /* flatness */
489
251k
                 sizeof(float) +  /* line width */
490
251k
                 sizeof(float) +  /* miter limit */
491
251k
                 3 +    /* bm_tk, op, and rend intent */
492
251k
                 sizeof(cldev->gs_gstate.alphaisshape) +
493
251k
                 sizeof(float) * 2  /* ca CA */
494
251k
                 ];
495
251k
        byte *bp = buf;
496
251k
        if (unknown & cap_join_known) {
497
40.5k
            *bp++ = (cldev->gs_gstate.line_params.start_cap << 3) +
498
40.5k
                cldev->gs_gstate.line_params.join;
499
40.5k
            *bp++ = (cldev->gs_gstate.line_params.end_cap << 3) +
500
40.5k
                cldev->gs_gstate.line_params.dash_cap;
501
40.5k
        }
502
251k
        if (unknown & cj_ac_sa_known) {
503
8.68k
            *bp++ =
504
8.68k
                ((cldev->gs_gstate.line_params.curve_join + 1) << 2) +
505
8.68k
                (cldev->gs_gstate.accurate_curves ? 2 : 0) +
506
8.68k
                (cldev->gs_gstate.stroke_adjust ? 1 : 0);
507
8.68k
        }
508
251k
        if (unknown & flatness_known) {
509
14.2k
            memcpy(bp, &cldev->gs_gstate.flatness, sizeof(float));
510
14.2k
            bp += sizeof(float);
511
14.2k
        }
512
251k
        if (unknown & line_width_known) {
513
84.9k
            float width =
514
84.9k
                gx_current_line_width(&cldev->gs_gstate.line_params);
515
516
84.9k
            memcpy(bp, &width, sizeof(width));
517
84.9k
            bp += sizeof(width);
518
84.9k
        }
519
251k
        if (unknown & miter_limit_known) {
520
2.39k
            memcpy(bp, &cldev->gs_gstate.line_params.miter_limit,
521
2.39k
                   sizeof(float));
522
2.39k
            bp += sizeof(float);
523
2.39k
        }
524
251k
        if (unknown & op_bm_tk_known) {
525
117k
            *bp++ =
526
117k
                ((int)cldev->gs_gstate.blend_mode << 3) +
527
117k
                cldev->gs_gstate.text_knockout;
528
117k
            *bp++ =
529
117k
                (cldev->gs_gstate.overprint_mode << 2) +
530
117k
                (cldev->gs_gstate.stroke_overprint << 1) +
531
117k
                cldev->gs_gstate.overprint;
532
117k
            *bp++ = cldev->gs_gstate.renderingintent;
533
117k
        }
534
251k
        if (unknown & ais_known) {
535
6.62k
            memcpy(bp, &cldev->gs_gstate.alphaisshape,
536
6.62k
                sizeof(cldev->gs_gstate.alphaisshape));
537
6.62k
            bp += sizeof(cldev->gs_gstate.alphaisshape);
538
6.62k
        }
539
251k
        if (unknown & stroke_alpha_known) {
540
94.8k
            memcpy(bp, &cldev->gs_gstate.strokeconstantalpha, sizeof(float));
541
94.8k
            bp += sizeof(float);
542
94.8k
        }
543
251k
        if (unknown & fill_alpha_known) {
544
95.0k
            memcpy(bp, &cldev->gs_gstate.fillconstantalpha, sizeof(float));
545
95.0k
            bp += sizeof(float);
546
95.0k
        }
547
251k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_misc2,
548
251k
                              1 + cmd_sizew(misc2_unknown) + (bp - buf));
549
251k
        if (code < 0)
550
0
            return 0;
551
251k
        memcpy(cmd_put_w(misc2_unknown, dp + 1), buf, bp - buf);
552
251k
        pcls->known |= misc2_unknown;
553
251k
    }
554
599k
    if (unknown & fill_adjust_known) {
555
9.50k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_fill_adjust,
556
9.50k
                              1 + sizeof(fixed) * 2);
557
9.50k
        if (code < 0)
558
0
            return code;
559
9.50k
        memcpy(dp + 1, &cldev->gs_gstate.fill_adjust.x, sizeof(fixed));
560
9.50k
        memcpy(dp + 1 + sizeof(fixed), &cldev->gs_gstate.fill_adjust.y, sizeof(fixed));
561
9.50k
        pcls->known |= fill_adjust_known;
562
9.50k
    }
563
599k
    if (unknown & ctm_known) {
564
319k
        int len = cmd_write_ctm_return_length(cldev, &ctm_only(&cldev->gs_gstate));
565
566
319k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_ctm, len + 1);
567
319k
        if (code < 0)
568
0
            return code;
569
319k
        code = cmd_write_ctm(&ctm_only(&cldev->gs_gstate), dp, len);
570
319k
        if (code < 0)
571
0
            return code;
572
319k
        pcls->known |= ctm_known;
573
319k
    }
574
599k
    if (unknown & dash_known) {
575
11.0k
        int n = cldev->gs_gstate.line_params.dash.pattern_size;
576
577
11.0k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_dash,
578
11.0k
                              2 + (n + 2) * sizeof(float));
579
11.0k
        if (code < 0)
580
0
            return code;
581
11.0k
        dp[1] = n + (cldev->gs_gstate.line_params.dash.adapt ? 0x80 : 0) +
582
11.0k
            (cldev->gs_gstate.line_params.dot_length_absolute ? 0x40 : 0);
583
11.0k
        memcpy(dp + 2, &cldev->gs_gstate.line_params.dot_length,
584
11.0k
               sizeof(float));
585
11.0k
        memcpy(dp + 2 + sizeof(float),
586
11.0k
               &cldev->gs_gstate.line_params.dash.offset,
587
11.0k
               sizeof(float));
588
11.0k
        if (n != 0)
589
8.56k
            memcpy(dp + 2 + sizeof(float) * 2,
590
8.56k
                   cldev->dash_pattern, n * sizeof(float));
591
11.0k
        pcls->known |= dash_known;
592
11.0k
    }
593
599k
    if (unknown & clip_path_known) {
594
        /*
595
         * We can write out the clipping path either as rectangles
596
         * or as a real (filled) path.
597
         */
598
394k
        const gx_clip_path *pcpath = cldev->clip_path;
599
394k
        int band_height = cldev->page_info.band_params.BandHeight;
600
394k
        int ymin = (pcls - cldev->states) * band_height;
601
394k
        int ymax = min(ymin + band_height, cldev->height);
602
394k
        gs_fixed_rect box;
603
394k
        int code;
604
394k
        int fill_adjust_size;
605
394k
        enum {
606
394k
            write_path_as_rect = 0,
607
394k
            write_path_as_rects = 1,
608
394k
            write_path_as_outer_box = 2,
609
394k
            write_path_as_path = 3
610
394k
        } method;
611
612
        /* We are going to begin_clip followed by the fill_adjust to use.
613
         * In order to know what fill_adjust to use, we need to know whether
614
         * we are going to send the clip through based upon its actual
615
         * 'path' entry, or whether we are going to send it based upon its
616
         * rectangle list representation. Accordingly, we have to do the
617
         * logic to figure out how we are going to send it now. */
618
394k
        if (pcpath->path_valid) {
619
90.8k
            if (gx_path_is_rectangle(&pcpath->path, &box) &&
620
90.8k
                fixed_is_int(box.p.x | box.p.y | box.q.x | box.q.y))
621
4.45k
                method = write_path_as_rect;
622
86.3k
            else if ( !(cldev->disable_mask & clist_disable_complex_clip) )
623
86.3k
                method = write_path_as_path;
624
0
            else
625
0
                method = write_path_as_outer_box;
626
303k
        } else {
627
303k
            const gx_clip_list *list = gx_cpath_list(pcpath);
628
303k
            const gx_clip_rect *prect = list->head;
629
630
303k
            if (prect != NULL &&
631
303k
                cldev->disable_mask & clist_disable_complex_clip)
632
0
                method = write_path_as_outer_box;
633
303k
            else
634
303k
                method = write_path_as_rects;
635
303k
        }
636
637
        /* And thus how large the fill_adjust values will be. */
638
394k
        if (method == write_path_as_path)
639
86.3k
            fill_adjust_size = cmd_size2w(pcpath->path_fill_adjust.x,
640
394k
                                          pcpath->path_fill_adjust.y);
641
307k
        else
642
307k
            fill_adjust_size = cmd_size2w(0, 0);
643
644
        /* Send the 'begin_clip' with the fill_adjust values. */
645
394k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_begin_clip, 1+fill_adjust_size);
646
394k
        if (code < 0)
647
0
            return code;
648
394k
        dp++;
649
394k
        if (method == write_path_as_path)
650
86.3k
            cmd_put2w(pcpath->path_fill_adjust.x,
651
394k
                      pcpath->path_fill_adjust.y, &dp);
652
307k
        else
653
307k
            cmd_put2w(0, 0, &dp);
654
655
        /* Then send the actual clip path representation. */
656
394k
        switch (method)
657
394k
        {
658
4.45k
        case write_path_as_rect:
659
            /* Write the path as a rectangle. */
660
4.45k
            code = cmd_write_rect_cmd(cldev, pcls, cmd_op_fill_rect,
661
4.45k
                                      fixed2int_var(box.p.x),
662
4.45k
                                      fixed2int_var(box.p.y),
663
4.45k
                                      fixed2int(box.q.x - box.p.x),
664
4.45k
                                      fixed2int(box.q.y - box.p.y));
665
4.45k
            break;
666
86.3k
        case write_path_as_path:
667
            /* Write the path. */
668
86.3k
            code = cmd_put_path(cldev, pcls, &pcpath->path,
669
86.3k
                                int2fixed(ymin - 1),
670
86.3k
                                int2fixed(ymax + 1),
671
86.3k
                                (byte)(pcpath->rule == gx_rule_even_odd ?
672
83.3k
                                 cmd_opv_eofill : cmd_opv_fill),
673
86.3k
                                true, sn_not_first);
674
86.3k
            break;
675
303k
        case write_path_as_rects:
676
303k
        {
677
            /* Write out the rectangles. */
678
303k
            const gx_clip_list *list = gx_cpath_list(pcpath);
679
303k
            const gx_clip_rect *prect = list->head;
680
681
303k
            if (prect == 0)
682
298k
                prect = &list->single;
683
1.43M
            for (; prect != 0 && code >= 0; prect = prect->next) {
684
1.13M
                if (prect->xmax > prect->xmin &&
685
1.13M
                    prect->ymin < ymax && prect->ymax > ymin) {
686
295k
                    code = cmd_write_rect_cmd(cldev, pcls, cmd_op_fill_rect,
687
295k
                                              prect->xmin, prect->ymin,
688
295k
                                              prect->xmax - prect->xmin,
689
295k
                                              prect->ymax - prect->ymin);
690
295k
                }
691
1.13M
            }
692
303k
            break;
693
0
        }
694
0
        default:
695
0
        {
696
            /* Clip is complex, but disabled. Write out the outer box */
697
0
            gs_fixed_rect box;
698
699
0
            gx_cpath_outer_box(pcpath, &box);
700
0
            box.p.x = fixed_floor(box.p.x);
701
0
            box.p.y = fixed_floor(box.p.y);
702
0
            code = cmd_write_rect_cmd(cldev, pcls, cmd_op_fill_rect,
703
0
                                      fixed2int_var(box.p.x),
704
0
                                      fixed2int_var(box.p.y),
705
0
                                      fixed2int_ceiling(box.q.x - box.p.x),
706
0
                                      fixed2int_ceiling(box.q.y - box.p.y));
707
0
        }
708
394k
        }
709
710
        /* And now we can send 'end_clip' so the reader can finalise everything. */
711
394k
        {
712
394k
            int end_code =
713
394k
                set_cmd_put_op(&dp, cldev, pcls, cmd_opv_end_clip, 1);
714
715
394k
            if (code >= 0)
716
394k
                code = end_code; /* take the first failure seen */
717
394k
            if (end_code < 0) {
718
                /*
719
                 * end_clip has to work despite lo-mem to maintain consistency.
720
                 * This isn't error recovery, but just to prevent dangling
721
                 * cmd_opv_begin_clip's.
722
                 */
723
0
                ++cldev->ignore_lo_mem_warnings;
724
0
                end_code =
725
0
                    set_cmd_put_op(&dp, cldev, pcls, cmd_opv_end_clip, 1);
726
0
                --cldev->ignore_lo_mem_warnings;
727
0
            }
728
394k
        }
729
394k
        if (code < 0)
730
0
            return code;
731
394k
        pcls->clip_enabled = 1;
732
394k
        pcls->known |= clip_path_known;
733
394k
    }
734
599k
    if (unknown & color_space_known) {
735
52.5k
        byte *dp;
736
737
52.5k
        if (cldev->color_space.byte1 & 8) { /* indexed */
738
1.37k
            const gs_color_space *pcs = cldev->color_space.space;
739
1.37k
            int hival = pcs->params.indexed.hival;
740
1.37k
            uint num_values = (hival + 1) *
741
1.37k
                gs_color_space_num_components(pcs->base_space);
742
1.37k
            bool use_proc = cldev->color_space.byte1 & 4;
743
1.37k
            const void *map_data;
744
1.37k
            uint map_size;
745
746
1.37k
            if (use_proc) {
747
0
                map_data = pcs->params.indexed.lookup.map->values;
748
0
                map_size = num_values *
749
0
                    sizeof(pcs->params.indexed.lookup.map->values[0]);
750
1.37k
            } else {
751
1.37k
                map_data = pcs->params.indexed.lookup.table.data;
752
1.37k
                map_size = num_values;
753
1.37k
            }
754
1.37k
            code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_color_space,
755
1.37k
                                  2 + cmd_sizew(hival) + map_size +
756
1.37k
                                  sizeof(clist_icc_color_t));
757
1.37k
            if (code < 0)
758
0
                return code;
759
            /* Save the ICC information */
760
1.37k
            memcpy(dp + 2, &(cldev->color_space.icc_info),
761
1.37k
                   sizeof(clist_icc_color_t));
762
1.37k
            memcpy(cmd_put_w(hival, dp + 2 +
763
1.37k
                   sizeof(clist_icc_color_t)), map_data, map_size);
764
51.2k
        } else {
765
51.2k
            code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_color_space,
766
51.2k
                2 + sizeof(clist_icc_color_t));
767
51.2k
            if (code < 0)
768
0
                return code;
769
51.2k
            memcpy(dp + 2, &(cldev->color_space.icc_info),
770
51.2k
                   sizeof(clist_icc_color_t));
771
51.2k
        }
772
52.5k
        dp[1] = cldev->color_space.byte1;
773
52.5k
        pcls->known |= color_space_known;
774
52.5k
    }
775
    /****** HANDLE masks ******/
776
599k
    return 0;
777
599k
}
778
779
/* ------ Driver procedures ------ */
780
781
int
782
clist_fill_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath,
783
            const gx_fill_params * params, const gx_drawing_color * pdcolor,
784
                const gx_clip_path * pcpath)
785
841k
{
786
841k
    gx_device_clist_writer * const cdev =
787
841k
        &((gx_device_clist *)dev)->writer;
788
841k
    uint unknown = 0;
789
841k
    int ry, rheight, rx, rwidth, y0, y1;
790
841k
    gs_logical_operation_t lop = pgs->log_op;
791
841k
    byte op = (byte)
792
841k
        (params->rule == gx_rule_even_odd ?
793
733k
         cmd_opv_eofill : cmd_opv_fill);
794
841k
    gs_fixed_point adjust;
795
841k
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
796
841k
    cmd_rects_enum_t re;
797
841k
    int code;
798
799
841k
    adjust = params->adjust;
800
841k
    {
801
841k
        gs_fixed_rect bbox;
802
803
841k
        if (ppath != NULL)
804
841k
            gx_path_bbox(ppath, &bbox);
805
286
        else {
806
            /* gx_default_fill_path passes the clip path for shfill. */
807
286
            gx_cpath_outer_box(pcpath, &bbox);
808
286
        }
809
841k
        ry = fixed2int(bbox.p.y) - 1;
810
841k
        rheight = fixed2int_ceiling(bbox.q.y) - ry + 1;
811
841k
        crop_fill_y(cdev, ry, rheight);
812
841k
        if (rheight <= 0)
813
157k
            return 0;
814
683k
        if (rheight > cdev->height)
815
0
            return gs_error_limitcheck;
816
683k
        rx = fixed2int(bbox.p.x) - 1;
817
683k
        rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
818
683k
        fit_fill_w(cdev, rx, rwidth);
819
683k
    }
820
683k
    if ( (cdev->disable_mask & clist_disable_fill_path) ||
821
683k
         gs_debug_c(',')
822
683k
         ) {
823
        /* Disable path-based banding. */
824
0
        return gx_default_fill_path(dev, pgs, ppath, params, pdcolor,
825
0
                                    pcpath);
826
0
    }
827
683k
    if (pdcolor != NULL && gx_dc_is_pattern2_color(pdcolor)) {
828
        /* Here we need to intersect *ppath, *pcpath and shading bbox.
829
           Call the default implementation, which has a special
830
           branch for processing a shading fill with the clip writer device.
831
           It will call us back with pdcolor=NULL for passing
832
           the intersected clipping path,
833
           and then will decompose the shading into trapezoids.
834
           See comment below about pdcolor == NULL.
835
         */
836
255
        cdev->cropping_saved = false;
837
255
        code = gx_default_fill_path(dev, pgs, ppath, params, pdcolor, pcpath);
838
255
        if (cdev->cropping_saved) {
839
255
            cdev->cropping_min = cdev->save_cropping_min;
840
255
            cdev->cropping_max = cdev->save_cropping_max;
841
255
            if_debug2m('v', cdev->memory,
842
255
                       "[v] clist_fill_path: restore cropping_min=%d croping_max=%d\n",
843
255
                       cdev->save_cropping_min, cdev->save_cropping_max);
844
255
        }
845
255
        return code;
846
255
    }
847
683k
    y0 = ry;
848
683k
    y1 = ry + rheight;
849
683k
    cmd_check_fill_known(cdev, pgs, params->flatness, &adjust, pcpath,
850
683k
                         &unknown);
851
683k
    if (unknown)
852
20.3k
        cmd_clear_known(cdev, unknown);
853
683k
    if (cdev->permanent_error < 0)
854
0
        return (cdev->permanent_error);
855
683k
    if (pdcolor == NULL) {
856
        /* See comment above about pattern2_color.
857
           Put the clipping path only.
858
           The graphics library will call us again with subdividing
859
           the shading into trapezoids and rectangles.
860
           Narrow cropping_min, croping_max for such calls. */
861
255
        cdev->cropping_saved = true;
862
255
        cdev->save_cropping_min = cdev->cropping_min;
863
255
        cdev->save_cropping_max = cdev->cropping_max;
864
255
        cdev->cropping_min = max(ry, cdev->cropping_min);
865
255
        cdev->cropping_max = min(ry + rheight, cdev->cropping_max);
866
255
        if_debug2m('v', cdev->memory,
867
255
                   "[v] clist_fill_path: narrow cropping_min=%d croping_max=%d\n",
868
255
                   cdev->save_cropping_min, cdev->save_cropping_max);
869
255
        RECT_ENUM_INIT(re, ry, rheight);
870
625
        do {
871
625
            RECT_STEP_INIT(re);
872
625
            if (pcpath != NULL) {
873
625
                code = cmd_do_write_unknown(cdev, re.pcls, clip_path_known);
874
625
                if (code < 0)
875
0
                    return code;
876
625
            }
877
625
            code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL);
878
625
            if (code  < 0)
879
0
                return code;
880
625
            re.y += re.height;
881
625
        } while (re.y < re.yend);
882
683k
    } else {
883
        /* We should not reach here with ppath==NULL (pdcolor != NULL, so not a shading fill */
884
683k
        if (ppath == NULL)
885
0
            return_error(gs_error_unregistered);
886
887
        /* If needed, update the trans_bbox */
888
683k
        if (cdev->pdf14_needed) {
889
26.2k
            gs_int_rect bbox;
890
891
26.2k
            bbox.p.x = rx;
892
26.2k
            bbox.q.x = rx + rwidth - 1;
893
26.2k
            bbox.p.y = ry;
894
26.2k
            bbox.q.y = ry + rheight - 1;
895
896
26.2k
            clist_update_trans_bbox(cdev, &bbox);
897
26.2k
        }
898
899
683k
        RECT_ENUM_INIT(re, ry, rheight);
900
1.40M
        do {
901
1.40M
            RECT_STEP_INIT(re);
902
1.40M
            code = cmd_do_write_unknown(cdev, re.pcls, FILL_KNOWN);
903
1.40M
            if (code < 0)
904
0
                return code;
905
1.40M
            if ((code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0 ||
906
1.40M
                (code = cmd_update_lop(cdev, re.pcls, lop)) < 0
907
1.40M
                )
908
0
                return code;
909
1.40M
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re,
910
1.40M
                                         devn_not_tile_fill);
911
1.40M
            if (code == gs_error_unregistered)
912
0
                return code;
913
1.40M
            if (code < 0) {
914
                /* Something went wrong, use the default implementation. */
915
14
                return gx_default_fill_path(dev, pgs, ppath, params, pdcolor,
916
14
                                            pcpath);
917
14
            }
918
1.40M
            re.pcls->color_usage.slow_rop |= slow_rop;
919
1.40M
            code = cmd_put_path(cdev, re.pcls, ppath,
920
1.40M
                                int2fixed(max(re.y - 1, y0)),
921
1.40M
                                int2fixed(min(re.y + re.height + 1, y1)),
922
1.40M
                                op,
923
1.40M
                                true, sn_none /* fill doesn't need the notes */ );
924
1.40M
            if (code < 0)
925
0
                return code;
926
1.40M
            re.y += re.height;
927
1.40M
        } while (re.y < re.yend);
928
683k
    }
929
683k
    return 0;
930
683k
}
931
932
int clist_lock_pattern(gx_device * pdev, gs_gstate * pgs, gs_id pattern, int lock)
933
46
{
934
46
    gx_device_clist_writer * const cdev =
935
46
        &((gx_device_clist *)pdev)->writer;
936
46
    byte *dp;
937
46
    int code;
938
939
    /* We need to both lock now, and ensure that we lock on reading this back. */
940
46
    code = gx_pattern_cache_entry_set_lock(pgs, pattern, lock);
941
46
    if (code < 0)
942
0
        return code;
943
944
46
    code = set_cmd_put_all_op(&dp, cdev, cmd_opv_lock_pattern,
945
46
                              1 + 1 + sizeof(pattern));
946
947
46
    if (code < 0)
948
0
        return code;
949
46
    dp[1] = lock;
950
46
    memcpy(dp+2, &pattern, sizeof(pattern));
951
46
    return 0;
952
46
}
953
954
int
955
clist_fill_stroke_path(gx_device * pdev, const gs_gstate * pgs,
956
                            gx_path * ppath,
957
                            const gx_fill_params * params_fill,
958
                            const gx_device_color * pdevc_fill,
959
                            const gx_stroke_params * params_stroke,
960
                            const gx_device_color * pdevc_stroke,
961
                            const gx_clip_path * pcpath)
962
3.84k
{
963
3.84k
    gx_device_clist_writer * const cdev =
964
3.84k
        &((gx_device_clist *)pdev)->writer;
965
3.84k
    int pattern_size = pgs->line_params.dash.pattern_size;
966
3.84k
    byte op = (byte) (params_fill->rule == gx_rule_even_odd ?
967
3.65k
                  cmd_opv_eofill_stroke : cmd_opv_fill_stroke);
968
3.84k
    uint unknown = 0;
969
3.84k
    gs_fixed_rect bbox;
970
3.84k
    gs_fixed_point expansion;
971
3.84k
    int adjust_y, expansion_code;
972
3.84k
    int ry, rheight;
973
3.84k
    gs_logical_operation_t lop = pgs->log_op;
974
3.84k
    bool slow_rop = cmd_slow_rop(pdev, lop_know_S_0(lop), pdevc_fill);
975
3.84k
    cmd_rects_enum_t re;
976
977
3.84k
    if (pdevc_stroke == NULL || pdevc_fill == NULL)
978
0
        return_error(gs_error_unknownerror);  /* shouldn't happen */
979
980
3.84k
    if ((cdev->disable_mask & (clist_disable_fill_path | clist_disable_stroke_path)) ||
981
3.84k
        gs_debug_c(',')
982
3.84k
        ) {
983
        /* Disable path-based banding. */
984
0
        return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
985
0
                                           params_stroke, pdevc_stroke, pcpath);
986
0
    }
987
    /* TODO: For now punt to default if we have shaded color (pattern2) */
988
3.84k
    if (gx_dc_is_pattern2_color(pdevc_fill) || gx_dc_is_pattern2_color(pdevc_stroke)) {
989
0
        return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
990
0
                                           params_stroke, pdevc_stroke, pcpath);
991
0
    }
992
3.84k
    gx_path_bbox(ppath, &bbox);
993
    /* We must use the supplied gs_gstate, not our saved one, */
994
    /* for computing the stroke expansion. */
995
3.84k
    expansion_code = gx_stroke_path_expansion(pgs, ppath, &expansion);
996
3.84k
    if (expansion_code < 0) {
997
        /* Expansion is too large: use the entire page. */
998
26
        adjust_y = 0;
999
26
        ry = 0;
1000
26
        rheight = pdev->height;
1001
3.81k
    } else {
1002
3.81k
        adjust_y = fixed2int_ceiling(expansion.y) + 1;
1003
3.81k
        ry = fixed2int(bbox.p.y) - adjust_y;
1004
3.81k
        rheight = fixed2int_ceiling(bbox.q.y) - ry + adjust_y;
1005
3.81k
        fit_fill_y(pdev, ry, rheight);
1006
3.81k
        fit_fill_h(pdev, ry, rheight);
1007
3.81k
        if (rheight <= 0)
1008
44
            return 0;
1009
3.81k
    }
1010
    /* Check the dash pattern, since we bail out if */
1011
    /* the pattern is too large. */
1012
3.80k
    if (cdev->gs_gstate.line_params.dash.pattern_size != pattern_size ||
1013
3.80k
        (pattern_size != 0 &&
1014
3.79k
         memcmp(cdev->dash_pattern, pgs->line_params.dash.pattern,
1015
0
                pattern_size * sizeof(float))) ||
1016
3.80k
        cdev->gs_gstate.line_params.dash.offset !=
1017
3.79k
          pgs->line_params.dash.offset ||
1018
3.80k
        cdev->gs_gstate.line_params.dash.adapt !=
1019
3.79k
          pgs->line_params.dash.adapt ||
1020
3.80k
        cdev->gs_gstate.line_params.dot_length !=
1021
3.79k
          pgs->line_params.dot_length ||
1022
3.80k
        cdev->gs_gstate.line_params.dot_length_absolute !=
1023
3.79k
          pgs->line_params.dot_length_absolute
1024
3.80k
    ) {
1025
        /* Bail out if the dash pattern is too long. */
1026
10
        if (pattern_size > cmd_max_dash)
1027
0
            return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
1028
0
                                               params_stroke, pdevc_stroke, pcpath);
1029
10
        unknown |= dash_known;
1030
        /*
1031
         * Temporarily reset the dash pattern pointer for gx_set_dash,
1032
         * but don't leave it set, since that would confuse the GC.
1033
         */
1034
10
        cdev->gs_gstate.line_params.dash.pattern = cdev->dash_pattern;
1035
10
        gx_set_dash(&cdev->gs_gstate.line_params.dash,
1036
10
                    pgs->line_params.dash.pattern,
1037
10
                    pgs->line_params.dash.pattern_size,
1038
10
                    pgs->line_params.dash.offset, NULL);
1039
10
        cdev->gs_gstate.line_params.dash.pattern = 0;
1040
10
        gx_set_dash_adapt(&cdev->gs_gstate.line_params.dash,
1041
10
                          pgs->line_params.dash.adapt);
1042
10
        gx_set_dot_length(&cdev->gs_gstate.line_params,
1043
10
                          pgs->line_params.dot_length,
1044
10
                          pgs->line_params.dot_length_absolute);
1045
10
    }
1046
1047
3.80k
    if (state_neq(line_params.start_cap) || state_neq(line_params.join) ||
1048
3.80k
        state_neq(line_params.end_cap) || state_neq(line_params.dash_cap)) {
1049
276
        unknown |= cap_join_known;
1050
276
        state_update(line_params.start_cap);
1051
276
        state_update(line_params.end_cap);
1052
276
        state_update(line_params.dash_cap);
1053
276
        state_update(line_params.join);
1054
276
    }
1055
3.80k
    cmd_check_fill_known(cdev, pgs, params_fill->flatness, &pgs->fill_adjust,
1056
3.80k
                         pcpath, &unknown);
1057
3.80k
    if (state_neq(line_params.half_width)) {
1058
595
        unknown |= line_width_known;
1059
595
        state_update(line_params.half_width);
1060
595
    }
1061
3.80k
    if (state_neq(line_params.miter_limit)) {
1062
33
        unknown |= miter_limit_known;
1063
33
        gx_set_miter_limit(&cdev->gs_gstate.line_params,
1064
33
                           pgs->line_params.miter_limit);
1065
33
    }
1066
3.80k
    if (state_neq(ctm.xx) || state_neq(ctm.xy) ||
1067
3.80k
        state_neq(ctm.yx) || state_neq(ctm.yy) ||
1068
    /* We don't actually need tx or ty, but we don't want to bother */
1069
    /* tracking them separately from the other coefficients. */
1070
3.80k
        state_neq(ctm.tx) || state_neq(ctm.ty)
1071
3.80k
        ) {
1072
864
        unknown |= ctm_known;
1073
864
        state_update(ctm);
1074
864
    }
1075
3.80k
    if (unknown)
1076
971
        cmd_clear_known(cdev, unknown);
1077
3.80k
    if (cdev->permanent_error < 0)
1078
0
      return (cdev->permanent_error);
1079
    /* If needed, update the trans_bbox */
1080
3.80k
    if (cdev->pdf14_needed) {
1081
1.21k
        gs_int_rect trans_bbox;
1082
1.21k
        int rx = fixed2int(bbox.p.x) - 1;
1083
1.21k
        int rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
1084
1.21k
        unknown |= STROKE_ALL_KNOWN;
1085
1086
1.21k
        fit_fill_w(cdev, rx, rwidth);
1087
1.21k
        trans_bbox.p.x = rx;
1088
1.21k
        trans_bbox.q.x = rx + rwidth - 1;
1089
1.21k
        trans_bbox.p.y = ry;
1090
1.21k
        trans_bbox.q.y = ry + rheight - 1;
1091
1092
1.21k
        clist_update_trans_bbox(cdev, &trans_bbox);
1093
1.21k
    }
1094
    /* If either fill or stroke uses overprint, or overprint_mode != 0, then we */
1095
    /* need to write out the overprint drawn_comps and retain_*     */
1096
3.80k
    if (((pgs->overprint_mode || pgs->overprint || pgs->stroke_overprint))) {
1097
657
        unknown |= op_bm_tk_known;
1098
657
    }
1099
3.80k
    RECT_ENUM_INIT(re, ry, rheight);
1100
17.1k
    do {
1101
17.1k
        int code;
1102
1103
17.1k
        RECT_STEP_INIT(re);
1104
17.1k
        if ((code = cmd_do_write_unknown(cdev, re.pcls, STROKE_ALL_KNOWN | FILL_KNOWN)) < 0)
1105
0
            return code;
1106
17.1k
        if ((code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0)
1107
0
            return code;
1108
17.1k
        if ((code = cmd_update_lop(cdev, re.pcls, lop)) < 0)
1109
0
            return code;
1110
        /* Write the stroke first since do_fill_stroke will have locked the pattern */
1111
        /* tile if needed, and we want it locked after reading the stroke color.  */
1112
17.1k
        code = cmd_put_drawing_color(cdev, re.pcls, pdevc_stroke, &re, devn_not_tile_stroke);
1113
17.1k
        if (code < 0) {
1114
            /* Something went wrong, use the default implementation. */
1115
0
            return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
1116
0
                params_stroke, pdevc_stroke, pcpath);
1117
0
        }
1118
17.1k
        code = cmd_put_drawing_color(cdev, re.pcls, pdevc_fill, &re, devn_not_tile_fill);
1119
17.1k
        if (code < 0) {
1120
            /* Something went wrong, use the default implementation. */
1121
10
            return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
1122
10
                                               params_stroke, pdevc_stroke, pcpath);
1123
10
        }
1124
17.1k
        re.pcls->color_usage.slow_rop |= slow_rop;
1125
1126
        /* Don't skip segments when expansion is unknown.  */
1127
1128
17.1k
        code = cmd_put_path(cdev, re.pcls, ppath, min_fixed, max_fixed,
1129
17.1k
                            op, false, (segment_notes)~0);
1130
17.1k
        if (code < 0)
1131
0
            return code;
1132
17.1k
        re.y += re.height;
1133
17.1k
    } while (re.y < re.yend);
1134
3.79k
    return 0;
1135
3.80k
}
1136
1137
int
1138
clist_stroke_path(gx_device * dev, const gs_gstate * pgs, gx_path * ppath,
1139
                  const gx_stroke_params * params,
1140
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
1141
286k
{
1142
286k
    gx_device_clist_writer * const cdev =
1143
286k
        &((gx_device_clist *)dev)->writer;
1144
286k
    int pattern_size = pgs->line_params.dash.pattern_size;
1145
286k
    uint unknown = 0;
1146
286k
    gs_fixed_rect bbox;
1147
286k
    gs_fixed_point expansion;
1148
286k
    int adjust_y, expansion_code;
1149
286k
    int ry, rheight;
1150
286k
    gs_logical_operation_t lop = pgs->log_op;
1151
286k
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
1152
286k
    cmd_rects_enum_t re;
1153
1154
286k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1155
286k
    if ((cdev->disable_mask & clist_disable_stroke_path) ||
1156
286k
        gs_debug_c(',')
1157
286k
        ) {
1158
        /* Disable path-based banding. */
1159
0
        return gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
1160
0
                                      pcpath);
1161
0
    }
1162
286k
    gx_path_bbox(ppath, &bbox);
1163
    /* We must use the supplied gs_gstate, not our saved one, */
1164
    /* for computing the stroke expansion. */
1165
286k
    expansion_code = gx_stroke_path_expansion(pgs, ppath, &expansion);
1166
286k
    if (expansion_code < 0) {
1167
        /* Expansion is too large: use the entire page. */
1168
10.0k
        adjust_y = 0;
1169
10.0k
        ry = 0;
1170
10.0k
        rheight = dev->height;
1171
276k
    } else {
1172
276k
        adjust_y = fixed2int_ceiling(expansion.y) + 1;
1173
276k
        ry = fixed2int(bbox.p.y) - adjust_y;
1174
276k
        rheight = fixed2int_ceiling(bbox.q.y) - ry + adjust_y;
1175
276k
        fit_fill_y(dev, ry, rheight);
1176
276k
        fit_fill_h(dev, ry, rheight);
1177
276k
        if (rheight <= 0)
1178
11.5k
            return 0;
1179
276k
    }
1180
    /* Check the dash pattern, since we bail out if */
1181
    /* the pattern is too large. */
1182
274k
    if (cdev->gs_gstate.line_params.dash.pattern_size != pattern_size ||
1183
274k
        (pattern_size != 0 &&
1184
273k
         memcmp(cdev->dash_pattern, pgs->line_params.dash.pattern,
1185
683
                pattern_size * sizeof(float))) ||
1186
274k
        cdev->gs_gstate.line_params.dash.offset !=
1187
273k
          pgs->line_params.dash.offset ||
1188
274k
        cdev->gs_gstate.line_params.dash.adapt !=
1189
273k
          pgs->line_params.dash.adapt ||
1190
274k
        cdev->gs_gstate.line_params.dot_length !=
1191
273k
          pgs->line_params.dot_length ||
1192
274k
        cdev->gs_gstate.line_params.dot_length_absolute !=
1193
273k
          pgs->line_params.dot_length_absolute
1194
274k
    ) {
1195
        /* Bail out if the dash pattern is too long. */
1196
1.29k
        if (pattern_size > cmd_max_dash)
1197
0
            return gx_default_stroke_path(dev, pgs, ppath, params,
1198
0
                                          pdcolor, pcpath);
1199
1.29k
        unknown |= dash_known;
1200
        /*
1201
         * Temporarily reset the dash pattern pointer for gx_set_dash,
1202
         * but don't leave it set, since that would confuse the GC.
1203
         */
1204
1.29k
        cdev->gs_gstate.line_params.dash.pattern = cdev->dash_pattern;
1205
1.29k
        gx_set_dash(&cdev->gs_gstate.line_params.dash,
1206
1.29k
                    pgs->line_params.dash.pattern,
1207
1.29k
                    pgs->line_params.dash.pattern_size,
1208
1.29k
                    pgs->line_params.dash.offset, NULL);
1209
1.29k
        cdev->gs_gstate.line_params.dash.pattern = 0;
1210
1.29k
        gx_set_dash_adapt(&cdev->gs_gstate.line_params.dash,
1211
1.29k
                          pgs->line_params.dash.adapt);
1212
1.29k
        gx_set_dot_length(&cdev->gs_gstate.line_params,
1213
1.29k
                          pgs->line_params.dot_length,
1214
1.29k
                          pgs->line_params.dot_length_absolute);
1215
1.29k
    }
1216
1217
274k
    if (state_neq(line_params.start_cap) || state_neq(line_params.join) ||
1218
274k
        state_neq(line_params.end_cap) || state_neq(line_params.dash_cap)) {
1219
7.14k
        unknown |= cap_join_known;
1220
7.14k
        state_update(line_params.start_cap);
1221
7.14k
        state_update(line_params.end_cap);
1222
7.14k
        state_update(line_params.dash_cap);
1223
7.14k
        state_update(line_params.join);
1224
7.14k
    }
1225
274k
    cmd_check_fill_known(cdev, pgs, params->flatness, &pgs->fill_adjust,
1226
274k
                         pcpath, &unknown);
1227
274k
    if (state_neq(line_params.half_width)) {
1228
8.59k
        unknown |= line_width_known;
1229
8.59k
        state_update(line_params.half_width);
1230
8.59k
    }
1231
274k
    if (state_neq(line_params.miter_limit)) {
1232
197
        unknown |= miter_limit_known;
1233
197
        gx_set_miter_limit(&cdev->gs_gstate.line_params,
1234
197
                           pgs->line_params.miter_limit);
1235
197
    }
1236
274k
    if (state_neq(ctm.xx) || state_neq(ctm.xy) ||
1237
274k
        state_neq(ctm.yx) || state_neq(ctm.yy) ||
1238
    /* We don't actually need tx or ty, but we don't want to bother */
1239
    /* tracking them separately from the other coefficients. */
1240
274k
        state_neq(ctm.tx) || state_neq(ctm.ty)
1241
274k
        ) {
1242
83.7k
        unknown |= ctm_known;
1243
83.7k
        state_update(ctm);
1244
83.7k
    }
1245
274k
    if (unknown)
1246
93.6k
        cmd_clear_known(cdev, unknown);
1247
274k
    if (cdev->permanent_error < 0)
1248
0
      return (cdev->permanent_error);
1249
    /* If needed, update the trans_bbox */
1250
274k
    if (cdev->pdf14_needed) {
1251
27.0k
        gs_int_rect trans_bbox;
1252
27.0k
        int rx = fixed2int(bbox.p.x) - 1;
1253
27.0k
        int rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
1254
1255
27.0k
        fit_fill_w(cdev, rx, rwidth);
1256
27.0k
        trans_bbox.p.x = rx;
1257
27.0k
        trans_bbox.q.x = rx + rwidth - 1;
1258
27.0k
        trans_bbox.p.y = ry;
1259
27.0k
        trans_bbox.q.y = ry + rheight - 1;
1260
1261
27.0k
        clist_update_trans_bbox(cdev, &trans_bbox);
1262
27.0k
    }
1263
274k
    RECT_ENUM_INIT(re, ry, rheight);
1264
532k
    do {
1265
532k
        int code;
1266
1267
532k
        RECT_STEP_INIT(re);
1268
532k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1269
532k
        if ((code = cmd_do_write_unknown(cdev, re.pcls, STROKE_ALL_KNOWN)) < 0 ||
1270
532k
            (code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0 ||
1271
532k
            (code = cmd_update_lop(cdev, re.pcls, lop)) < 0
1272
532k
            )
1273
0
            return code;
1274
532k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1275
532k
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, devn_not_tile_stroke);
1276
532k
            if (code == gs_error_unregistered)
1277
0
                return code;
1278
532k
        if (code < 0) {
1279
            /* Something went wrong, use the default implementation. */
1280
28
            cdev->cropping_saved = false;
1281
28
            code = gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
1282
28
                                          pcpath);
1283
28
            if (cdev->cropping_saved) {
1284
0
                cdev->cropping_min = cdev->save_cropping_min;
1285
0
                cdev->cropping_max = cdev->save_cropping_max;
1286
0
                if_debug2m('v', cdev->memory,
1287
0
                           "[v] clist_stroke_path: restore cropping_min=%d croping_max=%d\n",
1288
0
                           cdev->save_cropping_min, cdev->save_cropping_max);
1289
0
            }
1290
28
            return code;
1291
28
        }
1292
531k
        re.pcls->color_usage.slow_rop |= slow_rop;
1293
531k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1294
531k
        {
1295
531k
            fixed ymin, ymax;
1296
1297
            /*
1298
             * If a dash pattern is active, we can't skip segments
1299
             * outside the clipping region, because that would throw off
1300
             * the pattern.
1301
             * Don't skip segments when expansion is unknown.
1302
             */
1303
1304
531k
            if (pattern_size || expansion_code < 0 ) {
1305
117k
                ymin = min_fixed;
1306
117k
                ymax = max_fixed;
1307
414k
            } else {
1308
414k
                ymin = int2fixed(re.y - adjust_y);
1309
414k
                ymax = int2fixed(re.y + re.height + adjust_y);
1310
414k
            }
1311
531k
            code = cmd_put_path(cdev, re.pcls, ppath, ymin, ymax,
1312
531k
                                cmd_opv_stroke,
1313
531k
                                false, (segment_notes)~0);
1314
531k
            if (code < 0)
1315
0
                return code;
1316
531k
        }
1317
531k
        re.y += re.height;
1318
531k
    } while (re.y < re.yend);
1319
274k
    return 0;
1320
274k
}
1321
1322
/*
1323
 * Fill_parallelogram and fill_triangle aren't very efficient.  This isn't
1324
 * important right now, since the non-degenerate case is only used for
1325
 * smooth shading.  However, the rectangular case of fill_parallelogram is
1326
 * sometimes used for images, so its performance does matter.
1327
 */
1328
1329
static int
1330
clist_put_polyfill(gx_device *dev, fixed px, fixed py,
1331
                   const gs_fixed_point *points, int num_points,
1332
                   const gx_drawing_color *pdcolor, gs_logical_operation_t lop)
1333
2.40M
{
1334
2.40M
    gx_path path;
1335
2.40M
    gs_memory_t *mem = dev->memory;
1336
2.40M
    int code;
1337
2.40M
    gx_device_clist_writer * const cdev =
1338
2.40M
        &((gx_device_clist *)dev)->writer;
1339
2.40M
    gs_fixed_rect bbox;
1340
2.40M
    int ry, rheight, y0, y1;
1341
2.40M
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
1342
2.40M
    cmd_rects_enum_t re;
1343
1344
2.40M
    if (gs_debug_c(','))
1345
0
        return -1;   /* path-based banding is disabled */
1346
2.40M
    gx_path_init_local(&path, mem);
1347
2.40M
    if ((code = gx_path_add_point(&path, px, py)) < 0 ||
1348
2.40M
        (code = gx_path_add_lines(&path, points, num_points)) < 0
1349
2.40M
        )
1350
0
        goto out;
1351
2.40M
    gx_path_bbox(&path, &bbox);
1352
2.40M
    ry = fixed2int(bbox.p.y) - 1;
1353
2.40M
    rheight = fixed2int_ceiling(bbox.q.y) - ry + 1;
1354
2.40M
    fit_fill_y(dev, ry, rheight);
1355
2.40M
    fit_fill_h(dev, ry, rheight);
1356
2.40M
    if (rheight <= 0)
1357
2.27M
        return 0;
1358
137k
    y0 = ry;
1359
137k
    y1 = ry + rheight;
1360
137k
    if (cdev->permanent_error < 0)
1361
0
      return (cdev->permanent_error);
1362
    /* If needed, update the trans_bbox */
1363
137k
    if (cdev->pdf14_needed) {
1364
200
        gs_int_rect trans_bbox;
1365
200
        int rx = fixed2int(bbox.p.x) - 1;
1366
200
        int rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
1367
1368
200
        fit_fill_w(cdev, rx, rwidth);
1369
200
        trans_bbox.p.x = rx;
1370
200
        trans_bbox.q.x = rx + rwidth - 1;
1371
200
        trans_bbox.p.y = ry;
1372
200
        trans_bbox.q.y = ry + rheight - 1;
1373
1374
200
        clist_update_trans_bbox(cdev, &trans_bbox);
1375
200
    }
1376
137k
    RECT_ENUM_INIT(re, ry, rheight);
1377
139k
    do {
1378
139k
        RECT_STEP_INIT(re);
1379
139k
        if ((code = cmd_update_lop(cdev, re.pcls, lop)) < 0 ||
1380
139k
            (code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, devn_not_tile_fill)) < 0)
1381
0
            goto out;
1382
139k
        re.pcls->color_usage.slow_rop |= slow_rop;
1383
139k
        code = cmd_put_path(cdev, re.pcls, &path,
1384
139k
                            int2fixed(max(re.y - 1, y0)),
1385
139k
                            int2fixed(min(re.y + re.height + 1, y1)),
1386
139k
                            cmd_opv_polyfill,
1387
139k
                            true, sn_none /* fill doesn't need the notes */ );
1388
139k
        if (code < 0)
1389
0
            goto out;
1390
139k
        re.y += re.height;
1391
139k
    } while (re.y < re.yend);
1392
137k
out:
1393
137k
    gx_path_free(&path, "clist_put_polyfill");
1394
137k
    return code;
1395
137k
}
1396
1397
int
1398
clist_fill_parallelogram(gx_device *dev, fixed px, fixed py,
1399
                         fixed ax, fixed ay, fixed bx, fixed by,
1400
                         const gx_drawing_color *pdcolor,
1401
                         gs_logical_operation_t lop)
1402
4.67M
{
1403
4.67M
    gs_fixed_point pts[3];
1404
4.67M
    int code;
1405
1406
4.67M
    if (PARALLELOGRAM_IS_RECT(ax, ay, bx, by)) {
1407
2.26M
        gs_int_rect r;
1408
1409
2.26M
        INT_RECT_FROM_PARALLELOGRAM(&r, px, py, ax, ay, bx, by);
1410
2.26M
        return gx_fill_rectangle_device_rop(r.p.x, r.p.y, r.q.x - r.p.x,
1411
2.26M
                                            r.q.y - r.p.y, pdcolor, dev, lop);
1412
2.26M
    }
1413
2.40M
    pts[0].x = px + ax, pts[0].y = py + ay;
1414
2.40M
    pts[1].x = pts[0].x + bx, pts[1].y = pts[0].y + by;
1415
2.40M
    pts[2].x = px + bx, pts[2].y = py + by;
1416
2.40M
    code = clist_put_polyfill(dev, px, py, pts, 3, pdcolor, lop);
1417
2.40M
    return (code >= 0 ? code :
1418
2.40M
            gx_default_fill_parallelogram(dev, px, py, ax, ay, bx, by,
1419
0
                                          pdcolor, lop));
1420
4.67M
}
1421
1422
int
1423
clist_fill_triangle(gx_device *dev, fixed px, fixed py,
1424
                    fixed ax, fixed ay, fixed bx, fixed by,
1425
                    const gx_drawing_color *pdcolor,
1426
                    gs_logical_operation_t lop)
1427
0
{
1428
0
    gs_fixed_point pts[2];
1429
0
    int code;
1430
1431
0
    pts[0].x = px + ax, pts[0].y = py + ay;
1432
0
    pts[1].x = px + bx, pts[1].y = py + by;
1433
0
    code = clist_put_polyfill(dev, px, py, pts, 2, pdcolor, lop);
1434
0
    return (code >= 0 ? code :
1435
0
            gx_default_fill_triangle(dev, px, py, ax, ay, bx, by,
1436
0
                                     pdcolor, lop));
1437
0
}
1438
1439
/* ------ Path utilities ------ */
1440
1441
/* Define the state bookkeeping for writing path segments. */
1442
typedef struct cmd_segment_writer_s {
1443
    /* Set at initialization */
1444
    gx_device_clist_writer *cldev;
1445
    gx_clist_state *pcls;
1446
    /* Updated dynamically */
1447
    segment_notes notes;
1448
    byte *dp;
1449
    int len;
1450
    gs_fixed_point delta_first;
1451
    byte cmd[6 * (1 + sizeof(fixed))];
1452
}
1453
cmd_segment_writer;
1454
1455
/* Put out a path segment command. */
1456
static int
1457
cmd_put_segment(cmd_segment_writer * psw, byte op,
1458
                const fixed * operands, segment_notes notes)
1459
11.2M
{
1460
11.2M
    const fixed *optr = operands;
1461
    /* Fetch num_operands before possible command merging. */
1462
11.2M
    static const byte op_num_operands[] = {
1463
11.2M
        cmd_segment_op_num_operands_values
1464
11.2M
    };
1465
11.2M
    int i = op_num_operands[op & 0xf];
1466
    /* One picky compiler complains if we initialize to psw->cmd - 1. */
1467
11.2M
    byte *q = psw->cmd;
1468
1469
11.2M
    --q;
1470
1471
#ifdef DEBUG
1472
    if (gs_debug_c('L')) {
1473
        int j;
1474
1475
        dmlprintf2(psw->cldev->memory, "[L]  %s:%d:", cmd_sub_op_names[op >> 4][op & 0xf],
1476
                  (int)notes);
1477
        for (j = 0; j < i; ++j)
1478
            dmprintf1(psw->cldev->memory, " %g", fixed2float(operands[j]));
1479
        dmputs(psw->cldev->memory, "\n");
1480
    }
1481
#endif
1482
1483
    /* Merge or shorten commands if possible. */
1484
11.2M
    if (op == cmd_opv_rlineto) {
1485
4.86M
        if (operands[0] == 0)
1486
1.25M
            op = cmd_opv_vlineto, optr = ++operands, i = 1;
1487
3.60M
        else if (operands[1] == 0)
1488
858k
            op = cmd_opv_hlineto, i = 1;
1489
2.74M
        else
1490
2.74M
            switch (*psw->dp) {
1491
1.01M
                case cmd_opv_rmoveto:
1492
1.01M
                    psw->delta_first.x = operands[0];
1493
1.01M
                    psw->delta_first.y = operands[1];
1494
1.01M
                    op = cmd_opv_rmlineto;
1495
1.72M
                  merge:cmd_uncount_op(*psw->dp, psw->len);
1496
1.72M
                    cmd_shorten_op(psw->cldev, psw->pcls, psw->len);  /* delete it */
1497
1.72M
                    q += psw->len - 1;
1498
1.72M
                    break;
1499
717k
                case cmd_opv_rmlineto:
1500
717k
                    if (notes != psw->notes)
1501
0
                        break;
1502
717k
                    op = cmd_opv_rm2lineto;
1503
717k
                    goto merge;
1504
356k
                case cmd_opv_rm2lineto:
1505
356k
                    if (notes != psw->notes)
1506
0
                        break;
1507
356k
                    if (operands[0] == -psw->delta_first.x &&
1508
356k
                        operands[1] == -psw->delta_first.y
1509
356k
                        ) {
1510
98.0k
                        cmd_uncount_op(cmd_opv_rm2lineto, psw->len);
1511
98.0k
                        *psw->dp = cmd_count_op(cmd_opv_rm3lineto, psw->len, psw->cldev->memory);
1512
98.0k
                        return 0;
1513
98.0k
                    }
1514
258k
                    break;
1515
657k
                default:
1516
657k
                    ;
1517
2.74M
            }
1518
4.86M
    }
1519
37.7M
    for (; --i >= 0; ++optr) {
1520
26.5M
        fixed d = *optr, d2;
1521
1522
26.5M
        if (is_bits(d, _fixed_shift + 11) &&
1523
26.5M
            !(d & (float2fixed(0.25) - 1))
1524
26.5M
            ) {
1525
1.17M
            cmd_count_add1(stats_cmd_diffs[3]);
1526
1.17M
            d = ((d >> (_fixed_shift - 2)) & 0x1fff) + 0xc000;
1527
1.17M
            q += 2;
1528
25.3M
        } else if (is_bits(d, 19) && i > 0 && is_bits(d2 = optr[1], 19)) {
1529
5.23M
            cmd_count_add1(stats_cmd_diffs[0]);
1530
5.23M
            q[1] = (byte) ((d >> 13) & 0x3f);
1531
5.23M
            q[2] = (byte) (d >> 5);
1532
5.23M
            q[3] = (byte) ((d << 3) + ((d2 >> 16) & 7));
1533
5.23M
            q[4] = (byte) (d2 >> 8);
1534
5.23M
            q[5] = (byte) d2;
1535
5.23M
            q += 5;
1536
5.23M
            --i, ++optr;
1537
5.23M
            continue;
1538
20.1M
        } else if (is_bits(d, 22)) {
1539
8.36M
            cmd_count_add1(stats_cmd_diffs[1]);
1540
8.36M
            q[1] = (byte) (((d >> 16) & 0x3f) + 0x40);
1541
8.36M
            q += 3;
1542
11.7M
        } else if (is_bits(d, 30)) {
1543
10.8M
            cmd_count_add1(stats_cmd_diffs[2]);
1544
10.8M
            q[1] = (byte) (((d >> 24) & 0x3f) + 0x80);
1545
10.8M
            q[2] = (byte) (d >> 16);
1546
10.8M
            q += 4;
1547
10.8M
        } else {
1548
897k
            int b;
1549
1550
897k
            cmd_count_add1(stats_cmd_diffs[4]);
1551
897k
            *++q = 0xe0;
1552
2.69M
            for (b = sizeof(fixed) - 1; b > 1; --b)
1553
1.79M
                *++q = (byte) (d >> (b * 8));
1554
897k
            q += 2;
1555
897k
        }
1556
21.3M
        q[-1] = (byte) (d >> 8);
1557
21.3M
        *q = (byte) d;
1558
21.3M
    }
1559
11.1M
    if (notes != psw->notes) {
1560
0
        byte *dp;
1561
0
        int code =
1562
0
            set_cmd_put_op(&dp, psw->cldev, psw->pcls, cmd_opv_set_misc2, 3);
1563
1564
0
        if (code < 0)
1565
0
            return code;
1566
0
        dp[1] = segment_notes_known;
1567
0
        dp[2] = notes;
1568
0
        psw->notes = notes;
1569
11.1M
    } {
1570
11.1M
        int len = q + 2 - psw->cmd;
1571
11.1M
        byte *dp;
1572
11.1M
        int code = set_cmd_put_op(&dp, psw->cldev, psw->pcls, op, len);
1573
1574
11.1M
        if (code < 0)
1575
0
            return code;
1576
11.1M
        memcpy(dp + 1, psw->cmd, len - 1);
1577
11.1M
        psw->len = len;
1578
11.1M
        psw->dp = dp;
1579
11.1M
    }
1580
0
    return 0;
1581
11.1M
}
1582
/* Put out a line segment command. */
1583
#define cmd_put_rmoveto(psw, operands)\
1584
2.24M
  cmd_put_segment(psw, cmd_opv_rmoveto, operands, sn_none)
1585
#define cmd_put_rgapto(psw, operands, notes)\
1586
0
  cmd_put_segment(psw, cmd_opv_rgapto, operands, notes)
1587
#define cmd_put_rlineto(psw, operands, notes)\
1588
4.86M
  cmd_put_segment(psw, cmd_opv_rlineto, operands, notes)
1589
1590
1591
/* Bug 693235 shows a problem with a 'large' stroke, that
1592
 * extends from almost the minimum extent permissible
1593
 * to almost the positive extent permissible. When we band
1594
 * that, and play it back, we subtract the y offset of the band
1595
 * from it, and that causes a very negative number to tip over
1596
 * to being a very positive number.
1597
 *
1598
 * To avoid this, we spot 'far out' entries in the path, and
1599
 * reduce them to being 'less far out'.
1600
 *
1601
 * We pick 'far out' as being outside the central 1/4 of our
1602
 * 2d plane. This is far larger than is ever going to be used
1603
 * by a real device (famous last words!).
1604
 *
1605
 * We reduce the lines by moving to 1/4 of the way along them.
1606
 *
1607
 * If we only ever actually want to render the central 1/16 of
1608
 * the plane (which is still far more generous than we'd expect),
1609
 * the reduced lines should be suitably small not to overflow,
1610
 * and yet not be reduced so much that the reduction is ever visible.
1611
 *
1612
 * In practice this gives us a 4 million x 4 million maximum
1613
 * resolution.
1614
 */
1615
1616
static int
1617
far_out(gs_fixed_point out)
1618
1.43M
{
1619
1.43M
    return (out.y >= max_fixed/2 || out.y <= -(max_fixed/2) || out.x >= max_fixed/2 || out.x <= -(max_fixed/2));
1620
1.43M
}
1621
1622
static void
1623
reduce_line(fixed *m0, fixed *m1, fixed x0, fixed y0, fixed x1, fixed y1)
1624
240k
{
1625
    /* We want to find m0, m1, 1/4 of the way from x0, y0 to x1, y1. */
1626
    /* Sacrifice 2 bits of accuracy to avoid overflow. */
1627
240k
    *m0 = (x0/4) + 3*(x1/4);
1628
240k
    *m1 = (y0/4) + 3*(y1/4);
1629
240k
}
1630
1631
/*
1632
 * Write a path.  We go to a lot of trouble to omit segments that are
1633
 * entirely outside the band.
1634
 */
1635
static int
1636
cmd_put_path(gx_device_clist_writer * cldev, gx_clist_state * pcls,
1637
             const gx_path * ppath, fixed ymin, fixed ymax, byte path_op,
1638
             bool implicit_close, segment_notes keep_notes)
1639
2.18M
{
1640
2.18M
    gs_path_enum cenum;
1641
2.18M
    cmd_segment_writer writer;
1642
1643
    /*
1644
     * initial_op is logically const.  We would like to declare it as
1645
     * static const, since some systems really dislike non-const statics,
1646
     * but this would entail a cast in set_first_point() that provokes a
1647
     * warning message from gcc.  Instead, we pay the (tiny) cost of an
1648
     * unnecessary dynamic initialization.
1649
     */
1650
2.18M
    byte initial_op = cmd_opv_end_run;
1651
1652
    /*
1653
     * We define the 'side' of a point according to its Y value as
1654
     * follows:
1655
     */
1656
26.0M
#define which_side(y) ((y) < ymin ? -1 : (y) >= ymax ? 1 : 0)
1657
1658
    /*
1659
     * While writing a subpath, we need to keep track of any segments
1660
     * skipped at the beginning of the subpath and any segments skipped
1661
     * just before the current segment.  We do this with two sets of
1662
     * state variables, one that tracks the actual path segments and one
1663
     * that tracks the emitted segments.
1664
     *
1665
     * The following track the actual segments:
1666
     */
1667
1668
    /*
1669
     * The point and side of the last moveto (skipped if
1670
     * start_side != 0):
1671
     */
1672
2.18M
    gs_fixed_point start;
1673
2.18M
    int start_side = 0x7badf00d; /* Initialize against indeterminizm. */
1674
1675
    /*
1676
     * Whether any lines or curves were skipped immediately
1677
     * following the moveto:
1678
     */
1679
2.18M
    bool start_skip = 0x7badf00d; /* Initialize against indeterminizm. */
1680
1681
    /* The side of the last point: */
1682
2.18M
    int side = 0x7badf00d; /* Initialize against indeterminizm. */
1683
1684
    /* The last point with side != 0: */
1685
2.18M
    gs_fixed_point out;
1686
1687
    /* If the last out-going segment was a lineto, */
1688
    /* its notes: */
1689
2.18M
    segment_notes out_notes = 0x7badf00d; /* Initialize against indeterminizm. */
1690
1691
    /*
1692
     * The following track the emitted segments:
1693
     */
1694
1695
    /* The last point emitted: */
1696
2.18M
    fixed px = int2fixed(pcls->rect.x);
1697
2.18M
    fixed py = int2fixed(pcls->rect.y);
1698
1699
    /* The point of the last emitted moveto: */
1700
2.18M
    gs_fixed_point first;
1701
1702
    /* Information about the last emitted operation: */
1703
2.18M
    int open = 0;   /* -1 if last was moveto, 1 if line/curveto, */
1704
                                /* 0 if newpath/closepath */
1705
2.18M
    struct { fixed vs[6]; } prev = { { 0 } };
1706
1707
2.18M
    first.x = first.y = out.x = out.y = start.x = start.y = 0; /* Quiet gcc warning. */
1708
2.18M
    if_debug4m('p', cldev->memory, "[p]initial (%g,%g), clip [%g..%g)\n",
1709
2.18M
               fixed2float(px), fixed2float(py),
1710
2.18M
               fixed2float(ymin), fixed2float(ymax));
1711
2.18M
    gx_path_enum_init(&cenum, ppath);
1712
2.18M
    writer.cldev = cldev;
1713
2.18M
    writer.pcls = pcls;
1714
2.18M
    writer.notes = sn_none;
1715
4.09M
#define set_first_point() (writer.dp = &initial_op)
1716
2.18M
#define first_point() (writer.dp == &initial_op)
1717
2.18M
    set_first_point();
1718
36.2M
    for (;;) {
1719
36.2M
        fixed vs[6];
1720
1721
66.2M
#define A vs[0]
1722
67.4M
#define B vs[1]
1723
36.2M
#define C vs[2]
1724
37.6M
#define D vs[3]
1725
36.2M
#define E vs[4]
1726
40.3M
#define F vs[5]
1727
36.2M
        int pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
1728
36.2M
        byte *dp;
1729
36.2M
        int code;
1730
1731
36.2M
        switch (pe_op) {
1732
2.36M
            case 0:
1733
                /* If the path is open and needs an implicit close, */
1734
                /* do the close and then come here again. */
1735
2.36M
                if (open > 0 && implicit_close)
1736
182k
                    goto close;
1737
                /* All done. */
1738
2.18M
                pcls->rect.x = fixed2int_var(px);
1739
2.18M
                pcls->rect.y = fixed2int_var(py);
1740
2.18M
                if_debug2m('p', cldev->memory, "[p]final (%d,%d)\n",
1741
2.18M
                           pcls->rect.x, pcls->rect.y);
1742
2.18M
                return set_cmd_put_op(&dp, cldev, pcls, path_op, 1);
1743
9.43M
            case gs_pe_moveto:
1744
                /* If the path is open and needs an implicit close, */
1745
                /* do a closepath and then redo the moveto. */
1746
9.43M
                if (open > 0 && implicit_close) {
1747
47.1k
                    gx_path_enum_backup(&cenum);
1748
47.1k
                    goto close;
1749
47.1k
                }
1750
9.39M
                open = -1;
1751
9.39M
                start.x = A, start.y = B;
1752
9.39M
                start_skip = false;
1753
9.39M
                if ((start_side = side = which_side(B)) != 0) {
1754
8.23M
                    out.x = A, out.y = B;
1755
8.23M
                    if_debug3m('p', cldev->memory, "[p]skip moveto (%g,%g) side %d\n",
1756
8.23M
                               fixed2float(out.x), fixed2float(out.y),
1757
8.23M
                               side);
1758
8.23M
                    continue;
1759
8.23M
                }
1760
1.15M
                C = A - px, D = B - py;
1761
1.15M
                first.x = px = A, first.y = py = B;
1762
1.15M
                code = cmd_put_rmoveto(&writer, &C);
1763
1.15M
                if_debug2m('p', cldev->memory, "[p]moveto (%g,%g)\n",
1764
1.15M
                           fixed2float(px), fixed2float(py));
1765
1.15M
                break;
1766
0
            case gs_pe_gapto:
1767
0
                {
1768
0
                    int next_side = which_side(B);
1769
0
                    segment_notes notes =
1770
0
                    gx_path_enum_notes(&cenum) & keep_notes;
1771
1772
0
                    if (next_side == side && side != 0) { /* Skip a line completely outside the clip region. */
1773
0
                        if (open < 0)
1774
0
                            start_skip = true;
1775
0
                        out.x = A, out.y = B;
1776
0
                        out_notes = notes;
1777
0
                        if_debug3m('p', cldev->memory, "[p]skip gapto (%g,%g) side %d\n",
1778
0
                                   fixed2float(out.x), fixed2float(out.y),
1779
0
                                   side);
1780
0
                        continue;
1781
0
                    }
1782
                    /* If we skipped any segments, put out a moveto/lineto. */
1783
0
                    if (side && ((open < 0) || (px != out.x || py != out.y || first_point()))) {
1784
0
                        if (far_out(out)) {
1785
                            /* out is far enough out that we have to worry about wrapping on playback. Reduce the extent. */
1786
0
                            if (open >= 0) {
1787
0
                                fixed mid[2];
1788
0
                                fixed m0, m1;
1789
0
                                reduce_line(&m0, &m1, out.x, out.y, px, py);
1790
0
                                mid[0] = m0 - px, mid[1] = m1 - py;
1791
0
                                code = cmd_put_rlineto(&writer, mid, out_notes);
1792
0
                                if (code < 0)
1793
0
                                    return code;
1794
0
                                px = m0, py = m1;
1795
0
                            }
1796
0
                            reduce_line(&out.x, &out.y, out.x, out.y, A, B);
1797
0
                        }
1798
0
                        C = out.x - px, D = out.y - py;
1799
0
                        if (open < 0) {
1800
0
                            first = out;
1801
0
                            code = cmd_put_rmoveto(&writer, &C);
1802
0
                        } else
1803
0
                            code = cmd_put_rlineto(&writer, &C, out_notes);
1804
0
                        if (code < 0)
1805
0
                            return code;
1806
0
                        px = out.x, py = out.y;
1807
0
                        if_debug3m('p', cldev->memory, "[p]catchup %s (%g,%g) for line\n",
1808
0
                                   (open < 0 ? "moveto" : "lineto"),
1809
0
                                   fixed2float(px), fixed2float(py));
1810
0
                    }
1811
0
                    if ((side = next_side) != 0) { /* Note a vertex going outside the clip region. */
1812
0
                        out.x = A, out.y = B;
1813
0
                    }
1814
0
                    C = A - px, D = B - py;
1815
0
                    px = A, py = B;
1816
0
                    open = 1;
1817
0
                    code = cmd_put_rgapto(&writer, &C, notes);
1818
0
                }
1819
0
                if_debug3m('p', cldev->memory, "[p]gapto (%g,%g) side %d\n",
1820
0
                           fixed2float(px), fixed2float(py), side);
1821
0
                break;
1822
13.4M
            case gs_pe_lineto:
1823
13.4M
                {
1824
13.4M
                    int next_side = which_side(B);
1825
13.4M
                    segment_notes notes =
1826
13.4M
                    gx_path_enum_notes(&cenum) & keep_notes;
1827
1828
13.4M
                    if (next_side == side && side != 0) { /* Skip a line completely outside the clip region. */
1829
9.76M
                        if (open < 0)
1830
8.47M
                            start_skip = true;
1831
9.76M
                        out.x = A, out.y = B;
1832
9.76M
                        out_notes = notes;
1833
9.76M
                        if_debug3m('p', cldev->memory, "[p]skip lineto (%g,%g) side %d\n",
1834
9.76M
                                   fixed2float(out.x), fixed2float(out.y),
1835
9.76M
                                   side);
1836
9.76M
                        continue;
1837
9.76M
                    }
1838
                    /* If we skipped any segments, put out a moveto/lineto. */
1839
3.67M
                    if (side && ((open < 0) || (px != out.x || py != out.y || first_point()))) {
1840
983k
                        if (far_out(out)) {
1841
                            /* out is far enough out that we have to worry about wrapping on playback. Reduce the extent. */
1842
205k
                            if (open >= 0) {
1843
16.7k
                                fixed mid[2];
1844
16.7k
                                fixed m0, m1;
1845
16.7k
                                reduce_line(&m0, &m1, out.x, out.y, px, py);
1846
16.7k
                                mid[0] = m0 - px, mid[1] = m1 - py;
1847
16.7k
                                code = cmd_put_rlineto(&writer, mid, out_notes);
1848
16.7k
                                if (code < 0)
1849
0
                                    return code;
1850
16.7k
                                px = m0, py = m1;
1851
16.7k
                            }
1852
205k
                            reduce_line(&out.x, &out.y, out.x, out.y, A, B);
1853
205k
                        }
1854
983k
                        C = out.x - px, D = out.y - py;
1855
983k
                        if (open < 0) {
1856
881k
                            first = out;
1857
881k
                            code = cmd_put_rmoveto(&writer, &C);
1858
881k
                        } else
1859
101k
                            code = cmd_put_rlineto(&writer, &C, out_notes);
1860
983k
                        if (code < 0)
1861
0
                            return code;
1862
983k
                        px = out.x, py = out.y;
1863
983k
                        if_debug3m('p', cldev->memory, "[p]catchup %s (%g,%g) for line\n",
1864
983k
                                   (open < 0 ? "moveto" : "lineto"),
1865
983k
                                   fixed2float(px), fixed2float(py));
1866
983k
                    }
1867
3.67M
                    if ((side = next_side) != 0) { /* Note a vertex going outside the clip region. */
1868
1.20M
                        out.x = A, out.y = B;
1869
1.20M
                    }
1870
3.67M
                    C = A - px, D = B - py;
1871
3.67M
                    px = A, py = B;
1872
3.67M
                    open = 1;
1873
3.67M
                    code = cmd_put_rlineto(&writer, &C, notes);
1874
3.67M
                }
1875
3.67M
                if_debug3m('p', cldev->memory, "[p]lineto (%g,%g) side %d\n",
1876
3.67M
                           fixed2float(px), fixed2float(py), side);
1877
3.67M
                break;
1878
1.84M
            case gs_pe_closepath:
1879
#ifdef DEBUG
1880
                {
1881
                    gs_path_enum cpenum;
1882
                    gs_fixed_point cvs[3];
1883
                    int op;
1884
1885
                    cpenum = cenum;
1886
                    switch (op = gx_path_enum_next(&cpenum, cvs)) {
1887
                        case 0:
1888
                        case gs_pe_moveto:
1889
                            break;
1890
                        default:
1891
                            mlprintf1(cldev->memory,
1892
                                      "closepath followed by %d, not end/moveto!\n",
1893
                                      op);
1894
                    }
1895
                }
1896
#endif
1897
                /* A closepath may require drawing an explicit line if */
1898
                /* we skipped any segments at the beginning of the path. */
1899
2.07M
              close:if (side != start_side) { /* If we skipped any segments, put out a moveto/lineto. */
1900
691k
                    if (side && (px != out.x || py != out.y || first_point())) {
1901
447k
                        if (far_out(out)) {
1902
                            /* out is far enough out that we have to worry about wrapping on playback. Reduce the extent. */
1903
9.40k
                            if (open >= 0) {
1904
9.40k
                                fixed mid[2];
1905
9.40k
                                fixed m0, m1;
1906
9.40k
                                reduce_line(&m0, &m1, out.x, out.y, px, py);
1907
9.40k
                                mid[0] = m0 - px, mid[1] = m1 - py;
1908
9.40k
                                code = cmd_put_rlineto(&writer, mid, out_notes);
1909
9.40k
                                if (code < 0)
1910
0
                                    return code;
1911
9.40k
                                px = m0, py = m1;
1912
9.40k
                            }
1913
9.40k
                            reduce_line(&out.x, &out.y, out.x, out.y, A, B);
1914
9.40k
                        }
1915
447k
                        C = out.x - px, D = out.y - py;
1916
447k
                        code = cmd_put_rlineto(&writer, &C, out_notes);
1917
447k
                        if (code < 0)
1918
0
                            return code;
1919
447k
                        px = out.x, py = out.y;
1920
447k
                        if_debug2m('p', cldev->memory, "[p]catchup line (%g,%g) for close\n",
1921
447k
                                   fixed2float(px), fixed2float(py));
1922
447k
                    }
1923
691k
                    if (open > 0 && start_skip) { /* Draw the closing line back to the start. */
1924
400k
                        C = start.x - px, D = start.y - py;
1925
400k
                        code = cmd_put_rlineto(&writer, &C, sn_none);
1926
400k
                        if (code < 0)
1927
0
                            return code;
1928
400k
                        px = start.x, py = start.y;
1929
400k
                        if_debug2m('p', cldev->memory, "[p]draw close to (%g,%g)\n",
1930
400k
                                   fixed2float(px), fixed2float(py));
1931
400k
                    }
1932
691k
                }
1933
                /*
1934
                 * We don't bother to update side because we know that the
1935
                 * next element after a closepath, if any, must be a moveto.
1936
                 * We must handle explicitly the possibility that the entire
1937
                 * subpath was skipped.
1938
                 */
1939
2.07M
                if (implicit_close || open <= 0) {
1940
                    /*
1941
                     * Force writing an explicit moveto if the next subpath
1942
                     * starts with a moveto to the same point where this one
1943
                     * ends.
1944
                     */
1945
1.91M
                    set_first_point();
1946
                    /*
1947
                     * If implicit_close == true, we don't need an explicit closepath,
1948
                     * because the filling algorithm will close subpath automatically.
1949
                     * Otherwise, if open < 0, we have an empty closed path.
1950
                     * If side != 0, it is outside the band, so we can
1951
                     * safely skip it, because the band has been expanded
1952
                     * with line width.
1953
                     */
1954
1.91M
                    if (side != 0) {
1955
1.34M
                        open = 0;
1956
1.34M
                        continue;
1957
1.34M
                    }
1958
1.91M
                }
1959
728k
                open = 0;
1960
728k
                px = first.x, py = first.y;
1961
728k
                code = cmd_put_segment(&writer, cmd_opv_closepath, &A, sn_none);
1962
728k
                if_debug0m('p', cldev->memory, "[p]close\n");
1963
728k
                break;
1964
9.17M
            case gs_pe_curveto:
1965
9.17M
                {
1966
9.17M
                    segment_notes notes =
1967
9.17M
                    gx_path_enum_notes(&cenum) & keep_notes;
1968
1969
9.17M
                    {
1970
9.17M
                        fixed bpy, bqy;
1971
9.17M
                        int all_side, out_side;
1972
1973
                        /* Compute the Y bounds for the clipping check. */
1974
9.17M
                        if (B < D)
1975
4.37M
                            bpy = B, bqy = D;
1976
4.79M
                        else
1977
4.79M
                            bpy = D, bqy = B;
1978
9.17M
                        if (F < bpy)
1979
2.72M
                            bpy = F;
1980
6.45M
                        else if (F > bqy)
1981
2.73M
                            bqy = F;
1982
9.17M
                        all_side = (bqy < ymin ? -1 : bpy > ymax ? 1 : 0);
1983
9.17M
                        if (all_side != 0) {
1984
5.98M
                            if (all_side == side) { /* Skip a curve entirely outside the clip region. */
1985
5.75M
                                if (open < 0)
1986
3.03M
                                    start_skip = true;
1987
5.75M
                                out.x = E, out.y = F;
1988
5.75M
                                out_notes = notes;
1989
5.75M
                                if_debug3m('p', cldev->memory,
1990
5.75M
                                           "[p]skip curveto (%g,%g) side %d\n",
1991
5.75M
                                           fixed2float(out.x), fixed2float(out.y),
1992
5.75M
                                           side);
1993
5.75M
                                continue;
1994
5.75M
                            }
1995
230k
                            out_side = all_side;
1996
230k
                        } else
1997
3.19M
                            out_side = which_side(F);
1998
                        /* If we skipped any segments, put out a moveto/lineto. */
1999
3.42M
                        if (side && ((open < 0) || (px != out.x || py != out.y || first_point()))) {
2000
415k
                            fixed diff[2];
2001
2002
415k
                            diff[0] = out.x - px, diff[1] = out.y - py;
2003
415k
                            if (open < 0) {
2004
207k
                                first = out;
2005
207k
                                code = cmd_put_rmoveto(&writer, diff);
2006
207k
                            } else
2007
207k
                                code = cmd_put_rlineto(&writer, diff, out_notes);
2008
415k
                            if (code < 0)
2009
0
                                return code;
2010
415k
                            px = out.x, py = out.y;
2011
415k
                            if_debug3m('p', cldev->memory,
2012
415k
                                       "[p]catchup %s (%g,%g) for curve\n",
2013
415k
                                       (open < 0 ? "moveto" : "lineto"),
2014
415k
                                       fixed2float(px), fixed2float(py));
2015
415k
                        }
2016
3.42M
                        if ((side = out_side) != 0) { /* Note a vertex going outside the clip region. */
2017
854k
                            out.x = E, out.y = F;
2018
854k
                        }
2019
3.42M
                    }
2020
0
                    {
2021
3.42M
                        fixed nx = E, ny = F;
2022
3.42M
                        const fixed *optr = vs;
2023
3.42M
                        byte op;
2024
2025
3.42M
                        if_debug7m('p', cldev->memory,
2026
3.42M
                                   "[p]curveto (%g,%g; %g,%g; %g,%g) side %d\n",
2027
3.42M
                                   fixed2float(A), fixed2float(B),
2028
3.42M
                                   fixed2float(C), fixed2float(D),
2029
3.42M
                                   fixed2float(E), fixed2float(F), side);
2030
3.42M
                        E -= C, F -= D;
2031
3.42M
                        C -= A, D -= B;
2032
3.42M
                        A -= px, B -= py;
2033
3.42M
                        if (*writer.dp >= cmd_opv_min_curveto &&
2034
3.42M
                            *writer.dp <= cmd_opv_max_curveto &&
2035
3.42M
                            ((prev.A == 0 &&
2036
2.70M
                              A == prev.E && C == prev.C && E == prev.A &&
2037
2.70M
                              B == -prev.F && D == -prev.D && F == -prev.B) ||
2038
2.70M
                             (prev.A != 0 &&
2039
2.67M
                              A == -prev.E && C == -prev.C && E == -prev.A &&
2040
2.67M
                              B == prev.F && D == prev.D && F == prev.B))
2041
3.42M
                            )
2042
31.5k
                            op = cmd_opv_scurveto;
2043
3.39M
                        else if (A == 0 && F == 0) {
2044
106k
                            optr++, op = cmd_opv_vhcurveto;
2045
106k
                            if ((B ^ C) >= 0) {
2046
62.5k
                                if (D == C && E == B)
2047
11.8k
                                    op = cmd_opv_vqcurveto;
2048
62.5k
                            } else if (D == -C && E == -B)
2049
3.29k
                                op = cmd_opv_vqcurveto;
2050
3.28M
                        } else if (B == 0 && E == 0) {
2051
87.3k
                            B = A, E = F, optr++, op = cmd_opv_hvcurveto;
2052
87.3k
                            if ((B ^ D) >= 0) {
2053
41.8k
                                if (C == D && E == B)
2054
1.43k
                                    op = cmd_opv_hqcurveto;
2055
45.5k
                            } else if (C == -D && E == -B)
2056
656
                                C = D, op = cmd_opv_hqcurveto;
2057
87.3k
                        }
2058
3.19M
                        else if (A == 0 && B == 0)
2059
8.52k
                            optr += 2, op = cmd_opv_nrcurveto;
2060
3.18M
                        else if (E == 0 && F == 0)
2061
15.7k
                            op = cmd_opv_rncurveto;
2062
3.17M
                        else
2063
3.17M
                            op = cmd_opv_rrcurveto;
2064
3.42M
                        memcpy(prev.vs, vs, sizeof(prev.vs));
2065
3.42M
                        px = nx, py = ny;
2066
3.42M
                        open = 1;
2067
3.42M
                        code = cmd_put_segment(&writer, op, optr, notes);
2068
3.42M
                    }
2069
3.42M
                }
2070
0
                break;
2071
0
            default:
2072
0
                return_error(gs_error_rangecheck);
2073
36.2M
        }
2074
8.98M
        if (code < 0)
2075
0
            return code;
2076
8.98M
#undef A
2077
8.98M
#undef B
2078
8.98M
#undef C
2079
8.98M
#undef D
2080
8.98M
#undef E
2081
8.98M
#undef F
2082
8.98M
    }
2083
2.18M
}