Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxshade1.c
Line
Count
Source
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
43.7k
{
56
43.7k
    int i, j;
57
58
218k
    for (i = 0; i < 4; i++) {
59
174k
        j = (i + 1) % 4;
60
174k
        curve[i].control[0].x = (curve[i].vertex.p.x * 2 + curve[j].vertex.p.x) / 3;
61
174k
        curve[i].control[0].y = (curve[i].vertex.p.y * 2 + curve[j].vertex.p.y) / 3;
62
174k
        curve[i].control[1].x = (curve[i].vertex.p.x + curve[j].vertex.p.x * 2) / 3;
63
174k
        curve[i].control[1].y = (curve[i].vertex.p.y + curve[j].vertex.p.y * 2) / 3;
64
174k
        curve[i].straight = true;
65
174k
    }
66
43.7k
}
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
204M
        p[1].x < rect->p.x &&
206
109M
        p[2].x < rect->p.x &&
207
109M
        p[3].x < rect->p.x)
208
109M
        return 0; /* Clipped away! */
209
292M
    if (p[0].x > rect->q.x &&
210
190M
        p[1].x > rect->q.x &&
211
190M
        p[2].x > rect->q.x &&
212
190M
        p[3].x > rect->q.x)
213
111M
        return 0; /* Clipped away! */
214
180M
    if (p[0].y < rect->p.y &&
215
789
        p[1].y < rect->p.y &&
216
789
        p[2].y < rect->p.y &&
217
773
        p[3].y < rect->p.y)
218
0
        return 0; /* Clipped away! */
