4 A generic structure for providing vector drawing.
5 (Helper routines, spline approximation, simple text drawers)
7 Copyright (C) 2003 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
30 static char* getToken(const char**p)
34 while(**p && strchr(" ,()\t\n\r", **p)) {
38 while(**p && !strchr(" ,()\t\n\r", **p)) {
41 result = malloc((*p)-start+1);
42 memcpy(result,start,(*p)-start+1);
43 result[(*p)-start] = 0;
47 void draw_conicTo(drawer_t*draw, FPOINT* c, FPOINT* to)
49 FPOINT* pos = &draw->pos;
51 c1.x = (pos->x + 2 * c->x) / 3;
52 c1.y = (pos->y + 2 * c->y) / 3;
53 c2.x = (2 * c->x + to->x) / 3;
54 c2.y = (2 * c->y + to->y) / 3;
55 draw_cubicTo(draw, &c1,&c2,to);
60 /* convenience routine */
61 static void draw_conicTo2(drawer_t*draw, double x1, double y1, double x2, double y2)
68 draw_conicTo(draw, &c1, &c2);
70 /* convenience routine */
71 static void draw_moveTo2(drawer_t*draw, double x, double y)
75 draw->moveTo(draw, &c);
77 /* convenience routine */
78 static void draw_lineTo2(drawer_t*draw, double x, double y)
82 draw->lineTo(draw, &c);
86 void draw_string(drawer_t*draw, const char*string)
88 const char*p = string;
90 char*token = getToken(&p);
93 if(!strncmp(token, "moveTo", 6) ||
94 !strncmp(token, "M", 1) //svg
97 to.x = atof(getToken(&p));
98 to.y = atof(getToken(&p));
99 draw->moveTo(draw, &to);
101 else if(!strncmp(token, "lineTo", 6) ||
102 !strncmp(token, "L", 1) //svg
105 to.x = atof(getToken(&p));
106 to.y = atof(getToken(&p));
107 draw->lineTo(draw, &to);
109 else if(!strncmp(token, "curveTo", 7) || !strncmp(token, "splineTo", 8)) {
111 mid.x = atof(getToken(&p));
112 mid.y = atof(getToken(&p));
113 to.x = atof(getToken(&p));
114 to.y = atof(getToken(&p));
115 draw->splineTo(draw, &mid, &to);
117 else if(!strncmp(token, "conicTo", 5)) {
119 mid.x = atof(getToken(&p));
120 mid.y = atof(getToken(&p));
121 to.x = atof(getToken(&p));
122 to.y = atof(getToken(&p));
123 draw_conicTo(draw, &mid, &to);
125 else if(!strncmp(token, "circle", 6)) {
127 double r2 = 0.70710678118654757*r;
128 mx = atof(getToken(&p));
129 my = atof(getToken(&p));
130 r = atof(getToken(&p));
131 draw_moveTo2(draw, mx, my-r);
132 draw_conicTo2(draw, mx+r2, my-r2, mx+r, my);
133 draw_conicTo2(draw, mx+r2, my+r2, mx, my+r);
134 draw_conicTo2(draw, mx-r2, my+r2, mx-r, my);
135 draw_conicTo2(draw, mx-r2, my-r2, mx, my-r);
137 else if(!strncmp(token, "box", 3)) {
139 x1 = atof(getToken(&p));
140 y1 = atof(getToken(&p));
141 x2 = atof(getToken(&p));
142 y2 = atof(getToken(&p));
143 draw_moveTo2(draw, x1, y1);
144 draw_lineTo2(draw, x1, y2);
145 draw_lineTo2(draw, x2, y2);
146 draw_lineTo2(draw, x2, y1);
147 draw_lineTo2(draw, x1, y1);
149 else if(!strncmp(token, "cubicTo", 5) ||
150 !strncmp(token, "C", 1) //svg
153 mid1.x = atof(getToken(&p));
154 mid1.y = atof(getToken(&p));
155 mid2.x = atof(getToken(&p));
156 mid2.y = atof(getToken(&p));
157 to.x = atof(getToken(&p));
158 to.y = atof(getToken(&p));
159 draw_cubicTo(draw, &mid1, &mid2, &to);
161 else if(!strncmp(token, "z", 1) //svg
166 fprintf(stderr, "drawer: Warning: unknown primitive '%s'\n", token);
179 struct SPLINEPOINT start;
180 struct SPLINEPOINT control;
181 struct SPLINEPOINT end;
186 struct SPLINEPOINT start;
187 struct SPLINEPOINT control1;
188 struct SPLINEPOINT control2;
189 struct SPLINEPOINT end;
192 static inline struct SPLINEPOINT cspline_getpoint(const struct cspline*s, double t)
194 struct SPLINEPOINT p;
198 double mtmt = mt*(1-t);
199 double mtmtmt = mtmt*(1-t);
200 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
201 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
202 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
203 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
206 static struct SPLINEPOINT qspline_getpoint(const struct qspline*s, double t)
208 struct SPLINEPOINT p;
209 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
210 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
214 static int approximate3(const struct cspline*s, struct qspline*q, int size, double quality2)
216 unsigned int gran = 0;
217 unsigned int istep = 0x80000000;
218 unsigned int istart = 0;
222 while(istart<0x80000000)
224 unsigned int iend = istart + istep;
225 double start = istart/(double)0x80000000;
226 double end = iend/(double)0x80000000;
229 char left = 0,recurse=0;
234 /* create simple approximation: a qspline which run's through the
235 qspline point at 0.5 */
236 test.start = cspline_getpoint(s, start);
237 test.control = cspline_getpoint(s, (start+end)/2);
238 test.end = cspline_getpoint(s, end);
239 /* fix the control point:
240 move it so that the new spline does runs through it */
241 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
242 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
244 /* depending on where we are in the spline, we either try to match
245 the left or right tangent */
249 pos = left?start:end;
251 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
252 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
253 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
254 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
256 test.control.x *= (end-start)/2;
257 test.control.y *= (end-start)/2;
258 test.control.x += test.start.x;
259 test.control.y += test.start.y;
261 test.control.x *= -(end-start)/2;
262 test.control.y *= -(end-start)/2;
263 test.control.x += test.end.x;
264 test.control.y += test.end.y;
269 /* measure the spline's accurancy, by taking a number of probes */
270 for(t=0;t<probes;t++) {
271 struct SPLINEPOINT qr1,qr2,cr1,cr2;
272 double pos = 0.5/(probes*2)*(t*2+1);
275 qr1 = qspline_getpoint(&test, pos);
276 cr1 = cspline_getpoint(s, start+pos*(end-start));
285 qr2 = qspline_getpoint(&test, (1-pos));
286 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
296 #else // quadratic error: *much* faster!
298 /* convert control point representation to
299 d*x^3 + c*x^2 + b*x + a */
301 /* FIXME: we need to do this for the subspline between [start,end],
303 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
304 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
306 /* use the integral over (f(x)-g(x))^2 between 0 and 1
307 to measure the approximation quality.
308 (it boils down to const*d^2)
310 recurse = (dx*dx + dy*dy > quality2);
313 if(recurse && istep>1 && size-level > num) {
320 while(!(istart & istep)) {
329 void draw_cubicTo(drawer_t*draw, FPOINT* control1, FPOINT* control2, FPOINT* to)
331 struct qspline q[128];
334 double maxerror = (500-(quality*5)>1?500-(quality*5):1)/20.0;
337 c.start.x = draw->pos.x;
338 c.start.y = draw->pos.y;
339 c.control1.x = control1->x;
340 c.control1.y = control1->y;
341 c.control2.x = control2->x;
342 c.control2.y = control2->y;
346 num = approximate3(&c, q, 128, maxerror*maxerror);
351 mid.x = q[t].control.x;
352 mid.y = q[t].control.y;
355 draw->splineTo(draw, &mid, &to);