Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/gxshade1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Rendering for non-mesh shadings */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsmatrix.h"   /* for gscoord.h */
23
#include "gscoord.h"
24
#include "gspath.h"
25
#include "gsptype2.h"
26
#include "gxcspace.h"
27
#include "gxdcolor.h"
28
#include "gxfarith.h"
29
#include "gxfixed.h"
30
#include "gxgstate.h"
31
#include "gxpath.h"
32
#include "gxshade.h"
33
#include "gxdevcli.h"
34
#include "gxshade4.h"
35
#include "gsicc_cache.h"
36
37
/* ---------------- Function-based shading ---------------- */
38
39
typedef struct Fb_frame_s { /* A rudiment of old code. */
40
    gs_rect region;
41
    gs_client_color cc[4];  /* colors at 4 corners */
42
    int state;
43
} Fb_frame_t;
44
45
typedef struct Fb_fill_state_s {
46
    shading_fill_state_common;
47
    const gs_shading_Fb_t *psh;
48
    gs_matrix_fixed ptm;  /* parameter space -> device space */
49
    Fb_frame_t frame;
50
} Fb_fill_state_t;
51
/****** NEED GC DESCRIPTOR ******/
52
53
static inline void
54
make_other_poles(patch_curve_t curve[4])
55
220k
{
56
220k
    int i, j;
57
58
1.10M
    for (i = 0; i < 4; i++) {
59
883k
        j = (i + 1) % 4;
60
883k
        curve[i].control[0].x = (curve[i].vertex.p.x * 2 + curve[j].vertex.p.x) / 3;
61
883k
        curve[i].control[0].y = (curve[i].vertex.p.y * 2 + curve[j].vertex.p.y) / 3;
62
883k
        curve[i].control[1].x = (curve[i].vertex.p.x + curve[j].vertex.p.x * 2) / 3;
63
883k
        curve[i].control[1].y = (curve[i].vertex.p.y + curve[j].vertex.p.y * 2) / 3;
64
883k
        curve[i].straight = true;
65
883k
    }
66
220k
}
67
68
/* Transform a point with a fixed-point result. */
69
static void
70
gs_point_transform2fixed_clamped(const gs_matrix_fixed * pmat,
71
                         double x, double y, gs_fixed_point * ppt)
72
0
{
73
0
    gs_point fpt;
74
75
0
    gs_point_transform(x, y, (const gs_matrix *)pmat, &fpt);
76
0
    ppt->x = clamp_coord(fpt.x);
77
0
    ppt->y = clamp_coord(fpt.y);
78
0
}
79
80
static int
81
Fb_fill_region(Fb_fill_state_t * pfs, const gs_fixed_rect *rect)
82
0
{
83
0
    patch_fill_state_t pfs1;
84
0
    patch_curve_t curve[4];
85
0
    Fb_frame_t * fp = &pfs->frame;
86
0
    int code;
87
88
0
    memcpy(&pfs1, (shading_fill_state_t *)pfs, sizeof(shading_fill_state_t));
89
0
    pfs1.Function = pfs->psh->params.Function;
90
0
    code = init_patch_fill_state(&pfs1);
91
0
    if (code < 0)
92
0
        return code;
93
0
    pfs1.maybe_self_intersecting = false;
94
0
    pfs1.n_color_args = 2;
95
0
    pfs1.rect = *rect;
96
0
    gs_point_transform2fixed(&pfs->ptm, fp->region.p.x, fp->region.p.y, &curve[0].vertex.p);
97
0
    gs_point_transform2fixed(&pfs->ptm, fp->region.q.x, fp->region.p.y, &curve[1].vertex.p);
98
0
    gs_point_transform2fixed(&pfs->ptm, fp->region.q.x, fp->region.q.y, &curve[2].vertex.p);
99
0
    gs_point_transform2fixed(&pfs->ptm, fp->region.p.x, fp->region.q.y, &curve[3].vertex.p);
100
0
    make_other_poles(curve);
101
0
    curve[0].vertex.cc[0] = fp->region.p.x;   curve[0].vertex.cc[1] = fp->region.p.y;
102
0
    curve[1].vertex.cc[0] = fp->region.q.x;   curve[1].vertex.cc[1] = fp->region.p.y;
103
0
    curve[2].vertex.cc[0] = fp->region.q.x;   curve[2].vertex.cc[1] = fp->region.q.y;
104
0
    curve[3].vertex.cc[0] = fp->region.p.x;   curve[3].vertex.cc[1] = fp->region.q.y;
105
0
    code = patch_fill(&pfs1, curve, NULL, NULL);
106
0
    if (term_patch_fill_state(&pfs1))
107
0
        return_error(gs_error_unregistered); /* Must not happen. */
108
0
    return code;
109
0
}
110
111
int
112
gs_shading_Fb_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
113
                             const gs_fixed_rect * rect_clip,
114
                             gx_device * dev, gs_gstate * pgs)
115
0
{
116
0
    const gs_shading_Fb_t * const psh = (const gs_shading_Fb_t *)psh0;
117
0
    gs_matrix save_ctm;
118
0
    int xi, yi, code;
119
0
    float x[2], y[2];
120
0
    Fb_fill_state_t state;
121
122
0
    code = shade_init_fill_state((shading_fill_state_t *) & state, psh0, dev, pgs);
123
0
    if (code < 0)
124
0
        return code;
125
0
    state.psh = psh;
126
    /****** HACK FOR FIXED-POINT MATRIX MULTIPLY ******/
127
0
    gs_currentmatrix((gs_gstate *) pgs, &save_ctm);
128
0
    gs_concat((gs_gstate *) pgs, &psh->params.Matrix);
129
0
    state.ptm = pgs->ctm;
130
0
    gs_setmatrix((gs_gstate *) pgs, &save_ctm);
131
    /* Compute the parameter X and Y ranges. */
132
0
    {
133
0
        gs_rect pbox;
134
135
0
        code = gs_bbox_transform_inverse(rect, &psh->params.Matrix, &pbox);
136
0
        if (code < 0)
137
0
            return code;
138
0
        x[0] = max(pbox.p.x, psh->params.Domain[0]);
139
0
        x[1] = min(pbox.q.x, psh->params.Domain[1]);
140
0
        y[0] = max(pbox.p.y, psh->params.Domain[2]);
141
0
        y[1] = min(pbox.q.y, psh->params.Domain[3]);
142
0
    }
143
0
    if (x[0] > x[1] || y[0] > y[1]) {
144
        /* The region is outside the shading area. */
145
0
        if (state.icclink != NULL) gsicc_release_link(state.icclink);
146
0
        return 0;
147
0
    }
148
0
    for (xi = 0; xi < 2; ++xi)
149
0
        for (yi = 0; yi < 2; ++yi) {
150
0
            float v[2];
151
152
0
            v[0] = x[xi], v[1] = y[yi];
153
0
            gs_function_evaluate(psh->params.Function, v,
154
0
                                 state.frame.cc[yi * 2 + xi].paint.values);
155
0
        }
156
0
    state.frame.region.p.x = x[0];
157
0
    state.frame.region.p.y = y[0];
158
0
    state.frame.region.q.x = x[1];
159
0
    state.frame.region.q.y = y[1];
160
0
    code = Fb_fill_region(&state, rect_clip);
161
0
    if (state.icclink != NULL) gsicc_release_link(state.icclink);
162
0
    return code;
163
0
}
164
165
/* ---------------- Axial shading ---------------- */
166
167
typedef struct A_fill_state_s {
168
    const gs_shading_A_t *psh;
169
    gs_point delta;
170
    double length;
171
    double t0, t1;
172
    double v0, v1, u0, u1;
173
} A_fill_state_t;
174
/****** NEED GC DESCRIPTOR ******/
175
176
/* Note t0 and t1 vary over [0..1], not the Domain. */
177
178
typedef struct
179
{
180
    patch_curve_t curve[4];
181
    gs_point corners[4];
182
} corners_and_curves;
183
184
/* Ghostscript cannot possibly render any patch whose bounds aren't
185
 * representable in fixed's. In fact, this is a larger limit than
186
 * we need. We notionally have an area defined by coordinates
187
 * that can be represented in fixed point with at least 1 bit to
188
 * spare.
189
 *
190
 * Any patch that lies completely outside this region can be clipped
191
 * away. Any patch that isn't representable by fixed points can be
192
 * subdivided into 4.
193
 *
194
 * This avoids us subdividing patches huge numbers of times because
195
 * one side is just outside the region we will accept.
196
 */
197
198
199
#define MIN_CLIP_LIMIT ((int)(fixed2int(min_fixed)/2))
200
#define MAX_CLIP_LIMIT ((int)(fixed2int(max_fixed)/2))
201
202
static int not_clipped_away(const gs_point *p, const gs_fixed_rect *rect)
203
401M
{
204
401M
    if (p[0].x < rect->p.x &&
205
401M
        p[1].x < rect->p.x &&
206
401M
        p[2].x < rect->p.x &&
207
401M
        p[3].x < rect->p.x)
208
109M
        return 0; /* Clipped away! */
209
292M
    if (p[0].x > rect->q.x &&
210
292M
        p[1].x > rect->q.x &&
211
292M
        p[2].x > rect->q.x &&
212
292M
        p[3].x > rect->q.x)
213
111M
        return 0; /* Clipped away! */
214
180M
    if (p[0].y < rect->p.y &&
215
180M
        p[1].y < rect->p.y &&
216
180M
        p[2].y < rect->p.y &&
217
180M
        p[3].y < rect->p.y)
218
5
        return 0; /* Clipped away! */
219
180M
    if (p[0].y > rect->q.y &&
220
180M
        p[1].y > rect->q.y &&
221
180M
        p[2].y > rect->q.y &&
222
180M
        p[3].y > rect->q.y)
223
0
        return 0; /* Clipped away! */
224
180M
    return 1;
225
180M
}
226
227
#define midpoint(a,b)\
228
1.29G
  (arith_rshift_1(a) + arith_rshift_1(b) + (((a) | (b)) & 1))
229
230
#define quarterpoint(a,b)\
231
482M
  (midpoint(a,midpoint(a,b)))
