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));
40 if((int)((d->x * 5120) == (int)(x * 5120)) &&
41 (int)((d->y * 5120) == (int)(y * 5120))) {
42 /* never mind- we're already there */
55 static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
57 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
58 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
61 /* starts with a line, not with a moveto. That needs we first
62 need an explicit moveto to (0,0) */
63 linedraw_moveTo(d, 0, 0);
77 static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
79 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
80 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
83 /* starts with a line, not with a moveto. That needs we first
84 need an explicit moveto to (0,0) */
85 linedraw_moveTo(d, 0, 0);
88 l->type = gfx_splineTo;
100 static void* linedraw_result(gfxdrawer_t*d)
102 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
103 void*result = (void*)i->start;
105 memset(d, 0, sizeof(gfxdrawer_t));
109 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
111 linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
115 d->moveTo = linedraw_moveTo;
116 d->lineTo = linedraw_lineTo;
117 d->splineTo = linedraw_splineTo;
118 d->result = linedraw_result;
121 typedef struct _qspline_abc
127 typedef struct qspline_t
134 typedef struct cspline_t
142 static void mkspline(qspline_abc_t*s, double x, double y, gfxline_t*l)
145 Form 1: x = t*t*l->x + 2*t*(1-t)*l->sx + (1-t)*(1-t)*x;
146 Form 2: x = a*t*t + b*t + c
148 s->cx = x; s->bx = 2*l->sx - 2*x; s->ax = l->x - 2*l->sx + x;
149 s->cy = y; s->by = 2*l->sy - 2*y; s->ay = l->y - 2*l->sy + y;
152 static void spline_get_controlpoint(qspline_abc_t*q, double t1, double t2, double*dx, double*dy)
155 double nax = q->ax*dt*dt;
156 double nay = q->ay*dt*dt;
157 double nbx = 2*q->ax*dt*t1 + q->bx*dt;
158 double nby = 2*q->ay*dt*t1 + q->by*dt;
159 double ncx = q->ax*t1*t1 + q->bx*t1 + q->cx;
160 double ncy = q->ay*t1*t1 + q->by*t1 + q->cy;
165 static double get_spline_len(qspline_abc_t*s)
167 int parts = (int)(sqrt(fabs(s->ax) + fabs(s->ay))*3);
172 if(parts < 3) parts = 3;
174 r2 = 1.0/(parts*parts);
177 double dx = s->ax*(2*i+1)*r2 + s->bx*r;
178 double dy = s->ay*(2*i+1)*r2 + s->by*r;
179 len += sqrt(dx*dx+dy*dy);
181 /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy,
182 s->cx + s->bx + s->ax,
183 s->cy + s->by + s->ay, len,
184 sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
186 assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
191 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
194 double linepos,nextpos;
198 if(line && line->type != gfx_moveTo) {
199 fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
202 if(!r || r[0]<0 || phase<0) {
203 fprintf(stderr, "gfxtool: invalid dashes");
207 for(;line;line=line->next) {
208 if(line->type == gfx_moveTo) {
209 d->moveTo(d, line->x, line->y);
210 on = 1; nextpos = r[0]; apos = 0; linepos = 0;
211 x = line->x; y = line->y;
212 while(linepos < phase) {
213 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
215 if(linepos < phase) {
223 //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
224 } else if(line->type == gfx_lineTo) {
225 double dx = line->x - x;
226 double dy = line->y - y;
227 double len = sqrt(dx*dx+dy*dy);
230 double lineend = linepos+len;
235 assert(nextpos>=linepos);
236 //printf("(line) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
237 while(nextpos<lineend) {
238 double nx = x + vx*(nextpos-linepos);
239 double ny = y + vy*(nextpos-linepos);
240 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
241 else {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
249 //printf("lineTo %f\n", 1.0);
250 d->lineTo(d, line->x,line->y);
252 x = line->x; y = line->y;
253 } else if(line->type == gfx_splineTo) {
255 double len, lineend,lastt;
256 mkspline(&q, x, y, line);
258 len = get_spline_len(&q);
259 //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
262 lineend = linepos+len;
265 printf("%f !< %f\n", nextpos, linepos);
266 assert(nextpos>=linepos);
267 //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
268 while(nextpos<lineend) {
269 double t = (nextpos-linepos)/len;
270 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
271 double nx = q.ax*t*t+q.bx*t+q.cx;
272 double ny = q.ay*t*t+q.by*t+q.cy;
275 spline_get_controlpoint(&q, lastt, t, &sx, &sy);
276 d->splineTo(d, sx, sy, nx,ny);
277 //printf("splineTo %f\n", nextpos);
280 //printf("moveTo %f\n", nextpos);
291 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
292 d->splineTo(d, sx, sy, line->x,line->y);
293 //printf("splineTo %f\n", 1.0);
295 x = line->x; y = line->y;
300 gfxline_t * gfxline_clone(gfxline_t*line)
305 gfxline_t*n = rfx_calloc(sizeof(gfxline_t));
319 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
323 gfxdrawer_target_gfxline(&d);
324 gfxtool_draw_dashed_line(&d, line, dashes, phase);
325 result= (gfxline_t*)d.result(&d);
329 void gfxline_show(gfxline_t*l, FILE*fi)
332 if(l->type == gfx_moveTo) {
333 fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
335 if(l->type == gfx_lineTo) {
336 fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
338 if(l->type == gfx_splineTo) {
339 fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
345 void gfxline_free(gfxline_t*l)
356 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
362 double mtmt = mt*(1-t);
363 double mtmtmt = mtmt*(1-t);
364 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
365 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
366 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
367 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
370 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
373 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
374 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
378 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
380 unsigned int gran = 0;
381 unsigned int istep = 0x80000000;
382 unsigned int istart = 0;
386 while(istart<0x80000000)
388 unsigned int iend = istart + istep;
389 double start = istart/(double)0x80000000;
390 double end = iend/(double)0x80000000;
393 char left = 0,recurse=0;
398 /* create simple approximation: a qspline_t which run's through the
399 qspline_t point at 0.5 */
400 test.start = cspline_getpoint(s, start);
401 test.control = cspline_getpoint(s, (start+end)/2);
402 test.end = cspline_getpoint(s, end);
403 /* fix the control point:
404 move it so that the new spline does runs through it */
405 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
406 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
408 /* depending on where we are in the spline, we either try to match
409 the left or right tangent */
413 pos = left?start:end;
415 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
416 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
417 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
418 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
420 test.control.x *= (end-start)/2;
421 test.control.y *= (end-start)/2;
422 test.control.x += test.start.x;
423 test.control.y += test.start.y;
425 test.control.x *= -(end-start)/2;
426 test.control.y *= -(end-start)/2;
427 test.control.x += test.end.x;
428 test.control.y += test.end.y;
433 /* measure the spline's accurancy, by taking a number of probes */
434 for(t=0;t<probes;t++) {
435 gfxpoint_t qr1,qr2,cr1,cr2;
436 double pos = 0.5/(probes*2)*(t*2+1);
439 qr1 = qspline_getpoint(&test, pos);
440 cr1 = cspline_getpoint(s, start+pos*(end-start));
449 qr2 = qspline_getpoint(&test, (1-pos));
450 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
460 #else // quadratic error: *much* faster!
462 /* convert control point representation to
463 d*x^3 + c*x^2 + b*x + a */
464 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
465 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
467 /* we need to do this for the subspline between [start,end], not [0,1]
468 as a transformation of t->a*t+b does nothing to highest coefficient
469 of the spline except multiply it with a^3, we just need to modify
471 {double m = end-start;
476 /* use the integral over (f(x)-g(x))^2 between 0 and 1
477 to measure the approximation quality.
478 (it boils down to const*d^2) */
479 recurse = (dx*dx + dy*dy > quality2);
482 if(recurse && istep>1 && size-level > num) {
489 while(!(istart & istep)) {
498 void gfxdraw_conicTo(gfxdrawer_t*draw, double cx, double cy, double tox, double toy, double quality)
500 double c1x = (draw->x + 2 * cx) / 3;
501 double c1y = (draw->y + 2 * cy) / 3;
502 double c2x = (2 * cx + tox) / 3;
503 double c2y = (2 * cy + toy) / 3;
504 gfxdraw_cubicTo(draw, c1x, c1y, c2x, c2y, tox, toy, quality);
508 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y, double quality)
512 double maxerror = quality>0 ? quality : 1.0;
524 num = approximate3(&c, q, 128, maxerror);
529 mid.x = q[t].control.x;
530 mid.y = q[t].control.y;
533 draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
537 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
539 if(box.xmin==0 && box.ymin==0 && box.xmax==0 && box.ymax==0) {
544 if(x==0 && y==0) box.xmax = 0.0000001;
558 gfxbbox_t gfxline_getbbox(gfxline_t*line)
561 gfxbbox_t bbox = {0,0,0,0};
564 if(line->type == gfx_moveTo) {
566 } else if(line->type == gfx_lineTo) {
567 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
568 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
570 } else if(line->type == gfx_splineTo) {
571 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
572 bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
573 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
583 void gfxline_dump(gfxline_t*line, FILE*fi, char*prefix)
586 if(line->type == gfx_moveTo) {
587 fprintf(fi, "%smoveTo %.2f %.2f\n", prefix, line->x, line->y);
588 } else if(line->type == gfx_lineTo) {
589 fprintf(fi, "%slineTo %.2f %.2f\n", prefix, line->x, line->y);
590 } else if(line->type == gfx_splineTo) {
591 fprintf(fi, "%ssplineTo (%.2f %.2f) %.2f %.2f\n", prefix, line->sx, line->sy, line->x, line->y);
597 gfxline_t* gfxline_append(gfxline_t*line1, gfxline_t*line2)
599 gfxline_t*l = line1;;
609 void gfxline_transform(gfxline_t*line, gfxmatrix_t*matrix)
612 double x = matrix->m00*line->x + matrix->m10*line->y + matrix->tx;
613 double y = matrix->m01*line->x + matrix->m11*line->y + matrix->ty;
616 if(line->type == gfx_splineTo) {
617 double sx = matrix->m00*line->sx + matrix->m10*line->sy + matrix->tx;
618 double sy = matrix->m01*line->sx + matrix->m11*line->sy + matrix->ty;
626 void gfxmatrix_dump(gfxmatrix_t*m, FILE*fi, char*prefix)
628 fprintf(fi, "%f %f | %f\n", m->m00, m->m10, m->tx);
629 fprintf(fi, "%f %f | %f\n", m->m01, m->m11, m->ty);
632 void gfxmatrix_transform(gfxmatrix_t*m, double* v, double*dest)
634 dest[0] = m->m00*v[0] + m->m10*v[1] + m->tx;
635 dest[1] = m->m01*v[0] + m->m11*v[1] + m->ty;
637 void gfxmatrix_invert(gfxmatrix_t*m, gfxmatrix_t*dest)
639 double det = m->m00 * m->m11 - m->m10 * m->m01;
641 memset(dest, 0, sizeof(gfxmatrix_t));
645 dest->m00 = m->m11 * det;
646 dest->m01 = -m->m01 * det;
647 dest->m10 = -m->m10 * det;
648 dest->m11 = m->m00 * det;
649 dest->tx = -(dest->m00 * m->tx + dest->m10 * m->ty);
650 dest->ty = -(dest->m01 * m->tx + dest->m11 * m->ty);