Coverage Report

Created: 2026-08-08 08:00

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
785
        p[1].y < rect->p.y &&
216
785
        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
162
        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.73k
             c[2].vertex.p.x <= pfs->rect.p.x &&
274
1.20k
             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
14
        {
293
            /* Check 1+2 off left. */
294
14
            v0 = 1;
295
14
            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
38
        {
356
            /* Check 0+3 off right. */
357
38
            v0 = 0;
358
38
            v1 = 3;
359
38
            goto check_right;
360
38
        }
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
90
                    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
90
                        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.64k
        {
426
            /* Check 0+1 off left. */
427
3.64k
            v0 = 0;
428
3.64k
            v1 = 1;
429
3.64k
            goto check_rot_left;
430
3.64k
        }
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.70k
                    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.93k
                        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.43k
        {
503
            /* Check 3+2 off right. */
504
2.43k
            v0 = 3;
505
2.43k
            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
114
                    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
11.8k
             c[1].vertex.p.y <= pfs->rect.p.y &&
570
5.77k
             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
45.9k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[1].vertex.p.y < pfs->rect.p.y)
582
1.79k
        {
583
            /* Check 0+1 off above. */
584
1.79k
            v0 = 0;
585
1.79k
            v1 = 1;
586
1.79k
            goto check_above;
587
1.79k
        }
588
44.1k
        else if (c[3].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
589
1.21k
        {
590
            /* Check 3+2 off above. */
591
1.21k
            v0 = 3;
592
1.21k
            v1 = 2;
593
3.01k
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.01k
            do
597
3.42k
            {
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.42k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
605
3.42k
                if (m0 >= pfs->rect.p.y)
606
888
                    goto check_above_quarter;
607
2.53k
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
608
2.53k
                if (m1 >= pfs->rect.p.y)
609
2.12k
                    goto check_above_quarter;
610
                /* So, we can completely discard the top half of the patch. */
611
412
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
612
412
                c[v0].vertex.p.y = m0;
613
412
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
614
412
                c[v1].vertex.p.y = m1;
615
412
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
616
412
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
617
412
                changed = 1;
618
412
            }
619
3.01k
            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.01k
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.01k
                do
626
3.58k
                {
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.58k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
636
3.58k
                    if (m0 >= pfs->rect.p.y)
637
681
                        break;
638
2.90k
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
639
2.90k
                    if (m1 >= pfs->rect.p.y)
640
2.33k
                        break;
641
                    /* So, we can completely discard the top half of the patch. */
642
575
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
643
575
                    c[v0].vertex.p.y = m0;
644
575
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
645
575
                    c[v1].vertex.p.y = m1;
646
575
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
647
575
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
648
575
                    changed = 1;
649
575
                }
650
3.01k
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
651
3.01k
            }
652
0
        }
653
654
        /* or the bottom half? */
655
45.9k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[1].vertex.p.y > pfs->rect.q.y)
656
1.09k
        {
657
            /* Check 0+1 off bottom. */
658
1.09k
            v0 = 0;
659
1.09k
            v1 = 1;
660
1.09k
            goto check_bottom;
661
1.09k
        }
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.85k
        {
664
            /* Check 3+2 off bottom. */
665
1.85k
            v0 = 3;
666
1.85k
            v1 = 2;
667
2.95k
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
2.95k
            do
671
3.42k
            {
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.42k
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
679
3.42k
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
680
2.68k
                    goto check_bottom_quarter;
681
737
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
682
737
                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
470
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
686
470
                c[v0].vertex.p.y = m0;
687
470
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
688
470
                c[v1].vertex.p.y = m1;
689
470
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
690
470
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
691
470
                changed = 1;
692
470
            }
693
2.95k
            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
2.95k
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
2.95k
                do
700
3.61k
                {
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.61k
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
710
3.61k
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
711
2.62k
                        break;
712
985
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
713
985
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
714
326
                        break;
715
                    /* So, we can completely discard the bottom half of the patch. */
716
659
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
717
659
                    c[v0].vertex.p.y = m0;
718
659
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
719
659
                    c[v1].vertex.p.y = m1;
720
659
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
721
659
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
722
659
                    changed = 1;
723
659
                }
724
2.95k
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
725
2.95k
            }
726
0
        }
727
728
        /* Now, rotated cases: Can we cull the top half? */
729
45.9k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[3].vertex.p.y < pfs->rect.p.y)
730
388
        {
731
            /* Check 0+3 off above. */
732
388
            v0 = 0;
733
388
            v1 = 3;
734
388
            goto check_rot_above;
735
388
        }
736
45.5k
        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
449
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
449
            do
745
470
            {
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
470
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
753
470
                if (m0 >= pfs->rect.p.y)
754
443
                    goto check_rot_above_quarter;
755
27
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
756
27
                if (m1 >= pfs->rect.p.y)
757
6
                    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
449
            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
449
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
449
                do
774
451
                {
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
451
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
784
451
                    if (m0 >= pfs->rect.p.y)
785
443
                        break;
786
8
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
787
8
                    if (m1 >= pfs->rect.p.y)
788
6
                        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
449
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
799
449
            }
800
0
        }
801
802
        /* or the bottom half? */
803
45.9k
        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
45.9k
        else if (c[1].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
811
13
        {
812
            /* Check 1+2 off the bottom. */
813
13
            v0 = 1;
814
13
            v1 = 2;
815
14
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
14
            do
819
35
            {
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
35
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
827
35
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
828
14
                    goto check_rot_bottom_quarter;
829
21
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
830
21
                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
21
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
834
21
                c[v0].vertex.p.y = m0;
835
21
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
836
21
                c[v1].vertex.p.y = m1;
837
21
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
838
21
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
839
21
                changed = 1;
840
21
            }
841
21
            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
14
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
14
                do
848
24
                {
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
24
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
858
24
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
859
14
                        break;
860
10
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
861
10
                    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
10
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
865
10
                    c[v0].vertex.p.y = m0;
866
10
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
867
10
                    c[v1].vertex.p.y = m1;
868
10
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
869
10
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
870
10
                    changed = 1;
871
10
                }
872
14
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
873
14
            }
874
0
        }
875
45.9k
    } 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