232
233
static int
234
subdivide_patch_fill(patch_fill_state_t *pfs, patch_curve_t c[4])
235
80.0M
{
236
80.0M
    fixed m0, m1;
237
80.0M
    int v0, v1;
238
80.0M
    int changed;
239
240
80.0M
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
241
349
        return 0;
242
243
    /* On entry we have a patch:
244
     *   c[0].vertex  c[1].vertex
245
     *
246
     *   c[3].vertex  c[2].vertex
247
     *
248
     * Only the corners are set. The control points are not!
249
     *
250
     * BUT... in terms of spacial coords, it might be different...
251
     * They might be flipped on X, Y or both, giving:
252
     *  01 or 10 or 32 or 23
253
     *  32    23    01    10
254
     * or they might be rotated, and then flipped on X, Y or both, giving:
255
     *  03 or 30 or 12 or 21
256
     *  12    21    03    30
257
     */
258
259
    /* The +MIDPOINT_ACCURACY in the tests below is to allow for us finding the midpoint of [a] = z+1 and [b] = z, and getting z+1,
260
     * and updating [a] to be z+1, hence never actually shrinking the gap. Just accept not culling the patch as
261
     * much as we might. See bug 706378 for an example. */
262
455M
#define MIDPOINT_ACCURACY 1
263
309M
#define QUARTERPOINT_ACCURACY 3
264
265
80.0M
    do {
266
80.0M
        changed = 0;
267
268
        /* Is the whole of our patch outside the clipping rectangle? */
269
        /* Tempting to try to roll this into the cases below, but that
270
         * doesn't work because we want <= or >= here. Do X ones first. */
271
80.0M
        if ((c[0].vertex.p.x <= pfs->rect.p.x &&
272
80.0M
             c[1].vertex.p.x <= pfs->rect.p.x &&
273
80.0M
             c[2].vertex.p.x <= pfs->rect.p.x &&
274
80.0M
             c[3].vertex.p.x <= pfs->rect.p.x) ||
275
80.0M
            (c[0].vertex.p.x >= pfs->rect.q.x &&
276
80.0M
             c[1].vertex.p.x >= pfs->rect.q.x &&
277
80.0M
             c[2].vertex.p.x >= pfs->rect.q.x &&
278
80.0M
             c[3].vertex.p.x >= pfs->rect.q.x))
279
6.61M
                return 0;
280
281
        /* First, let's try to see if we can cull the patch horizontally with the clipping
282
         * rectangle. */
283
        /* Non rotated cases first. Can we cull the left hand half? */
284
73.4M
        if (c[0].vertex.p.x < pfs->rect.p.x && c[3].vertex.p.x < pfs->rect.p.x)
285
42.7M
        {
286
            /* Check 0+3 off left. */
287
42.7M
            v0 = 0;
288
42.7M
            v1 = 3;
289
42.7M
            goto check_left;
290
42.7M
        }
291
30.6M
        else if (c[1].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
292
29
        {
293
            /* Check 1+2 off left. */
294
29
            v0 = 1;
295
29
            v1 = 2;
296
42.7M
check_left:
297
            /* At this point we know that the condition for the following loop is true, so it
298
             * can be a do...while rather than a while. */
299
42.7M
            do
300
79.1M
            {
301
                /* Let's form (X coords only):
302
                 *
303
                 * c[v0].vertex  m0  c[v0^1].vertex
304
                 * c[v1].vertex  m1  c[v1^1].vertex
305
                 */
306
79.1M
                m0 = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
307
79.1M
                if (m0 >= pfs->rect.p.x)
308
42.7M
                    goto check_left_quarter;
309
36.4M
                m1 = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
310
36.4M
                if (m1 >= pfs->rect.p.x)
311
1
                    goto check_left_quarter;
312
                /* So, we can completely discard the left hand half of the patch. */
313
36.4M
                c[v0].vertex.p.x = m0;
314
36.4M
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
315
36.4M
                c[v1].vertex.p.x = m1;
316
36.4M
                c[v1].vertex.p.y = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
317
36.4M
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
318
36.4M
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
319
36.4M
                changed = 1;
320
36.4M
            }
321
42.7M
            while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
322
0
            if (0)
323
0
            {
324
42.7M
check_left_quarter:
325
                /* At this point we know that the condition for the following loop is true, so it
326
                 * can be a do...while rather than a while. */
327
42.7M
                do
328
65.8M
                {
329
                    /* Let's form (X coords only):
330
                     *
331
                     * c[v0].vertex  m0  x  x  c[v0^1].vertex
332
                     * c[v1].vertex  m1  x  x  c[v1^1].vertex
333
                     */
334
65.8M
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
335
65.8M
                    if (m0 >= pfs->rect.p.x)
336
42.7M
                        break;
337
23.0M
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
338
23.0M
                    if (m1 >= pfs->rect.p.x)
339
1
                        break;
340
                    /* So, we can completely discard the left hand quarter of the patch. */
341
23.0M
                    c[v0].vertex.p.x = m0;
342
23.0M
                    c[v0].vertex.p.y = midpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
343
23.0M
                    c[v1].vertex.p.x = m1;
344
23.0M
                    c[v1].vertex.p.y = midpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
345
23.0M
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
346
23.0M
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
347
23.0M
                    changed = 1;
348
23.0M
                }
349
42.7M
                while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
350
42.7M
            }
351
0
        }
352
353
        /* or the right hand half? */
354
73.4M
        if (c[0].vertex.p.x > pfs->rect.q.x && c[3].vertex.p.x > pfs->rect.q.x)
355
54
        {
356
            /* Check 0+3 off right. */
357
54
            v0 = 0;
358
54
            v1 = 3;
359
54
            goto check_right;
360
54
        }
361
73.4M
        else if (c[1].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
362
42.7M
        {
363
            /* Check 1+2 off right. */
364
42.7M
            v0 = 1;
365
42.7M
            v1 = 2;
366
42.7M
check_right:
367
            /* At this point we know that the condition for the following loop is true, so it
368
             * can be a do...while rather than a while. */
369
42.7M
            do
370
79.2M
            {
371
                /* Let's form (X coords only):
372
                 *
373
                 * c[v0].vertex  m0  c[v0^1].vertex
374
                 * c[v1].vertex  m1  c[v1^1].vertex
375
                 */
376
79.2M
                m0 = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
377
79.2M
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
378
110
                    goto check_right_quarter;
379
79.2M
                m1 = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
380
79.2M
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
381
42.7M
                    goto check_right_quarter;
382
                /* So, we can completely discard the left hand half of the patch. */
383
36.4M
                c[v0].vertex.p.x = m0;
384
36.4M
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
385
36.4M
                c[v1].vertex.p.x = m1;
386
36.4M
                c[v1].vertex.p.y = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
387
36.4M
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
388
36.4M
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
389
36.4M
                changed = 1;
390
36.4M
            }
391
42.7M
            while (c[v0].vertex.p.x > pfs->rect.q.x+MIDPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+MIDPOINT_ACCURACY);
392
0
            if (0)
393
0
            {
394
42.7M
check_right_quarter:
395
                /* At this point we know that the condition for the following loop is true, so it
396
                 * can be a do...while rather than a while. */
397
42.7M
                do
398
65.8M
                {
399
                    /* Let's form (X coords only):
400
                     *
401
                     * c[v0].vertex  m0  x  x  c[v0^1].vertex
402
                     * c[v1].vertex  m1  x  x  c[v1^1].vertex
403
                     */
404
65.8M
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
405
65.8M
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
406
110
                        break;
407
65.8M
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
408
65.8M
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
409
42.7M
                        break;
410
                    /* So, we can completely discard the left hand half of the patch. */
411
23.0M
                    c[v0].vertex.p.x = m0;
412
23.0M
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
413
23.0M
                    c[v1].vertex.p.x = m1;
414
23.0M
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
415
23.0M
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
416
23.0M
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
417
23.0M
                    changed = 1;
418
23.0M
                }
419
42.7M
                while (c[v0].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
420
42.7M
            }
421
0
        }
422
423
        /* Now, rotated cases: Can we cull the left hand half? */
424
73.4M
        if (c[0].vertex.p.x < pfs->rect.p.x && c[1].vertex.p.x < pfs->rect.p.x)
425
16.5k
        {
426
            /* Check 0+1 off left. */
427
16.5k
            v0 = 0;
428
16.5k
            v1 = 1;
429
16.5k
            goto check_rot_left;
430
16.5k
        }
431
73.4M
        else if (c[3].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
432
30.4M
        {
433
            /* Check 3+2 off left. */
434
30.4M
            v0 = 3;
435
30.4M
            v1 = 2;
436
30.4M
check_rot_left:
437
            /* At this point we know that the condition for the following loop is true, so it
438
             * can be a do...while rather than a while. */
439
30.4M
            do
440
61.8M
            {
441
                /* Let's form (X coords only):
442
                 *
443
                 * c[v0].vertex    m0  c[v0^3].vertex
444
                 * c[v1^3].vertex  m1  c[v1].vertex
445
                 */
446
61.8M
                m0 = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
447
61.8M
                if (m0 >= pfs->rect.p.x)
448
22.6k
                    goto check_rot_left_quarter;
449
61.8M
                m1 = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
450
61.8M
                if (m1 >= pfs->rect.p.x)
451
30.4M
                    goto check_rot_left_quarter;
452
                /* So, we can completely discard the left hand half of the patch. */
453
31.4M
                c[v0].vertex.p.x = m0;
454
31.4M
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
455
31.4M
                c[v1].vertex.p.x = m1;
456
31.4M
                c[v1].vertex.p.y = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
457
31.4M
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
458
31.4M
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
459
31.4M
                changed = 1;
460
31.4M
            }
461
31.4M
            while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
462
0
            if (0)
463
0
            {
464
30.4M
check_rot_left_quarter:
465
                /* At this point we know that the condition for the following loop is true, so it
466
                 * can be a do...while rather than a while. */
467
30.4M
                do
468
46.0M
                {
469
                    /* Let's form (X coords only):
470
                     *
471
                     * c[v0].vertex  m0  x  x  c[v0^3].vertex
472
                     * c[v1].vertex  m1  x  x  c[v1^3].vertex
473
                     */
474
46.0M
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
475
46.0M
                    if (m0 >= pfs->rect.p.x)
476
17.9k
                        break;
477
46.0M
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
478
46.0M
                    if (m1 >= pfs->rect.p.x)
479
30.4M
                        break;
480
                    /* So, we can completely discard the left hand half of the patch. */
481
15.6M
                    c[v0].vertex.p.x = m0;
482
15.6M
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
483
15.6M
                    c[v1].vertex.p.x = m1;
484
15.6M
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
485
15.6M
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
486
15.6M
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
487
15.6M
                    changed = 1;
488
15.6M
                }
489
30.4M
                while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
490
30.4M
            }
491
0
        }
492
493
        /* or the right hand half? */
494
73.4M
        if (c[0].vertex.p.x > pfs->rect.q.x && c[1].vertex.p.x > pfs->rect.q.x)
495
30.4M
        {
496
            /* Check 0+1 off right. */
497
30.4M
            v0 = 0;
498
30.4M
            v1 = 1;
499
30.4M
            goto check_rot_right;
500
30.4M
        }
501
43.0M
        else if (c[3].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
502
13.2k
        {
503
            /* Check 3+2 off right. */
504
13.2k
            v0 = 3;
505
13.2k
            v1 = 2;
506
30.4M
check_rot_right:
507
            /* At this point we know that the condition for the following loop is true, so it
508
             * can be a do...while rather than a while. */
509
30.4M
            do
510
61.8M
            {
511
                /* Let's form (X coords only):
512
                 *
513
                 * c[v0].vertex  m0  c[v0^3].vertex
514
                 * c[v1].vertex  m1  c[v1^3].vertex
515
                 */
516
61.8M
                m0 = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
517
61.8M
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
518
30.4M
                    goto check_rot_right_quarter;
519
31.4M
                m1 = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
520
31.4M
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
521
578
                    goto check_rot_right_quarter;
522
                /* So, we can completely discard the left hand half of the patch. */
523
31.4M
                c[v0].vertex.p.x = m0;
524
31.4M
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
525
31.4M
                c[v1].vertex.p.x = m1;
526
31.4M
                c[v1].vertex.p.y = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
527
31.4M
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
528
31.4M
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
529
31.4M
                changed = 1;
530
31.4M
            }
531
31.4M
            while (c[v0].vertex.p.x > pfs->rect.q.x+MIDPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+MIDPOINT_ACCURACY);
532
0
            if (0)
533
0
            {
534
30.4M
check_rot_right_quarter:
535
                /* At this point we know that the condition for the following loop is true, so it
536
                 * can be a do...while rather than a while. */
537
30.4M
                do
538
46.0M
                {
539
                    /* Let's form (X coords only):
540
                     *
541
                     * c[v0].vertex  m0  c[v0^3].vertex
542
                     * c[v1].vertex  m1  c[v1^3].vertex
543
                     */
544
46.0M
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
545
46.0M
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
546
30.4M
                        break;
547
15.6M
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
548
15.6M
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
549
5.72k
                        break;
550
                    /* So, we can completely discard the left hand half of the patch. */
551
15.6M
                    c[v0].vertex.p.x = m0;
552
15.6M
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
553
15.6M
                    c[v1].vertex.p.x = m1;
554
15.6M
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
555
15.6M
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
556
15.6M
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
557
15.6M
                    changed = 1;
558
15.6M
                }
559
30.4M
                while (c[v0].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
560
30.4M
            }
561
0
        }
562
563
        /* Is the whole of our patch outside the clipping rectangle? */
564
        /* Tempting to try to roll this into the cases below, but that
565
         * doesn't work because we want <= or >= here. Do Y ones. Can't have
566
         * done this earlier, as the previous set of tests might have reduced
567
         * the range here. */
568
73.4M
        if ((c[0].vertex.p.y <= pfs->rect.p.y &&
569
73.4M
             c[1].vertex.p.y <= pfs->rect.p.y &&
570
73.4M
             c[2].vertex.p.y <= pfs->rect.p.y &&
571
73.4M
             c[3].vertex.p.y <= pfs->rect.p.y) ||
572
73.4M
            (c[0].vertex.p.y >= pfs->rect.q.y &&
573
73.4M
             c[1].vertex.p.y >= pfs->rect.q.y &&
574
73.4M
             c[2].vertex.p.y >= pfs->rect.q.y &&
575
73.4M
             c[3].vertex.p.y >= pfs->rect.q.y))
576
73.2M
            return 0;
577
578
        /* Now, let's try to see if we can cull the patch vertically with the clipping
579
         * rectangle. */
580
        /* Non rotated cases first. Can we cull the top half? */
581
232k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[1].vertex.p.y < pfs->rect.p.y)
582
8.95k
        {
583
            /* Check 0+1 off above. */
584
8.95k
            v0 = 0;
585
8.95k
            v1 = 1;
586
8.95k
            goto check_above;
587
8.95k
        }
588
223k
        else if (c[3].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
589
6.37k
        {
590
            /* Check 3+2 off above. */
591
6.37k
            v0 = 3;
592
6.37k
            v1 = 2;
593
15.3k
check_above:
594
            /* At this point we know that the condition for the following loop is true, so it
595
             * can be a do...while rather than a while. */
596
15.3k
            do
597
17.4k
            {
598
                /* Let's form (Y coords only):
599
                 *
600
                 * c[v0].vertex     c[v1].vertex
601
                 * m0               m1
602
                 * c[v0^3].vertex   c[v1^3].vertex
603
                 */
604
17.4k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
605
17.4k
                if (m0 >= pfs->rect.p.y)
606
5.23k
                    goto check_above_quarter;
607
12.2k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
608
12.2k
                if (m1 >= pfs->rect.p.y)
609
10.0k
                    goto check_above_quarter;
610
                /* So, we can completely discard the top half of the patch. */
611
2.10k
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
612
2.10k
                c[v0].vertex.p.y = m0;
613
2.10k
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
614
2.10k
                c[v1].vertex.p.y = m1;
615
2.10k
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
616
2.10k
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
617
2.10k
                changed = 1;
618
2.10k
            }
619
15.3k
            while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
620
0
            if (0)
621
0
            {
622
15.3k
check_above_quarter:
623
                /* At this point we know that the condition for the following loop is true, so it
624
                 * can be a do...while rather than a while. */
625
15.3k
                do
626
18.5k
                {
627
                    /* Let's form (Y coords only):
628
                     *
629
                     * c[v0].vertex     c[v1].vertex
630
                     * m0               m1
631
                     * x                x
632
                     * x                x
633
                     * c[v0^3].vertex   c[v1^3].vertex
634
                     */
635
18.5k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
636
18.5k
                    if (m0 >= pfs->rect.p.y)
637
4.33k
                        break;
638
14.2k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
639
14.2k
                    if (m1 >= pfs->rect.p.y)
640
10.9k
                        break;
641
                    /* So, we can completely discard the top half of the patch. */
642
3.26k
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
643
3.26k
                    c[v0].vertex.p.y = m0;
644
3.26k
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
645
3.26k
                    c[v1].vertex.p.y = m1;
646
3.26k
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
647
3.26k
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
648
3.26k
                    changed = 1;
649
3.26k
                }
650
15.3k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
651
15.3k
            }
652
0
        }
653
654
        /* or the bottom half? */
655
232k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[1].vertex.p.y > pfs->rect.q.y)
656
6.56k
        {
657
            /* Check 0+1 off bottom. */
658
6.56k
            v0 = 0;
659
6.56k
            v1 = 1;
660
6.56k
            goto check_bottom;
661
6.56k
        }
662
226k
        else if (c[3].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
663
8.64k
        {
664
            /* Check 3+2 off bottom. */
665
8.64k
            v0 = 3;
666
8.64k
            v1 = 2;
667
15.2k
check_bottom:
668
            /* At this point we know that the condition for the following loop is true, so it
669
             * can be a do...while rather than a while. */
670
15.2k
            do
671
17.5k
            {
672
                /* Let's form (Y coords only):
673
                 *
674
                 * c[v0].vertex     c[v1].vertex
675
                 * m0               m1
676
                 * c[v0^3].vertex   c[v1^3].vertex
677
                 */
678
17.5k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
679
17.5k
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
680
12.0k
                    goto check_bottom_quarter;
681
5.54k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
682
5.54k
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
683
3.16k
                    goto check_bottom_quarter;
684
                /* So, we can completely discard the bottom half of the patch. */
685
2.37k
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
686
2.37k
                c[v0].vertex.p.y = m0;
687
2.37k
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
688
2.37k
                c[v1].vertex.p.y = m1;
689
2.37k
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
690
2.37k
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
691
2.37k
                changed = 1;
692
2.37k
            }
693
15.2k
            while (c[v0].vertex.p.y > pfs->rect.q.y+MIDPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+MIDPOINT_ACCURACY);
694
0
            if (0)
695
0
            {
696
15.2k
check_bottom_quarter:
697
                /* At this point we know that the condition for the following loop is true, so it
698
                 * can be a do...while rather than a while. */
699
15.2k
                do
700
18.8k
                {
701
                    /* Let's form (Y coords only):
702
                     *
703
                     * c[v0].vertex     c[v1].vertex
704
                     * x                x
705
                     * x                x
706
                     * m0               m1
707
                     * c[v0^3].vertex   c[v1^3].vertex
708
                     */
709
18.8k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
710
18.8k
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
711
11.6k
                        break;
712
7.19k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
713
7.19k
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
714
3.60k
                        break;
715
                    /* So, we can completely discard the bottom half of the patch. */
716
3.59k
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
717
3.59k
                    c[v0].vertex.p.y = m0;
718
3.59k
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
719
3.59k
                    c[v1].vertex.p.y = m1;
720
3.59k
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
721
3.59k
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
722
3.59k
                    changed = 1;
723
3.59k
                }
724
15.2k
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
725
15.2k
            }
726
0
        }
727
728
        /* Now, rotated cases: Can we cull the top half? */
729
232k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[3].vertex.p.y < pfs->rect.p.y)
730
1.00k
        {
731
            /* Check 0+3 off above. */
732
1.00k
            v0 = 0;
733
1.00k
            v1 = 3;
734
1.00k
            goto check_rot_above;
735
1.00k
        }
736
231k
        else if (c[1].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
737
604
        {
738
            /* Check 1+2 off above. */
739
604
            v0 = 1;
740
604
            v1 = 2;
741
1.60k
check_rot_above:
742
            /* At this point we know that the condition for the following loop is true, so it
743
             * can be a do...while rather than a while. */
744
1.60k
            do
745
2.02k
            {
746
                /* Let's form (Y coords only):
747
                 *
748
                 * c[v0].vertex     c[v1].vertex
749
                 * m0               m1
750
                 * c[v0^1].vertex   c[v1^1].vertex
751
                 */
752
2.02k
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
753
2.02k
                if (m0 >= pfs->rect.p.y)
754
1.59k
                    goto check_rot_above_quarter;
755
431
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
756
431
                if (m1 >= pfs->rect.p.y)
757
9
                    goto check_rot_above_quarter;
758
                /* So, we can completely discard the top half of the patch. */
759
422
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
760
422
                c[v0].vertex.p.y = m0;
761
422
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
762
422
                c[v1].vertex.p.y = m1;
763
422
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
764
422
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
765
422
                changed = 1;
766
422
            }
767
1.60k
            while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
768
0
            if (0)
769
0
            {
770
1.60k
check_rot_above_quarter:
771
                /* At this point we know that the condition for the following loop is true, so it
772
                 * can be a do...while rather than a while. */
773
1.60k
                do
774
1.66k
                {
775
                    /* Let's form (Y coords only):
776
                     *
777
                     * c[v0].vertex     c[v1].vertex
778
                     * m0               m1
779
                     * x                x
780
                     * x                x
781
                     * c[v0^1].vertex   c[v1^1].vertex
782
                     */
783
1.66k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
784
1.66k
                    if (m0 >= pfs->rect.p.y)
785
1.59k
                        break;
786
70
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
787
70
                    if (m1 >= pfs->rect.p.y)
788
9
                        break;
789
                    /* So, we can completely discard the top half of the patch. */
790
61
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
791
61
                    c[v0].vertex.p.y = m0;
792
61
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
793
61
                    c[v1].vertex.p.y = m1;
794
61
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
795
61
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
796
61
                    changed = 1;
797
61
                }
798
1.60k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
799
1.60k
            }
800
0
        }
801
802
        /* or the bottom half? */
803
232k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[3].vertex.p.y > pfs->rect.q.y)
804
38
        {
805
            /* Check 0+3 off the bottom. */
806
38
            v0 = 0;
807
38
            v1 = 3;
808
38
            goto check_rot_bottom;
809
38
        }
810
232k
        else if (c[1].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
811
43
        {
812
            /* Check 1+2 off the bottom. */
813
43
            v0 = 1;
814
43
            v1 = 2;
815
81
check_rot_bottom:
816
            /* At this point we know that the condition for the following loop is true, so it
817
             * can be a do...while rather than a while. */
818
81
            do
819
201
            {
820
                /* Let's form (Y coords only):
821
                 *
822
                 * c[v0].vertex     c[v1].vertex
823
                 * m0               m1
824
                 * c[v0^1].vertex   c[v1^1].vertex
825
                 */
826
201
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
827
201
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
828
81
                    goto check_rot_bottom_quarter;
829
120
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
830
120
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
831
0
                    goto check_rot_bottom_quarter;
832
                /* So, we can completely discard the left hand half of the patch. */
833
120
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
834
120
                c[v0].vertex.p.y = m0;
835
120
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
836
120
                c[v1].vertex.p.y = m1;
837
120
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
838
120
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
839
120
                changed = 1;
840
120
            }
841
120
            while (c[v0].vertex.p.y > pfs->rect.q.y+MIDPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+MIDPOINT_ACCURACY);
842
0
            if (0)
843
0
            {
844
81
check_rot_bottom_quarter:
845
                /* At this point we know that the condition for the following loop is true, so it
846
                 * can be a do...while rather than a while. */
847
81
                do
848
115
                {
849
                    /* Let's form (Y coords only):
850
                     *
851
                     * c[v0].vertex     c[v1].vertex
852
                     * x                x
853
                     * x                x
854
                     * m0               m1
855
                     * c[v0^1].vertex   c[v1^1].vertex
856
                     */
857
115
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
858
115
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
859
81
                        break;
860
34
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
861
34
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
862
0
                        break;
863
                    /* So, we can completely discard the left hand half of the patch. */
864
34
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
865
34
                    c[v0].vertex.p.y = m0;
866
34
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
867
34
                    c[v1].vertex.p.y = m1;
868
34
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
869
34
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
870
34
                    changed = 1;
871
34
                }
872
81
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
873
81
            }
874
0
        }
875
232k
    } while (changed);
876
877
220k
    c[0].vertex.cc[1] = c[1].vertex.cc[1] =
878
220k
                        c[2].vertex.cc[1] =
879
220k
                        c[3].vertex.cc[1] = 0;
880
220k
    make_other_poles(c);
881
220k
    return patch_fill(pfs, c, NULL, NULL);
882
80.0M
}
883
#undef midpoint
884
#undef quarterpoint
885
#undef MIDPOINT_ACCURACY
886
#undef QUARTERPOINT_ACCURACY
887
888
964M
#define f_fits_in_fixed(f) f_fits_in_bits(f, fixed_int_bits)
889
890
static int
891
A_fill_region_floats(patch_fill_state_t *pfs1, corners_and_curves *cc, int depth)
892
180M
{
893
180M
    corners_and_curves sub[4];
894
180M
    int code;
895
896
180M
    if (depth == 32)
897
3
        return gs_error_limitcheck;
898
899
180M
    if (depth > 0 &&
900
180M
        f_fits_in_fixed(cc->corners[0].x) &&
901
180M
        f_fits_in_fixed(cc->corners[0].y) &&
902
180M
        f_fits_in_fixed(cc->corners[1].x) &&
903
180M
        f_fits_in_fixed(cc->corners[1].y) &&
904
180M
        f_fits_in_fixed(cc->corners[2].x) &&
905
180M
        f_fits_in_fixed(cc->corners[2].y) &&
906
180M
        f_fits_in_fixed(cc->corners[3].x) &&
907
180M
        f_fits_in_fixed(cc->corners[3].y))
908
79.8M
    {
909
79.8M
        cc->curve[0].vertex.p.x = float2fixed(cc->corners[0].x);
910
79.8M
        cc->curve[0].vertex.p.y = float2fixed(cc->corners[0].y);
911
79.8M
        cc->curve[1].vertex.p.x = float2fixed(cc->corners[1].x);
912
79.8M
        cc->curve[1].vertex.p.y = float2fixed(cc->corners[1].y);
913
79.8M
        cc->curve[2].vertex.p.x = float2fixed(cc->corners[2].x);
914
79.8M
        cc->curve[2].vertex.p.y = float2fixed(cc->corners[2].y);
915
79.8M
        cc->curve[3].vertex.p.x = float2fixed(cc->corners[3].x);
916
79.8M
        cc->curve[3].vertex.p.y = float2fixed(cc->corners[3].y);
917
79.8M
        return subdivide_patch_fill(pfs1, cc->curve);
918
79.8M
    }
919
920
    /* We have patches with corners:
921
     *  0  1
922
     *  3  2
923
     * We subdivide these into 4 smaller patches:
924
     *
925
     *  0   10   1     Where 0123 are corners
926
     *   [0]  [1]      [0][1][2][3] are patches.
927
     *  3   23   2
928
     *  0   10   1
929
     *   [3]  [2]
930
     *  3   23   2
931
     */
932
933
100M
    sub[0].corners[0].x = cc->corners[0].x;
934
100M
    sub[0].corners[0].y = cc->corners[0].y;
935
100M
    sub[1].corners[1].x = cc->corners[1].x;
936
100M
    sub[1].corners[1].y = cc->corners[1].y;
937
100M
    sub[2].corners[2].x = cc->corners[2].x;
938
100M
    sub[2].corners[2].y = cc->corners[2].y;
939
100M
    sub[3].corners[3].x = cc->corners[3].x;
940
100M
    sub[3].corners[3].y = cc->corners[3].y;
941
100M
    sub[1].corners[0].x = sub[0].corners[1].x = (cc->corners[0].x + cc->corners[1].x)/2;
942
100M
    sub[1].corners[0].y = sub[0].corners[1].y = (cc->corners[0].y + cc->corners[1].y)/2;
943
100M
    sub[3].corners[2].x = sub[2].corners[3].x = (cc->corners[2].x + cc->corners[3].x)/2;
944
100M
    sub[3].corners[2].y = sub[2].corners[3].y = (cc->corners[2].y + cc->corners[3].y)/2;
945
100M
    sub[3].corners[0].x = sub[0].corners[3].x = (cc->corners[0].x + cc->corners[3].x)/2;
946
100M
    sub[3].corners[0].y = sub[0].corners[3].y = (cc->corners[0].y + cc->corners[3].y)/2;
947
100M
    sub[2].corners[1].x = sub[1].corners[2].x = (cc->corners[1].x + cc->corners[2].x)/2;
948
100M
    sub[2].corners[1].y = sub[1].corners[2].y = (cc->corners[1].y + cc->corners[2].y)/2;
949
100M
    sub[0].corners[2].x = sub[1].corners[3].x =
950
100M
                          sub[2].corners[0].x =
951
100M
                          sub[3].corners[1].x = (sub[0].corners[3].x + sub[1].corners[2].x)/2;
952
100M
    sub[0].corners[2].y = sub[1].corners[3].y =
953
100M
                          sub[2].corners[0].y =
954
100M
                          sub[3].corners[1].y = (sub[0].corners[3].y + sub[1].corners[2].y)/2;
955
100M
    sub[0].curve[0].vertex.cc[0] = sub[0].curve[3].vertex.cc[0] =
956
100M
                                   sub[3].curve[0].vertex.cc[0] =
957
100M
                                   sub[3].curve[3].vertex.cc[0] = cc->curve[0].vertex.cc[0];
958
100M
    sub[1].curve[1].vertex.cc[0] = sub[1].curve[2].vertex.cc[0] =
959
100M
                                   sub[2].curve[1].vertex.cc[0] =
960
100M
                                   sub[2].curve[2].vertex.cc[0] = cc->curve[1].vertex.cc[0];
961
100M
    sub[0].curve[1].vertex.cc[0] = sub[0].curve[2].vertex.cc[0] =
962
100M
                                   sub[1].curve[0].vertex.cc[0] =
963
100M
                                   sub[1].curve[3].vertex.cc[0] =
964
100M
                                   sub[2].curve[0].vertex.cc[0] =
965
100M
                                   sub[2].curve[3].vertex.cc[0] =
966
100M
                                   sub[3].curve[1].vertex.cc[0] =
967
100M
                                   sub[3].curve[2].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
968
969
100M
    depth++;
970
100M
    if (not_clipped_away(sub[0].corners, &pfs1->rect)) {
971
39.5M
        code = A_fill_region_floats(pfs1, &sub[0], depth);
972
39.5M
        if (code < 0)
973
96
            return code;
974
39.5M
    }
975
100M
    if (not_clipped_away(sub[1].corners, &pfs1->rect)) {
976
49.4M
        code = A_fill_region_floats(pfs1, &sub[1], depth);
977
49.4M
        if (code < 0)
978
0
            return code;
979
49.4M
    }
980
100M
    if (not_clipped_away(sub[2].corners, &pfs1->rect)) {
981
39.5M
        code = A_fill_region_floats(pfs1, &sub[2], depth);
982
39.5M
        if (code < 0)
983
0
            return code;
984
39.5M
    }
985
100M
    if (not_clipped_away(sub[3].corners, &pfs1->rect)) {
986
51.7M
        code = A_fill_region_floats(pfs1, &sub[3], depth);
987
51.7M
        if (code < 0)
988
0
            return code;
989
51.7M
    }
990
991
100M
    return 0;
992
100M
}
993
994
2.27k
#define midpoint(a,b)      ((a+b)/2)
995
996
123
#define quarterpoint(a,b)  ((a+3*b)/4)
997
998
static int
999
subdivide_patch_fill_floats(patch_fill_state_t *pfs, corners_and_curves *cc)
1000
342
{
1001
342
    double m0, m1;
1002
342
    int v0, v1;
1003
342
    int changed;
1004
1005
342
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
1006
269
        return 0;
1007
1008
    /* On entry we have a patch:
1009
     *   c[0].vertex  c[1].vertex
1010
     *
1011
     *   c[3].vertex  c[2].vertex
1012
     *
1013
     * Only the corners are set. The control points are not!
1014
     *
1015
     * BUT... in terms of spacial coords, it might be different...
1016
     * They might be flipped on X, Y or both, giving:
1017
     *  01 or 10 or 32 or 23
1018
     *  32    23    01    10
1019
     * or they might be rotated, and then flipped on X, Y or both, giving:
1020
     *  03 or 30 or 12 or 21
1021
     *  12    21    03    30
1022
     */
1023
1024
    /* The +MIDPOINT_ACCURACY in the tests below is to allow for us finding the midpoint of [a] = z+1 and [b] = z, and getting z+1,
1025
     * and updating [a] to be z+1, hence never actually shrinking the gap. Just accept not culling the patch as
1026
     * much as we might. See bug 706378 for an example. */
1027
1.43k
#define MIDPOINT_ACCURACY 0.0001
1028
73
#define QUARTERPOINT_ACCURACY 0.0003
1029
1030
108
    do {
1031
108
        changed = 0;
1032
1033
        /* Is the whole of our patch outside the clipping rectangle? */
1034
        /* Tempting to try to roll this into the cases below, but that
1035
         * doesn't work because we want <= or >= here. Do the X ones
1036
         * first. */
1037
108
        if ((cc->corners[0].x <= pfs->rect.p.x &&
1038
108
             cc->corners[1].x <= pfs->rect.p.x &&
1039
108
             cc->corners[2].x <= pfs->rect.p.x &&
1040
108
             cc->corners[3].x <= pfs->rect.p.x) ||
1041
108
            (cc->corners[0].x >= pfs->rect.q.x &&
1042
86
             cc->corners[1].x >= pfs->rect.q.x &&
1043
86
             cc->corners[2].x >= pfs->rect.q.x &&
1044
86
             cc->corners[3].x >= pfs->rect.q.x))
1045
37
                return 0;
1046
1047
        /* First, let's try to see if we can cull the patch horizontally with the clipping
1048
         * rectangle. */
1049
        /* Non rotated cases first. Can we cull the left hand half? */
1050
71
        if (cc->corners[0].x < pfs->rect.p.x && cc->corners[3].x < pfs->rect.p.x)
1051
9
        {
1052
            /* Check 0+3 off left. */
1053
9
            v0 = 0;
1054
9
            v1 = 3;
1055
9
            goto check_left;
1056
9
        }
1057
62
        else if (cc->corners[1].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1058
30
        {
1059
            /* Check 1+2 off left. */
1060
30
            v0 = 1;
1061
30
            v1 = 2;
1062
39
check_left:
1063
            /* At this point we know that the condition for the following loop is true, so it
1064
             * can be a do...while rather than a while. */
1065
39
            do
1066
239
            {
1067
                /* Let's form (X coords only):
1068
                 *
1069
                 * c[v0].vertex  m0  c[v0^1].vertex
1070
                 * c[v1].vertex  m1  c[v1^1].vertex
1071
                 */
1072
239
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1073
239
                if (m0 >= pfs->rect.p.x)
1074
39
                    goto check_left_quarter;
1075
200
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1076
200
                if (m1 >= pfs->rect.p.x)
1077
0
                    goto check_left_quarter;
1078
                /* So, we can completely discard the left hand half of the patch. */
1079
200
                cc->corners[v0].x = m0;
1080
200
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1081
200
                cc->corners[v1].x = m1;
1082
200
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1083
200
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1084
200
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1085
200
                changed = 1;
1086
200
            }
1087
200
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1088
0
            if (0)
1089
0
            {
1090
39
check_left_quarter:
1091
                /* At this point we know that the condition for the following loop is true, so it
1092
                 * can be a do...while rather than a while. */
1093
39
                do
1094
39
                {
1095
                    /* Let's form (X coords only):
1096
                     *
1097
                     * c[v0].vertex  m0  x  x  c[v0^1].vertex
1098
                     * c[v1].vertex  m1  x  x  c[v1^1].vertex
1099
                     */
1100
39
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1101
39
                    if (m0 >= pfs->rect.p.x)
1102
39
                        break;
1103
0
                    m1 = quarterpoint(cc->corners[v1].x, cc->corners[v1^1].x);
1104
0
                    if (m1 >= pfs->rect.p.x)
1105
0
                        break;
1106
                    /* So, we can completely discard the left hand quarter of the patch. */
1107
0
                    cc->corners[v0].x = m0;
1108
0
                    cc->corners[v0].y = midpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1109
0
                    cc->corners[v1].x = m1;
1110
0
                    cc->corners[v1].y = midpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1111
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^1].vertex.cc[0])/4;
1112
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^1].vertex.cc[0])/4;
1113
0
                    changed = 1;
1114
0
                }
1115
39
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1116
39
            }
1117
0
        }
1118
1119
        /* or the right hand half? */
1120
71
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[3].x > pfs->rect.q.x)
1121
39
        {
1122
            /* Check 0+3 off right. */
1123
39
            v0 = 0;
1124
39
            v1 = 3;
1125
39
            goto check_right;
1126
39
        }
1127
32
        else if (cc->corners[1].x > pfs->rect.q.x && cc->corners[2].x > pfs->rect.q.x)
1128
3
        {
1129
            /* Check 1+2 off right. */
1130
3
            v0 = 1;
1131
3
            v1 = 2;
1132
42
check_right:
1133
            /* At this point we know that the condition for the following loop is true, so it
1134
             * can be a do...while rather than a while. */
1135
42
            do
1136
314
            {
1137
                /* Let's form (X coords only):
1138
                 *
1139
                 * c[v0].vertex  m0  c[v0^1].vertex
1140
                 * c[v1].vertex  m1  c[v1^1].vertex
1141
                 */
1142
314
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1143
314
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1144
41
                    goto check_right_quarter;
1145
273
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1146
273
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1147
1
                    goto check_right_quarter;
1148
                /* So, we can completely discard the left hand half of the patch. */
1149
272
                cc->corners[v0].x = m0;
1150
272
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1151
272
                cc->corners[v1].x = m1;
1152
272
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1153
272
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1154
272
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1155
272
                changed = 1;
1156
272
            }
1157
272
            while (cc->corners[v0].x > pfs->rect.q.x+MIDPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+MIDPOINT_ACCURACY);
1158
0
            if (0)
1159
0
            {
1160
42
check_right_quarter:
1161
                /* At this point we know that the condition for the following loop is true, so it
1162
                 * can be a do...while rather than a while. */
1163
42
                do
1164
42
                {
1165
                    /* Let's form (X coords only):
1166
                     *
1167
                     * c[v0].vertex  m0  x  x  c[v0^1].vertex
1168
                     * c[v1].vertex  m1  x  x  c[v1^1].vertex
1169
                     */
1170
42
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1171
42
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1172
41
                        break;
1173
1
                    m1 = quarterpoint(cc->corners[v1].x, cc->corners[v1^1].x);
1174
1
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1175
1
                        break;
1176
                    /* So, we can completely discard the left hand half of the patch. */
1177
0
                    cc->corners[v0].x = m0;
1178
0
                    cc->corners[v0].y = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1179
0
                    cc->corners[v1].x = m1;
1180
0
                    cc->corners[v1].y = quarterpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1181
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^1].vertex.cc[0])/4;
1182
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^1].vertex.cc[0])/4;
1183
0
                    changed = 1;
1184
0
                }
1185
42
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1186
42
            }
1187
0
        }
