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 */
30 typedef struct _linedraw_internal
34 } linedraw_internal_t;
36 static void linedraw_moveTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
38 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
39 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
41 if((int)((d->x * 5120) == (int)(x * 5120)) &&
42 (int)((d->y * 5120) == (int)(y * 5120))) {
43 /* never mind- we're already there */
57 static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
59 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
60 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
63 /* starts with a line, not with a moveto. That needs we first
64 need an explicit moveto to (0,0) */
65 linedraw_moveTo(d, 0, 0);
79 static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
81 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
82 gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
85 /* starts with a line, not with a moveto. That needs we first
86 need an explicit moveto to (0,0) */
87 linedraw_moveTo(d, 0, 0);
90 l->type = gfx_splineTo;
102 static void* linedraw_result(gfxdrawer_t*d)
104 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
105 void*result = (void*)i->start;
107 memset(d, 0, sizeof(gfxdrawer_t));
111 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
113 linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
117 d->moveTo = linedraw_moveTo;
118 d->lineTo = linedraw_lineTo;
119 d->splineTo = linedraw_splineTo;
120 d->result = linedraw_result;
123 typedef struct _qspline_abc
129 typedef struct qspline_t
136 typedef struct cspline_t
144 static void mkspline(qspline_abc_t*s, double x, double y, gfxline_t*l)
147 Form 1: x = t*t*l->x + 2*t*(1-t)*l->sx + (1-t)*(1-t)*x;
148 Form 2: x = a*t*t + b*t + c
150 s->cx = x; s->bx = 2*l->sx - 2*x; s->ax = l->x - 2*l->sx + x;
151 s->cy = y; s->by = 2*l->sy - 2*y; s->ay = l->y - 2*l->sy + y;
154 static void spline_get_controlpoint(qspline_abc_t*q, double t1, double t2, double*dx, double*dy)
157 double nax = q->ax*dt*dt;
158 double nay = q->ay*dt*dt;
159 double nbx = 2*q->ax*dt*t1 + q->bx*dt;
160 double nby = 2*q->ay*dt*t1 + q->by*dt;
161 double ncx = q->ax*t1*t1 + q->bx*t1 + q->cx;
162 double ncy = q->ay*t1*t1 + q->by*t1 + q->cy;
167 static double get_spline_len(qspline_abc_t*s)
169 int parts = (int)(sqrt(fabs(s->ax) + fabs(s->ay))*3);
174 if(parts < 3) parts = 3;
176 r2 = 1.0/(parts*parts);
179 double dx = s->ax*(2*i+1)*r2 + s->bx*r;
180 double dy = s->ay*(2*i+1)*r2 + s->by*r;
181 len += sqrt(dx*dx+dy*dy);
183 /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy,
184 s->cx + s->bx + s->ax,
185 s->cy + s->by + s->ay, len,
186 sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
188 assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
193 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
196 double linepos,nextpos;
200 if(line && line->type != gfx_moveTo) {
201 fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
204 if(!r || r[0]<0 || phase<0) {
205 fprintf(stderr, "gfxtool: invalid dashes");
209 for(;line;line=line->next) {
210 if(line->type == gfx_moveTo) {
211 d->moveTo(d, line->x, line->y);
212 on = 1; nextpos = r[0]; apos = 0; linepos = 0;
213 x = line->x; y = line->y;
214 while(linepos < phase) {
215 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
217 if(linepos < phase) {
225 //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
226 } else if(line->type == gfx_lineTo) {
227 double dx = line->x - x;
228 double dy = line->y - y;
229 double len = sqrt(dx*dx+dy*dy);
232 double lineend = linepos+len;
237 assert(nextpos>=linepos);
238 //printf("(line) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
239 while(nextpos<lineend) {
240 double nx = x + vx*(nextpos-linepos);
241 double ny = y + vy*(nextpos-linepos);
242 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
243 else {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
251 //printf("lineTo %f\n", 1.0);
252 d->lineTo(d, line->x,line->y);
254 x = line->x; y = line->y;
255 } else if(line->type == gfx_splineTo) {
257 double len, lineend,lastt;
258 mkspline(&q, x, y, line);
260 len = get_spline_len(&q);
261 //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
264 lineend = linepos+len;
267 printf("%f !< %f\n", nextpos, linepos);
268 assert(nextpos>=linepos);
269 //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
270 while(nextpos<lineend) {
271 double t = (nextpos-linepos)/len;
272 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
273 double nx = q.ax*t*t+q.bx*t+q.cx;
274 double ny = q.ay*t*t+q.by*t+q.cy;
277 spline_get_controlpoint(&q, lastt, t, &sx, &sy);
278 d->splineTo(d, sx, sy, nx,ny);
279 //printf("splineTo %f\n", nextpos);
282 //printf("moveTo %f\n", nextpos);
293 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
294 d->splineTo(d, sx, sy, line->x,line->y);
295 //printf("splineTo %f\n", 1.0);
297 x = line->x; y = line->y;
302 gfxline_t * gfxline_clone(gfxline_t*line)
307 gfxline_t*n = rfx_calloc(sizeof(gfxline_t));
320 void gfxline_optimize(gfxline_t*line)
323 /* step 1: convert splines to lines, where possible */
326 if(l->type == gfx_splineTo) {
331 if(fabs(dx*sy - dy*sx) < 0.000001 && (dx*sx + dy*sy) >= 0) {
332 l->type = gfx_lineTo;
339 /* step 2: combine adjacent lines and splines, where possible */
341 while(l && l->next) {
342 gfxline_t*next = l->next;
345 if(l->type == gfx_lineTo && next->type == gfx_lineTo) {
348 double nx = next->x-x;
349 double ny = next->y-y;
350 if(fabs(dx*ny - dy*nx) < 0.000001 && (dx*nx + dy*ny) >= 0) {
353 } else if(l->type == gfx_splineTo && next->type == gfx_splineTo) {
357 l->next = next->next;
372 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
376 gfxdrawer_target_gfxline(&d);
377 gfxtool_draw_dashed_line(&d, line, dashes, phase);
378 result= (gfxline_t*)d.result(&d);
382 void gfxline_show(gfxline_t*l, FILE*fi)
385 if(l->type == gfx_moveTo) {
386 fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
388 if(l->type == gfx_lineTo) {
389 fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
391 if(l->type == gfx_splineTo) {
392 fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
398 void gfxline_free(gfxline_t*l)
400 if(l && (l+1) == l->next) {
414 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
420 double mtmt = mt*(1-t);
421 double mtmtmt = mtmt*(1-t);
422 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
423 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
424 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
425 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
428 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
431 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
432 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
436 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
438 unsigned int gran = 0;
439 unsigned int istep = 0x80000000;
440 unsigned int istart = 0;
444 while(istart<0x80000000)
446 unsigned int iend = istart + istep;
447 double start = istart/(double)0x80000000;
448 double end = iend/(double)0x80000000;
451 char left = 0,recurse=0;
456 /* create simple approximation: a qspline_t which run's through the
457 qspline_t point at 0.5 */
458 test.start = cspline_getpoint(s, start);
459 test.control = cspline_getpoint(s, (start+end)/2);
460 test.end = cspline_getpoint(s, end);
461 /* fix the control point:
462 move it so that the new spline does runs through it */
463 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
464 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
466 /* depending on where we are in the spline, we either try to match
467 the left or right tangent */
471 pos = left?start:end;
473 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
474 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
475 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
476 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
478 test.control.x *= (end-start)/2;
479 test.control.y *= (end-start)/2;
480 test.control.x += test.start.x;
481 test.control.y += test.start.y;
483 test.control.x *= -(end-start)/2;
484 test.control.y *= -(end-start)/2;
485 test.control.x += test.end.x;
486 test.control.y += test.end.y;
491 /* measure the spline's accurancy, by taking a number of probes */
492 for(t=0;t<probes;t++) {
493 gfxpoint_t qr1,qr2,cr1,cr2;
494 double pos = 0.5/(probes*2)*(t*2+1);
497 qr1 = qspline_getpoint(&test, pos);
498 cr1 = cspline_getpoint(s, start+pos*(end-start));
507 qr2 = qspline_getpoint(&test, (1-pos));
508 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
518 #else // quadratic error: *much* faster!
520 /* convert control point representation to
521 d*x^3 + c*x^2 + b*x + a */
522 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
523 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
525 /* we need to do this for the subspline between [start,end], not [0,1]
526 as a transformation of t->a*t+b does nothing to highest coefficient
527 of the spline except multiply it with a^3, we just need to modify
529 {double m = end-start;
534 /* use the integral over (f(x)-g(x))^2 between 0 and 1
535 to measure the approximation quality.
536 (it boils down to const*d^2) */
537 recurse = (dx*dx + dy*dy > quality2);
540 if(recurse && istep>1 && size-level > num) {
547 while(!(istart & istep)) {
556 void gfxdraw_conicTo(gfxdrawer_t*draw, double cx, double cy, double tox, double toy, double quality)
558 double c1x = (draw->x + 2 * cx) / 3;
559 double c1y = (draw->y + 2 * cy) / 3;
560 double c2x = (2 * cx + tox) / 3;
561 double c2y = (2 * cy + toy) / 3;
562 gfxdraw_cubicTo(draw, c1x, c1y, c2x, c2y, tox, toy, quality);
566 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y, double quality)
570 double maxerror = quality>0 ? quality : 1.0;
582 num = approximate3(&c, q, 128, maxerror);
587 mid.x = q[t].control.x;
588 mid.y = q[t].control.y;
591 draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
595 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
597 if(box.xmin==0 && box.ymin==0 && box.xmax==0 && box.ymax==0) {
602 if(x==0 && y==0) box.xmax = 0.0000001;
616 gfxbbox_t gfxline_getbbox(gfxline_t*line)
619 gfxbbox_t bbox = {0,0,0,0};
622 if(line->type == gfx_moveTo) {
624 } else if(line->type == gfx_lineTo) {
625 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
626 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
628 } else if(line->type == gfx_splineTo) {
629 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
630 bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
631 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
641 void gfxline_dump(gfxline_t*line, FILE*fi, char*prefix)
644 if(line->type == gfx_moveTo) {
645 fprintf(fi, "%smoveTo %.2f %.2f\n", prefix, line->x, line->y);
646 } else if(line->type == gfx_lineTo) {
647 fprintf(fi, "%slineTo %.2f %.2f\n", prefix, line->x, line->y);
648 } else if(line->type == gfx_splineTo) {
649 fprintf(fi, "%ssplineTo (%.2f %.2f) %.2f %.2f\n", prefix, line->sx, line->sy, line->x, line->y);
655 gfxline_t* gfxline_append(gfxline_t*line1, gfxline_t*line2)
657 gfxline_t*l = line1;;
667 void gfxline_transform(gfxline_t*line, gfxmatrix_t*matrix)
670 double x = matrix->m00*line->x + matrix->m10*line->y + matrix->tx;
671 double y = matrix->m01*line->x + matrix->m11*line->y + matrix->ty;
674 if(line->type == gfx_splineTo) {
675 double sx = matrix->m00*line->sx + matrix->m10*line->sy + matrix->tx;
676 double sy = matrix->m01*line->sx + matrix->m11*line->sy + matrix->ty;
684 void gfxmatrix_dump(gfxmatrix_t*m, FILE*fi, char*prefix)
686 fprintf(fi, "%f %f | %f\n", m->m00, m->m10, m->tx);
687 fprintf(fi, "%f %f | %f\n", m->m01, m->m11, m->ty);
690 void gfxmatrix_transform(gfxmatrix_t*m, double* v, double*dest)
692 dest[0] = m->m00*v[0] + m->m10*v[1] + m->tx;
693 dest[1] = m->m01*v[0] + m->m11*v[1] + m->ty;
695 void gfxmatrix_invert(gfxmatrix_t*m, gfxmatrix_t*dest)
697 double det = m->m00 * m->m11 - m->m10 * m->m01;
699 memset(dest, 0, sizeof(gfxmatrix_t));
703 dest->m00 = m->m11 * det;
704 dest->m01 = -m->m01 * det;
705 dest->m10 = -m->m10 * det;
706 dest->m11 = m->m00 * det;
707 dest->tx = -(dest->m00 * m->tx + dest->m10 * m->ty);
708 dest->ty = -(dest->m01 * m->tx + dest->m11 * m->ty);
710 void gfxmatrix_unit(gfxmatrix_t*m)
712 memset(m, 0, sizeof(gfxmatrix_t));
716 void gfxmatrix_multiply(gfxmatrix_t*m1, gfxmatrix_t*m2, gfxmatrix_t*dest)
718 dest->m00 = m1->m00*m2->m00 + m1->m10*m2->m01;
719 dest->m01 = m1->m01*m2->m00 + m1->m11*m2->m01;
720 dest->m10 = m1->m00*m2->m10 + m1->m10*m2->m11;
721 dest->m11 = m1->m01*m2->m10 + m1->m11*m2->m11;
722 dest->tx = m1->m00*m2->tx + m1->m10*m2->ty + m1->tx;
723 dest->ty = m1->m01*m2->tx + m1->m11*m2->ty + m1->ty;
726 gfxfontlist_t* gfxfontlist_create()
728 /* Initial list ist empty */
732 gfxfont_t*gfxfontlist_findfont(gfxfontlist_t*list, char*id)
734 gfxfontlist_t*l = list;
736 if(!strcmp((char*)l->font->id, id)) {
743 char gfxfontlist_hasfont(gfxfontlist_t*list, gfxfont_t*font)
745 gfxfontlist_t*l = list;
747 if(!strcmp((char*)l->font->id, font->id)) {
754 gfxfontlist_t*gfxfontlist_addfont(gfxfontlist_t*list, gfxfont_t*font)
756 gfxfontlist_t*last=0,*l = list;
759 if(!strcmp((char*)l->font->id, font->id)) {
760 return list; // we already know this font
765 fprintf(stderr, "Tried to add zero font\n");
767 l = (gfxfontlist_t*)rfx_calloc(sizeof(gfxfontlist_t));
777 void gfxfontlist_free(gfxfontlist_t*list, char deletefonts)
779 gfxfontlist_t*l = list;
781 gfxfontlist_t*next = l;
782 memset(l, 0, sizeof(*l));
784 gfxfont_free(l->font);
791 gfxline_t*gfxline_makerectangle(int x1,int y1,int x2, int y2)
793 gfxline_t* line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t)*5);
794 line[0].x = x1;line[0].y = y1;line[0].type = gfx_moveTo;line[0].next = &line[1];
795 line[1].x = x2;line[1].y = y1;line[1].type = gfx_lineTo;line[1].next = &line[2];
796 line[2].x = x2;line[2].y = y2;line[2].type = gfx_lineTo;line[2].next = &line[3];
797 line[3].x = x1;line[3].y = y2;line[3].type = gfx_lineTo;line[3].next = &line[4];
798 line[4].x = x1;line[4].y = y1;line[4].type = gfx_lineTo;
802 void gfximage_transform(gfximage_t*img, gfxcxform_t*cxform)
805 int size = img->width*img->height;
811 rr = (int)(cxform->rr*256);gr = (int)(cxform->gr*256);
812 rg = (int)(cxform->rg*256);gg = (int)(cxform->gg*256);
813 rb = (int)(cxform->rb*256);gb = (int)(cxform->gb*256);
814 ra = (int)(cxform->ra*256);ga = (int)(cxform->ga*256);
815 br = (int)(cxform->br*256);ar = (int)(cxform->ar*256);tr = (int)(cxform->tr*256);
816 bg = (int)(cxform->bg*256);ag = (int)(cxform->ag*256);tg = (int)(cxform->tg*256);
817 bb = (int)(cxform->bb*256);ab = (int)(cxform->ab*256);tb = (int)(cxform->tb*256);
818 ba = (int)(cxform->ba*256);aa = (int)(cxform->aa*256);ta = (int)(cxform->ta*256);
820 for(t=0;t<size;t++) {
821 gfxcolor_t*pixel = &img->data[t];
822 unsigned char r = (pixel->r * rr + pixel->g * rg + pixel->b * rb + pixel->a * ra + tr) / 256;
823 unsigned char g = (pixel->r * gr + pixel->g * gg + pixel->b * gb + pixel->a * ga + tg) / 256;
824 unsigned char b = (pixel->r * br + pixel->g * bg + pixel->b * bb + pixel->a * ba + tb) / 256;
825 unsigned char a = (pixel->r * ar + pixel->g * ag + pixel->b * ab + pixel->a * aa + ta) / 256;