Coverage Report

Created: 2025-06-10 07:06

/src/ghostpdl/base/gxshade1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Rendering for non-mesh shadings */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsmatrix.h"   /* for gscoord.h */
23
#include "gscoord.h"
24
#include "gspath.h"
25
#include "gsptype2.h"
26
#include "gxcspace.h"
27
#include "gxdcolor.h"
28
#include "gxfarith.h"
29
#include "gxfixed.h"
30
#include "gxgstate.h"
31
#include "gxpath.h"
32
#include "gxshade.h"
33
#include "gxdevcli.h"
34
#include "gxshade4.h"
35
#include "gsicc_cache.h"
36
37
/* ---------------- Function-based shading ---------------- */
38
39
typedef struct Fb_frame_s { /* A rudiment of old code. */
40
    gs_rect region;
41
    gs_client_color cc[4];  /* colors at 4 corners */
42
    int state;
43
} Fb_frame_t;
44
45
typedef struct Fb_fill_state_s {
46
    shading_fill_state_common;
47
    const gs_shading_Fb_t *psh;
48
    gs_matrix_fixed ptm;  /* parameter space -> device space */
49
    Fb_frame_t frame;
50
} Fb_fill_state_t;
51
/****** NEED GC DESCRIPTOR ******/
52
53
static inline void
54
make_other_poles(patch_curve_t curve[4])
55
3.39k
{
56
3.39k
    int i, j;
57
58
16.9k
    for (i = 0; i < 4; i++) {
59
13.5k
        j = (i + 1) % 4;
60
13.5k
        curve[i].control[0].x = (curve[i].vertex.p.x * 2 + curve[j].vertex.p.x) / 3;
61
13.5k
        curve[i].control[0].y = (curve[i].vertex.p.y * 2 + curve[j].vertex.p.y) / 3;
62
13.5k
        curve[i].control[1].x = (curve[i].vertex.p.x + curve[j].vertex.p.x * 2) / 3;
63
13.5k
        curve[i].control[1].y = (curve[i].vertex.p.y + curve[j].vertex.p.y * 2) / 3;
64
13.5k
        curve[i].straight = true;
65
13.5k
    }
66
3.39k
}
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
0
{
204
0
    if (p[0].x < rect->p.x &&
205
0
        p[1].x < rect->p.x &&
206
0
        p[2].x < rect->p.x &&
207
0
        p[3].x < rect->p.x)
208
0
        return 0; /* Clipped away! */
209
0
    if (p[0].x > rect->q.x &&
210
0
        p[1].x > rect->q.x &&
211
0
        p[2].x > rect->q.x &&
212
0
        p[3].x > rect->q.x)
213
0
        return 0; /* Clipped away! */
214
0
    if (p[0].y < rect->p.y &&
215
0
        p[1].y < rect->p.y &&
216
0
        p[2].y < rect->p.y &&
217
0
        p[3].y < rect->p.y)
218
0
        return 0; /* Clipped away! */
219
0
    if (p[0].y > rect->q.y &&
220
0
        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
0
    return 1;
225
0
}
226
227
#define midpoint(a,b)\
228
7.16k
  (arith_rshift_1(a) + arith_rshift_1(b) + (((a) | (b)) & 1))
229
230
#define quarterpoint(a,b)\
231
4.20k
  (midpoint(a,midpoint(a,b)))
232
233
static int
234
subdivide_patch_fill(patch_fill_state_t *pfs, patch_curve_t c[4])
235
3.56k
{
236
3.56k
    fixed m0, m1;
237
3.56k
    int v0, v1;
238
3.56k
    int changed;
239
240
3.56k
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
241
4
        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
3.55k
#define MIDPOINT_ACCURACY 1
263
3.55k
#define QUARTERPOINT_ACCURACY 3
264
265
3.88k
    do {
266
3.88k
        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
3.88k
        if ((c[0].vertex.p.x <= pfs->rect.p.x &&
272
3.88k
             c[1].vertex.p.x <= pfs->rect.p.x &&
273
3.88k
             c[2].vertex.p.x <= pfs->rect.p.x &&
274
3.88k
             c[3].vertex.p.x <= pfs->rect.p.x) ||
275
3.88k
            (c[0].vertex.p.x >= pfs->rect.q.x &&
276
3.76k
             c[1].vertex.p.x >= pfs->rect.q.x &&
277
3.76k
             c[2].vertex.p.x >= pfs->rect.q.x &&
278
3.76k
             c[3].vertex.p.x >= pfs->rect.q.x))
279
159
                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
3.72k
        if (c[0].vertex.p.x < pfs->rect.p.x && c[3].vertex.p.x < pfs->rect.p.x)
285
112
        {
286
            /* Check 0+3 off left. */
287
112
            v0 = 0;
288
112
            v1 = 3;
289
112
            goto check_left;
290
112
        }
291
3.60k
        else if (c[1].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
292
1
        {
293
            /* Check 1+2 off left. */
294
1
            v0 = 1;
295
1
            v1 = 2;
296
113
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
113
            do
300
113
            {
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
113
                m0 = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
307
113
                if (m0 >= pfs->rect.p.x)
308
113
                    goto check_left_quarter;
309
0
                m1 = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
310
0
                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
0
                c[v0].vertex.p.x = m0;
314
0
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
315
0
                c[v1].vertex.p.x = m1;
316
0
                c[v1].vertex.p.y = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
317
0
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
318
0
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
319
0
                changed = 1;
320
0
            }
321
113
            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
113
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
113
                do
328
114
                {
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
114
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
335
114
                    if (m0 >= pfs->rect.p.x)
336
113
                        break;
337
1
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
338
1
                    if (m1 >= pfs->rect.p.x)
339
0
                        break;
340
                    /* So, we can completely discard the left hand quarter of the patch. */
341
1
                    c[v0].vertex.p.x = m0;
342
1
                    c[v0].vertex.p.y = midpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
343
1
                    c[v1].vertex.p.x = m1;
344
1
                    c[v1].vertex.p.y = midpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
345
1
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
346
1
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
347
1
                    changed = 1;
348
1
                }
349
113
                while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
350
113
            }
351
0
        }
352
353
        /* or the right hand half? */
354
3.72k
        if (c[0].vertex.p.x > pfs->rect.q.x && c[3].vertex.p.x > pfs->rect.q.x)
355
0
        {
356
            /* Check 0+3 off right. */
357
0
            v0 = 0;
358
0
            v1 = 3;
359
0
            goto check_right;
360
0
        }
361
3.72k
        else if (c[1].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
362
8
        {
363
            /* Check 1+2 off right. */
364
8
            v0 = 1;
365
8
            v1 = 2;
366
8
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
8
            do
370
12
            {
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
12
                m0 = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
377
12
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
378
8
                    goto check_right_quarter;
379
4
                m1 = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
380
4
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
381
0
                    goto check_right_quarter;
382
                /* So, we can completely discard the left hand half of the patch. */
383
4
                c[v0].vertex.p.x = m0;
384
4
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
385
4
                c[v1].vertex.p.x = m1;
386
4
                c[v1].vertex.p.y = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
387
4
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
388
4
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
389
4
                changed = 1;
390
4
            }
391
8
            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
8
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
8
                do
398
12
                {
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
12
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
405
12
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
406
8
                        break;
407
4
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
408
4
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
409
0
                        break;
410
                    /* So, we can completely discard the left hand half of the patch. */
411
4
                    c[v0].vertex.p.x = m0;
412
4
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
413
4
                    c[v1].vertex.p.x = m1;
414
4
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
415
4
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
416
4
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
417
4
                    changed = 1;
418
4
                }
419
8
                while (c[v0].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
420
8
            }
421
0
        }
422
423
        /* Now, rotated cases: Can we cull the left hand half? */
424
3.72k
        if (c[0].vertex.p.x < pfs->rect.p.x && c[1].vertex.p.x < pfs->rect.p.x)
425
346
        {
426
            /* Check 0+1 off left. */
427
346
            v0 = 0;
428
346
            v1 = 1;
429
346
            goto check_rot_left;
430
346
        }
431
3.37k
        else if (c[3].vertex.p.x < pfs->rect.p.x && c[2].vertex.p.x < pfs->rect.p.x)
432
185
        {
433
            /* Check 3+2 off left. */
434
185
            v0 = 3;
435
185
            v1 = 2;
436
531
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
531
            do
440
541
            {
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
541
                m0 = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
447
541
                if (m0 >= pfs->rect.p.x)
448
503
                    goto check_rot_left_quarter;
449
38
                m1 = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
450
38
                if (m1 >= pfs->rect.p.x)
451
28
                    goto check_rot_left_quarter;
452
                /* So, we can completely discard the left hand half of the patch. */
453
10
                c[v0].vertex.p.x = m0;
454
10
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
455
10
                c[v1].vertex.p.x = m1;
456
10
                c[v1].vertex.p.y = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
457
10
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
458
10
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
459
10
                changed = 1;
460
10
            }
461
531
            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
531
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
531
                do
468
628
                {
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
628
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
475
628
                    if (m0 >= pfs->rect.p.x)
476
376
                        break;
477
252
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
478
252
                    if (m1 >= pfs->rect.p.x)
479
155
                        break;
480
                    /* So, we can completely discard the left hand half of the patch. */
481
97
                    c[v0].vertex.p.x = m0;
482
97
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
483
97
                    c[v1].vertex.p.x = m1;
484
97
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
485
97
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
486
97
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
487
97
                    changed = 1;
488
97
                }
489
531
                while (c[v0].vertex.p.x < pfs->rect.p.x && c[v1].vertex.p.x < pfs->rect.p.x);
490
531
            }
491
0
        }
492
493
        /* or the right hand half? */
494
3.72k
        if (c[0].vertex.p.x > pfs->rect.q.x && c[1].vertex.p.x > pfs->rect.q.x)
495
121
        {
496
            /* Check 0+1 off right. */
497
121
            v0 = 0;
498
121
            v1 = 1;
499
121
            goto check_rot_right;
500
121
        }
501
3.60k
        else if (c[3].vertex.p.x > pfs->rect.q.x && c[2].vertex.p.x > pfs->rect.q.x)
502
339
        {
503
            /* Check 3+2 off right. */
504
339
            v0 = 3;
505
339
            v1 = 2;
506
460
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
460
            do
510
469
            {
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
469
                m0 = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
517
469
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
518
442
                    goto check_rot_right_quarter;
519
27
                m1 = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
520
27
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
521
18
                    goto check_rot_right_quarter;
522
                /* So, we can completely discard the left hand half of the patch. */
523
9
                c[v0].vertex.p.x = m0;
524
9
                c[v0].vertex.p.y = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
525
9
                c[v1].vertex.p.x = m1;
526
9
                c[v1].vertex.p.y = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
527
9
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
528
9
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
529
9
                changed = 1;
530
9
            }
531
460
            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
460
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
460
                do
538
559
                {
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
559
                    m0 = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
545
559
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
546
317
                        break;
547
242
                    m1 = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
548
242
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
549
143
                        break;
550
                    /* So, we can completely discard the left hand half of the patch. */
551
99
                    c[v0].vertex.p.x = m0;
552
99
                    c[v0].vertex.p.y = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
553
99
                    c[v1].vertex.p.x = m1;
554
99
                    c[v1].vertex.p.y = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
555
99
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
556
99
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
557
99
                    changed = 1;
558
99
                }
559
460
                while (c[v0].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && c[v1].vertex.p.x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
560
460
            }
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
3.72k
        if ((c[0].vertex.p.y <= pfs->rect.p.y &&
569
3.72k
             c[1].vertex.p.y <= pfs->rect.p.y &&
570
3.72k
             c[2].vertex.p.y <= pfs->rect.p.y &&
571
3.72k
             c[3].vertex.p.y <= pfs->rect.p.y) ||
572
3.72k
            (c[0].vertex.p.y >= pfs->rect.q.y &&
573
3.71k
             c[1].vertex.p.y >= pfs->rect.q.y &&
574
3.71k
             c[2].vertex.p.y >= pfs->rect.q.y &&
575
3.71k
             c[3].vertex.p.y >= pfs->rect.q.y))
576
5
            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
3.71k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[1].vertex.p.y < pfs->rect.p.y)
582
257
        {
583
            /* Check 0+1 off above. */
584
257
            v0 = 0;
585
257
            v1 = 1;
586
257
            goto check_above;
587
257
        }
588
3.45k
        else if (c[3].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
589
148
        {
590
            /* Check 3+2 off above. */
591
148
            v0 = 3;
592
148
            v1 = 2;
593
405
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
405
            do
597
460
            {
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
460
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
605
460
                if (m0 >= pfs->rect.p.y)
606
83
                    goto check_above_quarter;
607
377
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
608
377
                if (m1 >= pfs->rect.p.y)
609
322
                    goto check_above_quarter;
610
                /* So, we can completely discard the top half of the patch. */
611
55
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
612
55
                c[v0].vertex.p.y = m0;
613
55
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
614
55
                c[v1].vertex.p.y = m1;
615
55
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
616
55
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
617
55
                changed = 1;
618
55
            }
619
405
            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
405
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
405
                do
626
493
                {
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
493
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
636
493
                    if (m0 >= pfs->rect.p.y)
637
53
                        break;
638
440
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
639
440
                    if (m1 >= pfs->rect.p.y)
640
352
                        break;
641
                    /* So, we can completely discard the top half of the patch. */
642
88
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
643
88
                    c[v0].vertex.p.y = m0;
644
88
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
645
88
                    c[v1].vertex.p.y = m1;
646
88
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
647
88
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
648
88
                    changed = 1;
649
88
                }
650
405
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
651
405
            }
652
0
        }
653
654
        /* or the bottom half? */
655
3.71k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[1].vertex.p.y > pfs->rect.q.y)
656
154
        {
657
            /* Check 0+1 off bottom. */
658
154
            v0 = 0;
659
154
            v1 = 1;
660
154
            goto check_bottom;
661
154
        }
662
3.56k
        else if (c[3].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
663
258
        {
664
            /* Check 3+2 off bottom. */
665
258
            v0 = 3;
666
258
            v1 = 2;
667
412
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
412
            do
671
477
            {
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
477
                m0 = midpoint(c[0].vertex.p.y, c[3].vertex.p.y);
679
477
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
680
388
                    goto check_bottom_quarter;
681
89
                m1 = midpoint(c[1].vertex.p.y, c[2].vertex.p.y);
682
89
                if (m1 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
683
24
                    goto check_bottom_quarter;
684
                /* So, we can completely discard the bottom half of the patch. */
685
65
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[3].vertex.p.x);
686
65
                c[v0].vertex.p.y = m0;
687
65
                c[v1].vertex.p.x = midpoint(c[1].vertex.p.x, c[2].vertex.p.x);
688
65
                c[v1].vertex.p.y = m1;
689
65
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[3].vertex.cc[0])/2;
690
65
                c[v1].vertex.cc[0] = (c[1].vertex.cc[0] + c[2].vertex.cc[0])/2;
691
65
                changed = 1;
692
65
            }
693
412
            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
412
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
412
                do
700
515
                {
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
515
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^3].vertex.p.y);
710
515
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
711
382
                        break;
712
133
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^3].vertex.p.y);
713
133
                    if (m1 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
714
30
                        break;
715
                    /* So, we can completely discard the bottom half of the patch. */
716
103
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^3].vertex.p.x);
717
103
                    c[v0].vertex.p.y = m0;