1.77k
#define midpoint(a,b)      ((a+b)/2)
995
996
112
#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
36
{
1001
36
    double m0, m1;
1002
36
    int v0, v1;
1003
36
    int changed;
1004
1005
36
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
1006
2
        return 0;
1007
1008
    /* On entry we have a patch:
1009
     *   c[0].vertex  c[1].vertex
1010
     *
1011
     *   c[3].vertex  c[2].vertex
1012
     *
1013
     * Only the corners are set. The control points are not!
1014
     *
1015
     * BUT... in terms of spacial coords, it might be different...
1016
     * They might be flipped on X, Y or both, giving:
1017
     *  01 or 10 or 32 or 23
1018
     *  32    23    01    10
1019
     * or they might be rotated, and then flipped on X, Y or both, giving:
1020
     *  03 or 30 or 12 or 21
1021
     *  12    21    03    30
1022
     */
1023
1024
    /* The +MIDPOINT_ACCURACY in the tests below is to allow for us finding the midpoint of [a] = z+1 and [b] = z, and getting z+1,
1025
     * and updating [a] to be z+1, hence never actually shrinking the gap. Just accept not culling the patch as
1026
     * much as we might. See bug 706378 for an example. */
1027
1.89k
#define MIDPOINT_ACCURACY 0.0001
1028
56
#define QUARTERPOINT_ACCURACY 0.0003
1029
1030
72
    do {
1031
72
        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
72
        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
68
            (cc->corners[0].x >= pfs->rect.q.x &&
1042
63
             cc->corners[1].x >= pfs->rect.q.x &&
1043
14
             cc->corners[2].x >= pfs->rect.q.x &&
1044
14
             cc->corners[3].x >= pfs->rect.q.x))
1045
18
                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
54
        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
51
        else if (cc->corners[1].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1058
37
        {
1059
            /* Check 1+2 off left. */
1060
37
            v0 = 1;
1061
37
            v1 = 2;
1062
40
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
40
            do
1066
88
            {
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
88
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1073
88
                if (m0 >= pfs->rect.p.x)
1074
40
                    goto check_left_quarter;
1075
48
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1076
48
                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
48
                cc->corners[v0].x = m0;
1080
48
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1081
48
                cc->corners[v1].x = m1;
1082
48
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1083
48
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1084
48
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1085
48
                changed = 1;
1086
48
            }
1087
48
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1088
0
            if (0)
1089
0
            {
1090
40
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
40
                do
1094
40
                {
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
40
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1101
40
                    if (m0 >= pfs->rect.p.x)
1102
40
                        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
40
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1116
40
            }
1117
0
        }
1118
1119
        /* or the right hand half? */
1120
54
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[3].x > pfs->rect.q.x)
1121
49
        {
1122
            /* Check 0+3 off right. */
1123
49
            v0 = 0;
1124
49
            v1 = 3;
1125
49
            goto check_right;
1126
49
        }
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
52
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
52
            do
1136
414
            {
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
414
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1143
414
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1144
51
                    goto check_right_quarter;
1145
363
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1146
363
                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
362
                cc->corners[v0].x = m0;
1150
362
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1151
362
                cc->corners[v1].x = m1;
1152
362
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1153
362
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1154
362
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1155
362
                changed = 1;
1156
362
            }
1157
362
            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
52
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
52
                do
1164
52
                {
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
52
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1171
52
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1172
51
                        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
52
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1186
52
            }
1187
0
        }
1188
1189
        /* Now, rotated cases: Can we cull the left hand half? */
1190
54
        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
54
        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
54
        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
54
        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
54
        if ((cc->corners[0].y <= pfs->rect.p.y &&
1335
13
             cc->corners[1].y <= pfs->rect.p.y &&
1336
13
             cc->corners[2].y <= pfs->rect.p.y &&
1337
1
             cc->corners[3].y <= pfs->rect.p.y) ||
1338
53
            (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
1
            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
53
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[1].y < pfs->rect.p.y)
1348
12
        {
1349
            /* Check 0+1 off above. */
1350
12
            v0 = 0;
1351
12
            v1 = 1;
1352
12
            goto check_above;
1353
12
        }
1354
41
        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
12
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
12
            do
1363
12
            {
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
12
                m0 = midpoint(cc->corners[0].y, cc->corners[3].y);
1371
12
                if (m0 >= pfs->rect.p.y)
1372
12
                    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
12
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1386
0
            if (0)
1387
0
            {
1388
12
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
12
                do
1392
12
                {
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
12
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1402
12
                    if (m0 >= pfs->rect.p.y)
1403
12
                        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
12
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1417
12
            }
1418
0
        }
1419
1420
        /* or the bottom half? */
1421
53
        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
53
        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
53
        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
53
        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
53
        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
50
        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
53
    } while (changed);
1642
1643
15
    return A_fill_region_floats(pfs, cc, 0);
1644
34
}
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.3k
{
1653
45.3k
    const gs_shading_A_t * const psh = pfs->psh;
1654
45.3k
    double x0 = psh->params.Coords[0] + pfs->delta.x * pfs->v0;
1655
45.3k
    double y0 = psh->params.Coords[1] + pfs->delta.y * pfs->v0;
1656
45.3k
    double x1 = psh->params.Coords[0] + pfs->delta.x * pfs->v1;
1657
45.3k
    double y1 = psh->params.Coords[1] + pfs->delta.y * pfs->v1;
1658
45.3k
    double h0 = pfs->u0, h1 = pfs->u1;
1659
45.3k
    corners_and_curves cc;
1660
45.3k
    int code;
1661
1662
45.3k
    double dx0 = pfs->delta.x * h0;
1663
45.3k
    double dy0 = pfs->delta.y * h0;
1664
45.3k
    double dx1 = pfs->delta.x * h1;
1665
45.3k
    double dy1 = pfs->delta.y * h1;
1666
1667
45.3k
    cc.curve[0].vertex.cc[0] = pfs->t0; /* The element cc[1] is set to a dummy value against */
1668
45.3k
    cc.curve[1].vertex.cc[0] = pfs->t1; /* interrupts while an idle processing in gxshade6.c .  */
1669
45.3k
    cc.curve[2].vertex.cc[0] = pfs->t1;
1670
45.3k
    cc.curve[3].vertex.cc[0] = pfs->t0;
1671
45.3k
    cc.corners[0].x = x0 + dy0;
1672
45.3k
    cc.corners[0].y = y0 - dx0;
1673
45.3k
    cc.corners[1].x = x1 + dy0;
1674
45.3k
    cc.corners[1].y = y1 - dx0;
1675
45.3k
    cc.corners[2].x = x1 + dy1;
1676
45.3k
    cc.corners[2].y = y1 - dx1;
1677
45.3k
    cc.corners[3].x = x0 + dy1;
1678
45.3k
    cc.corners[3].y = y0 - dx1;
1679
45.3k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[0].x, cc.corners[0].y, &cc.curve[0].vertex.p);
1680
45.3k
    if (code < 0)
1681
34
        goto fail;
1682
45.3k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[1].x, cc.corners[1].y, &cc.curve[1].vertex.p);
1683
45.3k
    if (code < 0)
