Coverage Report

Created: 2025-06-24 07:01

/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
358k
{
56
358k
    int i, j;
57
58
1.79M
    for (i = 0; i < 4; i++) {
59
1.43M
        j = (i + 1) % 4;
60
1.43M
        curve[i].control[0].x = (curve[i].vertex.p.x * 2 + curve[j].vertex.p.x) / 3;
61
1.43M
        curve[i].control[0].y = (curve[i].vertex.p.y * 2 + curve[j].vertex.p.y) / 3;
62
1.43M
        curve[i].control[1].x = (curve[i].vertex.p.x + curve[j].vertex.p.x * 2) / 3;
63
1.43M
        curve[i].control[1].y = (curve[i].vertex.p.y + curve[j].vertex.p.y * 2) / 3;
64
1.43M
        curve[i].straight = true;
65
1.43M
    }
66
358k
}
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.1M
{
236
80.1M
    fixed m0, m1;
237
80.1M
    int v0, v1;
238
80.1M
    int changed;
239
240
80.1M
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
241
392
        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.1M
    do {
266
80.1M
        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.1M
        if ((c[0].vertex.p.x <= pfs->rect.p.x &&
272
80.1M
             c[1].vertex.p.x <= pfs->rect.p.x &&
273
80.1M
             c[2].vertex.p.x <= pfs->rect.p.x &&
274
80.1M
             c[3].vertex.p.x <= pfs->rect.p.x) ||
275
80.1M
            (c[0].vertex.p.x >= pfs->rect.q.x &&
276
80.1M
             c[1].vertex.p.x >= pfs->rect.q.x &&
277
80.1M
             c[2].vertex.p.x >= pfs->rect.q.x &&
278
80.1M
             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.5M
        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.8M
        else if (c[1].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
292
33
        {
293
            /* Check 1+2 off left. */
294
33
            v0 = 1;
295
33
            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
2
                    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
2
                        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.5M
        if (c[0].vertex.p.x > pfs->rect.q.x && c[3].vertex.p.x > pfs->rect.q.x)
355
58
        {
356
            /* Check 0+3 off right. */
357
58
            v0 = 0;
358
58
            v1 = 3;
359
58
            goto check_right;
360
58
        }
361
73.5M
        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
122
                    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
122
                        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.5M
        if (c[0].vertex.p.x < pfs->rect.p.x && c[1].vertex.p.x < pfs->rect.p.x)
425
18.5k
        {
426
            /* Check 0+1 off left. */
427
18.5k
            v0 = 0;
428
18.5k
            v1 = 1;
429
18.5k
            goto check_rot_left;
430
18.5k
        }
431
73.5M
        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
25.3k
                    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
20.0k
                        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.5M
        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.1M
        else if (c[3].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
502
15.1k
        {
503
            /* Check 3+2 off right. */
504
15.1k
            v0 = 3;
505
15.1k
            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
668
                    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
6.51k
                        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.5M
        if ((c[0].vertex.p.y <= pfs->rect.p.y &&
569
73.5M
             c[1].vertex.p.y <= pfs->rect.p.y &&
570
73.5M
             c[2].vertex.p.y <= pfs->rect.p.y &&
571
73.5M
             c[3].vertex.p.y <= pfs->rect.p.y) ||
572
73.5M
            (c[0].vertex.p.y >= pfs->rect.q.y &&
573
73.5M
             c[1].vertex.p.y >= pfs->rect.q.y &&
574
73.5M
             c[2].vertex.p.y >= pfs->rect.q.y &&
575
73.5M
             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
372k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[1].vertex.p.y < pfs->rect.p.y)
582
10.1k
        {
583
            /* Check 0+1 off above. */
584
10.1k
            v0 = 0;
585
10.1k
            v1 = 1;
586
10.1k
            goto check_above;
587
10.1k
        }
588
362k
        else if (c[3].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
589
7.44k
        {
590
            /* Check 3+2 off above. */
591
7.44k
            v0 = 3;
592
7.44k
            v1 = 2;
593
17.5k
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
17.5k
            do
597
19.9k
            {
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
19.9k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
605
19.9k
                if (m0 >= pfs->rect.p.y)
606
6.57k
                    goto check_above_quarter;
607
13.4k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
608
13.4k
                if (m1 >= pfs->rect.p.y)
609
10.9k
                    goto check_above_quarter;
610
                /* So, we can completely discard the top half of the patch. */
611
2.45k
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
612
2.45k
                c[v0].vertex.p.y = m0;
613
2.45k
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
614
2.45k
                c[v1].vertex.p.y = m1;
615
2.45k
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
616
2.45k
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
617
2.45k
                changed = 1;
618
2.45k
            }
619
17.5k
            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
17.5k
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
17.5k
                do
626
21.3k
                {
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
21.3k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
636
21.3k
                    if (m0 >= pfs->rect.p.y)
637
5.59k
                        break;
638
15.7k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
639
15.7k
                    if (m1 >= pfs->rect.p.y)
640
11.9k
                        break;
641
                    /* So, we can completely discard the top half of the patch. */
642
3.75k
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
643
3.75k
                    c[v0].vertex.p.y = m0;
644
3.75k
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
645
3.75k
                    c[v1].vertex.p.y = m1;
646
3.75k
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
647
3.75k
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
648
3.75k
                    changed = 1;
649
3.75k
                }
650
17.5k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
651
17.5k
            }
652
0
        }
653
654
        /* or the bottom half? */
655
372k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[1].vertex.p.y > pfs->rect.q.y)
656
7.67k
        {
657
            /* Check 0+1 off bottom. */
658
7.67k
            v0 = 0;
659
7.67k
            v1 = 1;
660
7.67k
            goto check_bottom;
661
7.67k
        }
662
364k
        else if (c[3].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
663
9.69k
        {
664
            /* Check 3+2 off bottom. */
665
9.69k
            v0 = 3;
666
9.69k
            v1 = 2;
667
17.3k
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
17.3k
            do
671
20.0k
            {
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
20.0k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
679
20.0k
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
680
13.1k
                    goto check_bottom_quarter;
681
6.87k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
682
6.87k
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
683
4.17k
                    goto check_bottom_quarter;
684
                /* So, we can completely discard the bottom half of the patch. */
685
2.69k
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
686
2.69k
                c[v0].vertex.p.y = m0;
687
2.69k
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
688
2.69k
                c[v1].vertex.p.y = m1;
689
2.69k
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
690
2.69k
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
691
2.69k
                changed = 1;
692
2.69k
            }
693
17.3k
            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
17.3k
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
17.3k
                do
700
21.4k
                {
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
21.4k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
710
21.4k
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
711
12.6k
                        break;
712
8.81k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
713
8.81k
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
714
4.71k
                        break;
715
                    /* So, we can completely discard the bottom half of the patch. */
716
4.10k
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
717
4.10k
                    c[v0].vertex.p.y = m0;
718
4.10k
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
719
4.10k
                    c[v1].vertex.p.y = m1;
720
4.10k
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
721
4.10k
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
722
4.10k
                    changed = 1;
723
4.10k
                }
724
17.3k
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
725
17.3k
            }
726
0
        }
727
728
        /* Now, rotated cases: Can we cull the top half? */
729
372k
        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
371k
        else if (c[1].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
737
700
        {
738
            /* Check 1+2 off above. */
739
700
            v0 = 1;
740
700
            v1 = 2;
741
1.70k
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.70k
            do
745
2.12k
            {
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.12k
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
753
2.12k
                if (m0 >= pfs->rect.p.y)
754
1.69k
                    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.70k
            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.70k
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.70k
                do
774
1.76k
                {
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.76k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
784
1.76k
                    if (m0 >= pfs->rect.p.y)
785
1.69k
                        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.70k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
799
1.70k
            }
800
0
        }
801
802
        /* or the bottom half? */
803
372k
        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
372k
        else if (c[1].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
811
71
        {
812
            /* Check 1+2 off the bottom. */
813
71
            v0 = 1;
814
71
            v1 = 2;
815
109
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
109
            do
819
448
            {
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
448
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
827
448
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
828
109
                    goto check_rot_bottom_quarter;
829
339
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
830
339
                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
339
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
834
339
                c[v0].vertex.p.y = m0;
835
339
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
836
339
                c[v1].vertex.p.y = m1;
837
339
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
838
339
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
839
339
                changed = 1;
840
339
            }
841
339
            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
109
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
109
                do
848
157
                {
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
157
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
858
157
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
859
109
                        break;
860
48
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
861
48
                    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
48
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
865
48
                    c[v0].vertex.p.y = m0;
866
48
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
867
48
                    c[v1].vertex.p.y = m1;
868
48
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
869
48
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
870
48
                    changed = 1;
871
48
                }
872
109
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
873
109
            }
874
0
        }
875
372k
    } while (changed);
876
877
358k
    c[0].vertex.cc[1] = c[1].vertex.cc[1] =
878
358k
                        c[2].vertex.cc[1] =
879
358k
                        c[3].vertex.cc[1] = 0;
880
358k
    make_other_poles(c);
881
358k
    return patch_fill(pfs, c, NULL, NULL);
882
80.1M
}
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
4
        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
128
            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.41k
#define midpoint(a,b)      ((a+b)/2)
995
996
127
#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
353
{
1001
353
    double m0, m1;
1002
353
    int v0, v1;
1003
353
    int changed;
1004
1005
353
    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.45k
#define MIDPOINT_ACCURACY 0.0001
1028
84
#define QUARTERPOINT_ACCURACY 0.0003
1029
1030
120
    do {
1031
120
        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
120
        if ((cc->corners[0].x <= pfs->rect.p.x &&
1038
120
             cc->corners[1].x <= pfs->rect.p.x &&
1039
120
             cc->corners[2].x <= pfs->rect.p.x &&
1040
120
             cc->corners[3].x <= pfs->rect.p.x) ||
1041
120
            (cc->corners[0].x >= pfs->rect.q.x &&
1042
92
             cc->corners[1].x >= pfs->rect.q.x &&
1043
92
             cc->corners[2].x >= pfs->rect.q.x &&
1044
92
             cc->corners[3].x >= pfs->rect.q.x))
1045
44
                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
76
        if (cc->corners[0].x < pfs->rect.p.x && cc->corners[3].x < pfs->rect.p.x)
1051
10
        {
1052
            /* Check 0+3 off left. */
1053
10
            v0 = 0;
1054
10
            v1 = 3;
1055
10
            goto check_left;
1056
10
        }
1057
66
        else if (cc->corners[1].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1058
31
        {
1059
            /* Check 1+2 off left. */
1060
31
            v0 = 1;
1061
31
            v1 = 2;
1062
41
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
41
            do
1066
269
            {
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
269
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1073
269
                if (m0 >= pfs->rect.p.x)
1074
41
                    goto check_left_quarter;
1075
228
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1076
228
                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
228
                cc->corners[v0].x = m0;
1080
228
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1081
228
                cc->corners[v1].x = m1;
1082
228
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1083
228
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1084
228
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1085
228
                changed = 1;
1086
228
            }
1087
228
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1088
0
            if (0)
1089
0
            {
1090
41
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
41
                do
1094
41
                {
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
41
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1101
41
                    if (m0 >= pfs->rect.p.x)
1102
41
                        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
41
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1116
41
            }
1117
0
        }
1118
1119
        /* or the right hand half? */
1120
76
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[3].x > pfs->rect.q.x)
1121
41
        {
1122
            /* Check 0+3 off right. */
1123
41
            v0 = 0;
1124
41
            v1 = 3;
1125
41
            goto check_right;
1126
41
        }
1127
35
        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
44
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
44
            do
1136
321
            {
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
321
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1143
321
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1144
43
                    goto check_right_quarter;
1145
278
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1146
278
                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
277
                cc->corners[v0].x = m0;
1150
277
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1151
277
                cc->corners[v1].x = m1;
1152
277
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1153
277
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1154
277
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1155
277
                changed = 1;
1156
277
            }
1157
277
            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
44
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
44
                do
1164
44
                {
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
44
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1171
44
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1172
43
                        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
44
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1186
44
            }
1187
0
        }
1188
1189
        /* Now, rotated cases: Can we cull the left hand half? */
1190
76
        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
76
        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
76
        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
76
        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
76
        if ((cc->corners[0].y <= pfs->rect.p.y &&
1335
76
             cc->corners[1].y <= pfs->rect.p.y &&
1336
76
             cc->corners[2].y <= pfs->rect.p.y &&
1337
76
             cc->corners[3].y <= pfs->rect.p.y) ||
1338
76
            (cc->corners[0].y >= pfs->rect.q.y &&
1339
59
             cc->corners[1].y >= pfs->rect.q.y &&
1340
59
             cc->corners[2].y >= pfs->rect.q.y &&
1341
59
             cc->corners[3].y >= pfs->rect.q.y))
1342
17
            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
59
        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
51
        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
59
        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
59
        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
59
        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
43
        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
59
        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
56
        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
59
    } while (changed);
1642
1643
23
    return A_fill_region_floats(pfs, cc, 0);
1644
84
}
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
361k
{
1653
361k
    const gs_shading_A_t * const psh = pfs->psh;
1654
361k
    double x0 = psh->params.Coords[0] + pfs->delta.x * pfs->v0;
1655
361k
    double y0 = psh->params.Coords[1] + pfs->delta.y * pfs->v0;
1656
361k
    double x1 = psh->params.Coords[0] + pfs->delta.x * pfs->v1;
1657
361k
    double y1 = psh->params.Coords[1] + pfs->delta.y * pfs->v1;
1658
361k
    double h0 = pfs->u0, h1 = pfs->u1;
1659
361k
    corners_and_curves cc;
1660
361k
    int code;
1661
1662
361k
    double dx0 = pfs->delta.x * h0;
1663
361k
    double dy0 = pfs->delta.y * h0;
1664
361k
    double dx1 = pfs->delta.x * h1;
1665
361k
    double dy1 = pfs->delta.y * h1;
1666
1667
361k
    cc.curve[0].vertex.cc[0] = pfs->t0; /* The element cc[1] is set to a dummy value against */
1668
361k
    cc.curve[1].vertex.cc[0] = pfs->t1; /* interrupts while an idle processing in gxshade6.c .  */
1669
361k
    cc.curve[2].vertex.cc[0] = pfs->t1;
1670
361k
    cc.curve[3].vertex.cc[0] = pfs->t0;
1671
361k
    cc.corners[0].x = x0 + dy0;
1672
361k
    cc.corners[0].y = y0 - dx0;
1673
361k
    cc.corners[1].x = x1 + dy0;
1674
361k
    cc.corners[1].y = y1 - dx0;
1675
361k
    cc.corners[2].x = x1 + dy1;
1676
361k
    cc.corners[2].y = y1 - dx1;
1677
361k
    cc.corners[3].x = x0 + dy1;
1678
361k
    cc.corners[3].y = y0 - dx1;
1679
361k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[0].x, cc.corners[0].y, &cc.curve[0].vertex.p);
1680
361k
    if (code < 0)
1681
342
        goto fail;
1682
361k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[1].x, cc.corners[1].y, &cc.curve[1].vertex.p);
1683
361k
    if (code < 0)
1684
11
        goto fail;
1685
361k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[2].x, cc.corners[2].y, &cc.curve[2].vertex.p);
1686
361k
    if (code < 0)
1687
0
        goto fail;
1688
361k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[3].x, cc.corners[3].y, &cc.curve[3].vertex.p);
1689
361k
    if (code < 0)
1690
0
        goto fail;
1691
361k
    return subdivide_patch_fill(pfs1, cc.curve);
1692
353
fail:
1693
353
    if (code != gs_error_limitcheck)
1694
0
        return code;
1695
353
    code = gs_point_transform(cc.corners[0].x, cc.corners[0].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[0]);
1696
353
    if (code < 0)
1697
0
        return code;
1698
353
    code = gs_point_transform(cc.corners[1].x, cc.corners[1].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[1]);
1699
353
    if (code < 0)
1700
0
        return code;
1701
353
    code = gs_point_transform(cc.corners[2].x, cc.corners[2].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[2]);
1702
353
    if (code < 0)
1703
0
        return code;
1704
353
    code = gs_point_transform(cc.corners[3].x, cc.corners[3].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[3]);
1705
353
    if (code < 0)
1706
0
        return code;
1707
353
    return subdivide_patch_fill_floats(pfs1, &cc);
1708
353
}
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
264k
{
1715
264k
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
1716
264k
    gs_function_t * const pfn = psh->params.Function;
1717
264k
    gs_matrix cmat;
1718
264k
    gs_rect t_rect;
1719
264k
    A_fill_state_t state;
1720
264k
    patch_fill_state_t pfs1;
1721
264k
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
1722
264k
    float dd = d1 - d0;
1723
264k
    double t0, t1;
1724
264k
    gs_point dist;
1725
264k
    int code;
1726
1727
264k
    state.psh = psh;
1728
264k
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
1729
264k
    if (code < 0)
1730
37
        return code;
1731
264k
    pfs1.Function = pfn;
1732
264k
    pfs1.rect = *clip_rect;
1733
264k
    code = init_patch_fill_state(&pfs1);
1734
264k
    if (code < 0)
1735
0
        goto fail;
1736
264k
    pfs1.maybe_self_intersecting = false;
1737
264k
    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
264k
    cmat.tx = psh->params.Coords[0];
1744
264k
    cmat.ty = psh->params.Coords[1];
1745
264k
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
1746
264k
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
1747
264k
    cmat.yx = state.delta.x;
1748
264k
    cmat.yy = state.delta.y;
1749
264k
    cmat.xx = cmat.yy;
1750
264k
    cmat.xy = -cmat.yx;
1751
264k
    code = gs_bbox_transform_inverse(rect, &cmat, &t_rect);
1752
264k
    if (code < 0) {
1753
0
        code = 0; /* Swallow this silently */
1754
0
        goto fail;
1755
0
    }
1756
264k
    t0 = min(max(t_rect.p.y, 0), 1);
1757
264k
    t1 = max(min(t_rect.q.y, 1), 0);
1758
264k
    state.v0 = t0;
1759
264k
    state.v1 = t1;
1760
264k
    state.u0 = t_rect.p.x;
1761
264k
    state.u1 = t_rect.q.x;
1762
264k
    state.t0 = t0 * dd + d0;
1763
264k
    state.t1 = t1 * dd + d0;
1764
264k
    code = gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pgs),
1765
264k
                          &dist);
1766
264k
    if (code < 0)
1767
0
        goto fail;
1768
264k
    state.length = hypot(dist.x, dist.y); /* device space line length */
1769
264k
    code = A_fill_region(&state, &pfs1);
1770
264k
    if (psh->params.Extend[0] && t0 > t_rect.p.y) {
1771
48.5k
        if (code < 0)
1772
0
            goto fail;
1773
        /* Use the general algorithm, because we need the trapping. */
1774
48.5k
        state.v0 = t_rect.p.y;
1775
48.5k
        state.v1 = t0;
1776
48.5k
        state.t0 = state.t1 = t0 * dd + d0;
1777
48.5k
        code = A_fill_region(&state, &pfs1);
1778
48.5k
    }
1779
264k
    if (psh->params.Extend[1] && t1 < t_rect.q.y) {
1780
48.7k
        if (code < 0)
1781
0
            goto fail;
1782
        /* Use the general algorithm, because we need the trapping. */
1783
48.7k
        state.v0 = t1;
1784
48.7k
        state.v1 = t_rect.q.y;
1785
48.7k
        state.t0 = state.t1 = t1 * dd + d0;
1786
48.7k
        code = A_fill_region(&state, &pfs1);
1787
48.7k
    }
1788
264k
fail:
1789
264k
    gsicc_release_link(pfs1.icclink);
1790
264k
    if (term_patch_fill_state(&pfs1))
1791
0
        return_error(gs_error_unregistered); /* Must not happen. */
1792
264k
    return code;
1793
264k
}
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
264k
{
1800
264k
    return gs_shading_A_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
1801
264k
}
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
337
{
1860
337
    double dx = x1 - x0, dy = y1 - y0;
1861
337
    double d = hypot(dx, dy);
1862
337
    gs_point p0, p1, pc0, pc1;
1863
337
    int k, j, code, dirn;
1864
337
    bool inside = 0;
1865
1866
    /* pc0 and pc1 are the centres of the respective circles. */
1867
337
    pc0.x = x0, pc0.y = y0;
1868
337
    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
337
    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
337
        p0.x = 0, p0.y = -1, dirn = 0;
1876
        /* Align stripes along radii for faster triangulation : */
1877
337
        inside = 1;
1878
337
        pfs->function_arg_shift = 1;
1879
337
    } 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.66k
    for (k = 0; k < 4; k++) {
1898
1.34k
        gs_point p[12];
1899
1.34k
        patch_curve_t curve[4];
1900
1901
        /* Set p1 to be 90 degrees anticlockwise from p0 */
1902
1.34k
        p1.x = -p0.y; p1.y = p0.x;
1903
1.34k
        if (dirn == 0) { /* Clockwise */
1904
1.00k
            make_quadrant_arc(p + 0, &pc0, &p1, &p0, r0);
1905
1.00k
            make_quadrant_arc(p + 6, &pc1, &p0, &p1, r1);
1906
1.00k
        } else { /* Anticlockwise */
1907
337
            make_quadrant_arc(p + 0, &pc0, &p0, &p1, r0);
1908
337
            make_quadrant_arc(p + 6, &pc1, &p1, &p0, r1);
1909
337
        }
1910
1.34k
        p[4].x = (p[3].x * 2 + p[6].x) / 3;
1911
1.34k
        p[4].y = (p[3].y * 2 + p[6].y) / 3;
1912
1.34k
        p[5].x = (p[3].x + p[6].x * 2) / 3;
1913
1.34k
        p[5].y = (p[3].y + p[6].y * 2) / 3;
1914
1.34k
        p[10].x = (p[9].x * 2 + p[0].x) / 3;
1915
1.34k
        p[10].y = (p[9].y * 2 + p[0].y) / 3;
1916
1.34k
        p[11].x = (p[9].x + p[0].x * 2) / 3;
1917
1.34k
        p[11].y = (p[9].y + p[0].y * 2) / 3;
1918
6.70k
        for (j = 0; j < 4; j++) {
1919
5.36k
            int jj = (j + inside) % 4;
1920
1921
5.36k
            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
5.36k
            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
5.36k
            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
5.36k
            curve[j].straight = (((j + inside) & 1) != 0);
1930
5.36k
        }
1931
1.34k
        curve[(0 + inside) % 4].vertex.cc[0] = t0;
1932
1.34k
        curve[(1 + inside) % 4].vertex.cc[0] = t0;
1933
1.34k
        curve[(2 + inside) % 4].vertex.cc[0] = t1;
1934
1.34k
        curve[(3 + inside) % 4].vertex.cc[0] = t1;
1935
1.34k
        curve[0].vertex.cc[1] = curve[1].vertex.cc[1] = 0; /* Initialize against FPE. */
1936
1.34k
        curve[2].vertex.cc[1] = curve[3].vertex.cc[1] = 0; /* Initialize against FPE. */
1937
1.34k
        code = patch_fill(pfs, curve, NULL, NULL);
1938
1.34k
        if (code < 0)
1939
8
            return code;
1940
        /* Move p0 to be ready for the next position */
1941
1.33k
        if (k == 0) {
1942
            /* p0 moves clockwise */
1943
337
            p1 = p0;
1944
337
            p0.x = p1.y; p0.y = -p1.x;
1945
337
            dirn = 0;
1946
995
        } else if (k == 1) {
1947
            /* p0 flips sides */
1948
337
            p0.x = -p0.x; p0.y = -p0.y;
1949
337
            dirn = 1;
1950
658
        } else if (k == 2) {
1951
            /* p0 moves anti-clockwise */
1952
329
            p1 = p0;
1953
329
            p0.x = -p1.y; p0.y = p1.x;
1954
329
            dirn = 0;
1955
329
        }
1956
1.33k
    }
1957
329
    return 0;
1958
337
}
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
146
{
2931
146
    double d, dd;
2932
2933
146
    dd = hypot(rect->p.x - x0, rect->p.y - y0);
2934
146
    d = hypot(rect->p.x - x0, rect->q.y - y0);
2935
146
    dd = max(dd, d);
2936
146
    d = hypot(rect->q.x - x0, rect->q.y - y0);
2937
146
    dd = max(dd, d);
2938
146
    d = hypot(rect->q.x - x0, rect->p.y - y0);
2939
146
    dd = max(dd, d);
2940
146
    return dd;
2941
146
}
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
296
{
3092
296
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3093
296
    double r0 = psh->params.Coords[2];
3094
296
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3095
296
    double r1 = psh->params.Coords[5];
3096
296
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
3097
296
    double d = hypot(dx, dy), r;
3098
296
    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
296
    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
296
    if (dr > d - 1e-4 * (d + dr)) {
3157
        /* Nested circles, or degenerate. */
3158
296
        if (r0 > r1) {
3159
5
            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
5
            if (Extend1 && r1 > 0)
3168
5
                return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3169
291
        } else {
3170
291
            if (Extend1) {
3171
146
                r = R_rect_radius(rect, x1, y1);
3172
146
                if (r > r1) {
3173
146
                    code = R_tensor_annulus(pfs, x1, y1, r, t1, x1, y1, r1, t1);
3174
146
                    if (code < 0)
3175
0
                        return code;
3176
146
                }
3177
146
            }
3178
291
            if (Extend0 && r0 > 0)
3179
0
                return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3180
291
        }
3181
296
    } 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
291
    return 0;
3229
296
}
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
5.04k
#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
738
{
3298
738
    double cx = rsa->x0 + (rsa->x1 - rsa->x0) * t;
3299
738
    double cy = rsa->y0 + (rsa->y1 - rsa->y0) * t;
3300
738
    double rx = rsa->p[point_index].x - cx;
3301
738
    double ry = rsa->p[point_index].y - cy;
3302
738
    double dx = rsa->p[point_index - 1].x - rsa->p[point_index].x;
3303
738
    double dy = rsa->p[point_index - 1].y - rsa->p[point_index].y;
3304
3305
738
    if (at_corner) {
3306
720
        double Dx = rsa->p[point_index + 1].x - rsa->p[point_index].x;
3307
720
        double Dy = rsa->p[point_index + 1].y - rsa->p[point_index].y;
3308
720
        bool b1 = (dx * rx + dy * ry >= 0);
3309
720
        bool b2 = (Dx * rx + Dy * ry >= 0);
3310
3311
720
        if (b1 & b2)
3312
164
            rsa->outer_contact[root_index] = true;
3313
720
    } else {
3314
18
        if (rx * dy - ry * dx < 0)
3315
6
            rsa->outer_contact[root_index] = true;
3316
18
    }
3317
738
}
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.44k
{
3322
1.44k
    int i;
3323
3324
4.32k
    for (i = 0; i < 2; i++) {
3325
2.88k
        bool good_root;
3326
3327
2.88k
        if (!have_root[i])
3328
1.42k
            continue;
3329
1.45k
        good_root = (!rsa->have_apex || (rsa->apex <= 0 || r0 == 0 ? t[i] >= rsa->apex : t[i] <= rsa->apex));
3330
1.45k
        if (good_root) {
3331
738
            radial_shading_external_contact(rsa, point_index, t[i], r0, r1, at_corner, i);
3332
738
            if (!rsa->have_root[i]) {
3333
45
                rsa->span[i][0] = rsa->span[i][1] = t[i];
3334
45
                rsa->have_root[i] = true;
3335
693
            } else {
3336
693
                if (rsa->span[i][0] > t[i])
3337
46
                    rsa->span[i][0] = t[i];
3338
693
                if (rsa->span[i][1] < t[i])
3339
101
                    rsa->span[i][1] = t[i];
3340
693
            }
3341
738
        }
3342
1.45k
    }
3343
1.44k
}
3344
3345
static void
3346
compute_radial_shading_span_extended_side(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3347
720
{
3348
720
    double cc, c;
3349
720
    bool have_root[2] = {false, false};
3350
720
    double t[2];
3351
720
    bool by_x = (rsa->p[point_index].x != rsa->p[point_index + 1].x);
3352
720
    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
720
    if (by_x) {
3365
344
        c = rsa->p[point_index].x - rsa->x0;
3366
344
        cc = rsa->x1 - rsa->x0;
3367
376
    } else {
3368
376
        c = rsa->p[point_index].y - rsa->y0;
3369
376
        cc = rsa->y1 - rsa->y0;
3370
376
    }
3371
720
    t[0] = (c - r0) / (cc + r1 - r0);
3372
720
    t[1] = (c + r0) / (cc - r1 + r0);
3373
720
    if (t[0] > t[1]) {
3374
504
        c    = t[0];
3375
504
        t[0] = t[1];
3376
504
        t[1] = c;
3377
504
    }
3378
2.16k
    for (i = 0; i < 2; i++) {
3379
1.44k
        double d, d0, d1;
3380
3381
1.44k
        if (by_x) {
3382
688
            d = rsa->y1 - rsa->y0 + r0 + (r1 - r0) * t[i];
3383
688
            d0 = rsa->p[point_index].y;
3384
688
            d1 = rsa->p[point_index + 1].y;
3385
752
        } else {
3386
752
            d = rsa->x1 - rsa->x0 + r0 + (r1 - r0) * t[i];
3387
752
            d0 = rsa->p[point_index].x;
3388
752
            d1 = rsa->p[point_index + 1].x;
3389
752
        }
3390
1.44k
        if (d1 > d0 ? d0 <= d && d <= d1 : d1 <= d && d <= d0)
3391
18
            have_root[i] = true;
3392
1.44k
    }
3393
720
    store_roots(rsa, have_root, t, r0, r1, point_index, false);
3394
720
}
3395
3396
static int
3397
compute_radial_shading_span_extended_point(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3398
720
{
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
720
    double p1x = rsa->x1 - rsa->x0;
3423
720
    double p1y = rsa->y1 - rsa->y0;
3424
720
    double qx  = rsa->p[point_index].x - rsa->x0;
3425
720
    double qy  = rsa->p[point_index].y - rsa->y0;
3426
720
    double a   = (Pw2(p1x) + Pw2(p1y) - Pw2(r0 - r1));
3427
720
    bool have_root[2] = {false, false};
3428
720
    double t[2];
3429
3430
720
    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
720
    } 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
720
        double minushalfb = r0*(r1-r0) + p1x*qx + p1y*qy;
3442
720
        double c          = Pw2(qx) + Pw2(qy) - Pw2(r0);
3443
720
        double desc2      = Pw2(minushalfb) - a*c; /* desc2 = 1/4 (b^2-4ac) */
3444
3445
720
        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
720
        } else {
3449
720
            double desc1 = sqrt(desc2); /* desc1 = 1/2 SQRT(b^2-4ac) */
3450
3451
720
            if (a > 0) {
3452
0
                t[0] = (minushalfb - desc1) / a;
3453
0
                t[1] = (minushalfb + desc1) / a;
3454
720
            } else {
3455
720
                t[0] = (minushalfb + desc1) / a;
3456
720
                t[1] = (minushalfb - desc1) / a;
3457
720
            }
3458
720
            have_root[0] = have_root[1] = true;
3459
720
        }
3460
720
    }
3461
720
    store_roots(rsa, have_root, t, r0, r1, point_index, true);
3462
720
    if (have_root[0] && have_root[1])
3463
720
        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
180
{
3476
180
    int span_type0, span_type1;
3477
3478
180
    span_type0 = compute_radial_shading_span_extended_point(rsa, r0, r1, 1);
3479
180
    if (span_type0 == -1)
3480
0
        return -1;
3481
180
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 2);
3482
180
    if (span_type0 != span_type1)
3483
0
        return -1;
3484
180
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 3);
3485
180
    if (span_type0 != span_type1)