718
103
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^3].vertex.p.x);
719
103
                    c[v1].vertex.p.y = m1;
720
103
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^3].vertex.cc[0])/4;
721
103
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^3].vertex.cc[0])/4;
722
103
                    changed = 1;
723
103
                }
724
412
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
725
412
            }
726
0
        }
727
728
        /* Now, rotated cases: Can we cull the top half? */
729
3.71k
        if (c[0].vertex.p.y < pfs->rect.p.y && c[3].vertex.p.y < pfs->rect.p.y)
730
11
        {
731
            /* Check 0+3 off above. */
732
11
            v0 = 0;
733
11
            v1 = 3;
734
11
            goto check_rot_above;
735
11
        }
736
3.70k
        else if (c[1].vertex.p.y < pfs->rect.p.y && c[2].vertex.p.y < pfs->rect.p.y)
737
0
        {
738
            /* Check 1+2 off above. */
739
0
            v0 = 1;
740
0
            v1 = 2;
741
11
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
11
            do
745
24
            {
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
24
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
753
24
                if (m0 >= pfs->rect.p.y)
754
11
                    goto check_rot_above_quarter;
755
13
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
756
13
                if (m1 >= pfs->rect.p.y)
757
0
                    goto check_rot_above_quarter;
758
                /* So, we can completely discard the top half of the patch. */
759
13
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
760
13
                c[v0].vertex.p.y = m0;
761
13
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
762
13
                c[v1].vertex.p.y = m1;
763
13
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
764
13
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
765
13
                changed = 1;
766
13
            }
767
13
            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
11
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
11
                do
774
15
                {
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
15
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
784
15
                    if (m0 >= pfs->rect.p.y)
785
11
                        break;
786
4
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
787
4
                    if (m1 >= pfs->rect.p.y)
788
0
                        break;
789
                    /* So, we can completely discard the top half of the patch. */
790
4
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
791
4
                    c[v0].vertex.p.y = m0;
792
4
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
793
4
                    c[v1].vertex.p.y = m1;
794
4
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
795
4
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
796
4
                    changed = 1;
797
4
                }
798
11
                while (c[v0].vertex.p.y < pfs->rect.p.y && c[v1].vertex.p.y < pfs->rect.p.y);
799
11
            }
800
0
        }
801
802
        /* or the bottom half? */
803
3.71k
        if (c[0].vertex.p.y > pfs->rect.q.y && c[3].vertex.p.y > pfs->rect.q.y)
804
0
        {
805
            /* Check 0+3 off the bottom. */
806
0
            v0 = 0;
807
0
            v1 = 3;
808
0
            goto check_rot_bottom;
809
0
        }
810
3.71k
        else if (c[1].vertex.p.y > pfs->rect.q.y && c[2].vertex.p.y > pfs->rect.q.y)
811
0
        {
812
            /* Check 1+2 off the bottom. */
813
0
            v0 = 1;
814
0
            v1 = 2;
815
0
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
0
            do
819
0
            {
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
0
                m0 = midpoint(c[0].vertex.p.y, c[1].vertex.p.y);
827
0
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
828
0
                    goto check_rot_bottom_quarter;
829
0
                m1 = midpoint(c[3].vertex.p.y, c[2].vertex.p.y);
830
0
                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
0
                c[v0].vertex.p.x = midpoint(c[0].vertex.p.x, c[1].vertex.p.x);
834
0
                c[v0].vertex.p.y = m0;
835
0
                c[v1].vertex.p.x = midpoint(c[3].vertex.p.x, c[2].vertex.p.x);
836
0
                c[v1].vertex.p.y = m1;
837
0
                c[v0].vertex.cc[0] = (c[0].vertex.cc[0] + c[1].vertex.cc[0])/2;
838
0
                c[v1].vertex.cc[0] = (c[3].vertex.cc[0] + c[2].vertex.cc[0])/2;
839
0
                changed = 1;
840
0
            }
841
0
            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
0
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
0
                do
848
0
                {
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
0
                    m0 = quarterpoint(c[v0].vertex.p.y, c[v0^1].vertex.p.y);
858
0
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
859
0
                        break;
860
0
                    m1 = quarterpoint(c[v1].vertex.p.y, c[v1^1].vertex.p.y);
861
0
                    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
0
                    c[v0].vertex.p.x = quarterpoint(c[v0].vertex.p.x, c[v0^1].vertex.p.x);
865
0
                    c[v0].vertex.p.y = m0;
866
0
                    c[v1].vertex.p.x = quarterpoint(c[v1].vertex.p.x, c[v1^1].vertex.p.x);
867
0
                    c[v1].vertex.p.y = m1;
868
0
                    c[v0].vertex.cc[0] = (c[v0].vertex.cc[0] + 3*c[v0^1].vertex.cc[0])/4;
869
0
                    c[v1].vertex.cc[0] = (c[v1].vertex.cc[0] + 3*c[v1^1].vertex.cc[0])/4;
870
0
                    changed = 1;
871
0
                }
872
0
                while (c[v0].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && c[v1].vertex.p.y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
873
0
            }
874
0
        }
875
3.71k
    } while (changed);
876
877
3.39k
    c[0].vertex.cc[1] = c[1].vertex.cc[1] =
878
3.39k
                        c[2].vertex.cc[1] =
879
3.39k
                        c[3].vertex.cc[1] = 0;
880
3.39k
    make_other_poles(c);
881
3.39k
    return patch_fill(pfs, c, NULL, NULL);