1684
2
        goto fail;
1685
45.3k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[2].x, cc.corners[2].y, &cc.curve[2].vertex.p);
1686
45.3k
    if (code < 0)
1687
0
        goto fail;
1688
45.3k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[3].x, cc.corners[3].y, &cc.curve[3].vertex.p);
1689
45.3k
    if (code < 0)
1690
0
        goto fail;
1691
45.3k
    return subdivide_patch_fill(pfs1, cc.curve);
1692
36
fail:
1693
36
    if (code != gs_error_limitcheck)
1694
0
        return code;
1695
36
    code = gs_point_transform(cc.corners[0].x, cc.corners[0].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[0]);
1696
36
    if (code < 0)
1697
0
        return code;
1698
36
    code = gs_point_transform(cc.corners[1].x, cc.corners[1].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[1]);
1699
36
    if (code < 0)
1700
0
        return code;
1701
36
    code = gs_point_transform(cc.corners[2].x, cc.corners[2].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[2]);
1702
36
    if (code < 0)
1703
0
        return code;
1704
36
    code = gs_point_transform(cc.corners[3].x, cc.corners[3].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[3]);
1705
36
    if (code < 0)
1706
0
        return code;
1707
36
    return subdivide_patch_fill_floats(pfs1, &cc);
1708
36
}
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.6k
{
1715
27.6k
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
1716
27.6k
    gs_function_t * const pfn = psh->params.Function;
1717
27.6k
    gs_matrix cmat;
1718
27.6k
    gs_rect t_rect;
1719
27.6k
    A_fill_state_t state;
1720
27.6k
    patch_fill_state_t pfs1;
1721
27.6k
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
1722
27.6k
    float dd = d1 - d0;
1723
27.6k
    double t0, t1;
1724
27.6k
    gs_point dist;
1725
27.6k
    int code;
1726
1727
27.6k
    state.psh = psh;
1728
27.6k
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
1729
27.6k
    if (code < 0)
1730
45
        return code;
1731
27.5k
    pfs1.Function = pfn;
1732
27.5k
    pfs1.rect = *clip_rect;
1733
27.5k
    code = init_patch_fill_state(&pfs1);
1734
27.5k
    if (code < 0)
1735
0
        goto fail;
1736
27.5k
    pfs1.maybe_self_intersecting = false;
1737
27.5k
    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.5k
    cmat.tx = psh->params.Coords[0];
1744
27.5k
    cmat.ty = psh->params.Coords[1];
1745
27.5k
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
1746
27.5k
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
1747
27.5k
    cmat.yx = state.delta.x;
1748
27.5k
    cmat.yy = state.delta.y;
1749
27.5k
    cmat.xx = cmat.yy;
1750
27.5k
    cmat.xy = -cmat.yx;
1751
27.5k
    code = gs_bbox_transform_inverse(rect, &cmat, &t_rect);
1752
27.5k
    if (code < 0) {
1753
0
        code = 0; /* Swallow this silently */
1754
0
        goto fail;
1755
0
    }
1756
27.5k
    t0 = min(max(t_rect.p.y, 0), 1);
1757
27.5k
    t1 = max(min(t_rect.q.y, 1), 0);
1758
27.5k
    state.v0 = t0;
1759
27.5k
    state.v1 = t1;
1760
27.5k
    state.u0 = t_rect.p.x;
1761
27.5k
    state.u1 = t_rect.q.x;
1762
27.5k
    state.t0 = t0 * dd + d0;
1763
27.5k
    state.t1 = t1 * dd + d0;
1764
27.5k
    code = gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pgs),
1765
27.5k
                          &dist);
1766
27.5k
    if (code < 0)
1767
0
        goto fail;
1768
27.5k
    state.length = hypot(dist.x, dist.y); /* device space line length */
1769
27.5k
    code = A_fill_region(&state, &pfs1);
1770
27.5k
    if (psh->params.Extend[0] && t0 > t_rect.p.y) {
1771
8.85k
        if (code < 0)
1772
1
            goto fail;
1773
        /* Use the general algorithm, because we need the trapping. */
1774
8.84k
        state.v0 = t_rect.p.y;
1775
8.84k
        state.v1 = t0;
1776
8.84k
        state.t0 = state.t1 = t0 * dd + d0;
1777
8.84k
        code = A_fill_region(&state, &pfs1);
1778
8.84k
    }
1779
27.5k
    if (psh->params.Extend[1] && t1 < t_rect.q.y) {
1780
8.93k
        if (code < 0)
1781
0
            goto fail;
1782
        /* Use the general algorithm, because we need the trapping. */
1783
8.93k
        state.v0 = t1;
1784
8.93k
        state.v1 = t_rect.q.y;
1785
8.93k
        state.t0 = state.t1 = t1 * dd + d0;
1786
8.93k
        code = A_fill_region(&state, &pfs1);
1787
8.93k
    }
1788
27.5k
fail:
1789
27.5k
    gsicc_release_link(pfs1.icclink);
1790
27.5k
    if (term_patch_fill_state(&pfs1))
1791
0
        return_error(gs_error_unregistered); /* Must not happen. */
1792
27.5k
    return code;