3486
0
        return -1;
3487
180
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 4);
3488
180
    if (span_type0 != span_type1)
3489
0
        return -1;
3490
180
    compute_radial_shading_span_extended_side(rsa, r0, r1, 1);
3491
180
    compute_radial_shading_span_extended_side(rsa, r0, r1, 2);
3492
180
    compute_radial_shading_span_extended_side(rsa, r0, r1, 3);
3493
180
    compute_radial_shading_span_extended_side(rsa, r0, r1, 4);
3494
180
    return span_type0;
3495
180
}
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
45
{
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
45
    const double extent = 1.02;
3558
45
    int span_type0, span_type1, span_type;
3559
3560
45
    memset(rsa, 0, sizeof(*rsa));
3561
45
    rsa->x0 = x0;
3562
45
    rsa->y0 = y0;
3563
45
    rsa->x1 = x1;
3564
45
    rsa->y1 = y1;
3565
45
    rsa->p[0] = rsa->p[4] = rect->p;
3566
45
    rsa->p[1].x = rsa->p[5].x = rect->p.x;
3567
45
    rsa->p[1].y = rsa->p[5].y = rect->q.y;
3568
45
    rsa->p[2] = rect->q;
3569
45
    rsa->p[3].x = rect->q.x;
3570
45
    rsa->p[3].y = rect->p.y;
3571
45
    rsa->have_apex = any_abs(r1 - r0) > 1e-7 * any_abs(r1 + r0);
3572
45
    rsa->apex = (rsa->have_apex ? -r0 / (r1 - r0) : 0);
3573
45
    span_type0 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 * extent);