882
3.55k
}
883
#undef midpoint
884
#undef quarterpoint
885
#undef MIDPOINT_ACCURACY
886
#undef QUARTERPOINT_ACCURACY
887
888
0
#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
0
{
893
0
    corners_and_curves sub[4];
894
0
    int code;
895
896
0
    if (depth == 32)
897
0
        return gs_error_limitcheck;
898
899
0
    if (depth > 0 &&
900
0
        f_fits_in_fixed(cc->corners[0].x) &&
901
0
        f_fits_in_fixed(cc->corners[0].y) &&
902
0
        f_fits_in_fixed(cc->corners[1].x) &&
903
0
        f_fits_in_fixed(cc->corners[1].y) &&
904
0
        f_fits_in_fixed(cc->corners[2].x) &&
905
0
        f_fits_in_fixed(cc->corners[2].y) &&
906
0
        f_fits_in_fixed(cc->corners[3].x) &&
907
0
        f_fits_in_fixed(cc->corners[3].y))
908
0
    {
909
0
        cc->curve[0].vertex.p.x = float2fixed(cc->corners[0].x);
910
0
        cc->curve[0].vertex.p.y = float2fixed(cc->corners[0].y);
911
0
        cc->curve[1].vertex.p.x = float2fixed(cc->corners[1].x);
912
0
        cc->curve[1].vertex.p.y = float2fixed(cc->corners[1].y);
913
0
        cc->curve[2].vertex.p.x = float2fixed(cc->corners[2].x);
914
0
        cc->curve[2].vertex.p.y = float2fixed(cc->corners[2].y);
915
0
        cc->curve[3].vertex.p.x = float2fixed(cc->corners[3].x);
916
0
        cc->curve[3].vertex.p.y = float2fixed(cc->corners[3].y);
917
0
        return subdivide_patch_fill(pfs1, cc->curve);
918
0
    }
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
0
    sub[0].corners[0].x = cc->corners[0].x;
934
0
    sub[0].corners[0].y = cc->corners[0].y;
935
0
    sub[1].corners[1].x = cc->corners[1].x;
936
0
    sub[1].corners[1].y = cc->corners[1].y;
937
0
    sub[2].corners[2].x = cc->corners[2].x;
938
0
    sub[2].corners[2].y = cc->corners[2].y;
939
0
    sub[3].corners[3].x = cc->corners[3].x;
940
0
    sub[3].corners[3].y = cc->corners[3].y;
941
0
    sub[1].corners[0].x = sub[0].corners[1].x = (cc->corners[0].x + cc->corners[1].x)/2;
942
0
    sub[1].corners[0].y = sub[0].corners[1].y = (cc->corners[0].y + cc->corners[1].y)/2;
943
0
    sub[3].corners[2].x = sub[2].corners[3].x = (cc->corners[2].x + cc->corners[3].x)/2;
944
0
    sub[3].corners[2].y = sub[2].corners[3].y = (cc->corners[2].y + cc->corners[3].y)/2;
945
0
    sub[3].corners[0].x = sub[0].corners[3].x = (cc->corners[0].x + cc->corners[3].x)/2;
946
0
    sub[3].corners[0].y = sub[0].corners[3].y = (cc->corners[0].y + cc->corners[3].y)/2;
947
0
    sub[2].corners[1].x = sub[1].corners[2].x = (cc->corners[1].x + cc->corners[2].x)/2;
948
0
    sub[2].corners[1].y = sub[1].corners[2].y = (cc->corners[1].y + cc->corners[2].y)/2;
949
0
    sub[0].corners[2].x = sub[1].corners[3].x =
950
0
                          sub[2].corners[0].x =
951
0
                          sub[3].corners[1].x = (sub[0].corners[3].x + sub[1].corners[2].x)/2;
952
0
    sub[0].corners[2].y = sub[1].corners[3].y =
953
0
                          sub[2].corners[0].y =
954
0
                          sub[3].corners[1].y = (sub[0].corners[3].y + sub[1].corners[2].y)/2;
955
0
    sub[0].curve[0].vertex.cc[0] = sub[0].curve[3].vertex.cc[0] =
956
0
                                   sub[3].curve[0].vertex.cc[0] =
957
0
                                   sub[3].curve[3].vertex.cc[0] = cc->curve[0].vertex.cc[0];
958
0
    sub[1].curve[1].vertex.cc[0] = sub[1].curve[2].vertex.cc[0] =
959
0
                                   sub[2].curve[1].vertex.cc[0] =
960
0
                                   sub[2].curve[2].vertex.cc[0] = cc->curve[1].vertex.cc[0];
961
0
    sub[0].curve[1].vertex.cc[0] = sub[0].curve[2].vertex.cc[0] =
962
0
                                   sub[1].curve[0].vertex.cc[0] =
963
0
                                   sub[1].curve[3].vertex.cc[0] =
964
0
                                   sub[2].curve[0].vertex.cc[0] =
965
0
                                   sub[2].curve[3].vertex.cc[0] =
966
0
                                   sub[3].curve[1].vertex.cc[0] =
967
0
                                   sub[3].curve[2].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
968
969
0
    depth++;
970
0
    if (not_clipped_away(sub[0].corners, &pfs1->rect)) {
971
0
        code = A_fill_region_floats(pfs1, &sub[0], depth);
972
0
        if (code < 0)
973
0
            return code;
974
0
    }
975
0
    if (not_clipped_away(sub[1].corners, &pfs1->rect)) {
976
0
        code = A_fill_region_floats(pfs1, &sub[1], depth);
977
0
        if (code < 0)
978
0
            return code;
979
0
    }
980
0
    if (not_clipped_away(sub[2].corners, &pfs1->rect)) {
981
0
        code = A_fill_region_floats(pfs1, &sub[2], depth);
982
0
        if (code < 0)
983
0
            return code;
984
0
    }
985
0
    if (not_clipped_away(sub[3].corners, &pfs1->rect)) {
986
0
        code = A_fill_region_floats(pfs1, &sub[3], depth);
987
0
        if (code < 0)
988
0
            return code;
989
0
    }
990
991
0
    return 0;
992
0
}
993
994
0
#define midpoint(a,b)      ((a+b)/2)
995
996
0
#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
0
{
1001
0
    double m0, m1;
1002
0
    int v0, v1;
1003
0
    int changed;
1004
1005
0
    if (pfs->rect.p.x >= pfs->rect.q.x || pfs->rect.p.y >= pfs->rect.q.y)
1006
0
        return 0;
1007
1008
    /* On entry we have a patch:
1009
     *   c[0].vertex  c[1].vertex
1010
     *
1011
     *   c[3].vertex  c[2].vertex
1012
     *
1013
     * Only the corners are set. The control points are not!
1014
     *
1015
     * BUT... in terms of spacial coords, it might be different...
1016
     * They might be flipped on X, Y or both, giving:
1017
     *  01 or 10 or 32 or 23
1018
     *  32    23    01    10
1019
     * or they might be rotated, and then flipped on X, Y or both, giving:
1020
     *  03 or 30 or 12 or 21
1021
     *  12    21    03    30
1022
     */
1023
1024
    /* The +MIDPOINT_ACCURACY in the tests below is to allow for us finding the midpoint of [a] = z+1 and [b] = z, and getting z+1,
1025
     * and updating [a] to be z+1, hence never actually shrinking the gap. Just accept not culling the patch as
1026
     * much as we might. See bug 706378 for an example. */
1027
0
#define MIDPOINT_ACCURACY 0.0001
1028
0
#define QUARTERPOINT_ACCURACY 0.0003
1029
1030
0
    do {
1031
0
        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
0
        if ((cc->corners[0].x <= pfs->rect.p.x &&
1038
0
             cc->corners[1].x <= pfs->rect.p.x &&
1039
0
             cc->corners[2].x <= pfs->rect.p.x &&
1040
0
             cc->corners[3].x <= pfs->rect.p.x) ||
1041
0
            (cc->corners[0].x >= pfs->rect.q.x &&
1042
0
             cc->corners[1].x >= pfs->rect.q.x &&
1043
0
             cc->corners[2].x >= pfs->rect.q.x &&
1044
0
             cc->corners[3].x >= pfs->rect.q.x))
1045
0
                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
0
        if (cc->corners[0].x < pfs->rect.p.x && cc->corners[3].x < pfs->rect.p.x)
1051
0
        {
1052
            /* Check 0+3 off left. */
1053
0
            v0 = 0;
1054
0
            v1 = 3;
1055
0
            goto check_left;
1056
0
        }
1057
0
        else if (cc->corners[1].x < pfs->rect.p.x && cc->corners[2].x < pfs->rect.p.x)
1058
0
        {
1059
            /* Check 1+2 off left. */
1060
0
            v0 = 1;
1061
0
            v1 = 2;
1062
0
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
0
            do
1066
0
            {
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
0
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1073
0
                if (m0 >= pfs->rect.p.x)
1074
0
                    goto check_left_quarter;
1075
0
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1076
0
                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
0
                cc->corners[v0].x = m0;
1080
0
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1081
0
                cc->corners[v1].x = m1;
1082
0
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1083
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1084
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1085
0
                changed = 1;
1086
0
            }
1087
0
            while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1088
0
            if (0)
1089
0
            {
1090
0
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
0
                do
1094
0
                {
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
0
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1101
0
                    if (m0 >= pfs->rect.p.x)
1102
0
                        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
0
                while (cc->corners[v0].x < pfs->rect.p.x && cc->corners[v1].x < pfs->rect.p.x);
1116
0
            }
1117
0
        }
1118
1119
        /* or the right hand half? */
1120
0
        if (cc->corners[0].x > pfs->rect.q.x && cc->corners[3].x > pfs->rect.q.x)
1121
0
        {
1122
            /* Check 0+3 off right. */
1123
0
            v0 = 0;
1124
0
            v1 = 3;
1125
0
            goto check_right;
1126
0
        }
1127
0
        else if (cc->corners[1].x > pfs->rect.q.x && cc->corners[2].x > pfs->rect.q.x)
1128
0
        {
1129
            /* Check 1+2 off right. */
1130
0
            v0 = 1;
1131
0
            v1 = 2;
1132
0
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
0
            do
1136
0
            {
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
0
                m0 = midpoint(cc->corners[0].x, cc->corners[1].x);
1143
0
                if (m0 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1144
0
                    goto check_right_quarter;
1145
0
                m1 = midpoint(cc->corners[3].x, cc->corners[2].x);
1146
0
                if (m1 <= pfs->rect.q.x+MIDPOINT_ACCURACY)
1147
0
                    goto check_right_quarter;
1148
                /* So, we can completely discard the left hand half of the patch. */
1149
0
                cc->corners[v0].x = m0;
1150
0
                cc->corners[v0].y = midpoint(cc->corners[0].y, cc->corners[1].y);
1151
0
                cc->corners[v1].x = m1;
1152
0
                cc->corners[v1].y = midpoint(cc->corners[3].y, cc->corners[2].y);
1153
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1154
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1155
0
                changed = 1;
1156
0
            }
1157
0
            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
0
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
0
                do
1164
0
                {
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
0
                    m0 = quarterpoint(cc->corners[v0].x, cc->corners[v0^1].x);
1171
0
                    if (m0 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1172
0
                        break;
1173
0
                    m1 = quarterpoint(cc->corners[v1].x, cc->corners[v1^1].x);
1174
0
                    if (m1 <= pfs->rect.q.x+QUARTERPOINT_ACCURACY)
1175
0
                        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
0
                while (cc->corners[v0].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY && cc->corners[v1].x > pfs->rect.q.x+QUARTERPOINT_ACCURACY);
1186
0
            }
1187
0
        }
1188
1189
        /* Now, rotated cases: Can we cull the left hand half? */
1190
0
        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
0
        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
0
        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
0
        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
0
        if ((cc->corners[0].y <= pfs->rect.p.y &&
1335
0
             cc->corners[1].y <= pfs->rect.p.y &&
1336
0
             cc->corners[2].y <= pfs->rect.p.y &&
1337
0
             cc->corners[3].y <= pfs->rect.p.y) ||
1338
0
            (cc->corners[0].y >= pfs->rect.q.y &&
1339
0
             cc->corners[1].y >= pfs->rect.q.y &&
1340
0
             cc->corners[2].y >= pfs->rect.q.y &&
1341
0
             cc->corners[3].y >= pfs->rect.q.y))
1342
0
            return 0;
1343
1344
        /* Now, let's try to see if we can cull the patch vertically with the clipping
1345
         * rectangle. */
1346
        /* Non rotated cases first. Can we cull the top half? */
1347
0
        if (cc->corners[0].y < pfs->rect.p.y && cc->corners[1].y < pfs->rect.p.y)
1348
0
        {
1349
            /* Check 0+1 off above. */
1350
0
            v0 = 0;
1351
0
            v1 = 1;
1352
0
            goto check_above;
1353
0
        }
1354
0
        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
0
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
0
            do
1363
0
            {
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
0
                m0 = midpoint(cc->corners[0].y, cc->corners[3].y);
1371
0
                if (m0 >= pfs->rect.p.y)
1372
0
                    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
0
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1386
0
            if (0)
1387
0
            {
1388
0
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
0
                do
1392
0
                {
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
0
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^3].y);
1402
0
                    if (m0 >= pfs->rect.p.y)
1403
0
                        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
0
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1417
0
            }
1418
0
        }
1419
1420
        /* or the bottom half? */
1421
0
        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
0
        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
0
        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
0
        else if (cc->corners[1].y < pfs->rect.p.y && cc->corners[2].y < pfs->rect.p.y)
1503
0
        {
1504
            /* Check 1+2 off above. */
1505
0
            v0 = 1;
1506
0
            v1 = 2;
1507
0
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
0
            do
1511
0
            {
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
0
                m0 = midpoint(cc->corners[0].y, cc->corners[1].y);
1519
0
                if (m0 >= pfs->rect.p.y)
1520
0
                    goto check_rot_above_quarter;
1521
0
                m1 = midpoint(cc->corners[3].y, cc->corners[2].y);
1522
0
                if (m1 >= pfs->rect.p.y)
1523
0
                    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
0
            while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1534
0
            if (0)
1535
0
            {
1536
0
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
0
                do
1540
0
                {
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
0
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1550
0
                    if (m0 >= pfs->rect.p.y)
1551
0
                        break;
1552
0
                    m1 = quarterpoint(cc->corners[v1].y, cc->corners[v1^1].y);
1553
0
                    if (m1 >= pfs->rect.p.y)
1554
0
                        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
0
                while (cc->corners[v0].y < pfs->rect.p.y && cc->corners[v1].y < pfs->rect.p.y);
1565
0
            }
1566
0
        }
1567
1568
        /* or the bottom half? */
1569
0
        if (cc->corners[0].y > pfs->rect.q.y && cc->corners[3].y > pfs->rect.q.y)
1570
0
        {
1571
            /* Check 0+3 off the bottom. */
1572
0
            v0 = 0;
1573
0
            v1 = 3;
1574
0
            goto check_rot_bottom;
1575
0
        }
1576
0
        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
0
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
0
            do
1585
0
            {
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
0
                m0 = midpoint(cc->corners[0].y, cc->corners[1].y);
1593
0
                if (m0 <= pfs->rect.q.y+MIDPOINT_ACCURACY)
1594
0
                    goto check_rot_bottom_quarter;
1595
0
                m1 = midpoint(cc->corners[3].y, cc->corners[2].y);
1596
0
                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
0
                cc->corners[v0].x = midpoint(cc->corners[0].x, cc->corners[1].x);
1600
0
                cc->corners[v0].y = m0;
1601
0
                cc->corners[v1].x = midpoint(cc->corners[3].x, cc->corners[2].x);
1602
0
                cc->corners[v1].y = m1;
1603
0
                cc->curve[v0].vertex.cc[0] = (cc->curve[0].vertex.cc[0] + cc->curve[1].vertex.cc[0])/2;
1604
0
                cc->curve[v1].vertex.cc[0] = (cc->curve[3].vertex.cc[0] + cc->curve[2].vertex.cc[0])/2;
1605
0
                changed = 1;
1606
0
            }
1607
0
            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
0
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
0
                do
1614
0
                {
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
0
                    m0 = quarterpoint(cc->corners[v0].y, cc->corners[v0^1].y);
1624
0
                    if (m0 <= pfs->rect.q.y+QUARTERPOINT_ACCURACY)
1625
0
                        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
0
                while (cc->corners[v0].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY && cc->corners[v1].y > pfs->rect.q.y+QUARTERPOINT_ACCURACY);
1639
0
            }
1640
0
        }
1641
0
    } while (changed);
1642
1643
0
    return A_fill_region_floats(pfs, cc, 0);
1644
0
}
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
3.56k
{
1653
3.56k
    const gs_shading_A_t * const psh = pfs->psh;
1654
3.56k
    double x0 = psh->params.Coords[0] + pfs->delta.x * pfs->v0;
1655
3.56k
    double y0 = psh->params.Coords[1] + pfs->delta.y * pfs->v0;
1656
3.56k
    double x1 = psh->params.Coords[0] + pfs->delta.x * pfs->v1;
1657
3.56k
    double y1 = psh->params.Coords[1] + pfs->delta.y * pfs->v1;
1658
3.56k
    double h0 = pfs->u0, h1 = pfs->u1;
1659
3.56k
    corners_and_curves cc;
1660
3.56k
    int code;
1661
1662
3.56k
    double dx0 = pfs->delta.x * h0;
1663
3.56k
    double dy0 = pfs->delta.y * h0;
1664
3.56k
    double dx1 = pfs->delta.x * h1;
1665
3.56k
    double dy1 = pfs->delta.y * h1;
1666
1667
3.56k
    cc.curve[0].vertex.cc[0] = pfs->t0; /* The element cc[1] is set to a dummy value against */
1668
3.56k
    cc.curve[1].vertex.cc[0] = pfs->t1; /* interrupts while an idle processing in gxshade6.c .  */
1669
3.56k
    cc.curve[2].vertex.cc[0] = pfs->t1;
1670
3.56k
    cc.curve[3].vertex.cc[0] = pfs->t0;
1671
3.56k
    cc.corners[0].x = x0 + dy0;
1672
3.56k
    cc.corners[0].y = y0 - dx0;
1673
3.56k
    cc.corners[1].x = x1 + dy0;
1674
3.56k
    cc.corners[1].y = y1 - dx0;
1675
3.56k
    cc.corners[2].x = x1 + dy1;
1676
3.56k
    cc.corners[2].y = y1 - dx1;
1677
3.56k
    cc.corners[3].x = x0 + dy1;
1678
3.56k
    cc.corners[3].y = y0 - dx1;
1679
3.56k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[0].x, cc.corners[0].y, &cc.curve[0].vertex.p);
1680
3.56k
    if (code < 0)
1681
0
        goto fail;
1682
3.56k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[1].x, cc.corners[1].y, &cc.curve[1].vertex.p);
1683
3.56k
    if (code < 0)
1684
0
        goto fail;
1685
3.56k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[2].x, cc.corners[2].y, &cc.curve[2].vertex.p);
1686
3.56k
    if (code < 0)
1687
0
        goto fail;
1688
3.56k
    code = gs_point_transform2fixed(&pfs1->pgs->ctm, cc.corners[3].x, cc.corners[3].y, &cc.curve[3].vertex.p);
1689
3.56k
    if (code < 0)
1690
0
        goto fail;
1691
3.56k
    return subdivide_patch_fill(pfs1, cc.curve);
1692
0
fail:
1693
0
    if (code != gs_error_limitcheck)
1694
0
        return code;
1695
0
    code = gs_point_transform(cc.corners[0].x, cc.corners[0].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[0]);
1696
0
    if (code < 0)
1697
0
        return code;
1698
0
    code = gs_point_transform(cc.corners[1].x, cc.corners[1].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[1]);
1699
0
    if (code < 0)
1700
0
        return code;
1701
0
    code = gs_point_transform(cc.corners[2].x, cc.corners[2].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[2]);
1702
0
    if (code < 0)
1703
0
        return code;
1704
0
    code = gs_point_transform(cc.corners[3].x, cc.corners[3].y, (const gs_matrix *)&pfs1->pgs->ctm, &cc.corners[3]);
1705
0
    if (code < 0)
1706
0
        return code;
1707
0
    return subdivide_patch_fill_floats(pfs1, &cc);
1708
0
}
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
1.75k
{
1715
1.75k
    const gs_shading_A_t *const psh = (const gs_shading_A_t *)psh0;
1716
1.75k
    gs_function_t * const pfn = psh->params.Function;
1717
1.75k
    gs_matrix cmat;
1718
1.75k
    gs_rect t_rect;
1719
1.75k
    A_fill_state_t state;
1720
1.75k
    patch_fill_state_t pfs1;
1721
1.75k
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
1722
1.75k
    float dd = d1 - d0;
1723
1.75k
    double t0, t1;
1724
1.75k
    gs_point dist;
1725
1.75k
    int code;
1726
1727
1.75k
    state.psh = psh;
1728
1.75k
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
1729
1.75k
    if (code < 0)
1730
2
        return code;
1731
1.75k
    pfs1.Function = pfn;
1732
1.75k
    pfs1.rect = *clip_rect;
1733
1.75k
    code = init_patch_fill_state(&pfs1);
1734
1.75k
    if (code < 0)
1735
0
        goto fail;
1736
1.75k
    pfs1.maybe_self_intersecting = false;
1737
1.75k
    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
1.75k
    cmat.tx = psh->params.Coords[0];
1744
1.75k
    cmat.ty = psh->params.Coords[1];
1745
1.75k
    state.delta.x = psh->params.Coords[2] - psh->params.Coords[0];
1746
1.75k
    state.delta.y = psh->params.Coords[3] - psh->params.Coords[1];
1747
1.75k
    cmat.yx = state.delta.x;
1748
1.75k
    cmat.yy = state.delta.y;
1749
1.75k
    cmat.xx = cmat.yy;
1750
1.75k
    cmat.xy = -cmat.yx;
1751
1.75k
    code = gs_bbox_transform_inverse(rect, &cmat, &t_rect);
1752
1.75k
    if (code < 0) {
1753
0
        code = 0; /* Swallow this silently */
1754
0
        goto fail;
1755
0
    }
1756
1.75k
    t0 = min(max(t_rect.p.y, 0), 1);
1757
1.75k
    t1 = max(min(t_rect.q.y, 1), 0);
1758
1.75k
    state.v0 = t0;
1759
1.75k
    state.v1 = t1;
1760
1.75k
    state.u0 = t_rect.p.x;
1761
1.75k
    state.u1 = t_rect.q.x;
1762
1.75k
    state.t0 = t0 * dd + d0;
1763
1.75k
    state.t1 = t1 * dd + d0;
1764
1.75k
    code = gs_distance_transform(state.delta.x, state.delta.y, &ctm_only(pgs),
1765
1.75k
                          &dist);
1766
1.75k
    if (code < 0)
1767
0
        goto fail;
1768
1.75k
    state.length = hypot(dist.x, dist.y); /* device space line length */
1769
1.75k
    code = A_fill_region(&state, &pfs1);
1770
1.75k
    if (psh->params.Extend[0] && t0 > t_rect.p.y) {
1771
909
        if (code < 0)
1772
0
            goto fail;
1773
        /* Use the general algorithm, because we need the trapping. */
1774
909
        state.v0 = t_rect.p.y;
1775
909
        state.v1 = t0;
1776
909
        state.t0 = state.t1 = t0 * dd + d0;
1777
909
        code = A_fill_region(&state, &pfs1);
1778
909
    }
1779
1.75k
    if (psh->params.Extend[1] && t1 < t_rect.q.y) {
1780
902
        if (code < 0)
1781
0
            goto fail;
1782
        /* Use the general algorithm, because we need the trapping. */
1783
902
        state.v0 = t1;
1784
902
        state.v1 = t_rect.q.y;
1785
902
        state.t0 = state.t1 = t1 * dd + d0;
1786
902
        code = A_fill_region(&state, &pfs1);
1787
902
    }
1788
1.75k
fail:
1789
1.75k
    gsicc_release_link(pfs1.icclink);
1790
1.75k
    if (term_patch_fill_state(&pfs1))
1791
0
        return_error(gs_error_unregistered); /* Must not happen. */
1792
1.75k
    return code;
1793
1.75k
}
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
1.75k
{
1800
1.75k
    return gs_shading_A_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
1801
1.75k
}
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
20
{
1860
20
    double dx = x1 - x0, dy = y1 - y0;
1861
20
    double d = hypot(dx, dy);
1862
20
    gs_point p0, p1, pc0, pc1;
1863
20
    int k, j, code, dirn;
1864
20
    bool inside = 0;
1865
1866
    /* pc0 and pc1 are the centres of the respective circles. */
1867
20
    pc0.x = x0, pc0.y = y0;
1868
20
    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
20
    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
20
        p0.x = 0, p0.y = -1, dirn = 0;
1876
        /* Align stripes along radii for faster triangulation : */
1877
20
        inside = 1;
1878
20
        pfs->function_arg_shift = 1;
1879
20
    } else {
1880
        /* Must generate canonic quadrangle arcs,
1881
           because we approximate them with curves. */
1882
0
        if(dx >= 0) {
1883
0
            if (dy >= 0)
1884
0
                p0.x = 1, p0.y = 0, dirn = (dx >= dy ? 1 : 0);
1885
0
            else
1886
0
                p0.x = 0, p0.y = -1, dirn = (dx >= -dy ? 0 : 1);
1887
0
        } else {
1888
0
            if (dy >= 0)
1889
0
                p0.x = 0, p0.y = 1, dirn = (-dx >= dy ? 1 : 0);
1890
0
            else
1891
0
                p0.x = -1, p0.y = 0, dirn = (-dx >= -dy ? 0 : 1);
1892
0
        }
1893
0
        pfs->function_arg_shift = 0;
1894
0
    }