219
180M
    if (p[0].y > rect->q.y &&
220
244
        p[1].y > rect->q.y &&
221
0
        p[2].y > rect->q.y &&
222
0
        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
79.8M
{
236
79.8M
    fixed m0, m1;
237
79.8M
    int v0, v1;
238
79.8M
    int changed;
239
240
79.8M
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
241
149
        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
79.8M
    do {
266
79.8M
        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
79.8M
        if ((c[0].vertex.p.x <= pfs->rect.p.x &&
272
42.7M
             c[1].vertex.p.x <= pfs->rect.p.x &&
273
5.17k
             c[2].vertex.p.x <= pfs->rect.p.x &&
274
1.25k
             c[3].vertex.p.x <= pfs->rect.p.x) ||
275
79.8M
            (c[0].vertex.p.x >= pfs->rect.q.x &&
276
37.0M
             c[1].vertex.p.x >= pfs->rect.q.x &&
277
37.0M
             c[2].vertex.p.x >= pfs->rect.q.x &&
278
37.0M
             c[3].vertex.p.x >= pfs->rect.q.x))
279
6.61M
                return 0;
280
281
        /* First, let's try to see if we can cull the patch horizontally with the clipping
282
         * rectangle. */
283
        /* Non rotated cases first. Can we cull the left hand half? */
284
73.2M
        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.4M
        else if (c[1].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
292
18
        {
293
            /* Check 1+2 off left. */
294
18
            v0 = 1;
295
18
            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
0
                    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
0
                        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.2M
        if (c[0].vertex.p.x > pfs->rect.q.x && c[3].vertex.p.x > pfs->rect.q.x)
355
50
        {
356
            /* Check 0+3 off right. */
357
50
            v0 = 0;
358
50
            v1 = 3;
359
50
            goto check_right;
360
50
        }
361
73.2M
        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
118
                    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
118
                        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.2M
        if (c[0].vertex.p.x < pfs->rect.p.x && c[1].vertex.p.x < pfs->rect.p.x)
425
3.20k
        {
426
            /* Check 0+1 off left. */
427
3.20k
            v0 = 0;
428
3.20k
            v1 = 1;
429
3.20k
            goto check_rot_left;
430
3.20k
        }
431
73.2M
        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
4.21k
                    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
3.47k
                        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.2M
        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
42.8M
        else if (c[3].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
502
2.47k
        {
503
            /* Check 3+2 off right. */
504
2.47k
            v0 = 3;
505
2.47k
            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
116
                    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
1.06k
                        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.2M
        if ((c[0].vertex.p.y <= pfs->rect.p.y &&
569
12.1k
             c[1].vertex.p.y <= pfs->rect.p.y &&
570
6.13k
             c[2].vertex.p.y <= pfs->rect.p.y &&
571
9
             c[3].vertex.p.y <= pfs->rect.p.y) ||
572
73.2M
            (c[0].vertex.p.y >= pfs->rect.q.y &&
573
73.2M
             c[1].vertex.p.y >= pfs->rect.q.y &&
574
73.2M
             c[2].vertex.p.y >= pfs->rect.q.y &&
575
73.2M
             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
46.0k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[1].vertex.p.y < pfs->rect.p.y)
582
1.77k
        {
583
            /* Check 0+1 off above. */
584
1.77k
            v0 = 0;
585
1.77k
            v1 = 1;
586
1.77k
            goto check_above;
587
1.77k
        }
588
44.2k
        else if (c[3].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
589
1.26k
        {
590
            /* Check 3+2 off above. */
591
1.26k
            v0 = 3;
592
1.26k
            v1 = 2;
593
3.04k
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
3.04k
            do
597
3.47k
            {
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
3.47k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
605
3.47k
                if (m0 >= pfs->rect.p.y)
606
945
                    goto check_above_quarter;
607
2.52k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
608
2.52k
                if (m1 >= pfs->rect.p.y)
609
2.10k
                    goto check_above_quarter;
610
                /* So, we can completely discard the top half of the patch. */
611
427
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
612
427
                c[v0].vertex.p.y = m0;
613
427
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
614
427
                c[v1].vertex.p.y = m1;
615
427
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
616
427
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
617
427
                changed = 1;
618
427
            }
619
3.04k
            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
3.04k
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
3.04k
                do
626
3.61k
                {
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
3.61k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
636
3.61k
                    if (m0 >= pfs->rect.p.y)
637
734
                        break;
638
2.87k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
639
2.87k
                    if (m1 >= pfs->rect.p.y)
640
2.31k
                        break;
641
                    /* So, we can completely discard the top half of the patch. */
642
566
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
643
566
                    c[v0].vertex.p.y = m0;
644
566
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
645
566
                    c[v1].vertex.p.y = m1;
646
566
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
647
566
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
648
566
                    changed = 1;
649
566
                }
650
3.04k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
651
3.04k
            }
652
0
        }
653
654
        /* or the bottom half? */
655
46.0k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[1].vertex.p.y > pfs->rect.q.y)
656
1.12k
        {
657
            /* Check 0+1 off bottom. */
658
1.12k
            v0 = 0;
659
1.12k
            v1 = 1;
660
1.12k
            goto check_bottom;
661
1.12k
        }
662
44.8k
        else if (c[3].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
663
1.89k
        {
664
            /* Check 3+2 off bottom. */
665
1.89k
            v0 = 3;
666
1.89k
            v1 = 2;
667
3.02k
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
3.02k
            do
671
3.50k
            {
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
3.50k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
679
3.50k
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
680
2.75k
                    goto check_bottom_quarter;
681
748
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
682
748
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
683
267
                    goto check_bottom_quarter;
684
                /* So, we can completely discard the bottom half of the patch. */
685
481
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
686
481
                c[v0].vertex.p.y = m0;
687
481
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
688
481
                c[v1].vertex.p.y = m1;
689
481
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
690
481
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
691
481
                changed = 1;
692
481
            }
693
3.02k
            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
3.02k
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
3.02k
                do
700
3.67k
                {
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
3.67k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
710
3.67k
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
711
2.69k
                        break;
712
981
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
713
981
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
714
328
                        break;
715
                    /* So, we can completely discard the bottom half of the patch. */
716
653
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
717
653
                    c[v0].vertex.p.y = m0;
718
653
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
719
653
                    c[v1].vertex.p.y = m1;
720
653
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
721
653
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
722
653
                    changed = 1;
723
653
                }
724
3.02k
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
725
3.02k
            }
726
0
        }
727
728
        /* Now, rotated cases: Can we cull the top half? */
729
46.0k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[3].vertex.p.y < pfs->rect.p.y)
730
229
        {
731
            /* Check 0+3 off above. */
732
229
            v0 = 0;
733
229
            v1 = 3;
734
229
            goto check_rot_above;
735
229
        }
736
45.7k
        else if (c[1].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
737
61
        {
738
            /* Check 1+2 off above. */
739
61
            v0 = 1;
740
61
            v1 = 2;
741
290
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
290
            do
745
311
            {
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
311
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
753
311
                if (m0 >= pfs->rect.p.y)
754
280
                    goto check_rot_above_quarter;
755
31
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
756
31
                if (m1 >= pfs->rect.p.y)
757
10
                    goto check_rot_above_quarter;
758
                /* So, we can completely discard the top half of the patch. */
759
21
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
760
21
                c[v0].vertex.p.y = m0;
761
21
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
762
21
                c[v1].vertex.p.y = m1;
763
21
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
764
21
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
765
21
                changed = 1;
766
21
            }
767
290
            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
290
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
290
                do
774
292
                {
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
292
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
784
292
                    if (m0 >= pfs->rect.p.y)
785
280
                        break;
786
12
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
787
12
                    if (m1 >= pfs->rect.p.y)
788
10
                        break;
789
                    /* So, we can completely discard the top half of the patch. */
790
2
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
791
2
                    c[v0].vertex.p.y = m0;
792
2
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
793
2
                    c[v1].vertex.p.y = m1;
794
2
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
795
2
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
796
2
                    changed = 1;
797
2
                }
798
290
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
799
290
            }
800
0
        }
801
802
        /* or the bottom half? */
803
46.0k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[3].vertex.p.y > pfs->rect.q.y)
804
1
        {
805
            /* Check 0+3 off the bottom. */
806
1
            v0 = 0;
807
1
            v1 = 3;
808
1
            goto check_rot_bottom;
809
1
        }
810
46.0k
        else if (c[1].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
811
16
        {
812
            /* Check 1+2 off the bottom. */
813
16
            v0 = 1;
814
16
            v1 = 2;
815
17
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
17
            do
819
46
            {
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
46
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
827
46
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
828
17
                    goto check_rot_bottom_quarter;
829
29
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
830
29
                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
29
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
834
29
                c[v0].vertex.p.y = m0;
835
29
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
836
29
                c[v1].vertex.p.y = m1;
837
29
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
838
29
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
839
29
                changed = 1;
840
29
            }
841
29
            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
17
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
17
                do
848
31
                {
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
31
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
858
31
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
859
17
                        break;
860
14
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
861
14
                    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
14
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
865
14
                    c[v0].vertex.p.y = m0;
866
14
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
867
14
                    c[v1].vertex.p.y = m1;
868
14
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
869
14
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
870
14
                    changed = 1;
871
14
                }
872
17
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
873
17
            }
874
0
        }
875
46.0k
    } while (changed);
876
877
43.7k
    c[0].vertex.cc[1] = c[1].vertex.cc[1] =
878
43.7k
                        c[2].vertex.cc[1] =
879
43.7k
                        c[3].vertex.cc[1] = 0;
880
43.7k
    make_other_poles(c);
881
43.7k
    return patch_fill(pfs, c, NULL, NULL);
882
79.8M
}
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
2
        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
79.8M
        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
64
            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.31k
#define midpoint(a,b)      ((a+b)/2)
995
996
142
#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
40
{
1001
40
    double m0, m1;
1002
40
    int v0, v1;
1003
40
    int changed;
1004
1005
40
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
1006
0
        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
2.46k
#define MIDPOINT_ACCURACY 0.0001
1028
71
#define QUARTERPOINT_ACCURACY 0.0003
1029
1030
90
    do {
1031
90
        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
90
        if ((cc->corners[0].x <= pfs->rect.p.x &&
1038
7
             cc->corners[1].x <= pfs->rect.p.x &&
1039
4
             cc->corners[2].x <= pfs->rect.p.x &&
1040
4
             cc->corners[3].x <= pfs->rect.p.x) ||
1041
86
            (cc->corners[0].x >= pfs->rect.q.x &&
1042
81
             cc->corners[1].x >= pfs->rect.q.x &&
1043
17
             cc->corners[2].x >= pfs->rect.q.x &&
1044
17
             cc->corners[3].x >= pfs->rect.q.x))
1045
21
                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
69
        if (cc->corners[0].x < pfs->rect.p.x && cc->corners[3].x < pfs->rect.p.x)
1051
3
        {
1052
            /* Check 0+3 off left. */
1053
3
            v0 = 0;
1054
3
            v1 = 3;
1055
3
            goto check_left;
1056
3
        }
1057
66
        else if (cc->corners[1].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1058
48
        {
1059
            /* Check 1+2 off left. */
1060
48
            v0 = 1;
1061
48
            v1 = 2;
1062
51
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
51
            do
1066
115
            {
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
115
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1073
115
                if (m0 >= pfs->rect.p.x)
1074
51
                    goto check_left_quarter;
1075
64
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1076
64
                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
64
                cc->corners[v0].x = m0;
1080
64
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1081
64
                cc->corners[v1].x = m1;
1082
64
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1083
64
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1084
64
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1085
64
                changed = 1;
1086
64
            }
1087
64
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1088
0
            if (0)
1089
0
            {
1090
51
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
51
                do
1094
51
                {
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
51
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1101
51
                    if (m0 >= pfs->rect.p.x)
1102
51
                        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
51
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1116
51
            }
1117
0
        }
1118
1119
        /* or the right hand half? */
1120
69
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[3].x > pfs->rect.q.x)
1121
64
        {
1122
            /* Check 0+3 off right. */
1123
64
            v0 = 0;
1124
64
            v1 = 3;
1125
64
            goto check_right;
1126
64
        }
1127
5
        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
67
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
67
            do
1136
541
            {
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
541
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1143
541
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1144
66
                    goto check_right_quarter;
1145
475
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1146
475
                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
474
                cc->corners[v0].x = m0;
1150
474
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1151
474
                cc->corners[v1].x = m1;
1152
474
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1153
474
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1154
474
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1155
474
                changed = 1;
1156
474
            }
1157
474
            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
67
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
67
                do
1164
67
                {
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
67
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1171
67
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1172
66
                        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
67
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1186
67
            }
1187
0
        }
1188
1189
        /* Now, rotated cases: Can we cull the left hand half? */
1190
69
        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
69
        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
69
        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
69
        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
69
        if ((cc->corners[0].y <= pfs->rect.p.y &&
1335
16
             cc->corners[1].y <= pfs->rect.p.y &&
1336
16
             cc->corners[2].y <= pfs->rect.p.y &&
1337
0
             cc->corners[3].y <= pfs->rect.p.y) ||
1338
69
            (cc->corners[0].y >= pfs->rect.q.y &&
1339
3
             cc->corners[1].y >= pfs->rect.q.y &&
1340
0
             cc->corners[2].y >= pfs->rect.q.y &&
1341
0
             cc->corners[3].y >= pfs->rect.q.y))
1342
0
            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
69
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[1].y < pfs->rect.p.y)
1348
16
        {
1349
            /* Check 0+1 off above. */
1350
16
            v0 = 0;
1351
16
            v1 = 1;
1352
16
            goto check_above;
1353
16
        }
1354
53
        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
16
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
16
            do
1363
16
            {
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
16
                m0 = midpoint(cc->corners[0].y, cc->corners[3].y);
1371
16
                if (m0 >= pfs->rect.p.y)
1372
16
                    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
16
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1386
0
            if (0)
1387
0
            {
1388
16
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
16
                do
1392
16
                {
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
16
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1402
16
                    if (m0 >= pfs->rect.p.y)
1403
16
                        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
16
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1417
16
            }
1418
0
        }
1419
1420
        /* or the bottom half? */
1421
69
        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
69
        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
69
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[3].y < pfs->rect.p.y)
1496
0
        {
1497
            /* Check 0+3 off above. */
1498
0
            v0 = 0;
1499
0
            v1 = 3;
1500
0
            goto check_rot_above;
1501
0
        }
1502
69
        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
3
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
3
            do
1511
3
            {
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
3
                m0 = midpoint(cc->corners[0].y, cc->corners[1].y);
1519
3
                if (m0 >= pfs->rect.p.y)
1520
2
                    goto check_rot_above_quarter;
1521
1
                m1 = midpoint(cc->corners[3].y, cc->corners[2].y);
1522
1
                if (m1 >= pfs->rect.p.y)
1523
1
                    goto check_rot_above_quarter;
1524
                /* So, we can completely discard the top half of the patch. */
1525
0
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[1].x);
1526
0
                cc->corners[v0].y = m0;
1527
0
                cc->corners[v1].x = midpoint(cc->corners[3].x, cc->corners[2].x);
1528
0
                cc->corners[v1].y = m1;
1529
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1530
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1531
0
                changed = 1;
1532
0
            }
1533
3
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1534
0
            if (0)
1535
0
            {
1536
3
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
3
                do
1540
3
                {
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
3
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1550
3
                    if (m0 >= pfs->rect.p.y)
1551
2
                        break;
1552
1
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1553
1
                    if (m1 >= pfs->rect.p.y)
1554
1
                        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
3
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1565
3
            }
1566
0
        }
1567
1568
        /* or the bottom half? */
1569
69
        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
66
        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
69
    } while (changed);
1642
1643
19
    return A_fill_region_floats(pfs, cc, 0);
1644
40
}
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
45.4k
{
1653
45.4k
    const gs_shading_A_t * const psh = pfs->psh;
1654
45.4k
    double x0 = psh->params.Coords[0] + pfs->delta.x * pfs->v0;
1655
45.4k
    double y0 = psh->params.Coords[1] + pfs->delta.y * pfs->v0;
1656
45.4k
    double x1 = psh->params.Coords[0] + pfs->delta.x * pfs->v1;
1657
45.4k
    double y1 = psh->params.Coords[1] + pfs->delta.y * pfs->v1;
1658
45.4k
    double h0 = pfs->u0, h1 = pfs->u1;
1659
45.4k
    corners_and_curves cc;
1660
45.4k
    int code;
1661
1662
45.4k
    double dx0 = pfs->delta.x * h0;
1663
45.4k
    double dy0 = pfs->delta.y * h0;
1664
45.4k
    double dx1 = pfs->delta.x * h1;
1665
45.4k
    double dy1 = pfs->delta.y * h1;
1666
1667
45.4k
    cc.curve[0].vertex.cc[0] = pfs->t0; /* The element cc[1] is set to a dummy value against */
1668
45.4k
    cc.curve[1].vertex.cc[0] = pfs->t1; /* interrupts while an idle processing in gxshade6.c .  */
1669
45.4k
    cc.curve[2].vertex.cc[0] = pfs->t1;
1670
45.4k
    cc.curve[3].vertex.cc[0] = pfs->t0;
1671
45.4k
    cc.corners[0].x = x0 + dy0;
1672
45.4k
    cc.corners[0].y = y0 - dx0;
1673
45.4k
    cc.corners[1].x = x1 + dy0;
1674
45.4k
    cc.corners[1].y = y1 - dx0;
1675
45.4k
    cc.corners[2].x = x1 + dy1;
1676
45.4k
    cc.corners[2].y = y1 - dx1;
1677
45.4k
    cc.corners[3].x = x0 + dy1;
1678
45.4k
    cc.corners[3].y = y0 - dx1;
1679
45.4k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[0].x, cc.corners[0].y, &cc.curve[0].vertex.p);
1680
45.4k
    if (code < 0)
1681
38
        goto fail;
1682
45.4k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[1].x, cc.corners[1].y, &cc.curve[1].vertex.p);
1683
45.4k
    if (code < 0)
