Coverage Report

Created: 2025-06-10 06:58

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