1895
    /* fixme: wish: cut invisible parts off.
1896
       Note : when r0 != r1 the invisible part is not a half circle. */
1897
100
    for (k = 0; k < 4; k++) {
1898
80
        gs_point p[12];
1899
80
        patch_curve_t curve[4];
1900
1901
        /* Set p1 to be 90 degrees anticlockwise from p0 */
1902
80
        p1.x = -p0.y; p1.y = p0.x;
1903
80
        if (dirn == 0) { /* Clockwise */
1904
60
            make_quadrant_arc(p + 0, &pc0, &p1, &p0, r0);
1905
60
            make_quadrant_arc(p + 6, &pc1, &p0, &p1, r1);
1906
60
        } else { /* Anticlockwise */
1907
20
            make_quadrant_arc(p + 0, &pc0, &p0, &p1, r0);
1908
20
            make_quadrant_arc(p + 6, &pc1, &p1, &p0, r1);
1909
20
        }
1910
80
        p[4].x = (p[3].x * 2 + p[6].x) / 3;
1911
80
        p[4].y = (p[3].y * 2 + p[6].y) / 3;
1912
80
        p[5].x = (p[3].x + p[6].x * 2) / 3;
1913
80
        p[5].y = (p[3].y + p[6].y * 2) / 3;
1914
80
        p[10].x = (p[9].x * 2 + p[0].x) / 3;
1915
80
        p[10].y = (p[9].y * 2 + p[0].y) / 3;
1916
80
        p[11].x = (p[9].x + p[0].x * 2) / 3;
1917
80
        p[11].y = (p[9].y + p[0].y * 2) / 3;
1918
400
        for (j = 0; j < 4; j++) {
1919
320
            int jj = (j + inside) % 4;
1920
1921
320
            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
320
            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
320
            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
320
            curve[j].straight = (((j + inside) & 1) != 0);
1930
320
        }
1931
80
        curve[(0 + inside) % 4].vertex.cc[0] = t0;
1932
80
        curve[(1 + inside) % 4].vertex.cc[0] = t0;
1933
80
        curve[(2 + inside) % 4].vertex.cc[0] = t1;
1934
80
        curve[(3 + inside) % 4].vertex.cc[0] = t1;
1935
80
        curve[0].vertex.cc[1] = curve[1].vertex.cc[1] = 0; /* Initialize against FPE. */
1936
80
        curve[2].vertex.cc[1] = curve[3].vertex.cc[1] = 0; /* Initialize against FPE. */
1937
80
        code = patch_fill(pfs, curve, NULL, NULL);
1938
80
        if (code < 0)
1939
0
            return code;
1940
        /* Move p0 to be ready for the next position */
1941
80
        if (k == 0) {
1942
            /* p0 moves clockwise */
1943
20
            p1 = p0;
1944
20
            p0.x = p1.y; p0.y = -p1.x;
1945
20
            dirn = 0;
1946
60
        } else if (k == 1) {
1947
            /* p0 flips sides */
1948
20
            p0.x = -p0.x; p0.y = -p0.y;
1949
20
            dirn = 1;
1950
40
        } else if (k == 2) {
1951
            /* p0 moves anti-clockwise */
1952
20
            p1 = p0;
1953
20
            p0.x = -p1.y; p0.y = p1.x;
1954
20
            dirn = 0;
1955
20
        }
1956
80
    }
1957
20
    return 0;
1958
20
}
1959
1960
/* Find the control points for two points on the arc of a circle
1961
 * the points must be within the same quadrant.
1962
 */
1963
static int find_arc_control_points(gs_point *from, gs_point *to, gs_point *from_control, gs_point *to_control, gs_point *centre)
1964
0
{
1965
0
    double from_tan_alpha, to_tan_alpha, from_alpha, to_alpha;
1966
0
    double half_inscribed_angle, intersect_x, intersect_y, intersect_dist;
1967
0
    double radius = sqrt(((from->x - centre->x) * (from->x - centre->x)) + ((from->y - centre->y) * (from->y - centre->y)));
1968
0
    double tangent_intersect_dist;
1969
0
    double F;
1970
0
    int quadrant;
1971
1972
    /* Quadrant 0 is upper right, numbered anti-clockwise.
1973
     * If the direction of the from->to is atni-clockwise, add 4
1974
     */
1975
0
    if (from->x > to->x) {
1976
0
        if (from->y > to->y) {
1977
0
            if (to->y >= centre->y)
1978
0
                quadrant = 1 + 4;
1979
0
            else
1980
0
                quadrant = 3;
1981
0
        } else {
1982
0
            if (to->x >= centre->x)
1983
0
                quadrant = 0 + 4;
1984
0
            else
1985
0
                quadrant = 2;
1986
0
        }
1987
0
    } else {
1988
0
        if (from->y > to->y) {
1989
0
            if (from->x >= centre->x)
1990
0
                quadrant = 0;
1991
0
            else
1992
0
                quadrant = 2 + 4;
1993
0
        } else {
1994
0
            if (from->x >= centre->x)
1995
0
                quadrant = 3 + 4;
1996
0
            else
1997
0
                quadrant = 1;
1998
0
        }
1999
0
    }
2000
2001
0
    switch(quadrant) {
2002
        /* quadrant 0, arc goes clockwise */
2003
0
        case 0:
2004
0
            if (from->x == centre->x) {
2005
0
                from_alpha = M_PI / 2;
2006
0
            } else {
2007
0
                from_tan_alpha = (from->y - centre->y) / (from->x - centre->x);
2008
0
                from_alpha = atan(from_tan_alpha);
2009
0
            }
2010
0
            to_tan_alpha = (to->y - centre->y) / (to->x - centre->x);
2011
0
            to_alpha = atan(to_tan_alpha);
2012
2013
0
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2014
0
            intersect_dist = radius / cos(half_inscribed_angle);
2015
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2016
2017
0
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2018
0
            intersect_y = centre->y + sin(to_alpha + half_inscribed_angle) * intersect_dist;
2019
0
            break;
2020
        /* quadrant 1, arc goes clockwise */
2021
0
        case 1:
2022
0
            from_tan_alpha = (from->y - centre->y) / (centre->x - from->x);
2023
0
            from_alpha = atan(from_tan_alpha);
2024
2025
0
            if (to->x == centre->x) {
2026
0
                to_alpha = M_PI / 2;
2027
0
            } else {
2028
0
                to_tan_alpha = (to->y - centre->y) / (centre->x - to->x);
2029
0
                to_alpha = atan(to_tan_alpha);
2030
0
            }
2031
2032
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2033
0
            intersect_dist = radius / cos(half_inscribed_angle);
2034
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2035
2036
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2037
0
            intersect_y = centre->y + sin(from_alpha + half_inscribed_angle) * intersect_dist;
2038
0
            break;
2039
        /* quadrant 2, arc goes clockwise */
2040
0
        case 2:
2041
0
            if (from->x == centre->x) {
2042
0
                from_alpha = M_PI / 2;
2043
0
            } else {
2044
0
                from_tan_alpha = (centre->y - from->y) / (centre->x - from->x);
2045
0
                from_alpha = atan(from_tan_alpha);
2046
0
            }
2047
2048
0
            to_tan_alpha = (centre->y - to->y) / (centre->x - to->x);
2049
0
            to_alpha = atan(to_tan_alpha);
2050
2051
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2052
0
            intersect_dist = radius / cos(half_inscribed_angle);
2053
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2054
2055
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2056
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2057
0
            break;
2058
        /* quadrant 3, arc goes clockwise */
2059
0
        case 3:
2060
0
            from_tan_alpha = (centre->y - from->y) / (from->x - centre->x);
2061
0
            from_alpha = atan(from_tan_alpha);
2062
2063
0
            if (to->x == centre->x) {
2064
0
                to_alpha = M_PI / 2;
2065
0
            } else {
2066
0
                to_tan_alpha = (centre->y - to->y) / (to->x - centre->x);
2067
0
                to_alpha = atan(to_tan_alpha);
2068
0
            }
2069
2070
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2071
0
            intersect_dist = radius / cos(half_inscribed_angle);
2072
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2073
2074
0
            intersect_x = centre->x + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2075
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2076
0
            break;
2077
        /* quadrant 0, arc goes anti-clockwise */
2078
0
        case 4:
2079
0
            from_tan_alpha = (from->y - centre->y) / (from->x - centre->x);
2080
0
            from_alpha = atan(from_tan_alpha);
2081
2082
0
            if (to->y == centre->y)
2083
0
                to_alpha = M_PI / 2;
2084
0
            else {
2085
0
                to_tan_alpha = (to->y - centre->y) / (to->x - centre->x);
2086
0
                to_alpha = atan(to_tan_alpha);
2087
0
            }
2088
2089
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2090
0
            intersect_dist = radius / cos(half_inscribed_angle);
2091
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2092
2093
0
            intersect_x = centre->x + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2094
0
            intersect_y = centre->y + sin(from_alpha + half_inscribed_angle) * intersect_dist;
2095
0
            break;
2096
        /* quadrant 1, arc goes anti-clockwise */
2097
0
        case 5:
2098
0
            from_tan_alpha = (centre->x - from->x) / (from->y - centre->y);
2099
0
            from_alpha = atan(from_tan_alpha);
2100
2101
0
            if (to->y == centre->y) {
2102
0
                to_alpha = M_PI / 2;
2103
0
            }
2104
0
            else {
2105
0
                to_tan_alpha = (centre->x - to->x) / (to->y - centre->y);
2106
0
                to_alpha = atan(to_tan_alpha);
2107
0
            }
2108
2109
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2110
0
            intersect_dist = radius / cos(half_inscribed_angle);
2111
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2112
2113
0
            intersect_x = centre->x - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2114
0
            intersect_y = centre->y + cos(from_alpha + half_inscribed_angle) * intersect_dist;
2115
0
            break;
2116
        /* quadrant 2, arc goes anti-clockwise */
2117
0
        case 6:
2118
0
            from_tan_alpha = (from->y - centre->y) / (centre->x - from->x);
2119
0
            from_alpha = atan(from_tan_alpha);
2120
2121
0
            if (to->x == centre->x) {
2122
0
                to_alpha = M_PI / 2;
2123
0
            } else {
2124
0
                to_tan_alpha = (centre->y - to->y) / (centre->x - to->x);
2125
0
                to_alpha = atan(to_tan_alpha);
2126
0
            }
2127
2128
0
            half_inscribed_angle = (to_alpha - from_alpha) / 2;
2129
0
            intersect_dist = radius / cos(half_inscribed_angle);
2130
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2131
2132
0
            intersect_x = centre->x - cos(from_alpha + half_inscribed_angle) * intersect_dist;
2133
0
            intersect_y = centre->y - sin(from_alpha + half_inscribed_angle) * intersect_dist;
2134
0
            break;
2135
        /* quadrant 3, arc goes anti-clockwise */
2136
0
        case 7:
2137
0
            if (from->x == centre->x) {
2138
0
                from_alpha = M_PI / 2;
2139
0
            } else {
2140
0
                from_tan_alpha = (centre->y - from->y) / (from->x - centre->x);
2141
0
                from_alpha = atan(from_tan_alpha);
2142
0
            }
2143
0
            to_tan_alpha = (centre->y - to->y) / (to->x - centre->x);
2144
0
            to_alpha = atan(to_tan_alpha);
2145
2146
0
            half_inscribed_angle = (from_alpha - to_alpha) / 2;
2147
0
            intersect_dist = radius / cos(half_inscribed_angle);
2148
0
            tangent_intersect_dist = tan(half_inscribed_angle) * radius;
2149
2150
0
            intersect_x = centre->x + cos(to_alpha + half_inscribed_angle) * intersect_dist;
2151
0
            intersect_y = centre->y - sin(to_alpha + half_inscribed_angle) * intersect_dist;
2152
0
            break;
2153
0
    }
2154
2155
0
    F = (4.0 / 3.0) / (1 + sqrt(1 + ((tangent_intersect_dist / radius) * (tangent_intersect_dist / radius))));
2156
2157
0
    from_control->x = from->x - ((from->x - intersect_x) * F);
2158
0
    from_control->y = from->y - ((from->y - intersect_y) * F);
2159
0
    to_control->x = to->x - ((to->x - intersect_x) * F);
2160
0
    to_control->y = to->y - ((to->y - intersect_y) * F);
2161
2162
0
    return 0;
2163
0
}
2164
2165
/* Create a 'patch_curve' element whch is a straight line between two points */
2166
static int patch_lineto(gs_matrix_fixed *ctm, gs_point *from, gs_point *to, patch_curve_t *p, float t)
2167
0
{
2168
0
    double x_1third, x_2third, y_1third, y_2third;
2169
2170
0
    x_1third = (to->x - from->x) / 3;
2171
0
    x_2third = x_1third * 2;
2172
0
    y_1third = (to->y - from->y) / 3;
2173
0
    y_2third = y_1third * 2;
2174
2175
0
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2176
0
    gs_point_transform2fixed(ctm, from->x + x_1third, from->y + y_1third, &p->control[0]);
2177
0
    gs_point_transform2fixed(ctm, from->x + x_2third, from->y + y_2third, &p->control[1]);
2178
2179
0
    p->vertex.cc[0] = t;
2180
0
    p->vertex.cc[1] = t;
2181
0
    p->straight = 1;
2182
2183
0
    return 0;
2184
0
}
2185
2186
static int patch_curveto(gs_matrix_fixed *ctm, gs_point *centre, gs_point *from, gs_point *to, patch_curve_t *p, float t)
2187
0
{
2188
0
    gs_point from_control, to_control;
2189
2190
0
    find_arc_control_points(from, to, &from_control, &to_control, centre);
2191
2192
0
    gs_point_transform2fixed(ctm, from->x, from->y, &p->vertex.p);
2193
0
    gs_point_transform2fixed(ctm, from_control.x, from_control.y, &p->control[0]);
2194
0
    gs_point_transform2fixed(ctm, to_control.x, to_control.y, &p->control[1]);
2195
0
    p->vertex.cc[0] = t;
2196
0
    p->vertex.cc[1] = t;
2197
0
    p->straight = 0;
2198
2199
0
    return 0;
2200
0
}
2201
2202
static int draw_quarter_annulus(patch_fill_state_t *pfs, gs_point *centre, double radius, gs_point *corner, float t)
2203
0
{
2204
0
    gs_point p0, p1, initial;
2205
0
    patch_curve_t p[4];
2206
0
    int code;
2207
2208
0
    if (corner->x > centre->x) {
2209
0
        initial.x = centre->x + radius;
2210
0
    }
2211
0
    else {
2212
0
        initial.x = centre->x - radius;
2213
0
    }
2214
0
    initial.y = centre->y;
2215
2216
0
    p1.x = initial.x;
2217
0
    p1.y = corner->y;
2218
0
    patch_lineto(&pfs->pgs->ctm, &initial, &p1, &p[0], t);
2219
0
    p0.x = centre->x;
2220
0
    p0.y = p1.y;
2221
0
    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &p[1], t);
