1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
#include "fitz.h"
enum { BUTT = 0, ROUND = 1, SQUARE = 2, MITER = 0, BEVEL = 2 };
struct sctx
{
fz_gel *gel;
fz_matrix *ctm;
float flatness;
int linecap;
int linejoin;
float linewidth;
float miterlimit;
fz_point beg[2];
fz_point seg[2];
int sn, bn;
int dot;
float *dashlist;
float dashphase;
int dashlen;
int toggle;
int offset;
float phase;
fz_point cur;
};
static void
line(struct sctx *s, float x0, float y0, float x1, float y1)
{
float tx0 = s->ctm->a * x0 + s->ctm->c * y0 + s->ctm->e;
float ty0 = s->ctm->b * x0 + s->ctm->d * y0 + s->ctm->f;
float tx1 = s->ctm->a * x1 + s->ctm->c * y1 + s->ctm->e;
float ty1 = s->ctm->b * x1 + s->ctm->d * y1 + s->ctm->f;
fz_insertgel(s->gel, tx0, ty0, tx1, ty1);
}
static void
arc(struct sctx *s,
float xc, float yc,
float x0, float y0,
float x1, float y1)
{
float th0, th1, r;
float theta;
float ox, oy, nx, ny;
int n, i;
r = fabs(s->linewidth);
theta = 2 * M_SQRT2 * sqrt(s->flatness / r);
th0 = atan2(y0, x0);
th1 = atan2(y1, x1);
if (r > 0)
{
if (th0 < th1)
th0 += M_PI * 2;
n = ceil((th0 - th1) / theta);
}
else
{
if (th1 < th0)
th1 += M_PI * 2;
n = ceil((th1 - th0) / theta);
}
ox = x0;
oy = y0;
for (i = 1; i < n; i++)
{
theta = th0 + (th1 - th0) * i / n;
nx = cos(theta) * r;
ny = sin(theta) * r;
line(s, xc + ox, yc + oy, xc + nx, yc + ny);
ox = nx;
oy = ny;
}
line(s, xc + ox, yc + oy, xc + x1, yc + y1);
}
static void
linestroke(struct sctx *s, fz_point a, fz_point b)
{
float dx = b.x - a.x;
float dy = b.y - a.y;
float scale = s->linewidth / sqrt(dx * dx + dy * dy);
float dlx = dy * scale;
float dly = -dx * scale;
line(s, a.x - dlx, a.y - dly, b.x - dlx, b.y - dly);
line(s, b.x + dlx, b.y + dly, a.x + dlx, a.y + dly);
}
static void
linejoin(struct sctx *s, fz_point a, fz_point b, fz_point c)
{
float miterlimit = s->miterlimit;
float linewidth = s->linewidth;
int linejoin = s->linejoin;
float dx0, dy0;
float dx1, dy1;
float dlx0, dly0;
float dlx1, dly1;
float dmx, dmy;
float dmr2;
float scale;
float cross;
dx0 = b.x - a.x;
dy0 = b.y - a.y;
dx1 = c.x - b.x;
dy1 = c.y - b.y;
if (dx0 * dx0 + dy0 * dy0 < FLT_EPSILON)
linejoin = BEVEL;
if (dx1 * dx1 + dy1 * dy1 < FLT_EPSILON)
linejoin = BEVEL;
scale = linewidth / sqrt(dx0 * dx0 + dy0 * dy0);
dlx0 = dy0 * scale;
dly0 = -dx0 * scale;
scale = linewidth / sqrt(dx1 * dx1 + dy1 * dy1);
dlx1 = dy1 * scale;
dly1 = -dx1 * scale;
cross = dx1 * dy0 - dx0 * dy1;
dmx = (dlx0 + dlx1) * 0.5;
dmy = (dly0 + dly1) * 0.5;
dmr2 = dmx * dmx + dmy * dmy;
if (cross * cross < FLT_EPSILON && dx0 * dx1 + dy0 * dy1 >= 0)
linejoin = BEVEL;
if (linejoin == MITER)
if (dmr2 * miterlimit * miterlimit < linewidth * linewidth)
linejoin = BEVEL;
if (linejoin == BEVEL)
{
line(s, b.x - dlx0, b.y - dly0, b.x - dlx1, b.y - dly1);
line(s, b.x + dlx1, b.y + dly1, b.x + dlx0, b.y + dly0);
}
if (linejoin == MITER)
{
scale = linewidth * linewidth / dmr2;
dmx *= scale;
dmy *= scale;
if (cross < 0)
{
line(s, b.x - dlx0, b.y - dly0, b.x - dlx1, b.y - dly1);
line(s, b.x + dlx1, b.y + dly1, b.x + dmx, b.y + dmy);
line(s, b.x + dmx, b.y + dmy, b.x + dlx0, b.y + dly0);
}
else
{
line(s, b.x + dlx1, b.y + dly1, b.x + dlx0, b.y + dly0);
line(s, b.x - dlx0, b.y - dly0, b.x - dmx, b.y - dmy);
line(s, b.x - dmx, b.y - dmy, b.x - dlx1, b.y - dly1);
}
}
if (linejoin == ROUND)
{
if (cross < 0)
{
line(s, b.x - dlx0, b.y - dly0, b.x - dlx1, b.y - dly1);
arc(s, b.x, b.y, dlx1, dly1, dlx0, dly0);
}
else
{
line(s, b.x + dlx1, b.y + dly1, b.x + dlx0, b.y + dly0);
arc(s, b.x, b.y, -dlx0, -dly0, -dlx1, -dly1);
}
}
}
static void
linecap(struct sctx *s, fz_point a, fz_point b)
{
float flatness = s->flatness;
float linewidth = s->linewidth;
int linecap = s->linecap;
float dx = b.x - a.x;
float dy = b.y - a.y;
float scale = linewidth / sqrt(dx * dx + dy * dy);
float dlx = dy * scale;
float dly = -dx * scale;
if (linecap == BUTT)
line(s, b.x - dlx, b.y - dly, b.x + dlx, b.y + dly);
if (linecap == ROUND)
{
int i;
int n = ceil(M_PI / (2.0 * M_SQRT2 * sqrt(flatness / linewidth)));
float ox = b.x - dlx;
float oy = b.y - dly;
for (i = 1; i < n; i++)
{
float theta = M_PI * i / n;
float cth = cos(theta);
float sth = sin(theta);
float nx = b.x - dlx * cth - dly * sth;
float ny = b.y - dly * cth + dlx * sth;
line(s, ox, oy, nx, ny);
ox = nx;
oy = ny;
}
line(s, ox, oy, b.x + dlx, b.y + dly);
}
if (linecap == SQUARE)
{
line(s, b.x - dlx, b.y - dly,
b.x - dlx - dly,
b.y - dly + dlx);
line(s, b.x - dlx - dly,
b.y - dly + dlx,
b.x + dlx - dly,
b.y + dly + dlx);
line(s, b.x + dlx - dly,
b.y + dly + dlx,
b.x + dlx, b.y + dly);
}
}
static void
linedot(struct sctx *s, fz_point a)
{
float flatness = s->flatness;
float linewidth = s->linewidth;
int n = ceil(M_PI / (M_SQRT2 * sqrt(flatness / linewidth)));
float ox = a.x - linewidth;
float oy = a.y;
int i;
for (i = 1; i < n; i++)
{
float theta = M_PI * 2 * i / n;
float cth = cos(theta);
float sth = sin(theta);
float nx = a.x - cth * linewidth;
float ny = a.y + sth * linewidth;
line(s, ox, oy, nx, ny);
ox = nx;
oy = ny;
}
line(s, ox, oy, a.x - linewidth, a.y);
}
static void
strokeflush(struct sctx *s)
{
if (s->sn == 2)
{
linecap(s, s->beg[1], s->beg[0]);
linecap(s, s->seg[0], s->seg[1]);
}
else if (s->dot)
{
linedot(s, s->beg[0]);
}
s->dot = 0;
}
static void
strokemoveto(struct sctx *s, fz_point cur)
{
strokeflush(s);
s->seg[0] = cur;
s->beg[0] = cur;
s->sn = 1;
s->bn = 1;
}
static void
strokelineto(struct sctx *s, fz_point cur)
{
float dx = cur.x - s->seg[s->sn-1].x;
float dy = cur.y - s->seg[s->sn-1].y;
if (dx * dx + dy * dy < FLT_EPSILON)
s->dot = 1;
linestroke(s, s->seg[s->sn-1], cur);
if (s->sn == 2)
{
linejoin(s, s->seg[0], s->seg[1], cur);
s->seg[0] = s->seg[1];
s->seg[1] = cur;
}
if (s->sn == 1)
s->seg[s->sn++] = cur;
if (s->bn == 1)
s->beg[s->bn++] = cur;
}
static void
strokeclosepath(struct sctx *s)
{
if (s->sn == 2)
{
strokelineto(s, s->beg[0]);
if (s->seg[1].x == s->beg[0].x && s->seg[1].y == s->beg[0].y)
linejoin(s, s->seg[0], s->beg[0], s->beg[1]);
else
linejoin(s, s->seg[1], s->beg[0], s->beg[1]);
}
else if (s->dot)
{
linedot(s, s->beg[0]);
}
s->bn = 0;
s->sn = 0;
s->dot = 0;
}
static void
strokebezier(struct sctx *s,
float xa, float ya,
float xb, float yb,
float xc, float yc,
float xd, float yd)
{
float dmax;
float xab, yab;
float xbc, ybc;
float xcd, ycd;
float xabc, yabc;
float xbcd, ybcd;
float xabcd, yabcd;
/* termination check */
dmax = ABS(xa - xb);
dmax = MAX(dmax, ABS(ya - yb));
dmax = MAX(dmax, ABS(xd - xc));
dmax = MAX(dmax, ABS(yd - yc));
if (dmax < s->flatness) {
fz_point p;
p.x = xd;
p.y = yd;
strokelineto(s, p);
return;
}
xab = xa + xb;
yab = ya + yb;
xbc = xb + xc;
ybc = yb + yc;
xcd = xc + xd;
ycd = yc + yd;
xabc = xab + xbc;
yabc = yab + ybc;
xbcd = xbc + xcd;
ybcd = ybc + ycd;
xabcd = xabc + xbcd;
yabcd = yabc + ybcd;
xab *= 0.5f; yab *= 0.5f;
xbc *= 0.5f; ybc *= 0.5f;
xcd *= 0.5f; ycd *= 0.5f;
xabc *= 0.25f; yabc *= 0.25f;
xbcd *= 0.25f; ybcd *= 0.25f;
xabcd *= 0.125f; yabcd *= 0.125f;
strokebezier(s, xa, ya, xab, yab, xabc, yabc, xabcd, yabcd);
strokebezier(s, xabcd, yabcd, xbcd, ybcd, xcd, ycd, xd, yd);
}
void
fz_strokepath(fz_gel *gel, fz_path *path, fz_matrix ctm, float flatness, float linewidth)
{
struct sctx s;
fz_point p0, p1, p2, p3;
int i;
s.gel = gel;
s.ctm = &ctm;
s.flatness = flatness;
s.linecap = path->linecap;
s.linejoin = path->linejoin;
s.linewidth = linewidth * 0.5; /* hairlines use a different value from the path value */
s.miterlimit = path->miterlimit;
s.sn = 0;
s.bn = 0;
s.dot = 0;
i = 0;
if (path->len > 0 && path->els[0].k != FZ_MOVETO)
fz_warn("assert: path must begin with moveto");
p0.x = p0.y = 0;
while (i < path->len)
{
switch (path->els[i++].k)
{
case FZ_MOVETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
strokemoveto(&s, p1);
p0 = p1;
break;
case FZ_LINETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
strokelineto(&s, p1);
p0 = p1;
break;
case FZ_CURVETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
p2.x = path->els[i++].v;
p2.y = path->els[i++].v;
p3.x = path->els[i++].v;
p3.y = path->els[i++].v;
strokebezier(&s, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
p0 = p3;
break;
case FZ_CLOSEPATH:
strokeclosepath(&s);
break;
}
}
strokeflush(&s);
}
static void
dashmoveto(struct sctx *s, fz_point a)
{
s->toggle = 1;
s->offset = 0;
s->phase = s->dashphase;
while (s->phase >= s->dashlist[s->offset])
{
s->toggle = !s->toggle;
s->phase -= s->dashlist[s->offset];
s->offset ++;
if (s->offset == s->dashlen)
s->offset = 0;
}
s->cur = a;
if (s->toggle)
strokemoveto(s, a);
}
static void
dashlineto(struct sctx *s, fz_point b)
{
float dx, dy;
float total, used, ratio;
fz_point a;
fz_point m;
a = s->cur;
dx = b.x - a.x;
dy = b.y - a.y;
total = sqrt(dx * dx + dy * dy);
used = 0;
while (total - used > s->dashlist[s->offset] - s->phase)
{
used += s->dashlist[s->offset] - s->phase;
ratio = used / total;
m.x = a.x + ratio * dx;
m.y = a.y + ratio * dy;
if (s->toggle)
strokelineto(s, m);
else
strokemoveto(s, m);
s->toggle = !s->toggle;
s->phase = 0;
s->offset ++;
if (s->offset == s->dashlen)
s->offset = 0;
}
s->phase += total - used;
s->cur = b;
if (s->toggle)
strokelineto(s, b);
}
static void
dashbezier(struct sctx *s,
float xa, float ya,
float xb, float yb,
float xc, float yc,
float xd, float yd)
{
float dmax;
float xab, yab;
float xbc, ybc;
float xcd, ycd;
float xabc, yabc;
float xbcd, ybcd;
float xabcd, yabcd;
/* termination check */
dmax = ABS(xa - xb);
dmax = MAX(dmax, ABS(ya - yb));
dmax = MAX(dmax, ABS(xd - xc));
dmax = MAX(dmax, ABS(yd - yc));
if (dmax < s->flatness) {
fz_point p;
p.x = xd;
p.y = yd;
dashlineto(s, p);
return;
}
xab = xa + xb;
yab = ya + yb;
xbc = xb + xc;
ybc = yb + yc;
xcd = xc + xd;
ycd = yc + yd;
xabc = xab + xbc;
yabc = yab + ybc;
xbcd = xbc + xcd;
ybcd = ybc + ycd;
xabcd = xabc + xbcd;
yabcd = yabc + ybcd;
xab *= 0.5f; yab *= 0.5f;
xbc *= 0.5f; ybc *= 0.5f;
xcd *= 0.5f; ycd *= 0.5f;
xabc *= 0.25f; yabc *= 0.25f;
xbcd *= 0.25f; ybcd *= 0.25f;
xabcd *= 0.125f; yabcd *= 0.125f;
dashbezier(s, xa, ya, xab, yab, xabc, yabc, xabcd, yabcd);
dashbezier(s, xabcd, yabcd, xbcd, ybcd, xcd, ycd, xd, yd);
}
void
fz_dashpath(fz_gel *gel, fz_path *path, fz_matrix ctm, float flatness, float linewidth)
{
struct sctx s;
fz_point p0, p1, p2, p3, beg;
int i;
s.gel = gel;
s.ctm = &ctm;
s.flatness = flatness;
s.linecap = path->linecap;
s.linejoin = path->linejoin;
s.linewidth = linewidth * 0.5;
s.miterlimit = path->miterlimit;
s.sn = 0;
s.bn = 0;
s.dot = 0;
s.dashlist = path->dashlist;
s.dashphase = path->dashphase;
s.dashlen = path->dashlen;
s.toggle = 0;
s.offset = 0;
s.phase = 0;
i = 0;
if (path->len > 0 && path->els[0].k != FZ_MOVETO)
fz_warn("assert: path must begin with moveto");
p0.x = p0.y = 0;
while (i < path->len)
{
switch (path->els[i++].k)
{
case FZ_MOVETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
dashmoveto(&s, p1);
beg = p0 = p1;
break;
case FZ_LINETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
dashlineto(&s, p1);
p0 = p1;
break;
case FZ_CURVETO:
p1.x = path->els[i++].v;
p1.y = path->els[i++].v;
p2.x = path->els[i++].v;
p2.y = path->els[i++].v;
p3.x = path->els[i++].v;
p3.y = path->els[i++].v;
dashbezier(&s, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
p0 = p3;
break;
case FZ_CLOSEPATH:
dashlineto(&s, beg);
break;
}
}
strokeflush(&s);
}
|