f0d3d348b01cee9125bd42debcd7c3af105e1ddf
[swftools.git] / lib / drawer.c
1 /*  drawer.c 
2     part of swftools
3
4     A generic structure for providing vector drawing.
5     (Helper routines, spline approximation, simple text drawers)
6
7     Copyright (C) 2003 Matthias Kramm <kramm@quiss.org>
8
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.
13
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.
18
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 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #include <math.h>
27 #include "drawer.h"
28
29 static char* getToken(const char**p)
30 {
31     const char*start;
32     char*result;
33     while(**p && strchr(" ,\t\n\r", **p)) {
34         (*p)++;
35     } 
36     start = *p;
37     while(**p && !strchr(" ,\t\n\r", **p)) {
38         (*p)++;
39     }
40     result = malloc((*p)-start+1);
41     memcpy(result,start,(*p)-start+1);
42     result[(*p)-start] = 0;
43     return result;
44 }
45
46 void draw_conicto(drawer_t*draw, FPOINT*  c, FPOINT*  to)
47 {
48     FPOINT* pos = &draw->pos;
49     FPOINT c1,c2;
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);
55
56     draw->pos = *to;
57 }
58
59 void draw_string(drawer_t*draw, const char*string)
60 {
61     const char*p = string;
62     while(*p) {
63         char*token = getToken(&p);
64         if(!strncmp(token, "moveTo", 6)) {
65             FPOINT to;
66             to.x = atoi(getToken(&p));
67             to.y = atoi(getToken(&p));
68             draw->moveTo(draw, &to);
69         }
70         else if(!strncmp(token, "lineTo", 6)) {
71             FPOINT to;
72             to.x = atoi(getToken(&p));
73             to.y = atoi(getToken(&p));
74             draw->lineTo(draw, &to);
75         }
76         else if(!strncmp(token, "curveTo", 7) || !strncmp(token, "splineTo", 8)) {
77             FPOINT mid,to;
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);
83         }
84         free(token);
85     }
86 }
87
88 struct SPLINEPOINT
89 {
90     double x,y;
91 };
92
93 struct qspline
94 {
95     struct SPLINEPOINT start;
96     struct SPLINEPOINT control;
97     struct SPLINEPOINT end;
98 };
99
100 struct cspline
101 {
102     struct SPLINEPOINT start;
103     struct SPLINEPOINT control1;
104     struct SPLINEPOINT control2;
105     struct SPLINEPOINT end;
106 };
107
108 /* move the control point so that the spline runs through the original
109    control point */
110 static void fixcp(struct qspline*s)
111 {
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;
119 }
120
121 static inline struct SPLINEPOINT cspline_getpoint(struct cspline*s, double t)
122 {
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);
128     return p;
129 }
130 static struct SPLINEPOINT qspline_getpoint(struct qspline*s, double t)
131 {
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);
135     return p;
136 }
137
138 static struct SPLINEPOINT cspline_getderivative(struct cspline*s, double t)
139 {
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);
145     return d;
146 }
147
148 static int approximate2(struct cspline*s, struct qspline*q, double quality, double start, double end, int max, int depth)
149 {
150     int num=0;
151     struct SPLINEPOINT qr1,qr2,cr1,cr2;
152     double dist1,dist2;
153     double qquality = quality*quality;
154     int t;
155     int recurse = 0;
156     int probes = 15;
157     struct qspline test;
158     char left = 0;
159     test.start = cspline_getpoint(s, start);
160     test.control = cspline_getpoint(s, (start+end)/2);
161     test.end = cspline_getpoint(s, end);
162     fixcp(&test);
163
164     if(start< 0.5) {
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;
170     } else {
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;
176     }
177
178     for(t=0;t<probes;t++) {
179         double pos = 0.5/(probes*2)*(t*2+1);
180         double dx,dy;
181         qr1 = qspline_getpoint(&test, pos);
182         cr1 = cspline_getpoint(s, start+pos*(end-start));
183
184         dx = qr1.x - cr1.x;
185         dy = qr1.y - cr1.y;
186         dist1 = dx*dx+dy*dy;
187
188         if(dist1>qquality) {
189             recurse=1;break;
190         }
191         qr2 = qspline_getpoint(&test, (1-pos));
192         cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
193
194         dx = qr2.x - cr2.x;
195         dy = qr2.y - cr2.y;
196         dist2 = dx*dx+dy*dy;
197
198         if(dist2>qquality) {
199             recurse=1;break;
200         }
201     }
202
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);
206         q+=num;
207         max-=num;
208         num += approximate2(s, q, quality, (start+end)/2, end, max, depth+1);
209         return num;
210     } else {
211         *q = test;
212         return 1;
213     }
214 }
215
216 void draw_cubicto(drawer_t*draw, FPOINT*  control1, FPOINT* control2, FPOINT*  to)
217 {
218     struct qspline q[128];
219     struct cspline c;
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;
226     c.end.x = to->x;
227     c.end.y = to->y;
228     double quality = 0.8;
229     double maxerror = (500-(quality*5)>1?500-(quality*5):1)/20.0;
230
231     int num = approximate2(&c, q, maxerror, 0.0, 1.0, 128, 0);
232     int t;
233     for(t=0;t<num;t++) {
234         FPOINT mid;
235         FPOINT to;
236         mid.x = q[t].control.x;
237         mid.y = q[t].control.y;
238         to.x = q[t].end.x;
239         to.y = q[t].end.y;
240         draw->splineTo(draw, &mid, &to);
241     }
242 }
243
244