2222
0
    p1.x = centre->x;
2223
0
    if (centre->y > corner->y) {
2224
0
        p1.y = centre->y - radius;
2225
0
    } else {
2226
0
        p1.y = centre->y + radius;
2227
0
    }
2228
0
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2229
0
    patch_curveto(&pfs->pgs->ctm, centre, &p1, &initial, &p[3], t);
2230
0
    code = patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL);
2231
0
    if (code < 0)
2232
0
        return code;
2233
2234
0
    if (corner->x > centre->x)
2235
0
        initial.x = corner->x - (corner->x - (centre->x + radius));
2236
0
    else
2237
0
        initial.x = centre->x - radius;
2238
0
    initial.y = corner->y;
2239
0
    patch_lineto(&pfs->pgs->ctm, corner, &initial, &p[0], t);
2240
2241
0
    p0.x = initial.x;
2242
0
    p0.y = centre->y;
2243
0
    patch_lineto(&pfs->pgs->ctm, &initial, &p0, &p[1], t);
2244
2245
0
    p1.y = p0.y;
2246
0
    p1.x = corner->x;
2247
0
    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &p[2], t);
2248
0
    patch_lineto(&pfs->pgs->ctm, &p1, corner, &p[3], t);
2249
2250
0
    return (patch_fill(pfs, (const patch_curve_t *)&p, NULL, NULL));
2251
0
}
2252
2253
static int R_tensor_annulus_extend_tangent(patch_fill_state_t *pfs,
2254
    double x0, double y0, double r0, double t0,
2255
    double x1, double y1, double r1, double t1, double r2)
2256
0
{
2257
0
    patch_curve_t curve[4];
2258
0
    gs_point p0, p1;
2259
0
    int code = 0, q = 0;
2260
2261
    /* special case axis aligned circles. Its quicker to handle these specially as it
2262
     * avoid lots of trigonometry in the general case code, and avoids us
2263
     * having to watch out for infinity as the result of tan() operations.
2264
     */
2265
0
    if (x0 == x1 || y0 == y1) {
2266
0
        if (x0 == x1 && y0 > y1) {
2267
            /* tangent at top of circles */
2268
0
            p0.x = x1, p0.y = y1;
2269
0
            p1.x = x1 + r2, p1.y = y1 - r2;
2270
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2271
0
            p1.x = x1 - r2, p1.y = y1 - r2;
2272
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2273
0
            p1.x = x1 + r2, p1.y = y1 + r1;
2274
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2275
0
            p1.x = x1 - r2, p1.y = y1 + r1;
2276
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2277
0
        }
2278
0
        if (x0 == x1 && y0 < y1) {
2279
            /* tangent at bottom of circles */
2280
0
            p0.x = x1, p0.y = y1;
2281
0
            p1.x = x1 + r2, p1.y = y1 + r2;
2282
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2283
0
            p1.x = x1 - r2, p1.y = y1 + r2;
2284
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2285
0
            p1.x = x1 + r2, p1.y = y1 - r1;
2286
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2287
0
            p1.x = x1 - r2, p1.y = y1 - r1;
2288
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2289
0
        }
2290
0
        if (y0 == y1 && x0 > x1) {
2291
            /* tangent at right of circles */
2292
0
            p0.x = x1, p0.y = y1;
2293
0
            p1.x = x1 - r2, p1.y = y1 - r2;
2294
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2295
0
            p1.x = x1 - r2, p1.y = y1 + r2;
2296
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2297
0
            p1.x = x1 + r1, p1.y = y1 + r2;
2298
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2299
0
            p1.x = x1 + r1, p1.y = y1 - r2;
2300
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2301
0
        }
2302
0
        if (y0 == y1 && x0 < x1) {
2303
            /* tangent at left of circles */
2304
0
            p0.x = x1, p0.y = y1;
2305
0
            p1.x = x1 + r2, p1.y = y1 - r2;
2306
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2307
0
            p1.x = x1 + r2, p1.y = y1 + r2;
2308
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2309
0
            p1.x = x1 - r1, p1.y = y1 + r2;
2310
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2311
0
            p1.x = x1 - r1, p1.y = y1 - r2;
2312
0
            draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2313
0
        }
2314
0
    }
2315
0
    else {
2316
0
        double tx, ty, endx, endy, intersectx, intersecty, alpha, sinalpha, cosalpha, tanalpha;
2317
0
        gs_point centre;
2318
2319
        /* First lets figure out which quadrant the smaller circle is in (we always
2320
         * get called to fill from the larger circle), x0, y0, r0 is the smaller circle.
2321
         */
2322
0
        if (x0 < x1) {
2323
0
            if (y0 < y1)
2324
0
                q = 2;
2325
0
            else
2326
0
                q = 1;
2327
0
        } else {
2328
0
            if (y0 < y1)
2329
0
                q = 3;
2330
0
            else
2331
0
                q = 0;
2332
0
        }
2333
0
        switch(q) {
2334
0
            case 0:
2335
                /* We have two four-sided elements, from the tangent point
2336
                 * each side, to the point where the tangent crosses an
2337
                 * axis of the larger circle. A line back to the edge
2338
                 * of the larger circle, a line to the point where an axis
2339
                 * crosses the smaller circle, then an arc back to the starting point.
2340
                 */
2341
                /* Figure out the tangent point */
2342
                /* sin (angle) = y1 - y0 / r1 - r0
2343
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2344
                 */
2345
0
                ty = y1 + ((y0 - y1) / (r1 - r0)) * r1;
2346
0
                tx = x1 + ((x0 - x1) / (r1 - r0)) * r1;
2347
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2348
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2349
                 * as its the same angle where it crosses the axis of the larger circle.
2350
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2351
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2352
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2353
                 * circle
2354
                 */
2355
0
                sinalpha = (y0 - y1) / (r1 - r0);
2356
0
                alpha = asin(sinalpha);
2357
0
                cosalpha = cos(alpha);
2358
0
                intersectx = x1 + (r1 / cosalpha);
2359
0
                intersecty = y1;
2360
2361
0
                p0.x = tx, p0.y = ty;
2362
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty - (ty - intersecty) / 2;
2363
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2364
0
                p0.x = intersectx, p0.y = intersecty;
2365
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2366
0
                p1.x = x1 + r1, p1.y = y1;
2367
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2368
0
                p0.x = tx, p0.y = ty;
2369
0
                centre.x = x1, centre.y = y1;
2370
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2371
0
                code = patch_fill(pfs, curve, NULL, NULL);
2372
0
                if (code < 0)
2373
0
                    return code;
2374
2375
0
                if (intersectx < x1 + r2) {
2376
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2377
                     * An 'annulus' where the right edge is less than the normal extent and a
2378
                     * quad which is a rectangle with one corner chopped of at an angle.
2379
                     */
2380
0
                    p0.x = x1, p0.y = y1;
2381
0
                    p1.x = intersectx, p1.y = y1 - r2;
2382
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2383
0
                    endx = x1 + r2;
2384
0
                    endy = y1 - (tan ((M_PI / 2) - alpha)) * (endx - intersectx);
2385
0
                    p0.x = intersectx, p0.y = y1;
2386
0
                    p1.x = x1 + r2, p1.y = endy;
2387
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2388
0
                    p0.x = x1 + r2, p0.y = y0 - r2;
2389
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2390
0
                    p1.x = intersectx, p1.y = p0.y;
2391
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2392
0
                    p0.x = intersectx, p0.y = y1;
2393
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2394
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2395
0
                    if (code < 0)
2396
0
                        return code;
2397
2398
0
                } else {
2399
                    /* Quadrant 3 is a normal quarter annulua */
2400
0
                    p0.x = x1, p0.y = y1;
2401
0
                    p1.x = x1 + r2, p1.y = y1 - r2;
2402
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2403
0
                }
2404
2405
                /* Q2 is always a full annulus... */
2406
0
                p0.x = x1, p0.y = y1;
2407
0
                p1.x = x1 - r2, p1.y = y1 - r2;
2408
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2409
2410
                /* alpha is now the angle between the x axis and the tangent to the
2411
                 * circles.
2412
                 */
2413
0
                alpha = (M_PI / 2) - alpha;
2414
0
                cosalpha = cos(alpha);
2415
0
                endy = y1 + (r1 / cosalpha);
2416
0
                endx = x1;
2417
2418
0
                p0.x = tx, p0.y = ty;
2419
0
                p1.x = endx - ((endx - tx) / 2), p1.y = endy - ((endy - ty) / 2);
2420
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2421
0
                p0.x = endx, p0.y = endy;
2422
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2423
0
                p1.x = x1, p1.y = y1 + r1;
2424
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2425
0
                p0.x = tx, p0.y = ty;
2426
0
                centre.x = x1, centre.y = y1;
2427
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2428
0
                code = patch_fill(pfs, curve, NULL, NULL);
2429
0
                if (code < 0)
2430
0
                    return code;
2431
2432
                /* Q1 is simimlar to Q3, either a full quarter annulus
2433
                 * or a partial one, depending on where the tangent crosses
2434
                 * the y axis
2435
                 */
2436
0
                tanalpha = tan(alpha);
2437
0
                intersecty = y1 + tanalpha * (r2 + (intersectx - x1));
2438
0
                intersectx = x1 - r2;
2439
2440
0
                if (endy < y1 + r2) {
2441
                    /* didn't get all the way to the edge, quadrant 1 is composed of 2 quads :-(
2442
                     * An 'annulus' where the right edge is less than the normal extent and a
2443
                     * quad which is a rectangle with one corner chopped of at an angle.
2444
                     */
2445
0
                    p0.x = x1, p0.y = y1;
2446
0
                    p1.x = x1 - r2, p1.y = endy;
2447
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2448
0
                    p0.x = x1, p0.y = y1 + r1;
2449
0
                    p1.x = x1, p1.y = endy;
2450
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2451
0
                    p0.x = x1 - r2, p0.y = intersecty;
2452
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2453
0
                    p1.x = p0.x, p1.y = y1 + r1;
2454
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2455
0
                    p0.x = x1, p0.y = y1 + r1;
2456
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2457
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2458
0
                    if (code < 0)
2459
0
                        return code;
2460
0
                } else {
2461
                    /* Quadrant 1 is a normal quarter annulua */
2462
0
                    p0.x = x1, p0.y = y1;
2463
0
                    p1.x = x1 - r2, p1.y = y1 + r2;
2464
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2465
0
                }
2466
0
                break;
2467
0
            case 1:
2468
                /* We have two four-sided elements, from the tangent point
2469
                 * each side, to the point where the tangent crosses an
2470
                 * axis of the larger circle. A line back to the edge
2471
                 * of the larger circle, a line to the point where an axis
2472
                 * crosses the smaller circle, then an arc back to the starting point.
2473
                 */
2474
                /* Figure out the tangent point */
2475
                /* sin (angle) = y1 - y0 / r1 - r0
2476
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2477
                 */
2478
0
                ty = y1 + ((y0 - y1) / (r1 - r0)) * r1;
2479
0
                tx = x1 - ((x1 - x0) / (r1 - r0)) * r1;
2480
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2481
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2482
                 * as its the same angle where it crosses the axis of the larger circle.
2483
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2484
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2485
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2486
                 * circle
2487
                 */
2488
0
                sinalpha = (y0 - y1) / (r1 - r0);
2489
0
                alpha = asin(sinalpha);
2490
0
                cosalpha = cos(alpha);
2491
0
                intersectx = x1 - (r1 / cosalpha);
2492
0
                intersecty = y1;
2493
2494
0
                p0.x = tx, p0.y = ty;
2495
0
                p1.x = tx - (tx - intersectx) / 2, p1.y = ty - (ty - intersecty) / 2;
2496
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2497
0
                p0.x = intersectx, p0.y = intersecty;
2498
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2499
0
                p1.x = x1 - r1, p1.y = y1;
2500
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2501
0
                p0.x = tx, p0.y = ty;
2502
0
                centre.x = x1, centre.y = y1;
2503
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2504
0
                code = patch_fill(pfs, curve, NULL, NULL);
2505
0
                if (code < 0)
2506
0
                    return code;
2507
2508
0
                if (intersectx > x1 - r2) {
2509
                    /* didn't get all the way to the edge, quadrant 2 is composed of 2 quads :-(
2510
                     * An 'annulus' where the right edge is less than the normal extent and a
2511
                     * quad which is a rectangle with one corner chopped of at an angle.
2512
                     */
2513
0
                    p0.x = x1, p0.y = y1;
2514
0
                    p1.x = intersectx, p1.y = y1 - r2;
2515
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2516
0
                    endx = x1 - r2;
2517
0
                    endy = y1 - (tan ((M_PI / 2) - alpha)) * (intersectx - endx);
2518
0
                    p0.x = intersectx, p0.y = y1;
2519
0
                    p1.x = x1 - r2, p1.y = endy;
2520
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2521
0
                    p0.x = x1 - r2, p0.y = y0 - r2;
2522
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2523
0
                    p1.x = intersectx, p1.y = p0.y;
2524
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2525
0
                    p0.x = intersectx, p0.y = y1;
2526
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2527
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2528
0
                    if (code < 0)
2529
0
                        return code;
2530
2531
0
                } else {
2532
                    /* Quadrant 2 is a normal quarter annulua */
2533
0
                    p0.x = x1, p0.y = y1;
2534
0
                    p1.x = x1 - r2, p1.y = y1 - r2;
2535
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2536
0
                }
2537
2538
                /* Q3 is always a full annulus... */
2539
0
                p0.x = x1, p0.y = y1;
2540
0
                p1.x = x1 + r2, p1.y = y1 - r2;
2541
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2542
2543
                /* alpha is now the angle between the x axis and the tangent to the
2544
                 * circles.
2545
                 */
2546
0
                alpha = (M_PI / 2) - alpha;
2547
0
                cosalpha = cos(alpha);
2548
0
                endy = y1 + (r1 / cosalpha);
2549
0
                endx = x1;
2550
2551
0
                p0.x = tx, p0.y = ty;
2552
0
                p1.x = endx + ((tx - endx) / 2), p1.y = endy - ((endy - ty) / 2);
2553
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2554
0
                p0.x = endx, p0.y = endy;
2555
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2556
0
                p1.x = x1, p1.y = y1 + r1;
2557
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2558
0
                p0.x = tx, p0.y = ty;
2559
0
                centre.x = x1, centre.y = y1;
2560
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2561
0
                code = patch_fill(pfs, curve, NULL, NULL);
2562
0
                if (code < 0)
2563
0
                    return code;
2564
2565
                /* Q0 is simimlar to Q2, either a full quarter annulus
2566
                 * or a partial one, depending on where the tangent crosses
2567
                 * the y axis
2568
                 */
2569
0
                tanalpha = tan(alpha);
2570
0
                intersecty = y1 + tanalpha * (r2 + (x1 - intersectx));
2571
0
                intersectx = x1 + r2;
2572
2573
0
                if (endy < y1 + r2) {
2574
                    /* didn't get all the way to the edge, quadrant 0 is composed of 2 quads :-(
2575
                     * An 'annulus' where the right edge is less than the normal extent and a
2576
                     * quad which is a rectangle with one corner chopped of at an angle.
2577
                     */
2578
0
                    p0.x = x1, p0.y = y1;
2579
0
                    p1.x = x1 + r2, p1.y = endy;
2580
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2581
0
                    p0.x = x1, p0.y = y1 + r1;
2582
0
                    p1.x = x1, p1.y = endy;
2583
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2584
0
                    p0.x = x1 + r2, p0.y = intersecty;
2585
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2586
0
                    p1.x = p0.x, p1.y = y1 + r1;
2587
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2588
0
                    p0.x = x1, p0.y = y1 + r1;
2589
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2590
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2591
0
                    if (code < 0)
2592
0
                        return code;
2593
0
                } else {
2594
                    /* Quadrant 0 is a normal quarter annulua */
2595
0
                    p0.x = x1, p0.y = y1;
2596
0
                    p1.x = x1 + r2, p1.y = y1 + r2;
2597
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2598
0
                }
2599
0
                break;
2600
0
            case 2:
2601
                /* We have two four-sided elements, from the tangent point
2602
                 * each side, to the point where the tangent crosses an
2603
                 * axis of the larger circle. A line back to the edge
2604
                 * of the larger circle, a line to the point where an axis
2605
                 * crosses the smaller circle, then an arc back to the starting point.
2606
                 */
2607
                /* Figure out the tangent point */
2608
                /* sin (angle) = y1 - y0 / r1 - r0
2609
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2610
                 */
2611
0
                ty = y1 - ((y1 - y0) / (r1 - r0)) * r1;
2612
0
                tx = x1 - ((x1 - x0) / (r1 - r0)) * r1;
2613
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2614
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2615
                 * as its the same angle where it crosses the axis of the larger circle.
2616
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2617
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2618
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2619
                 * circle
2620
                 */
2621
0
                sinalpha = (y1 - y0) / (r1 - r0);
2622
0
                alpha = asin(sinalpha);
2623
0
                cosalpha = cos(alpha);
2624
0
                intersectx = x1 - (r1 / cosalpha);
2625
0
                intersecty = y1;
2626
2627
0
                p0.x = tx, p0.y = ty;
2628
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty - (ty - intersecty) / 2;
2629
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2630
0
                p0.x = intersectx, p0.y = intersecty;
2631
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2632
0
                p1.x = x1 - r1, p1.y = y1;
2633
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2634
0
                p0.x = tx, p0.y = ty;
2635
0
                centre.x = x1, centre.y = y1;
2636
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2637
0
                code = patch_fill(pfs, curve, NULL, NULL);
2638
0
                if (code < 0)
2639
0
                    return code;
2640
0
                if (intersectx > x1 - r2) {
2641
                    /* didn't get all the way to the edge, quadrant 1 is composed of 2 quads :-(
2642
                     * An 'annulus' where the right edge is less than the normal extent and a
2643
                     * quad which is a rectangle with one corner chopped of at an angle.
2644
                     */
2645
0
                    p0.x = x1, p0.y = y1;
2646
0
                    p1.x = intersectx, p1.y = y1 + r2;
2647
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2648
0
                    endy = y1+r2;
2649
0
                    endx = intersectx - ((endy - intersecty) / (tan ((M_PI / 2) - alpha)));
2650
0
                    p0.x = intersectx, p0.y = y1;
2651
0
                    p1.x = endx, p1.y = endy;
2652
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2653
0
                    p0.x = x1 - r1, p0.y = endy;
2654
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2655
0
                    p1.x = x1 - r1, p1.y = y1;
2656
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2657
0
                    p0.x = intersectx, p0.y = y1;
2658
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2659
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2660
0
                    if (code < 0)
2661
0
                        return code;
2662
0
                } else {
2663
                    /* Quadrant 1 is a normal quarter annulua */
2664
0
                    p0.x = x1, p0.y = y1;
2665
0
                    p1.x = x1 - r2, p1.y = y1 + r2;
2666
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2667
0
                }
2668
2669
                /* Q0 is always a full annulus... */
2670
0
                p0.x = x1, p0.y = y1;
2671
0
                p1.x = x1 + r2, p1.y = y1 + r2;
2672
0
                if (p1.y < 0)
2673
0
                    p1.y = 0;
2674
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2675
2676
                /* alpha is now the angle between the x axis and the tangent to the
2677
                 * circles.
2678
                 */
2679
0
                alpha = (M_PI / 2) - alpha;
2680
0
                cosalpha = cos(alpha);
2681
0
                endy = y1 - (r1 / cosalpha);
2682
0
                endx = x1;
2683
2684
0
                p0.x = tx, p0.y = ty;
2685
0
                p1.x = endx + ((endx - tx) / 2), p1.y = endy - ((ty - endy) / 2);
2686
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2687
0
                p0.x = endx, p0.y = endy;
2688
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2689
0
                p1.x = x1, p1.y = y1 - r1;
2690
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2691
0
                p0.x = tx, p0.y = ty;
2692
0
                centre.x = x1, centre.y = y1;
2693
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2694
0
                code = patch_fill(pfs, curve, NULL, NULL);
2695
0
                if (code < 0)
2696
0
                    return code;
2697
2698
                /* Q3 is simimlar to Q1, either a full quarter annulus
2699
                 * or a partial one, depending on where the tangent crosses
2700
                 * the y axis
2701
                 */
2702
0
                tanalpha = tan(alpha);
2703
0
                intersecty = y1 - tanalpha * (r2 + (x1 - intersectx));
2704
0
                intersectx = x1 + r2;
2705
2706
0
                if (endy > y1 - r2) {
2707
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2708
                     * An 'annulus' where the right edge is less than the normal extent and a
2709
                     * quad which is a rectangle with one corner chopped of at an angle.
2710
                     */
2711
0
                    p0.x = x1, p0.y = y1;
2712
0
                    p1.x = x1 + r2, p1.y = endy;
2713
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2714
0
                    p0.x = x1, p0.y = y1 - r1;
2715
0
                    p1.x = x1, p1.y = endy;
2716
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2717
0
                    p0.x = x1 + r2, p0.y = intersecty;
2718
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2719
0
                    p1.x = p0.x, p1.y = y1 - r1;
2720
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2721
0
                    p0.x = x1, p0.y = y1 - r1;
2722
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2723
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2724
0
                    if (code < 0)
2725
0
                        return code;
2726
0
                } else {
2727
                    /* Quadrant 1 is a normal quarter annulua */
2728
0
                    p0.x = x1, p0.y = y1;
2729
0
                    p1.x = x1 + r2, p1.y = y1 - r2;
2730
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2731
0
                }
2732
0
                break;
2733
0
            case 3:
2734
                /* We have two four-sided elements, from the tangent point
2735
                 * each side, to the point where the tangent crosses an
2736
                 * axis of the larger circle. A line back to the edge
2737
                 * of the larger circle, a line to the point where an axis
2738
                 * crosses the smaller circle, then an arc back to the starting point.
2739
                 */
2740
                /* Figure out the tangent point */
2741
                /* sin (angle) = y1 - y0 / r1 - r0
2742
                 * ty = ((y1 - y0) / (r1 - r0)) * r1
2743
                 */
2744
0
                ty = y1 - ((y1 - y0) / (r1 - r0)) * r1;
2745
0
                tx = x1 + ((x0 - x1) / (r1 - r0)) * r1;
2746
                /* Now actually calculating the point where the tangent crosses the axis of the larger circle
2747
                 * So we need to know the angle the tangent makes with the axis of the smaller circle
2748
                 * as its the same angle where it crosses the axis of the larger circle.
2749
                 * We know the centres and the tangent are co-linear, so sin(a) = y0 - y1 / r1 - r0
2750
                 * We know the tangent is r1 from the centre of the larger circle, so the hypotenuse
2751
                 * is r0 / cos(a). That gives us 'x' and we already know y as its the centre of the larger
2752
                 * circle
2753
                 */
2754
0
                sinalpha = (y1 - y0) / (r1 - r0);
2755
0
                alpha = asin(sinalpha);
2756
0
                cosalpha = cos(alpha);
2757
0
                intersectx = x1 + (r1 / cosalpha);
2758
0
                intersecty = y1;
2759
2760
0
                p0.x = tx, p0.y = ty;
2761
0
                p1.x = tx + (intersectx - tx) / 2, p1.y = ty + (intersecty - ty) / 2;
2762
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2763
0
                p0.x = intersectx, p0.y = intersecty;
2764
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2765
0
                p1.x = x1 + r1, p1.y = y1;
2766
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2767
0
                p0.x = tx, p0.y = ty;
2768
0
                centre.x = x1, centre.y = y1;
2769
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2770
0
                code = patch_fill(pfs, curve, NULL, NULL);
2771
0
                if (code < 0)
2772
0
                    return code;
2773
0
                if (intersectx < x1 + r2) {
2774
                    /* didn't get all the way to the edge, quadrant 0 is composed of 2 quads :-(
2775
                     * An 'annulus' where the right edge is less than the normal extent and a
2776
                     * quad which is a rectangle with one corner chopped of at an angle.
2777
                     */
2778
0
                    p0.x = x1, p0.y = y1;
2779
0
                    p1.x = intersectx, p1.y = y1 + r2;
2780
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2781
0
                    endy = y1 + r2;
2782
0
                    endx = intersectx + ((endy - intersecty) / (tan ((M_PI / 2) - alpha)));
2783
0
                    p0.x = intersectx, p0.y = y1;
2784
0
                    p1.x = endx, p1.y = endy;
2785
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2786
0
                    p0.x = x1 + r1, p0.y = endy;
2787
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2788
0
                    p1.x = x1 + r1, p1.y = y1;
2789
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2790
0
                    p0.x = intersectx, p0.y = y1;
2791
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2792
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2793
0
                    if (code < 0)
2794
0
                        return code;
2795
2796
0
                } else {
2797
                    /* Quadrant 0 is a normal quarter annulua */
2798
0
                    p0.x = x1, p0.y = y1;
2799
0
                    p1.x = x1 + r2, p1.y = y1 + r2;
2800
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2801
0
                }
2802
                /* Q1 is always a full annulus... */
2803
0
                p0.x = x1, p0.y = y1;
2804
0
                p1.x = x1 - r2, p1.y = y1 + r2;
2805
0
                draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2806
2807
                /* alpha is now the angle between the x axis and the tangent to the
2808
                 * circles.
2809
                 */
2810
0
                alpha = (M_PI / 2) - alpha;
2811
0
                cosalpha = cos(alpha);
2812
0
                endy = y1 - (r1 / cosalpha);
2813
0
                endx = x1;
2814
2815
0
                p0.x = tx, p0.y = ty;
2816
0
                p1.x = endx + ((tx - endx) / 2), p1.y = endy + ((ty - endy) / 2);
2817
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2818
0
                p0.x = endx, p0.y = endy;
2819
0
                patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2820
0
                p1.x = x1, p1.y = y1 - r1;
2821
0
                patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2822
0
                p0.x = tx, p0.y = ty;
2823
0
                centre.x = x1, centre.y = y1;
2824
0
                patch_curveto(&pfs->pgs->ctm, &centre, &p1, &p0, &curve[3], t0);
2825
0
                code = patch_fill(pfs, curve, NULL, NULL);
2826
0
                if (code < 0)
2827
0
                    return code;
2828
2829
                /* Q3 is simimlar to Q1, either a full quarter annulus
2830
                 * or a partial one, depending on where the tangent crosses
2831
                 * the y axis
2832
                 */
2833
0
                tanalpha = tan(alpha);
2834
0
                intersecty = y1 - tanalpha * (r2 + (intersectx - x1));
2835
0
                intersectx = x1 - r2;
2836
2837
0
                if (endy > y1 - r2) {
2838
                    /* didn't get all the way to the edge, quadrant 3 is composed of 2 quads :-(
2839
                     * An 'annulus' where the right edge is less than the normal extent and a
2840
                     * quad which is a rectangle with one corner chopped of at an angle.
2841
                     */
2842
0
                    p0.x = x1, p0.y = y1;
2843
0
                    p1.x = x1 - r2, p1.y = endy;
2844
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2845
0
                    p0.x = x1, p0.y = y1 - r1;
2846
0
                    p1.x = x1, p1.y = endy;
2847
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[0], t0);
2848
0
                    p0.x = x1 - r2, p0.y = intersecty;
2849
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[1], t0);
2850
0
                    p1.x = p0.x, p1.y = y1 - r1;
2851
0
                    patch_lineto(&pfs->pgs->ctm, &p0, &p1, &curve[2], t0);
2852
0
                    p0.x = x1, p0.y = y1 - r1;
2853
0
                    patch_lineto(&pfs->pgs->ctm, &p1, &p0, &curve[3], t0);
2854
0
                    code = patch_fill(pfs, curve, NULL, NULL);
2855
0
                    if (code < 0)
2856
0
                        return code;
2857
0
                } else {
2858
                    /* Quadrant 1 is a normal quarter annulua */
2859
0
                    p0.x = x1, p0.y = y1;
2860
0
                    p1.x = x1 - r2, p1.y = y1 - r2;
2861
0
                    draw_quarter_annulus(pfs, &p0, r1, &p1, t0);
2862
0
                }
2863
0
                break;
2864
0
        }