1188
1189
        /* Now, rotated cases: Can we cull the left hand half? */
1190
71
        if (cc->corners[0].x < pfs->rect.p.x && cc->corners[1].x < pfs->rect.p.x)
1191
0
        {
1192
            /* Check 0+1 off left. */
1193
0
            v0 = 0;
1194
0
            v1 = 1;
1195
0
            goto check_rot_left;
1196
0
        }
1197
71
        else if (cc->corners[3].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1198
0
        {
1199
            /* Check 3+2 off left. */
1200
0
            v0 = 3;
1201
0
            v1 = 2;
1202
0
check_rot_left:
1203
            /* At this point we know that the condition for the following loop is true, so it
1204
             * can be a do...while rather than a while. */
1205
0
            do
1206
0
            {
1207
                /* Let's form (X coords only):
1208
                 *
1209
                 * c[v0].vertex    m0  c[v0^3].vertex
1210
                 * c[v1^3].vertex  m1  c[v1].vertex
1211
                 */
1212
0
                m0 = midpoint(cc->corners[0].x, cc->corners[3].x);
1213
0
                if (m0 >= pfs->rect.p.x)
1214
0
                    goto check_rot_left_quarter;
1215
0
                m1 = midpoint(cc->corners[1].x, cc->corners[2].x);
1216
0
                if (m1 >= pfs->rect.p.x)
1217
0
                    goto check_rot_left_quarter;
1218
                /* So, we can completely discard the left hand half of the patch. */
1219
0
                cc->corners[v0].x = m0;
1220
0
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[3].y);
1221
0
                cc->corners[v1].x = m1;
1222
0
                cc->corners[v1].y = midpoint(cc->corners[1].y, cc->corners[2].y);
1223
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[3].vertex.cc[0])/2;
1224
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[1].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1225
0
                changed = 1;
1226
0
            }
1227
0
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1228
0
            if (0)
1229
0
            {
1230
0
check_rot_left_quarter:
1231
                /* At this point we know that the condition for the following loop is true, so it
1232
                 * can be a do...while rather than a while. */
1233
0
                do
1234
0
                {
1235
                    /* Let's form (X coords only):
1236
                     *
1237
                     * c[v0].vertex  m0  x  x  c[v0^3].vertex
1238
                     * c[v1].vertex  m1  x  x  c[v1^3].vertex
1239
                     */
1240
0
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^3].x);
1241
0
                    if (m0 >= pfs->rect.p.x)
1242
0
                        break;
1243
0
                    m1 = quarterpoint(cc->corners[v1].x, cc->corners[v1^3].x);
1244
0
                    if (m1 >= pfs->rect.p.x)
1245
0
                        break;
1246
                    /* So, we can completely discard the left hand half of the patch. */
1247
0
                    cc->corners[v0].x = m0;
1248
0
                    cc->corners[v0].y = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1249
0
                    cc->corners[v1].x = m1;
1250
0
                    cc->corners[v1].y = quarterpoint(cc->corners[v1].y, cc->corners[v1^3].y);
1251
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^3].vertex.cc[0])/4;
1252
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^3].vertex.cc[0])/4;
1253
0
                    changed = 1;
1254
0
                }
1255
0
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1256
0
            }
1257
0
        }
1258
1259
        /* or the right hand half? */
1260
71
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[1].x > pfs->rect.q.x)
1261
0
        {
1262
            /* Check 0+1 off right. */
1263
0
            v0 = 0;
1264
0
            v1 = 1;
1265
0
            goto check_rot_right;
1266
0
        }
1267
71
        else if (cc->corners[3].x > pfs->rect.q.x && cc->corners[2].x > pfs->rect.q.x)
1268
0
        {
1269
            /* Check 3+2 off right. */
1270
0
            v0 = 3;
1271
0
            v1 = 2;
1272
0
check_rot_right:
1273
            /* At this point we know that the condition for the following loop is true, so it
1274
             * can be a do...while rather than a while. */
1275
0
            do
1276
0
            {
1277
                /* Let's form (X coords only):
1278
                 *
1279
                 * c[v0].vertex  m0  c[v0^3].vertex
1280
                 * c[v1].vertex  m1  c[v1^3].vertex
1281
                 */
1282
0
                m0 = midpoint(cc->corners[0].x, cc->corners[3].x);
1283
0
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1284
0
                    goto check_rot_right_quarter;
1285
0
                m1 = midpoint(cc->corners[1].x, cc->corners[2].x);
1286
0
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1287
0
                    goto check_rot_right_quarter;
1288
                /* So, we can completely discard the left hand half of the patch. */
1289
0
                cc->corners[v0].x = m0;
1290
0
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[3].y);
1291
0
                cc->corners[v1].x = m1;
1292
0
                cc->corners[v1].y = midpoint(cc->corners[1].y, cc->corners[2].y);
1293
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[3].vertex.cc[0])/2;
1294
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[1].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1295
0
                changed = 1;
1296
0
            }
1297
0
            while (cc->corners[v0].x > pfs->rect.q.x+MIDPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+MIDPOINT_ACCURACY);
1298
0
            if (0)
1299
0
            {
1300
0
check_rot_right_quarter:
1301
                /* At this point we know that the condition for the following loop is true, so it
1302
                 * can be a do...while rather than a while. */
1303
0
                do
1304
0
                {
1305
                    /* Let's form (X coords only):
1306
                     *
1307
                     * c[v0].vertex  m0  c[v0^3].vertex
1308
                     * c[v1].vertex  m1  c[v1^3].vertex
1309
                     */
1310
0
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^3].x);
1311
0
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1312
0
                        break;
1313
0
                    m1 = quarterpoint(cc->corners[v1].x, cc->corners[v1^3].x);
1314
0
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1315
0
                        break;
1316
                    /* So, we can completely discard the left hand half of the patch. */
1317
0
                    cc->corners[v0].x = m0;
1318
0
                    cc->corners[v0].y = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1319
0
                    cc->corners[v1].x = m1;
1320
0
                    cc->corners[v1].y = quarterpoint(cc->corners[v1].y, cc->corners[v1^3].y);
1321
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^3].vertex.cc[0])/4;
1322
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^3].vertex.cc[0])/4;
1323
0
                    changed = 1;
1324
0
                }
1325
0
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1326
0
            }
1327
0
        }
1328
1329
        /* Is the whole of our patch outside the clipping rectangle? */
1330
        /* Tempting to try to roll this into the cases below, but that
1331
         * doesn't work because we want <= or >= here. Do the Y ones.
1332
         * Can't do these at the same time as the X ones, as the cases
1333
         * above may have reduced Y by the time we get here. */
1334
71
        if ((cc->corners[0].y <= pfs->rect.p.y &&
1335
71
             cc->corners[1].y <= pfs->rect.p.y &&
1336
71
             cc->corners[2].y <= pfs->rect.p.y &&
1337
71
             cc->corners[3].y <= pfs->rect.p.y) ||
1338
71
            (cc->corners[0].y >= pfs->rect.q.y &&
1339
56
             cc->corners[1].y >= pfs->rect.q.y &&
1340
56
             cc->corners[2].y >= pfs->rect.q.y &&
1341
56
             cc->corners[3].y >= pfs->rect.q.y))
1342
15
            return 0;
1343
1344
        /* Now, let's try to see if we can cull the patch vertically with the clipping
1345
         * rectangle. */
1346
        /* Non rotated cases first. Can we cull the top half? */
1347
56
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[1].y < pfs->rect.p.y)
1348
8
        {
1349
            /* Check 0+1 off above. */
1350
8
            v0 = 0;
1351
8
            v1 = 1;
1352
8
            goto check_above;
1353
8
        }
1354
48
        else if (cc->corners[3].y < pfs->rect.p.y && cc->corners[2].y < pfs->rect.p.y)
1355
0
        {
1356
            /* Check 3+2 off above. */
1357
0
            v0 = 3;
1358
0
            v1 = 2;
1359
8
check_above:
1360
            /* At this point we know that the condition for the following loop is true, so it
1361
             * can be a do...while rather than a while. */
1362
8
            do
1363
8
            {
1364
                /* Let's form (Y coords only):
1365
                 *
1366
                 * c[v0].vertex     c[v1].vertex
1367
                 * m0               m1
1368
                 * c[v0^3].vertex   c[v1^3].vertex
1369
                 */
1370
8
                m0 = midpoint(cc->corners[0].y, cc->corners[3].y);
1371
8
                if (m0 >= pfs->rect.p.y)
1372
8
                    goto check_above_quarter;
1373
0
                m1 = midpoint(cc->corners[1].y, cc->corners[2].y);
1374
0
                if (m1 >= pfs->rect.p.y)
1375
0
                    goto check_above_quarter;
1376
                /* So, we can completely discard the top half of the patch. */
1377
0
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[3].x);
1378
0
                cc->corners[v0].y = m0;
1379
0
                cc->corners[v1].x = midpoint(cc->corners[1].x, cc->corners[2].x);
1380
0
                cc->corners[v1].y = m1;
1381
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[3].vertex.cc[0])/2;
1382
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[1].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1383
0
                changed = 1;
1384
0
            }
1385
8
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1386
0
            if (0)
1387
0
            {
1388
8
check_above_quarter:
1389
                /* At this point we know that the condition for the following loop is true, so it
1390
                 * can be a do...while rather than a while. */
1391
8
                do
1392
8
                {
1393
                    /* Let's form (Y coords only):
1394
                     *
1395
                     * c[v0].vertex     c[v1].vertex
1396
                     * m0               m1
1397
                     * x                x
1398
                     * x                x
1399
                     * c[v0^3].vertex   c[v1^3].vertex
1400
                     */
1401
8
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1402
8
                    if (m0 >= pfs->rect.p.y)
1403
8
                        break;
1404
0
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^3].y);
1405
0
                    if (m1 >= pfs->rect.p.y)
1406
0
                        break;
1407
                    /* So, we can completely discard the top half of the patch. */
1408
0
                    cc->corners[v0].x = quarterpoint(cc->corners[v0].x, cc->corners[v0^3].x);
1409
0
                    cc->corners[v0].y = m0;
1410
0
                    cc->corners[v1].x = quarterpoint(cc->corners[v1].x, cc->corners[v1^3].x);
1411
0
                    cc->corners[v1].y = m1;
1412
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^3].vertex.cc[0])/4;
1413
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^3].vertex.cc[0])/4;
1414
0
                    changed = 1;
1415
0
                }
1416
8
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1417
8
            }
1418
0
        }
1419
1420
        /* or the bottom half? */
1421
56
        if (cc->corners[0].y > pfs->rect.q.y && cc->corners[1].y > pfs->rect.q.y)
1422
0
        {
1423
            /* Check 0+1 off bottom. */
1424
0
            v0 = 0;
1425
0
            v1 = 1;
1426
0
            goto check_bottom;
1427
0
        }
1428
56
        else if (cc->corners[3].y > pfs->rect.q.y && cc->corners[2].y > pfs->rect.q.y)
1429
0
        {
1430
            /* Check 3+2 off bottom. */
1431
0
            v0 = 3;
1432
0
            v1 = 2;
1433
0
check_bottom:
1434
            /* At this point we know that the condition for the following loop is true, so it
1435
             * can be a do...while rather than a while. */
1436
0
            do
1437
0
            {
1438
                /* Let's form (Y coords only):
1439
                 *
1440
                 * c[v0].vertex     c[v1].vertex
1441
                 * m0               m1
1442
                 * c[v0^3].vertex   c[v1^3].vertex
1443
                 */
1444
0
                m0 = midpoint(cc->corners[0].y, cc->corners[3].y);
1445
0
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
1446
0
                    goto check_bottom_quarter;
1447
0
                m1 = midpoint(cc->corners[1].y, cc->corners[2].y);
1448
0
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
1449
0
                    goto check_bottom_quarter;
1450
                /* So, we can completely discard the bottom half of the patch. */
1451
0
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[3].x);
1452
0
                cc->corners[v0].y = m0;
1453
0
                cc->corners[v1].x = midpoint(cc->corners[1].x, cc->corners[2].x);
1454
0
                cc->corners[v1].y = m1;
1455
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[3].vertex.cc[0])/2;
1456
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[1].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1457
0
                changed = 1;
1458
0
            }
1459
0
            while (cc->corners[v0].y > pfs->rect.q.y+MIDPOINT_ACCURACY && cc->corners[v1].y > pfs->rect.q.y+MIDPOINT_ACCURACY);
1460
0
            if (0)
1461
0
            {
1462
0
check_bottom_quarter:
1463
                /* At this point we know that the condition for the following loop is true, so it
1464
                 * can be a do...while rather than a while. */
1465
0
                do
1466
0
                {
1467
                    /* Let's form (Y coords only):
1468
                     *
1469
                     * c[v0].vertex     c[v1].vertex
1470
                     * x                x
1471
                     * x                x
1472
                     * m0               m1
1473
                     * c[v0^3].vertex   c[v1^3].vertex
1474
                     */
1475
0
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1476
0
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
1477
0
                        break;
1478
0
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^3].y);
1479
0
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
1480
0
                        break;
1481
                    /* So, we can completely discard the bottom half of the patch. */
1482
0
                    cc->corners[v0].x = quarterpoint(cc->corners[v0].x, cc->corners[v0^3].x);
1483
0
                    cc->corners[v0].y = m0;
1484
0
                    cc->corners[v1].x = quarterpoint(cc->corners[v1].x, cc->corners[v1^3].x);
1485
0
                    cc->corners[v1].y = m1;
1486
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^3].vertex.cc[0])/4;
1487
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^3].vertex.cc[0])/4;
1488
0
                    changed = 1;
1489
0
                }
1490
0
                while (cc->corners[v0].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && cc->corners[v1].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
1491
0
            }
1492
0
        }
1493
1494
        /* Now, rotated cases: Can we cull the top half? */