1793
27.5k
}
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.6k
{
1800
27.6k
    return gs_shading_A_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
1801
27.6k
}
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
226
{
1860
226
    double dx = x1 - x0, dy = y1 - y0;
1861
226
    double d = hypot(dx, dy);
1862
226
    gs_point p0, p1, pc0, pc1;
1863
226
    int k, j, code, dirn;
1864
226
    bool inside = 0;
1865
1866
    /* pc0 and pc1 are the centres of the respective circles. */
1867
226
    pc0.x = x0, pc0.y = y0;
1868
226
    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
226
    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
224
        p0.x = 0, p0.y = -1, dirn = 0;
1876
        /* Align stripes along radii for faster triangulation : */
1877
224
        inside = 1;
1878
224
        pfs->function_arg_shift = 1;
1879
224
    } else {
1880
        /* Must generate canonic quadrangle arcs,
1881
           because we approximate them with curves. */
1882
2
        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
2
        } else {
1888
2
            if (dy >= 0)
1889
2
                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
2
        }
1893
2
        pfs->function_arg_shift = 0;
1894
2
    }
1895
    /* fixme: wish: cut invisible parts off.
1896
       Note : when r0 != r1 the invisible part is not a half circle. */
1897
1.11k
    for (k = 0; k < 4; k++) {
1898
897
        gs_point p[12];
1899
897
        patch_curve_t curve[4];
1900
1901
        /* Set p1 to be 90 degrees anticlockwise from p0 */
1902
897
        p1.x = -p0.y; p1.y = p0.x;
1903
897
        if (dirn == 0) { /* Clockwise */
1904
670
            make_quadrant_arc(p + 0, &pc0, &p1, &p0, r0);
1905
670
            make_quadrant_arc(p + 6, &pc1, &p0, &p1, r1);
1906
670
        } else { /* Anticlockwise */
1907
227
            make_quadrant_arc(p + 0, &pc0, &p0, &p1, r0);
1908
227
            make_quadrant_arc(p + 6, &pc1, &p1, &p0, r1);
1909
227
        }
1910
897
        p[4].x = (p[3].x * 2 + p[6].x) / 3;
1911
897
        p[4].y = (p[3].y * 2 + p[6].y) / 3;
1912
897
        p[5].x = (p[3].x + p[6].x * 2) / 3;
1913
897
        p[5].y = (p[3].y + p[6].y * 2) / 3;
1914
897
        p[10].x = (p[9].x * 2 + p[0].x) / 3;
1915
897
        p[10].y = (p[9].y * 2 + p[0].y) / 3;
1916
897
        p[11].x = (p[9].x + p[0].x * 2) / 3;
1917
897
        p[11].y = (p[9].y + p[0].y * 2) / 3;
1918
4.48k
        for (j = 0; j < 4; j++) {
1919
3.58k
            int jj = (j + inside) % 4;
1920
1921
3.58k
            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.58k
            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.58k
            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.58k
            curve[j].straight = (((j + inside) & 1) != 0);
1930
3.58k
        }
1931
897
        curve[(0 + inside) % 4].vertex.cc[0] = t0;
1932
897
        curve[(1 + inside) % 4].vertex.cc[0] = t0;
1933
897
        curve[(2 + inside) % 4].vertex.cc[0] = t1;
1934
897
        curve[(3 + inside) % 4].vertex.cc[0] = t1;
1935
897
        curve[0].vertex.cc[1] = curve[1].vertex.cc[1] = 0; /* Initialize against FPE. */
1936
897
        curve[2].vertex.cc[1] = curve[3].vertex.cc[1] = 0; /* Initialize against FPE. */
1937
897
        code = patch_fill(pfs, curve, NULL, NULL);
1938
897
        if (code < 0)
1939
5
            return code;
1940
        /* Move p0 to be ready for the next position */
1941
892
        if (k == 0) {
1942
            /* p0 moves clockwise */
1943
225
            p1 = p0;
1944
225
            p0.x = p1.y; p0.y = -p1.x;
1945
225
            dirn = 0;
1946
667
        } else if (k == 1) {
1947
            /* p0 flips sides */
1948
225
            p0.x = -p0.x; p0.y = -p0.y;
1949
225
            dirn = 1;
1950
442
        } else if (k == 2) {
1951
            /* p0 moves anti-clockwise */
1952
221
            p1 = p0;
1953
221
            p0.x = -p1.y; p0.y = p1.x;
1954
221
            dirn = 0;
1955
221
        }
1956
892
    }
1957
221
    return 0;