1684
2
        goto fail;
1685
45.4k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[2].x, cc.corners[2].y, &cc.curve[2].vertex.p);
1686
45.4k
    if (code < 0)
1687
0
        goto fail;
1688
45.4k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[3].x, cc.corners[3].y, &cc.curve[3].vertex.p);
1689
45.4k
    if (code < 0)
1690
0
        goto fail;
1691
45.4k
    return subdivide_patch_fill(pfs1, cc.curve);
1692
40
fail:
1693
40
    if (code != gs_error_limitcheck)
1694
0
        return code;
1695
40
    code = gs_point_transform(cc.corners[0].x, cc.corners[0].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[0]);
1696
40
    if (code < 0)
1697
0
        return code;
1698
40
    code = gs_point_transform(cc.corners[1].x, cc.corners[1].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[1]);
1699
40
    if (code < 0)
1700
0
        return code;
1701
40
    code = gs_point_transform(cc.corners[2].x, cc.corners[2].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[2]);
1702
40
    if (code < 0)
1703
0
        return code;
1704
40
    code = gs_point_transform(cc.corners[3].x, cc.corners[3].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[3]);
1705
40
    if (code < 0)
1706
0
        return code;
1707
40
    return subdivide_patch_fill_floats(pfs1, &cc);
1708
40
}
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
27.9k
{
1715
27.9k
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
1716
27.9k
    gs_function_t * const pfn = psh->params.Function;
1717
27.9k
    gs_matrix cmat;
1718
27.9k
    gs_rect t_rect;
1719
27.9k
    A_fill_state_t state;
1720
27.9k
    patch_fill_state_t pfs1;
1721
27.9k
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
1722
27.9k
    float dd = d1 - d0;
1723
27.9k
    double t0, t1;
1724
27.9k
    gs_point dist;
1725
27.9k
    int code;
1726
1727
27.9k
    state.psh = psh;
1728
27.9k
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
1729
27.9k
    if (code < 0)
1730
43
        return code;
1731
27.9k
    pfs1.Function = pfn;
1732
27.9k
    pfs1.rect = *clip_rect;
1733
27.9k
    code = init_patch_fill_state(&pfs1);
1734
27.9k
    if (code < 0)
1735
0
        goto fail;
1736
27.9k
    pfs1.maybe_self_intersecting = false;
1737
27.9k
    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
27.9k
    cmat.tx = psh->params.Coords[0];
1744
27.9k
    cmat.ty = psh->params.Coords[1];
1745
27.9k
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
1746
27.9k
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
1747
27.9k
    cmat.yx = state.delta.x;
1748
27.9k
    cmat.yy = state.delta.y;
1749
27.9k
    cmat.xx = cmat.yy;
1750
27.9k
    cmat.xy = -cmat.yx;
1751
27.9k
    code = gs_bbox_transform_inverse(rect, &cmat, &t_rect);
1752
27.9k
    if (code < 0) {
1753
0
        code = 0; /* Swallow this silently */
1754
0
        goto fail;
1755
0
    }
1756
27.9k
    t0 = min(max(t_rect.p.y, 0), 1);
1757
27.9k
    t1 = max(min(t_rect.q.y, 1), 0);
1758
27.9k
    state.v0 = t0;
1759
27.9k
    state.v1 = t1;
1760
27.9k
    state.u0 = t_rect.p.x;
1761
27.9k
    state.u1 = t_rect.q.x;
1762
27.9k
    state.t0 = t0 * dd + d0;
1763
27.9k
    state.t1 = t1 * dd + d0;
1764
27.9k
    code = gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pgs),