3574
45
    if (span_type0 == -1)
3575
0
        return -1;
3576
45
    span_type1 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 / extent);
3577
45
    if (span_type0 != span_type1)
3578
0
        return -1;
3579
45
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 * extent);
3580
45
    if (span_type0 != span_type1)
3581
0
        return -1;
3582
45
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 / extent);
3583
45
    if (span_type1 == -1)
3584
0
        return -1;
3585
45
    if (r0 < r1) {
3586
40
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3587
0
            rsa->span[0][0] = rsa->apex; /* Likely never happens. Remove ? */
3588
40
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3589
10
            rsa->span[1][0] = rsa->apex;
3590
40
    } else if (r0 > r1) {
3591
5
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3592
0
            rsa->span[0][1] = rsa->apex;
3593
5
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3594
0
            rsa->span[1][1] = rsa->apex; /* Likely never happens. Remove ? */
3595
5
    }
3596
45
    span_type = 0;
3597
45
    if (rsa->have_root[0] && rsa->span[0][0] < 0)
3598
0
        span_type |= 1;
3599
45
    if (rsa->have_root[1] && rsa->span[1][0] < 0)
3600
0
        span_type |= 1;
3601
45
    if (rsa->have_root[0] && rsa->span[0][1] > 0 && rsa->span[0][0] < 1)