1958
226
}
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
4
{
1965
4
    double from_tan_alpha, to_tan_alpha, from_alpha, to_alpha;
1966
4
    double half_inscribed_angle, intersect_x, intersect_y, intersect_dist;
1967
4
    double radius = sqrt(((from->x - centre->x) * (from->x - centre->x)) + ((from->y - centre->y) * (from->y - centre->y)));
1968
4
    double tangent_intersect_dist;
1969
4
    double F;
1970
4
    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
4
    if (from->x > to->x) {
1976
2
        if (from->y > to->y) {
1977
1
            if (to->y >= centre->y)
1978
1
                quadrant = 1 + 4;
1979
0
            else
1980
0
                quadrant = 3;
1981
1
        } else {
1982
1
            if (to->x >= centre->x)
1983
0
                quadrant = 0 + 4;
1984
1
            else
1985
1
                quadrant = 2;
1986
1
        }
1987
2
    } else {
1988
2
        if (from->y > to->y) {
1989
1
            if (from->x >= centre->x)
1990
1
                quadrant = 0;
1991
0
            else
1992
0
                quadrant = 2 + 4;
1993
1
        } else {
1994
1
            if (from->x >= centre->x)
1995
1
                quadrant = 3 + 4;
1996
0
            else
1997
0
                quadrant = 1;
1998
1
        }
1999
2
    }
2000
2001
4
    switch(quadrant) {
2002
        /* quadrant 0, arc goes clockwise */
2003
1
        case 0:
2004
1
            if (from->x == centre->x) {
2005
1
                from_alpha = M_PI / 2;
2006
1
            } 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
1
            to_tan_alpha = (to->y - centre->y) / (to->x - centre->x);
2011
1
            to_alpha = atan(to_tan_alpha);
2012
2013
1
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2014
1
            intersect_dist = radius / cos(half_inscribed_angle);
2015
1
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2016
2017
1
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2018
1
            intersect_y = centre->y + sin(to_alpha + half_inscribed_angle) * intersect_dist;
2019
1
            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
1
        case 2:
2041
1
            if (from->x == centre->x) {
2042
1
                from_alpha = M_PI / 2;
2043
1
            } 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
1
            to_tan_alpha = (centre->y - to->y) / (centre->x - to->x);
2049
1
            to_alpha = atan(to_tan_alpha);
2050
2051
1
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2052
1
            intersect_dist = radius / cos(half_inscribed_angle);
2053
1
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2054
2055
1
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2056
1
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2057
1
            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
1
        case 5:
2098
1
            from_tan_alpha = (centre->x - from->x) / (from->y - centre->y);
2099
1
            from_alpha = atan(from_tan_alpha);
2100
2101
1
            if (to->y == centre->y) {
2102
1
                to_alpha = M_PI / 2;
2103
1
            }
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
1
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2110
1
            intersect_dist = radius / cos(half_inscribed_angle);
2111
1
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2112
2113
1
            intersect_x = centre->x - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2114
1
            intersect_y = centre->y + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2115
1
            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
1
        case 7:
2137
1
            if (from->x == centre->x) {
2138
1
                from_alpha = M_PI / 2;
2139
1
            } 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
1
            to_tan_alpha = (centre->y - to->y) / (to->x - centre->x);
2144
1
            to_alpha = atan(to_tan_alpha);
2145
2146
1
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2147
1
            intersect_dist = radius / cos(half_inscribed_angle);
2148
1
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2149
2150
1
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2151
1
            intersect_y = centre->y - sin(to_alpha + half_inscribed_angle) * intersect_dist;
2152
1
            break;
2153
4
    }
2154
2155
4
    F = (4.0 / 3.0) / (1 + sqrt(1 + ((tangent_intersect_dist / radius) * (tangent_intersect_dist / radius))));
2156
2157
4
    from_control->x = from->x - ((from->x - intersect_x) * F);
2158
4
    from_control->y = from->y - ((from->y - intersect_y) * F);
2159
4
    to_control->x = to->x - ((to->x - intersect_x) * F);
2160
4
    to_control->y = to->y - ((to->y - intersect_y) * F);
2161
2162
4
    return 0;
2163
4
}
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
28
{
2168
28
    double x_1third, x_2third, y_1third, y_2third;
2169
2170
28
    x_1third = (to->x - from->x) / 3;
2171
28
    x_2third = x_1third * 2;
2172
28
    y_1third = (to->y - from->y) / 3;
2173
28
    y_2third = y_1third * 2;
2174
2175
28
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2176
28
    gs_point_transform2fixed(ctm, from->x + x_1third, from->y + y_1third, &p->control[0]);
2177
28
    gs_point_transform2fixed(ctm, from->x + x_2third, from->y + y_2third, &p->control[1]);
2178
2179
28
    p->vertex.cc[0] = t;
2180
28
    p->vertex.cc[1] = t;
2181
28
    p->straight = 1;
2182
2183
28
    return 0;
2184
28
}
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
4
{
2188
4
    gs_point from_control, to_control;
2189
2190
4
    find_arc_control_points(from, to, &from_control, &to_control, centre);
2191
2192
4
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2193
4
    gs_point_transform2fixed(ctm, from_control.x, from_control.y, &p->control[0]);
2194
4
    gs_point_transform2fixed(ctm, to_control.x, to_control.y, &p->control[1]);
2195
4
    p->vertex.cc[0] = t;
2196
4
    p->vertex.cc[1] = t;
2197
4
    p->straight = 0;
2198
2199
4
    return 0;
2200
4
}
2201
2202
static int draw_quarter_annulus(patch_fill_state_t *pfs, gs_point *centre, double radius, gs_point *corner, float t)
2203
4
{
2204
4
    gs_point p0, p1, initial;
2205
4
    patch_curve_t p[4];
2206
4
    int code;
2207
2208
4
    if (corner->x > centre->x) {
2209
2
        initial.x = centre->x + radius;
2210
2
    }
2211
2
    else {
2212
2
        initial.x = centre->x - radius;
2213
2
    }
2214
4
    initial.y = centre->y;
2215
2216
4
    p1.x = initial.x;
2217
4
    p1.y = corner->y;
2218
4
    patch_lineto(&pfs->pgs->ctm, &initial, &p1, &p[0], t);
2219
4
    p0.x = centre->x;
2220
4
    p0.y = p1.y;
2221
4
    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &p[1], t);
2222
4
    p1.x = centre->x;
2223
4
    if (centre->y > corner->y) {
2224
2
        p1.y = centre->y - radius;
2225
2
    } else {
2226
2
        p1.y = centre->y + radius;
2227
2
    }
2228
4
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2229
4
    patch_curveto(&pfs->pgs->ctm, centre, &p1, &initial, &p[3], t);
2230
4
    code = patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL);
2231
4
    if (code < 0)
2232
0
        return code;
2233
2234
4
    if (corner->x > centre->x)
2235
2
        initial.x = corner->x - (corner->x - (centre->x + radius));
2236
2
    else
2237
2
        initial.x = centre->x - radius;
2238
4
    initial.y = corner->y;
2239
4
    patch_lineto(&pfs->pgs->ctm, corner, &initial, &p[0], t);
2240
2241
4
    p0.x = initial.x;
2242
4
    p0.y = centre->y;
2243
4
    patch_lineto(&pfs->pgs->ctm, &initial, &p0, &p[1], t);
2244
2245
4
    p1.y = p0.y;
2246
4
    p1.x = corner->x;
2247
4
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2248
4
    patch_lineto(&pfs->pgs->ctm, &p1, corner, &p[3], t);
2249
2250
4
    return (patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL));
