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