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 */
34 typedef struct _linedraw_internal
40 } linedraw_internal_t;
42 static void linedraw_moveTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
44 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
45 gfxline_t*l = (gfxline_t*)rfx_alloc(sizeof(gfxline_t));
60 static void linedraw_lineTo(gfxdrawer_t*d, gfxcoord_t x, gfxcoord_t y)
62 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
64 /* starts with a line, not with a moveto. As this is the first
65 entry in the list, this is probably *meant* to be a moveto */
66 linedraw_moveTo(d, x, y);
70 gfxline_t*l = (gfxline_t*)rfx_alloc(sizeof(gfxline_t));
82 static void linedraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
84 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
86 linedraw_moveTo(d, x, y);
90 gfxline_t*l = (gfxline_t*)rfx_alloc(sizeof(gfxline_t));
91 l->type = gfx_splineTo;
103 static void linedraw_close(gfxdrawer_t*d)
105 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
108 linedraw_lineTo(d, i->x0, i->y0);
113 static void* linedraw_result(gfxdrawer_t*d)
115 linedraw_internal_t*i = (linedraw_internal_t*)d->internal;
116 void*result = (void*)i->start;
118 memset(d, 0, sizeof(gfxdrawer_t));
122 void gfxdrawer_target_gfxline(gfxdrawer_t*d)
124 linedraw_internal_t*i = (linedraw_internal_t*)rfx_calloc(sizeof(linedraw_internal_t));
128 d->moveTo = linedraw_moveTo;
129 d->lineTo = linedraw_lineTo;
130 d->splineTo = linedraw_splineTo;
131 d->close = linedraw_close;
132 d->result = linedraw_result;
135 typedef struct _qspline_abc
141 typedef struct qspline_t
148 typedef struct cspline_t
156 static void mkspline(qspline_abc_t*s, double x, double y, gfxline_t*l)
159 Form 1: x = t*t*l->x + 2*t*(1-t)*l->sx + (1-t)*(1-t)*x;
160 Form 2: x = a*t*t + b*t + c
162 s->cx = x; s->bx = 2*l->sx - 2*x; s->ax = l->x - 2*l->sx + x;
163 s->cy = y; s->by = 2*l->sy - 2*y; s->ay = l->y - 2*l->sy + y;
166 static void spline_get_controlpoint(qspline_abc_t*q, double t1, double t2, double*dx, double*dy)
169 double nax = q->ax*dt*dt;
170 double nay = q->ay*dt*dt;
171 double nbx = 2*q->ax*dt*t1 + q->bx*dt;
172 double nby = 2*q->ay*dt*t1 + q->by*dt;
173 double ncx = q->ax*t1*t1 + q->bx*t1 + q->cx;
174 double ncy = q->ay*t1*t1 + q->by*t1 + q->cy;
179 static double get_spline_len(qspline_abc_t*s)
181 int parts = (int)(sqrt(fabs(s->ax) + fabs(s->ay))*3);
186 if(parts < 3) parts = 3;
188 r2 = 1.0/(parts*parts);
191 double dx = s->ax*(2*i+1)*r2 + s->bx*r;
192 double dy = s->ay*(2*i+1)*r2 + s->by*r;
193 len += sqrt(dx*dx+dy*dy);
195 /*printf("Spline from %f,%f to %f,%f has len %f (%f)\n", s->cx, s->cy,
196 s->cx + s->bx + s->ax,
197 s->cy + s->by + s->ay, len,
198 sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay))
200 assert(len+0.5 >= sqrt((s->bx + s->ax)*(s->bx + s->ax) + (s->by + s->ay)*(s->by + s->ay)));
205 void gfxtool_draw_dashed_line(gfxdrawer_t*d, gfxline_t*line, float*r, float phase)
208 double linepos,nextpos;
212 if(line && line->type != gfx_moveTo) {
213 fprintf(stderr, "gfxtool: outline doesn't start with a moveTo");
219 for(i=0;r[i]>=0;i++) {
222 if(!r || (r[0]<=0 && r[0]>-0.01) || dashlen<0.001) {
223 // no dashing. just draw the thing
225 if(line->type == gfx_moveTo) {
226 d->moveTo(d, line->x, line->y);
227 } else if(line->type == gfx_lineTo) {
228 d->lineTo(d, line->x, line->y);
229 } else if(line->type == gfx_splineTo) {
230 d->splineTo(d, line->sx, line->sy, line->x, line->y);
236 if(r[0]<0 || phase<0) {
237 fprintf(stderr, "gfxtool: invalid (negative) dashes: %f, phase=%f\n", r[0], phase);
241 for(;line;line=line->next) {
242 if(line->type == gfx_moveTo) {
243 d->moveTo(d, line->x, line->y);
244 on = 1; nextpos = r[0]; apos = 0; linepos = 0;
245 x = line->x; y = line->y;
246 while(linepos < phase) {
247 //printf("[+] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f\n", linepos, phase, on, apos, nextpos);
249 if(linepos < phase) {
257 //printf("[k] linepos: %f, phase: %f, on:%d, apos:%d nextpos:%f \n", linepos, phase, on, apos, nextpos);
258 } else if(line->type == gfx_lineTo) {
259 double dx = line->x - x;
260 double dy = line->y - y;
261 double len = sqrt(dx*dx+dy*dy);
264 double lineend = linepos+len;
269 assert(nextpos>=linepos);
270 //printf("(line) 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 nx = x + vx*(nextpos-linepos);
273 double ny = y + vy*(nextpos-linepos);
274 if(on) {d->lineTo(d, nx,ny);/*printf("lineTo %f\n", nextpos);*/}
275 else {d->moveTo(d, nx,ny);/*printf("moveTo %f\n", nextpos);*/}
283 //printf("lineTo %f\n", 1.0);
284 d->lineTo(d, line->x,line->y);
286 x = line->x; y = line->y;
287 } else if(line->type == gfx_splineTo) {
289 double len, lineend,lastt;
290 mkspline(&q, x, y, line);
292 len = get_spline_len(&q);
293 //printf("%f %f -> %f %f, len: %f\n", x, y, line->x, line->y, len);
296 lineend = linepos+len;
299 printf("%f !< %f\n", nextpos, linepos);
300 assert(nextpos>=linepos);
301 //printf("(spline) on:%d apos: %d nextpos: %f, line pos: %f, line end: %f\n", on, apos, nextpos, linepos, linepos+len);
302 while(nextpos<lineend) {
303 double t = (nextpos-linepos)/len;
304 //printf("%f (%f-%f) apos=%d r[apos]=%f\n", t, nextpos, linepos, apos, r[apos]);
305 double nx = q.ax*t*t+q.bx*t+q.cx;
306 double ny = q.ay*t*t+q.by*t+q.cy;
309 spline_get_controlpoint(&q, lastt, t, &sx, &sy);
310 d->splineTo(d, sx, sy, nx,ny);
311 //printf("splineTo %f\n", nextpos);
314 //printf("moveTo %f\n", nextpos);
325 spline_get_controlpoint(&q, lastt, 1, &sx, &sy);
326 d->splineTo(d, sx, sy, line->x,line->y);
327 //printf("splineTo %f\n", 1.0);
329 x = line->x; y = line->y;
334 gfxline_t * gfxline_clone(gfxline_t*line)
339 gfxline_t*n = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
353 static char splineIsStraight(double x, double y, gfxline_t*l)
355 if(l->type == gfx_moveTo)
357 if(l->type == gfx_lineTo)
363 if(fabs(dx*sy - dy*sx) < 0.000001 && (dx*sx + dy*sy) >= 0) {
369 void gfxline_optimize(gfxline_t*line)
372 /* step 1: convert splines to lines, where possible */
375 if(l->type == gfx_splineTo && splineIsStraight(x,y,l)) {
376 l->type = gfx_lineTo;
382 /* step 2: combine adjacent lines and splines, where possible */
384 while(l && l->next) {
385 gfxline_t*next = l->next;
388 if(l->type == gfx_lineTo && next->type == gfx_lineTo) {
391 double nx = next->x-l->x;
392 double ny = next->y-l->y;
393 if(fabs(dx*ny - dy*nx) < 0.000001 && (dx*nx + dy*ny) >= 0) {
396 } else if(l->type == gfx_splineTo && next->type == gfx_splineTo) {
400 l->next = next->next;
415 gfxline_t* gfxtool_dash_line(gfxline_t*line, float*dashes, float phase)
419 gfxdrawer_target_gfxline(&d);
420 gfxtool_draw_dashed_line(&d, line, dashes, phase);
421 result= (gfxline_t*)d.result(&d);
425 void gfxline_show(gfxline_t*l, FILE*fi)
428 if(l->type == gfx_moveTo) {
429 fprintf(fi, "moveTo %.2f,%.2f\n", l->x, l->y);
431 if(l->type == gfx_lineTo) {
432 fprintf(fi, "lineTo %.2f,%.2f\n", l->x, l->y);
434 if(l->type == gfx_splineTo) {
435 fprintf(fi, "splineTo %.2f,%.2f %.2f,%.2f\n", l->sx, l->sy, l->x, l->y);
441 void gfxline_free(gfxline_t*l)
443 if(l && (l+1) == l->next) {
457 static inline gfxpoint_t cspline_getpoint(const struct cspline_t*s, double t)
463 double mtmt = mt*(1-t);
464 double mtmtmt = mtmt*(1-t);
465 p.x= s->end.x*ttt + 3*s->control2.x*tt*mt
466 + 3*s->control1.x*t*mtmt + s->start.x*mtmtmt;
467 p.y= s->end.y*ttt + 3*s->control2.y*tt*mt
468 + 3*s->control1.y*t*mtmt + s->start.y*mtmtmt;
471 static gfxpoint_t qspline_getpoint(const qspline_t*s, double t)
474 p.x= s->end.x*t*t + 2*s->control.x*t*(1-t) + s->start.x*(1-t)*(1-t);
475 p.y= s->end.y*t*t + 2*s->control.y*t*(1-t) + s->start.y*(1-t)*(1-t);
479 static int approximate3(const cspline_t*s, qspline_t*q, int size, double quality2)
481 unsigned int gran = 0;
482 unsigned int istep = 0x80000000;
483 unsigned int istart = 0;
487 while(istart<0x80000000)
489 unsigned int iend = istart + istep;
490 double start = istart/(double)0x80000000;
491 double end = iend/(double)0x80000000;
494 char left = 0,recurse=0;
499 /* create simple approximation: a qspline_t which run's through the
500 qspline_t point at 0.5 */
501 test.start = cspline_getpoint(s, start);
502 test.control = cspline_getpoint(s, (start+end)/2);
503 test.end = cspline_getpoint(s, end);
504 /* fix the control point:
505 move it so that the new spline does runs through it */
506 test.control.x = -(test.end.x + test.start.x)/2 + 2*(test.control.x);
507 test.control.y = -(test.end.y + test.start.y)/2 + 2*(test.control.y);
509 /* depending on where we are in the spline, we either try to match
510 the left or right tangent */
514 pos = left?start:end;
516 test.control.x = s->end.x*(3*qpos) + 3*s->control2.x*(2*pos-3*qpos) +
517 3*s->control1.x*(1-4*pos+3*qpos) + s->start.x*(-3+6*pos-3*qpos);
518 test.control.y = s->end.y*(3*qpos) + 3*s->control2.y*(2*pos-3*qpos) +
519 3*s->control1.y*(1-4*pos+3*qpos) + s->start.y*(-3+6*pos-3*qpos);
521 test.control.x *= (end-start)/2;
522 test.control.y *= (end-start)/2;
523 test.control.x += test.start.x;
524 test.control.y += test.start.y;
526 test.control.x *= -(end-start)/2;
527 test.control.y *= -(end-start)/2;
528 test.control.x += test.end.x;
529 test.control.y += test.end.y;
534 /* measure the spline's accurancy, by taking a number of probes */
535 for(t=0;t<probes;t++) {
536 gfxpoint_t qr1,qr2,cr1,cr2;
537 double pos = 0.5/(probes*2)*(t*2+1);
540 qr1 = qspline_getpoint(&test, pos);
541 cr1 = cspline_getpoint(s, start+pos*(end-start));
550 qr2 = qspline_getpoint(&test, (1-pos));
551 cr2 = cspline_getpoint(s, start+(1-pos)*(end-start));
561 #else // quadratic error: *much* faster!
563 /* convert control point representation to
564 d*x^3 + c*x^2 + b*x + a */
565 dx= s->end.x - s->control2.x*3 + s->control1.x*3 - s->start.x;
566 dy= s->end.y - s->control2.y*3 + s->control1.y*3 - s->start.y;
568 /* we need to do this for the subspline between [start,end], not [0,1]
569 as a transformation of t->a*t+b does nothing to highest coefficient
570 of the spline except multiply it with a^3, we just need to modify
572 {double m = end-start;
577 /* use the integral over (f(x)-g(x))^2 between 0 and 1
578 to measure the approximation quality.
579 (it boils down to const*d^2) */
580 recurse = (dx*dx + dy*dy > quality2);
583 if(recurse && istep>1 && size-level > num) {
590 while(!(istart & istep)) {
599 void gfxdraw_conicTo(gfxdrawer_t*draw, double cx, double cy, double tox, double toy, double quality)
601 double c1x = (draw->x + 2 * cx) / 3;
602 double c1y = (draw->y + 2 * cy) / 3;
603 double c2x = (2 * cx + tox) / 3;
604 double c2y = (2 * cy + toy) / 3;
605 gfxdraw_cubicTo(draw, c1x, c1y, c2x, c2y, tox, toy, quality);
609 void gfxdraw_cubicTo(gfxdrawer_t*draw, double c1x, double c1y, double c2x, double c2y, double x, double y, double quality)
613 double maxerror = quality>0 ? quality : 1.0;
625 num = approximate3(&c, q, 128, maxerror);
630 mid.x = q[t].control.x;
631 mid.y = q[t].control.y;
634 draw->splineTo(draw, mid.x, mid.y, to.x, to.y);
638 gfxbbox_t gfxbbox_expand_to_point(gfxbbox_t box, gfxcoord_t x, gfxcoord_t y)
640 if(box.xmin==0 && box.ymin==0 && box.xmax==0 && box.ymax==0) {
645 if(x==0 && y==0) box.xmax = 0.0000001;
659 gfxbbox_t gfxbbox_expand_to_bbox(gfxbbox_t box, gfxbbox_t box2)
661 box = gfxbbox_expand_to_point(box, box2.xmin, box2.ymin);
662 box = gfxbbox_expand_to_point(box, box2.xmax, box2.ymax);
666 void gfxbbox_intersect(gfxbbox_t*box1, gfxbbox_t*box2)
668 if(box2->xmin > box1->xmin)
669 box1->xmin = box2->xmin;
670 if(box2->ymin > box1->ymin)
671 box1->ymin = box2->ymin;
672 if(box2->xmax < box1->xmax)
673 box1->xmax = box2->xmax;
674 if(box2->ymax > box1->ymax)
675 box1->ymax = box2->ymax;
676 if(box1->xmin > box1->xmax)
677 box1->xmax = box1->xmin;
678 if(box1->ymin > box1->ymax)
679 box1->ymax = box1->ymin;
682 gfxbbox_t gfxline_getbbox(gfxline_t*line)
685 gfxbbox_t bbox = {0,0,0,0};
688 if(line->type == gfx_moveTo) {
690 } else if(line->type == gfx_lineTo) {
691 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
692 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
694 } else if(line->type == gfx_splineTo) {
695 if(last) bbox = gfxbbox_expand_to_point(bbox, x, y);
696 bbox = gfxbbox_expand_to_point(bbox, line->sx, line->sy);
697 bbox = gfxbbox_expand_to_point(bbox, line->x, line->y);
707 gfxline_t* gfxline_append(gfxline_t*line1, gfxline_t*line2)
709 gfxline_t*l = line1;;
719 void gfxline_transform(gfxline_t*line, gfxmatrix_t*matrix)
722 double x = matrix->m00*line->x + matrix->m10*line->y + matrix->tx;
723 double y = matrix->m01*line->x + matrix->m11*line->y + matrix->ty;
726 if(line->type == gfx_splineTo) {
727 double sx = matrix->m00*line->sx + matrix->m10*line->sy + matrix->tx;
728 double sy = matrix->m01*line->sx + matrix->m11*line->sy + matrix->ty;
736 void gfxmatrix_dump(gfxmatrix_t*m, FILE*fi, char*prefix)
738 fprintf(fi, "%s%f %f | %f\n", prefix, m->m00, m->m10, m->tx);
739 fprintf(fi, "%s%f %f | %f\n", prefix, m->m01, m->m11, m->ty);
742 void gfxmatrix_transform(gfxmatrix_t*m, double* v, double*dest)
744 dest[0] = m->m00*v[0] + m->m10*v[1] + m->tx;
745 dest[1] = m->m01*v[0] + m->m11*v[1] + m->ty;
747 void gfxmatrix_invert(gfxmatrix_t*m, gfxmatrix_t*dest)
749 double det = m->m00 * m->m11 - m->m10 * m->m01;
751 memset(dest, 0, sizeof(gfxmatrix_t));
755 dest->m00 = m->m11 * det;
756 dest->m01 = -m->m01 * det;
757 dest->m10 = -m->m10 * det;
758 dest->m11 = m->m00 * det;
759 dest->tx = -(dest->m00 * m->tx + dest->m10 * m->ty);
760 dest->ty = -(dest->m01 * m->tx + dest->m11 * m->ty);
762 void gfxmatrix_unit(gfxmatrix_t*m)
764 memset(m, 0, sizeof(gfxmatrix_t));
768 void gfxmatrix_multiply(gfxmatrix_t*m1, gfxmatrix_t*m2, gfxmatrix_t*dest)
770 dest->m00 = m1->m00*m2->m00 + m1->m10*m2->m01;
771 dest->m01 = m1->m01*m2->m00 + m1->m11*m2->m01;
772 dest->m10 = m1->m00*m2->m10 + m1->m10*m2->m11;
773 dest->m11 = m1->m01*m2->m10 + m1->m11*m2->m11;
774 dest->tx = m1->m00*m2->tx + m1->m10*m2->ty + m1->tx;
775 dest->ty = m1->m01*m2->tx + m1->m11*m2->ty + m1->ty;
778 gfxfontlist_t* gfxfontlist_create()
780 /* Initial list ist empty */
784 gfxfont_t*gfxfontlist_findfont(gfxfontlist_t*list, char*id)
786 gfxfontlist_t*l = list;
788 if(!strcmp((char*)l->font->id, id)) {
795 char gfxfontlist_hasfont(gfxfontlist_t*list, gfxfont_t*font)
797 gfxfontlist_t*l = list;
799 if(!strcmp((char*)l->font->id, font->id)) {
806 void*gfxfontlist_getuserdata(gfxfontlist_t*list, const char*id)
808 gfxfontlist_t*l = list;
810 if(!strcmp((char*)l->font->id, id)) {
817 gfxfontlist_t*gfxfontlist_addfont2(gfxfontlist_t*list, gfxfont_t*font, void*user)
819 gfxfontlist_t*last=0,*l = list;
822 if(l->font == font) {
823 return list; // we already know this font
828 fprintf(stderr, "Tried to add zero font\n");
830 l = (gfxfontlist_t*)rfx_calloc(sizeof(gfxfontlist_t));
841 gfxfontlist_t*gfxfontlist_addfont(gfxfontlist_t*list, gfxfont_t*font)
843 return gfxfontlist_addfont2(list, font, 0);
845 void gfxfontlist_free(gfxfontlist_t*list, char deletefonts)
847 gfxfontlist_t*l = list;
849 gfxfontlist_t*next = l->next;
850 if(deletefonts && l->font) {
851 gfxfont_free(l->font);l->font=0;
859 gfxline_t*gfxline_makerectangle(double x1,double y1,double x2, double y2)
861 gfxline_t* line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t)*5);
862 line[0].x = x1;line[0].y = y1;line[0].type = gfx_moveTo;line[0].next = &line[1];
863 line[1].x = x2;line[1].y = y1;line[1].type = gfx_lineTo;line[1].next = &line[2];
864 line[2].x = x2;line[2].y = y2;line[2].type = gfx_lineTo;line[2].next = &line[3];
865 line[3].x = x1;line[3].y = y2;line[3].type = gfx_lineTo;line[3].next = &line[4];
866 line[4].x = x1;line[4].y = y1;line[4].type = gfx_lineTo;
870 gfxline_t*gfxline_makecircle(double x,double y,double rx, double ry)
874 double begin = 0.7070;
875 gfxline_t** line = (gfxline_t**)rfx_calloc(sizeof(gfxline_t*)*9);
878 line[t] = rfx_calloc(sizeof(gfxline_t));
880 line[0]->type = gfx_moveTo;
881 line[0]->x = x+begin*rx;
882 line[0]->y = y+begin*ry;
884 line[t-1]->next = line[t];
885 line[t]->type = gfx_splineTo;
888 #define R(nr,cx,cy,mx,my) \
889 line[nr]->sx = line[nr-1]->x + (cx); \
890 line[nr]->sy = line[nr-1]->y + (cy); \
891 line[nr]->x = line[nr]->sx + (mx); \
892 line[nr]->y = line[nr]->sy + (my);
893 R(1, -C1*rx, C1*ry, -C2*rx, 0);
894 R(2, -C2*rx, 0, -C1*rx, -C1*ry);
895 R(3, -C1*rx, -C1*ry, 0, -C2*ry);
896 R(4, 0, -C2*ry, C1*rx, -C1*ry);
897 R(5, C1*rx, -C1*ry, C2*rx, 0);
898 R(6, C2*rx, 0, C1*rx, C1*ry);
899 R(7, C1*rx, C1*ry, 0, C2*ry);
900 R(8, 0, C2*ry, -C1*rx, C1*ry);
901 gfxline_t*l = line[0];
906 gfxbbox_t* gfxline_isrectangle(gfxline_t*_l)
911 gfxline_t*l = gfxline_clone(_l);
926 if(xc==2 && x!=x1 && x!=x2) {fail=1;break;}
927 else if(xc>=1 && x==x1) {left=0;}
928 else if(xc==2 && x==x2) {left=1;}
929 else if(xc==1 && x!=x1) {x2 = x; xc=2; left=1;}
930 else if(xc==0) {x1 = x; xc=1;left=0;}
931 else {fprintf(stderr, "Internal error in rectangle detection\n");}
933 if(yc==2 && y!=y1 && y!=y2) {fail=1;break;}
934 else if(yc>=1 && y==y1) {top=0;}
935 else if(yc==2 && y==y2) {top=1;}
936 else if(yc==1 && y!=y1) {y2 = y; yc=2; top=1;}
937 else if(yc==0) {y1 = y; yc=1;top=0;}
938 else {fprintf(stderr, "Internal error in rectangle detection\n");}
940 char pos=top<<1|left;
943 /* diagonal lines not allowed */
948 /* no corner except the first one may be touched twice */
949 if(pos && (corners & 1<<pos)) {
952 /* mark which corners have been touched so far */
960 if(corners!=0x0f) return 0; // not all 4 corners reached
962 if(x2<x1) {double x = x2;x2=x1;x1=x;}
963 if(y2<y1) {double y = y2;y2=y1;y1=y;}
965 gfxbbox_t*r = malloc(sizeof(gfxbbox_t));
966 r->xmin = x1; r->ymin = y1;
967 r->xmax = x2; r->ymax = y2;
971 void gfximage_transform(gfximage_t*img, gfxcxform_t*cxform)
974 int size = img->width*img->height;
980 rr = (int)(cxform->rr*256);gr = (int)(cxform->gr*256);
981 rg = (int)(cxform->rg*256);gg = (int)(cxform->gg*256);
982 rb = (int)(cxform->rb*256);gb = (int)(cxform->gb*256);
983 ra = (int)(cxform->ra*256);ga = (int)(cxform->ga*256);
984 br = (int)(cxform->br*256);ar = (int)(cxform->ar*256);tr = (int)(cxform->tr*256);
985 bg = (int)(cxform->bg*256);ag = (int)(cxform->ag*256);tg = (int)(cxform->tg*256);
986 bb = (int)(cxform->bb*256);ab = (int)(cxform->ab*256);tb = (int)(cxform->tb*256);
987 ba = (int)(cxform->ba*256);aa = (int)(cxform->aa*256);ta = (int)(cxform->ta*256);
989 for(t=0;t<size;t++) {
990 gfxcolor_t*pixel = &img->data[t];
991 unsigned char r = (pixel->r * rr + pixel->g * rg + pixel->b * rb + pixel->a * ra + tr) / 256;
992 unsigned char g = (pixel->r * gr + pixel->g * gg + pixel->b * gb + pixel->a * ga + tg) / 256;
993 unsigned char b = (pixel->r * br + pixel->g * bg + pixel->b * bb + pixel->a * ba + tb) / 256;
994 unsigned char a = (pixel->r * ar + pixel->g * ag + pixel->b * ab + pixel->a * aa + ta) / 256;
1001 void gfxline_dump(gfxline_t*line, FILE*fi, char*prefix)
1004 if(line->type == gfx_moveTo) {
1005 fprintf(fi, "%smoveTo %.2f %.2f\n", prefix, line->x, line->y);
1006 } else if(line->type == gfx_lineTo) {
1007 fprintf(fi, "%slineTo %.2f %.2f\n", prefix, line->x, line->y);
1008 } else if(line->type == gfx_splineTo) {
1009 fprintf(fi, "%ssplineTo (%.2f %.2f) %.2f %.2f\n", prefix, line->sx, line->sy, line->x, line->y);
1015 static char gfxpoint_equals(void*c1, void*c2)
1017 return !memcmp(c1, c2, sizeof(gfxpoint_t));
1019 static unsigned int gfxpoint_hash(void*c)
1021 return string_hash3(c, sizeof(gfxpoint_t));
1023 static void* gfxpoint_clone(void*c)
1025 void*n = malloc(sizeof(gfxpoint_t));
1026 memcpy(n, c, sizeof(gfxpoint_t));
1029 static void gfxpoint_destroy(void*c)
1033 static type_t gfxpoint_type = {
1034 hash: (hash_func)gfxpoint_hash,
1035 equals: (equals_func)gfxpoint_equals,
1036 dup: (dup_func)gfxpoint_clone,
1037 free: (free_func)gfxpoint_destroy,
1040 gfxline_t* gfxline_restitch(gfxline_t*line)
1042 dict_t*ff = dict_new2(&gfxpoint_type);
1043 dict_t*rev = dict_new2(&gfxpoint_type);
1047 gfxline_t*next = line->next;
1048 if(line->type == gfx_moveTo && (line->next && line->next->type != gfx_moveTo)) {
1049 gfxpoint_t xy = {line->x, line->y};
1050 dict_put(ff, &xy, line);
1052 } else if(!line->next || line->next->type == gfx_moveTo) {
1054 gfxpoint_t xy = {line->x, line->y};
1055 dict_put(rev, &xy, prev);
1063 gfxpoint_t pos = {0,0};
1065 gfxline_t*result = 0;
1069 while(dict_count(ff)) {
1070 char reverse = 0, stitch = 1;
1071 gfxline_t*l = dict_lookup(ff, &pos);
1073 char d = dict_del2(ff,&pos,l);assert(d);
1075 l = dict_lookup(rev, &pos);
1078 char d = dict_del2(rev,&pos,l);assert(d);
1082 /* try to find *any* entry. this is costly, but
1083 doesn't happen too often */
1085 DICT_ITERATE_DATA(ff, gfxline_t*, l2) {
1090 gfxpoint_t xy = {l->x,l->y};
1091 char d = dict_del2(ff,&xy,l);assert(d);
1096 while(end->next) end = end->next;
1099 char d = dict_del2(rev,&pos,l);assert(d);
1101 l = gfxline_reverse(l);
1104 char d = dict_del2(ff,&pos,end);assert(d);
1107 assert(l->type == gfx_moveTo);
1108 if(stitch && !first) {
1109 /* cut away the moveTo */
1110 gfxline_t*next = l->next;
1129 gfxline_t* gfxline_reverse(gfxline_t*line)
1133 gfxline_t*next = line->next;
1134 if(next && next->type != gfx_moveTo) {
1135 line->type = next->type;
1136 line->sx = next->sx;
1137 line->sy = next->sy;
1139 line->type = gfx_moveTo;
1148 void gfxgradient_destroy(gfxgradient_t*gradient)
1151 gfxgradient_t*next = gradient->next;
1157 gfxparams_t* gfxparams_new()
1159 return (gfxparams_t*)rfx_calloc(sizeof(gfxparams_t));
1162 void gfxparams_store(gfxparams_t*params, const char*key, const char*value)
1164 gfxparam_t*o = params->params;
1166 if(!strcmp(key, o->key)) {
1167 /* overwrite old value */
1168 free((void*)o->value);
1169 o->value = strdup(value);
1174 gfxparam_t*p = (gfxparam_t*)malloc(sizeof(gfxparam_t));
1175 p->key = strdup(key);
1176 p->value = strdup(value);
1180 params->last->next = p;
1188 void gfxparams_free(gfxparams_t*params)
1190 gfxparam_t*p = params->params;
1192 gfxparam_t*next = p->next;
1193 free((void*)p->key);
1194 if(p->value) free((void*)p->value);