2251
4
}
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
1
{
2257
1
    patch_curve_t curve[4];
2258
1
    gs_point p0, p1;
2259
1
    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
1
    if (x0 == x1 || y0 == y1) {
2266
1
        if (x0 == x1 && y0 > y1) {
2267
            /* tangent at top of circles */
2268
1
            p0.x = x1, p0.y = y1;
2269
1
            p1.x = x1 + r2, p1.y = y1 - r2;
2270
1
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2271
1
            p1.x = x1 - r2, p1.y = y1 - r2;
2272
1
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2273
1
            p1.x = x1 + r2, p1.y = y1 + r1;
2274
1
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2275
1
            p1.x = x1 - r2, p1.y = y1 + r1;
2276
1
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2277
1
        }
2278
1
        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
1
        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
1
        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
1
    }
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
1
    return 0;
2867
1
}
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
100
{
2931
100
    double d, dd;
2932
2933
100
    dd = hypot(rect->p.x - x0, rect->p.y - y0);
2934
100
    d = hypot(rect->p.x - x0, rect->q.y - y0);
2935
100
    dd = max(dd, d);
2936
100
    d = hypot(rect->q.x - x0, rect->q.y - y0);
2937
100
    dd = max(dd, d);
2938
100
    d = hypot(rect->q.x - x0, rect->p.y - y0);
2939
100
    dd = max(dd, d);
2940
100
    return dd;
2941
100
}
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
1
{
2974
1
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
2975
1
    double d = hypot(dx, dy);
2976
    /* Assuming dr > d / 3 && d > dr + 1e-7 * (d + dr), see the caller. */
2977
1
    double r = r_rect * 1.4143; /* A few bigger than sqrt(2). */
2978
1
    double ax, ay, as; /* Cone apex. */
2979
1
    double g0; /* The distance from apex to the tangent point of the 0th circle. */
2980
1
    int code;
2981
2982
1
    as = r0 / (r0 - r1);
2983
1
    ax = x0 + (x1 - x0) * as;
2984
1
    ay = y0 + (y1 - y0) * as;
2985
1
    g0 = sqrt(dx * dx + dy * dy - dr * dr) * as;
2986
1
    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
1
    } 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
1
        double es, er; /* The limit circle parameter, radius. */
3015
1
        double ex, ey; /* The limit circle centrum. */
3016
3017
1
        es = as - as * r / g0; /* Always negative. */
3018
1
        er = r * r0 / g0 ;
3019
1
        ex = x0 + dx * es;
3020
1
        ey = y0 + dy * es;
3021
        /* Fill the annulus: */
3022
1
        code = R_tensor_annulus(pfs, x0, y0, r0, t0, ex, ey, er, t0);
3023
1
        if (code < 0)
3024
0
            return code;
3025
        /* Fill entire ending circle to ensure entire rect is covered. */
3026
1
        return R_tensor_annulus(pfs, ex, ey, er, t0, ex, ey, 0, t0);
3027
1
    }
3028
1
}
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
197
{
3092
197
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3093
197
    double r0 = psh->params.Coords[2];
3094
197
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3095
197
    double r1 = psh->params.Coords[5];
3096
197
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
3097
197
    double d = hypot(dx, dy), r;
3098
197
    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
197
    if (any_abs (dr - d) < 0.001) {
3137
2
        if ((r0 > r1 && Extend0) || (r1 > r0 && Extend1)) {
3138
1
            r = R_rect_radius(rect, x0, y0);
3139
1
            if (r0 < r1)
3140
0
                code = R_tensor_annulus_extend_tangent(pfs, x0, y0, r0, t1, x1, y1, r1, t1, r);
3141
1
            else
3142
1
                code = R_tensor_annulus_extend_tangent(pfs, x1, y1, r1, t0, x0, y0, r0, t0, r);
3143
1
            if (code < 0)
3144
0
                return code;
3145
1
        } else {
3146
1
            if (r0 > r1) {
3147
1
                if (Extend1 && r1 > 0)
3148
0
                    return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3149
1
            }
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
1
        }
3155
2
    } else
3156
195
    if (dr > d - 1e-4 * (d + dr)) {
3157
        /* Nested circles, or degenerate. */
3158
193
        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
193
        } else {
3170
193
            if (Extend1) {
3171
98
                r = R_rect_radius(rect, x1, y1);
3172
98
                if (r > r1) {
3173
98
                    code = R_tensor_annulus(pfs, x1, y1, r, t1, x1, y1, r1, t1);
3174
98
                    if (code < 0)
3175
0
                        return code;
3176
98
                }
3177
98
            }
3178
193
            if (Extend0 && r0 > 0)
3179
0
                return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3180
193
        }
3181
193
    } else if (dr > d / 3) {
3182
        /* Obtuse cone. */
3183
2
        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
2
        } else {
3194
2
            if (Extend1) {
3195
1
                r = R_rect_radius(rect, x1, y1);
3196
1
                code = R_obtuse_cone(pfs, rect, x1, y1, r1, x0, y0, r0, t1, r);
3197
1
                if (code < 0)
3198
0
                    return code;
3199
1
            }
3200
2
            if (Extend0 && r0 != 0)
3201
0
                return R_tensor_cone_apex(pfs, rect, x1, y1, r1, x0, y0, r0, t0);
3202
2
        }
3203
2
    } 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
197
    return 0;