1495
56
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[3].y < pfs->rect.p.y)
1496
16
        {
1497
            /* Check 0+3 off above. */
1498
16
            v0 = 0;
1499
16
            v1 = 3;
1500
16
            goto check_rot_above;
1501
16
        }
1502
40
        else if (cc->corners[1].y < pfs->rect.p.y && cc->corners[2].y < pfs->rect.p.y)
1503
3
        {
1504
            /* Check 1+2 off above. */
1505
3
            v0 = 1;
1506
3
            v1 = 2;
1507
19
check_rot_above:
1508
            /* At this point we know that the condition for the following loop is true, so it
1509
             * can be a do...while rather than a while. */
1510
19
            do
1511
80
            {
1512
                /* Let's form (Y coords only):
1513
                 *
1514
                 * c[v0].vertex     c[v1].vertex
1515
                 * m0               m1
1516
                 * c[v0^1].vertex   c[v1^1].vertex
1517
                 */
1518
80
                m0 = midpoint(cc->corners[0].y, cc->corners[1].y);
1519
80
                if (m0 >= pfs->rect.p.y)
1520
8
                    goto check_rot_above_quarter;
1521
72
                m1 = midpoint(cc->corners[3].y, cc->corners[2].y);
1522
72
                if (m1 >= pfs->rect.p.y)
1523
11
                    goto check_rot_above_quarter;
1524
                /* So, we can completely discard the top half of the patch. */
1525
61
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[1].x);
1526
61
                cc->corners[v0].y = m0;
1527
61
                cc->corners[v1].x = midpoint(cc->corners[3].x, cc->corners[2].x);
1528
61
                cc->corners[v1].y = m1;
1529
61
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1530
61
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1531
61
                changed = 1;
1532
61
            }
1533
61
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1534
0
            if (0)
1535
0
            {
1536
19
check_rot_above_quarter:
1537
                /* At this point we know that the condition for the following loop is true, so it
1538
                 * can be a do...while rather than a while. */
1539
19
                do
1540
19
                {
1541
                    /* Let's form (Y coords only):
1542
                     *
1543
                     * c[v0].vertex     c[v1].vertex
1544
                     * m0               m1
1545
                     * x                x
1546
                     * x                x
1547
                     * c[v0^1].vertex   c[v1^1].vertex
1548
                     */
1549
19
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1550
19
                    if (m0 >= pfs->rect.p.y)
1551
8
                        break;
1552
11
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1553
11
                    if (m1 >= pfs->rect.p.y)
1554
11
                        break;
1555
                    /* So, we can completely discard the top half of the patch. */
1556
0
                    cc->corners[v0].x = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1557
0
                    cc->corners[v0].y = m0;
1558
0
                    cc->corners[v1].x = quarterpoint(cc->corners[v1].x, cc->corners[v1^1].x);
1559
0
                    cc->corners[v1].y = m1;
1560
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^1].vertex.cc[0])/4;
1561
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^1].vertex.cc[0])/4;
1562
0
                    changed = 1;
1563
0
                }
1564
19
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1565
19
            }
1566
0
        }
1567
1568
        /* or the bottom half? */
1569
56
        if (cc->corners[0].y > pfs->rect.q.y && cc->corners[3].y > pfs->rect.q.y)
1570
3
        {
1571
            /* Check 0+3 off the bottom. */
1572
3
            v0 = 0;
1573
3
            v1 = 3;
1574
3
            goto check_rot_bottom;
1575
3
        }
1576
53
        else if (cc->corners[1].y > pfs->rect.q.y && cc->corners[2].y > pfs->rect.q.y)
1577
0
        {
1578
            /* Check 1+2 off the bottom. */
1579
0
            v0 = 1;
1580
0
            v1 = 2;
1581
3
check_rot_bottom:
1582
            /* At this point we know that the condition for the following loop is true, so it
1583
             * can be a do...while rather than a while. */
1584
3
            do
1585
8
            {
1586
                /* Let's form (Y coords only):
1587
                 *
1588
                 * c[v0].vertex     c[v1].vertex
1589
                 * m0               m1
1590
                 * c[v0^1].vertex   c[v1^1].vertex
1591
                 */
1592
8
                m0 = midpoint(cc->corners[0].y, cc->corners[1].y);
1593
8
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
1594
3
                    goto check_rot_bottom_quarter;
1595
5
                m1 = midpoint(cc->corners[3].y, cc->corners[2].y);
1596
5
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
1597
0
                    goto check_rot_bottom_quarter;
1598
                /* So, we can completely discard the left hand half of the patch. */
1599
5
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[1].x);
1600
5
                cc->corners[v0].y = m0;
1601
5
                cc->corners[v1].x = midpoint(cc->corners[3].x, cc->corners[2].x);
1602
5
                cc->corners[v1].y = m1;
1603
5
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1604
5
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1605
5
                changed = 1;
1606
5
            }
1607
5
            while (cc->corners[v0].y > pfs->rect.q.y+MIDPOINT_ACCURACY && cc->corners[v1].y > pfs->rect.q.y+MIDPOINT_ACCURACY);
1608
0
            if (0)
1609
0
            {
1610
3
check_rot_bottom_quarter:
1611
                /* At this point we know that the condition for the following loop is true, so it
1612
                 * can be a do...while rather than a while. */
1613
3
                do
1614
3
                {
1615
                    /* Let's form (Y coords only):
1616
                     *
1617
                     * c[v0].vertex     c[v1].vertex
1618
                     * x                x
1619
                     * x                x
1620
                     * m0               m1
1621
                     * c[v0^1].vertex   c[v1^1].vertex
1622
                     */
1623
3
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1624
3
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
1625
3
                        break;
1626
0
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1627
0
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
1628
0
                        break;
1629
                    /* So, we can completely discard the left hand half of the patch. */
1630
0
                    cc->corners[v0].x = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1631
0
                    cc->corners[v0].y = m0;
1632
0
                    cc->corners[v1].x = quarterpoint(cc->corners[v1].x, cc->corners[v1^1].x);
1633
0
                    cc->corners[v1].y = m1;
1634
0
                    cc->curve[v0].vertex.cc[0] = (cc->curve[v0].vertex.cc[0] + 3*cc->curve[v0^1].vertex.cc[0])/4;
1635
0
                    cc->curve[v1].vertex.cc[0] = (cc->curve[v1].vertex.cc[0] + 3*cc->curve[v1^1].vertex.cc[0])/4;
1636
0
                    changed = 1;
1637
0
                }
1638
3
                while (cc->corners[v0].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && cc->corners[v1].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
1639
3
            }
1640
0
        }
1641
56
    } while (changed);
1642
1643
21
    return A_fill_region_floats(pfs, cc, 0);
1644
73
}
1645
#undef midpoint
1646
#undef quarterpoint
1647
#undef MIDPOINT_ACCURACY
1648
#undef QUARTERPOINT_ACCURACY
1649
1650
static int
1651
A_fill_region(A_fill_state_t * pfs, patch_fill_state_t *pfs1)
1652
223k
{
1653
223k
    const gs_shading_A_t * const psh = pfs->psh;
1654
223k
    double x0 = psh->params.Coords[0] + pfs->delta.x * pfs->v0;
1655
223k
    double y0 = psh->params.Coords[1] + pfs->delta.y * pfs->v0;
1656
223k
    double x1 = psh->params.Coords[0] + pfs->delta.x * pfs->v1;
1657
223k
    double y1 = psh->params.Coords[1] + pfs->delta.y * pfs->v1;
1658
223k
    double h0 = pfs->u0, h1 = pfs->u1;
1659
223k
    corners_and_curves cc;
1660
223k
    int code;
1661
1662
223k
    double dx0 = pfs->delta.x * h0;
1663
223k
    double dy0 = pfs->delta.y * h0;
1664
223k
    double dx1 = pfs->delta.x * h1;
1665
223k
    double dy1 = pfs->delta.y * h1;
1666
1667
223k
    cc.curve[0].vertex.cc[0] = pfs->t0; /* The element cc[1] is set to a dummy value against */
1668
223k
    cc.curve[1].vertex.cc[0] = pfs->t1; /* interrupts while an idle processing in gxshade6.c .  */
1669
223k
    cc.curve[2].vertex.cc[0] = pfs->t1;
1670
223k
    cc.curve[3].vertex.cc[0] = pfs->t0;
1671
223k
    cc.corners[0].x = x0 + dy0;
1672
223k
    cc.corners[0].y = y0 - dx0;
1673
223k
    cc.corners[1].x = x1 + dy0;
1674
223k
    cc.corners[1].y = y1 - dx0;
1675
223k
    cc.corners[2].x = x1 + dy1;
1676
223k
    cc.corners[2].y = y1 - dx1;
1677
223k
    cc.corners[3].x = x0 + dy1;
1678
223k
    cc.corners[3].y = y0 - dx1;
1679
223k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[0].x, cc.corners[0].y, &cc.curve[0].vertex.p);
1680
223k
    if (code < 0)
1681
334
        goto fail;
1682
223k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[1].x, cc.corners[1].y, &cc.curve[1].vertex.p);
1683
223k
    if (code < 0)
1684
8
        goto fail;
1685
223k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[2].x, cc.corners[2].y, &cc.curve[2].vertex.p);
1686
223k
    if (code < 0)
1687
0
        goto fail;
1688
223k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[3].x, cc.corners[3].y, &cc.curve[3].vertex.p);
1689
223k
    if (code < 0)
1690
0
        goto fail;
1691
223k
    return subdivide_patch_fill(pfs1, cc.curve);
1692
342
fail:
1693
342
    if (code != gs_error_limitcheck)
1694
0
        return code;
1695
342
    code = gs_point_transform(cc.corners[0].x, cc.corners[0].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[0]);
1696
342
    if (code < 0)
1697
0
        return code;
1698
342
    code = gs_point_transform(cc.corners[1].x, cc.corners[1].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[1]);
1699
342
    if (code < 0)
1700
0
        return code;
1701
342
    code = gs_point_transform(cc.corners[2].x, cc.corners[2].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[2]);
1702
342
    if (code < 0)
1703
0
        return code;
1704
342
    code = gs_point_transform(cc.corners[3].x, cc.corners[3].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[3]);
1705
342
    if (code < 0)
1706
0
        return code;
1707
342
    return subdivide_patch_fill_floats(pfs1, &cc);
1708
342
}
1709
1710
static inline int
1711
gs_shading_A_fill_rectangle_aux(const gs_shading_t * psh0, const gs_rect * rect,
1712
                            const gs_fixed_rect *clip_rect,
1713
                            gx_device * dev, gs_gstate * pgs)
1714
135k
{
1715
135k
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
1716
135k
    gs_function_t * const pfn = psh->params.Function;
1717
135k
    gs_matrix cmat;
1718
135k
    gs_rect t_rect;
1719
135k
    A_fill_state_t state;
1720
135k
    patch_fill_state_t pfs1;
1721
135k
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
1722
135k
    float dd = d1 - d0;
1723
135k
    double t0, t1;
1724
135k
    gs_point dist;
1725
135k
    int code;
1726
1727
135k
    state.psh = psh;
1728
135k
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
1729
135k
    if (code < 0)
1730
35
        return code;
1731
135k
    pfs1.Function = pfn;
1732
135k
    pfs1.rect = *clip_rect;
1733
135k
    code = init_patch_fill_state(&pfs1);
1734
135k
    if (code < 0)
1735
0
        goto fail;
1736
135k
    pfs1.maybe_self_intersecting = false;
1737
135k
    pfs1.function_arg_shift = 1;
1738
    /*
1739
     * Compute the parameter range.  We construct a matrix in which
1740
     * (0,0) corresponds to t = 0 and (0,1) corresponds to t = 1,
1741
     * and use it to inverse-map the rectangle to be filled.
1742
     */
1743
135k
    cmat.tx = psh->params.Coords[0];
1744
135k
    cmat.ty = psh->params.Coords[1];
1745
135k
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
1746
135k
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
1747
135k
    cmat.yx = state.delta.x;
1748
135k
    cmat.yy = state.delta.y;
1749
135k
    cmat.xx = cmat.yy;
1750
135k
    cmat.xy = -cmat.yx;
1751
135k
    code = gs_bbox_transform_inverse(rect, &cmat, &t_rect);
1752
135k
    if (code < 0) {
1753
0
        code = 0; /* Swallow this silently */
1754
0
        goto fail;
1755
0
    }
1756
135k
    t0 = min(max(t_rect.p.y, 0), 1);
1757
135k
    t1 = max(min(t_rect.q.y, 1), 0);
1758
135k
    state.v0 = t0;
1759
135k
    state.v1 = t1;
1760
135k
    state.u0 = t_rect.p.x;
1761
135k
    state.u1 = t_rect.q.x;
1762
135k
    state.t0 = t0 * dd + d0;
1763
135k
    state.t1 = t1 * dd + d0;
1764
135k
    code = gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pgs),
1765
135k
                          &dist);
1766
135k
    if (code < 0)
1767
0
        goto fail;
1768
135k
    state.length = hypot(dist.x, dist.y); /* device space line length */
1769
135k
    code = A_fill_region(&state, &pfs1);
1770
135k
    if (psh->params.Extend[0] && t0 > t_rect.p.y) {
1771
44.0k
        if (code < 0)
1772
0
            goto fail;
1773
        /* Use the general algorithm, because we need the trapping. */
1774
44.0k
        state.v0 = t_rect.p.y;
1775
44.0k
        state.v1 = t0;
1776
44.0k
        state.t0 = state.t1 = t0 * dd + d0;
1777
44.0k
        code = A_fill_region(&state, &pfs1);
1778
44.0k
    }
1779
135k
    if (psh->params.Extend[1] && t1 < t_rect.q.y) {
1780
44.3k
        if (code < 0)
1781
0
            goto fail;
1782
        /* Use the general algorithm, because we need the trapping. */
1783
44.3k
        state.v0 = t1;
1784
44.3k
        state.v1 = t_rect.q.y;
1785
44.3k
        state.t0 = state.t1 = t1 * dd + d0;
1786
44.3k
        code = A_fill_region(&state, &pfs1);
1787
44.3k
    }
1788
135k
fail:
1789
135k
    gsicc_release_link(pfs1.icclink);
1790
135k
    if (term_patch_fill_state(&pfs1))
1791
0
        return_error(gs_error_unregistered); /* Must not happen. */
1792
135k
    return code;
1793
135k
}
1794
1795
int
1796
gs_shading_A_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
1797
                            const gs_fixed_rect * rect_clip,
1798
                            gx_device * dev, gs_gstate * pgs)
1799
135k
{
1800
135k
    return gs_shading_A_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
1801
135k
}
1802
1803
/* ---------------- Radial shading ---------------- */
1804
1805
/* Some notes on what I have struggled to understand about the following
1806
 * function. This function renders the 'tube' given by interpolating one
1807
 * circle to another.
1808
 *
1809
 * The first circle is at (x0, y0) with radius r0, and has 'color' t0.
1810
 * The other circle is at (x1, y1) with radius r1, and has 'color' t1.
1811
 *
1812
 * We perform this rendering by approximating each quadrant of the 'tube'
1813
 * by a tensor patch. The tensor patch is formed by taking a curve along
1814
 * 1/4 of the circumference of the first circle, a straight line to the
1815
 * equivalent point on the circumference of the second circle, a curve
1816
 * back along the circumference of the second circle, and then a straight
1817
 * line back to where we started.
1818
 *
1819
 * There is additional logic in this function that forms the directions of
1820
 * the curves differently for different quadrants. This is done to ensure
1821
 * that we always paint 'around' the tube from the back towards the front,
1822
 * so we don't get unexpected regions showing though. This is explained more
1823
 * below.
1824
 *
1825
 * The original code here examined the position change between the two
1826
 * circles dx and dy. Based upon this vector it would pick which quadrant/
1827
 * tensor patch to draw first. It would draw the quadrants/tensor patches
1828
 * in anticlockwise order. Presumably this was intended to be done so that
1829
 * the 'top' quadrant would be drawn last.
1830
 *
1831
 * Unfortunately this did not always work; see bug 692513. If the quadrants
1832
 * were rendered in the order 0,1,2,3, the rendering of 1 was leaving traces
1833
 * on top of 0, which was unexpected.
1834
 *
1835
 * I have therefore altered the code slightly; rather than picking a start
1836
 * quadrant and moving anticlockwise, we now draw the 'undermost' quadrant,
1837
 * then the two adjacent quadrants, then the topmost quadrant.
1838
 *
1839
 * For the purposes of explanation, we shall label the octants as below:
1840
 *
1841
 *     \2|1/       and Quadrants as:       |
1842
 *     3\|/0                            Q1 | Q0
1843
 *    ---+---                          ----+----
1844
 *     4/|\7                            Q2 | Q3
1845
 *     /5|6\                               |
1846
 *
1847
 * We find (dx,dy), the difference between the centres of the circles.
1848
 * We look to see which octant this falls in. Firstly, this tells us which
1849
 * quadrant of the circle we need to draw first (Octant n, starts with
1850
 * Quadrant floor(n/2)). Secondly, it tells us which direction to form the
1851
 * tensor patch in; we always want to draw from the side 'closest' to
1852
 * dx/dy to the side further away. This ensures that we don't overwrite
1853
 * pixels in the incorrect order as the patch decomposes.
1854
 */
1855
static int
1856
R_tensor_annulus(patch_fill_state_t *pfs,
1857
    double x0, double y0, double r0, double t0,
1858
    double x1, double y1, double r1, double t1)
1859
300
{
1860
300
    double dx = x1 - x0, dy = y1 - y0;
1861
300
    double d = hypot(dx, dy);
1862
300
    gs_point p0, p1, pc0, pc1;
1863
300
    int k, j, code, dirn;
1864
300
    bool inside = 0;
1865
1866
    /* pc0 and pc1 are the centres of the respective circles. */
1867
300
    pc0.x = x0, pc0.y = y0;
1868
300
    pc1.x = x1, pc1.y = y1;
1869
    /* Set p0 up so it's a unit vector giving the direction of 90 degrees
1870
     * to the right of the major axis as we move from p0c to p1c. */
1871
300
    if (r0 + d <= r1 || r1 + d <= r0) {
1872
        /* One circle is inside another one.
1873
           Use any subdivision,
1874
           but don't depend on dx, dy, which may be too small. */
1875
300
        p0.x = 0, p0.y = -1, dirn = 0;
1876
        /* Align stripes along radii for faster triangulation : */
1877
300
        inside = 1;
1878
300
        pfs->function_arg_shift = 1;
1879
300
    } else {
1880
        /* Must generate canonic quadrangle arcs,
1881
           because we approximate them with curves. */
1882
0
        if(dx >= 0) {
1883
0
            if (dy >= 0)
1884
0
                p0.x = 1, p0.y = 0, dirn = (dx >= dy ? 1 : 0);
1885
0
            else
1886
0
                p0.x = 0, p0.y = -1, dirn = (dx >= -dy ? 0 : 1);
1887
0
        } else {
1888
0
            if (dy >= 0)
1889
0
                p0.x = 0, p0.y = 1, dirn = (-dx >= dy ? 1 : 0);
1890
0
            else
1891
0
                p0.x = -1, p0.y = 0, dirn = (-dx >= -dy ? 0 : 1);
1892
0
        }
1893
0
        pfs->function_arg_shift = 0;
1894
0
    }
1895
    /* fixme: wish: cut invisible parts off.
1896
       Note : when r0 != r1 the invisible part is not a half circle. */
1897
1.48k
    for (k = 0; k < 4; k++) {
1898
1.19k
        gs_point p[12];
1899
1.19k
        patch_curve_t curve[4];
1900
1901
        /* Set p1 to be 90 degrees anticlockwise from p0 */
1902
1.19k
        p1.x = -p0.y; p1.y = p0.x;
1903
1.19k
        if (dirn == 0) { /* Clockwise */
1904
894
            make_quadrant_arc(p + 0, &pc0, &p1, &p0, r0);
1905
894
            make_quadrant_arc(p + 6, &pc1, &p0, &p1, r1);
1906
894
        } else { /* Anticlockwise */
1907
300
            make_quadrant_arc(p + 0, &pc0, &p0, &p1, r0);
1908
300
            make_quadrant_arc(p + 6, &pc1, &p1, &p0, r1);
1909
300
        }
1910
1.19k
        p[4].x = (p[3].x * 2 + p[6].x) / 3;
1911
1.19k
        p[4].y = (p[3].y * 2 + p[6].y) / 3;
1912
1.19k
        p[5].x = (p[3].x + p[6].x * 2) / 3;
1913
1.19k
        p[5].y = (p[3].y + p[6].y * 2) / 3;
1914
1.19k
        p[10].x = (p[9].x * 2 + p[0].x) / 3;
1915
1.19k
        p[10].y = (p[9].y * 2 + p[0].y) / 3;
1916
1.19k
        p[11].x = (p[9].x + p[0].x * 2) / 3;
1917
1.19k
        p[11].y = (p[9].y + p[0].y * 2) / 3;
1918
5.97k
        for (j = 0; j < 4; j++) {
1919
4.77k
            int jj = (j + inside) % 4;
1920
1921
4.77k
            if (gs_point_transform2fixed(&pfs->pgs->ctm,         p[j*3 + 0].x, p[j*3 + 0].y, &curve[jj].vertex.p) < 0)
1922
0
                gs_point_transform2fixed_clamped(&pfs->pgs->ctm, p[j*3 + 0].x, p[j*3 + 0].y, &curve[jj].vertex.p);
1923
1924
4.77k
            if (gs_point_transform2fixed(&pfs->pgs->ctm,         p[j*3 + 1].x, p[j*3 + 1].y, &curve[jj].control[0]) < 0)
1925
0
                gs_point_transform2fixed_clamped(&pfs->pgs->ctm, p[j*3 + 1].x, p[j*3 + 1].y, &curve[jj].control[0]);
1926
1927
4.77k
            if (gs_point_transform2fixed(&pfs->pgs->ctm,         p[j*3 + 2].x, p[j*3 + 2].y, &curve[jj].control[1]) < 0)
1928
0
                gs_point_transform2fixed_clamped(&pfs->pgs->ctm, p[j*3 + 2].x, p[j*3 + 2].y, &curve[jj].control[1]);
1929
4.77k
            curve[j].straight = (((j + inside) & 1) != 0);
1930
4.77k
        }
1931
1.19k
        curve[(0 + inside) % 4].vertex.cc[0] = t0;
1932
1.19k
        curve[(1 + inside) % 4].vertex.cc[0] = t0;
1933
1.19k
        curve[(2 + inside) % 4].vertex.cc[0] = t1;
1934
1.19k
        curve[(3 + inside) % 4].vertex.cc[0] = t1;
1935
1.19k
        curve[0].vertex.cc[1] = curve[1].vertex.cc[1] = 0; /* Initialize against FPE. */
1936
1.19k
        curve[2].vertex.cc[1] = curve[3].vertex.cc[1] = 0; /* Initialize against FPE. */
1937
1.19k
        code = patch_fill(pfs, curve, NULL, NULL);
1938
1.19k
        if (code < 0)
1939
6
            return code;
1940
        /* Move p0 to be ready for the next position */
1941
1.18k
        if (k == 0) {
1942
            /* p0 moves clockwise */
1943
300
            p1 = p0;
1944
300
            p0.x = p1.y; p0.y = -p1.x;
1945
300
            dirn = 0;
1946
888
        } else if (k == 1) {
1947
            /* p0 flips sides */
1948
300
            p0.x = -p0.x; p0.y = -p0.y;
1949
300
            dirn = 1;
1950
588
        } else if (k == 2) {
1951
            /* p0 moves anti-clockwise */
1952
294
            p1 = p0;
1953
294
            p0.x = -p1.y; p0.y = p1.x;
1954
294
            dirn = 0;
1955
294
        }
1956
1.18k
    }
1957
294
    return 0;
1958
300
}
1959
1960
/* Find the control points for two points on the arc of a circle
1961
 * the points must be within the same quadrant.
1962
 */