1765
27.9k
                          &dist);
1766
27.9k
    if (code < 0)
1767
0
        goto fail;
1768
27.9k
    state.length = hypot(dist.x, dist.y); /* device space line length */
1769
27.9k
    code = A_fill_region(&state, &pfs1);
1770
27.9k
    if (psh->params.Extend[0] && t0 > t_rect.p.y) {
1771
8.73k
        if (code < 0)
1772
0
            goto fail;
1773
        /* Use the general algorithm, because we need the trapping. */
1774
8.73k
        state.v0 = t_rect.p.y;
1775
8.73k
        state.v1 = t0;
1776
8.73k
        state.t0 = state.t1 = t0 * dd + d0;
1777
8.73k
        code = A_fill_region(&state, &pfs1);
1778
8.73k
    }
1779
27.9k
    if (psh->params.Extend[1] && t1 < t_rect.q.y) {
1780
8.84k
        if (code < 0)
1781
0
            goto fail;
1782
        /* Use the general algorithm, because we need the trapping. */
1783
8.84k
        state.v0 = t1;
1784
8.84k
        state.v1 = t_rect.q.y;
1785
8.84k
        state.t0 = state.t1 = t1 * dd + d0;
1786
8.84k
        code = A_fill_region(&state, &pfs1);
1787
8.84k
    }
