Coverage Report

Created: 2026-10-28 08:05

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