1963
static int find_arc_control_points(gs_point *from, gs_point *to, gs_point *from_control, gs_point *to_control, gs_point *centre)
1964
0
{
1965
0
    double from_tan_alpha, to_tan_alpha, from_alpha, to_alpha;
1966
0
    double half_inscribed_angle, intersect_x, intersect_y, intersect_dist;
1967
0
    double radius = sqrt(((from->x - centre->x) * (from->x - centre->x)) + ((from->y - centre->y) * (from->y - centre->y)));
1968
0
    double tangent_intersect_dist;
1969
0
    double F;
1970
0
    int quadrant;
1971
1972
    /* Quadrant 0 is upper right, numbered anti-clockwise.
1973
     * If the direction of the from->to is atni-clockwise, add 4
1974
     */
1975
0
    if (from->x > to->x) {
1976
0
        if (from->y > to->y) {
1977
0
            if (to->y >= centre->y)
1978
0
                quadrant = 1 + 4;
1979
0
            else
1980
0
                quadrant = 3;
1981
0
        } else {
1982
0
            if (to->x >= centre->x)
1983
0
                quadrant = 0 + 4;
1984
0
            else
1985
0
                quadrant = 2;
1986
0
        }
1987
0
    } else {
1988
0
        if (from->y > to->y) {
1989
0
            if (from->x >= centre->x)
1990
0
                quadrant = 0;
1991
0
            else
1992
0
                quadrant = 2 + 4;
1993
0
        } else {
1994
0
            if (from->x >= centre->x)
1995
0
                quadrant = 3 + 4;
1996
0
            else
1997
0
                quadrant = 1;
1998
0
        }
1999
0
    }
2000
2001
0
    switch(quadrant) {
2002
        /* quadrant 0, arc goes clockwise */
2003
0
        case 0:
2004
0
            if (from->x == centre->x) {
2005
0
                from_alpha = M_PI / 2;
2006
0
            } else {
2007
0
                from_tan_alpha = (from->y - centre->y) / (from->x - centre->x);
2008
0
                from_alpha = atan(from_tan_alpha);
2009
0
            }
2010
0
            to_tan_alpha = (to->y - centre->y) / (to->x - centre->x);
2011
0
            to_alpha = atan(to_tan_alpha);
2012
2013
0
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2014
0
            intersect_dist = radius / cos(half_inscribed_angle);
2015
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2016
2017
0
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2018
0
            intersect_y = centre->y + sin(to_alpha + half_inscribed_angle) * intersect_dist;
2019
0
            break;
2020
        /* quadrant 1, arc goes clockwise */
2021
0
        case 1:
2022
0
            from_tan_alpha = (from->y - centre->y) / (centre->x - from->x);
2023
0
            from_alpha = atan(from_tan_alpha);
2024
2025
0
            if (to->x == centre->x) {
2026
0
                to_alpha = M_PI / 2;
2027
0
            } else {
2028
0
                to_tan_alpha = (to->y - centre->y) / (centre->x - to->x);
2029
0
                to_alpha = atan(to_tan_alpha);
2030
0
            }
2031
2032
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2033
0
            intersect_dist = radius / cos(half_inscribed_angle);
2034
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2035
2036
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2037
0
            intersect_y = centre->y + sin(from_alpha + half_inscribed_angle) * intersect_dist;
2038
0
            break;
2039
        /* quadrant 2, arc goes clockwise */
2040
0
        case 2:
2041
0
            if (from->x == centre->x) {
2042
0
                from_alpha = M_PI / 2;
2043
0
            } else {
2044
0
                from_tan_alpha = (centre->y - from->y) / (centre->x - from->x);
2045
0
                from_alpha = atan(from_tan_alpha);
2046
0
            }
2047
2048
0
            to_tan_alpha = (centre->y - to->y) / (centre->x - to->x);
2049
0
            to_alpha = atan(to_tan_alpha);
2050
2051
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2052
0
            intersect_dist = radius / cos(half_inscribed_angle);
2053
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2054
2055
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2056
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2057
0
            break;
2058
        /* quadrant 3, arc goes clockwise */
2059
0
        case 3:
2060
0
            from_tan_alpha = (centre->y - from->y) / (from->x - centre->x);
2061
0
            from_alpha = atan(from_tan_alpha);
2062
2063
0
            if (to->x == centre->x) {
2064
0
                to_alpha = M_PI / 2;
2065
0
            } else {
2066
0
                to_tan_alpha = (centre->y - to->y) / (to->x - centre->x);
2067
0
                to_alpha = atan(to_tan_alpha);
2068
0
            }
2069
2070
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2071
0
            intersect_dist = radius / cos(half_inscribed_angle);
2072
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2073
2074
0
            intersect_x = centre->x + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2075
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2076
0
            break;
2077
        /* quadrant 0, arc goes anti-clockwise */
2078
0
        case 4:
2079
0
            from_tan_alpha = (from->y - centre->y) / (from->x - centre->x);
2080
0
            from_alpha = atan(from_tan_alpha);
2081
2082
0
            if (to->y == centre->y)
2083
0
                to_alpha = M_PI / 2;
2084
0
            else {
2085
0
                to_tan_alpha = (to->y - centre->y) / (to->x - centre->x);
2086
0
                to_alpha = atan(to_tan_alpha);
2087
0
            }
2088
2089
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2090
0
            intersect_dist = radius / cos(half_inscribed_angle);
2091
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2092
2093
0
            intersect_x = centre->x + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2094
0
            intersect_y = centre->y + sin(from_alpha + half_inscribed_angle) * intersect_dist;
2095
0
            break;
2096
        /* quadrant 1, arc goes anti-clockwise */
2097
0
        case 5:
2098
0
            from_tan_alpha = (centre->x - from->x) / (from->y - centre->y);
2099
0
            from_alpha = atan(from_tan_alpha);
2100
2101
0
            if (to->y == centre->y) {
2102
0
                to_alpha = M_PI / 2;
2103
0
            }
2104
0
            else {
2105
0
                to_tan_alpha = (centre->x - to->x) / (to->y - centre->y);
2106
0
                to_alpha = atan(to_tan_alpha);
2107
0
            }
2108
2109
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2110
0
            intersect_dist = radius / cos(half_inscribed_angle);
2111
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2112
2113
0
            intersect_x = centre->x - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2114
0
            intersect_y = centre->y + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2115
0
            break;
2116
        /* quadrant 2, arc goes anti-clockwise */
2117
0
        case 6:
2118
0
            from_tan_alpha = (from->y - centre->y) / (centre->x - from->x);
2119
0
            from_alpha = atan(from_tan_alpha);
2120
2121
0
            if (to->x == centre->x) {
2122
0
                to_alpha = M_PI / 2;
2123
0
            } else {
2124
0
                to_tan_alpha = (centre->y - to->y) / (centre->x - to->x);
2125
0
                to_alpha = atan(to_tan_alpha);
2126
0
            }
2127
2128
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2129
0
            intersect_dist = radius / cos(half_inscribed_angle);
2130
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2131
2132
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2133
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2134
0
            break;
2135
        /* quadrant 3, arc goes anti-clockwise */
2136
0
        case 7:
2137
0
            if (from->x == centre->x) {
2138
0
                from_alpha = M_PI / 2;
2139
0
            } else {
2140
0
                from_tan_alpha = (centre->y - from->y) / (from->x - centre->x);
2141
0
                from_alpha = atan(from_tan_alpha);
2142
0
            }
2143
0
            to_tan_alpha = (centre->y - to->y) / (to->x - centre->x);
2144
0
            to_alpha = atan(to_tan_alpha);
2145
2146
0
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2147
0
            intersect_dist = radius / cos(half_inscribed_angle);
2148
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2149
2150
0
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2151
0
            intersect_y = centre->y - sin(to_alpha + half_inscribed_angle) * intersect_dist;
2152
0
            break;
2153
0
    }
2154
2155
0
    F = (4.0 / 3.0) / (1 + sqrt(1 + ((tangent_intersect_dist / radius) * (tangent_intersect_dist / radius))));
2156
2157
0
    from_control->x = from->x - ((from->x - intersect_x) * F);
2158
0
    from_control->y = from->y - ((from->y - intersect_y) * F);
2159
0
    to_control->x = to->x - ((to->x - intersect_x) * F);
2160
0
    to_control->y = to->y - ((to->y - intersect_y) * F);
2161
2162
0
    return 0;
2163
0
}
2164
2165
/* Create a 'patch_curve' element whch is a straight line between two points */
2166
static int patch_lineto(gs_matrix_fixed *ctm, gs_point *from, gs_point *to, patch_curve_t *p, float t)
2167
0
{
2168
0
    double x_1third, x_2third, y_1third, y_2third;
2169
2170
0
    x_1third = (to->x - from->x) / 3;
2171
0
    x_2third = x_1third * 2;
2172
0
    y_1third = (to->y - from->y) / 3;
2173
0
    y_2third = y_1third * 2;
2174
2175
0
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2176
0
    gs_point_transform2fixed(ctm, from->x + x_1third, from->y + y_1third, &p->control[0]);
2177
0
    gs_point_transform2fixed(ctm, from->x + x_2third, from->y + y_2third, &p->control[1]);
2178
2179
0
    p->vertex.cc[0] = t;
2180
0
    p->vertex.cc[1] = t;
2181
0
    p->straight = 1;
2182
2183
0
    return 0;
2184
0
}
2185
2186
static int patch_curveto(gs_matrix_fixed *ctm, gs_point *centre, gs_point *from, gs_point *to, patch_curve_t *p, float t)
2187
0
{
2188
0
    gs_point from_control, to_control;
2189
2190
0
    find_arc_control_points(from, to, &from_control, &to_control, centre);
2191
2192
0
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2193
0
    gs_point_transform2fixed(ctm, from_control.x, from_control.y, &p->control[0]);
2194
0
    gs_point_transform2fixed(ctm, to_control.x, to_control.y, &p->control[1]);
2195
0
    p->vertex.cc[0] = t;
2196
0
    p->vertex.cc[1] = t;
2197
0
    p->straight = 0;
2198
2199
0
    return 0;
2200
0
}
2201
2202
static int draw_quarter_annulus(patch_fill_state_t *pfs, gs_point *centre, double radius, gs_point *corner, float t)
2203
0
{
2204
0
    gs_point p0, p1, initial;
2205
0
    patch_curve_t p[4];
2206
0
    int code;
2207
2208
0
    if (corner->x > centre->x) {
2209
0
        initial.x = centre->x + radius;
2210
0
    }
2211
0
    else {
2212
0
        initial.x = centre->x - radius;
2213
0
    }
2214
0
    initial.y = centre->y;
2215
2216
0
    p1.x = initial.x;
2217
0
    p1.y = corner->y;
2218
0
    patch_lineto(&pfs->pgs->ctm, &initial, &p1, &p[0], t);
2219
0
    p0.x = centre->x;
2220
0
    p0.y = p1.y;
2221
0
    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &p[1], t);
2222
0
    p1.x = centre->x;
2223
0
    if (centre->y > corner->y) {
2224
0
        p1.y = centre->y - radius;
2225
0
    } else {
2226
0
        p1.y = centre->y + radius;
2227
0
    }
2228
0
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2229
0
    patch_curveto(&pfs->pgs->ctm, centre, &p1, &initial, &p[3], t);
2230
0
    code = patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL);
2231
0
    if (code < 0)
2232
0
        return code;
2233
2234
0
    if (corner->x > centre->x)
2235
0
        initial.x = corner->x - (corner->x - (centre->x + radius));
2236
0
    else
2237
0
        initial.x = centre->x - radius;
2238
0
    initial.y = corner->y;
2239
0
    patch_lineto(&pfs->pgs->ctm, corner, &initial, &p[0], t);
2240
2241
0
    p0.x = initial.x;
2242
0
    p0.y = centre->y;
2243
0
    patch_lineto(&pfs->pgs->ctm, &initial, &p0, &p[1], t);
2244
2245
0
    p1.y = p0.y;
2246
0
    p1.x = corner->x;
2247
0
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2248
0
    patch_lineto(&pfs->pgs->ctm, &p1, corner, &p[3], t);
2249
2250
0
    return (patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL));
2251
0
}
2252
2253
static int R_tensor_annulus_extend_tangent(patch_fill_state_t *pfs,
2254
    double x0, double y0, double r0, double t0,
2255
    double x1, double y1, double r1, double t1, double r2)