2865
0
    }
2866
0
    return 0;
2867
0
}
2868
2869
static int
2870
R_outer_circle(patch_fill_state_t *pfs, const gs_rect *rect,
2871
        double x0, double y0, double r0,
2872
        double x1, double y1, double r1,
2873
        double *x2, double *y2, double *r2)
2874
0
{
2875
0
    double dx = x1 - x0, dy = y1 - y0;
2876
0
    double sp, sq, s;
2877
2878
    /* Compute a cone circle, which contacts the rect externally. */
2879
    /* Don't bother with all 4 sides of the rect,
2880
       just do with the X or Y span only,
2881
       so it's not an exact contact, sorry. */
2882
0
    if (any_abs(dx) > any_abs(dy)) {
2883
        /* Solving :
2884
            x0 + (x1 - x0) * sq + r0 + (r1 - r0) * sq == bbox_px
2885
            (x1 - x0) * sp + (r1 - r0) * sp == bbox_px - x0 - r0
2886
            sp = (bbox_px - x0 - r0) / (x1 - x0 + r1 - r0)
2887
2888
            x0 + (x1 - x0) * sq - r0 - (r1 - r0) * sq == bbox_qx
2889
            (x1 - x0) * sq - (r1 - r0) * sq == bbox_x - x0 + r0
2890
            sq = (bbox_x - x0 + r0) / (x1 - x0 - r1 + r0)
2891
         */
2892
0
        if (x1 - x0 + r1 - r0 ==  0) /* We checked for obtuse cone. */
2893
0
            return_error(gs_error_unregistered); /* Must not happen. */
2894
0
        if (x1 - x0 - r1 + r0 ==  0) /* We checked for obtuse cone. */
2895
0
            return_error(gs_error_unregistered); /* Must not happen. */
2896
0
        sp = (rect->p.x - x0 - r0) / (x1 - x0 + r1 - r0);
2897
0
        sq = (rect->q.x - x0 + r0) / (x1 - x0 - r1 + r0);
2898
0
    } else {
2899
        /* Same by Y. */
2900
0
        if (y1 - y0 + r1 - r0 ==  0) /* We checked for obtuse cone. */
2901
0
            return_error(gs_error_unregistered); /* Must not happen. */
2902
0
        if (y1 - y0 - r1 + r0 ==  0) /* We checked for obtuse cone. */
2903
0
            return_error(gs_error_unregistered); /* Must not happen. */
2904
0
        sp = (rect->p.y - y0 - r0) / (y1 - y0 + r1 - r0);
2905
0
        sq = (rect->q.y - y0 + r0) / (y1 - y0 - r1 + r0);
2906
0
    }
2907
0
    if (sp >= 1 && sq >= 1)
2908
0
        s = max(sp, sq);
2909
0
    else if(sp >= 1)
2910
0
        s = sp;
2911
0
    else if (sq >= 1)
2912
0
        s = sq;
2913
0
    else {
2914
        /* The circle 1 is outside the rect, use it. */
2915
0
        s = 1;
2916
0
    }
2917
0
    if (r0 + (r1 - r0) * s < 0) {
2918
        /* Passed the cone apex, use the apex. */
2919
0
        s = r0 / (r0 - r1);
2920
0
        *r2 = 0;
2921
0
    } else
2922
0
        *r2 = r0 + (r1 - r0) * s;
2923
0
    *x2 = x0 + (x1 - x0) * s;
2924
0
    *y2 = y0 + (y1 - y0) * s;
2925
0
    return 0;
2926
0
}
2927
2928
static double
2929
R_rect_radius(const gs_rect *rect, double x0, double y0)
2930
10
{
2931
10
    double d, dd;
2932
2933
10
    dd = hypot(rect->p.x - x0, rect->p.y - y0);
2934
10
    d = hypot(rect->p.x - x0, rect->q.y - y0);
2935
10
    dd = max(dd, d);
2936
10
    d = hypot(rect->q.x - x0, rect->q.y - y0);
2937
10
    dd = max(dd, d);
2938
10
    d = hypot(rect->q.x - x0, rect->p.y - y0);
2939
10
    dd = max(dd, d);
2940
10
    return dd;
2941
10
}
2942
2943
static int
2944
R_fill_triangle_new(patch_fill_state_t *pfs, const gs_rect *rect,
2945
    double x0, double y0, double x1, double y1, double x2, double y2, double t)
