Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "mupdf/xps.h"
2
3
#define MAX_STOPS 256
4
5
enum { SPREAD_PAD, SPREAD_REPEAT, SPREAD_REFLECT };
6
7
/*
8
* Parse a list of GradientStop elements.
9
* Fill the offset and color arrays, and
10
* return the number of stops parsed.
11
*/
12
13
struct stop
14
{
15
float offset;
16
float r, g, b, a;
17
int index;
18
};
19
20
static int cmp_stop(const void *a, const void *b)
21
{
22
const struct stop *astop = a;
23
const struct stop *bstop = b;
24
float diff = astop->offset - bstop->offset;
25
if (diff < 0)
26
return -1;
27
if (diff > 0)
28
return 1;
29
return astop->index - bstop->index;
30
}
31
32
static inline float lerp(float a, float b, float x)
33
{
34
return a + (b - a) * x;
35
}
36
37
static int
38
xps_parse_gradient_stops(fz_context *ctx, xps_document *doc, char *base_uri, fz_xml *node,
39
struct stop *stops, int maxcount)
40
{
41
fz_colorspace *colorspace;
42
float sample[FZ_MAX_COLORS];
43
float rgb[3];
44
int before, after;
45
int count;
46
int i;
47
48
/* We may have to insert 2 extra stops when postprocessing */
49
maxcount -= 2;
50
51
count = 0;
52
while (node && count < maxcount)
53
{
54
if (fz_xml_is_tag(node, "GradientStop"))
55
{
56
char *offset = fz_xml_att(node, "Offset");
57
char *color = fz_xml_att(node, "Color");
58
if (offset && color)
59
{
60
stops[count].offset = fz_atof(offset);
61
stops[count].index = count;
62
63
xps_parse_color(ctx, doc, base_uri, color, &colorspace, sample);
64
65
fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, sample + 1);
66
67
stops[count].r = rgb[0];
68
stops[count].g = rgb[1];
69
stops[count].b = rgb[2];
70
stops[count].a = sample[0];
71
72
count ++;
73
}
74
}
75
node = fz_xml_next(node);
76
}
77
78
if (count == 0)
79
{
80
fz_warn(ctx, "gradient brush has no gradient stops");
81
stops[0].offset = 0;
82
stops[0].r = 0;
83
stops[0].g = 0;
84
stops[0].b = 0;
85
stops[0].a = 1;
86
stops[1].offset = 1;
87
stops[1].r = 1;
88
stops[1].g = 1;
89
stops[1].b = 1;
90
stops[1].a = 1;
91
return 2;
92
}
93
94
if (count == maxcount)
95
fz_warn(ctx, "gradient brush exceeded maximum number of gradient stops");
96
97
/* Postprocess to make sure the range of offsets is 0.0 to 1.0 */
98
99
qsort(stops, count, sizeof(struct stop), cmp_stop);
100
101
before = -1;
102
after = -1;
103
104
for (i = 0; i < count; i++)
105
{
106
if (stops[i].offset < 0)
107
before = i;
108
if (stops[i].offset > 1)
109
{
110
after = i;
111
break;
112
}
113
}
114
115
/* Remove all stops < 0 except the largest one */
116
if (before > 0)
117
{
118
memmove(stops, stops + before, (count - before) * sizeof(struct stop));
119
count -= before;
120
}
121
122
/* Remove all stops > 1 except the smallest one */
123
if (after >= 0)
124
count = after + 1;
125
126
/* Expand single stop to 0 .. 1 */
127
if (count == 1)
128
{
129
stops[1] = stops[0];
130
stops[0].offset = 0;
131
stops[1].offset = 1;
132
return 2;
133
}
134
135
/* First stop < 0 -- interpolate value to 0 */
136
if (stops[0].offset < 0)
137
{
138
float d = -stops[0].offset / (stops[1].offset - stops[0].offset);
139
stops[0].offset = 0;
140
stops[0].r = lerp(stops[0].r, stops[1].r, d);
141
stops[0].g = lerp(stops[0].g, stops[1].g, d);
142
stops[0].b = lerp(stops[0].b, stops[1].b, d);
143
stops[0].a = lerp(stops[0].a, stops[1].a, d);
144
}
145
146
/* Last stop > 1 -- interpolate value to 1 */
147
if (stops[count-1].offset > 1)
148
{
149
float d = (1 - stops[count-2].offset) / (stops[count-1].offset - stops[count-2].offset);
150
stops[count-1].offset = 1;
151
stops[count-1].r = lerp(stops[count-2].r, stops[count-1].r, d);
152
stops[count-1].g = lerp(stops[count-2].g, stops[count-1].g, d);
153
stops[count-1].b = lerp(stops[count-2].b, stops[count-1].b, d);
154
stops[count-1].a = lerp(stops[count-2].a, stops[count-1].a, d);
155
}
156
157
/* First stop > 0 -- insert a duplicate at 0 */
158
if (stops[0].offset > 0)
159
{
160
memmove(stops + 1, stops, count * sizeof(struct stop));
161
stops[0] = stops[1];
162
stops[0].offset = 0;
163
count++;
164
}
165
166
/* Last stop < 1 -- insert a duplicate at 1 */
167
if (stops[count-1].offset < 1)
168
{
169
stops[count] = stops[count-1];
170
stops[count].offset = 1;
171
count++;
172
}
173
174
return count;
175
}
176
177
static void
178
xps_sample_gradient_stops(fz_context *ctx, xps_document *doc, fz_shade *shade, struct stop *stops, int count)
179
{
180
float offset, d;
181
int i, k;
182
183
k = 0;
184
for (i = 0; i < 256; i++)
185
{
186
offset = i / 255.0f;
187
while (k + 1 < count && offset > stops[k+1].offset)
188
k++;
189
190
d = (offset - stops[k].offset) / (stops[k+1].offset - stops[k].offset);
191
192
shade->function[i][0] = lerp(stops[k].r, stops[k+1].r, d);
193
shade->function[i][1] = lerp(stops[k].g, stops[k+1].g, d);
194
shade->function[i][2] = lerp(stops[k].b, stops[k+1].b, d);
195
shade->function[i][3] = lerp(stops[k].a, stops[k+1].a, d);
196
}
197
}
198
199
/*
200
* Radial gradients map more or less to Radial shadings.
201
* The inner circle is always a point.
202
* The outer circle is actually an ellipse,
203
* mess with the transform to squash the circle into the right aspect.
204
*/
205
206
static void
207
xps_draw_one_radial_gradient(fz_context *ctx, xps_document *doc, const fz_matrix *ctm,
208
struct stop *stops, int count,
209
int extend,
210
float x0, float y0, float r0,
211
float x1, float y1, float r1)
212
{
213
fz_device *dev = doc->dev;
214
fz_shade *shade;
215
216
/* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */
217
shade = fz_malloc_struct(ctx, fz_shade);
218
FZ_INIT_STORABLE(shade, 1, fz_drop_shade_imp);
219
shade->colorspace = fz_device_rgb(ctx);
220
shade->bbox = fz_infinite_rect;
221
shade->matrix = fz_identity;
222
shade->use_background = 0;
223
shade->use_function = 1;
224
shade->type = FZ_RADIAL;
225
shade->u.l_or_r.extend[0] = extend;
226
shade->u.l_or_r.extend[1] = extend;
227
228
xps_sample_gradient_stops(ctx, doc, shade, stops, count);
229
230
shade->u.l_or_r.coords[0][0] = x0;
231
shade->u.l_or_r.coords[0][1] = y0;
232
shade->u.l_or_r.coords[0][2] = r0;
233
shade->u.l_or_r.coords[1][0] = x1;
234
shade->u.l_or_r.coords[1][1] = y1;
235
shade->u.l_or_r.coords[1][2] = r1;
236
237
fz_fill_shade(ctx, dev, shade, ctm, 1);
238
239
fz_drop_shade(ctx, shade);
240
}
241
242
/*
243
* Linear gradients.
244
*/
245
246
static void
247
xps_draw_one_linear_gradient(fz_context *ctx, xps_document *doc, const fz_matrix *ctm,
248
struct stop *stops, int count,
249
int extend,
250
float x0, float y0, float x1, float y1)
251
{
252
fz_device *dev = doc->dev;
253
fz_shade *shade;
254
255
/* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */
256
shade = fz_malloc_struct(ctx, fz_shade);
257
FZ_INIT_STORABLE(shade, 1, fz_drop_shade_imp);
258
shade->colorspace = fz_device_rgb(ctx);
259
shade->bbox = fz_infinite_rect;
260
shade->matrix = fz_identity;
261
shade->use_background = 0;
262
shade->use_function = 1;
263
shade->type = FZ_LINEAR;
264
shade->u.l_or_r.extend[0] = extend;
265
shade->u.l_or_r.extend[1] = extend;
266
267
xps_sample_gradient_stops(ctx, doc, shade, stops, count);
268
269
shade->u.l_or_r.coords[0][0] = x0;
270
shade->u.l_or_r.coords[0][1] = y0;
271
shade->u.l_or_r.coords[0][2] = 0;
272
shade->u.l_or_r.coords[1][0] = x1;
273
shade->u.l_or_r.coords[1][1] = y1;
274
shade->u.l_or_r.coords[1][2] = 0;
275
276
fz_fill_shade(ctx, dev, shade, ctm, doc->opacity[doc->opacity_top]);
277
278
fz_drop_shade(ctx, shade);
279
}
280
281
/*
282
* We need to loop and create many shading objects to account
283
* for the Repeat and Reflect SpreadMethods.
284
* I'm not smart enough to calculate this analytically
285
* so we iterate and check each object until we
286
* reach a reasonable limit for infinite cases.
287
*/
288
289
static inline float point_inside_circle(float px, float py, float x, float y, float r)
290
{
291
float dx = px - x;
292
float dy = py - y;
293
return dx * dx + dy * dy <= r * r;
294
}
295
296
static void
297
xps_draw_radial_gradient(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
298
struct stop *stops, int count,
299
fz_xml *root, int spread)
300
{
301
float x0, y0, r0;
302
float x1, y1, r1;
303
float xrad = 1;
304
float yrad = 1;
305
float invscale;
306
int i, ma = 1;
307
fz_matrix local_ctm = *ctm;
308
fz_matrix inv;
309
fz_rect local_area = *area;
310
311
char *center_att = fz_xml_att(root, "Center");
312
char *origin_att = fz_xml_att(root, "GradientOrigin");
313
char *radius_x_att = fz_xml_att(root, "RadiusX");
314
char *radius_y_att = fz_xml_att(root, "RadiusY");
315
316
x0 = y0 = 0.0;
317
x1 = y1 = 1.0;
318
xrad = 1.0;
319
yrad = 1.0;
320
321
if (origin_att)
322
xps_parse_point(ctx, doc, origin_att, &x0, &y0);
323
if (center_att)
324
xps_parse_point(ctx, doc, center_att, &x1, &y1);
325
if (radius_x_att)
326
xrad = fz_atof(radius_x_att);
327
if (radius_y_att)
328
yrad = fz_atof(radius_y_att);
329
330
xrad = fz_max(0.01f, xrad);
331
yrad = fz_max(0.01f, yrad);
332
333
/* scale the ctm to make ellipses */
334
if (fz_abs(xrad) > FLT_EPSILON)
335
{
336
fz_pre_scale(&local_ctm, 1, yrad/xrad);
337
}
338
339
if (yrad != 0.0)
340
{
341
invscale = xrad / yrad;
342
y0 = y0 * invscale;
343
y1 = y1 * invscale;
344
}
345
346
r0 = 0;
347
r1 = xrad;
348
349
fz_transform_rect(&local_area, fz_invert_matrix(&inv, &local_ctm));
350
ma = fz_maxi(ma, ceilf(hypotf(local_area.x0 - x0, local_area.y0 - y0) / xrad));
351
ma = fz_maxi(ma, ceilf(hypotf(local_area.x1 - x0, local_area.y0 - y0) / xrad));
352
ma = fz_maxi(ma, ceilf(hypotf(local_area.x0 - x0, local_area.y1 - y0) / xrad));
353
ma = fz_maxi(ma, ceilf(hypotf(local_area.x1 - x0, local_area.y1 - y0) / xrad));
354
355
if (spread == SPREAD_REPEAT)
356
{
357
for (i = ma - 1; i >= 0; i--)
358
xps_draw_one_radial_gradient(ctx, doc, &local_ctm, stops, count, 0, x0, y0, r0 + i * xrad, x1, y1, r1 + i * xrad);
359
}
360
else if (spread == SPREAD_REFLECT)
361
{
362
if ((ma % 2) != 0)
363
ma++;
364
for (i = ma - 2; i >= 0; i -= 2)
365
{
366
xps_draw_one_radial_gradient(ctx, doc, &local_ctm, stops, count, 0, x0, y0, r0 + i * xrad, x1, y1, r1 + i * xrad);
367
xps_draw_one_radial_gradient(ctx, doc, &local_ctm, stops, count, 0, x0, y0, r0 + (i + 2) * xrad, x1, y1, r1 + i * xrad);
368
}
369
}
370
else
371
{
372
xps_draw_one_radial_gradient(ctx, doc, &local_ctm, stops, count, 1, x0, y0, r0, x1, y1, r1);
373
}
374
}
375
376
/*
377
* Calculate how many iterations are needed to cover
378
* the bounding box.
379
*/
380
381
static void
382
xps_draw_linear_gradient(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
383
struct stop *stops, int count,
384
fz_xml *root, int spread)
385
{
386
float x0, y0, x1, y1;
387
int i, mi, ma;
388
float dx, dy, x, y, k;
389
fz_point p1, p2;
390
fz_matrix inv;
391
fz_rect local_area = *area;
392
393
char *start_point_att = fz_xml_att(root, "StartPoint");
394
char *end_point_att = fz_xml_att(root, "EndPoint");
395
396
x0 = y0 = 0;
397
x1 = y1 = 1;
398
399
if (start_point_att)
400
xps_parse_point(ctx, doc, start_point_att, &x0, &y0);
401
if (end_point_att)
402
xps_parse_point(ctx, doc, end_point_att, &x1, &y1);
403
404
p1.x = x0; p1.y = y0; p2.x = x1; p2.y = y1;
405
fz_transform_rect(&local_area, fz_invert_matrix(&inv, ctm));
406
x = p2.x - p1.x; y = p2.y - p1.y;
407
k = ((local_area.x0 - p1.x) * x + (local_area.y0 - p1.y) * y) / (x * x + y * y);
408
mi = floorf(k); ma = ceilf(k);
409
k = ((local_area.x1 - p1.x) * x + (local_area.y0 - p1.y) * y) / (x * x + y * y);
410
mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k));
411
k = ((local_area.x0 - p1.x) * x + (local_area.y1 - p1.y) * y) / (x * x + y * y);
412
mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k));
413
k = ((local_area.x1 - p1.x) * x + (local_area.y1 - p1.y) * y) / (x * x + y * y);
414
mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k));
415
dx = x1 - x0; dy = y1 - y0;
416
417
if (spread == SPREAD_REPEAT)
418
{
419
for (i = mi; i < ma; i++)
420
xps_draw_one_linear_gradient(ctx, doc, ctm, stops, count, 0, x0 + i * dx, y0 + i * dy, x1 + i * dx, y1 + i * dy);
421
}
422
else if (spread == SPREAD_REFLECT)
423
{
424
if ((mi % 2) != 0)
425
mi--;
426
for (i = mi; i < ma; i += 2)
427
{
428
xps_draw_one_linear_gradient(ctx, doc, ctm, stops, count, 0, x0 + i * dx, y0 + i * dy, x1 + i * dx, y1 + i * dy);
429
xps_draw_one_linear_gradient(ctx, doc, ctm, stops, count, 0, x0 + (i + 2) * dx, y0 + (i + 2) * dy, x1 + i * dx, y1 + i * dy);
430
}
431
}
432
else
433
{
434
xps_draw_one_linear_gradient(ctx, doc, ctm, stops, count, 1, x0, y0, x1, y1);
435
}
436
}
437
438
/*
439
* Parse XML tag and attributes for a gradient brush, create color/opacity
440
* function objects and call gradient drawing primitives.
441
*/
442
443
static void
444
xps_parse_gradient_brush(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
445
char *base_uri, xps_resource *dict, fz_xml *root,
446
void (*draw)(fz_context *ctx, xps_document *, const fz_matrix*, const fz_rect *, struct stop *, int, fz_xml *, int))
447
{
448
fz_xml *node;
449
450
char *opacity_att;
451
char *spread_att;
452
char *transform_att;
453
454
fz_xml *transform_tag = NULL;
455
fz_xml *stop_tag = NULL;
456
457
struct stop stop_list[MAX_STOPS];
458
int stop_count;
459
fz_matrix transform;
460
int spread_method;
461
462
opacity_att = fz_xml_att(root, "Opacity");
463
spread_att = fz_xml_att(root, "SpreadMethod");
464
transform_att = fz_xml_att(root, "Transform");
465
466
for (node = fz_xml_down(root); node; node = fz_xml_next(node))
467
{
468
if (fz_xml_is_tag(node, "LinearGradientBrush.Transform"))
469
transform_tag = fz_xml_down(node);
470
if (fz_xml_is_tag(node, "RadialGradientBrush.Transform"))
471
transform_tag = fz_xml_down(node);
472
if (fz_xml_is_tag(node, "LinearGradientBrush.GradientStops"))
473
stop_tag = fz_xml_down(node);
474
if (fz_xml_is_tag(node, "RadialGradientBrush.GradientStops"))
475
stop_tag = fz_xml_down(node);
476
}
477
478
xps_resolve_resource_reference(ctx, doc, dict, &transform_att, &transform_tag, NULL);
479
480
spread_method = SPREAD_PAD;
481
if (spread_att)
482
{
483
if (!strcmp(spread_att, "Pad"))
484
spread_method = SPREAD_PAD;
485
if (!strcmp(spread_att, "Reflect"))
486
spread_method = SPREAD_REFLECT;
487
if (!strcmp(spread_att, "Repeat"))
488
spread_method = SPREAD_REPEAT;
489
}
490
491
transform = fz_identity;
492
if (transform_att)
493
xps_parse_render_transform(ctx, doc, transform_att, &transform);
494
if (transform_tag)
495
xps_parse_matrix_transform(ctx, doc, transform_tag, &transform);
496
fz_concat(&transform, &transform, ctm);
497
498
if (!stop_tag) {
499
fz_warn(ctx, "missing gradient stops tag");
500
return;
501
}
502
503
stop_count = xps_parse_gradient_stops(ctx, doc, base_uri, stop_tag, stop_list, MAX_STOPS);
504
if (stop_count == 0)
505
{
506
fz_warn(ctx, "no gradient stops found");
507
return;
508
}
509
510
xps_begin_opacity(ctx, doc, &transform, area, base_uri, dict, opacity_att, NULL);
511
512
draw(ctx, doc, &transform, area, stop_list, stop_count, root, spread_method);
513
514
xps_end_opacity(ctx, doc, base_uri, dict, opacity_att, NULL);
515
}
516
517
void
518
xps_parse_linear_gradient_brush(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
519
char *base_uri, xps_resource *dict, fz_xml *root)
520
{
521
xps_parse_gradient_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_draw_linear_gradient);
522
}
523
524
void
525
xps_parse_radial_gradient_brush(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
526
char *base_uri, xps_resource *dict, fz_xml *root)
527
{
528
xps_parse_gradient_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_draw_radial_gradient);
529
}
530
531