1788
27.9k
fail:
1789
27.9k
    gsicc_release_link(pfs1.icclink);
1790
27.9k
    if (term_patch_fill_state(&pfs1))
1791
0
        return_error(gs_error_unregistered); /* Must not happen. */
1792
27.9k
    return code;
1793
27.9k
}
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
27.9k
{
1800
27.9k
    return gs_shading_A_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
1801
27.9k
}
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
212
{
1860
212
    double dx = x1 - x0, dy = y1 - y0;
1861
212
    double d = hypot(dx, dy);
1862
212
    gs_point p0, p1, pc0, pc1;
1863
212
    int k, j, code, dirn;
1864
212
    bool inside = 0;
1865
1866
    /* pc0 and pc1 are the centres of the respective circles. */
1867
212
    pc0.x = x0, pc0.y = y0;
1868
212
    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
212
    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
212
        p0.x = 0, p0.y = -1, dirn = 0;
1876
        /* Align stripes along radii for faster triangulation : */
1877
212
        inside = 1;
1878
212
        pfs->function_arg_shift = 1;
1879
212
    } 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.05k
    for (k = 0; k < 4; k++) {
1898
843
        gs_point p[12];
1899
843
        patch_curve_t curve[4];
1900
1901
        /* Set p1 to be 90 degrees anticlockwise from p0 */
1902
843
        p1.x = -p0.y; p1.y = p0.x;
1903
843
        if (dirn == 0) { /* Clockwise */
1904
631
            make_quadrant_arc(p + 0, &pc0, &p1, &p0, r0);
1905
631
            make_quadrant_arc(p + 6, &pc1, &p0, &p1, r1);
1906
631
        } else { /* Anticlockwise */
1907
212
            make_quadrant_arc(p + 0, &pc0, &p0, &p1, r0);
1908
212
            make_quadrant_arc(p + 6, &pc1, &p1, &p0, r1);
1909
212
        }
1910
843
        p[4].x = (p[3].x * 2 + p[6].x) / 3;
1911
843
        p[4].y = (p[3].y * 2 + p[6].y) / 3;
1912
843
        p[5].x = (p[3].x + p[6].x * 2) / 3;
1913
843
        p[5].y = (p[3].y + p[6].y * 2) / 3;
1914
843
        p[10].x = (p[9].x * 2 + p[0].x) / 3;
1915
843
        p[10].y = (p[9].y * 2 + p[0].y) / 3;
1916
843
        p[11].x = (p[9].x + p[0].x * 2) / 3;
1917
843
        p[11].y = (p[9].y + p[0].y * 2) / 3;
1918
4.21k
        for (j = 0; j < 4; j++) {
1919
3.37k
            int jj = (j + inside) % 4;
1920
1921
3.37k
            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
3.37k
            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
3.37k
            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
3.37k
            curve[j].straight = (((j + inside) & 1) != 0);
1930
3.37k
        }
1931
843
        curve[(0 + inside) % 4].vertex.cc[0] = t0;
1932
843
        curve[(1 + inside) % 4].vertex.cc[0] = t0;
1933
843
        curve[(2 + inside) % 4].vertex.cc[0] = t1;
1934
843
        curve[(3 + inside) % 4].vertex.cc[0] = t1;
1935
843
        curve[0].vertex.cc[1] = curve[1].vertex.cc[1] = 0; /* Initialize against FPE. */
1936
843
        curve[2].vertex.cc[1] = curve[3].vertex.cc[1] = 0; /* Initialize against FPE. */
1937
843
        code = patch_fill(pfs, curve, NULL, NULL);
1938
843
        if (code < 0)
1939
5
            return code;
1940
        /* Move p0 to be ready for the next position */
1941
838
        if (k == 0) {
1942
            /* p0 moves clockwise */
1943
212
            p1 = p0;
1944
212
            p0.x = p1.y; p0.y = -p1.x;
1945
212
            dirn = 0;
1946
626
        } else if (k == 1) {
1947
            /* p0 flips sides */
1948
212
            p0.x = -p0.x; p0.y = -p0.y;
1949
212
            dirn = 1;
1950
414
        } else if (k == 2) {
1951
            /* p0 moves anti-clockwise */
1952
207
            p1 = p0;
1953
207
            p0.x = -p1.y; p0.y = p1.x;
1954
207
            dirn = 0;
1955
207
        }
1956
838
    }
