Coverage Report

Created: 2025-06-10 07:24

/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
0
{
57
    /*
58
     * We only know how to compute an accurate color set for the
59
     * standard CMYK color mapping function.
60
     */
61
0
    if (dev_proc(cldev, dev_spec_op)((gx_device *)cldev,
62
0
                                     gxdso_is_std_cmyk_1bit, NULL, 0) <= 0)
63
0
        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
0
}
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
2.79M
{
86
2.79M
    gs_rop3_t rop = lop_rop(lop);
87
88
2.79M
    if (pdcolor != 0 && gx_dc_is_pure(pdcolor)) {
89
2.78M
        gx_color_index color = gx_dc_pure_color(pdcolor);
90
91
2.78M
        if (color == gx_device_black(dev))
92
2.59M
            rop = rop3_know_T_0(rop);
93
192k
        else if (color == gx_device_white(dev))
94
15.7k
            rop = rop3_know_T_1(rop);
95
2.78M
    }
96
2.79M
    return !(rop == rop3_0 || rop == rop3_1 ||
97
2.79M
             rop == rop3_S || rop == rop3_T);
98
2.79M
}
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
3.63M
{
109
3.63M
    const gx_device_halftone * pdht = pdcolor->type->get_dev_halftone(pdcolor);
110
3.63M
    int                        code, di;
111
3.63M
    uint                       dc_size = 0, req_size;
112
3.63M
    gx_device_color_saved *    psdc = &pcls->sdc;
113
3.63M
    byte *                     dp;
114
3.63M
    byte *                     dp0;
115
3.63M
    gs_int_point               color_phase;
116
3.63M
    int            buffer_space;
117
3.63M
    int            offset = 0;
118
3.63M
    int            left;
119
3.63M
    uint           portion_size, prefix_size;
120
3.63M
    int            req_size_final;
121
3.63M
    bool           is_pattern;
122
3.63M
    gs_id          pattern_id = gs_no_id;
123
3.63M
    bool           all_bands = (pre == NULL);
124
125
    /* see if the halftone must be inserted in the command list */
126
3.63M
    if ( pdht != NULL                          &&
127
3.63M
         pdht->id != cldev->device_halftone_id   ) {
128
0
        if ((code = cmd_put_halftone(cldev, pdht)) < 0)
129
0
            return code;
130
0
        psdc->type = gx_dc_type_none; /* force writing */
131
0
    }
132
133
3.63M
    if (psdc->devn_type != devn_type) {
134
214k
        psdc->type = gx_dc_type_none; /* force writing if fill/stroke mismatch. */
135
214k
        psdc->devn_type = devn_type;
136
214k
    }
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
3.63M
    di = gx_get_dc_type_index(pdcolor);
148
3.63M
    code = pdcolor->type->write( pdcolor,
149
3.63M
                                 psdc,
150
3.63M
                                 (gx_device *)cldev,
151
3.63M
                                 0,
152
3.63M
                                 0,
153
3.63M
                                 &dc_size );
154
155
    /* if the returned value is > 0, no change in the color is necessary */
156
3.63M
    if (code > 0 && ((devn_type == devn_not_tile_fill) || (devn_type == devn_not_tile_stroke)))
157
3.18M
        return 0;
158
450k
    else if (code < 0 && code != gs_error_rangecheck)
159
8
        return code;
160
450k
    if (!all_bands && dc_size * pre->rect_nbands > 1024*1024 /* arbitrary */)
161
28
        all_bands = true;
162
450k
    is_pattern = gx_dc_is_pattern1_color(pdcolor);
163
450k
    if (is_pattern)
164
46.7k
        pattern_id = gs_dc_get_pattern_id(pdcolor);
165
450k
    if (all_bands) {
166
1.10k
        gx_clist_state * pcls1;
167
168
6.20k
        for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++) {
169
6.17k
            if (pcls1->pattern_id == pattern_id) {
170
1.07k
                pcls->pattern_id = gs_no_id; /* Force writing entire pattern. */
171
1.07k
                break;
172
1.07k
            }
173
6.17k
        }
174
1.10k
    }
175
450k
    left = dc_size;
176
177
450k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
178
    /* see if phase information must be inserted in the command list */
179
450k
    if (pdcolor->type->get_phase(pdcolor, &color_phase) &&
180
450k
        (color_phase.x != pcls->screen_phase[gs_color_select_texture].x ||
181
46.7k
         color_phase.y != pcls->screen_phase[gs_color_select_texture].y || all_bands)) {
182
        /* Devc phase is the reverse of screen phase! */
183
28
        code = cmd_set_screen_phase_generic(cldev, pcls,
184
28
                                            -color_phase.x, -color_phase.y,
185
28
                                            gs_color_select_texture, all_bands);
186
28
        if (code < 0)
187
0
            return code;
188
28
    }
189
190
450k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
191
450k
    if (is_pattern) {
192
46.7k
        pattern_id = gs_dc_get_pattern_id(pdcolor);
193
194
46.7k
        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
13
            left = sizeof(pattern_id);
201
13
        }
202
46.7k
    }
203
204
450k
    do {
205
450k
        int extop;
206
450k
        prefix_size = 2 + 1 + (offset > 0 ? enc_u_sizew(offset) : 0);
207
450k
        req_size = left + prefix_size + enc_u_sizew(left);
208
450k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
209
450k
        code = cmd_get_buffer_space(cldev, pcls, req_size);
210
450k
        if (code < 0)
211
0
            return code;
212
450k
        buffer_space = min(code, req_size);
213
450k
        portion_size = buffer_space - prefix_size - enc_u_sizew(left);
214
450k
        req_size_final = portion_size + prefix_size + enc_u_sizew(portion_size);
215
450k
        if (req_size_final > buffer_space)
216
0
            return_error(gs_error_unregistered); /* Must not happen. */
217
450k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
218
450k
        switch (devn_type) {
219
313k
            case devn_not_tile_fill:
220
313k
                extop = cmd_opv_ext_put_fill_dcolor;
221
313k
                break;
222
136k
            case devn_not_tile_stroke:
223
136k
                extop = cmd_opv_ext_put_stroke_dcolor;
224
136k
                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
450k
        }
234
450k
        if (all_bands)
235
1.54k
            code = set_cmd_put_all_extended_op(&dp, cldev, extop, req_size_final);
236
449k
        else
237
449k
            code = set_cmd_put_extended_op(&dp, cldev, pcls, extop, req_size_final);
238
450k
        if (code < 0)
239
0
            return code;
240
450k
        dp0 = dp;
241
450k
        dp += 2;
242
450k
        *dp++ = di | (offset > 0 ? 0x80 : 0);
243
450k
        if (offset > 0)
244
450k
            enc_u_putw(offset, dp);
245
450k
        enc_u_putw(portion_size, dp);
246
450k
        code = pdcolor->type->write( pdcolor,
247
450k
                                     &pcls->sdc,
248
450k
                                     (gx_device *)cldev,
249
450k
                                     offset,
250
450k
                                     dp,
251
450k
                                     &portion_size);
252
450k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cldev);
253
450k
        if (code < 0) {
254
0
            if (offset == 0)
255
0
                cldev->cnext = dp0;
256
0
            return code;
257
0
        }
258
450k
        offset += portion_size;
259
450k
        left -= portion_size;
260
450k
    } while (left);
261
262
    /* attempt to properly calculate color_usage */
263
450k
    pcls->color_usage.or |= cmd_drawing_color_usage(cldev, pdcolor);
264
265
    /* record the color we have just serialized color */
266
450k
    pdcolor->type->save_dc(pdcolor, &pcls->sdc);
267
450k
    if (pattern_id != gs_no_id) {
268
        /* Don't record empty tiles because they're not cached. */
269
6.20k
        pcls->pattern_id = pattern_id;
270
6.20k
    }
271
450k
    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
46.7k
        pcls->sdc.colors.pattern.id = pattern_id;
275
46.7k
        if (pattern_id &&
276
46.7k
            (gx_pattern1_get_transptr(pdcolor) != NULL ||
277
6.20k
             gx_pattern1_clist_has_trans(pdcolor))) {
278
            /* update either this band or all bands with the trans_bbox */
279
5.17k
            if (all_bands) {
280
22
                pcls->color_usage.trans_bbox.p.x = 0;
281
22
                pcls->color_usage.trans_bbox.q.x = cldev->width;  /* no other information available */
282
22
                pcls->color_usage.trans_bbox.p.y = 0;
283
22
                pcls->color_usage.trans_bbox.q.y = cldev->height;
284
22
                clist_update_trans_bbox(cldev, &(pcls->color_usage.trans_bbox));
285
5.14k
            } else {
286
5.14k
                pcls->color_usage.trans_bbox.p.x = 0;
287
5.14k
                pcls->color_usage.trans_bbox.q.x = cldev->width;  /* no other information available */
288
5.14k
                pcls->color_usage.trans_bbox.p.y = pre->y;
289
5.14k
                pcls->color_usage.trans_bbox.q.y = pre->yend;
290
5.14k
            }
291
5.17k
        }
292
46.7k
    }
293
450k
    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
28
        gx_clist_state * pcls1;
298
299
5.12k
        for (pcls1 = cldev->states; pcls1 < cldev->states + cldev->nbands; pcls1++) {
300
5.10k
            pcls1->sdc = pcls->sdc;
301
5.10k
            pcls1->pattern_id = pcls->pattern_id;
302
5.10k
            pcls1->tile_phase.x = pcls->tile_phase.x;
303
5.10k
            pcls1->tile_phase.y = pcls->tile_phase.y;
304
5.10k
            pcls1->color_usage.or = pcls->color_usage.or;
305
5.10k
        }
306
28
    }
307
450k
    return code;
