summaryrefslogtreecommitdiff
path: root/fitz/res_path.c
blob: 894e25a81cfbe87c93f23d336225e254e9cc99fc (plain)
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
#include <assert.h>
#include "fitz.h"

fz_path *
fz_new_path(fz_context *ctx)
{
	fz_path *path;

	path = fz_malloc(ctx, sizeof(fz_path));
	path->len = 0;
	path->cap = 0;
	path->items = NULL;

	return path;
}

fz_path *
fz_clone_path(fz_context *ctx, fz_path *old)
{
	fz_path *path;

	assert(old);
	path = fz_malloc(ctx, sizeof(fz_path));
	path->len = old->len;
	path->cap = old->len;
	path->items = fz_malloc_array(ctx, path->cap, sizeof(fz_path_item));
	memcpy(path->items, old->items, sizeof(fz_path_item) * path->len);

	return path;
}

void
fz_free_path(fz_context *ctx, fz_path *path)
{
	fz_free(ctx, path->items);
	fz_free(ctx, path);
}

static void
grow_path(fz_context *ctx, fz_path *path, int n)
{
	if (path->len + n < path->cap)
		return;
	while (path->len + n > path->cap)
		path->cap = path->cap + 36;
	path->items = fz_resize_array(ctx, path->items, path->cap, sizeof(fz_path_item));
}

void
fz_moveto(fz_context *ctx, fz_path *path, float x, float y)
{
	grow_path(ctx, path, 3);
	path->items[path->len++].k = FZ_MOVETO;
	path->items[path->len++].v = x;
	path->items[path->len++].v = y;
}

void
fz_lineto(fz_context *ctx, fz_path *path, float x, float y)
{
	if (path->len == 0)
	{
		fz_warn(ctx, "lineto with no current point");
		return;
	}
	grow_path(ctx, path, 3);
	path->items[path->len++].k = FZ_LINETO;
	path->items[path->len++].v = x;
	path->items[path->len++].v = y;
}

void
fz_curveto(fz_context *ctx, fz_path *path,
	float x1, float y1,
	float x2, float y2,
	float x3, float y3)
{
	if (path->len == 0)
	{
		fz_warn(ctx, "curveto with no current point");
		return;
	}
	grow_path(ctx, path, 7);
	path->items[path->len++].k = FZ_CURVETO;
	path->items[path->len++].v = x1;
	path->items[path->len++].v = y1;
	path->items[path->len++].v = x2;
	path->items[path->len++].v = y2;
	path->items[path->len++].v = x3;
	path->items[path->len++].v = y3;
}

void
fz_curvetov(fz_context *ctx, fz_path *path, float x2, float y2, float x3, float y3)
{
	float x1, y1;
	if (path->len == 0)
	{
		fz_warn(ctx, "curvetov with no current point");
		return;
	}
	x1 = path->items[path->len-2].v;
	y1 = path->items[path->len-1].v;
	fz_curveto(ctx, path, x1, y1, x2, y2, x3, y3);
}

void
fz_curvetoy(fz_context *ctx, fz_path *path, float x1, float y1, float x3, float y3)
{
	fz_curveto(ctx, path, x1, y1, x3, y3, x3, y3);
}

void
fz_closepath(fz_context *ctx, fz_path *path)
{
	if (path->len == 0)
	{
		fz_warn(ctx, "closepath with no current point");
		return;
	}
	grow_path(ctx, path, 1);
	path->items[path->len++].k = FZ_CLOSE_PATH;
}

static inline fz_rect bound_expand(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_bound_path(fz_path *path, fz_stroke_state *stroke, fz_matrix ctm)
{
	fz_point p;
	fz_rect r = fz_empty_rect;
	int i = 0;

	if (path->len)
	{
		p.x = path->items[1].v;
		p.y = path->items[2].v;
		p = fz_transform_point(ctm, p);
		r.x0 = r.x1 = p.x;
		r.y0 = r.y1 = p.y;
	}

	while (i < path->len)
	{
		switch (path->items[i++].k)
		{
		case FZ_CURVETO:
			p.x = path->items[i++].v;
			p.y = path->items[i++].v;
			r = bound_expand(r, fz_transform_point(ctm, p));
			p.x = path->items[i++].v;
			p.y = path->items[i++].v;
			r = bound_expand(r, fz_transform_point(ctm, p));
			p.x = path->items[i++].v;
			p.y = path->items[i++].v;
			r = bound_expand(r, fz_transform_point(ctm, p));
			break;
		case FZ_MOVETO:
		case FZ_LINETO:
			p.x = path->items[i++].v;
			p.y = path->items[i++].v;
			r = bound_expand(r, fz_transform_point(ctm, p));
			break;
		case FZ_CLOSE_PATH:
			break;
		}
	}

	if (stroke)
	{
		float miterlength = stroke->miterlimit;
		float linewidth = stroke->linewidth;
		float expand = MAX(miterlength, linewidth) * 0.5f;
		r.x0 -= expand;
		r.y0 -= expand;
		r.x1 += expand;
		r.y1 += expand;
	}

	return r;
}

void
fz_transform_path(fz_path *path, fz_matrix ctm)
{
	fz_point p;
	int k, i = 0;

	while (i < path->len)
	{
		switch (path->items[i++].k)
		{
		case FZ_CURVETO:
			for (k = 0; k < 3; k++)
			{
				p.x = path->items[i].v;
				p.y = path->items[i+1].v;
				p = fz_transform_point(ctm, p);
				path->items[i].v = p.x;
				path->items[i+1].v = p.y;
				i += 2;
			}
			break;
		case FZ_MOVETO:
		case FZ_LINETO:
			p.x = path->items[i].v;
			p.y = path->items[i+1].v;
			p = fz_transform_point(ctm, p);
			path->items[i].v = p.x;
			path->items[i+1].v = p.y;
			i += 2;
			break;
		case FZ_CLOSE_PATH:
			break;
		}
	}
}

void
fz_debug_path(fz_path *path, int indent)
{
	float x, y;
	int i = 0;
	int n;
	while (i < path->len)
	{
		for (n = 0; n < indent; n++)
			putchar(' ');
		switch (path->items[i++].k)
		{
		case FZ_MOVETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			printf("%g %g m\n", x, y);
			break;
		case FZ_LINETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			printf("%g %g l\n", x, y);
			break;
		case FZ_CURVETO:
			x = path->items[i++].v;
			y = path->items[i++].v;
			printf("%g %g ", x, y);
			x = path->items[i++].v;
			y = path->items[i++].v;
			printf("%g %g ", x, y);
			x = path->items[i++].v;
			y = path->items[i++].v;
			printf("%g %g c\n", x, y);
			break;
		case FZ_CLOSE_PATH:
			printf("h\n");
			break;
		}
	}
}