3602
5
        span_type |= 2;
3603
45
    if (rsa->have_root[1] && rsa->span[1][1] > 0 && rsa->span[1][0] < 1)
3604
39
        span_type |= 4;
3605
45
    if (rsa->have_root[0] && rsa->span[0][1] > 1)
3606
5
        span_type |= 8;
3607
45
    if (rsa->have_root[1] && rsa->span[1][1] > 1)
3608
8
        span_type |= 8;
3609
45
    return span_type;
3610
45
}
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
44
{
3615
44
    double s0 = span_[0], s1 = span_[1], w;
3616
3617
44
    if (s0 < 0)
3618
0
        s0 = 0;
3619
44
    if (s1 < 0)
3620
0
        s1 = 0;
3621
44
    if (s0 > 1)
3622
0
        s0 = 1;
3623
44
    if (s1 > 1)
3624
12
        s1 = 1;
3625
44
    w = s1 - s0;
3626
44
    if (w == 0)
3627
0
        return false; /* Don't pass a degenerate shading. */
3628
44
    if (w > 0.3)
3629
33
        return false; /* The span is big, don't shorten it. */
3630
11
    { /* Do shorten. */
3631
11
        double R0 = *r0, X0 = *x0, Y0 = *y0, D0 = *d0;
3632
11
        double R1 = *r1, X1 = *x1, Y1 = *y1, D1 = *d1;
3633
3634
11
        *r0 = R0 + (R1 - R0) * s0;
3635
11
        *x0 = X0 + (X1 - X0) * s0;
3636
11
        *y0 = Y0 + (Y1 - Y0) * s0;
3637
11
        *d0 = D0 + (D1 - D0) * s0;
3638
11
        *r1 = R0 + (R1 - R0) * s1;
3639
11
        *x1 = X0 + (X1 - X0) * s1;
3640
11
        *y1 = Y0 + (Y1 - Y0) * s1;
3641
11
        *d1 = D0 + (D1 - D0) * s1;
3642
11
    }
3643
11
    return true;
3644
44
}
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
187
{
3649
187
    const double d = hypot(x1 - x0, y1 - y0);
3650
187
    const double area0 = M_PI * r0 * r0 / 2;
3651
187
    const double area1 = M_PI * r1 * r1 / 2;
3652
187
    const double area2 = (r0 + r1) / 2 * d;
3653
187
    const double arbitrary = 8;
3654
187
    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
187
    areaX = (rect->q.x - rect->p.x) * (rect->q.x - rect->p.x);
3662
187
    if (areaX * arbitrary < area0 + area1 + area2)
3663
35
        return true;
3664
152
    areaY = (rect->q.y - rect->p.y) * (rect->q.y - rect->p.y);
3665
152
    if (areaY * arbitrary < area0 + area1 + area2)
3666
10
        return true;
3667
142
    return false;
3668
152
}
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
191
{
3675
191
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
3676
191
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
3677
191
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3678
191
    double r0 = psh->params.Coords[2];
3679
191
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3680
191
    double r1 = psh->params.Coords[5];
3681
191
    radial_shading_attrs_t rsa;
3682
191
    int span_type; /* <0 - don't shorten, 1 - extent0, 2 - first contact, 4 - last contact, 8 - extent1. */
3683
191
    int code;
3684
191
    patch_fill_state_t pfs1;
3685
3686
191
    if (r0 == 0 && r1 == 0)
3687
4
        return 0; /* PLRM requires to paint nothing. */
3688
187
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
3689
187
    if (code < 0)
3690
0
        return code;
3691
187
    pfs1.Function = psh->params.Function;
3692
187
    code = init_patch_fill_state(&pfs1);
3693
187
    if (code < 0) {
3694
0
        if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3695
0
        return code;
3696
0
    }
3697
187
    pfs1.function_arg_shift = 0;
3698
187
    pfs1.rect = *clip_rect;
3699
187
    pfs1.maybe_self_intersecting = false;
3700
187
    if (is_radial_shading_large(x0, y0, r0, x1, y1, r1, rect))
3701
45
        span_type = compute_radial_shading_span(&rsa, x0, y0, r0, x1, y1, r1, rect);
3702
142
    else
3703
142
        span_type = -1;
3704
187
    if (span_type < 0) {
3705
142
        code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3706
142
        if (code >= 0)
3707
142
            code = R_tensor_annulus(&pfs1, x0, y0, r0, d0, x1, y1, r1, d1);
3708
142
        if (code >= 0)
3709
142
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3710
142
    } else if (span_type == 1) {
3711
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d0);
3712
45
    } else if (span_type == 8) {
3713
1
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d1);
3714
44
    } else {
3715
44
        bool second_interval = true;
3716
3717
44
        code = 0;
3718
44
        if (span_type & 1)
3719
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3720
44
        if ((code >= 0) && (span_type & 2)) {
3721
5
            float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3722
5
            double R0 = r0, R1 = r1;
3723
3724
5
            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
5
            } else {
3732
5
                second_interval = shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[0]);
3733
5
            }
3734
5
            code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3735
5
        }
3736
44
        if (code >= 0 && second_interval) {
3737
44
            if (span_type & 4) {
3738
39
                float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3739
39
                double R0 = r0, R1 = r1;
3740
3741
39
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[1]);
3742
39
                code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3743
39
            }
3744
44
        }
3745
44
        if (code >= 0 && (span_type & 8))
3746
12
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3747
44
    }
3748
187
    if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3749
187
    if (term_patch_fill_state(&pfs1))
3750
0
        return_error(gs_error_unregistered); /* Must not happen. */
3751
187
    return code;
3752
187
}
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
191
{
3759
191
    return gs_shading_R_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
3760
191
}