308
450k
}
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
453k
{
317
453k
    if (cldev->page_uses_transparency &&
318
453k
        (cldev->color_info.polarity != cldev->clist_color_info.polarity ||
319
453k
        (cldev->color_info.num_components != cldev->clist_color_info.num_components))) {
320
        /* we would have to transform the color which would impact performance */
321
83.2k
        return gx_color_usage_all(cldev);
322
83.2k
    }
323
324
370k
    if (gx_dc_is_pure(pdcolor))
325
323k
        return gx_color_index2usage((gx_device *)cldev, gx_dc_pure_color(pdcolor));
326
46.7k
    else if (gx_dc_is_binary_halftone(pdcolor))
327
0
        return gx_color_index2usage((gx_device *)cldev,
328
0
                                    gx_color_index2usage((gx_device *)cldev, gx_dc_binary_color0(pdcolor)) |
329
0
                                    gx_color_index2usage((gx_device *)cldev, gx_dc_binary_color1(pdcolor)));
330
46.7k
    else if (gx_dc_is_colored_halftone(pdcolor))
331
0
        return gx_color_index2usage((gx_device *)cldev, colored_halftone_color_usage(cldev, pdcolor));
332
46.7k
    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
46.7k
    else
339
46.7k
        return gx_color_usage_all(cldev);
340
370k
}
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
86.2k
{
347
86.2k
    uint unknown = ~known;
348
86.2k
    gx_clist_state *pcls = cldev->states;
349
86.2k
    int i;
350
351
20.5M
    for (i = cldev->nbands; --i >= 0; ++pcls)
352
20.4M
        pcls->known &= unknown;
353
86.2k
}
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
1.40M
{
359
1.40M
    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
1.40M
    cldev->clip_path = pcpath;
364
1.40M
    if (pcpath->id == cldev->clip_path_id)
365
1.36M
        return false;
366
36.9k
    cldev->clip_path_id = pcpath->id;
367
36.9k
    return true;
368
1.40M
}
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
67.2k
{
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
67.2k
    if (state_neq(line_params.curve_join) || state_neq(accurate_curves) ||
387
67.2k
        state_neq(stroke_adjust)
388
67.2k
        ) {
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
67.2k
    if (cdev->gs_gstate.flatness != flatness) {
395
205
        *punknown |= flatness_known;
396
205
        cdev->gs_gstate.flatness = flatness;
397
205
    }
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
67.2k
    if (state_neq(overprint) || state_neq(overprint_mode) ||
405
67.2k
        state_neq(blend_mode) || state_neq(text_knockout) ||
406
67.2k
        state_neq(stroke_overprint) || state_neq(renderingintent)) {
407
1.97k
        *punknown |= op_bm_tk_known;
408
1.97k
        state_update(overprint);
409
1.97k
        state_update(overprint_mode);
410
1.97k
        state_update(blend_mode);
411
1.97k
        state_update(text_knockout);
412
1.97k
        state_update(stroke_overprint);
413
1.97k
        state_update(renderingintent);
414
1.97k
    }
415
67.2k
    if (state_neq(alphaisshape)) {
416
355
        *punknown |= ais_known;
417
355
        state_update(alphaisshape);
418
355
    }
419
67.2k
    if (state_neq(strokeconstantalpha)) {
420
636
        *punknown |= stroke_alpha_known;
421
636
        state_update(strokeconstantalpha);
422
636
    }
423
67.2k
    if (cdev->gs_gstate.fillconstantalpha != pgs->fillconstantalpha) {
424
761
        *punknown |= fill_alpha_known;
425
761
        state_update(fillconstantalpha);
426
761
    }
427
67.2k
    if (cdev->gs_gstate.fill_adjust.x != padjust->x ||
428
67.2k
        cdev->gs_gstate.fill_adjust.y != padjust->y
429
67.2k
        ) {
430
124
        *punknown |= fill_adjust_known;
431
124
        cdev->gs_gstate.fill_adjust = *padjust;
432
124
    }
433
67.2k
    if (cmd_check_clip_path(cdev, pcpath))
434
28.3k
        *punknown |= clip_path_known;
435
67.2k
}
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
269k
{
441
269k
    stream s;
442
443
269k
    s_init(&s, cldev->memory);
444
269k
    swrite_position_only(&s);
445
269k
    sput_matrix(&s, m);
446
269k
    return (uint)stell(&s);
447
269k
}
448
449
/* Compute the written CTM length. */
450
int
451
cmd_write_ctm_return_length_nodevice(const gs_matrix *m)
452
84.9k
{
453
84.9k
    stream s;
454
455
84.9k
    s_init(&s, NULL);
456
84.9k
    swrite_position_only(&s);
457
84.9k
    sput_matrix(&s, m);
458
84.9k
    return (uint)stell(&s);
459
84.9k
}
460
461
/* Write out CTM. */
462
int
463
cmd_write_ctm(const gs_matrix *m, byte *dp, int len)
464
354k
{
465
354k
    stream s;
466
467
354k
    s_init(&s, NULL);
468
354k
    swrite_string(&s, dp + 1, len);
469
354k
    sput_matrix(&s, m);
470
354k
    return 0;
471
354k
}
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
621k
{
478
621k
    uint unknown = ~pcls->known & must_know;
479
621k
    uint misc2_unknown = unknown & misc2_all_known;
480
621k
    byte *dp;
481
621k
    int code;
482
483
621k
    if (misc2_unknown) {
484
200k
        byte buf[
485
200k
                 1 +    /* cap_join: start_cap|join */
486
200k
                 1 +            /*           end_cap|dash_cap */
487
200k
                 1 +    /* cj_ac_sa */
488
200k
                 sizeof(float) +  /* flatness */
489
200k
                 sizeof(float) +  /* line width */
490
200k
                 sizeof(float) +  /* miter limit */
491
200k
                 3 +    /* bm_tk, op, and rend intent */
492
200k
                 sizeof(cldev->gs_gstate.alphaisshape) +
493
200k
                 sizeof(float) * 2  /* ca CA */
494
200k
                 ];
495
200k
        byte *bp = buf;
496
200k
        if (unknown & cap_join_known) {
497
35.9k
            *bp++ = (cldev->gs_gstate.line_params.start_cap << 3) +
498
35.9k
                cldev->gs_gstate.line_params.join;
499
35.9k
            *bp++ = (cldev->gs_gstate.line_params.end_cap << 3) +
500
35.9k
                cldev->gs_gstate.line_params.dash_cap;
501
35.9k
        }
502
200k
        if (unknown & cj_ac_sa_known) {
503
27.8k
            *bp++ =
504
27.8k
                ((cldev->gs_gstate.line_params.curve_join + 1) << 2) +
505
27.8k
                (cldev->gs_gstate.accurate_curves ? 2 : 0) +
506
27.8k
                (cldev->gs_gstate.stroke_adjust ? 1 : 0);
507
27.8k
        }
508
200k
        if (unknown & flatness_known) {
509
9.89k
            memcpy(bp, &cldev->gs_gstate.flatness, sizeof(float));
510
9.89k
            bp += sizeof(float);
511
9.89k
        }
512
200k
        if (unknown & line_width_known) {
513
75.1k
            float width =
514
75.1k
                gx_current_line_width(&cldev->gs_gstate.line_params);
515
516
75.1k
            memcpy(bp, &width, sizeof(width));
517
75.1k
            bp += sizeof(width);
518
75.1k
        }
519
200k
        if (unknown & miter_limit_known) {
520
1.16k
            memcpy(bp, &cldev->gs_gstate.line_params.miter_limit,
521
1.16k
                   sizeof(float));
522
1.16k
            bp += sizeof(float);
523
1.16k
        }
524
200k
        if (unknown & op_bm_tk_known) {
525
109k
            *bp++ =
526
109k
                ((int)cldev->gs_gstate.blend_mode << 3) +
527
109k
                cldev->gs_gstate.text_knockout;
528
109k
            *bp++ =
529
109k
                (cldev->gs_gstate.overprint_mode << 2) +
530
109k
                (cldev->gs_gstate.stroke_overprint << 1) +
531
109k
                cldev->gs_gstate.overprint;
532
109k
            *bp++ = cldev->gs_gstate.renderingintent;
533
109k
        }
534
200k
        if (unknown & ais_known) {
535
21.3k
            memcpy(bp, &cldev->gs_gstate.alphaisshape,
536
21.3k
                sizeof(cldev->gs_gstate.alphaisshape));
537
21.3k
            bp += sizeof(cldev->gs_gstate.alphaisshape);
538
21.3k
        }
539
200k
        if (unknown & stroke_alpha_known) {
540
65.0k
            memcpy(bp, &cldev->gs_gstate.strokeconstantalpha, sizeof(float));
541
65.0k
            bp += sizeof(float);
542
65.0k
        }
543
200k
        if (unknown & fill_alpha_known) {
544
68.5k
            memcpy(bp, &cldev->gs_gstate.fillconstantalpha, sizeof(float));
545
68.5k
            bp += sizeof(float);
546
68.5k
        }
547
200k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_misc2,
548
200k
                              1 + cmd_sizew(misc2_unknown) + (bp - buf));
549
200k
        if (code < 0)
550
0
            return 0;
551
200k
        memcpy(cmd_put_w(misc2_unknown, dp + 1), buf, bp - buf);
552
200k
        pcls->known |= misc2_unknown;
553
200k
    }
554
621k
    if (unknown & fill_adjust_known) {
555
8.24k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_fill_adjust,
556
8.24k
                              1 + sizeof(fixed) * 2);
557
8.24k
        if (code < 0)
558
0
            return code;
559
8.24k
        memcpy(dp + 1, &cldev->gs_gstate.fill_adjust.x, sizeof(fixed));
560
8.24k
        memcpy(dp + 1 + sizeof(fixed), &cldev->gs_gstate.fill_adjust.y, sizeof(fixed));
561
8.24k
        pcls->known |= fill_adjust_known;
562
8.24k
    }
