Coverage Report

Created: 2022-10-31 07:00

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