1957
207
    return 0;
1958
212
}
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
92
{
2931
92
    double d, dd;
2932
2933
92
    dd = hypot(rect->p.x - x0, rect->p.y - y0);
2934
92
    d = hypot(rect->p.x - x0, rect->q.y - y0);
2935
92
    dd = max(dd, d);
2936
92
    d = hypot(rect->q.x - x0, rect->q.y - y0);
2937
92
    dd = max(dd, d);
2938
92
    d = hypot(rect->q.x - x0, rect->p.y - y0);
2939
92
    dd = max(dd, d);
2940
92
    return dd;
2941
92
}
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
179
{
3092
179
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3093
179
    double r0 = psh->params.Coords[2];
3094
179
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3095
179
    double r1 = psh->params.Coords[5];
3096
179
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
3097
179
    double d = hypot(dx, dy), r;
3098
179
    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
179
    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
179
    if (dr > d - 1e-4 * (d + dr)) {
3157
        /* Nested circles, or degenerate. */
3158
179
        if (r0 > r1) {
3159
0
            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
0
            if (Extend1 && r1 > 0)
3168
0
                return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3169
179
        } else {
3170
179
            if (Extend1) {
3171
92
                r = R_rect_radius(rect, x1, y1);
3172
92
                if (r > r1) {
3173
92
                    code = R_tensor_annulus(pfs, x1, y1, r, t1, x1, y1, r1, t1);
3174
92
                    if (code < 0)
3175
0
                        return code;
3176
92
                }
3177
92
            }
3178
179
            if (Extend0 && r0 > 0)
3179
0
                return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3180
179
        }
3181
179
    } 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
179
    return 0;
3229
179
}
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
3.80k
#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
562
{
3298
562
    double cx = rsa->x0 + (rsa->x1 - rsa->x0) * t;
3299
562
    double cy = rsa->y0 + (rsa->y1 - rsa->y0) * t;
3300
562
    double rx = rsa->p[point_index].x - cx;
3301
562
    double ry = rsa->p[point_index].y - cy;
3302
562
    double dx = rsa->p[point_index - 1].x - rsa->p[point_index].x;
3303
562
    double dy = rsa->p[point_index - 1].y - rsa->p[point_index].y;
3304
3305
562
    if (at_corner) {
3306
544
        double Dx = rsa->p[point_index + 1].x - rsa->p[point_index].x;
3307
544
        double Dy = rsa->p[point_index + 1].y - rsa->p[point_index].y;
3308
544
        bool b1 = (dx * rx + dy * ry >= 0);
3309
544
        bool b2 = (Dx * rx + Dy * ry >= 0);
3310
3311
544
        if (b1 & b2)
3312
144
            rsa->outer_contact[root_index] = true;
3313
544
    } else {
3314
18
        if (rx * dy - ry * dx < 0)
3315
6
            rsa->outer_contact[root_index] = true;
3316
18
    }
3317
562
}
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.08k
{
3322
1.08k
    int i;
3323
3324
3.26k
    for (i = 0; i < 2; i++) {
3325
2.17k
        bool good_root;
3326
3327
2.17k
        if (!have_root[i])
3328
1.07k
            continue;
3329
1.10k
        good_root = (!rsa->have_apex || (rsa->apex <= 0 || r0 == 0 ? t[i] >= rsa->apex : t[i] <= rsa->apex));
3330
1.10k
        if (good_root) {
3331
562
            radial_shading_external_contact(rsa, point_index, t[i], r0, r1, at_corner, i);
3332
562
            if (!rsa->have_root[i]) {
3333
34
                rsa->span[i][0] = rsa->span[i][1] = t[i];
3334
34
                rsa->have_root[i] = true;
3335
528
            } else {
3336
528
                if (rsa->span[i][0] > t[i])
3337
31
                    rsa->span[i][0] = t[i];
3338
528
                if (rsa->span[i][1] < t[i])
3339
77
                    rsa->span[i][1] = t[i];
3340
528
            }
3341
562
        }
3342
1.10k
    }
3343
1.08k
}
3344
3345
static void
3346
compute_radial_shading_span_extended_side(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3347
544
{
3348
544
    double cc, c;
3349
544
    bool have_root[2] = {false, false};
3350
544
    double t[2];
3351
544
    bool by_x = (rsa->p[point_index].x != rsa->p[point_index + 1].x);
3352
544
    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
544
    if (by_x) {
3365
248
        c = rsa->p[point_index].x - rsa->x0;
3366
248
        cc = rsa->x1 - rsa->x0;
3367
296
    } else {
3368
296
        c = rsa->p[point_index].y - rsa->y0;
3369
296
        cc = rsa->y1 - rsa->y0;
3370
296
    }
3371
544
    t[0] = (c - r0) / (cc + r1 - r0);
3372
544
    t[1] = (c + r0) / (cc - r1 + r0);
3373
544
    if (t[0] > t[1]) {
3374
432
        c    = t[0];
3375
432
        t[0] = t[1];
3376
432
        t[1] = c;
3377
432
    }
3378
1.63k
    for (i = 0; i < 2; i++) {
3379
1.08k
        double d, d0, d1;
3380
3381
1.08k
        if (by_x) {
3382
496
            d = rsa->y1 - rsa->y0 + r0 + (r1 - r0) * t[i];
3383
496
            d0 = rsa->p[point_index].y;
3384
496
            d1 = rsa->p[point_index + 1].y;
3385
592
        } else {
3386
592
            d = rsa->x1 - rsa->x0 + r0 + (r1 - r0) * t[i];
3387
592
            d0 = rsa->p[point_index].x;
3388
592
            d1 = rsa->p[point_index + 1].x;
3389
592
        }
3390
1.08k
        if (d1 > d0 ? d0 <= d && d <= d1 : d1 <= d && d <= d0)
3391
18
            have_root[i] = true;
3392
1.08k
    }
3393
544
    store_roots(rsa, have_root, t, r0, r1, point_index, false);
3394
544
}
3395
3396
static int
3397
compute_radial_shading_span_extended_point(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3398
544
{
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
544
    double p1x = rsa->x1 - rsa->x0;
3423
544
    double p1y = rsa->y1 - rsa->y0;
3424
544
    double qx  = rsa->p[point_index].x - rsa->x0;
3425
544
    double qy  = rsa->p[point_index].y - rsa->y0;
3426
544
    double a   = (Pw2(p1x) + Pw2(p1y) - Pw2(r0 - r1));
3427
544
    bool have_root[2] = {false, false};
3428
544
    double t[2];
3429
3430
544
    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
544
    } 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
544
        double minushalfb = r0*(r1-r0) + p1x*qx + p1y*qy;
3442
544
        double c          = Pw2(qx) + Pw2(qy) - Pw2(r0);
3443
544
        double desc2      = Pw2(minushalfb) - a*c; /* desc2 = 1/4 (b^2-4ac) */
3444
3445
544
        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
544
        } else {
3449
544
            double desc1 = sqrt(desc2); /* desc1 = 1/2 SQRT(b^2-4ac) */
3450
3451
544
            if (a > 0) {
3452
0
                t[0] = (minushalfb - desc1) / a;
3453
0
                t[1] = (minushalfb + desc1) / a;
3454
544
            } else {
3455
544
                t[0] = (minushalfb + desc1) / a;
3456
544
                t[1] = (minushalfb - desc1) / a;
3457
544
            }
3458
544
            have_root[0] = have_root[1] = true;
3459
544
        }
3460
544
    }