563
621k
    if (unknown & ctm_known) {
564
269k
        int len = cmd_write_ctm_return_length(cldev, &ctm_only(&cldev->gs_gstate));
565
566
269k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_ctm, len + 1);
567
269k
        if (code < 0)
568
0
            return code;
569
269k
        code = cmd_write_ctm(&ctm_only(&cldev->gs_gstate), dp, len);
570
269k
        if (code < 0)
571
0
            return code;
572
269k
        pcls->known |= ctm_known;
573
269k
    }
574
621k
    if (unknown & dash_known) {
575
90.7k
        int n = cldev->gs_gstate.line_params.dash.pattern_size;
576
577
90.7k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_dash,
578
90.7k
                              2 + (n + 2) * sizeof(float));
579
90.7k
        if (code < 0)
580
0
            return code;
581
90.7k
        dp[1] = n + (cldev->gs_gstate.line_params.dash.adapt ? 0x80 : 0) +
582
90.7k
            (cldev->gs_gstate.line_params.dot_length_absolute ? 0x40 : 0);
583
90.7k
        memcpy(dp + 2, &cldev->gs_gstate.line_params.dot_length,
584
90.7k
               sizeof(float));
585
90.7k
        memcpy(dp + 2 + sizeof(float),
586
90.7k
               &cldev->gs_gstate.line_params.dash.offset,
587
90.7k
               sizeof(float));
588
90.7k
        if (n != 0)
589
67.3k
            memcpy(dp + 2 + sizeof(float) * 2,
590
67.3k
                   cldev->dash_pattern, n * sizeof(float));
591
90.7k
        pcls->known |= dash_known;
592
90.7k
    }
593
621k
    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
459k
        const gx_clip_path *pcpath = cldev->clip_path;
599
459k
        int band_height = cldev->page_info.band_params.BandHeight;
600
459k
        int ymin = (pcls - cldev->states) * band_height;
601
459k
        int ymax = min(ymin + band_height, cldev->height);
602
459k
        gs_fixed_rect box;
603
459k
        int code;
604
459k
        int fill_adjust_size;
605
459k
        enum {
606
459k
            write_path_as_rect = 0,
607
459k
            write_path_as_rects = 1,
608
459k
            write_path_as_outer_box = 2,
609
459k
            write_path_as_path = 3
610
459k
        } 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
459k
        if (pcpath->path_valid) {
619
130k
            if (gx_path_is_rectangle(&pcpath->path, &box) &&
620
130k
                fixed_is_int(box.p.x | box.p.y | box.q.x | box.q.y))
621
5.55k
                method = write_path_as_rect;
622
125k
            else if ( !(cldev->disable_mask & clist_disable_complex_clip) )
623
125k
                method = write_path_as_path;
624
0
            else
625
0
                method = write_path_as_outer_box;
626
328k
        } else {
627
328k
            const gx_clip_list *list = gx_cpath_list(pcpath);
628
328k
            const gx_clip_rect *prect = list->head;
629
630
328k
            if (prect != NULL &&
631
328k
                cldev->disable_mask & clist_disable_complex_clip)
632
0
                method = write_path_as_outer_box;
633
328k
            else
634
328k
                method = write_path_as_rects;
635
328k
        }
636
637
        /* And thus how large the fill_adjust values will be. */
638
459k
        if (method == write_path_as_path)
639
125k
            fill_adjust_size = cmd_size2w(pcpath->path_fill_adjust.x,
640
459k
                                          pcpath->path_fill_adjust.y);
641
334k
        else
642
334k
            fill_adjust_size = cmd_size2w(0, 0);
643
644
        /* Send the 'begin_clip' with the fill_adjust values. */
645
459k
        code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_begin_clip, 1+fill_adjust_size);
646
459k
        if (code < 0)
647
0
            return code;
648
459k
        dp++;
649
459k
        if (method == write_path_as_path)
650
125k
            cmd_put2w(pcpath->path_fill_adjust.x,
651
459k
                      pcpath->path_fill_adjust.y, &dp);
652
334k
        else
653
334k
            cmd_put2w(0, 0, &dp);
654
655
        /* Then send the actual clip path representation. */
656
459k
        switch (method)
657
459k
        {
658
5.55k
        case write_path_as_rect:
659
            /* Write the path as a rectangle. */
660
5.55k
            code = cmd_write_rect_cmd(cldev, pcls, cmd_op_fill_rect,
661
5.55k
                                      fixed2int_var(box.p.x),
662
5.55k
                                      fixed2int_var(box.p.y),
663
5.55k
                                      fixed2int(box.q.x - box.p.x),
664
5.55k
                                      fixed2int(box.q.y - box.p.y));
665
5.55k
            break;
666
125k
        case write_path_as_path:
667
            /* Write the path. */
668
125k
            code = cmd_put_path(cldev, pcls, &pcpath->path,
669
125k
                                int2fixed(ymin - 1),
670
125k
                                int2fixed(ymax + 1),
671
125k
                                (byte)(pcpath->rule == gx_rule_even_odd ?
672
121k
                                 cmd_opv_eofill : cmd_opv_fill),
673
125k
                                true, sn_not_first);
674
125k
            break;
675
328k
        case write_path_as_rects:
676
328k
        {
677
            /* Write out the rectangles. */
678
328k
            const gx_clip_list *list = gx_cpath_list(pcpath);
679
328k
            const gx_clip_rect *prect = list->head;
680
681
328k
            if (prect == 0)
682
307k
                prect = &list->single;
683
3.88M
            for (; prect != 0 && code >= 0; prect = prect->next) {
684
3.55M
                if (prect->xmax > prect->xmin &&
685
3.55M
                    prect->ymin < ymax && prect->ymax > ymin) {
686
229k
                    code = cmd_write_rect_cmd(cldev, pcls, cmd_op_fill_rect,
687
229k
                                              prect->xmin, prect->ymin,
688
229k
                                              prect->xmax - prect->xmin,
689
229k
                                              prect->ymax - prect->ymin);
690
229k
                }
691
3.55M
            }
692
328k
            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
459k
        }
709
710
        /* And now we can send 'end_clip' so the reader can finalise everything. */
711
459k
        {
712
459k
            int end_code =
713
459k
                set_cmd_put_op(&dp, cldev, pcls, cmd_opv_end_clip, 1);
714
715
459k
            if (code >= 0)
716
459k
                code = end_code; /* take the first failure seen */
717
459k
            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
459k
        }
729
459k
        if (code < 0)
730
0
            return code;
731
459k
        pcls->clip_enabled = 1;
732
459k
        pcls->known |= clip_path_known;
733
459k
    }
734
621k
    if (unknown & color_space_known) {
735
53.1k
        byte *dp;
736
737
53.1k
        if (cldev->color_space.byte1 & 8) { /* indexed */
738
181
            const gs_color_space *pcs = cldev->color_space.space;
739
181
            int hival = pcs->params.indexed.hival;
740
181
            uint num_values = (hival + 1) *
741
181
                gs_color_space_num_components(pcs->base_space);
742
181
            bool use_proc = cldev->color_space.byte1 & 4;
743
181
            const void *map_data;
744
181
            uint map_size;
745
746
181
            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
181
            } else {
751
181
                map_data = pcs->params.indexed.lookup.table.data;
752
181
                map_size = num_values;
753
181
            }
754
181
            code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_color_space,
755
181
                                  2 + cmd_sizew(hival) + map_size +
756
181
                                  sizeof(clist_icc_color_t));
757
181
            if (code < 0)
758
0
                return code;
759
            /* Save the ICC information */
760
181
            memcpy(dp + 2, &(cldev->color_space.icc_info),
761
181
                   sizeof(clist_icc_color_t));
762
181
            memcpy(cmd_put_w(hival, dp + 2 +
763
181
                   sizeof(clist_icc_color_t)), map_data, map_size);
764
52.9k
        } else {
765
52.9k
            code = set_cmd_put_op(&dp, cldev, pcls, cmd_opv_set_color_space,
766
52.9k
                2 + sizeof(clist_icc_color_t));
767
52.9k
            if (code < 0)
768
0
                return code;
769
52.9k
            memcpy(dp + 2, &(cldev->color_space.icc_info),
770
52.9k
                   sizeof(clist_icc_color_t));
771
52.9k
        }
772
53.1k
        dp[1] = cldev->color_space.byte1;
773
53.1k
        pcls->known |= color_space_known;
774
53.1k
    }
775
    /****** HANDLE masks ******/
776
621k
    return 0;
777
621k
}
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
35.1k
{
786
35.1k
    gx_device_clist_writer * const cdev =
787
35.1k
        &((gx_device_clist *)dev)->writer;
788
35.1k
    uint unknown = 0;
789
35.1k
    int ry, rheight, rx, rwidth, y0, y1;
790
35.1k
    gs_logical_operation_t lop = pgs->log_op;
791
35.1k
    byte op = (byte)
792
35.1k
        (params->rule == gx_rule_even_odd ?
793
32.3k
         cmd_opv_eofill : cmd_opv_fill);
794
35.1k
    gs_fixed_point adjust;
795
35.1k
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
796
35.1k
    cmd_rects_enum_t re;
797
35.1k
    int code;
798
799
35.1k
    adjust = params->adjust;
800
35.1k
    {
801
35.1k
        gs_fixed_rect bbox;
802
803
35.1k
        if (ppath != NULL)
804
35.1k
            gx_path_bbox(ppath, &bbox);
805
0
        else {
806
            /* gx_default_fill_path passes the clip path for shfill. */
807
0
            gx_cpath_outer_box(pcpath, &bbox);
808
0
        }
809
35.1k
        ry = fixed2int(bbox.p.y) - 1;
810
35.1k
        rheight = fixed2int_ceiling(bbox.q.y) - ry + 1;
811
35.1k
        crop_fill_y(cdev, ry, rheight);
812
35.1k
        if (rheight <= 0)
813
3.66k
            return 0;
814
31.4k
        if (rheight > cdev->height)
815
0
            return gs_error_limitcheck;
816
31.4k
        rx = fixed2int(bbox.p.x) - 1;
817
31.4k
        rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
818
31.4k
        fit_fill_w(cdev, rx, rwidth);
819
31.4k
    }
820
31.4k
    if ( (cdev->disable_mask & clist_disable_fill_path) ||
821
31.4k
         gs_debug_c(',')
822
31.4k
         ) {
823
        /* Disable path-based banding. */
824
0
        return gx_default_fill_path(dev, pgs, ppath, params, pdcolor,
825
0
                                    pcpath);
826
0
    }
827
31.4k
    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
0
        cdev->cropping_saved = false;
837
0
        code = gx_default_fill_path(dev, pgs, ppath, params, pdcolor, pcpath);
838
0
        if (cdev->cropping_saved) {
839
0
            cdev->cropping_min = cdev->save_cropping_min;
840
0
            cdev->cropping_max = cdev->save_cropping_max;
841
0
            if_debug2m('v', cdev->memory,
842
0
                       "[v] clist_fill_path: restore cropping_min=%d croping_max=%d\n",
843
0
                       cdev->save_cropping_min, cdev->save_cropping_max);
844
0
        }
845
0
        return code;
846
0
    }