2256
0
{
2257
0
    patch_curve_t curve[4];
2258
0
    gs_point p0, p1;
2259
0
    int code = 0, q = 0;
2260
2261
    /* special case axis aligned circles. Its quicker to handle these specially as it
2262
     * avoid lots of trigonometry in the general case code, and avoids us
2263
     * having to watch out for infinity as the result of tan() operations.
2264
     */
2265
0
    if (x0 == x1 || y0 == y1) {
2266
0
        if (x0 == x1 && y0 > y1) {
2267
            /* tangent at top of circles */
2268
0
            p0.x = x1, p0.y = y1;
2269
0
            p1.x = x1 + r2, p1.y = y1 - r2;
2270
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2271
0
            p1.x = x1 - r2, p1.y = y1 - r2;
2272
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2273
0
            p1.x = x1 + r2, p1.y = y1 + r1;
2274
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2275
0
            p1.x = x1 - r2, p1.y = y1 + r1;
2276
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2277
0
        }
2278
0
        if (x0 == x1 && y0 < y1) {
2279
            /* tangent at bottom of circles */
2280
0
            p0.x = x1, p0.y = y1;
2281
0
            p1.x = x1 + r2, p1.y = y1 + r2;
2282
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2283
0
            p1.x = x1 - r2, p1.y = y1 + r2;
2284
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2285
0
            p1.x = x1 + r2, p1.y = y1 - r1;
2286
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2287
0
            p1.x = x1 - r2, p1.y = y1 - r1;
2288
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2289
0
        }
2290
0
        if (y0 == y1 && x0 > x1) {
2291
            /* tangent at right of circles */
2292
0
            p0.x = x1, p0.y = y1;
2293
0
            p1.x = x1 - r2, p1.y = y1 - r2;
2294
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2295
0
            p1.x = x1 - r2, p1.y = y1 + r2;
2296
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2297
0
            p1.x = x1 + r1, p1.y = y1 + r2;
2298
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2299
0
            p1.x = x1 + r1, p1.y = y1 - r2;
2300
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2301
0
        }
2302
0
        if (y0 == y1 && x0 < x1) {
2303
            /* tangent at left of circles */
2304
0
            p0.x = x1, p0.y = y1;
2305
0
            p1.x = x1 + r2, p1.y = y1 - r2;
2306
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2307
0
            p1.x = x1 + r2, p1.y = y1 + r2;
2308
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2309
0
            p1.x = x1 - r1, p1.y = y1 + r2;
2310
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2311
0
            p1.x = x1 - r1, p1.y = y1 - r2;
2312
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2313
0
        }
2314
0
    }
2315
0
    else {
2316
0
        double tx, ty, endx, endy, intersectx, intersecty, alpha, sinalpha, cosalpha, tanalpha;
2317
0
        gs_point centre;
2318
2319
        /* First lets figure out which quadrant the smaller circle is in (we always
2320
         * get called to fill from the larger circle), x0, y0, r0 is the smaller circle.
2321
         */
2322
0
        if (x0 < x1) {
2323
0
            if (y0 < y1)
2324
0
                q = 2;
2325
0
            else
2326
0
                q = 1;
2327
0
        } else {
2328
0
            if (y0 < y1)
2329
0
                q = 3;
2330
0
            else
2331
0
                q = 0;
2332
0
        }
2333
0
        switch(q) {
2334
0
            case 0:
2335
                /* We have two four-sided elements, from the tangent point
2336
                 * each side, to the point where the tangent crosses an
2337
                 * axis of the larger circle. A line back to the edge
2338
                 * of the larger circle, a line to the point where an axis
2339
                 * crosses the smaller circle, then an arc back to the starting point.
2340
                 */
2341
                /* Figure out the tangent point */
2342
                /* sin (angle) = y1 - y0 / r1 - r0
2343
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2344
                 */
2345
0
                ty = y1 + ((y0 - y1) / (r1 - r0)) * r1;
2346
0
                tx = x1 + ((x0 - x1) / (r1 - r0)) * r1;
2347
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2348
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2349
                 * as its the same angle where it crosses the axis of the larger circle.
2350
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2351
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2352
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2353
                 * circle
2354
                 */
2355
0
                sinalpha = (y0 - y1) / (r1 - r0);
2356
0
                alpha = asin(sinalpha);
2357
0
                cosalpha = cos(alpha);
2358
0
                intersectx = x1 + (r1 / cosalpha);
2359
0
                intersecty = y1;
2360
2361
0
                p0.x = tx, p0.y = ty;
2362
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty - (ty - intersecty) / 2;
2363
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2364
0
                p0.x = intersectx, p0.y = intersecty;
2365
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2366
0
                p1.x = x1 + r1, p1.y = y1;
2367
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2368
0
                p0.x = tx, p0.y = ty;
2369
0
                centre.x = x1, centre.y = y1;
2370
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2371
0
                code = patch_fill(pfs, curve, NULL, NULL);
2372
0
                if (code < 0)
2373
0
                    return code;
2374
2375
0
                if (intersectx < x1 + r2) {
2376
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2377
                     * An 'annulus' where the right edge is less than the normal extent and a
2378
                     * quad which is a rectangle with one corner chopped of at an angle.
2379
                     */
2380
0
                    p0.x = x1, p0.y = y1;
2381
0
                    p1.x = intersectx, p1.y = y1 - r2;
2382
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2383
0
                    endx = x1 + r2;
2384
0
                    endy = y1 - (tan ((M_PI / 2) - alpha)) * (endx - intersectx);
2385
0
                    p0.x = intersectx, p0.y = y1;
2386
0
                    p1.x = x1 + r2, p1.y = endy;
2387
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2388
0
                    p0.x = x1 + r2, p0.y = y0 - r2;
2389
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2390
0
                    p1.x = intersectx, p1.y = p0.y;
2391
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2392
0
                    p0.x = intersectx, p0.y = y1;
2393
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2394
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2395
0
                    if (code < 0)
2396
0
                        return code;
2397
2398
0
                } else {
2399
                    /* Quadrant 3 is a normal quarter annulua */
2400
0
                    p0.x = x1, p0.y = y1;
2401
0
                    p1.x = x1 + r2, p1.y = y1 - r2;
2402
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2403
0
                }
2404
2405
                /* Q2 is always a full annulus... */
2406
0
                p0.x = x1, p0.y = y1;
2407
0
                p1.x = x1 - r2, p1.y = y1 - r2;
2408
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2409
2410
                /* alpha is now the angle between the x axis and the tangent to the
2411
                 * circles.
2412
                 */
2413
0
                alpha = (M_PI / 2) - alpha;
2414
0
                cosalpha = cos(alpha);
2415
0
                endy = y1 + (r1 / cosalpha);
2416
0
                endx = x1;
2417
2418
0
                p0.x = tx, p0.y = ty;
2419
0
                p1.x = endx - ((endx - tx) / 2), p1.y = endy - ((endy - ty) / 2);
2420
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2421
0
                p0.x = endx, p0.y = endy;
2422
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2423
0
                p1.x = x1, p1.y = y1 + r1;
2424
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2425
0
                p0.x = tx, p0.y = ty;
2426
0
                centre.x = x1, centre.y = y1;
2427
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2428
0
                code = patch_fill(pfs, curve, NULL, NULL);
2429
0
                if (code < 0)
2430
0
                    return code;
2431
2432
                /* Q1 is simimlar to Q3, either a full quarter annulus
2433
                 * or a partial one, depending on where the tangent crosses
2434
                 * the y axis
2435
                 */
2436
0
                tanalpha = tan(alpha);
2437
0
                intersecty = y1 + tanalpha * (r2 + (intersectx - x1));
2438
0
                intersectx = x1 - r2;
2439
2440
0
                if (endy < y1 + r2) {
2441
                    /* didn't get all the way to the edge, quadrant 1 is composed of 2 quads :-(
2442
                     * An 'annulus' where the right edge is less than the normal extent and a
2443
                     * quad which is a rectangle with one corner chopped of at an angle.
2444
                     */
2445
0
                    p0.x = x1, p0.y = y1;
2446
0
                    p1.x = x1 - r2, p1.y = endy;
2447
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2448
0
                    p0.x = x1, p0.y = y1 + r1;
2449
0
                    p1.x = x1, p1.y = endy;
2450
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2451
0
                    p0.x = x1 - r2, p0.y = intersecty;
2452
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2453
0
                    p1.x = p0.x, p1.y = y1 + r1;
2454
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2455
0
                    p0.x = x1, p0.y = y1 + r1;
2456
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2457
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2458
0
                    if (code < 0)
2459
0
                        return code;
2460
0
                } else {
2461
                    /* Quadrant 1 is a normal quarter annulua */
2462
0
                    p0.x = x1, p0.y = y1;
2463
0
                    p1.x = x1 - r2, p1.y = y1 + r2;
2464
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2465
0
                }
2466
0
                break;
2467
0
            case 1:
2468
                /* We have two four-sided elements, from the tangent point
2469
                 * each side, to the point where the tangent crosses an
2470
                 * axis of the larger circle. A line back to the edge
2471
                 * of the larger circle, a line to the point where an axis
2472
                 * crosses the smaller circle, then an arc back to the starting point.
2473
                 */
2474
                /* Figure out the tangent point */
2475
                /* sin (angle) = y1 - y0 / r1 - r0
2476
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2477
                 */
2478
0
                ty = y1 + ((y0 - y1) / (r1 - r0)) * r1;
2479
0
                tx = x1 - ((x1 - x0) / (r1 - r0)) * r1;
2480
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2481
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2482
                 * as its the same angle where it crosses the axis of the larger circle.
2483
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2484
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2485
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2486
                 * circle
2487
                 */
2488
0
                sinalpha = (y0 - y1) / (r1 - r0);
2489
0
                alpha = asin(sinalpha);
2490
0
                cosalpha = cos(alpha);
2491
0
                intersectx = x1 - (r1 / cosalpha);
2492
0
                intersecty = y1;
2493
2494
0
                p0.x = tx, p0.y = ty;
2495
0
                p1.x = tx - (tx - intersectx) / 2, p1.y = ty - (ty - intersecty) / 2;
2496
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2497
0
                p0.x = intersectx, p0.y = intersecty;
2498
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2499
0
                p1.x = x1 - r1, p1.y = y1;
2500
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2501
0
                p0.x = tx, p0.y = ty;
2502
0
                centre.x = x1, centre.y = y1;
2503
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2504
0
                code = patch_fill(pfs, curve, NULL, NULL);
2505
0
                if (code < 0)
2506
0
                    return code;
2507
2508
0
                if (intersectx > x1 - r2) {
2509
                    /* didn't get all the way to the edge, quadrant 2 is composed of 2 quads :-(
2510
                     * An 'annulus' where the right edge is less than the normal extent and a
2511
                     * quad which is a rectangle with one corner chopped of at an angle.
2512
                     */
2513
0
                    p0.x = x1, p0.y = y1;
2514
0
                    p1.x = intersectx, p1.y = y1 - r2;
2515
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2516
0
                    endx = x1 - r2;
2517
0
                    endy = y1 - (tan ((M_PI / 2) - alpha)) * (intersectx - endx);
2518
0
                    p0.x = intersectx, p0.y = y1;
2519
0
                    p1.x = x1 - r2, p1.y = endy;
2520
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2521
0
                    p0.x = x1 - r2, p0.y = y0 - r2;
2522
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2523
0
                    p1.x = intersectx, p1.y = p0.y;
2524
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2525
0
                    p0.x = intersectx, p0.y = y1;
2526
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2527
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2528
0
                    if (code < 0)
2529
0
                        return code;
2530
2531
0
                } else {
2532
                    /* Quadrant 2 is a normal quarter annulua */
2533
0
                    p0.x = x1, p0.y = y1;
2534
0
                    p1.x = x1 - r2, p1.y = y1 - r2;
2535
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2536
0
                }
2537
2538
                /* Q3 is always a full annulus... */
2539
0
                p0.x = x1, p0.y = y1;
2540
0
                p1.x = x1 + r2, p1.y = y1 - r2;
2541
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2542
2543
                /* alpha is now the angle between the x axis and the tangent to the
2544
                 * circles.
2545
                 */
2546
0
                alpha = (M_PI / 2) - alpha;
2547
0
                cosalpha = cos(alpha);
2548
0
                endy = y1 + (r1 / cosalpha);
2549
0
                endx = x1;
2550
2551
0
                p0.x = tx, p0.y = ty;
2552
0
                p1.x = endx + ((tx - endx) / 2), p1.y = endy - ((endy - ty) / 2);
2553
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2554
0
                p0.x = endx, p0.y = endy;
2555
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2556
0
                p1.x = x1, p1.y = y1 + r1;
2557
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2558
0
                p0.x = tx, p0.y = ty;
2559
0
                centre.x = x1, centre.y = y1;
2560
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2561
0
                code = patch_fill(pfs, curve, NULL, NULL);
2562
0
                if (code < 0)
2563
0
                    return code;
2564
2565
                /* Q0 is simimlar to Q2, either a full quarter annulus
2566
                 * or a partial one, depending on where the tangent crosses
2567
                 * the y axis
2568
                 */
2569
0
                tanalpha = tan(alpha);
2570
0
                intersecty = y1 + tanalpha * (r2 + (x1 - intersectx));
2571
0
                intersectx = x1 + r2;
2572
2573
0
                if (endy < y1 + r2) {
2574
                    /* didn't get all the way to the edge, quadrant 0 is composed of 2 quads :-(
2575
                     * An 'annulus' where the right edge is less than the normal extent and a
2576
                     * quad which is a rectangle with one corner chopped of at an angle.
2577
                     */
2578
0
                    p0.x = x1, p0.y = y1;
2579
0
                    p1.x = x1 + r2, p1.y = endy;
2580
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2581
0
                    p0.x = x1, p0.y = y1 + r1;
2582
0
                    p1.x = x1, p1.y = endy;
2583
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2584
0
                    p0.x = x1 + r2, p0.y = intersecty;
2585
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2586
0
                    p1.x = p0.x, p1.y = y1 + r1;
2587
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2588
0
                    p0.x = x1, p0.y = y1 + r1;
2589
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2590
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2591
0
                    if (code < 0)
2592
0
                        return code;
2593
0
                } else {
2594
                    /* Quadrant 0 is a normal quarter annulua */
2595
0
                    p0.x = x1, p0.y = y1;
2596
0
                    p1.x = x1 + r2, p1.y = y1 + r2;
2597
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2598
0
                }
2599
0
                break;
2600
0
            case 2:
2601
                /* We have two four-sided elements, from the tangent point
2602
                 * each side, to the point where the tangent crosses an
2603
                 * axis of the larger circle. A line back to the edge
2604
                 * of the larger circle, a line to the point where an axis
2605
                 * crosses the smaller circle, then an arc back to the starting point.
2606
                 */
2607
                /* Figure out the tangent point */
2608
                /* sin (angle) = y1 - y0 / r1 - r0
2609
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2610
                 */
2611
0
                ty = y1 - ((y1 - y0) / (r1 - r0)) * r1;
2612
0
                tx = x1 - ((x1 - x0) / (r1 - r0)) * r1;
2613
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2614
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2615
                 * as its the same angle where it crosses the axis of the larger circle.
2616
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2617
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2618
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2619
                 * circle
2620
                 */
2621
0
                sinalpha = (y1 - y0) / (r1 - r0);
2622
0
                alpha = asin(sinalpha);
2623
0
                cosalpha = cos(alpha);
2624
0
                intersectx = x1 - (r1 / cosalpha);
2625
0
                intersecty = y1;
2626
2627
0
                p0.x = tx, p0.y = ty;
2628
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty - (ty - intersecty) / 2;
2629
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2630
0
                p0.x = intersectx, p0.y = intersecty;
2631
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2632
0
                p1.x = x1 - r1, p1.y = y1;
2633
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2634
0
                p0.x = tx, p0.y = ty;
2635
0
                centre.x = x1, centre.y = y1;
2636
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2637
0
                code = patch_fill(pfs, curve, NULL, NULL);
2638
0
                if (code < 0)
2639
0
                    return code;
2640
0
                if (intersectx > x1 - r2) {
2641
                    /* didn't get all the way to the edge, quadrant 1 is composed of 2 quads :-(
2642
                     * An 'annulus' where the right edge is less than the normal extent and a
2643
                     * quad which is a rectangle with one corner chopped of at an angle.
2644
                     */
2645
0
                    p0.x = x1, p0.y = y1;
2646
0
                    p1.x = intersectx, p1.y = y1 + r2;
2647
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2648
0
                    endy = y1+r2;
2649
0
                    endx = intersectx - ((endy - intersecty) / (tan ((M_PI / 2) - alpha)));
2650
0
                    p0.x = intersectx, p0.y = y1;
2651
0
                    p1.x = endx, p1.y = endy;
2652
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2653
0
                    p0.x = x1 - r1, p0.y = endy;
2654
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2655
0
                    p1.x = x1 - r1, p1.y = y1;
2656
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2657
0
                    p0.x = intersectx, p0.y = y1;
2658
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2659
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2660
0
                    if (code < 0)
2661
0
                        return code;
2662
0
                } else {
2663
                    /* Quadrant 1 is a normal quarter annulua */
2664
0
                    p0.x = x1, p0.y = y1;
2665
0
                    p1.x = x1 - r2, p1.y = y1 + r2;
2666
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2667
0
                }
2668
2669
                /* Q0 is always a full annulus... */
2670
0
                p0.x = x1, p0.y = y1;
2671
0
                p1.x = x1 + r2, p1.y = y1 + r2;
2672
0
                if (p1.y < 0)
2673
0
                    p1.y = 0;
2674
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2675
2676
                /* alpha is now the angle between the x axis and the tangent to the
2677
                 * circles.
2678
                 */
2679
0
                alpha = (M_PI / 2) - alpha;
2680
0
                cosalpha = cos(alpha);
2681
0
                endy = y1 - (r1 / cosalpha);
2682
0
                endx = x1;
2683
2684
0
                p0.x = tx, p0.y = ty;
2685
0
                p1.x = endx + ((endx - tx) / 2), p1.y = endy - ((ty - endy) / 2);
2686
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2687
0
                p0.x = endx, p0.y = endy;
2688
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2689
0
                p1.x = x1, p1.y = y1 - r1;
2690
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2691
0
                p0.x = tx, p0.y = ty;
2692
0
                centre.x = x1, centre.y = y1;
2693
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2694
0
                code = patch_fill(pfs, curve, NULL, NULL);
2695
0
                if (code < 0)
2696
0
                    return code;
2697
2698
                /* Q3 is simimlar to Q1, either a full quarter annulus
2699
                 * or a partial one, depending on where the tangent crosses
2700
                 * the y axis
2701
                 */
2702
0
                tanalpha = tan(alpha);
2703
0
                intersecty = y1 - tanalpha * (r2 + (x1 - intersectx));
2704
0
                intersectx = x1 + r2;
2705
2706
0
                if (endy > y1 - r2) {
2707
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2708
                     * An 'annulus' where the right edge is less than the normal extent and a
2709
                     * quad which is a rectangle with one corner chopped of at an angle.
2710
                     */
2711
0
                    p0.x = x1, p0.y = y1;
2712
0
                    p1.x = x1 + r2, p1.y = endy;
2713
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2714
0
                    p0.x = x1, p0.y = y1 - r1;
2715
0
                    p1.x = x1, p1.y = endy;
2716
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2717
0
                    p0.x = x1 + r2, p0.y = intersecty;
2718
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2719
0
                    p1.x = p0.x, p1.y = y1 - r1;
2720
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2721
0
                    p0.x = x1, p0.y = y1 - r1;
2722
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2723
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2724
0
                    if (code < 0)
2725
0
                        return code;
2726
0
                } else {
2727
                    /* Quadrant 1 is a normal quarter annulua */
2728
0
                    p0.x = x1, p0.y = y1;
2729
0
                    p1.x = x1 + r2, p1.y = y1 - r2;
2730
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2731
0
                }
2732
0
                break;
2733
0
            case 3:
2734
                /* We have two four-sided elements, from the tangent point
2735
                 * each side, to the point where the tangent crosses an
2736
                 * axis of the larger circle. A line back to the edge
2737
                 * of the larger circle, a line to the point where an axis
2738
                 * crosses the smaller circle, then an arc back to the starting point.
2739
                 */
2740
                /* Figure out the tangent point */
2741
                /* sin (angle) = y1 - y0 / r1 - r0
2742
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2743
                 */
2744
0
                ty = y1 - ((y1 - y0) / (r1 - r0)) * r1;
2745
0
                tx = x1 + ((x0 - x1) / (r1 - r0)) * r1;
2746
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2747
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2748
                 * as its the same angle where it crosses the axis of the larger circle.
2749
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2750
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2751
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2752
                 * circle
2753
                 */
2754
0
                sinalpha = (y1 - y0) / (r1 - r0);
2755
0
                alpha = asin(sinalpha);
2756
0
                cosalpha = cos(alpha);
2757
0
                intersectx = x1 + (r1 / cosalpha);
2758
0
                intersecty = y1;
2759
2760
0
                p0.x = tx, p0.y = ty;
2761
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty + (intersecty - ty) / 2;
2762
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2763
0
                p0.x = intersectx, p0.y = intersecty;
2764
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2765
0
                p1.x = x1 + r1, p1.y = y1;
2766
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2767
0
                p0.x = tx, p0.y = ty;
2768
0
                centre.x = x1, centre.y = y1;
2769
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2770
0
                code = patch_fill(pfs, curve, NULL, NULL);
2771
0
                if (code < 0)
2772
0
                    return code;
2773
0
                if (intersectx < x1 + r2) {
2774
                    /* didn't get all the way to the edge, quadrant 0 is composed of 2 quads :-(
2775
                     * An 'annulus' where the right edge is less than the normal extent and a
2776
                     * quad which is a rectangle with one corner chopped of at an angle.
2777
                     */
2778
0
                    p0.x = x1, p0.y = y1;
2779
0
                    p1.x = intersectx, p1.y = y1 + r2;
2780
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2781
0
                    endy = y1 + r2;
2782
0
                    endx = intersectx + ((endy - intersecty) / (tan ((M_PI / 2) - alpha)));
2783
0
                    p0.x = intersectx, p0.y = y1;
2784
0
                    p1.x = endx, p1.y = endy;
2785
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2786
0
                    p0.x = x1 + r1, p0.y = endy;
2787
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2788
0
                    p1.x = x1 + r1, p1.y = y1;
2789
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2790
0
                    p0.x = intersectx, p0.y = y1;
2791
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2792
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2793
0
                    if (code < 0)
2794
0
                        return code;
2795
2796
0
                } else {
2797
                    /* Quadrant 0 is a normal quarter annulua */
2798
0
                    p0.x = x1, p0.y = y1;
2799
0
                    p1.x = x1 + r2, p1.y = y1 + r2;
2800
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2801
0
                }
2802
                /* Q1 is always a full annulus... */
2803
0
                p0.x = x1, p0.y = y1;
2804
0
                p1.x = x1 - r2, p1.y = y1 + r2;
2805
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2806
2807
                /* alpha is now the angle between the x axis and the tangent to the
2808
                 * circles.
2809
                 */
2810
0
                alpha = (M_PI / 2) - alpha;
2811
0
                cosalpha = cos(alpha);
2812
0
                endy = y1 - (r1 / cosalpha);
2813
0
                endx = x1;
2814
2815
0
                p0.x = tx, p0.y = ty;
2816
0
                p1.x = endx + ((tx - endx) / 2), p1.y = endy + ((ty - endy) / 2);
2817
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2818
0
                p0.x = endx, p0.y = endy;
2819
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2820
0
                p1.x = x1, p1.y = y1 - r1;
2821
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2822
0
                p0.x = tx, p0.y = ty;
2823
0
                centre.x = x1, centre.y = y1;
2824
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2825
0
                code = patch_fill(pfs, curve, NULL, NULL);
2826
0
                if (code < 0)
2827
0
                    return code;
2828
2829
                /* Q3 is simimlar to Q1, either a full quarter annulus
2830
                 * or a partial one, depending on where the tangent crosses
2831
                 * the y axis
2832
                 */
2833
0
                tanalpha = tan(alpha);
2834
0
                intersecty = y1 - tanalpha * (r2 + (intersectx - x1));
2835
0
                intersectx = x1 - r2;
2836
2837
0
                if (endy > y1 - r2) {
2838
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2839
                     * An 'annulus' where the right edge is less than the normal extent and a
2840
                     * quad which is a rectangle with one corner chopped of at an angle.
2841
                     */
2842
0
                    p0.x = x1, p0.y = y1;
2843
0
                    p1.x = x1 - r2, p1.y = endy;
2844
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2845
0
                    p0.x = x1, p0.y = y1 - r1;
2846
0
                    p1.x = x1, p1.y = endy;
2847
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2848
0
                    p0.x = x1 - r2, p0.y = intersecty;
2849
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2850
0
                    p1.x = p0.x, p1.y = y1 - r1;
2851
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2852
0
                    p0.x = x1, p0.y = y1 - r1;
2853
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2854
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2855
0
                    if (code < 0)
2856
0
                        return code;
2857
0
                } else {
2858
                    /* Quadrant 1 is a normal quarter annulua */
2859
0
                    p0.x = x1, p0.y = y1;
2860
0
                    p1.x = x1 - r2, p1.y = y1 - r2;
2861
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2862
0
                }
2863
0
                break;
2864
0
        }
2865
0
    }
2866
0
    return 0;
2867
0
}
2868
2869
static int
2870
R_outer_circle(patch_fill_state_t *pfs, const gs_rect *rect,
2871
        double x0, double y0, double r0,
2872
        double x1, double y1, double r1,
2873
        double *x2, double *y2, double *r2)