3461
544
    store_roots(rsa, have_root, t, r0, r1, point_index, true);
3462
544
    if (have_root[0] && have_root[1])
3463
544
        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
136
{
3476
136
    int span_type0, span_type1;
3477
3478
136
    span_type0 = compute_radial_shading_span_extended_point(rsa, r0, r1, 1);
3479
136
    if (span_type0 == -1)
3480
0
        return -1;
3481
136
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 2);
3482
136
    if (span_type0 != span_type1)
3483
0
        return -1;
3484
136
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 3);
3485
136
    if (span_type0 != span_type1)
3486
0
        return -1;
3487
136
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 4);
3488
136
    if (span_type0 != span_type1)
3489
0
        return -1;
3490
136
    compute_radial_shading_span_extended_side(rsa, r0, r1, 1);
3491
136
    compute_radial_shading_span_extended_side(rsa, r0, r1, 2);
3492
136
    compute_radial_shading_span_extended_side(rsa, r0, r1, 3);
3493
136
    compute_radial_shading_span_extended_side(rsa, r0, r1, 4);
3494
136
    return span_type0;
3495
136
}
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
34
{
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
34
    const double extent = 1.02;
3558
34
    int span_type0, span_type1, span_type;
3559
3560
34
    memset(rsa, 0, sizeof(*rsa));
3561
34
    rsa->x0 = x0;
3562
34
    rsa->y0 = y0;
3563
34
    rsa->x1 = x1;
3564
34
    rsa->y1 = y1;
3565
34
    rsa->p[0] = rsa->p[4] = rect->p;
3566
34
    rsa->p[1].x = rsa->p[5].x = rect->p.x;
3567
34
    rsa->p[1].y = rsa->p[5].y = rect->q.y;
3568
34
    rsa->p[2] = rect->q;
3569
34
    rsa->p[3].x = rect->q.x;
3570
34
    rsa->p[3].y = rect->p.y;
3571
34
    rsa->have_apex = any_abs(r1 - r0) > 1e-7 * any_abs(r1 + r0);
3572
34
    rsa->apex = (rsa->have_apex ? -r0 / (r1 - r0) : 0);
3573
34
    span_type0 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 * extent);
3574
34
    if (span_type0 == -1)
3575
0
        return -1;
3576
34
    span_type1 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 / extent);
3577
34
    if (span_type0 != span_type1)
3578
0
        return -1;
3579
34
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 * extent);
3580
34
    if (span_type0 != span_type1)
3581
0
        return -1;
3582
34
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 / extent);
3583
34
    if (span_type1 == -1)
3584
0
        return -1;
3585
34
    if (r0 < r1) {
3586
34
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3587
0
            rsa->span[0][0] = rsa->apex; /* Likely never happens. Remove ? */
3588
34
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3589
7
            rsa->span[1][0] = rsa->apex;
3590
34
    } else if (r0 > r1) {
3591
0
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3592
0
            rsa->span[0][1] = rsa->apex;
3593
0
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3594
0
            rsa->span[1][1] = rsa->apex; /* Likely never happens. Remove ? */
3595
0
    }
3596
34
    span_type = 0;
3597
34
    if (rsa->have_root[0] && rsa->span[0][0] < 0)
3598
0
        span_type |= 1;
3599
34
    if (rsa->have_root[1] && rsa->span[1][0] < 0)
3600
0
        span_type |= 1;
3601
34
    if (rsa->have_root[0] && rsa->span[0][1] > 0 && rsa->span[0][0] < 1)