2946
0
{
2947
0
    shading_vertex_t p0, p1, p2;
2948
0
    patch_color_t *c;
2949
0
    int code;
2950
0
    reserve_colors(pfs, &c, 1); /* Can't fail */
2951
2952
0
    p0.c = c;
2953
0
    p1.c = c;
2954
0
    p2.c = c;
2955
0
    code = gs_point_transform2fixed(&pfs->pgs->ctm, x0, y0, &p0.p);
2956
0
    if (code >= 0)
2957
0
        code = gs_point_transform2fixed(&pfs->pgs->ctm, x1, y1, &p1.p);
2958
0
    if (code >= 0)
2959
0
        code = gs_point_transform2fixed(&pfs->pgs->ctm, x2, y2, &p2.p);
2960
0
    if (code >= 0) {
2961
0
        c->t[0] = c->t[1] = t;
2962
0
        patch_resolve_color(c, pfs);
2963
0
        code = mesh_triangle(pfs, &p0, &p1, &p2);
2964
0
    }
2965
0
    release_colors(pfs, pfs->color_stack, 1);
2966
0
    return code;
2967
0
}
2968
2969
static int
2970
R_obtuse_cone(patch_fill_state_t *pfs, const gs_rect *rect,
2971
        double x0, double y0, double r0,
2972
        double x1, double y1, double r1, double t0, double r_rect)
2973
0
{
2974
0
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
2975
0
    double d = hypot(dx, dy);
2976
    /* Assuming dr > d / 3 && d > dr + 1e-7 * (d + dr), see the caller. */
2977
0
    double r = r_rect * 1.4143; /* A few bigger than sqrt(2). */
2978
0
    double ax, ay, as; /* Cone apex. */
2979
0
    double g0; /* The distance from apex to the tangent point of the 0th circle. */
2980
0
    int code;
2981
2982
0
    as = r0 / (r0 - r1);
2983
0
    ax = x0 + (x1 - x0) * as;
2984
0
    ay = y0 + (y1 - y0) * as;
2985
0
    g0 = sqrt(dx * dx + dy * dy - dr * dr) * as;
2986
0
    if (g0 < 1e-7 * r0) {
2987
        /* Nearly degenerate, replace with half-plane. */
2988
        /* Restrict the half plane with triangle, which covers the rect. */
2989
0
        gs_point p0, p1, p2; /* Right tangent limit, apex limit, left tangent linit,
2990
                                (right, left == when looking from the apex). */
2991
2992
0
        p0.x = ax - dy * r / d;
2993
0
        p0.y = ay + dx * r / d;
2994
0
        p1.x = ax - dx * r / d;
2995
0
        p1.y = ay - dy * r / d;
2996
0
        p2.x = ax + dy * r / d;
2997
0
        p2.y = ay - dx * r / d;
2998
        /* Split into 2 triangles at the apex,
2999
           so that the apex is preciselly covered.
3000
           Especially important when it is not exactly degenerate. */
3001
0
        code = R_fill_triangle_new(pfs, rect, ax, ay, p0.x, p0.y, p1.x, p1.y, t0);
3002
0
        if (code < 0)
3003
0
            return code;
3004
0
        return R_fill_triangle_new(pfs, rect, ax, ay, p1.x, p1.y, p2.x, p2.y, t0);
3005
0
    } else {
3006
        /* Compute the "limit" circle so that its
3007
           tangent points are outside the rect. */
3008
        /* Note: this branch is executed when the condition above is false :
3009
           g0 >= 1e-7 * r0 .
3010
           We believe that computing this branch with doubles
3011
           provides enough precision after converting coordinates into 'fixed',
3012
           and that the limit circle radius is not dramatically big.
3013
         */
3014
0
        double es, er; /* The limit circle parameter, radius. */
3015
0
        double ex, ey; /* The limit circle centrum. */
3016
3017
0
        es = as - as * r / g0; /* Always negative. */
3018
0
        er = r * r0 / g0 ;
3019
0
        ex = x0 + dx * es;
3020
0
        ey = y0 + dy * es;
3021
        /* Fill the annulus: */
3022
0
        code = R_tensor_annulus(pfs, x0, y0, r0, t0, ex, ey, er, t0);
3023
0
        if (code < 0)
3024
0
            return code;
3025
        /* Fill entire ending circle to ensure entire rect is covered. */
3026
0
        return R_tensor_annulus(pfs, ex, ey, er, t0, ex, ey, 0, t0);
3027
0
    }
3028
0
}
3029
3030
static int
3031
R_tensor_cone_apex(patch_fill_state_t *pfs, const gs_rect *rect,
3032
        double x0, double y0, double r0,
3033
        double x1, double y1, double r1, double t)
3034
0
{
3035
0
    double as = r0 / (r0 - r1);
3036
0
    double ax = x0 + (x1 - x0) * as;
3037
0
    double ay = y0 + (y1 - y0) * as;
3038
3039
0
    return R_tensor_annulus(pfs, x1, y1, r1, t, ax, ay, 0, t);
3040
0
}
3041
3042
/*
3043
 * A map of this code:
3044
 *
3045
 * R_extensions
3046
 * |-> (R_rect_radius)
3047
 * |-> (R_outer_circle)
3048
 * |-> R_obtuse_cone
3049
 * |   |-> R_fill_triangle_new
3050
 * |   |   '-> mesh_triangle
3051
 * |   |       '-> mesh_triangle_rec <--.
3052
 * |   |           |--------------------'
3053
 * |   |           |-> small_mesh_triangle
3054
 * |   |           |   '-> fill_triangle
3055
 * |   |           |       '-> triangle_by_4 <--.
3056
 * |   |           |           |----------------'
3057
 * |   |           |           |-> constant_color_triangle
3058
 * |   |           |           |-> make_wedge_median (etc)
3059
 * |   |           '-----------+--------------------.
3060
 * |   '-------------------.                        |
3061
 * |-> R_tensor_cone_apex  |                        |
3062
 * |   '-------------------+                        |
3063
 * '-> R_tensor_annulus <--'                       \|/
3064
 *     |-> (make_quadrant_arc)                      |
3065
 *     '-> patch_fill                               |
3066
 *         |-> fill_patch <--.                      |
3067
 *         |   |-------------'                      |
3068
 *         |   |------------------------------------+
3069
 *         |   '-> fill_stripe                      |
3070
 *         |       |-----------------------.        |
3071
 *         |      \|/                      |        |
3072
 *         |-> fill_wedges                 |        |
3073
 *             '-> fill_wedges_aux <--.    |        |
3074
 *                 |------------------'   \|/       |
3075
 *                 |----------------> mesh_padding  '
3076
 *                 |                  '----------------------------------.
3077
 *                 '-> wedge_by_triangles <--.      .                    |
3078
 *                     |---------------------'      |                    |
3079
 *                     '-> fill_triangle_wedge <----'                    |
3080
 *                         '-> fill_triangle_wedge_aux                   |
3081
 *                             '-> fill_wedge_trap                       |
3082
 *                                 '-> wedge_trap_decompose              |
3083
 *                                     '-> linear_color_trapezoid        |
3084
 *                                         '-> decompose_linear_color <--|
3085
 *                                             |-------------------------'
3086
 *                                             '-> constant_color_trapezoid
3087
 */
3088
static int
3089
R_extensions(patch_fill_state_t *pfs, const gs_shading_R_t *psh, const gs_rect *rect,
3090
        double t0, double t1, bool Extend0, bool Extend1)
3091
20
{
3092
20
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3093
20
    double r0 = psh->params.Coords[2];
3094
20
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3095
20
    double r1 = psh->params.Coords[5];
3096
20
    double dx = x1 - x0, dy = y1 - y0, dr = any_abs(r1 - r0);
3097
20
    double d = hypot(dx, dy), r;
3098
20
    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
20
    if (any_abs (dr - d) < 0.001) {
3137
0
        if ((r0 > r1 && Extend0) || (r1 > r0 && Extend1)) {
3138
0
            r = R_rect_radius(rect, x0, y0);
3139
0
            if (r0 < r1)
3140
0
                code = R_tensor_annulus_extend_tangent(pfs, x0, y0, r0, t1, x1, y1, r1, t1, r);
3141
0
            else
3142
0
                code = R_tensor_annulus_extend_tangent(pfs, x1, y1, r1, t0, x0, y0, r0, t0, r);
3143
0
            if (code < 0)
3144
0
                return code;
3145
0
        } else {
3146
0
            if (r0 > r1) {
3147
0
                if (Extend1 && r1 > 0)
3148
0
                    return R_tensor_annulus(pfs, x1, y1, r1, t1, x1, y1, 0, t1);
3149
0
            }
3150
0
            else {
3151
0
                if (Extend0 && r0 > 0)
3152
0
                    return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3153
0
            }
3154
0
        }
3155
0
    } else
3156
20
    if (dr > d - 1e-4 * (d + dr)) {
3157
        /* Nested circles, or degenerate. */
3158
20
        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
20
        } else {
3170
20
            if (Extend1) {
3171
10
                r = R_rect_radius(rect, x1, y1);
3172
10
                if (r > r1) {
3173
10
                    code = R_tensor_annulus(pfs, x1, y1, r, t1, x1, y1, r1, t1);
3174
10
                    if (code < 0)
3175
0
                        return code;
3176
10
                }
3177
10
            }
3178
20
            if (Extend0 && r0 > 0)
3179
0
                return R_tensor_annulus(pfs, x0, y0, r0, t0, x0, y0, 0, t0);
3180
20
        }
3181
20
    } else if (dr > d / 3) {
3182
        /* Obtuse cone. */
3183
0
        if (r0 > r1) {
3184
0
            if (Extend0) {
3185
0
                r = R_rect_radius(rect, x0, y0);
3186
0
                code = R_obtuse_cone(pfs, rect, x0, y0, r0, x1, y1, r1, t0, r);
3187
0
                if (code < 0)
3188
0
                    return code;
3189
0
            }
3190
0
            if (Extend1 && r1 != 0)
3191
0
                return R_tensor_cone_apex(pfs, rect, x0, y0, r0, x1, y1, r1, t1);
3192
0
            return 0;
3193
0
        } else {
3194
0
            if (Extend1) {
3195
0
                r = R_rect_radius(rect, x1, y1);
3196
0
                code = R_obtuse_cone(pfs, rect, x1, y1, r1, x0, y0, r0, t1, r);
3197
0
                if (code < 0)
3198
0
                    return code;
3199
0
            }
3200
0
            if (Extend0 && r0 != 0)
3201
0
                return R_tensor_cone_apex(pfs, rect, x1, y1, r1, x0, y0, r0, t0);
3202
0
        }
3203
0
    } else {
3204
        /* Acute cone or cylinder. */
3205
0
        double x2, y2, r2, x3, y3, r3;
3206
3207
0
        if (Extend0) {
3208
0
            code = R_outer_circle(pfs, rect, x1, y1, r1, x0, y0, r0, &x3, &y3, &r3);
3209
0
            if (code < 0)
3210
0
                return code;
3211
0
            if (x3 != x1 || y3 != y1) {
3212
0
                code = R_tensor_annulus(pfs, x0, y0, r0, t0, x3, y3, r3, t0);
3213
0
                if (code < 0)
3214
0
                    return code;
3215
0
            }
3216
0
        }
3217
0
        if (Extend1) {
3218
0
            code = R_outer_circle(pfs, rect, x0, y0, r0, x1, y1, r1, &x2, &y2, &r2);
3219
0
            if (code < 0)
3220
0
                return code;
3221
0
            if (x2 != x0 || y2 != y0) {
3222
0
                code = R_tensor_annulus(pfs, x1, y1, r1, t1, x2, y2, r2, t1);
3223
0
                if (code < 0)
3224
0
                    return code;
3225
0
            }
3226
0
        }
3227
0
    }
3228
20
    return 0;
