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);
145 if(parts < 3) parts = 3;
148 double r = 1.0/parts;
149 double r2 = 1.0/(parts*parts);
152 double dx = s->ax*(2*i+1)*r2 + s->bx*r;
153 double dy = s->ay*(2*i+1)*r2 + s->by*r;
154 len += sqrt(dx*dx+dy*dy);
156 /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy,
157 s->cx + s->bx + s->ax,
158 s->cy + s->by + s->ay, len,
159 sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
161 assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
166 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
169 double linepos,nextpos;
173 if(line && line->type != gfx_moveTo) {
174 fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
177 if(!r || r[0]<0 || phase<0) {
178 fprintf(stderr, "gfxtool: invalid dashes");
182 for(;line;line=line->next) {
183 if(line->type == gfx_moveTo) {
184 d->moveTo(d, line->x, line->y);
185 on = 1; nextpos = r[0]; apos = 0; linepos = 0;
186 x = line->x; y = line->y;
187 while(linepos < phase) {
188 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
190 if(linepos < phase) {
198 //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
199 } else if(line->type == gfx_lineTo) {
200 double dx = line->x - x;
201 double dy = line->y - y;
202 double len = sqrt(dx*dx+dy*dy);
207 double lineend = linepos+len;
208 assert(nextpos>=linepos);
209 //printf("(line) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
210 while(nextpos<lineend) {
211 double nx = x + vx*(nextpos-linepos);
212 double ny = y + vy*(nextpos-linepos);
213 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
214 else {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
222 //printf("lineTo %f\n", 1.0);
223 d->lineTo(d, line->x,line->y);
225 x = line->x; y = line->y;
226 } else if(line->type == gfx_splineTo) {
228 mkspline(&q, x, y, line);
230 double len = get_spline_len(&q);
231 //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
234 double lineend = linepos+len;
237 printf("%f !< %f\n", nextpos, linepos);
238 assert(nextpos>=linepos);
239 //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
240 while(nextpos<lineend) {
241 double t = (nextpos-linepos)/len;
242 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
243 double nx = q.ax*t*t+q.bx*t+q.cx;
244 double ny = q.ay*t*t+q.by*t+q.cy;
247 spline_get_controlpoint(&q, lastt, t, &sx, &sy);
248 d->splineTo(d, sx, sy, nx,ny);
249 //printf("splineTo %f\n", nextpos);
252 //printf("moveTo %f\n", nextpos);
263 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
264 d->splineTo(d, sx, sy, line->x,line->y);
265 //printf("splineTo %f\n", 1.0);
267 x = line->x; y = line->y;
272 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
275 gfxdrawer_target_gfxline(&d);
276 gfxtool_draw_dashed_line(&d, line, dashes, phase);
277 gfxline_t*result= (gfxline_t*)d.result(&d);
281 void gfxline_show(gfxline_t*l, FILE*fi)
284 if(l->type == moveTo) {
285 fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
287 if(l->type == lineTo) {
288 fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
290 if(l->type == splineTo) {
291 fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
297 void gfxline_free(gfxline_t*l)
308 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
314 double mtmt = mt*(1-t);
315 double mtmtmt = mtmt*(1-t);
316 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
317 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
318 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
319 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
322 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
325 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
326 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
330 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
332 unsigned int gran = 0;
333 unsigned int istep = 0x80000000;
334 unsigned int istart = 0;
338 while(istart<0x80000000)
340 unsigned int iend = istart + istep;
341 double start = istart/(double)0x80000000;
342 double end = iend/(double)0x80000000;
345 char left = 0,recurse=0;
350 /* create simple approximation: a qspline_t which run's through the
351 qspline_t point at 0.5 */
352 test.start = cspline_getpoint(s, start);
353 test.control = cspline_getpoint(s, (start+end)/2);
354 test.end = cspline_getpoint(s, end);
355 /* fix the control point:
356 move it so that the new spline does runs through it */
357 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
358 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
360 /* depending on where we are in the spline, we either try to match
361 the left or right tangent */
365 pos = left?start:end;
367 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
368 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
369 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
370 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
372 test.control.x *= (end-start)/2;
373 test.control.y *= (end-start)/2;
374 test.control.x += test.start.x;
375 test.control.y += test.start.y;
377 test.control.x *= -(end-start)/2;
378 test.control.y *= -(end-start)/2;
379 test.control.x += test.end.x;
380 test.control.y += test.end.y;
385 /* measure the spline's accurancy, by taking a number of probes */
386 for(t=0;t<probes;t++) {
387 gfxpoint_t qr1,qr2,cr1,cr2;
388 double pos = 0.5/(probes*2)*(t*2+1);
391 qr1 = qspline_getpoint(&test, pos);
392 cr1 = cspline_getpoint(s, start+pos*(end-start));
401 qr2 = qspline_getpoint(&test, (1-pos));
402 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
412 #else // quadratic error: *much* faster!
414 /* convert control point representation to
415 d*x^3 + c*x^2 + b*x + a */
417 /* FIXME: we need to do this for the subspline between [start,end],
419 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
420 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
422 /* use the integral over (f(x)-g(x))^2 between 0 and 1
423 to measure the approximation quality.
424 (it boils down to const*d^2)
426 recurse = (dx*dx + dy*dy > quality2);
429 if(recurse && istep>1 && size-level > num) {
436 while(!(istart & istep)) {
445 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y)
449 double maxerror = 0.04;
461 num = approximate3(&c, q, 128, maxerror*maxerror);
466 mid.x = q[t].control.x;
467 mid.y = q[t].control.y;
470 draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
474 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
476 if(box.xmin==0 || box.ymin==0 || box.xmax==0 || box.ymax==0) {
481 if(x==0 && y==0) box.xmax = 0.0000001;
495 gfxbbox_t gfxline_getbbox(gfxline_t*line)
498 gfxbbox_t bbox = {0,0,0,0};
501 if(line->type == gfx_moveTo) {
503 } else if(line->type == gfx_lineTo) {
504 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
505 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
507 } else if(line->type == gfx_splineTo) {
508 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
509 bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
510 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
520 void gfxline_dump(gfxline_t*line, FILE*fi)
523 if(line->type == gfx_moveTo) {
524 fprintf(fi, "moveTo %.2f %.2f\n", line->x, line->y);
525 } else if(line->type == gfx_lineTo) {
526 fprintf(fi, "lineTo %.2f %.2f\n", line->x, line->y);
527 } else if(line->type == gfx_splineTo) {
528 fprintf(fi, "splineTo (%.2f %.2f) %.2f %.2f\n", line->sx, line->sy, line->x, line->y);