847
31.4k
    y0 = ry;
848
31.4k
    y1 = ry + rheight;
849
31.4k
    cmd_check_fill_known(cdev, pgs, params->flatness, &adjust, pcpath,
850
31.4k
                         &unknown);
851
31.4k
    if (unknown)
852
6.02k
        cmd_clear_known(cdev, unknown);
853
31.4k
    if (cdev->permanent_error < 0)
854
0
        return (cdev->permanent_error);
855
31.4k
    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
0
        cdev->cropping_saved = true;
862
0
        cdev->save_cropping_min = cdev->cropping_min;
863
0
        cdev->save_cropping_max = cdev->cropping_max;
864
0
        cdev->cropping_min = max(ry, cdev->cropping_min);
865
0
        cdev->cropping_max = min(ry + rheight, cdev->cropping_max);
866
0
        if_debug2m('v', cdev->memory,
867
0
                   "[v] clist_fill_path: narrow cropping_min=%d croping_max=%d\n",
868
0
                   cdev->save_cropping_min, cdev->save_cropping_max);
869
0
        RECT_ENUM_INIT(re, ry, rheight);
870
0
        do {
871
0
            RECT_STEP_INIT(re);
872
0
            if (pcpath != NULL) {
873
0
                code = cmd_do_write_unknown(cdev, re.pcls, clip_path_known);
874
0
                if (code < 0)
875
0
                    return code;
876
0
            }
877
0
            code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL);
878
0
            if (code  < 0)
879
0
                return code;
880
0
            re.y += re.height;
881
0
        } while (re.y < re.yend);
882
31.4k
    } else {
883
        /* We should not reach here with ppath==NULL (pdcolor != NULL, so not a shading fill */
884
31.4k
        if (ppath == NULL)
885
0
            return_error(gs_error_unregistered);
886
887
        /* If needed, update the trans_bbox */
888
31.4k
        if (cdev->pdf14_needed) {
889
31.4k
            gs_int_rect bbox;
890
891
31.4k
            bbox.p.x = rx;
892
31.4k
            bbox.q.x = rx + rwidth - 1;
893
31.4k
            bbox.p.y = ry;
894
31.4k
            bbox.q.y = ry + rheight - 1;
895
896
31.4k
            clist_update_trans_bbox(cdev, &bbox);
897
31.4k
        }
898
899
31.4k
        RECT_ENUM_INIT(re, ry, rheight);
900
277k
        do {
901
277k
            RECT_STEP_INIT(re);
902
277k
            code = cmd_do_write_unknown(cdev, re.pcls, FILL_KNOWN);
903
277k
            if (code < 0)
904
0
                return code;
905
277k
            if ((code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0 ||
906
277k
                (code = cmd_update_lop(cdev, re.pcls, lop)) < 0
907
277k
                )
908
0
                return code;
909
277k
            code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re,
910
277k
                                         devn_not_tile_fill);
911
277k
            if (code == gs_error_unregistered)
912
0
                return code;
913
277k
            if (code < 0) {
914
                /* Something went wrong, use the default implementation. */
915
4
                return gx_default_fill_path(dev, pgs, ppath, params, pdcolor,
916
4
                                            pcpath);
917
4
            }
918
277k
            re.pcls->color_usage.slow_rop |= slow_rop;
919
277k
            code = cmd_put_path(cdev, re.pcls, ppath,
920
277k
                                int2fixed(max(re.y - 1, y0)),
921
277k
                                int2fixed(min(re.y + re.height + 1, y1)),
922
277k
                                op,
923
277k
                                true, sn_none /* fill doesn't need the notes */ );
924
277k
            if (code < 0)
925
0
                return code;
926
277k
            re.y += re.height;
927
277k
        } while (re.y < re.yend);
928
31.4k
    }
929
31.4k
    return 0;
