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 */
29 static char* getToken(const char**p)
33 while(**p && strchr(" ,\t\n\r", **p)) {
37 while(**p && !strchr(" ,\t\n\r", **p)) {
40 result = malloc((*p)-start+1);
41 memcpy(result,start,(*p)-start+1);
42 result[(*p)-start] = 0;
46 void draw_conicto(drawer_t*draw, FPOINT* c, FPOINT* to)
48 FPOINT* pos = &draw->pos;
50 c1.x = (pos->x + 2 * c->x) / 3;
51 c1.y = (pos->y + 2 * c->y) / 3;
52 c2.x = (2 * c->x + to->x) / 3;
53 c2.y = (2 * c->y + to->y) / 3;
54 draw_cubicto(draw, &c1,&c2,to);
59 void draw_string(drawer_t*draw, const char*string)
61 const char*p = string;
63 char*token = getToken(&p);
64 if(!strncmp(token, "moveTo", 6)) {
66 to.x = atoi(getToken(&p));
67 to.y = atoi(getToken(&p));
68 draw->moveTo(draw, &to);
70 else if(!strncmp(token, "lineTo", 6)) {
72 to.x = atoi(getToken(&p));
73 to.y = atoi(getToken(&p));
74 draw->lineTo(draw, &to);
76 else if(!strncmp(token, "curveTo", 7) || !strncmp(token, "splineTo", 8)) {
78 mid.x = atoi(getToken(&p));
79 mid.y = atoi(getToken(&p));
80 to.x = atoi(getToken(&p));
81 to.y = atoi(getToken(&p));
82 draw->splineTo(draw, &mid, &to);
95 struct SPLINEPOINT start;
96 struct SPLINEPOINT control;
97 struct SPLINEPOINT end;
102 struct SPLINEPOINT start;
103 struct SPLINEPOINT control1;
104 struct SPLINEPOINT control2;
105 struct SPLINEPOINT end;
108 /* move the control point so that the spline runs through the original
110 static void fixcp(struct qspline*s)
112 struct SPLINEPOINT mid,dir;
113 mid.x = (s->end.x + s->start.x)/2;
114 mid.y = (s->end.y + s->start.y)/2;
115 dir.x = s->control.x - mid.x;
116 dir.y = s->control.y - mid.y;
117 s->control.x = mid.x + 2*dir.x;
118 s->control.y = mid.y + 2*dir.y;
121 static inline struct SPLINEPOINT cspline_getpoint(struct cspline*s, double t)
123 struct SPLINEPOINT p;
124 p.x= s->end.x*t*t*t + 3*s->control2.x*t*t*(1-t)
125 + 3*s->control1.x*t*(1-t)*(1-t) + s->start.x*(1-t)*(1-t)*(1-t);
126 p.y= s->end.y*t*t*t + 3*s->control2.y*t*t*(1-t)
127 + 3*s->control1.y*t*(1-t)*(1-t) + s->start.y*(1-t)*(1-t)*(1-t);
130 static struct SPLINEPOINT qspline_getpoint(struct qspline*s, double t)
132 struct SPLINEPOINT p;
133 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
134 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
138 static struct SPLINEPOINT cspline_getderivative(struct cspline*s, double t)
140 struct SPLINEPOINT d;
141 d.x = s->end.x*(3*t*t) + 3*s->control2.x*(2*t-3*t*t) +
142 3*s->control1.x*(1-4*t+3*t*t) + s->start.x*(-3+6*t-3*t*t);
143 d.y = s->end.y*(3*t*t) + 3*s->control2.y*(2*t-3*t*t) +
144 3*s->control1.y*(1-4*t+3*t*t) + s->start.y*(-3+6*t-3*t*t);
148 static int approximate2(struct cspline*s, struct qspline*q, double quality, double start, double end, int max, int depth)
151 struct SPLINEPOINT qr1,qr2,cr1,cr2;
153 double qquality = quality*quality;
159 test.start = cspline_getpoint(s, start);
160 test.control = cspline_getpoint(s, (start+end)/2);
161 test.end = cspline_getpoint(s, end);
165 test.control = cspline_getderivative(s, start);
166 test.control.x *= (end-start)/2;
167 test.control.y *= (end-start)/2;
168 test.control.x += test.start.x;
169 test.control.y += test.start.y;
171 test.control = cspline_getderivative(s, end);
172 test.control.x *= -(end-start)/2;
173 test.control.y *= -(end-start)/2;
174 test.control.x += test.end.x;
175 test.control.y += test.end.y;
178 for(t=0;t<probes;t++) {
179 double pos = 0.5/(probes*2)*(t*2+1);
181 qr1 = qspline_getpoint(&test, pos);
182 cr1 = cspline_getpoint(s, start+pos*(end-start));
191 qr2 = qspline_getpoint(&test, (1-pos));
192 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
203 if(recurse && (end-start)>1.0/120 && max-depth > 0) {
204 /* quality is too bad, split it up recursively */
205 num += approximate2(s, q, quality, start, (start+end)/2, max, depth+1);
208 num += approximate2(s, q, quality, (start+end)/2, end, max, depth+1);
216 void draw_cubicto(drawer_t*draw, FPOINT* control1, FPOINT* control2, FPOINT* to)
218 struct qspline q[128];
220 c.start.x = draw->pos.x;
221 c.start.y = draw->pos.y;
222 c.control1.x = control1->x;
223 c.control1.y = control1->y;
224 c.control2.x = control2->x;
225 c.control2.y = control2->y;
228 double quality = 0.8;
229 double maxerror = (500-(quality*5)>1?500-(quality*5):1)/20.0;
231 int num = approximate2(&c, q, maxerror, 0.0, 1.0, 128, 0);
236 mid.x = q[t].control.x;
237 mid.y = q[t].control.y;
240 draw->splineTo(draw, &mid, &to);