3602
0
        span_type |= 2;
3603
34
    if (rsa->have_root[1] && rsa->span[1][1] > 0 && rsa->span[1][0] < 1)
3604
33
        span_type |= 4;
3605
34
    if (rsa->have_root[0] && rsa->span[0][1] > 1)
3606
0
        span_type |= 8;
3607
34
    if (rsa->have_root[1] && rsa->span[1][1] > 1)
3608
6
        span_type |= 8;
3609
34
    return span_type;
3610
34
}
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
33
{
3615
33
    double s0 = span_[0], s1 = span_[1], w;
3616
3617
33
    if (s0 < 0)
3618
0
        s0 = 0;
3619
33
    if (s1 < 0)
3620
0
        s1 = 0;
3621
33
    if (s0 > 1)
3622
0
        s0 = 1;
3623
33
    if (s1 > 1)
3624
5
        s1 = 1;
3625
33
    w = s1 - s0;
3626
33
    if (w == 0)
3627
0
        return false; /* Don't pass a degenerate shading. */
3628
33
    if (w > 0.3)
3629
26
        return false; /* The span is big, don't shorten it. */
3630
7
    { /* Do shorten. */
3631
7
        double R0 = *r0, X0 = *x0, Y0 = *y0, D0 = *d0;
3632
7
        double R1 = *r1, X1 = *x1, Y1 = *y1, D1 = *d1;
3633
3634
7
        *r0 = R0 + (R1 - R0) * s0;
3635
7
        *x0 = X0 + (X1 - X0) * s0;
3636
7
        *y0 = Y0 + (Y1 - Y0) * s0;
3637
7
        *d0 = D0 + (D1 - D0) * s0;
3638
7
        *r1 = R0 + (R1 - R0) * s1;
3639
7
        *x1 = X0 + (X1 - X0) * s1;
3640
7
        *y1 = Y0 + (Y1 - Y0) * s1;
3641
7
        *d1 = D0 + (D1 - D0) * s1;
3642
7
    }
3643
7
    return true;
3644
33
}
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
121
{
3649
121
    const double d = hypot(x1 - x0, y1 - y0);
3650
121
    const double area0 = M_PI * r0 * r0 / 2;
3651
121
    const double area1 = M_PI * r1 * r1 / 2;
3652
121
    const double area2 = (r0 + r1) / 2 * d;
3653
121
    const double arbitrary = 8;
3654
121
    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
121
    areaX = (rect->q.x - rect->p.x) * (rect->q.x - rect->p.x);
3662
121
    if (areaX * arbitrary < area0 + area1 + area2)
3663
28
        return true;
3664
93
    areaY = (rect->q.y - rect->p.y) * (rect->q.y - rect->p.y);
3665
93
    if (areaY * arbitrary < area0 + area1 + area2)
3666
6
        return true;
3667
87
    return false;
3668
93
}
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
123
{
3675
123
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
3676
123
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
3677
123
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3678
123
    double r0 = psh->params.Coords[2];
3679
123
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3680
123
    double r1 = psh->params.Coords[5];
3681
123
    radial_shading_attrs_t rsa;
3682
123
    int span_type; /* <0 - don't shorten, 1 - extent0, 2 - first contact, 4 - last contact, 8 - extent1. */
3683
123
    int code;
3684
123
    patch_fill_state_t pfs1;
3685
3686
123
    if (r0 == 0 && r1 == 0)
3687
2
        return 0; /* PLRM requires to paint nothing. */
3688
121
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
3689
121
    if (code < 0)
3690
0
        return code;
3691
121
    pfs1.Function = psh->params.Function;
3692
121
    code = init_patch_fill_state(&pfs1);
3693
121
    if (code < 0) {
3694
0
        if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3695
0
        return code;
3696
0
    }
3697
121
    pfs1.function_arg_shift = 0;
3698
121
    pfs1.rect = *clip_rect;
3699
121
    pfs1.maybe_self_intersecting = false;
3700
121
    if (is_radial_shading_large(x0, y0, r0, x1, y1, r1, rect))
3701
34
        span_type = compute_radial_shading_span(&rsa, x0, y0, r0, x1, y1, r1, rect);
3702
87
    else
3703
87
        span_type = -1;
3704
121
    if (span_type < 0) {
3705
87
        code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3706
87
        if (code >= 0)
3707
87
            code = R_tensor_annulus(&pfs1, x0, y0, r0, d0, x1, y1, r1, d1);
3708
87
        if (code >= 0)
3709
87
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3710
87
    } else if (span_type == 1) {
3711
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d0);
3712
34
    } else if (span_type == 8) {
3713
1
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d1);
3714
33
    } else {
3715
33
        bool second_interval = true;
3716
3717
33
        code = 0;
3718
33
        if (span_type & 1)
3719
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3720
33
        if ((code >= 0) && (span_type & 2)) {
3721
0
            float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3722
0
            double R0 = r0, R1 = r1;
3723
3724
0
            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
0
            } else {
3732
0
                second_interval = shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[0]);
3733
0
            }
3734
0
            code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3735
0
        }
3736
33
        if (code >= 0 && second_interval) {
3737
33
            if (span_type & 4) {
3738
33
                float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3739
33
                double R0 = r0, R1 = r1;
3740
3741
33
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[1]);
3742
33
                code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3743
33
            }
3744
33
        }
3745
33
        if (code >= 0 && (span_type & 8))
3746
5
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3747
33
    }
3748
121
    if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3749
121
    if (term_patch_fill_state(&pfs1))
3750
0
        return_error(gs_error_unregistered); /* Must not happen. */
3751
121
    return code;
3752
121
}
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
123
{
3759
123
    return gs_shading_R_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
3760
123
}