3 Various utility functions for dealing with gfxdevices.
5 Part of the swftools package.
7 Copyright (c) 2005 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 typedef struct _linedraw_internal
33 } linedraw_internal_t;
35 static void linedraw_moveTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
37 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
38 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
49 static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
51 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
52 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
63 static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
65 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
66 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
67 l->type = gfx_splineTo;
79 static void* linedraw_result(gfxdrawer_t*d)
81 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
82 void*result = (void*)i->start;
84 memset(d, 0, sizeof(gfxdrawer_t));
88 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
90 linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
92 d->moveTo = linedraw_moveTo;
93 d->lineTo = linedraw_lineTo;
94 d->splineTo = linedraw_splineTo;
95 d->result = linedraw_result;
98 typedef struct _qspline_abc
104 typedef struct qspline_t
111 typedef struct cspline_t
119 static void mkspline(qspline_abc_t*s, double x, double y, gfxline_t*l)
122 Form 1: x = t*t*l->x + 2*t*(1-t)*l->sx + (1-t)*(1-t)*x;
123 Form 2: x = a*t*t + b*t + c
125 s->cx = x; s->bx = 2*l->sx - 2*x; s->ax = l->x - 2*l->sx + x;
126 s->cy = y; s->by = 2*l->sy - 2*y; s->ay = l->y - 2*l->sy + y;
129 static void spline_get_controlpoint(qspline_abc_t*q, double t1, double t2, double*dx, double*dy)
132 double nax = q->ax*dt*dt;
133 double nay = q->ay*dt*dt;
134 double nbx = 2*q->ax*dt*t1 + q->bx*dt;
135 double nby = 2*q->ay*dt*t1 + q->by*dt;
136 double ncx = q->ax*t1*t1 + q->bx*t1 + q->cx;
137 double ncy = q->ay*t1*t1 + q->by*t1 + q->cy;
142 static double get_spline_len(qspline_abc_t*s)
144 int parts = (int)(sqrt(fabs(s->ax) + fabs(s->ay))*3);
149 if(parts < 3) parts = 3;
151 r2 = 1.0/(parts*parts);
154 double dx = s->ax*(2*i+1)*r2 + s->bx*r;
155 double dy = s->ay*(2*i+1)*r2 + s->by*r;
156 len += sqrt(dx*dx+dy*dy);
158 /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy,
159 s->cx + s->bx + s->ax,
160 s->cy + s->by + s->ay, len,
161 sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
163 assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
168 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
171 double linepos,nextpos;
175 if(line && line->type != gfx_moveTo) {
176 fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
179 if(!r || r[0]<0 || phase<0) {
180 fprintf(stderr, "gfxtool: invalid dashes");
184 for(;line;line=line->next) {
185 if(line->type == gfx_moveTo) {
186 d->moveTo(d, line->x, line->y);
187 on = 1; nextpos = r[0]; apos = 0; linepos = 0;
188 x = line->x; y = line->y;
189 while(linepos < phase) {
190 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
192 if(linepos < phase) {
200 //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
201 } else if(line->type == gfx_lineTo) {
202 double dx = line->x - x;
203 double dy = line->y - y;
204 double len = sqrt(dx*dx+dy*dy);
207 double lineend = linepos+len;
212 assert(nextpos>=linepos);
213 //printf("(line) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
214 while(nextpos<lineend) {
215 double nx = x + vx*(nextpos-linepos);
216 double ny = y + vy*(nextpos-linepos);
217 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
218 else {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
226 //printf("lineTo %f\n", 1.0);
227 d->lineTo(d, line->x,line->y);
229 x = line->x; y = line->y;
230 } else if(line->type == gfx_splineTo) {
232 double len, lineend,lastt;
233 mkspline(&q, x, y, line);
235 len = get_spline_len(&q);
236 //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
239 lineend = linepos+len;
242 printf("%f !< %f\n", nextpos, linepos);
243 assert(nextpos>=linepos);
244 //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
245 while(nextpos<lineend) {
246 double t = (nextpos-linepos)/len;
247 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
248 double nx = q.ax*t*t+q.bx*t+q.cx;
249 double ny = q.ay*t*t+q.by*t+q.cy;
252 spline_get_controlpoint(&q, lastt, t, &sx, &sy);
253 d->splineTo(d, sx, sy, nx,ny);
254 //printf("splineTo %f\n", nextpos);
257 //printf("moveTo %f\n", nextpos);
268 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
269 d->splineTo(d, sx, sy, line->x,line->y);
270 //printf("splineTo %f\n", 1.0);
272 x = line->x; y = line->y;
277 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
281 gfxdrawer_target_gfxline(&d);
282 gfxtool_draw_dashed_line(&d, line, dashes, phase);
283 result= (gfxline_t*)d.result(&d);
287 void gfxline_show(gfxline_t*l, FILE*fi)
290 if(l->type == moveTo) {
291 fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
293 if(l->type == lineTo) {
294 fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
296 if(l->type == splineTo) {
297 fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
303 void gfxline_free(gfxline_t*l)
314 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
320 double mtmt = mt*(1-t);
321 double mtmtmt = mtmt*(1-t);
322 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
323 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
324 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
325 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
328 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
331 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
332 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
336 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
338 unsigned int gran = 0;
339 unsigned int istep = 0x80000000;
340 unsigned int istart = 0;
344 while(istart<0x80000000)
346 unsigned int iend = istart + istep;
347 double start = istart/(double)0x80000000;
348 double end = iend/(double)0x80000000;
351 char left = 0,recurse=0;
356 /* create simple approximation: a qspline_t which run's through the
357 qspline_t point at 0.5 */
358 test.start = cspline_getpoint(s, start);
359 test.control = cspline_getpoint(s, (start+end)/2);
360 test.end = cspline_getpoint(s, end);
361 /* fix the control point:
362 move it so that the new spline does runs through it */
363 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
364 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
366 /* depending on where we are in the spline, we either try to match
367 the left or right tangent */
371 pos = left?start:end;
373 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
374 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
375 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
376 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
378 test.control.x *= (end-start)/2;
379 test.control.y *= (end-start)/2;
380 test.control.x += test.start.x;
381 test.control.y += test.start.y;
383 test.control.x *= -(end-start)/2;
384 test.control.y *= -(end-start)/2;
385 test.control.x += test.end.x;
386 test.control.y += test.end.y;
391 /* measure the spline's accurancy, by taking a number of probes */
392 for(t=0;t<probes;t++) {
393 gfxpoint_t qr1,qr2,cr1,cr2;
394 double pos = 0.5/(probes*2)*(t*2+1);
397 qr1 = qspline_getpoint(&test, pos);
398 cr1 = cspline_getpoint(s, start+pos*(end-start));
407 qr2 = qspline_getpoint(&test, (1-pos));
408 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
418 #else // quadratic error: *much* faster!
420 /* convert control point representation to
421 d*x^3 + c*x^2 + b*x + a */
423 /* FIXME: we need to do this for the subspline between [start,end],
425 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
426 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
428 /* use the integral over (f(x)-g(x))^2 between 0 and 1
429 to measure the approximation quality.
430 (it boils down to const*d^2)
432 recurse = (dx*dx + dy*dy > quality2);
435 if(recurse && istep>1 && size-level > num) {
442 while(!(istart & istep)) {
451 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y)
455 double maxerror = 0.04;
467 num = approximate3(&c, q, 128, maxerror*maxerror);
472 mid.x = q[t].control.x;
473 mid.y = q[t].control.y;
476 draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
480 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
482 if(box.xmin==0 || box.ymin==0 || box.xmax==0 || box.ymax==0) {
487 if(x==0 && y==0) box.xmax = 0.0000001;
501 gfxbbox_t gfxline_getbbox(gfxline_t*line)
504 gfxbbox_t bbox = {0,0,0,0};
507 if(line->type == gfx_moveTo) {
509 } else if(line->type == gfx_lineTo) {
510 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
511 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
513 } else if(line->type == gfx_splineTo) {
514 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
515 bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
516 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
526 void gfxline_dump(gfxline_t*line, FILE*fi)
529 if(line->type == gfx_moveTo) {
530 fprintf(fi, "moveTo %.2f %.2f\n", line->x, line->y);
531 } else if(line->type == gfx_lineTo) {
532 fprintf(fi, "lineTo %.2f %.2f\n", line->x, line->y);
533 } else if(line->type == gfx_splineTo) {
534 fprintf(fi, "splineTo (%.2f %.2f) %.2f %.2f\n", line->sx, line->sy, line->x, line->y);