2874
0
{
2875
0
    double dx = x1 - x0, dy = y1 - y0;
2876
0
    double sp, sq, s;
2877
2878
    /* Compute a cone circle, which contacts the rect externally. */
2879
    /* Don't bother with all 4 sides of the rect,
2880
       just do with the X or Y span only,
2881
       so it's not an exact contact, sorry. */
2882
0
    if (any_abs(dx) > any_abs(dy)) {
2883
        /* Solving :
2884
            x0 + (x1 - x0) * sq + r0 + (r1 - r0) * sq == bbox_px
2885
            (x1 - x0) * sp + (r1 - r0) * sp == bbox_px - x0 - r0
2886
            sp = (bbox_px - x0 - r0) / (x1 - x0 + r1 - r0)
2887
2888
            x0 + (x1 - x0) * sq - r0 - (r1 - r0) * sq == bbox_qx
2889
            (x1 - x0) * sq - (r1 - r0) * sq == bbox_x - x0 + r0
2890
            sq = (bbox_x - x0 + r0) / (x1 - x0 - r1 + r0)
2891
         */
2892
0
        if (x1 - x0 + r1 - r0 ==  0) /* We checked for obtuse cone. */
2893
0
            return_error(gs_error_unregistered); /* Must not happen. */
2894
0
        if (x1 - x0 - r1 + r0 ==  0) /* We checked for obtuse cone. */
2895
0
            return_error(gs_error_unregistered); /* Must not happen. */
2896
0
        sp = (rect->p.x - x0 - r0) / (x1 - x0 + r1 - r0);
2897
0
        sq = (rect->q.x - x0 + r0) / (x1 - x0 - r1 + r0);
2898
0
    } else {
2899
        /* Same by Y. */
2900
0
        if (y1 - y0 + r1 - r0 ==  0) /* We checked for obtuse cone. */
2901
0
            return_error(gs_error_unregistered); /* Must not happen. */
2902
0
        if (y1 - y0 - r1 + r0 ==  0) /* We checked for obtuse cone. */
2903
0
            return_error(gs_error_unregistered); /* Must not happen. */
2904
0
        sp = (rect->p.y - y0 - r0) / (y1 - y0 + r1 - r0);
2905
0
        sq = (rect->q.y - y0 + r0) / (y1 - y0 - r1 + r0);
2906
0
    }
2907
0
    if (sp >= 1 && sq >= 1)
2908
0
        s = max(sp, sq);
2909
0
    else if(sp >= 1)
2910
0
        s = sp;
2911
0
    else if (sq >= 1)
2912
0
        s = sq;
2913
0
    else {
2914
        /* The circle 1 is outside the rect, use it. */
2915
0
        s = 1;
2916
0
    }
2917
0
    if (r0 + (r1 - r0) * s < 0) {
2918
        /* Passed the cone apex, use the apex. */
2919
0
        s = r0 / (r0 - r1);
2920
0
        *r2 = 0;
2921
0
    } else
2922
0
        *r2 = r0 + (r1 - r0) * s;
2923
0
    *x2 = x0 + (x1 - x0) * s;
2924
0
    *y2 = y0 + (y1 - y0) * s;
2925
0
    return 0;
2926
0
}
2927
2928
static double
2929
R_rect_radius(const gs_rect *rect, double x0, double y0)
2930
132
{
2931
132
    double d, dd;
2932
2933
132
    dd = hypot(rect->p.x - x0, rect->p.y - y0);
2934
132
    d = hypot(rect->p.x - x0, rect->q.y - y0);
2935
132
    dd = max(dd, d);
2936
132
    d = hypot(rect->q.x - x0, rect->q.y - y0);
2937
132
    dd = max(dd, d);
2938
132
    d = hypot(rect->q.x - x0, rect->p.y - y0);
2939
132
    dd = max(dd, d);
2940
132
    return dd;
2941
132
}
2942
2943
static int
2944
R_fill_triangle_new(patch_fill_state_t *pfs, const gs_rect *rect,
2945
    double x0, double y0, double x1, double y1, double x2, double y2, double t)
2946
0
{
2947
0
    shading_vertex_t p0, p1, p2;
2948
0
    patch_color_t *c;
2949
0
    int code;
2950
0
    reserve_colors(pfs, &c, 1); /* Can't fail */
2951
2952
0
    p0.c = c;
2953
0
    p1.c = c;
2954
0
    p2.c = c;
2955
0
    code = gs_point_transform2fixed(&pfs->pgs->ctm, x0, y0, &p0.p);
2956
0
    if (code >= 0)
2957
0
        code = gs_point_transform2fixed(&pfs->pgs->ctm, x1, y1, &p1.p);
2958
0
    if (code >= 0)
2959
0
        code = gs_point_transform2fixed(&pfs->pgs->ctm, x2, y2, &p2.p);
2960
0
    if (code >= 0) {
2961
0
        c->t[0] = c->t[1] = t;
2962
0
        patch_resolve_color(c, pfs);
2963
0
        code = mesh_triangle(pfs, &p0, &p1, &p2);
2964
0
    }
2965
0
    release_colors(pfs, pfs->color_stack, 1);
2966
0
    return code;
2967
0
}
2968
2969
static int
2970
R_obtuse_cone(patch_fill_state_t *pfs, const gs_rect *rect,
2971
        double x0, double y0, double r0,
2972
        double x1, double y1, double r1, double t0, double r_rect)
2973
0
{
2974
0
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
2975
0
    double d = hypot(dx, dy);
2976
    /* Assuming dr > d / 3 && d > dr + 1e-7 * (d + dr), see the caller. */
2977
0
    double r = r_rect * 1.4143; /* A few bigger than sqrt(2). */
2978
0
    double ax, ay, as; /* Cone apex. */
2979
0
    double g0; /* The distance from apex to the tangent point of the 0th circle. */
2980
0
    int code;
2981
2982
0
    as = r0 / (r0 - r1);
2983
0
    ax = x0 + (x1 - x0) * as;
2984
0
    ay = y0 + (y1 - y0) * as;
2985
0
    g0 = sqrt(dx * dx + dy * dy - dr * dr) * as;
2986
0
    if (g0 < 1e-7 * r0) {
2987
        /* Nearly degenerate, replace with half-plane. */
2988
        /* Restrict the half plane with triangle, which covers the rect. */
2989
0
        gs_point p0, p1, p2; /* Right tangent limit, apex limit, left tangent linit,
2990
                                (right, left == when looking from the apex). */
2991
2992
0
        p0.x = ax - dy * r / d;
2993
0
        p0.y = ay + dx * r / d;
2994
0
        p1.x = ax - dx * r / d;
2995
0
        p1.y = ay - dy * r / d;
2996
0
        p2.x = ax + dy * r / d;
2997
0
        p2.y = ay - dx * r / d;
2998
        /* Split into 2 triangles at the apex,
2999
           so that the apex is preciselly covered.
3000
           Especially important when it is not exactly degenerate. */
3001
0
        code = R_fill_triangle_new(pfs, rect, ax, ay, p0.x, p0.y, p1.x, p1.y, t0);
3002
0
        if (code < 0)
3003
0
            return code;
3004
0
        return R_fill_triangle_new(pfs, rect, ax, ay, p1.x, p1.y, p2.x, p2.y, t0);
3005
0
    } else {
3006
        /* Compute the "limit" circle so that its
3007
           tangent points are outside the rect. */
3008
        /* Note: this branch is executed when the condition above is false :
3009
           g0 >= 1e-7 * r0 .
3010
           We believe that computing this branch with doubles
3011
           provides enough precision after converting coordinates into 'fixed',
3012
           and that the limit circle radius is not dramatically big.
3013
         */
3014
0
        double es, er; /* The limit circle parameter, radius. */
3015
0
        double ex, ey; /* The limit circle centrum. */
3016
3017
0
        es = as - as * r / g0; /* Always negative. */
3018
0
        er = r * r0 / g0 ;
3019
0
        ex = x0 + dx * es;
3020
0
        ey = y0 + dy * es;
3021
        /* Fill the annulus: */
3022
0
        code = R_tensor_annulus(pfs, x0, y0, r0, t0, ex, ey, er, t0);
3023
0
        if (code < 0)
3024
0
            return code;
3025
        /* Fill entire ending circle to ensure entire rect is covered. */
3026
0
        return R_tensor_annulus(pfs, ex, ey, er, t0, ex, ey, 0, t0);
3027
0
    }
3028
0
}
3029
3030
static int
3031
R_tensor_cone_apex(patch_fill_state_t *pfs, const gs_rect *rect,
3032
        double x0, double y0, double r0,
3033
        double x1, double y1, double r1, double t)
3034
0
{
3035
0
    double as = r0 / (r0 - r1);
3036
0
    double ax = x0 + (x1 - x0) * as;
3037
0
    double ay = y0 + (y1 - y0) * as;
3038
3039
0
    return R_tensor_annulus(pfs, x1, y1, r1, t, ax, ay, 0, t);
3040
0
}
3041
3042
/*
3043
 * A map of this code:
3044
 *
3045
 * R_extensions
3046
 * |-> (R_rect_radius)
3047
 * |-> (R_outer_circle)
3048
 * |-> R_obtuse_cone
3049
 * |   |-> R_fill_triangle_new
3050
 * |   |   '-> mesh_triangle
3051
 * |   |       '-> mesh_triangle_rec <--.
3052
 * |   |           |--------------------'
3053
 * |   |           |-> small_mesh_triangle
3054
 * |   |           |   '-> fill_triangle
3055
 * |   |           |       '-> triangle_by_4 <--.
3056
 * |   |           |           |----------------'
3057
 * |   |           |           |-> constant_color_triangle
3058
 * |   |           |           |-> make_wedge_median (etc)
3059
 * |   |           '-----------+--------------------.
3060
 * |   '-------------------.                        |
3061
 * |-> R_tensor_cone_apex  |                        |
3062
 * |   '-------------------+                        |
3063
 * '-> R_tensor_annulus <--'                       \|/
3064
 *     |-> (make_quadrant_arc)                      |
3065
 *     '-> patch_fill                               |
3066
 *         |-> fill_patch <--.                      |
3067
 *         |   |-------------'                      |
3068
 *         |   |------------------------------------+
3069
 *         |   '-> fill_stripe                      |
3070
 *         |       |-----------------------.        |
3071
 *         |      \|/                      |        |
3072
 *         |-> fill_wedges                 |        |
3073
 *             '-> fill_wedges_aux <--.    |        |
3074
 *                 |------------------'   \|/       |
3075
 *                 |----------------> mesh_padding  '
3076
 *                 |                  '----------------------------------.
3077
 *                 '-> wedge_by_triangles <--.      .                    |
3078
 *                     |---------------------'      |                    |
3079
 *                     '-> fill_triangle_wedge <----'                    |
3080
 *                         '-> fill_triangle_wedge_aux                   |
3081
 *                             '-> fill_wedge_trap                       |
3082
 *                                 '-> wedge_trap_decompose              |
3083
 *                                     '-> linear_color_trapezoid        |
3084
 *                                         '-> decompose_linear_color <--|
3085
 *                                             |-------------------------'
3086
 *                                             '-> constant_color_trapezoid
3087
 */
3088
static int
3089
R_extensions(patch_fill_state_t *pfs, const gs_shading_R_t *psh, const gs_rect *rect,
3090
        double t0, double t1, bool Extend0, bool Extend1)
3091
266
{
3092
266
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3093
266
    double r0 = psh->params.Coords[2];
3094
266
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3095
266
    double r1 = psh->params.Coords[5];
3096
266
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
3097
266
    double d = hypot(dx, dy), r;
3098
266
    int code;
3099
3100
    /* In order for the circles to be nested, one end circle
3101
     * needs to be sufficiently large to cover the entirety
3102
     * of the other end circle. i.e.
3103
     *
3104
     *     max(r0,r1) >= d + min(r0,r1)
3105
     * === min(r0,r1) + dr >= d + min(r0,r1)
3106
     * === dr >= d
3107
     *
3108
     * This, plus a fudge factor for FP operation is what we use below.
3109
     *
3110
     * An "Obtuse Cone" is defined to be one for which the "opening
3111
     * angle" is obtuse.
3112
     *
3113
     * Consider two circles; one at (r0,r0) of radius r0, and one at
3114
     * (r1,r1) of radius r1. These clearly lie on the acute/obtuse
3115
     * boundary. The distance between the centres of these two circles
3116
     * is d = sqr(2.(r0-r1)^2) by pythagoras. Thus d = sqr(2).dr.
3117
     * By observation if d gets longer, we become acute, shorter, obtuse.
3118
     * i.e. if sqr(2).dr > d we are obtuse, if d > sqr(2).dr we are acute.
3119
     * (Thanks to Paul Gardiner for this reasoning).
3120
     *
3121
     * The code below tests (dr > d/3) (i.e. 3.dr > d). This
3122
     * appears to be a factor of 2 and a bit out, so I am confused
3123
     * by it.
3124
     *
3125
     * Either Igor meant something different to the standard meaning
3126
     * of "Obtuse Cone", or he got his maths wrong. Or he was more
3127
     * cunning than I can understand. Leave it as it until we find
3128
     * an actual example that goes wrong.
3129
     */
3130
3131
    /* Tests with Acrobat seem to indicate that it uses a fudge factor
3132
     * of around .0001. (i.e. [1.0001 0 0 0 0 1] is accepted as a
3133
     * non nested circle, but [1.00009 0 0 0 0 1] is a nested one.
3134
     * Approximate the same sort of value here to appease bug 690831.
3135
     */
3136
266
    if (any_abs (dr - d) < 0.001) {
3137
0
        if ((r0 > r1 && Extend0) || (r1 > r0 && Extend1)) {
3138
0
            r = R_rect_radius(rect, x0, y0);
3139
0
            if (r0 < r1)
3140
0
                code = R_tensor_annulus_extend_tangent(pfs, x0, y0, r0, t1, x1, y1, r1, t1, r);
3141
0
            else
3142
0
                code = R_tensor_annulus_extend_tangent(pfs, x1, y1, r1, t0, x0, y0, r0, t0, r);
3143
0
            if (code < 0)
3144
0
                return code;
3145
0
        } else {
3146
0
            if (r0 > r1) {
3147
0
                if (Extend1 && r1 > 0)
3148
0
                    return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3149
0
            }
3150
0
            else {
3151
0
                if (Extend0 && r0 > 0)
3152
0
                    return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3153
0
            }
3154
0
        }
3155
0
    } else
3156
266
    if (dr > d - 1e-4 * (d + dr)) {
3157
        /* Nested circles, or degenerate. */
3158
266
        if (r0 > r1) {
3159
4
            if (Extend0) {
3160
0
                r = R_rect_radius(rect, x0, y0);
3161
0
                if (r > r0) {
3162
0
                    code = R_tensor_annulus(pfs, x0, y0, r, t0, x0, y0, r0, t0);
3163
0
                    if (code < 0)
3164
0
                        return code;
3165
0
                }
3166
0
            }
3167
4
            if (Extend1 && r1 > 0)
3168
4
                return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3169
262
        } else {
3170
262
            if (Extend1) {
3171
132
                r = R_rect_radius(rect, x1, y1);
3172
132
                if (r > r1) {
3173
132
                    code = R_tensor_annulus(pfs, x1, y1, r, t1, x1, y1, r1, t1);
3174
132
                    if (code < 0)
3175
0
                        return code;
3176
132
                }
3177
132
            }
3178
262
            if (Extend0 && r0 > 0)
3179
0
                return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3180
262
        }
3181
266
    } else if (dr > d / 3) {
3182
        /* Obtuse cone. */
3183
0
        if (r0 > r1) {
3184
0
            if (Extend0) {
3185
0
                r = R_rect_radius(rect, x0, y0);
3186
0
                code = R_obtuse_cone(pfs, rect, x0, y0, r0, x1, y1, r1, t0, r);
3187
0
                if (code < 0)
3188
0
                    return code;
3189
0
            }
3190
0
            if (Extend1 && r1 != 0)
3191
0
                return R_tensor_cone_apex(pfs, rect, x0, y0, r0, x1, y1, r1, t1);
3192
0
            return 0;
3193
0
        } else {
3194
0
            if (Extend1) {
3195
0
                r = R_rect_radius(rect, x1, y1);
3196
0
                code = R_obtuse_cone(pfs, rect, x1, y1, r1, x0, y0, r0, t1, r);
3197
0
                if (code < 0)
3198
0
                    return code;
3199
0
            }
3200
0
            if (Extend0 && r0 != 0)
3201
0
                return R_tensor_cone_apex(pfs, rect, x1, y1, r1, x0, y0, r0, t0);
3202
0
        }
3203
0
    } else {
3204
        /* Acute cone or cylinder. */
3205
0
        double x2, y2, r2, x3, y3, r3;
3206
3207
0
        if (Extend0) {
3208
0
            code = R_outer_circle(pfs, rect, x1, y1, r1, x0, y0, r0, &x3, &y3, &r3);
3209
0
            if (code < 0)
3210
0
                return code;
3211
0
            if (x3 != x1 || y3 != y1) {
3212
0
                code = R_tensor_annulus(pfs, x0, y0, r0, t0, x3, y3, r3, t0);
3213
0
                if (code < 0)
3214
0
                    return code;
3215
0
            }
3216
0
        }
3217
0
        if (Extend1) {
3218
0
            code = R_outer_circle(pfs, rect, x0, y0, r0, x1, y1, r1, &x2, &y2, &r2);
3219
0
            if (code < 0)
3220
0
                return code;
3221
0
            if (x2 != x0 || y2 != y0) {
3222
0
                code = R_tensor_annulus(pfs, x1, y1, r1, t1, x2, y2, r2, t1);
3223
0
                if (code < 0)
3224
0
                    return code;
3225
0
            }
3226
0
        }
3227
0
    }
3228
262
    return 0;
3229
266
}
3230
3231
static int
3232
R_fill_rect_with_const_color(patch_fill_state_t *pfs, const gs_fixed_rect *clip_rect, float t)
3233
1
{
3234
#if 0 /* Disabled because the clist writer device doesn't pass
3235
         the clipping path with fill_recatangle. */
3236
    patch_color_t pc;
3237
    const gs_color_space *pcs = pfs->direct_space;
3238
    gx_device_color dc;
3239
    int code;
3240
3241
    code = gs_function_evaluate(pfs->Function, &t, pc.cc.paint.values);
3242
    if (code < 0)
3243
        return code;
3244
    pcs->type->restrict_color(&pc.cc, pcs);
3245
    code = patch_color_to_device_color(pfs, &pc, &dc);
3246
    if (code < 0)
3247
        return code;
3248
    return gx_fill_rectangle_device_rop(fixed2int_pixround(clip_rect->p.x), fixed2int_pixround(clip_rect->p.y),
3249
                                        fixed2int_pixround(clip_rect->q.x) - fixed2int_pixround(clip_rect->p.x),
3250
                                        fixed2int_pixround(clip_rect->q.y) - fixed2int_pixround(clip_rect->p.y),
3251
                                        &dc, pfs->dev, pfs->pgs->log_op);
3252
#else
3253
    /* Can't apply fill_rectangle, because the clist writer device doesn't pass
3254
       the clipping path with fill_recatangle. Convert into trapezoids instead.
3255
    */
3256
1
    quadrangle_patch p;
3257
1
    shading_vertex_t pp[2][2];
3258
1
    const gs_color_space *pcs = pfs->direct_space;
3259
1
    patch_color_t pc;
3260
1
    int code;
3261
3262
1
    code = gs_function_evaluate(pfs->Function, &t, pc.cc.paint.values);
3263
1
    if (code < 0)
3264
0
        return code;
3265
1
    pcs->type->restrict_color(&pc.cc, pcs);
3266
1
    pc.t[0] = pc.t[1] = t;
3267
1
    pp[0][0].p = clip_rect->p;
3268
1
    pp[0][1].p.x = clip_rect->q.x;
3269
1
    pp[0][1].p.y = clip_rect->p.y;
3270
1
    pp[1][0].p.x = clip_rect->p.x;
3271
1
    pp[1][0].p.y = clip_rect->q.y;
3272
1
    pp[1][1].p = clip_rect->q;
3273
1
    pp[0][0].c = pp[0][1].c = pp[1][0].c = pp[1][1].c = &pc;
3274
1
    p.p[0][0] = &pp[0][0];
3275
1
    p.p[0][1] = &pp[0][1];
3276
1
    p.p[1][0] = &pp[1][0];
3277
1
    p.p[1][1] = &pp[1][1];
3278
1
    return constant_color_quadrangle(pfs, &p, false);
3279
1
#endif
3280
1
}
3281
3282
typedef struct radial_shading_attrs_s {
3283
    double x0, y0;
3284
    double x1, y1;
3285
    double span[2][2];
3286
    double apex;
3287
    bool have_apex;
3288
    bool have_root[2]; /* ongoing contact, outgoing contact. */
3289
    bool outer_contact[2];
3290
    gs_point p[6]; /* 4 corners of the rectangle, p[4] = p[0], p[5] = p[1] */
3291
} radial_shading_attrs_t;
3292
3293
4.14k
#define Pw2(a) ((a)*(a))
3294
3295
static void
3296
radial_shading_external_contact(radial_shading_attrs_t *rsa, int point_index, double t, double r0, double r1, bool at_corner, int root_index)
3297
604
{
3298
604
    double cx = rsa->x0 + (rsa->x1 - rsa->x0) * t;
3299
604
    double cy = rsa->y0 + (rsa->y1 - rsa->y0) * t;
3300
604
    double rx = rsa->p[point_index].x - cx;
3301
604
    double ry = rsa->p[point_index].y - cy;
3302
604
    double dx = rsa->p[point_index - 1].x - rsa->p[point_index].x;
3303
604
    double dy = rsa->p[point_index - 1].y - rsa->p[point_index].y;
3304
3305
604
    if (at_corner) {
3306
592
        double Dx = rsa->p[point_index + 1].x - rsa->p[point_index].x;
3307
592
        double Dy = rsa->p[point_index + 1].y - rsa->p[point_index].y;
3308
592
        bool b1 = (dx * rx + dy * ry >= 0);
3309
592
        bool b2 = (Dx * rx + Dy * ry >= 0);
3310
3311
592
        if (b1 & b2)
3312
124
            rsa->outer_contact[root_index] = true;
3313
592
    } else {
3314
12
        if (rx * dy - ry * dx < 0)
3315
4
            rsa->outer_contact[root_index] = true;
3316
12
    }
3317
604
}
3318
3319
static void
3320
store_roots(radial_shading_attrs_t *rsa, const bool have_root[2], const double t[2], double r0, double r1, int point_index, bool at_corner)
3321
1.18k
{
3322
1.18k
    int i;
3323
3324
3.55k
    for (i = 0; i < 2; i++) {
3325
2.36k
        bool good_root;
3326
3327
2.36k
        if (!have_root[i])
3328
1.17k
            continue;
3329
1.19k
        good_root = (!rsa->have_apex || (rsa->apex <= 0 || r0 == 0 ? t[i] >= rsa->apex : t[i] <= rsa->apex));
3330
1.19k
        if (good_root) {
3331
604
            radial_shading_external_contact(rsa, point_index, t[i], r0, r1, at_corner, i);
3332
604
            if (!rsa->have_root[i]) {
3333
37
                rsa->span[i][0] = rsa->span[i][1] = t[i];
3334
37
                rsa->have_root[i] = true;
3335
567
            } else {
3336
567
                if (rsa->span[i][0] > t[i])
3337
37
                    rsa->span[i][0] = t[i];
3338
567
                if (rsa->span[i][1] < t[i])
3339
85
                    rsa->span[i][1] = t[i];
3340
567
            }
3341
604
        }
3342
1.19k
    }
3343
1.18k
}
3344
3345
static void
3346
compute_radial_shading_span_extended_side(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3347
592
{
3348
592
    double cc, c;
3349
592
    bool have_root[2] = {false, false};
3350
592
    double t[2];
3351
592
    bool by_x = (rsa->p[point_index].x != rsa->p[point_index + 1].x);
3352
592
    int i;
3353
3354
    /* As t moves from 0 to 1, the circles move from r0 to r1, and from
3355
     * from position p0 to py. For simplicity, adjust so that p0 is at
3356
     * the origin. Consider the projection of the circle drawn at any given
3357
     * time onto the x axis. The range of points would be:
3358
     * p1x*t +/- (r0+(r1-r0)*t). We are interested in the first (and last)
3359
     * moments when the range includes a point c on the x axis. So solve for:
3360
     * p1x*t +/- (r0+(r1-r0)*t) = c. Let cc = p1x.
3361
     * So p1x*t0 + (r1-r0)*t0 = c - r0 => t0 = (c - r0)/(p1x + r1 - r0)
3362
     *    p1x*t1 - (r1-r0)*t1 = c + r0 => t1 = (c + r0)/(p1x - r1 + r0)
3363
     */
3364
592
    if (by_x) {
3365
288
        c = rsa->p[point_index].x - rsa->x0;
3366
288
        cc = rsa->x1 - rsa->x0;
3367
304
    } else {
3368
304
        c = rsa->p[point_index].y - rsa->y0;
3369
304
        cc = rsa->y1 - rsa->y0;
3370
304
    }
3371
592
    t[0] = (c - r0) / (cc + r1 - r0);
3372
592
    t[1] = (c + r0) / (cc - r1 + r0);
3373
592
    if (t[0] > t[1]) {
3374
420
        c    = t[0];
3375
420
        t[0] = t[1];
3376
420
        t[1] = c;
3377
420
    }
3378
1.77k
    for (i = 0; i < 2; i++) {
3379
1.18k
        double d, d0, d1;
3380
3381
1.18k
        if (by_x) {
3382
576
            d = rsa->y1 - rsa->y0 + r0 + (r1 - r0) * t[i];
3383
576
            d0 = rsa->p[point_index].y;
3384
576
            d1 = rsa->p[point_index + 1].y;
3385
608
        } else {
3386
608
            d = rsa->x1 - rsa->x0 + r0 + (r1 - r0) * t[i];
3387
608
            d0 = rsa->p[point_index].x;
3388
608
            d1 = rsa->p[point_index + 1].x;
3389
608
        }
3390
1.18k
        if (d1 > d0 ? d0 <= d && d <= d1 : d1 <= d && d <= d0)
3391
12
            have_root[i] = true;
3392
1.18k
    }
3393
592
    store_roots(rsa, have_root, t, r0, r1, point_index, false);
3394
592
}
3395
3396
static int
3397
compute_radial_shading_span_extended_point(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3398
592
{
3399
    /* As t moves from 0 to 1, the circles move from r0 to r1, and from
3400
     * from position p0 to py. At any given time t, therefore, we
3401
     * paint the points that are distance r0+(r1-r0)*t from point
3402
     * (p0x+(p1x-p0x)*t,p0y+(p1y-p0y)*t) = P(t).
3403
     *
3404
     * To simplify our algebra, adjust so that (p0x, p0y) is at the origin.
3405
     * To find the time(s) t at which the a point q is painted, we therefore
3406
     * solve for t in:
3407
     *
3408
     * |q-P(t)| = r0+(r1-r0)*t
3409
     *
3410
     *   (qx-p1x*t)^2 + (qy-p1y*t)^2 - (r0+(r1-r0)*t)^2 = 0
3411
     * = qx^2 - 2qx.p1x.t + p1x^2.t^2 + qy^2 - 2qy.p1y.t + p1y^2.t^2 -
3412
     *                                   (r0^2 + 2r0(r1-r0)t + (r1-r0)^2.t^2)
3413
     * =   qx^2 + qy^2 - r0^2
3414
     *   + -2(qx.p1x + qy.p1y + r0(r1-r0)).t
3415
     *   + (p1x^2 + p1y^2 - (r1-r0)^2).t^2
3416
     *
3417
     * So solve using the usual t = (-b +/- SQRT(b^2 - 4ac)) where
3418
     *   a = p1x^2 + p1y^2 - (r1-r0)^2
3419
     *   b = -2(qx.p1x + qy.p1y + r0(r1-r0))
3420
     *   c = qx^2 + qy^2 - r0^2
3421
     */
3422
592
    double p1x = rsa->x1 - rsa->x0;
3423
592
    double p1y = rsa->y1 - rsa->y0;
3424
592
    double qx  = rsa->p[point_index].x - rsa->x0;
3425
592
    double qy  = rsa->p[point_index].y - rsa->y0;
3426
592
    double a   = (Pw2(p1x) + Pw2(p1y) - Pw2(r0 - r1));
3427
592
    bool have_root[2] = {false, false};
3428
592
    double t[2];
3429
3430
592
    if (fabs(a) < 1e-8) {
3431
        /* Linear equation. */
3432
        /* This case is always the ongoing ellipse contact. */
3433
0
        double cx = rsa->x0 - (rsa->x1 - rsa->x0) * r0 / (r1 - r0);
3434
0
        double cy = rsa->y0 - (rsa->y1 - rsa->y0) * r0 / (r1 - r0);
3435
3436
0
        t[0] = (Pw2(qx) + Pw2(qy))/(cx*qx + cy*qy) / 2;
3437
0
        have_root[0] = true;
3438
592
    } else {
3439
        /* Square equation.  No solution if b^2 - 4ac = 0. Equivalently if
3440
         * (b^2)/4 -a.c = 0 === (b/2)^2 - a.c = 0 ===  (-b/2)^2 - a.c = 0 */
3441
592
        double minushalfb = r0*(r1-r0) + p1x*qx + p1y*qy;
3442
592
        double c          = Pw2(qx) + Pw2(qy) - Pw2(r0);
3443
592
        double desc2      = Pw2(minushalfb) - a*c; /* desc2 = 1/4 (b^2-4ac) */
3444
3445
592
        if (desc2 < 0) {
3446
0
            return -1; /* The point is outside the shading coverage.
3447
                          Do not shorten, because we didn't observe it in practice. */
3448
592
        } else {
3449
592
            double desc1 = sqrt(desc2); /* desc1 = 1/2 SQRT(b^2-4ac) */
3450
3451
592
            if (a > 0) {
3452
0
                t[0] = (minushalfb - desc1) / a;
3453
0
                t[1] = (minushalfb + desc1) / a;
3454
592
            } else {
3455
592
                t[0] = (minushalfb + desc1) / a;
3456
592
                t[1] = (minushalfb - desc1) / a;
3457
592
            }
3458
592
            have_root[0] = have_root[1] = true;
3459
592
        }
3460
592
    }
3461
592
    store_roots(rsa, have_root, t, r0, r1, point_index, true);
3462
592
    if (have_root[0] && have_root[1])
3463
592
        return 15;
3464
0
    if (have_root[0])
3465
0
        return 15 - 4;
3466
0
    if (have_root[1])
3467
0
        return 15 - 2;
3468
0
    return -1;
3469
0
}
3470
3471
#undef Pw2
3472
3473
static int
3474
compute_radial_shading_span_extended(radial_shading_attrs_t *rsa, double r0, double r1)
3475
148
{
3476
148
    int span_type0, span_type1;
3477
3478
148
    span_type0 = compute_radial_shading_span_extended_point(rsa, r0, r1, 1);
3479
148
    if (span_type0 == -1)
3480
0
        return -1;
3481
148
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 2);
3482
148
    if (span_type0 != span_type1)
3483
0
        return -1;
3484
148
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 3);
3485
148
    if (span_type0 != span_type1)
3486
0
        return -1;
3487
148
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 4);
3488
148
    if (span_type0 != span_type1)