930
31.4k
}
931
932
int clist_lock_pattern(gx_device * pdev, gs_gstate * pgs, gs_id pattern, int lock)
933
0
{
934
0
    gx_device_clist_writer * const cdev =
935
0
        &((gx_device_clist *)pdev)->writer;
936
0
    byte *dp;
937
0
    int code;
938
939
    /* We need to both lock now, and ensure that we lock on reading this back. */
940
0
    code = gx_pattern_cache_entry_set_lock(pgs, pattern, lock);
941
0
    if (code < 0)
942
0
        return code;
943
944
0
    code = set_cmd_put_all_op(&dp, cdev, cmd_opv_lock_pattern,
945
0
                              1 + 1 + sizeof(pattern));
946
947
0
    if (code < 0)
948
0
        return code;
949
0
    dp[1] = lock;
950
0
    memcpy(dp+2, &pattern, sizeof(pattern));
951
0
    return 0;
952
0
}
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
904
{
963
904
    gx_device_clist_writer * const cdev =
964
904
        &((gx_device_clist *)pdev)->writer;
965
904
    int pattern_size = pgs->line_params.dash.pattern_size;
966
904
    byte op = (byte) (params_fill->rule == gx_rule_even_odd ?
967
823
                  cmd_opv_eofill_stroke : cmd_opv_fill_stroke);
968
904
    uint unknown = 0;
969
904
    gs_fixed_rect bbox;
970
904
    gs_fixed_point expansion;
971
904
    int adjust_y, expansion_code;
972
904
    int ry, rheight;
973
904
    gs_logical_operation_t lop = pgs->log_op;
974
904
    bool slow_rop = cmd_slow_rop(pdev, lop_know_S_0(lop), pdevc_fill);
975
904
    cmd_rects_enum_t re;
976
977
904
    if (pdevc_stroke == NULL || pdevc_fill == NULL)
978
0
        return_error(gs_error_unknownerror);  /* shouldn't happen */
979
980
904
    if ((cdev->disable_mask & (clist_disable_fill_path | clist_disable_stroke_path)) ||
981
904
        gs_debug_c(',')
982
904
        ) {
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
904
    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
904
    gx_path_bbox(ppath, &bbox);
993
    /* We must use the supplied gs_gstate, not our saved one, */
994
    /* for computing the stroke expansion. */
995
904
    expansion_code = gx_stroke_path_expansion(pgs, ppath, &expansion);
996
904
    if (expansion_code < 0) {
997
        /* Expansion is too large: use the entire page. */
998
183
        adjust_y = 0;
999
183
        ry = 0;
1000
183
        rheight = pdev->height;
1001
721
    } else {
1002
721
        adjust_y = fixed2int_ceiling(expansion.y) + 1;
1003
721
        ry = fixed2int(bbox.p.y) - adjust_y;
1004
721
        rheight = fixed2int_ceiling(bbox.q.y) - ry + adjust_y;
1005
721
        fit_fill_y(pdev, ry, rheight);
1006
721
        fit_fill_h(pdev, ry, rheight);
1007
721
        if (rheight <= 0)
1008
4
            return 0;
1009
721
    }
1010
    /* Check the dash pattern, since we bail out if */
1011
    /* the pattern is too large. */
1012
900
    if (cdev->gs_gstate.line_params.dash.pattern_size != pattern_size ||
1013
900
        (pattern_size != 0 &&
1014
898
         memcmp(cdev->dash_pattern, pgs->line_params.dash.pattern,
1015
0
                pattern_size * sizeof(float))) ||
1016
900
        cdev->gs_gstate.line_params.dash.offset !=
1017
898
          pgs->line_params.dash.offset ||
1018
900
        cdev->gs_gstate.line_params.dash.adapt !=
1019
898
          pgs->line_params.dash.adapt ||
1020
900
        cdev->gs_gstate.line_params.dot_length !=
1021
898
          pgs->line_params.dot_length ||
1022
900
        cdev->gs_gstate.line_params.dot_length_absolute !=
1023
898
          pgs->line_params.dot_length_absolute
1024
900
    ) {
1025
        /* Bail out if the dash pattern is too long. */
1026
2
        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
2
        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
2
        cdev->gs_gstate.line_params.dash.pattern = cdev->dash_pattern;
1035
2
        gx_set_dash(&cdev->gs_gstate.line_params.dash,
1036
2
                    pgs->line_params.dash.pattern,
1037
2
                    pgs->line_params.dash.pattern_size,
1038
2
                    pgs->line_params.dash.offset, NULL);
1039
2
        cdev->gs_gstate.line_params.dash.pattern = 0;
1040
2
        gx_set_dash_adapt(&cdev->gs_gstate.line_params.dash,
1041
2
                          pgs->line_params.dash.adapt);
1042
2
        gx_set_dot_length(&cdev->gs_gstate.line_params,
1043
2
                          pgs->line_params.dot_length,
1044
2
                          pgs->line_params.dot_length_absolute);
1045
2
    }
1046
1047
900
    if (state_neq(line_params.start_cap) || state_neq(line_params.join) ||
1048
900
        state_neq(line_params.end_cap) || state_neq(line_params.dash_cap)) {
1049
56
        unknown |= cap_join_known;
1050
56
        state_update(line_params.start_cap);
1051
56
        state_update(line_params.end_cap);
1052
56
        state_update(line_params.dash_cap);
1053
56
        state_update(line_params.join);
1054
56
    }
1055
900
    cmd_check_fill_known(cdev, pgs, params_fill->flatness, &pgs->fill_adjust,
1056
900
                         pcpath, &unknown);
1057
900
    if (state_neq(line_params.half_width)) {
1058
162
        unknown |= line_width_known;
1059
162
        state_update(line_params.half_width);
1060
162
    }
1061
900
    if (state_neq(line_params.miter_limit)) {
1062
3
        unknown |= miter_limit_known;
1063
3
        gx_set_miter_limit(&cdev->gs_gstate.line_params,
1064
3
                           pgs->line_params.miter_limit);
1065
3
    }
1066
900
    if (state_neq(ctm.xx) || state_neq(ctm.xy) ||
1067
900
        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
900
        state_neq(ctm.tx) || state_neq(ctm.ty)
1071
900
        ) {
1072
257
        unknown |= ctm_known;
1073
257
        state_update(ctm);
1074
257
    }
1075
900
    if (unknown)
1076
291
        cmd_clear_known(cdev, unknown);
1077
900
    if (cdev->permanent_error < 0)
1078
0
      return (cdev->permanent_error);
1079
    /* If needed, update the trans_bbox */
1080
900
    if (cdev->pdf14_needed) {
1081
900
        gs_int_rect trans_bbox;
1082
900
        int rx = fixed2int(bbox.p.x) - 1;
1083
900
        int rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
1084
900
        unknown |= STROKE_ALL_KNOWN;
1085
1086
900
        fit_fill_w(cdev, rx, rwidth);
1087
900
        trans_bbox.p.x = rx;
1088
900
        trans_bbox.q.x = rx + rwidth - 1;
1089
900
        trans_bbox.p.y = ry;
1090
900
        trans_bbox.q.y = ry + rheight - 1;
1091
1092
900
        clist_update_trans_bbox(cdev, &trans_bbox);
1093
900
    }
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
900
    if (((pgs->overprint_mode || pgs->overprint || pgs->stroke_overprint))) {
1097
86
        unknown |= op_bm_tk_known;
1098
86
    }
1099
900
    RECT_ENUM_INIT(re, ry, rheight);
1100
46.8k
    do {
1101
46.8k
        int code;
1102
1103
46.8k
        RECT_STEP_INIT(re);
1104
46.8k
        if ((code = cmd_do_write_unknown(cdev, re.pcls, STROKE_ALL_KNOWN | FILL_KNOWN)) < 0)
1105
0
            return code;
1106
46.8k
        if ((code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0)
1107
0
            return code;
1108
46.8k
        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
46.8k
        code = cmd_put_drawing_color(cdev, re.pcls, pdevc_stroke, &re, devn_not_tile_stroke);
1113
46.8k
        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
46.8k
        code = cmd_put_drawing_color(cdev, re.pcls, pdevc_fill, &re, devn_not_tile_fill);
1119
46.8k
        if (code < 0) {
1120
            /* Something went wrong, use the default implementation. */
1121
0
            return gx_default_fill_stroke_path(pdev, pgs, ppath, params_fill, pdevc_fill,
1122
0
                                               params_stroke, pdevc_stroke, pcpath);
1123
0
        }
1124
46.8k
        re.pcls->color_usage.slow_rop |= slow_rop;
1125
1126
        /* Don't skip segments when expansion is unknown.  */
1127
1128
46.8k
        code = cmd_put_path(cdev, re.pcls, ppath, min_fixed, max_fixed,
1129
46.8k
                            op, false, (segment_notes)~0);
1130
46.8k
        if (code < 0)
1131
0
            return code;
1132
46.8k
        re.y += re.height;
1133
46.8k
    } while (re.y < re.yend);
1134
900
    return 0;
1135
900
}
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
35.7k
{
1142
35.7k
    gx_device_clist_writer * const cdev =
1143
35.7k
        &((gx_device_clist *)dev)->writer;
1144
35.7k
    int pattern_size = pgs->line_params.dash.pattern_size;
1145
35.7k
    uint unknown = 0;
1146
35.7k
    gs_fixed_rect bbox;
1147
35.7k
    gs_fixed_point expansion;
1148
35.7k
    int adjust_y, expansion_code;
1149
35.7k
    int ry, rheight;
1150
35.7k
    gs_logical_operation_t lop = pgs->log_op;
1151
35.7k
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
1152
35.7k
    cmd_rects_enum_t re;
1153
1154
35.7k
    CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1155
35.7k
    if ((cdev->disable_mask & clist_disable_stroke_path) ||
1156
35.7k
        gs_debug_c(',')
1157
35.7k
        ) {
1158
        /* Disable path-based banding. */
1159
0
        return gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
1160
0
                                      pcpath);
1161
0
    }
1162
35.7k
    gx_path_bbox(ppath, &bbox);
1163
    /* We must use the supplied gs_gstate, not our saved one, */
1164
    /* for computing the stroke expansion. */
1165
35.7k
    expansion_code = gx_stroke_path_expansion(pgs, ppath, &expansion);
1166
35.7k
    if (expansion_code < 0) {
1167
        /* Expansion is too large: use the entire page. */
1168
785
        adjust_y = 0;
1169
785
        ry = 0;
1170
785
        rheight = dev->height;
1171
34.9k
    } else {
1172
34.9k
        adjust_y = fixed2int_ceiling(expansion.y) + 1;
1173
34.9k
        ry = fixed2int(bbox.p.y) - adjust_y;
1174
34.9k
        rheight = fixed2int_ceiling(bbox.q.y) - ry + adjust_y;
1175
34.9k
        fit_fill_y(dev, ry, rheight);
1176
34.9k
        fit_fill_h(dev, ry, rheight);
1177
34.9k
        if (rheight <= 0)
1178
846
            return 0;
1179
34.9k
    }
1180
    /* Check the dash pattern, since we bail out if */
1181
    /* the pattern is too large. */
1182
34.8k
    if (cdev->gs_gstate.line_params.dash.pattern_size != pattern_size ||
1183
34.8k
        (pattern_size != 0 &&
1184
34.0k
         memcmp(cdev->dash_pattern, pgs->line_params.dash.pattern,
1185
1.44k
                pattern_size * sizeof(float))) ||
1186
34.8k
        cdev->gs_gstate.line_params.dash.offset !=
1187
32.9k
          pgs->line_params.dash.offset ||
1188
34.8k
        cdev->gs_gstate.line_params.dash.adapt !=
1189
32.9k
          pgs->line_params.dash.adapt ||
1190
34.8k
        cdev->gs_gstate.line_params.dot_length !=
1191
32.9k
          pgs->line_params.dot_length ||
1192
34.8k
        cdev->gs_gstate.line_params.dot_length_absolute !=
1193
32.9k
          pgs->line_params.dot_length_absolute
1194
34.8k
    ) {
1195
        /* Bail out if the dash pattern is too long. */
1196
1.88k
        if (pattern_size > cmd_max_dash)
1197
0
            return gx_default_stroke_path(dev, pgs, ppath, params,
1198
0
                                          pdcolor, pcpath);
1199
1.88k
        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.88k
        cdev->gs_gstate.line_params.dash.pattern = cdev->dash_pattern;
1205
1.88k
        gx_set_dash(&cdev->gs_gstate.line_params.dash,
1206
1.88k
                    pgs->line_params.dash.pattern,
1207
1.88k
                    pgs->line_params.dash.pattern_size,
1208
1.88k
                    pgs->line_params.dash.offset, NULL);
1209
1.88k
        cdev->gs_gstate.line_params.dash.pattern = 0;
1210
1.88k
        gx_set_dash_adapt(&cdev->gs_gstate.line_params.dash,
1211
1.88k
                          pgs->line_params.dash.adapt);
1212
1.88k
        gx_set_dot_length(&cdev->gs_gstate.line_params,
1213
1.88k
                          pgs->line_params.dot_length,
1214
1.88k
                          pgs->line_params.dot_length_absolute);
1215
1.88k
    }
1216
1217
34.8k
    if (state_neq(line_params.start_cap) || state_neq(line_params.join) ||
1218
34.8k
        state_neq(line_params.end_cap) || state_neq(line_params.dash_cap)) {
1219
1.27k
        unknown |= cap_join_known;
1220
1.27k
        state_update(line_params.start_cap);
1221
1.27k
        state_update(line_params.end_cap);
1222
1.27k
        state_update(line_params.dash_cap);
1223
1.27k
        state_update(line_params.join);
1224
1.27k
    }
1225
34.8k
    cmd_check_fill_known(cdev, pgs, params->flatness, &pgs->fill_adjust,
1226
34.8k
                         pcpath, &unknown);
1227
34.8k
    if (state_neq(line_params.half_width)) {
1228
1.72k
        unknown |= line_width_known;
1229
1.72k
        state_update(line_params.half_width);
1230
1.72k
    }
1231
34.8k
    if (state_neq(line_params.miter_limit)) {
1232
25
        unknown |= miter_limit_known;
1233
25
        gx_set_miter_limit(&cdev->gs_gstate.line_params,
1234
25
                           pgs->line_params.miter_limit);
1235
25
    }
1236
34.8k
    if (state_neq(ctm.xx) || state_neq(ctm.xy) ||
1237
34.8k
        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
34.8k
        state_neq(ctm.tx) || state_neq(ctm.ty)
1241
34.8k
        ) {
1242
25.5k
        unknown |= ctm_known;
1243
25.5k
        state_update(ctm);
1244
25.5k
    }
1245
34.8k
    if (unknown)
1246
29.6k
        cmd_clear_known(cdev, unknown);
1247
34.8k
    if (cdev->permanent_error < 0)
1248
0
      return (cdev->permanent_error);
1249
    /* If needed, update the trans_bbox */
1250
34.8k
    if (cdev->pdf14_needed) {
1251
34.8k
        gs_int_rect trans_bbox;
1252
34.8k
        int rx = fixed2int(bbox.p.x) - 1;
1253
34.8k
        int rwidth = fixed2int_ceiling(bbox.q.x) - rx + 1;
1254
1255
34.8k
        fit_fill_w(cdev, rx, rwidth);
1256
34.8k
        trans_bbox.p.x = rx;
1257
34.8k
        trans_bbox.q.x = rx + rwidth - 1;
1258
34.8k
        trans_bbox.p.y = ry;
1259
34.8k
        trans_bbox.q.y = ry + rheight - 1;
1260
1261
34.8k
        clist_update_trans_bbox(cdev, &trans_bbox);
1262
34.8k
    }
1263
34.8k
    RECT_ENUM_INIT(re, ry, rheight);
1264
564k
    do {
1265
564k
        int code;
1266
1267
564k
        RECT_STEP_INIT(re);
1268
564k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1269
564k
        if ((code = cmd_do_write_unknown(cdev, re.pcls, STROKE_ALL_KNOWN)) < 0 ||
1270
564k
            (code = cmd_do_enable_clip(cdev, re.pcls, pcpath != NULL)) < 0 ||
1271
564k
            (code = cmd_update_lop(cdev, re.pcls, lop)) < 0
1272
564k
            )
1273
0
            return code;
1274
564k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1275
564k
        code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, devn_not_tile_stroke);
1276
564k
            if (code == gs_error_unregistered)
1277
0
                return code;
1278
564k
        if (code < 0) {
1279
            /* Something went wrong, use the default implementation. */
1280
0
            cdev->cropping_saved = false;
1281
0
            code = gx_default_stroke_path(dev, pgs, ppath, params, pdcolor,
1282
0
                                          pcpath);
1283
0
            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
0
            return code;
1291
0
        }
1292
564k
        re.pcls->color_usage.slow_rop |= slow_rop;
1293
564k
        CMD_CHECK_LAST_OP_BLOCK_DEFINED(cdev);
1294
564k
        {
1295
564k
            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
564k
            if (pattern_size || expansion_code < 0 ) {
1305
262k
                ymin = min_fixed;
1306
262k
                ymax = max_fixed;
1307
301k
            } else {
1308
301k
                ymin = int2fixed(re.y - adjust_y);
1309
301k
                ymax = int2fixed(re.y + re.height + adjust_y);
1310
301k
            }
1311
564k
            code = cmd_put_path(cdev, re.pcls, ppath, ymin, ymax,
1312
564k
                                cmd_opv_stroke,
1313
564k
                                false, (segment_notes)~0);
1314
564k
            if (code < 0)
1315
0
                return code;
1316
564k
        }
1317
564k
        re.y += re.height;
1318
564k
    } while (re.y < re.yend);
1319
34.8k
    return 0;
1320
34.8k
}
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
200
{
1334
200
    gx_path path;
1335
200
    gs_memory_t *mem = dev->memory;
1336
200
    int code;
1337
200
    gx_device_clist_writer * const cdev =
1338
200
        &((gx_device_clist *)dev)->writer;
1339
200
    gs_fixed_rect bbox;
1340
200
    int ry, rheight, y0, y1;
1341
200
    bool slow_rop = cmd_slow_rop(dev, lop_know_S_0(lop), pdcolor);
1342
200
    cmd_rects_enum_t re;
1343
1344
200
    if (gs_debug_c(','))
1345
0
        return -1;   /* path-based banding is disabled */
1346
200
    gx_path_init_local(&path, mem);
1347
200
    if ((code = gx_path_add_point(&path, px, py)) < 0 ||
1348
200
        (code = gx_path_add_lines(&path, points, num_points)) < 0
1349
200
        )
1350
0
        goto out;
1351
200
    gx_path_bbox(&path, &bbox);
1352
200
    ry = fixed2int(bbox.p.y) - 1;
1353
200
    rheight = fixed2int_ceiling(bbox.q.y) - ry + 1;
1354
200
    fit_fill_y(dev, ry, rheight);
1355
200
    fit_fill_h(dev, ry, rheight);
1356
200
    if (rheight <= 0)
1357
0
        return 0;
1358
200
    y0 = ry;
1359
200
    y1 = ry + rheight;
1360
200
    if (cdev->permanent_error < 0)
1361
0
      return (cdev->permanent_error);
1362
    /* If needed, update the trans_bbox */
1363
200
    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
200
    RECT_ENUM_INIT(re, ry, rheight);
1377
200
    do {
1378
200
        RECT_STEP_INIT(re);
1379
200
        if ((code = cmd_update_lop(cdev, re.pcls, lop)) < 0 ||
1380
200
            (code = cmd_put_drawing_color(cdev, re.pcls, pdcolor, &re, devn_not_tile_fill)) < 0)
1381
0
            goto out;
1382
200
        re.pcls->color_usage.slow_rop |= slow_rop;
1383
200
        code = cmd_put_path(cdev, re.pcls, &path,
1384
200
                            int2fixed(max(re.y - 1, y0)),
1385
200
                            int2fixed(min(re.y + re.height + 1, y1)),
1386
200
                            cmd_opv_polyfill,
1387
200
                            true, sn_none /* fill doesn't need the notes */ );
1388
200
        if (code < 0)
1389
0
            goto out;
1390
200
        re.y += re.height;
1391
200
    } while (re.y < re.yend);
1392
200
out:
1393
200
    gx_path_free(&path, "clist_put_polyfill");
1394
200
    return code;
1395
200
}
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
15.9k
{
1403
15.9k
    gs_fixed_point pts[3];
1404
15.9k
    int code;
1405
1406
15.9k
    if (PARALLELOGRAM_IS_RECT(ax, ay, bx, by)) {
1407
15.7k
        gs_int_rect r;
1408
1409
15.7k
        INT_RECT_FROM_PARALLELOGRAM(&r, px, py, ax, ay, bx, by);
1410
15.7k
        return gx_fill_rectangle_device_rop(r.p.x, r.p.y, r.q.x - r.p.x,
1411
15.7k
                                            r.q.y - r.p.y, pdcolor, dev, lop);
1412
15.7k
    }
1413
200
    pts[0].x = px + ax, pts[0].y = py + ay;
1414
200
    pts[1].x = pts[0].x + bx, pts[1].y = pts[0].y + by;
1415
200
    pts[2].x = px + bx, pts[2].y = py + by;
1416
200
    code = clist_put_polyfill(dev, px, py, pts, 3, pdcolor, lop);
1417
200
    return (code >= 0 ? code :
1418
200
            gx_default_fill_parallelogram(dev, px, py, ax, ay, bx, by,
1419
0
                                          pdcolor, lop));
1420
15.9k
}
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
4.86M
{
1460
4.86M
    const fixed *optr = operands;
1461
    /* Fetch num_operands before possible command merging. */
1462
4.86M
    static const byte op_num_operands[] = {
1463
4.86M
        cmd_segment_op_num_operands_values
1464
4.86M
    };
1465
4.86M
    int i = op_num_operands[op & 0xf];
1466
    /* One picky compiler complains if we initialize to psw->cmd - 1. */
1467
4.86M
    byte *q = psw->cmd;
1468
1469
4.86M
    --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
4.86M
    if (op == cmd_opv_rlineto) {
1485
2.92M
        if (operands[0] == 0)
1486
1.74M
            op = cmd_opv_vlineto, optr = ++operands, i = 1;
1487
1.18M
        else if (operands[1] == 0)
1488
710k
            op = cmd_opv_hlineto, i = 1;
1489
471k
        else
1490
471k
            switch (*psw->dp) {
1491
173k
                case cmd_opv_rmoveto:
1492
173k
                    psw->delta_first.x = operands[0];
1493
173k
                    psw->delta_first.y = operands[1];
1494
173k
                    op = cmd_opv_rmlineto;
1495
226k
                  merge:cmd_uncount_op(*psw->dp, psw->len);
1496
226k
                    cmd_shorten_op(psw->cldev, psw->pcls, psw->len);  /* delete it */
1497
226k
                    q += psw->len - 1;
1498
226k
                    break;
1499
52.9k
                case cmd_opv_rmlineto:
1500
52.9k
                    if (notes != psw->notes)
1501
0
                        break;
1502
52.9k
                    op = cmd_opv_rm2lineto;
1503
52.9k
                    goto merge;
1504
16.1k
                case cmd_opv_rm2lineto:
1505
16.1k
                    if (notes != psw->notes)
1506
0
                        break;
1507
16.1k
                    if (operands[0] == -psw->delta_first.x &&
1508
16.1k
                        operands[1] == -psw->delta_first.y
1509
16.1k
                        ) {
1510
743
                        cmd_uncount_op(cmd_opv_rm2lineto, psw->len);
1511
743
                        *psw->dp = cmd_count_op(cmd_opv_rm3lineto, psw->len, psw->cldev->memory);
1512
743
                        return 0;
1513
743
                    }
1514
15.4k
                    break;
1515
228k
                default:
1516
228k
                    ;
1517
471k
            }
1518
2.92M
    }
1519
11.1M
    for (; --i >= 0; ++optr) {
1520
6.23M
        fixed d = *optr, d2;
1521
1522
6.23M
        if (is_bits(d, _fixed_shift + 11) &&
1523
6.23M
            !(d & (float2fixed(0.25) - 1))
1524
6.23M
            ) {
1525
2.57M
            cmd_count_add1(stats_cmd_diffs[3]);
1526
2.57M
            d = ((d >> (_fixed_shift - 2)) & 0x1fff) + 0xc000;
1527
2.57M
            q += 2;
1528
3.66M
        } else if (is_bits(d, 19) && i > 0 && is_bits(d2 = optr[1], 19)) {
1529
1.00M
            cmd_count_add1(stats_cmd_diffs[0]);
1530
1.00M
            q[1] = (byte) ((d >> 13) & 0x3f);
1531
1.00M
            q[2] = (byte) (d >> 5);
1532
1.00M
            q[3] = (byte) ((d << 3) + ((d2 >> 16) & 7));
1533
1.00M
            q[4] = (byte) (d2 >> 8);
1534
1.00M
            q[5] = (byte) d2;
1535
1.00M
            q += 5;
1536
1.00M
            --i, ++optr;
1537
1.00M
            continue;
1538
2.66M
        } else if (is_bits(d, 22)) {
1539
2.15M
            cmd_count_add1(stats_cmd_diffs[1]);
1540
2.15M
            q[1] = (byte) (((d >> 16) & 0x3f) + 0x40);
1541
2.15M
            q += 3;
1542
2.15M
        } else if (is_bits(d, 30)) {
1543
242k
            cmd_count_add1(stats_cmd_diffs[2]);
1544
242k
            q[1] = (byte) (((d >> 24) & 0x3f) + 0x80);
1545
242k
            q[2] = (byte) (d >> 16);
1546
242k
            q += 4;
1547
265k
        } else {
1548
265k
            int b;
1549
1550
265k
            cmd_count_add1(stats_cmd_diffs[4]);
1551
265k
            *++q = 0xe0;
1552
797k
            for (b = sizeof(fixed) - 1; b > 1; --b)
1553
531k
                *++q = (byte) (d >> (b * 8));
1554
265k
            q += 2;
1555
265k
        }
1556
5.23M
        q[-1] = (byte) (d >> 8);
1557
5.23M
        *q = (byte) d;
1558
5.23M
    }
1559
4.86M
    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
4.86M
    } {
1570
4.86M
        int len = q + 2 - psw->cmd;
1571
4.86M
        byte *dp;
1572
4.86M
        int code = set_cmd_put_op(&dp, psw->cldev, psw->pcls, op, len);
1573
1574
4.86M
        if (code < 0)
1575
0
            return code;
1576
4.86M
        memcpy(dp + 1, psw->cmd, len - 1);
1577
4.86M
        psw->len = len;
1578
4.86M
        psw->dp = dp;
1579
4.86M
    }