3229
197
}
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.36k
#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
498
{
3298
498
    double cx = rsa->x0 + (rsa->x1 - rsa->x0) * t;
3299
498
    double cy = rsa->y0 + (rsa->y1 - rsa->y0) * t;
3300
498
    double rx = rsa->p[point_index].x - cx;
3301
498
    double ry = rsa->p[point_index].y - cy;
3302
498
    double dx = rsa->p[point_index - 1].x - rsa->p[point_index].x;
3303
498
    double dy = rsa->p[point_index - 1].y - rsa->p[point_index].y;
3304
3305
498
    if (at_corner) {
3306
480
        double Dx = rsa->p[point_index + 1].x - rsa->p[point_index].x;
3307
480
        double Dy = rsa->p[point_index + 1].y - rsa->p[point_index].y;
3308
480
        bool b1 = (dx * rx + dy * ry >= 0);
3309
480
        bool b2 = (Dx * rx + Dy * ry >= 0);
3310
3311
480
        if (b1 & b2)
3312
116
            rsa->outer_contact[root_index] = true;
3313
480
    } else {
3314
18
        if (rx * dy - ry * dx < 0)
3315
6
            rsa->outer_contact[root_index] = true;
3316
18
    }
3317
498
}
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
960
{
3322
960
    int i;
3323
3324
2.88k
    for (i = 0; i < 2; i++) {
3325
1.92k
        bool good_root;
3326
3327
1.92k
        if (!have_root[i])
3328
942
            continue;
3329
978
        good_root = (!rsa->have_apex || (rsa->apex <= 0 || r0 == 0 ? t[i] >= rsa->apex : t[i] <= rsa->apex));
3330
978
        if (good_root) {
3331
498
            radial_shading_external_contact(rsa, point_index, t[i], r0, r1, at_corner, i);
3332
498
            if (!rsa->have_root[i]) {
3333
30
                rsa->span[i][0] = rsa->span[i][1] = t[i];
3334
30
                rsa->have_root[i] = true;
3335
468
            } else {
3336
468
                if (rsa->span[i][0] > t[i])
3337
29
                    rsa->span[i][0] = t[i];
3338
468
                if (rsa->span[i][1] < t[i])
3339
69
                    rsa->span[i][1] = t[i];
3340
468
            }
3341
498
        }
3342
978
    }
3343
960
}
3344
3345
static void
3346
compute_radial_shading_span_extended_side(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3347
480
{
3348
480
    double cc, c;
3349
480
    bool have_root[2] = {false, false};
3350
480
    double t[2];
3351
480
    bool by_x = (rsa->p[point_index].x != rsa->p[point_index + 1].x);
3352
480
    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
480
    if (by_x) {
3365
224
        c = rsa->p[point_index].x - rsa->x0;
3366
224
        cc = rsa->x1 - rsa->x0;
3367
256
    } else {
3368
256
        c = rsa->p[point_index].y - rsa->y0;
3369
256
        cc = rsa->y1 - rsa->y0;
3370
256
    }
3371
480
    t[0] = (c - r0) / (cc + r1 - r0);
3372
480
    t[1] = (c + r0) / (cc - r1 + r0);
3373
480
    if (t[0] > t[1]) {
3374
392
        c    = t[0];
3375
392
        t[0] = t[1];
3376
392
        t[1] = c;
3377
392
    }
3378
1.44k
    for (i = 0; i < 2; i++) {
3379
960
        double d, d0, d1;
3380
3381
960
        if (by_x) {
3382
448
            d = rsa->y1 - rsa->y0 + r0 + (r1 - r0) * t[i];
3383
448
            d0 = rsa->p[point_index].y;
3384
448
            d1 = rsa->p[point_index + 1].y;
3385
512
        } else {
3386
512
            d = rsa->x1 - rsa->x0 + r0 + (r1 - r0) * t[i];
3387
512
            d0 = rsa->p[point_index].x;
3388
512
            d1 = rsa->p[point_index + 1].x;
3389
512
        }
3390
960
        if (d1 > d0 ? d0 <= d && d <= d1 : d1 <= d && d <= d0)
3391
18
            have_root[i] = true;
3392
960
    }
3393
480
    store_roots(rsa, have_root, t, r0, r1, point_index, false);
3394
480
}
3395
3396
static int
3397
compute_radial_shading_span_extended_point(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3398
480
{
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
480
    double p1x = rsa->x1 - rsa->x0;
3423
480
    double p1y = rsa->y1 - rsa->y0;
3424
480
    double qx  = rsa->p[point_index].x - rsa->x0;
3425
480
    double qy  = rsa->p[point_index].y - rsa->y0;
3426
480
    double a   = (Pw2(p1x) + Pw2(p1y) - Pw2(r0 - r1));
3427
480
    bool have_root[2] = {false, false};
3428
480
    double t[2];
3429
3430
480
    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
480
    } 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
480
        double minushalfb = r0*(r1-r0) + p1x*qx + p1y*qy;
3442
480
        double c          = Pw2(qx) + Pw2(qy) - Pw2(r0);
3443
480
        double desc2      = Pw2(minushalfb) - a*c; /* desc2 = 1/4 (b^2-4ac) */
3444
3445
480
        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
480
        } else {
3449
480
            double desc1 = sqrt(desc2); /* desc1 = 1/2 SQRT(b^2-4ac) */
3450
3451
480
            if (a > 0) {
3452
0
                t[0] = (minushalfb - desc1) / a;
3453
0
                t[1] = (minushalfb + desc1) / a;
3454
480
            } else {
3455
480
                t[0] = (minushalfb + desc1) / a;
3456
480
                t[1] = (minushalfb - desc1) / a;
3457
480
            }
3458
480
            have_root[0] = have_root[1] = true;
3459
480
        }
3460
480
    }
3461
480
    store_roots(rsa, have_root, t, r0, r1, point_index, true);
3462
480
    if (have_root[0] && have_root[1])
3463
480
        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
120
{
3476
120
    int span_type0, span_type1;
3477
3478
120
    span_type0 = compute_radial_shading_span_extended_point(rsa, r0, r1, 1);
3479
120
    if (span_type0 == -1)
3480
0
        return -1;
3481
120
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 2);
3482
120
    if (span_type0 != span_type1)
3483
0
        return -1;
3484
120
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 3);
3485
120
    if (span_type0 != span_type1)
3486
0
        return -1;
3487
120
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 4);
3488
120
    if (span_type0 != span_type1)
3489
0
        return -1;
3490
120
    compute_radial_shading_span_extended_side(rsa, r0, r1, 1);
3491
120
    compute_radial_shading_span_extended_side(rsa, r0, r1, 2);
3492
120
    compute_radial_shading_span_extended_side(rsa, r0, r1, 3);
3493
120
    compute_radial_shading_span_extended_side(rsa, r0, r1, 4);
3494
120
    return span_type0;
3495
120
}
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
30
{
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
30
    const double extent = 1.02;
3558
30
    int span_type0, span_type1, span_type;
3559
3560
30
    memset(rsa, 0, sizeof(*rsa));
3561
30
    rsa->x0 = x0;
3562
30
    rsa->y0 = y0;
3563
30
    rsa->x1 = x1;
3564
30
    rsa->y1 = y1;
3565
30
    rsa->p[0] = rsa->p[4] = rect->p;
3566
30
    rsa->p[1].x = rsa->p[5].x = rect->p.x;
3567
30
    rsa->p[1].y = rsa->p[5].y = rect->q.y;
3568
30
    rsa->p[2] = rect->q;
3569
30
    rsa->p[3].x = rect->q.x;
3570
30
    rsa->p[3].y = rect->p.y;
3571
30
    rsa->have_apex = any_abs(r1 - r0) > 1e-7 * any_abs(r1 + r0);
3572
30
    rsa->apex = (rsa->have_apex ? -r0 / (r1 - r0) : 0);
3573
30
    span_type0 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 * extent);
3574
30
    if (span_type0 == -1)
3575
0
        return -1;
3576
30
    span_type1 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 / extent);
3577
30
    if (span_type0 != span_type1)