3229
20
}
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
0
{
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
0
    quadrangle_patch p;
3257
0
    shading_vertex_t pp[2][2];
3258
0
    const gs_color_space *pcs = pfs->direct_space;
3259
0
    patch_color_t pc;
3260
0
    int code;
3261
3262
0
    code = gs_function_evaluate(pfs->Function, &t, pc.cc.paint.values);
3263
0
    if (code < 0)
3264
0
        return code;
3265
0
    pcs->type->restrict_color(&pc.cc, pcs);
3266
0
    pc.t[0] = pc.t[1] = t;
3267
0
    pp[0][0].p = clip_rect->p;
3268
0
    pp[0][1].p.x = clip_rect->q.x;
3269
0
    pp[0][1].p.y = clip_rect->p.y;
3270
0
    pp[1][0].p.x = clip_rect->p.x;
3271
0
    pp[1][0].p.y = clip_rect->q.y;
3272
0
    pp[1][1].p = clip_rect->q;
3273
0
    pp[0][0].c = pp[0][1].c = pp[1][0].c = pp[1][1].c = &pc;
3274
0
    p.p[0][0] = &pp[0][0];
3275
0
    p.p[0][1] = &pp[0][1];
3276
0
    p.p[1][0] = &pp[1][0];
3277
0
    p.p[1][1] = &pp[1][1];
3278
0
    return constant_color_quadrangle(pfs, &p, false);
3279
0
#endif
3280
0
}
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
0
#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
0
{
3298
0
    double cx = rsa->x0 + (rsa->x1 - rsa->x0) * t;
3299
0
    double cy = rsa->y0 + (rsa->y1 - rsa->y0) * t;
3300
0
    double rx = rsa->p[point_index].x - cx;
3301
0
    double ry = rsa->p[point_index].y - cy;
3302
0
    double dx = rsa->p[point_index - 1].x - rsa->p[point_index].x;
3303
0
    double dy = rsa->p[point_index - 1].y - rsa->p[point_index].y;
3304
3305
0
    if (at_corner) {
3306
0
        double Dx = rsa->p[point_index + 1].x - rsa->p[point_index].x;
3307
0
        double Dy = rsa->p[point_index + 1].y - rsa->p[point_index].y;
3308
0
        bool b1 = (dx * rx + dy * ry >= 0);
3309
0
        bool b2 = (Dx * rx + Dy * ry >= 0);
3310
3311
0
        if (b1 & b2)
3312
0
            rsa->outer_contact[root_index] = true;
3313
0
    } else {
3314
0
        if (rx * dy - ry * dx < 0)
3315
0
            rsa->outer_contact[root_index] = true;
3316
0
    }
3317
0
}
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
0
{
3322
0
    int i;
3323
3324
0
    for (i = 0; i < 2; i++) {
3325
0
        bool good_root;
3326
3327
0
        if (!have_root[i])
3328
0
            continue;
3329
0
        good_root = (!rsa->have_apex || (rsa->apex <= 0 || r0 == 0 ? t[i] >= rsa->apex : t[i] <= rsa->apex));
3330
0
        if (good_root) {
3331
0
            radial_shading_external_contact(rsa, point_index, t[i], r0, r1, at_corner, i);
3332
0
            if (!rsa->have_root[i]) {
3333
0
                rsa->span[i][0] = rsa->span[i][1] = t[i];
3334
0
                rsa->have_root[i] = true;
3335
0
            } else {
3336
0
                if (rsa->span[i][0] > t[i])
3337
0
                    rsa->span[i][0] = t[i];
3338
0
                if (rsa->span[i][1] < t[i])
3339
0
                    rsa->span[i][1] = t[i];
3340
0
            }
3341
0
        }
3342
0
    }
3343
0
}
3344
3345
static void
3346
compute_radial_shading_span_extended_side(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3347
0
{
3348
0
    double cc, c;
3349
0
    bool have_root[2] = {false, false};
3350
0
    double t[2];
3351
0
    bool by_x = (rsa->p[point_index].x != rsa->p[point_index + 1].x);
3352
0
    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
0
    if (by_x) {
3365
0
        c = rsa->p[point_index].x - rsa->x0;
3366
0
        cc = rsa->x1 - rsa->x0;
3367
0
    } else {
3368
0
        c = rsa->p[point_index].y - rsa->y0;
3369
0
        cc = rsa->y1 - rsa->y0;
3370
0
    }
3371
0
    t[0] = (c - r0) / (cc + r1 - r0);
3372
0
    t[1] = (c + r0) / (cc - r1 + r0);
3373
0
    if (t[0] > t[1]) {
3374
0
        c    = t[0];
3375
0
        t[0] = t[1];
3376
0
        t[1] = c;
3377
0
    }
3378
0
    for (i = 0; i < 2; i++) {
3379
0
        double d, d0, d1;
3380
3381
0
        if (by_x) {
3382
0
            d = rsa->y1 - rsa->y0 + r0 + (r1 - r0) * t[i];
3383
0
            d0 = rsa->p[point_index].y;
3384
0
            d1 = rsa->p[point_index + 1].y;
3385
0
        } else {
3386
0
            d = rsa->x1 - rsa->x0 + r0 + (r1 - r0) * t[i];
3387
0
            d0 = rsa->p[point_index].x;
3388
0
            d1 = rsa->p[point_index + 1].x;
3389
0
        }
3390
0
        if (d1 > d0 ? d0 <= d && d <= d1 : d1 <= d && d <= d0)
3391
0
            have_root[i] = true;
3392
0
    }
3393
0
    store_roots(rsa, have_root, t, r0, r1, point_index, false);
3394
0
}
3395
3396
static int
3397
compute_radial_shading_span_extended_point(radial_shading_attrs_t *rsa, double r0, double r1, int point_index)
3398
0
{
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
0
    double p1x = rsa->x1 - rsa->x0;
3423
0
    double p1y = rsa->y1 - rsa->y0;
3424
0
    double qx  = rsa->p[point_index].x - rsa->x0;
3425
0
    double qy  = rsa->p[point_index].y - rsa->y0;
3426
0
    double a   = (Pw2(p1x) + Pw2(p1y) - Pw2(r0 - r1));
3427
0
    bool have_root[2] = {false, false};
3428
0
    double t[2];
3429
3430
0
    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
0
    } 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
0
        double minushalfb = r0*(r1-r0) + p1x*qx + p1y*qy;
3442
0
        double c          = Pw2(qx) + Pw2(qy) - Pw2(r0);
3443
0
        double desc2      = Pw2(minushalfb) - a*c; /* desc2 = 1/4 (b^2-4ac) */
3444
3445
0
        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
0
        } else {
3449
0
            double desc1 = sqrt(desc2); /* desc1 = 1/2 SQRT(b^2-4ac) */
3450
3451
0
            if (a > 0) {
3452
0
                t[0] = (minushalfb - desc1) / a;
3453
0
                t[1] = (minushalfb + desc1) / a;
3454
0
            } else {
3455
0
                t[0] = (minushalfb + desc1) / a;
3456
0
                t[1] = (minushalfb - desc1) / a;
3457
0
            }
3458
0
            have_root[0] = have_root[1] = true;
3459
0
        }
3460
0
    }
3461
0
    store_roots(rsa, have_root, t, r0, r1, point_index, true);
3462
0
    if (have_root[0] && have_root[1])
3463
0
        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
0
{
3476
0
    int span_type0, span_type1;
3477
3478
0
    span_type0 = compute_radial_shading_span_extended_point(rsa, r0, r1, 1);
3479
0
    if (span_type0 == -1)
3480
0
        return -1;
3481
0
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 2);
3482
0
    if (span_type0 != span_type1)
3483
0
        return -1;
3484
0
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 3);
3485
0
    if (span_type0 != span_type1)
3486
0
        return -1;
3487
0
    span_type1 = compute_radial_shading_span_extended_point(rsa, r0, r1, 4);
3488
0
    if (span_type0 != span_type1)
3489
0
        return -1;
3490
0
    compute_radial_shading_span_extended_side(rsa, r0, r1, 1);
3491
0
    compute_radial_shading_span_extended_side(rsa, r0, r1, 2);
3492
0
    compute_radial_shading_span_extended_side(rsa, r0, r1, 3);
3493
0
    compute_radial_shading_span_extended_side(rsa, r0, r1, 4);
3494
0
    return span_type0;
3495
0
}
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
0
{
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
0
    const double extent = 1.02;
3558
0
    int span_type0, span_type1, span_type;
3559
3560
0
    memset(rsa, 0, sizeof(*rsa));
3561
0
    rsa->x0 = x0;
3562
0
    rsa->y0 = y0;
3563
0
    rsa->x1 = x1;
3564
0
    rsa->y1 = y1;
3565
0
    rsa->p[0] = rsa->p[4] = rect->p;
3566
0
    rsa->p[1].x = rsa->p[5].x = rect->p.x;
3567
0
    rsa->p[1].y = rsa->p[5].y = rect->q.y;
3568
0
    rsa->p[2] = rect->q;
3569
0
    rsa->p[3].x = rect->q.x;
3570
0
    rsa->p[3].y = rect->p.y;
3571
0
    rsa->have_apex = any_abs(r1 - r0) > 1e-7 * any_abs(r1 + r0);
3572
0
    rsa->apex = (rsa->have_apex ? -r0 / (r1 - r0) : 0);
3573
0
    span_type0 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 * extent);
3574
0
    if (span_type0 == -1)
3575
0
        return -1;
3576
0
    span_type1 = compute_radial_shading_span_extended(rsa, r0 / extent, r1 / extent);
3577
0
    if (span_type0 != span_type1)
3578
0
        return -1;
3579
0
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 * extent);
3580
0
    if (span_type0 != span_type1)
3581
0
        return -1;
3582
0
    span_type1 = compute_radial_shading_span_extended(rsa, r0 * extent, r1 / extent);
3583
0
    if (span_type1 == -1)
3584
0
        return -1;
3585
0
    if (r0 < r1) {
3586
0
        if (rsa->have_root[0] && !rsa->outer_contact[0])
3587
0
            rsa->span[0][0] = rsa->apex; /* Likely never happens. Remove ? */
3588
0
        if (rsa->have_root[1] && !rsa->outer_contact[1])
3589
0
            rsa->span[1][0] = rsa->apex;
3590
0
    } 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
0
    span_type = 0;
3597
0
    if (rsa->have_root[0] && rsa->span[0][0] < 0)
3598
0
        span_type |= 1;
3599
0
    if (rsa->have_root[1] && rsa->span[1][0] < 0)
3600
0
        span_type |= 1;
3601
0
    if (rsa->have_root[0] && rsa->span[0][1] > 0 && rsa->span[0][0] < 1)
3602
0
        span_type |= 2;
3603
0
    if (rsa->have_root[1] && rsa->span[1][1] > 0 && rsa->span[1][0] < 1)
3604
0
        span_type |= 4;
3605
0
    if (rsa->have_root[0] && rsa->span[0][1] > 1)
3606
0
        span_type |= 8;
3607
0
    if (rsa->have_root[1] && rsa->span[1][1] > 1)
3608
0
        span_type |= 8;
3609
0
    return span_type;
3610
0
}
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
0
{
3615
0
    double s0 = span_[0], s1 = span_[1], w;
3616
3617
0
    if (s0 < 0)
3618
0
        s0 = 0;
3619
0
    if (s1 < 0)
3620
0
        s1 = 0;
3621
0
    if (s0 > 1)
3622
0
        s0 = 1;
3623
0
    if (s1 > 1)
3624
0
        s1 = 1;
3625
0
    w = s1 - s0;
3626
0
    if (w == 0)
3627
0
        return false; /* Don't pass a degenerate shading. */
3628
0
    if (w > 0.3)
3629
0
        return false; /* The span is big, don't shorten it. */
3630
0
    { /* Do shorten. */
3631
0
        double R0 = *r0, X0 = *x0, Y0 = *y0, D0 = *d0;
3632
0
        double R1 = *r1, X1 = *x1, Y1 = *y1, D1 = *d1;
3633
3634
0
        *r0 = R0 + (R1 - R0) * s0;
3635
0
        *x0 = X0 + (X1 - X0) * s0;
3636
0
        *y0 = Y0 + (Y1 - Y0) * s0;
3637
0
        *d0 = D0 + (D1 - D0) * s0;
3638
0
        *r1 = R0 + (R1 - R0) * s1;
3639
0
        *x1 = X0 + (X1 - X0) * s1;
3640
0
        *y1 = Y0 + (Y1 - Y0) * s1;
3641
0
        *d1 = D0 + (D1 - D0) * s1;
3642
0
    }
3643
0
    return true;
3644
0
}
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
10
{
3649
10
    const double d = hypot(x1 - x0, y1 - y0);
3650
10
    const double area0 = M_PI * r0 * r0 / 2;
3651
10
    const double area1 = M_PI * r1 * r1 / 2;
3652
10
    const double area2 = (r0 + r1) / 2 * d;
3653
10
    const double arbitrary = 8;
3654
10
    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
10
    areaX = (rect->q.x - rect->p.x) * (rect->q.x - rect->p.x);
3662
10
    if (areaX * arbitrary < area0 + area1 + area2)
3663
0
        return true;
3664
10
    areaY = (rect->q.y - rect->p.y) * (rect->q.y - rect->p.y);
3665
10
    if (areaY * arbitrary < area0 + area1 + area2)
3666
0
        return true;
3667
10
    return false;
3668
10
}
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
10
{
3675
10
    const gs_shading_R_t *const psh = (const gs_shading_R_t *)psh0;
3676
10
    float d0 = psh->params.Domain[0], d1 = psh->params.Domain[1];
3677
10
    float x0 = psh->params.Coords[0], y0 = psh->params.Coords[1];
3678
10
    double r0 = psh->params.Coords[2];
3679
10
    float x1 = psh->params.Coords[3], y1 = psh->params.Coords[4];
3680
10
    double r1 = psh->params.Coords[5];
3681
10
    radial_shading_attrs_t rsa;
3682
10
    int span_type; /* <0 - don't shorten, 1 - extent0, 2 - first contact, 4 - last contact, 8 - extent1. */
3683
10
    int code;
3684
10
    patch_fill_state_t pfs1;
3685
3686
10
    if (r0 == 0 && r1 == 0)
3687
0
        return 0; /* PLRM requires to paint nothing. */
3688
10
    code = shade_init_fill_state((shading_fill_state_t *)&pfs1, psh0, dev, pgs);
3689
10
    if (code < 0)
3690
0
        return code;
3691
10
    pfs1.Function = psh->params.Function;
3692
10
    code = init_patch_fill_state(&pfs1);
3693
10
    if (code < 0) {
3694
0
        if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3695
0
        return code;
3696
0
    }
3697
10
    pfs1.function_arg_shift = 0;
3698
10
    pfs1.rect = *clip_rect;
3699
10
    pfs1.maybe_self_intersecting = false;
3700
10
    if (is_radial_shading_large(x0, y0, r0, x1, y1, r1, rect))
3701
0
        span_type = compute_radial_shading_span(&rsa, x0, y0, r0, x1, y1, r1, rect);
3702
10
    else
3703
10
        span_type = -1;
3704
10
    if (span_type < 0) {
3705
10
        code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3706
10
        if (code >= 0)
3707
10
            code = R_tensor_annulus(&pfs1, x0, y0, r0, d0, x1, y1, r1, d1);
3708
10
        if (code >= 0)
3709
10
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3710
10
    } else if (span_type == 1) {
3711
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d0);
3712
0
    } else if (span_type == 8) {
3713
0
        code = R_fill_rect_with_const_color(&pfs1, clip_rect, d1);
3714
0
    } else {
3715
0
        bool second_interval = true;
3716
3717
0
        code = 0;
3718
0
        if (span_type & 1)
3719
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, psh->params.Extend[0], false);
3720
0
        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
0
        if (code >= 0 && second_interval) {
3737
0
            if (span_type & 4) {
3738
0
                float X0 = x0, Y0 = y0, D0 = d0, X1 = x1, Y1 = y1, D1 = d1;
3739
0
                double R0 = r0, R1 = r1;
3740
3741
0
                shorten_radial_shading(&X0, &Y0, &R0, &D0, &X1, &Y1, &R1, &D1, rsa.span[1]);
3742
0
                code = R_tensor_annulus(&pfs1, X0, Y0, R0, D0, X1, Y1, R1, D1);
3743
0
            }
3744
0
        }
3745
0
        if (code >= 0 && (span_type & 8))
3746
0
            code = R_extensions(&pfs1, psh, rect, d0, d1, false, psh->params.Extend[1]);
3747
0
    }
3748
10
    if (pfs1.icclink != NULL) gsicc_release_link(pfs1.icclink);
3749
10
    if (term_patch_fill_state(&pfs1))
3750
0
        return_error(gs_error_unregistered); /* Must not happen. */
3751
10
    return code;
3752
10
}
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
10
{
3759
10
    return gs_shading_R_fill_rectangle_aux(psh0, rect, rect_clip, dev, pgs);
3760
10
}