3489
0
        return -1;
3490
148
    compute_radial_shading_span_extended_side(rsa, r0, r1, 1);
3491
148
    compute_radial_shading_span_extended_side(rsa, r0, r1, 2);
3492
148
    compute_radial_shading_span_extended_side(rsa, r0, r1, 3);
3493
148
    compute_radial_shading_span_extended_side(rsa, r0, r1, 4);
3494
148
    return span_type0;
3495
148
}
3496
3497
static int
3498
compute_radial_shading_span(radial_shading_attrs_t *rsa, float x0, float y0, double r0, float x1, float y1, double r1, const gs_rect * rect)
3499
37
{
3500
    /* If the shading area is much larger than the path bbox,
3501
       we want to shorten the shading for a faster rendering.
3502
       If any point of the path bbox falls outside the shading area,
3503
       our math is not applicable, and we render entire shading.
3504
       If the path bbox is inside the shading area,
3505
       we compute 1 or 2 'spans' - the shading parameter intervals,
3506
       which covers the bbox. For doing that we need to resolve
3507
       a square eqation by the shading parameter
3508
       for each corner of the bounding box,
3509
       and for each side of the shading bbox.
3510
       Note the equation to be solved in the user space.
3511
       Since each equation gives 2 roots (because the points are
3512
       strongly inside the shading area), we will get 2 parameter intervals -
3513
       the 'lower' one corresponds to the first (ongoing) contact of
3514
       the running circle, and the second one corresponds to the last (outgoing) contact
3515
       (like in a sun eclipse; well our sun is rectangular).
3516
3517
       Here are few exceptions.
3518
3519
       First, the equation degenerates when the distance sqrt((x1-x0)^2 + (y1-y0)^2)
3520
       appears equal to r0-r1. In this case the base circles do contact,
3521
       and the running circle does contact at the same point.
3522
       The equation degenerates to a linear one.
3523
       Since we don't want float precision noize to affect the result,
3524
       we compute this condition in 'fixed' coordinates.
3525
3526
       Second, Postscript approximates any circle with 3d order beziers.
3527
       This approximation may give a 2% error.
3528
       Therefore using the precise roots may cause a dropout.
3529
       To prevetn them, we slightly modify the base radii.
3530
       However the sign of modification smartly depends
3531
       on the relative sizes of the base circles,
3532
       and on the contact number. Currently we don't want to
3533
       define and debug the smart optimal logic for that,
3534
       so we simply try all 4 variants for each source equation,
3535
       and use the union of intervals.
3536
3537
       Third, we could compute which quarter of the circle
3538
       really covers the path bbox. Using it we could skip
3539
       rendering of uncovering quarters. Currently we do not
3540
       implement this optimization. The general tensor patch algorithm
3541
       will skip uncovering parts.
3542
3543
       Fourth, when one base circle is (almost) inside the other,
3544
       the parameter interval must include the shading apex.
3545
       To know that, we determine whether the contacting circle
3546
       is outside the rectangle (the "outer" contact),
3547
       or it is (partially) inside the rectangle.
3548
3549
       At last, a small shortening of a shading won't give a
3550
       sensible speedup, but it may replace a symmetric function domain
3551
       with an assymmetric one, so that the rendering
3552
       would be asymmetyric for a symmetric shading.
3553
       Therefore we do not perform a small sortening.
3554
       Instead we shorten only if the shading span
3555
       is much smaller that the shading domain.
3556
     */
3557
37
    const double extent = 1.02;
3558
37
    int span_type0, span_type1, span_type;
3559
3560
37
    memset(rsa, 0, sizeof(*rsa));
3561
37
    rsa->x0 = x0;
3562
37
    rsa->y0 = y0;
3563
37
    rsa->x1 = x1;
3564
37
    rsa->y1 = y1;
3565
37
    rsa->p[0] = rsa->p[4] = rect->p;
3566
37
    rsa->p[1].x = rsa->p[5].x = rect->p.x;
3567
37
    rsa->p[1].y = rsa->p[5].y = rect->q.y;
3568
37
    rsa->p[2] = rect->q;
3569
37
    rsa->p[3].x = rect->q.x;
3570
37
    rsa->p[3].y = rect->p.y;
3571
37
    rsa->have_apex = any_abs(r1 - r0) > 1e-7 * any_abs(r1 + r0);
3572
37
    rsa->apex = (rsa->have_apex ? -r0 / (r1 - r0) : 0);
3573
37
    span_type0 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 * extent);
3574
37
    if (span_type0 == -1)
3575
0
        return -1;
3576
37
    span_type1 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 / extent);
3577
37
    if (span_type0 != span_type1)
3578
0
        return -1;
3579
37
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 * extent);
3580
37
    if (span_type0 != span_type1)
3581
0
        return -1;
3582
37
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 / extent);
3583
37
    if (span_type1 == -1)
3584
0
        return -1;
3585
37
    if (r0 < r1) {
3586
33
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3587
0
            rsa->span[0][0] = rsa->apex; /* Likely never happens. Remove ? */
3588
33
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3589
9
            rsa->span[1][0] = rsa->apex;
3590
33
    } else if (r0 > r1) {
3591
4
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3592
0
            rsa->span[0][1] = rsa->apex;
3593
4
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3594
0
            rsa->span[1][1] = rsa->apex; /* Likely never happens. Remove ? */
3595
4
    }
3596
37
    span_type = 0;
3597
37
    if (rsa->have_root[0] && rsa->span[0][0] < 0)
3598
0
        span_type |= 1;
3599
37
    if (rsa->have_root[1] && rsa->span[1][0] < 0)
3600
0
        span_type |= 1;
3601
37
    if (rsa->have_root[0] && rsa->span[0][1] > 0 && rsa->span[0][0] < 1)
3602
4
        span_type |= 2;
3603
37
    if (rsa->have_root[1] && rsa->span[1][1] > 0 && rsa->span[1][0] < 1)
3604
32
        span_type |= 4;
3605
37
    if (rsa->have_root[0] && rsa->span[0][1] > 1)
3606
4
        span_type |= 8;
3607
37
    if (rsa->have_root[1] && rsa->span[1][1] > 1)
3608
7
        span_type |= 8;
3609
37
    return span_type;
3610
37
}
3611
3612
static bool
3613
shorten_radial_shading(float *x0, float *y0, double *r0, float *d0, float *x1, float *y1, double *r1, float *d1, double span_[2])
3614
36
{
3615
36
    double s0 = span_[0], s1 = span_[1], w;
3616
3617
36
    if (s0 < 0)
3618
0
        s0 = 0;
3619
36
    if (s1 < 0)
3620
0
        s1 = 0;
3621
36
    if (s0 > 1)
3622
0
        s0 = 1;
3623
36
    if (s1 > 1)
3624
10
        s1 = 1;
3625
36
    w = s1 - s0;
3626
36
    if (w == 0)
3627
0
        return false; /* Don't pass a degenerate shading. */
3628
36
    if (w > 0.3)
3629
28
        return false; /* The span is big, don't shorten it. */
3630
8
    { /* Do shorten. */
3631
8
        double R0 = *r0, X0 = *x0, Y0 = *y0, D0 = *d0;
3632
8
        double R1 = *r1, X1 = *x1, Y1 = *y1, D1 = *d1;
3633
3634
8
        *r0 = R0 + (R1 - R0) * s0;
3635
8
        *x0 = X0 + (X1 - X0) * s0;
3636
8
        *y0 = Y0 + (Y1 - Y0) * s0;
3637
8
        *d0 = D0 + (D1 - D0) * s0;
3638
8
        *r1 = R0 + (R1 - R0) * s1;
3639
8
        *x1 = X0 + (X1 - X0) * s1;
3640
8
        *y1 = Y0 + (Y1 - Y0) * s1;
3641
8
        *d1 = D0 + (D1 - D0) * s1;
3642
8
    }
3643
8
    return true;
3644
36
}
3645
3646
static bool inline
3647
is_radial_shading_large(double x0, double y0, double r0, double x1, double y1, double r1, const gs_rect * rect)
3648
165
{
3649
165
    const double d = hypot(x1 - x0, y1 - y0);
3650
165
    const double area0 = M_PI * r0 * r0 / 2;
3651
165
    const double area1 = M_PI * r1 * r1 / 2;
3652
165
    const double area2 = (r0 + r1) / 2 * d;
3653
165
    const double arbitrary = 8;
3654
165
    double areaX, areaY;
3655
3656
    /* The shading area is not equal to area0 + area1 + area2
3657
       when one circle is (almost) inside the other.
3658
       We believe that the 'arbitrary' coefficient recovers that
3659
       when it is set greater than 2. */
3660
    /* If one dimension is large enough, the shading parameter span is wide. */
3661
165
    areaX = (rect->q.x - rect->p.x) * (rect->q.x - rect->p.x);
3662
165
    if (areaX * arbitrary < area0 + area1 + area2)
3663
28
        return true;
3664
137
    areaY = (rect->q.y - rect->p.y) * (rect->q.y - rect->p.y);
3665
137
    if (areaY * arbitrary < area0 + area1 + area2)
3666
9
        return true;
3667
128
    return false;
3668
137
}
3669
3670
static int
3671
gs_shading_R_fill_rectangle_aux(const gs_shading_t * psh0, const gs_rect * rect,
3672
                            const gs_fixed_rect *clip_rect,
3673
                            gx_device * dev, gs_gstate * pgs)
3674
168
{
3675
168
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
3676
168
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
3677
168
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3678
168
    double r0 = psh->params.Coords[2];
3679
168
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3680
168
    double r1 = psh->params.Coords[5];
3681
168
    radial_shading_attrs_t rsa;
3682
168
    int span_type; /* <0 - don't shorten, 1 - extent0, 2 - first contact, 4 - last contact, 8 - extent1. */
3683
168
    int code;
3684
168
    patch_fill_state_t pfs1;
3685
3686
168
    if (r0 == 0 && r1 == 0)
3687
3
        return 0; /* PLRM requires to paint nothing. */
3688
165
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
3689
165
    if (code < 0)
3690
0
        return code;
3691
165
    pfs1.Function = psh->params.Function;
3692
165
    code = init_patch_fill_state(&pfs1);
3693
165
    if (code < 0) {
3694
0
        if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3695
0
        return code;
3696
0
    }
3697
165
    pfs1.function_arg_shift = 0;
3698
165
    pfs1.rect = *clip_rect;
3699
165
    pfs1.maybe_self_intersecting = false;
3700
165
    if (is_radial_shading_large(x0, y0, r0, x1, y1, r1, rect))
3701
37
        span_type = compute_radial_shading_span(&rsa, x0, y0, r0, x1, y1, r1, rect);
3702
128
    else
3703
128
        span_type = -1;
3704
165
    if (span_type < 0) {
3705
128
        code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3706
128
        if (code >= 0)
3707
128
            code = R_tensor_annulus(&pfs1, x0, y0, r0, d0, x1, y1, r1, d1);
3708
128
        if (code >= 0)
3709
128
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3710
128
    } else if (span_type == 1) {
3711
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d0);
3712
37
    } else if (span_type == 8) {
3713
1
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d1);
3714
36
    } else {
3715
36
        bool second_interval = true;
3716
3717
36
        code = 0;
3718
36
        if (span_type & 1)
3719
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3720
36
        if ((code >= 0) && (span_type & 2)) {
3721
4
            float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3722
4
            double R0 = r0, R1 = r1;
3723
3724
4
            if ((span_type & 4) && rsa.span[0][1] >= rsa.span[1][0]) {
3725
0
                double united[2];
3726
3727
0
                united[0] = rsa.span[0][0];
3728
0
                united[1] = rsa.span[1][1];
3729
0
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, united);
3730
0
                second_interval = false;
3731
4
            } else {
3732
4
                second_interval = shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[0]);
3733
4
            }
3734
4
            code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3735
4
        }
3736
36
        if (code >= 0 && second_interval) {
3737
36
            if (span_type & 4) {
3738
32
                float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3739
32
                double R0 = r0, R1 = r1;
3740
3741
32
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[1]);
3742
32
                code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3743
32
            }
3744
36
        }
3745
36
        if (code >= 0 && (span_type & 8))
3746
10
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3747
36
    }
3748
165
    if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3749
165
    if (term_patch_fill_state(&pfs1))
3750
0
        return_error(gs_error_unregistered); /* Must not happen. */
3751
165
    return code;
3752
165
}
3753
3754
int
3755
gs_shading_R_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
3756
                            const gs_fixed_rect * rect_clip,
3757
                            gx_device * dev, gs_gstate * pgs)
3758
168
{
3759
168
    return gs_shading_R_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
3760
168
}