1580
0
    return 0;
1581
4.86M
}
1582
/* Put out a line segment command. */
1583
#define cmd_put_rmoveto(psw, operands)\
1584
1.03M
  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
2.92M
  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
776k
{
1619
776k
    return (out.y >= max_fixed/2 || out.y <= -(max_fixed/2) || out.x >= max_fixed/2 || out.x <= -(max_fixed/2));
1620
776k
}
1621
1622
static void
1623
reduce_line(fixed *m0, fixed *m1, fixed x0, fixed y0, fixed x1, fixed y1)
1624
22.4k
{
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
22.4k
    *m0 = (x0/4) + 3*(x1/4);
1628
22.4k
    *m1 = (y0/4) + 3*(y1/4);
1629
22.4k
}
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
1.01M
{
1640
1.01M
    gs_path_enum cenum;
1641
1.01M
    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
1.01M
    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
7.92M
#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
1.01M
    gs_fixed_point start;
1673
1.01M
    int start_side = 0x7badf00d; /* Initialize against indeterminizm. */
1674
1675
    /*
1676
     * Whether any lines or curves were skipped immediately
1677
     * following the moveto:
1678
     */
1679
1.01M
    bool start_skip = 0x7badf00d; /* Initialize against indeterminizm. */
1680
1681
    /* The side of the last point: */
1682
1.01M
    int side = 0x7badf00d; /* Initialize against indeterminizm. */
1683
1684
    /* The last point with side != 0: */
1685
1.01M
    gs_fixed_point out;
1686
1687
    /* If the last out-going segment was a lineto, */
1688
    /* its notes: */
1689
1.01M
    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
1.01M
    fixed px = int2fixed(pcls->rect.x);
1697
1.01M
    fixed py = int2fixed(pcls->rect.y);
1698
1699
    /* The point of the last emitted moveto: */
1700
1.01M
    gs_fixed_point first;
1701
1702
    /* Information about the last emitted operation: */
1703
1.01M
    int open = 0;   /* -1 if last was moveto, 1 if line/curveto, */
1704
                                /* 0 if newpath/closepath */
1705
1.01M
    struct { fixed vs[6]; } prev = { { 0 } };
1706
1707
1.01M
    first.x = first.y = out.x = out.y = start.x = start.y = 0; /* Quiet gcc warning. */
1708
1.01M
    if_debug4m('p', cldev->memory, "[p]initial (%g,%g), clip [%g..%g)\n",
1709
1.01M
               fixed2float(px), fixed2float(py),
1710
1.01M
               fixed2float(ymin), fixed2float(ymax));
1711
1.01M
    gx_path_enum_init(&cenum, ppath);
1712
1.01M
    writer.cldev = cldev;
1713
1.01M
    writer.pcls = pcls;
1714
1.01M
    writer.notes = sn_none;
1715
2.11M
#define set_first_point() (writer.dp = &initial_op)
1716
1.01M
#define first_point() (writer.dp == &initial_op)
1717
1.01M
    set_first_point();
1718
10.6M
    for (;;) {
1719
10.6M
        fixed vs[6];
1720
1721
15.2M
#define A vs[0]
1722
16.5M
#define B vs[1]
1723
10.6M
#define C vs[2]
1724
10.6M
#define D vs[3]
1725
10.6M
#define E vs[4]
1726
10.6M
#define F vs[5]
1727
10.6M
        int pe_op = gx_path_enum_next(&cenum, (gs_fixed_point *) vs);
1728
10.6M
        byte *dp;
1729
10.6M
        int code;
1730
1731
10.6M
        switch (pe_op) {
1732
1.03M
            case 0:
1733
                /* If the path is open and needs an implicit close, */
1734
                /* do the close and then come here again. */
1735
1.03M
                if (open > 0 && implicit_close)
1736
16.5k
                    goto close;
1737
                /* All done. */
1738
1.01M
                pcls->rect.x = fixed2int_var(px);
1739
1.01M
                pcls->rect.y = fixed2int_var(py);
1740
1.01M
                if_debug2m('p', cldev->memory, "[p]final (%d,%d)\n",
1741
1.01M
                           pcls->rect.x, pcls->rect.y);
1742
1.01M
                return set_cmd_put_op(&dp, cldev, pcls, path_op, 1);
1743
1.84M
            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
1.84M
                if (open > 0 && implicit_close) {
1747
6.43k
                    gx_path_enum_backup(&cenum);
1748
6.43k
                    goto close;
1749
6.43k
                }
1750
1.84M
                open = -1;
1751
1.84M
                start.x = A, start.y = B;
1752
1.84M
                start_skip = false;
1753
1.84M
                if ((start_side = side = which_side(B)) != 0) {
1754
1.29M
                    out.x = A, out.y = B;
1755
1.29M
                    if_debug3m('p', cldev->memory, "[p]skip moveto (%g,%g) side %d\n",
1756
1.29M
                               fixed2float(out.x), fixed2float(out.y),
1757
1.29M
                               side);
1758
1.29M
                    continue;
1759
1.29M
                }
1760
546k
                C = A - px, D = B - py;
1761
546k
                first.x = px = A, first.y = py = B;
1762
546k
                code = cmd_put_rmoveto(&writer, &C);
1763
546k
                if_debug2m('p', cldev->memory, "[p]moveto (%g,%g)\n",
1764
546k
                           fixed2float(px), fixed2float(py));
1765
546k
                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
5.53M
            case gs_pe_lineto:
1823
5.53M
                {
1824
5.53M
                    int next_side = which_side(B);
1825
5.53M
                    segment_notes notes =
1826
5.53M
                    gx_path_enum_notes(&cenum) & keep_notes;
1827
1828
5.53M
                    if (next_side == side && side != 0) { /* Skip a line completely outside the clip region. */
1829
3.21M
                        if (open < 0)
1830
2.55M
                            start_skip = true;
1831
3.21M
                        out.x = A, out.y = B;
1832
3.21M
                        out_notes = notes;
1833
3.21M
                        if_debug3m('p', cldev->memory, "[p]skip lineto (%g,%g) side %d\n",
1834
3.21M
                                   fixed2float(out.x), fixed2float(out.y),
1835
3.21M
                                   side);
1836
3.21M
                        continue;
1837
3.21M
                    }
1838
                    /* If we skipped any segments, put out a moveto/lineto. */
1839
2.31M
                    if (side && ((open < 0) || (px != out.x || py != out.y || first_point()))) {
1840
507k
                        if (far_out(out)) {
1841
                            /* out is far enough out that we have to worry about wrapping on playback. Reduce the extent. */
1842
17.3k
                            if (open >= 0) {
1843
2.89k
                                fixed mid[2];
1844
2.89k
                                fixed m0, m1;
1845
2.89k
                                reduce_line(&m0, &m1, out.x, out.y, px, py);
1846
2.89k
                                mid[0] = m0 - px, mid[1] = m1 - py;
1847
2.89k
                                code = cmd_put_rlineto(&writer, mid, out_notes);
1848
2.89k
                                if (code < 0)
1849
0
                                    return code;
1850
2.89k
                                px = m0, py = m1;
1851
2.89k
                            }
1852
17.3k
                            reduce_line(&out.x, &out.y, out.x, out.y, A, B);
1853
17.3k
                        }
1854
507k
                        C = out.x - px, D = out.y - py;
1855
507k
                        if (open < 0) {
1856
457k
                            first = out;
1857
457k
                            code = cmd_put_rmoveto(&writer, &C);
1858
457k
                        } else
1859
50.4k
                            code = cmd_put_rlineto(&writer, &C, out_notes);
1860
507k
                        if (code < 0)
1861
0
                            return code;
1862
507k
                        px = out.x, py = out.y;
1863
507k
                        if_debug3m('p', cldev->memory, "[p]catchup %s (%g,%g) for line\n",
1864
507k
                                   (open < 0 ? "moveto" : "lineto"),
1865
507k
                                   fixed2float(px), fixed2float(py));
1866
507k
                    }
1867
2.31M
                    if ((side = next_side) != 0) { /* Note a vertex going outside the clip region. */
1868
538k
                        out.x = A, out.y = B;
1869
538k
                    }
1870
2.31M
                    C = A - px, D = B - py;
1871
2.31M
                    px = A, py = B;
1872
2.31M
                    open = 1;
1873
2.31M
                    code = cmd_put_rlineto(&writer, &C, notes);
1874
2.31M
                }
1875
2.31M
                if_debug3m('p', cldev->memory, "[p]lineto (%g,%g) side %d\n",
1876
2.31M
                           fixed2float(px), fixed2float(py), side);
1877
2.31M
                break;
1878
1.31M
            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
1.34M
              close:if (side != start_side) { /* If we skipped any segments, put out a moveto/lineto. */
1900
329k
                    if (side && (px != out.x || py != out.y || first_point())) {
1901
269k
                        if (far_out(out)) {
1902
                            /* out is far enough out that we have to worry about wrapping on playback. Reduce the extent. */
1903
1.10k
                            if (open >= 0) {
1904
1.10k
                                fixed mid[2];
1905
1.10k
                                fixed m0, m1;
1906
1.10k
                                reduce_line(&m0, &m1, out.x, out.y, px, py);
1907
1.10k
                                mid[0] = m0 - px, mid[1] = m1 - py;
1908
1.10k
                                code = cmd_put_rlineto(&writer, mid, out_notes);
1909
1.10k
                                if (code < 0)
1910
0
                                    return code;
1911
1.10k
                                px = m0, py = m1;
1912
1.10k
                            }
1913
1.10k
                            reduce_line(&out.x, &out.y, out.x, out.y, A, B);
1914
1.10k
                        }
1915
269k
                        C = out.x - px, D = out.y - py;
1916
269k
                        code = cmd_put_rlineto(&writer, &C, out_notes);
1917
269k
                        if (code < 0)
1918
0
                            return code;
1919
269k
                        px = out.x, py = out.y;
1920
269k
                        if_debug2m('p', cldev->memory, "[p]catchup line (%g,%g) for close\n",
1921
269k
                                   fixed2float(px), fixed2float(py));
1922
269k
                    }
1923
329k
                    if (open > 0 && start_skip) { /* Draw the closing line back to the start. */
1924
273k
                        C = start.x - px, D = start.y - py;
1925
273k
                        code = cmd_put_rlineto(&writer, &C, sn_none);
1926
273k
                        if (code < 0)
1927
0
                            return code;
1928
273k
                        px = start.x, py = start.y;
1929
273k
                        if_debug2m('p', cldev->memory, "[p]draw close to (%g,%g)\n",
1930
273k
                                   fixed2float(px), fixed2float(py));
1931
273k
                    }
1932
329k
                }
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
1.34M
                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.10M
                    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.10M
                    if (side != 0) {
1955
1.00M
                        open = 0;
1956
1.00M
                        continue;
1957
1.00M
                    }
1958
1.10M
                }
1959
336k
                open = 0;
1960
336k
                px = first.x, py = first.y;
1961
336k
                code = cmd_put_segment(&writer, cmd_opv_closepath, &A, sn_none);
1962
336k
                if_debug0m('p', cldev->memory, "[p]close\n");
1963
336k
                break;
1964
894k
            case gs_pe_curveto:
1965
894k
                {
1966
894k
                    segment_notes notes =
1967
894k
                    gx_path_enum_notes(&cenum) & keep_notes;
1968
1969
894k
                    {
1970
894k
                        fixed bpy, bqy;
1971
894k
                        int all_side, out_side;
1972
1973
                        /* Compute the Y bounds for the clipping check. */
1974
894k
                        if (B < D)
1975
241k
                            bpy = B, bqy = D;
1976
652k
                        else
1977
652k
                            bpy = D, bqy = B;
1978
894k
                        if (F < bpy)
1979
190k
                            bpy = F;
1980
703k
                        else if (F > bqy)
1981
192k
                            bqy = F;
1982
894k
                        all_side = (bqy < ymin ? -1 : bpy > ymax ? 1 : 0);
1983
894k
                        if (all_side != 0) {
1984
345k
                            if (all_side == side) { /* Skip a curve entirely outside the clip region. */
1985
329k
                                if (open < 0)
1986
234k
                                    start_skip = true;
1987
329k
                                out.x = E, out.y = F;
1988
329k
                                out_notes = notes;
1989
329k
                                if_debug3m('p', cldev->memory,
1990
329k
                                           "[p]skip curveto (%g,%g) side %d\n",
1991
329k
                                           fixed2float(out.x), fixed2float(out.y),
1992
329k
                                           side);
1993
329k
                                continue;
1994
329k
                            }
1995
15.9k
                            out_side = all_side;
1996
15.9k
                        } else
1997
549k
                            out_side = which_side(F);
1998
                        /* If we skipped any segments, put out a moveto/lineto. */
1999
565k
                        if (side && ((open < 0) || (px != out.x || py != out.y || first_point()))) {
2000
48.0k
                            fixed diff[2];
2001
2002
48.0k
                            diff[0] = out.x - px, diff[1] = out.y - py;
2003
48.0k
                            if (open < 0) {
2004
35.5k
                                first = out;
2005
35.5k
                                code = cmd_put_rmoveto(&writer, diff);
2006
35.5k
                            } else
2007
12.5k
                                code = cmd_put_rlineto(&writer, diff, out_notes);
2008
48.0k
                            if (code < 0)
2009
0
                                return code;
2010
48.0k
                            px = out.x, py = out.y;
2011
48.0k
                            if_debug3m('p', cldev->memory,
2012
48.0k
                                       "[p]catchup %s (%g,%g) for curve\n",
2013
48.0k
                                       (open < 0 ? "moveto" : "lineto"),
2014
48.0k
                                       fixed2float(px), fixed2float(py));
2015
48.0k
                        }
2016
565k
                        if ((side = out_side) != 0) { /* Note a vertex going outside the clip region. */
2017
66.4k
                            out.x = E, out.y = F;
2018
66.4k
                        }
2019
565k
                    }
2020
0
                    {
2021
565k
                        fixed nx = E, ny = F;
2022
565k
                        const fixed *optr = vs;
2023
565k
                        byte op;
2024
2025
565k
                        if_debug7m('p', cldev->memory,
2026
565k
                                   "[p]curveto (%g,%g; %g,%g; %g,%g) side %d\n",
2027
565k
                                   fixed2float(A), fixed2float(B),
2028
565k
                                   fixed2float(C), fixed2float(D),
2029
565k
                                   fixed2float(E), fixed2float(F), side);
2030
565k
                        E -= C, F -= D;
2031
565k
                        C -= A, D -= B;
2032
565k
                        A -= px, B -= py;
2033
565k
                        if (*writer.dp >= cmd_opv_min_curveto &&
2034
565k
                            *writer.dp <= cmd_opv_max_curveto &&
2035
565k
                            ((prev.A == 0 &&
2036
87.6k
                              A == prev.E && C == prev.C && E == prev.A &&
2037
87.6k
                              B == -prev.F && D == -prev.D && F == -prev.B) ||
2038
87.6k
                             (prev.A != 0 &&
2039
84.4k
                              A == -prev.E && C == -prev.C && E == -prev.A &&
2040
84.4k
                              B == prev.F && D == prev.D && F == prev.B))
2041
565k
                            )
2042
3.25k
                            op = cmd_opv_scurveto;
2043
562k
                        else if (A == 0 && F == 0) {
2044
397k
                            optr++, op = cmd_opv_vhcurveto;
2045
397k
                            if ((B ^ C) >= 0) {
2046
390k
                                if (D == C && E == B)
2047
371k
                                    op = cmd_opv_vqcurveto;
2048
390k
                            } else if (D == -C && E == -B)
2049
997
                                op = cmd_opv_vqcurveto;
2050
397k
                        } else if (B == 0 && E == 0) {
2051
26.6k
                            B = A, E = F, optr++, op = cmd_opv_hvcurveto;
2052
26.6k
                            if ((B ^ D) >= 0) {
2053
6.08k
                                if (C == D && E == B)
2054
1.00k
                                    op = cmd_opv_hqcurveto;
2055
20.5k
                            } else if (C == -D && E == -B)
2056
2.88k
                                C = D, op = cmd_opv_hqcurveto;
2057
26.6k
                        }
2058
137k
                        else if (A == 0 && B == 0)
2059
813
                            optr += 2, op = cmd_opv_nrcurveto;
2060
136k
                        else if (E == 0 && F == 0)
2061
1.40k
                            op = cmd_opv_rncurveto;
2062
135k
                        else
2063
135k
                            op = cmd_opv_rrcurveto;
2064
565k
                        memcpy(prev.vs, vs, sizeof(prev.vs));
2065
565k
                        px = nx, py = ny;
2066
565k
                        open = 1;
2067
565k
                        code = cmd_put_segment(&writer, op, optr, notes);
2068
565k
                    }
2069
565k
                }
2070
0
                break;
2071
0
            default:
2072
0
                return_error(gs_error_rangecheck);
2073
10.6M
        }
2074
3.76M
        if (code < 0)
2075
0
            return code;
2076
3.76M
#undef A
2077
3.76M
#undef B
2078
3.76M
#undef C
2079
3.76M
#undef D
2080
3.76M
#undef E
2081
3.76M
#undef F
2082
3.76M
    }
2083
1.01M
}