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
|
#include "fitz_base.h"
#include "fitz_tree.h"
fz_error
fz_newpathnode(fz_pathnode **pathp)
{
fz_pathnode *path;
path = *pathp = fz_malloc(sizeof(fz_pathnode));
if (!path)
return fz_rethrow(-1, "out of memory");
fz_initnode((fz_node*)path, FZ_NPATH);
path->paint = FZ_FILL;
path->linecap = 0;
path->linejoin = 0;
path->linewidth = 1.0;
path->miterlimit = 10.0;
path->dash = nil;
path->len = 0;
path->cap = 0;
path->els = nil;
return fz_okay;
}
fz_error
fz_clonepathnode(fz_pathnode **pathp, fz_pathnode *oldpath)
{
fz_pathnode *path;
path = *pathp = fz_malloc(sizeof(fz_pathnode));
if (!path)
return fz_rethrow(-1, "out of memory");
fz_initnode((fz_node*)path, FZ_NPATH);
path->paint = FZ_FILL;
path->linecap = 0;
path->linejoin = 0;
path->linewidth = 1.0;
path->miterlimit = 10.0;
path->dash = nil;
path->len = oldpath->len;
path->cap = oldpath->len;
path->els = fz_malloc(sizeof (fz_pathel) * path->len);
if (!path->els) {
fz_free(path);
return fz_rethrow(-1, "out of memory");
}
memcpy(path->els, oldpath->els, sizeof(fz_pathel) * path->len);
return fz_okay;
}
void
fz_droppathnode(fz_pathnode *node)
{
fz_free(node->dash);
fz_free(node->els);
}
static fz_error
growpath(fz_pathnode *path, int n)
{
int newcap;
fz_pathel *newels;
while (path->len + n > path->cap)
{
newcap = path->cap + 36;
newels = fz_realloc(path->els, sizeof (fz_pathel) * newcap);
if (!newels)
return fz_rethrow(-1, "out of memory");
path->cap = newcap;
path->els = newels;
}
return fz_okay;
}
fz_error
fz_moveto(fz_pathnode *path, float x, float y)
{
if (growpath(path, 3) != fz_okay)
return fz_rethrow(-1, "out of memory");
path->els[path->len++].k = FZ_MOVETO;
path->els[path->len++].v = x;
path->els[path->len++].v = y;
return fz_okay;
}
fz_error
fz_lineto(fz_pathnode *path, float x, float y)
{
if (path->len == 0)
return fz_throw("no current point");
if (growpath(path, 3) != fz_okay)
return fz_rethrow(-1, "out of memory");
path->els[path->len++].k = FZ_LINETO;
path->els[path->len++].v = x;
path->els[path->len++].v = y;
return fz_okay;
}
fz_error
fz_curveto(fz_pathnode *path,
float x1, float y1,
float x2, float y2,
float x3, float y3)
{
if (path->len == 0)
return fz_throw("no current point");
if (growpath(path, 7) != fz_okay)
return fz_rethrow(-1, "out of memory");
path->els[path->len++].k = FZ_CURVETO;
path->els[path->len++].v = x1;
path->els[path->len++].v = y1;
path->els[path->len++].v = x2;
path->els[path->len++].v = y2;
path->els[path->len++].v = x3;
path->els[path->len++].v = y3;
return fz_okay;
}
fz_error
fz_curvetov(fz_pathnode *path, float x2, float y2, float x3, float y3)
{
float x1 = path->els[path->len-2].v;
float y1 = path->els[path->len-1].v;
return fz_curveto(path, x1, y1, x2, y2, x3, y3);
}
fz_error
fz_curvetoy(fz_pathnode *path, float x1, float y1, float x3, float y3)
{
return fz_curveto(path, x1, y1, x3, y3, x3, y3);
}
fz_error
fz_closepath(fz_pathnode *path)
{
if (path->len == 0)
{
fz_warn("tried to close an empty path");
return fz_okay;
}
if (growpath(path, 1) != fz_okay)
return fz_rethrow(-1, "out of memory");
path->els[path->len++].k = FZ_CLOSEPATH;
return fz_okay;
}
fz_error
fz_endpath(fz_pathnode *path, fz_pathkind paint, fz_stroke *stroke, fz_dash *dash)
{
if (path->len == 0)
fz_warn("creating an empty path");
path->paint = paint;
path->dash = dash;
if (stroke)
{
path->linecap = stroke->linecap;
path->linejoin = stroke->linejoin;
path->linewidth = stroke->linewidth;
path->miterlimit = stroke->miterlimit;
}
if (path->linewidth < 0.01)
path->linewidth = 0.01;
return fz_okay;
}
static inline fz_rect boundexpand(fz_rect r, fz_point p)
{
if (p.x < r.x0) r.x0 = p.x;
if (p.y < r.y0) r.y0 = p.y;
if (p.x > r.x1) r.x1 = p.x;
if (p.y > r.y1) r.y1 = p.y;
return r;
}
fz_rect
fz_boundpathnode(fz_pathnode *path, fz_matrix ctm)
{
fz_point p;
fz_rect r = fz_emptyrect;
int i = 0;
if (path->len)
{
p.x = path->els[1].v;
p.y = path->els[2].v;
p = fz_transformpoint(ctm, p);
r.x0 = r.x1 = p.x;
r.y0 = r.y1 = p.y;
}
while (i < path->len)
{
switch (path->els[i++].k)
{
case FZ_CURVETO:
p.x = path->els[i++].v;
p.y = path->els[i++].v;
r = boundexpand(r, fz_transformpoint(ctm, p));
p.x = path->els[i++].v;
p.y = path->els[i++].v;
r = boundexpand(r, fz_transformpoint(ctm, p));
case FZ_MOVETO:
case FZ_LINETO:
p.x = path->els[i++].v;
p.y = path->els[i++].v;
r = boundexpand(r, fz_transformpoint(ctm, p));
break;
case FZ_CLOSEPATH:
break;
}
}
if (path->paint == FZ_STROKE)
{
float miterlength = sin(path->miterlimit / 2.0);
float linewidth = path->linewidth;
float expand = MAX(miterlength, linewidth) / 2.0;
r.x0 -= expand;
r.y0 -= expand;
r.x1 += expand;
r.y1 += expand;
}
return r;
}
void
fz_printpathnode(fz_pathnode *path, int indent)
{
float x, y;
int i = 0;
int n;
while (i < path->len)
{
for (n = 0; n < indent; n++)
putchar(' ');
switch (path->els[i++].k)
{
case FZ_MOVETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("%g %g m\n", x, y);
break;
case FZ_LINETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("%g %g l\n", x, y);
break;
case FZ_CURVETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("%g %g ", x, y);
x = path->els[i++].v;
y = path->els[i++].v;
printf("%g %g ", x, y);
x = path->els[i++].v;
y = path->els[i++].v;
printf("%g %g c\n", x, y);
break;
case FZ_CLOSEPATH:
printf("h\n");
}
}
for (n = 0; n < indent; n++)
putchar(' ');
switch (path->paint)
{
case FZ_STROKE:
printf("S\n");
break;
case FZ_FILL:
printf("f\n");
break;
case FZ_EOFILL:
printf("f*\n");
break;
}
}
void
fz_debugpathnode(fz_pathnode *path, int indent)
{
float x, y;
int i = 0;
int n;
while (i < path->len)
{
for (n = 0; n < indent; n++)
putchar(' ');
switch (path->els[i++].k)
{
case FZ_MOVETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("<moveto x=\"%g\" y=\"%g\" />\n", x, y);
break;
case FZ_LINETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("<lineto x=\"%g\" y=\"%g\" />\n", x, y);
break;
case FZ_CURVETO:
x = path->els[i++].v;
y = path->els[i++].v;
printf("<curveto x1=\"%g\" y1=\"%g\" ", x, y);
x = path->els[i++].v;
y = path->els[i++].v;
printf("x2=\"%g\" y2=\"%g\" ", x, y);
x = path->els[i++].v;
y = path->els[i++].v;
printf("x3=\"%g\" y3=\"%g\" />\n", x, y);
break;
case FZ_CLOSEPATH:
printf("<closepath />\n");
}
}
}
fz_error
fz_newdash(fz_dash **dashp, float phase, int len, float *array)
{
fz_dash *dash;
int i;
dash = *dashp = fz_malloc(sizeof(fz_dash) + sizeof(float) * len);
if (!dash)
return fz_rethrow(-1, "out of memory");
dash->len = len;
dash->phase = phase;
for (i = 0; i < len; i++)
dash->array[i] = array[i];
return fz_okay;
}
void
fz_dropdash(fz_dash *dash)
{
fz_free(dash);
}
|