added missing include
[swftools.git] / lib / gfxtools.c
1 /* gfxtools.c 
2
3    Various utility functions for dealing with gfxdevices.
4
5    Part of the swftools package.
6
7    Copyright (c) 2005 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 <stdio.h>
24 #include <stdlib.h>
25 #include <memory.h>
26 #include <math.h>
27 #include <assert.h>
28 #include "gfxtools.h"
29 #include "gfxfont.h"
30
31 typedef struct _linedraw_internal
32 {
33     gfxline_t*start;
34     gfxline_t*next;
35 } linedraw_internal_t;
36
37 static void linedraw_moveTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
38 {
39     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
40     gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
41     l->type = gfx_moveTo;
42     if((int)((d->x * 5120) == (int)(x * 5120)) &&
43        (int)((d->y * 5120) == (int)(y * 5120))) {
44         /* never mind- we're already there */
45         return;
46
47     }
48     l->sx = l->sy = 0;
49     d->x = l->x = x;
50     d->y = l->y = y;
51     l->next = 0;
52     if(i->next)
53         i->next->next = l;
54     i->next = l;
55     if(!i->start)
56         i->start = l;
57 }
58 static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
59 {
60     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
61     gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
62
63     if(!i->start) {
64         /* starts with a line, not with a moveto. That needs we first
65            need an explicit moveto to (0,0) */
66         linedraw_moveTo(d, 0, 0);
67     }
68
69     l->type = gfx_lineTo;
70     d->x = l->x = x;
71     d->y = l->y = y;
72
73     l->next = 0;
74     if(i->next)
75         i->next->next = l;
76     i->next = l;
77     if(!i->start)
78         i->start = l;
79 }
80 static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
81 {
82     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
83     gfxline_t*l = rfx_alloc(sizeof(gfxline_t));
84
85     if(!i->start) {
86         /* starts with a line, not with a moveto. That needs we first
87            need an explicit moveto to (0,0) */
88         linedraw_moveTo(d, 0, 0);
89     }
90
91     l->type = gfx_splineTo;
92     d->x = l->x = x; 
93     d->y = l->y = y;
94     l->sx = sx; 
95     l->sy = sy;
96     l->next = 0;
97     if(i->next)
98         i->next->next = l;
99     i->next = l;
100     if(!i->start)
101         i->start = l;
102 }
103 static void* linedraw_result(gfxdrawer_t*d)
104 {
105     linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
106     void*result = (void*)i->start;
107     rfx_free(i);
108     memset(d, 0, sizeof(gfxdrawer_t));
109     return result;
110 }
111
112 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
113 {
114     linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
115     d->x = 0x7fffffff;
116     d->y = 0x7fffffff;
117     d->internal = i;
118     d->moveTo = linedraw_moveTo;
119     d->lineTo = linedraw_lineTo;
120     d->splineTo = linedraw_splineTo;
121     d->result = linedraw_result;
122 }
123
124 typedef struct _qspline_abc
125 {
126     double ax,bx,cx;
127     double ay,by,cy;
128 } qspline_abc_t;
129
130 typedef struct qspline_t
131 {
132     gfxpoint_t start;
133     gfxpoint_t control;
134     gfxpoint_t end;
135 } qspline_t;
136
137 typedef struct cspline_t
138 {
139     gfxpoint_t start;
140     gfxpoint_t control1;
141     gfxpoint_t control2;
142     gfxpoint_t end;
143 } cspline_t;
144
145 static void mkspline(qspline_abc_t*s, double x, double y, gfxline_t*l)
146 {
147     /* 
148        Form 1: x = t*t*l->x + 2*t*(1-t)*l->sx + (1-t)*(1-t)*x;
149        Form 2: x = a*t*t + b*t + c
150     */
151     s->cx = x; s->bx = 2*l->sx - 2*x; s->ax = l->x - 2*l->sx + x;
152     s->cy = y; s->by = 2*l->sy - 2*y; s->ay = l->y - 2*l->sy + y;
153 }
154
155 static void spline_get_controlpoint(qspline_abc_t*q, double t1, double t2, double*dx, double*dy)
156 {
157     double dt = t2-t1;
158     double nax = q->ax*dt*dt;
159     double nay = q->ay*dt*dt;
160     double nbx = 2*q->ax*dt*t1 + q->bx*dt;
161     double nby = 2*q->ay*dt*t1 + q->by*dt;
162     double ncx = q->ax*t1*t1 + q->bx*t1 + q->cx;
163     double ncy = q->ay*t1*t1 + q->by*t1 + q->cy;
164     *dx = ncx + nbx/2;
165     *dy = ncy + nby/2;
166 }
167
168 static double get_spline_len(qspline_abc_t*s)
169 {
170     int parts = (int)(sqrt(fabs(s->ax) + fabs(s->ay))*3);
171     int i;
172     double len = 0;
173     double r;
174     double r2;
175     if(parts < 3) parts = 3;
176     r = 1.0/parts;
177     r2 = 1.0/(parts*parts);
178     for(i=0;i<parts;i++)
179     {
180         double dx = s->ax*(2*i+1)*r2 + s->bx*r;
181         double dy = s->ay*(2*i+1)*r2 + s->by*r;
182         len += sqrt(dx*dx+dy*dy);
183     }
184     /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy, 
185             s->cx + s->bx + s->ax,
186             s->cy + s->by + s->ay, len,
187             sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
188             );
189     assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
190      */
191     return len;
192 }
193
194 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
195 {
196     double x=0,y=0;
197     double linepos,nextpos;
198     char on;
199     int apos;
200
201     if(line && line->type != gfx_moveTo) {
202         fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
203         return;
204     }
205     if(!r || r[0]<0 || phase<0) {
206         fprintf(stderr, "gfxtool: invalid dashes");
207         return;
208     }
209
210     for(;line;line=line->next) {
211         if(line->type == gfx_moveTo) {
212             d->moveTo(d, line->x, line->y);
213             on = 1; nextpos = r[0]; apos = 0; linepos = 0;
214             x = line->x; y = line->y;
215             while(linepos < phase) {
216                 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
217                 linepos += r[apos];
218                 if(linepos < phase) {
219                     on ^= 1;
220                     if(r[++apos]<0)
221                         apos = 0;
222                     nextpos += r[apos];
223                 }
224             }
225             linepos = phase;
226             //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
227         } else if(line->type == gfx_lineTo) {
228             double dx = line->x - x;
229             double dy = line->y - y;
230             double len = sqrt(dx*dx+dy*dy);
231             double vx;
232             double vy;
233             double lineend = linepos+len;
234             if(len==0)
235                 continue;
236             vx = dx/len;
237             vy = dy/len;
238             assert(nextpos>=linepos);
239             //printf("(line) 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 nx = x + vx*(nextpos-linepos);
242                 double ny = y + vy*(nextpos-linepos);
243                 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
244                 else   {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
245                 on^=1;
246                 if(r[++apos]<0)
247                     apos = 0;
248                 nextpos+=r[apos];
249             }
250             linepos = lineend;
251             if(on) {
252                 //printf("lineTo %f\n", 1.0);
253                 d->lineTo(d, line->x,line->y);
254             }
255             x = line->x; y = line->y;
256         } else if(line->type == gfx_splineTo) {
257             qspline_abc_t q;
258             double len, lineend,lastt;
259             mkspline(&q, x, y, line);
260
261             len = get_spline_len(&q);
262             //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
263             if(len==0)
264                 continue;
265             lineend = linepos+len;
266             lastt = 0;
267             if(nextpos<linepos)
268                 printf("%f !< %f\n", nextpos, linepos);
269             assert(nextpos>=linepos);
270             //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
271             while(nextpos<lineend) {
272                 double t = (nextpos-linepos)/len;
273                 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
274                 double nx = q.ax*t*t+q.bx*t+q.cx;
275                 double ny = q.ay*t*t+q.by*t+q.cy;
276                 if(on) {
277                     double sx,sy;
278                     spline_get_controlpoint(&q, lastt, t, &sx, &sy);
279                     d->splineTo(d, sx, sy, nx,ny);
280                     //printf("splineTo %f\n", nextpos);
281                 } else  {
282                     d->moveTo(d, nx,ny);
283                     //printf("moveTo %f\n", nextpos);
284                 }
285                 lastt =  t;
286                 on^=1;
287                 if(r[++apos]<0)
288                     apos = 0;
289                 nextpos+=r[apos];
290             }
291             linepos = lineend;
292             if(on) {
293                 double sx,sy;
294                 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
295                 d->splineTo(d, sx, sy, line->x,line->y);
296                 //printf("splineTo %f\n", 1.0);
297             }
298             x = line->x; y = line->y;
299         }
300     }
301 }
302
303 gfxline_t * gfxline_clone(gfxline_t*line)
304 {
305     gfxline_t*dest = 0;
306     gfxline_t*pos = 0;
307     while(line) {
308         gfxline_t*n = rfx_calloc(sizeof(gfxline_t));
309         *n = *line;
310         n->next = 0;
311         if(!pos) {
312             dest = pos = n;
313         } else {
314             pos->next = n;
315             pos = n;
316         }
317         line = line->next;
318     }
319     return dest;
320 }
321 void gfxline_optimize(gfxline_t*line)
322 {
323     gfxline_t*l = line;
324     /* step 1: convert splines to lines, where possible */
325     double x=0,y=0;
326     while(l) {
327         if(l->type == gfx_splineTo) {
328             double dx = l->x-x;
329             double dy = l->y-y;
330             double sx = l->sx-x;
331             double sy = l->sy-y;
332             if(fabs(dx*sy - dy*sx) < 0.000001 && (dx*sx + dy*sy) >= 0) {
333                 l->type = gfx_lineTo;
334             }
335         }
336         x = l->x;
337         y = l->y;
338         l = l->next;
339     }
340     /* step 2: combine adjacent lines and splines, where possible */
341     l = line;
342     while(l && l->next) {
343         gfxline_t*next = l->next;
344         char combine = 0;
345         double sx=0,sy=0;
346         if(l->type == gfx_lineTo && next->type == gfx_lineTo) {
347             double dx = l->x-x;
348             double dy = l->y-y;
349             double nx = next->x-x;
350             double ny = next->y-y;
351             if(fabs(dx*ny - dy*nx) < 0.000001 && (dx*nx + dy*ny) >= 0) {
352                 combine = 1;
353             }
354         } else if(l->type == gfx_splineTo && next->type == gfx_splineTo) {
355             /* TODO */
356         }
357         if(combine) {
358             l->next = next->next;
359             next->next = 0;
360             l->x = next->x;
361             l->y = next->y;
362             l->sx = sx;
363             l->sy = sy;
364             rfx_free(next);
365         } else {
366             x = l->x;
367             y = l->y;
368             l = l->next;
369         }
370     }
371 }
372
373 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
374 {
375     gfxdrawer_t d;
376     gfxline_t*result;
377     gfxdrawer_target_gfxline(&d);
378     gfxtool_draw_dashed_line(&d, line, dashes, phase);
379     result= (gfxline_t*)d.result(&d);
380     return result;
381 }
382
383 void gfxline_show(gfxline_t*l, FILE*fi)
384 {
385     while(l) {
386         if(l->type == gfx_moveTo) {
387             fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
388         }
389         if(l->type == gfx_lineTo) {
390             fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
391         }
392         if(l->type == gfx_splineTo) {
393             fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
394         }
395         l = l->next;
396     }
397 }
398
399 void gfxline_free(gfxline_t*l)
400 {
401     if(l && (l+1) == l->next) {
402         /* flattened */
403         rfx_free(l);
404     } else {
405         gfxline_t*next;
406         while(l) {
407             next = l->next;
408             l->next = 0;
409             rfx_free(l);
410             l = next;
411         }
412     }
413 }
414
415 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
416 {
417     gfxpoint_t p;
418     double tt = t*t;
419     double ttt = tt*t;
420     double mt = (1-t);
421     double mtmt = mt*(1-t);
422     double mtmtmt = mtmt*(1-t);
423     p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
424             + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
425     p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
426             + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
427     return p;
428 }
429 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
430 {
431     gfxpoint_t p;
432     p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
433     p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
434     return p;
435 }
436
437 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
438 {
439     unsigned int gran = 0;
440     unsigned int istep = 0x80000000;
441     unsigned int istart = 0;
442     int num = 0;
443     int level = 0;
444     
445     while(istart<0x80000000)
446     {
447         unsigned int iend = istart + istep;
448         double start = istart/(double)0x80000000;
449         double end = iend/(double)0x80000000;
450         qspline_t test;
451         double pos,qpos;
452         char left = 0,recurse=0;
453         int t;
454         int probes = 15;
455         double dx,dy;
456
457         /* create simple approximation: a qspline_t which run's through the
458            qspline_t point at 0.5 */
459         test.start = cspline_getpoint(s, start);
460         test.control = cspline_getpoint(s, (start+end)/2);
461         test.end = cspline_getpoint(s, end);
462         /* fix the control point:
463            move it so that the new spline does runs through it */
464         test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
465         test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
466
467         /* depending on where we are in the spline, we either try to match
468            the left or right tangent */
469         if(start<0.5) 
470             left=1;
471         /* get derivative */
472         pos = left?start:end;
473         qpos = pos*pos;
474         test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) + 
475                     3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
476         test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) + 
477                     3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
478         if(left) {
479             test.control.x *= (end-start)/2;
480             test.control.y *= (end-start)/2;
481             test.control.x += test.start.x;
482             test.control.y += test.start.y;
483         } else {
484             test.control.x *= -(end-start)/2;
485             test.control.y *= -(end-start)/2;
486             test.control.x += test.end.x;
487             test.control.y += test.end.y;
488         }
489
490 //#define PROBES
491 #ifdef PROBES
492         /* measure the spline's accurancy, by taking a number of probes */
493         for(t=0;t<probes;t++) {
494             gfxpoint_t qr1,qr2,cr1,cr2;
495             double pos = 0.5/(probes*2)*(t*2+1);
496             double dx,dy;
497             double dist1,dist2;
498             qr1 = qspline_getpoint(&test, pos);
499             cr1 = cspline_getpoint(s, start+pos*(end-start));
500
501             dx = qr1.x - cr1.x;
502             dy = qr1.y - cr1.y;
503             dist1 = dx*dx+dy*dy;
504
505             if(dist1>quality2) {
506                 recurse=1;break;
507             }
508             qr2 = qspline_getpoint(&test, (1-pos));
509             cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
510
511             dx = qr2.x - cr2.x;
512             dy = qr2.y - cr2.y;
513             dist2 = dx*dx+dy*dy;
514
515             if(dist2>quality2) {
516                 recurse=1;break;
517             }
518         }
519 #else // quadratic error: *much* faster!
520
521         /* convert control point representation to 
522            d*x^3 + c*x^2 + b*x + a */
523         dx= s->end.x  - s->control2.x*3 + s->control1.x*3 - s->start.x;
524         dy= s->end.y  - s->control2.y*3 + s->control1.y*3 - s->start.y;
525         
526         /* we need to do this for the subspline between [start,end], not [0,1] 
527            as a transformation of t->a*t+b does nothing to highest coefficient
528            of the spline except multiply it with a^3, we just need to modify
529            d here. */
530         {double m = end-start;
531          dx*=m*m*m;
532          dy*=m*m*m;
533         }
534         
535         /* use the integral over (f(x)-g(x))^2 between 0 and 1
536            to measure the approximation quality. 
537            (it boils down to const*d^2) */
538         recurse = (dx*dx + dy*dy > quality2);
539 #endif
540
541         if(recurse && istep>1 && size-level > num) {
542             istep >>= 1;
543             level++;
544         } else {
545             *q++ = test;
546             num++;
547             istart += istep;
548             while(!(istart & istep)) {
549                 level--;
550                 istep <<= 1;
551             }
552         }
553     }
554     return num;
555 }
556
557 void gfxdraw_conicTo(gfxdrawer_t*draw, double cx, double cy, double tox, double toy, double quality)
558 {
559     double c1x = (draw->x + 2 * cx) / 3;
560     double c1y = (draw->y + 2 * cy) / 3;
561     double c2x = (2 * cx + tox) / 3;
562     double c2y = (2 * cy + toy) / 3;
563     gfxdraw_cubicTo(draw, c1x, c1y, c2x, c2y, tox, toy, quality);
564 }
565
566
567 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y, double quality)
568 {
569     qspline_t q[128];
570     cspline_t c;
571     double maxerror = quality>0 ? quality : 1.0;
572     int t,num;
573
574     c.start.x = draw->x;
575     c.start.y = draw->y;
576     c.control1.x = c1x;
577     c.control1.y = c1y;
578     c.control2.x = c2x;
579     c.control2.y = c2y;
580     c.end.x = x;
581     c.end.y = y;
582     
583     num = approximate3(&c, q, 128, maxerror);
584
585     for(t=0;t<num;t++) {
586         gfxpoint_t mid;
587         gfxpoint_t to;
588         mid.x = q[t].control.x;
589         mid.y = q[t].control.y;
590         to.x = q[t].end.x;
591         to.y = q[t].end.y;
592         draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
593     }
594 }
595
596 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
597 {
598     if(box.xmin==0 && box.ymin==0 && box.xmax==0 && box.ymax==0) {
599         box.xmin = x;
600         box.ymin = y;
601         box.xmax = x;
602         box.ymax = y;
603         if(x==0 && y==0) box.xmax = 0.0000001;
604         return box;
605     }
606     if(x < box.xmin)
607         box.xmin = x;
608     if(x > box.xmax)
609         box.xmax = x;
610     if(y < box.ymin)
611         box.ymin = y;
612     if(y > box.ymax)
613         box.ymax = y;
614     return box;
615 }
616
617 gfxbbox_t gfxline_getbbox(gfxline_t*line)
618 {
619     gfxcoord_t x=0,y=0;
620     gfxbbox_t bbox = {0,0,0,0};
621     char last = 0;
622     while(line) {
623         if(line->type == gfx_moveTo) {
624             last = 1;
625         } else if(line->type == gfx_lineTo) {
626             if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
627             bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
628             last = 0;
629         } else if(line->type == gfx_splineTo) {
630             if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
631             bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
632             bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
633             last = 0;
634         }
635         x = line->x;
636         y = line->y;
637         line = line->next;
638     }
639     return bbox;
640 }
641
642 void gfxline_dump(gfxline_t*line, FILE*fi, char*prefix)
643 {
644     while(line) {
645         if(line->type == gfx_moveTo) {
646             fprintf(fi, "%smoveTo %.2f %.2f\n", prefix, line->x, line->y);
647         } else if(line->type == gfx_lineTo) {
648             fprintf(fi, "%slineTo %.2f %.2f\n", prefix, line->x, line->y);
649         } else if(line->type == gfx_splineTo) {
650             fprintf(fi, "%ssplineTo (%.2f %.2f) %.2f %.2f\n", prefix, line->sx, line->sy, line->x, line->y);
651         }
652         line = line->next;
653     }
654 }
655
656 gfxline_t* gfxline_append(gfxline_t*line1, gfxline_t*line2)
657 {
658     gfxline_t*l = line1;;
659     if(!l)
660         return line2;
661     while(l->next) {
662         l = l->next;
663     }
664     l->next = line2;
665     return line1;
666 }
667
668 void gfxline_transform(gfxline_t*line, gfxmatrix_t*matrix)
669 {
670     while(line) {
671         double x = matrix->m00*line->x + matrix->m10*line->y + matrix->tx;
672         double y = matrix->m01*line->x + matrix->m11*line->y + matrix->ty;
673         line->x = x;
674         line->y = y;
675         if(line->type == gfx_splineTo) {
676             double sx = matrix->m00*line->sx + matrix->m10*line->sy + matrix->tx;
677             double sy = matrix->m01*line->sx + matrix->m11*line->sy + matrix->ty;
678             line->sx = sx;
679             line->sy = sy;
680         }
681         line = line->next;
682     }
683 }
684
685 void gfxmatrix_dump(gfxmatrix_t*m, FILE*fi, char*prefix)
686 {
687     fprintf(fi, "%f %f | %f\n", m->m00, m->m10, m->tx);
688     fprintf(fi, "%f %f | %f\n", m->m01, m->m11, m->ty);
689 }
690
691 void gfxmatrix_transform(gfxmatrix_t*m, double* v, double*dest)
692 {
693     dest[0] = m->m00*v[0] + m->m10*v[1] + m->tx;
694     dest[1] = m->m01*v[0] + m->m11*v[1] + m->ty;
695 }
696 void gfxmatrix_invert(gfxmatrix_t*m, gfxmatrix_t*dest)
697 {
698     double det = m->m00 * m->m11 - m->m10 * m->m01;
699     if(!det) {
700         memset(dest, 0, sizeof(gfxmatrix_t));
701         return;
702     }
703     det = 1/det;
704     dest->m00 = m->m11 * det;
705     dest->m01 = -m->m01 * det;
706     dest->m10 = -m->m10 * det;
707     dest->m11 = m->m00 * det;
708     dest->tx = -(dest->m00 * m->tx + dest->m10 * m->ty);
709     dest->ty = -(dest->m01 * m->tx + dest->m11 * m->ty);
710 }
711 void gfxmatrix_unit(gfxmatrix_t*m)
712 {
713     memset(m, 0, sizeof(gfxmatrix_t));
714     m->m00 = 1.0;
715     m->m11 = 1.0;
716 }
717 void gfxmatrix_multiply(gfxmatrix_t*m1, gfxmatrix_t*m2, gfxmatrix_t*dest)
718 {
719     dest->m00 = m1->m00*m2->m00 + m1->m10*m2->m01;
720     dest->m01 = m1->m01*m2->m00 + m1->m11*m2->m01;
721     dest->m10 = m1->m00*m2->m10 + m1->m10*m2->m11;
722     dest->m11 = m1->m01*m2->m10 + m1->m11*m2->m11;
723     dest->tx = m1->m00*m2->tx + m1->m10*m2->ty + m1->tx;
724     dest->ty = m1->m01*m2->tx + m1->m11*m2->ty + m1->ty;
725 }
726
727 gfxfontlist_t* gfxfontlist_create()
728 {
729     /* Initial list ist empty */
730     return 0;
731 }
732
733 gfxfont_t*gfxfontlist_findfont(gfxfontlist_t*list, char*id)
734 {
735     gfxfontlist_t*l = list;
736     while(l) {
737         if(!strcmp((char*)l->font->id, id)) {
738             return l->font;
739         }
740         l = l->next;
741     }
742     return 0;
743 }
744 char gfxfontlist_hasfont(gfxfontlist_t*list, gfxfont_t*font)
745 {
746     gfxfontlist_t*l = list;
747     while(l) {
748         if(!strcmp((char*)l->font->id, font->id)) {
749             return 1;
750         }
751         l = l->next;
752     }
753     return 0;
754 }
755 gfxfontlist_t*gfxfontlist_addfont(gfxfontlist_t*list, gfxfont_t*font)
756 {
757     gfxfontlist_t*last=0,*l = list;
758     while(l) {
759         last = l;
760         if(!strcmp((char*)l->font->id, font->id)) {
761             return list; // we already know this font
762         }
763         l = l->next;
764     }
765     if(!font) {
766         fprintf(stderr, "Tried to add zero font\n");
767     }
768     l = (gfxfontlist_t*)rfx_calloc(sizeof(gfxfontlist_t));
769     l->font = font;
770     l->next = 0;
771     if(last) {
772         last->next = l;
773         return list;
774     } else {
775         return l;
776     }
777 }
778 void gfxfontlist_free(gfxfontlist_t*list, char deletefonts)
779 {
780     gfxfontlist_t*l = list;
781     while(l) {
782         gfxfontlist_t*next = l->next;
783         memset(l, 0, sizeof(*l));
784         if(l->font) {
785             gfxfont_free(l->font);
786         }
787         free(l);
788         l = next;
789     }
790 }
791
792 gfxline_t*gfxline_makerectangle(int x1,int y1,int x2, int y2)
793 {
794     gfxline_t* line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t)*5);
795     line[0].x = x1;line[0].y = y1;line[0].type = gfx_moveTo;line[0].next = &line[1];
796     line[1].x = x2;line[1].y = y1;line[1].type = gfx_lineTo;line[1].next = &line[2];
797     line[2].x = x2;line[2].y = y2;line[2].type = gfx_lineTo;line[2].next = &line[3];
798     line[3].x = x1;line[3].y = y2;line[3].type = gfx_lineTo;line[3].next = &line[4];
799     line[4].x = x1;line[4].y = y1;line[4].type = gfx_lineTo;
800     return line;
801 }
802
803 void gfximage_transform(gfximage_t*img, gfxcxform_t*cxform)
804 {
805     int t;
806     int size = img->width*img->height;
807
808     int rr,rg,rb,ra, tr;
809     int gr,gg,gb,ga, tg;
810     int br,bg,bb,ba, tb;
811     int ar,ag,ab,aa, ta;
812     rr = (int)(cxform->rr*256);gr = (int)(cxform->gr*256);
813     rg = (int)(cxform->rg*256);gg = (int)(cxform->gg*256);
814     rb = (int)(cxform->rb*256);gb = (int)(cxform->gb*256);
815     ra = (int)(cxform->ra*256);ga = (int)(cxform->ga*256);
816     br = (int)(cxform->br*256);ar = (int)(cxform->ar*256);tr = (int)(cxform->tr*256);
817     bg = (int)(cxform->bg*256);ag = (int)(cxform->ag*256);tg = (int)(cxform->tg*256);
818     bb = (int)(cxform->bb*256);ab = (int)(cxform->ab*256);tb = (int)(cxform->tb*256);
819     ba = (int)(cxform->ba*256);aa = (int)(cxform->aa*256);ta = (int)(cxform->ta*256);
820
821     for(t=0;t<size;t++) {
822         gfxcolor_t*pixel = &img->data[t];
823         unsigned char r = (pixel->r * rr + pixel->g * rg + pixel->b * rb + pixel->a * ra + tr) / 256;
824         unsigned char g = (pixel->r * gr + pixel->g * gg + pixel->b * gb + pixel->a * ga + tg) / 256;
825         unsigned char b = (pixel->r * br + pixel->g * bg + pixel->b * bb + pixel->a * ba + tb) / 256;
826         unsigned char a = (pixel->r * ar + pixel->g * ag + pixel->b * ab + pixel->a * aa + ta) / 256;
827         pixel->r = r;
828         pixel->g = g;
829         pixel->b = b;
830         pixel->a = a;
831     }
832 }