3578
0
        return -1;
3579
30
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 * extent);
3580
30
    if (span_type0 != span_type1)
3581
0
        return -1;
3582
30
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 / extent);
3583
30
    if (span_type1 == -1)
3584
0
        return -1;
3585
30
    if (r0 < r1) {
3586
30
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3587
0
            rsa->span[0][0] = rsa->apex; /* Likely never happens. Remove ? */
3588
30
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3589
7
            rsa->span[1][0] = rsa->apex;
3590
30
    } 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
30
    span_type = 0;
3597
30
    if (rsa->have_root[0] && rsa->span[0][0] < 0)
3598
0
        span_type |= 1;
3599
30
    if (rsa->have_root[1] && rsa->span[1][0] < 0)
3600
0
        span_type |= 1;
3601
30
    if (rsa->have_root[0] && rsa->span[0][1] > 0 && rsa->span[0][0] < 1)
3602
0
        span_type |= 2;
3603
30
    if (rsa->have_root[1] && rsa->span[1][1] > 0 && rsa->span[1][0] < 1)
3604
29
        span_type |= 4;
3605
30
    if (rsa->have_root[0] && rsa->span[0][1] > 1)
3606
0
        span_type |= 8;
3607
30
    if (rsa->have_root[1] && rsa->span[1][1] > 1)
3608
5
        span_type |= 8;
3609
30
    return span_type;
3610
30
}
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
29
{
3615
29
    double s0 = span_[0], s1 = span_[1], w;
3616
3617
29
    if (s0 < 0)
3618
0
        s0 = 0;
3619
29
    if (s1 < 0)
3620
0
        s1 = 0;
3621
29
    if (s0 > 1)
3622
0
        s0 = 1;
3623
29
    if (s1 > 1)
3624
4
        s1 = 1;
3625
29
    w = s1 - s0;
3626
29
    if (w == 0)
3627
0
        return false; /* Don't pass a degenerate shading. */
3628
29
    if (w > 0.3)
3629
23
        return false; /* The span is big, don't shorten it. */
3630
6
    { /* Do shorten. */
3631
6
        double R0 = *r0, X0 = *x0, Y0 = *y0, D0 = *d0;
3632
6
        double R1 = *r1, X1 = *x1, Y1 = *y1, D1 = *d1;
3633
3634
6
        *r0 = R0 + (R1 - R0) * s0;
3635
6
        *x0 = X0 + (X1 - X0) * s0;
3636
6
        *y0 = Y0 + (Y1 - Y0) * s0;
3637
6
        *d0 = D0 + (D1 - D0) * s0;
3638
6
        *r1 = R0 + (R1 - R0) * s1;
3639
6
        *x1 = X0 + (X1 - X0) * s1;
3640
6
        *y1 = Y0 + (Y1 - Y0) * s1;
3641
6
        *d1 = D0 + (D1 - D0) * s1;
3642
6
    }
3643
6
    return true;
3644
29
}
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
127
{
3649
127
    const double d = hypot(x1 - x0, y1 - y0);
3650
127
    const double area0 = M_PI * r0 * r0 / 2;
3651
127
    const double area1 = M_PI * r1 * r1 / 2;
3652
127
    const double area2 = (r0 + r1) / 2 * d;
3653
127
    const double arbitrary = 8;
3654
127
    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
127
    areaX = (rect->q.x - rect->p.x) * (rect->q.x - rect->p.x);
3662
127
    if (areaX * arbitrary < area0 + area1 + area2)
3663
24
        return true;
3664
103
    areaY = (rect->q.y - rect->p.y) * (rect->q.y - rect->p.y);
3665
103
    if (areaY * arbitrary < area0 + area1 + area2)
3666
6
        return true;
3667
97
    return false;
3668
103
}
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
129
{
3675
129
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
3676
129
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
3677
129
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3678
129
    double r0 = psh->params.Coords[2];
3679
129
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3680
129
    double r1 = psh->params.Coords[5];
3681
129
    radial_shading_attrs_t rsa;
3682
129
    int span_type; /* <0 - don't shorten, 1 - extent0, 2 - first contact, 4 - last contact, 8 - extent1. */
3683
129
    int code;
3684
129
    patch_fill_state_t pfs1;
3685
3686
129
    if (r0 == 0 && r1 == 0)
3687
2
        return 0; /* PLRM requires to paint nothing. */
3688
127
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
3689
127
    if (code < 0)
3690
0
        return code;
3691
127
    pfs1.Function = psh->params.Function;
3692
127
    code = init_patch_fill_state(&pfs1);
3693
127
    if (code < 0) {
3694
0
        if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3695
0
        return code;
3696
0
    }
3697
127
    pfs1.function_arg_shift = 0;
3698
127
    pfs1.rect = *clip_rect;
3699
127
    pfs1.maybe_self_intersecting = false;
3700
127
    if (is_radial_shading_large(x0, y0, r0, x1, y1, r1, rect))
3701
30
        span_type = compute_radial_shading_span(&rsa, x0, y0, r0, x1, y1, r1, rect);
3702
97
    else
3703
97
        span_type = -1;
3704
127
    if (span_type < 0) {
3705
97
        code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3706
97
        if (code >= 0)
3707
97
            code = R_tensor_annulus(&pfs1, x0, y0, r0, d0, x1, y1, r1, d1);
3708
97
        if (code >= 0)
3709
96
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3710
97
    } else if (span_type == 1) {
3711
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d0);
3712
30
    } else if (span_type == 8) {
3713
1
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d1);
3714
29
    } else {
3715
29
        bool second_interval = true;
3716
3717
29
        code = 0;
3718
29
        if (span_type & 1)
3719
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3720
29
        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
29
        if (code >= 0 && second_interval) {
3737
29
            if (span_type & 4) {
3738
29
                float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3739
29
                double R0 = r0, R1 = r1;
3740
3741
29
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[1]);
3742
29
                code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3743
29
            }
3744
29
        }
3745
29
        if (code >= 0 && (span_type & 8))
3746
4
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3747
29
    }
3748
127
    if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3749
127
    if (term_patch_fill_state(&pfs1))
3750
0
        return_error(gs_error_unregistered); /* Must not happen. */
3751
127
    return code;
3752
127
}
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
129
{
3759
129
    return gs_shading_R_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
3760
129
}