3 Part of the swftools package.
5 Copyright (c) 2005/2006/2007 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 #include "../gfxdevice.h"
26 #include "../gfxtools.h"
28 #define PNG_INLINE_EXPORTS
34 typedef gfxcolor_t RGBA;
36 typedef struct _renderpoint
41 typedef struct _renderline
48 typedef struct _internal_result {
50 struct _internal_result*next;
53 typedef struct _clipbuffer {
55 struct _clipbuffer*next;
58 typedef struct _internal {
76 internal_result_t*results;
77 internal_result_t*result_next;
80 typedef enum {filltype_solid,filltype_clip,filltype_bitmap,filltype_gradient} filltype_t;
82 typedef struct _fillinfo {
93 static inline void add_pixel(internal_t*i, float x, int y)
97 if(x >= i->width2 || y >= i->height2 || y<0) return;
99 if(y<i->ymin) i->ymin = y;
100 if(y>i->ymax) i->ymax = y;
102 renderline_t*l = &i->lines[y];
104 if(l->num == l->size) {
106 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
108 l->points[l->num] = p;
112 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
113 problem appears to often */
116 #define INT(x) ((int)((x)+16)-16)
118 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
120 internal_t*i = (internal_t*)dev->internal;
122 double ny1, ny2, stepx;
124 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
125 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
142 ny1 = INT(y1) + 1.0 + CUT;
145 ny2 = INT(y2) - 1.0 + CUT;
152 x1 = x1 + (ny1-y1)*stepx;
153 x2 = x2 + (ny2-y2)*stepx;
162 float xx = (float)(startx + posx);
163 add_pixel(i, xx ,posy);
169 #define PI 3.14159265358979
170 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
172 /* TODO: handle cap styles */
174 internal_t*i = (internal_t*)dev->internal;
187 /* Make sure the line is always at least one pixel wide */
189 /* That's what Macromedia's Player does at least at zoom level >= 1. */
192 /* That's what Macromedia's Player seems to do at zoom level 0. */
193 /* TODO: needs testing */
195 /* TODO: how does this interact with scaling? */
196 if(width * i->multiply < 1.0)
197 width = 1.0 / i->multiply;
200 sd = (double)dx*(double)dx+(double)dy*(double)dy;
222 add_line(dev, x1+vx, y1+vy, xx, yy);
225 for(t=1;t<segments;t++) {
226 double s = sin(t*PI/segments);
227 double c = cos(t*PI/segments);
228 xx = (x2 + vx*c - vy*s);
229 yy = (y2 + vx*s + vy*c);
230 add_line(dev, lastx, lasty, xx, yy);
237 add_line(dev, lastx, lasty, xx, yy);
242 add_line(dev, lastx, lasty, xx, yy);
245 for(t=1;t<segments;t++) {
246 double s = sin(t*PI/segments);
247 double c = cos(t*PI/segments);
248 xx = (x1 - vx*c + vy*s);
249 yy = (y1 - vx*s - vy*c);
250 add_line(dev, lastx, lasty, xx, yy);
254 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
257 static int compare_renderpoints(const void * _a, const void * _b)
259 renderpoint_t*a = (renderpoint_t*)_a;
260 renderpoint_t*b = (renderpoint_t*)_b;
261 if(a->x < b->x) return -1;
262 if(a->x > b->x) return 1;
266 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
270 U32 bit = 1<<(x1&31);
271 int bitpos = (x1/32);
274 int ainv = 255-col.a;
275 col.r = (col.r*col.a)>>8;
276 col.g = (col.g*col.a)>>8;
277 col.b = (col.b*col.a)>>8;
280 line[x].r = ((line[x].r*ainv)>>8)+col.r;
281 line[x].g = ((line[x].g*ainv)>>8)+col.g;
282 line[x].b = ((line[x].b*ainv)>>8)+col.b;
284 line[x].a = ((line[x].a*ainv)>>8)+col.a;
304 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
308 gfxmatrix_t*m = info->matrix;
309 gfximage_t*b = info->image;
311 if(!b->width || !b->height) {
312 gfxcolor_t red = {255,255,0,0};
313 fill_line_solid(line, z, y, x1, x2, red);
317 double det = m->m00*m->m11 - m->m01*m->m10;
318 if(fabs(det) < 0.0005) {
319 /* x direction equals y direction- the image is invisible */
323 double xx1 = ( (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
324 double yy1 = (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
325 double xinc1 = m->m11 * det;
326 double yinc1 = m->m01 * det;
328 U32 bit = 1<<(x1&31);
329 int bitpos = (x1/32);
334 int xx = (int)(xx1 + x * xinc1);
335 int yy = (int)(yy1 - x * yinc1);
338 if(info->clip_or_radial) {
340 if(xx>=b->width) xx = b->width-1;
342 if(yy>=b->height) yy = b->height-1;
346 if(xx<0) xx += b->width;
347 if(yy<0) yy += b->height;
350 col = b->data[yy*b->width+xx];
353 /* needs bitmap with premultiplied alpha */
354 line[x].r = ((line[x].r*ainv)>>8)+col.r;
355 line[x].g = ((line[x].g*ainv)>>8)+col.g;
356 line[x].b = ((line[x].b*ainv)>>8)+col.b;
366 static void fill_line_gradient(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
370 gfxmatrix_t*m = info->matrix;
371 RGBA*g= info->gradient;
373 double det = m->m00*m->m11 - m->m01*m->m10;
374 if(fabs(det) < 0.0005) {
375 /* x direction equals y direction */
379 double xx1 = ( (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
380 double yy1 = (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
381 double xinc1 = m->m11 * det;
382 double yinc1 = m->m01 * det;
384 U32 bit = 1<<(x1&31);
385 int bitpos = (x1/32);
393 if(info->clip_or_radial) {
394 double xx = xx1 + x * xinc1;
395 double yy = yy1 + y * yinc1;
396 double r = sqrt(xx*xx + yy*yy);
398 pos = (int)(r*255.999);
400 double r = xx1 + x * xinc1;
403 pos = (int)((r+1)*127.999);
408 /* needs bitmap with premultiplied alpha */
409 line[x].r = ((line[x].r*ainv)>>8)+col.r;
410 line[x].g = ((line[x].g*ainv)>>8)+col.g;
411 line[x].b = ((line[x].b*ainv)>>8)+col.b;
421 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
425 U32 bit = 1<<(x1&31);
426 int bitpos = (x1/32);
437 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
439 if(fill->type == filltype_solid)
440 fill_line_solid(line, zline, y, startx, endx, *fill->color);
441 else if(fill->type == filltype_clip)
442 fill_line_clip(line, zline, y, startx, endx);
443 else if(fill->type == filltype_bitmap)
444 fill_line_bitmap(line, zline, y, startx, endx, fill);
445 else if(fill->type == filltype_gradient)
446 fill_line_gradient(line, zline, y, startx, endx, fill);
449 void fill(gfxdevice_t*dev, fillinfo_t*fill)
451 internal_t*i = (internal_t*)dev->internal;
454 for(y=i->ymin;y<=i->ymax;y++) {
455 renderpoint_t*points = i->lines[y].points;
456 RGBA*line = &i->img[i->width2*y];
457 U32*zline = &i->clipbuf->data[i->bitwidth*y];
460 int num = i->lines[y].num;
462 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
465 renderpoint_t*p = &points[n];
466 renderpoint_t*next= n<num-1?&points[n+1]:0;
468 int endx = next?next->x:i->width2;
477 fill_line(dev, line, zline, y, startx, endx, fill);
480 if(endx == i->width2)
483 if(fill->type == filltype_clip) {
484 if(i->clipbuf->next) {
485 U32*line2 = &i->clipbuf->next->data[i->bitwidth*y];
487 for(x=0;x<i->bitwidth;x++)
488 zline[x] &= line2[x];
496 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
499 memset(&info, 0, sizeof(info));
500 info.type = filltype_solid;
505 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
507 internal_t*i = (internal_t*)dev->internal;
508 if(!strcmp(key, "antialize") || !strcmp(key, "antialise")) {
509 i->antialize = atoi(value);
510 i->zoom = i->antialize * i->multiply;
512 } else if(!strcmp(key, "multiply")) {
513 i->multiply = atoi(value);
514 i->zoom = i->antialize * i->multiply;
515 fprintf(stderr, "Warning: multiply not implemented yet\n");
517 } else if(!strcmp(key, "fillwhite")) {
518 i->fillwhite = atoi(value);
524 void newclip(struct _gfxdevice*dev)
526 internal_t*i = (internal_t*)dev->internal;
528 clipbuffer_t*c = (clipbuffer_t*)rfx_calloc(sizeof(clipbuffer_t));
529 c->data = (U32*)rfx_calloc(sizeof(U32) * i->bitwidth * i->height2);
530 c->next = i->clipbuf;
533 memcpy(c->data, c->next->data, i->bitwidth*i->height2);
535 memset(c->data, 0, sizeof(U32)*i->bitwidth*i->height2);
538 void endclip(struct _gfxdevice*dev, char removelast)
540 internal_t*i = (internal_t*)dev->internal;
542 /* test for at least one cliplevel (the one we created ourselves) */
543 if(!i->clipbuf || (!i->clipbuf->next && !removelast)) {
544 fprintf(stderr, "endclip without any active clip buffers\n");
548 clipbuffer_t*c = i->clipbuf;
549 i->clipbuf = i->clipbuf->next;
551 free(c->data);c->data = 0;
555 void render_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
557 internal_t*i = (internal_t*)dev->internal;
560 /*if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
561 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
565 if(line->type == gfx_moveTo) {
566 } else if(line->type == gfx_lineTo) {
567 double x1=x*i->zoom,y1=y*i->zoom;
568 double x3=line->x*i->zoom,y3=line->y*i->zoom;
569 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
570 fill_solid(dev, color);
571 } else if(line->type == gfx_splineTo) {
575 double x1=x*i->zoom,y1=y*i->zoom;
576 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
577 double x3=line->x*i->zoom,y3=line->y*i->zoom;
579 double c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
583 parts = (int)(sqrt(c)/3);
584 if(!parts) parts = 1;
586 for(t=1;t<=parts;t++) {
587 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
588 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
590 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
591 fill_solid(dev, color);
602 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
604 internal_t*i = (internal_t*)dev->internal;
609 int x1,y1,x2,y2,x3,y3;
611 if(line->type == gfx_moveTo) {
612 } else if(line->type == gfx_lineTo) {
613 double x1=x*i->zoom,y1=y*i->zoom;
614 double x3=line->x*i->zoom,y3=line->y*i->zoom;
616 add_line(dev, x1, y1, x3, y3);
617 } else if(line->type == gfx_splineTo) {
618 int c,t,parts,qparts;
621 double x1=x*i->zoom,y1=y*i->zoom;
622 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
623 double x3=line->x*i->zoom,y3=line->y*i->zoom;
625 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
629 parts = (int)(sqrt(c));
630 if(!parts) parts = 1;
632 for(t=1;t<=parts;t++) {
633 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
634 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
636 add_line(dev, xx, yy, nx, ny);
647 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
649 internal_t*i = (internal_t*)dev->internal;
651 memset(&info, 0, sizeof(info));
653 info.type = filltype_clip;
654 draw_line(dev, line);
658 void render_endclip(struct _gfxdevice*dev)
660 internal_t*i = (internal_t*)dev->internal;
664 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
666 internal_t*i = (internal_t*)dev->internal;
668 draw_line(dev, line);
669 fill_solid(dev, color);
672 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
674 internal_t*i = (internal_t*)dev->internal;
676 gfxmatrix_t m2 = *matrix;
678 draw_line(dev, line);
681 memset(&info, 0, sizeof(info));
682 info.type = filltype_bitmap;
685 info.cxform = cxform;
687 m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
688 m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
693 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
695 internal_t*i = (internal_t*)dev->internal;
697 gfxmatrix_t m2 = *matrix;
699 draw_line(dev, line);
703 memset(&info, 0, sizeof(info));
704 info.type = filltype_gradient;
708 m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
709 m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
711 info.clip_or_radial = type == gfxgradient_radial;
714 gfxcolor_t color = {0,0,0,0};
717 int nextpos = gradient->pos*256;
720 msg("<error> Invalid gradient- contains values > 1.0");
724 gfxcolor_t nextcolor = gradient->color;
728 double step = 1.0/(nextpos-pos);
730 for(t=pos;t<nextpos;t++) {
731 g[t].r = color.r*p0 + nextcolor.r*p1;
732 g[t].g = color.g*p0 + nextcolor.g*p1;
733 g[t].b = color.b*p0 + nextcolor.b*p1;
734 g[t].a = color.a*p0 + nextcolor.a*p1;
742 gradient = gradient->next;
745 msg("<error> Invalid gradient- doesn't end with 1.0");
751 void render_addfont(struct _gfxdevice*dev, gfxfont_t*font)
755 void render_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
757 internal_t*i = (internal_t*)dev->internal;
761 /* align characters to whole pixels */
762 matrix->tx = (int)(matrix->tx * i->antialize) / i->antialize;
763 matrix->ty = (int)(matrix->ty * i->antialize) / i->antialize;
765 gfxglyph_t*glyph = &font->glyphs[glyphnr];
766 gfxline_t*line2 = gfxline_clone(glyph->line);
767 gfxline_transform(line2, matrix);
768 draw_line(dev, line2);
769 fill_solid(dev, color);
775 void render_result_write(gfxresult_t*r, int filedesc)
777 internal_result_t*i= (internal_result_t*)r->internal;
779 int render_result_save(gfxresult_t*r, const char*filename)
781 internal_result_t*i= (internal_result_t*)r->internal;
783 return 0; // no pages drawn
787 char filenamebuf[256];
788 char*origname = strdup(filename);
789 int l = strlen(origname);
790 if(l>3 && strchr("gG",origname[l-1]) && strchr("nN",filename[l-2]) &&
791 strchr("pP",origname[l-3]) && filename[l-4]=='.') {
795 sprintf(filenamebuf, "%s.%d.png", origname, nr);
796 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
801 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
805 char*gfximage_asXPM(gfximage_t*img, int depth)
808 char*str = (char*)malloc(img->width*img->height*4 + 500 + 16*depth*depth*depth);
810 p+= sprintf(p, "static char *noname[] = {\n\"%d %d 262144 3\",\n");
814 for(b=0;b<depth;b++) {
815 p += sprintf(p, "\"%c%c%c c #%02x%02x%02x\",\n", r+32,g+32,b+32, r*d,g*d,b*d);
818 for(y=0;y<img->height;y++) {
820 gfxcolor_t*col = &img->data[y*img->height];
822 for(x=0;x<img->width;x++) {
823 p+=sprintf(p, "%c%c%c", 32+(col->r/d), 32+(col->g/d), 32+(col->b/d));
825 p+=sprintf(p, "\",\n");
830 void*render_result_get(gfxresult_t*r, const char*name)
832 internal_result_t*i= (internal_result_t*)r->internal;
833 if(!strncmp(name,"xpm",3)) {
834 int pagenr = atoi(&name[3]);
843 return gfximage_asXPM(&i->img, 64);
844 } else if(!strncmp(name,"page",4)) {
845 int pagenr = atoi(&name[4]);
858 void render_result_destroy(gfxresult_t*r)
860 internal_result_t*i= (internal_result_t*)r->internal;
863 internal_result_t*next = i->next;
864 free(i->img.data);i->img.data = 0;
867 the following rfx_free causes a segfault on WIN32 machines,
876 gfxresult_t* render_finish(struct _gfxdevice*dev)
878 internal_t*i = (internal_t*)dev->internal;
880 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
882 res->internal = i->results;i->results = 0;
883 res->write = render_result_write;
884 res->save = render_result_save;
885 res->get = render_result_get;
886 res->destroy = render_result_destroy;
888 free(dev->internal); dev->internal = 0; i = 0;
893 void render_startpage(struct _gfxdevice*dev, int width, int height)
895 internal_t*i = (internal_t*)dev->internal;
898 if(i->width2 || i->height2) {
899 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
903 i->width = width*i->multiply;
904 i->height = height*i->multiply;
905 i->width2 = width*i->zoom;
906 i->height2 = height*i->zoom;
907 i->bitwidth = (i->width2+31)/32;
909 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
910 for(y=0;y<i->height2;y++) {
911 memset(&i->lines[y], 0, sizeof(renderline_t));
912 i->lines[y].points = 0;
915 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
917 memset(i->img, 0xff, sizeof(RGBA)*i->width2*i->height2);
920 i->ymin = 0x7fffffff;
921 i->ymax = -0x80000000;
924 /* initialize initial clipping field, which doesn't clip anything yet */
926 memset(i->clipbuf->data, 255, sizeof(U32)*i->bitwidth*i->height2);
929 static void store_image(internal_t*i, internal_result_t*ir)
931 ir->img.data = (gfxcolor_t*)malloc(i->width*i->height*sizeof(gfxcolor_t));
932 ir->img.width = i->width;
933 ir->img.height = i->height;
935 gfxcolor_t*dest = ir->img.data;
937 if(i->antialize <= 1) /* no antializing */ {
939 for(y=0;y<i->height;y++) {
940 RGBA*line = &i->img[y*i->width];
941 memcpy(&dest[y*i->width], line, sizeof(RGBA)*i->width);
944 RGBA**lines = (RGBA**)rfx_calloc(sizeof(RGBA*)*i->antialize);
945 int q = i->antialize*i->antialize;
949 for(y=0;y<i->height2;y++) {
951 ypos = y % i->antialize;
952 lines[ypos] = &i->img[y*i->width2];
953 if(ypos == i->antialize-1) {
954 RGBA*out = &dest[(y2++)*i->width];
957 for(x=0;x<i->width;x++) {
958 int xpos = x*i->antialize;
961 for(yp=0;yp<i->antialize;yp++) {
962 RGBA*lp = &lines[yp][xpos];
964 for(xp=0;xp<i->antialize;xp++) {
983 void render_endpage(struct _gfxdevice*dev)
985 internal_t*i = (internal_t*)dev->internal;
987 if(!i->width2 || !i->height2) {
988 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
994 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
998 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
1005 if(i->result_next) {
1006 i->result_next->next = ir;
1011 i->result_next = ir;
1013 for(y=0;y<i->height2;y++) {
1014 rfx_free(i->lines[y].points); i->lines[y].points = 0;
1016 rfx_free(i->lines);i->lines=0;
1018 if(i->img) {rfx_free(i->img);i->img = 0;}
1024 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
1026 /* not supported for this output device */
1029 void gfxdevice_render_init(gfxdevice_t*dev)
1031 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
1032 memset(dev, 0, sizeof(gfxdevice_t));
1034 dev->name = "render";
1046 dev->setparameter = render_setparameter;
1047 dev->startpage = render_startpage;
1048 dev->startclip = render_startclip;
1049 dev->endclip = render_endclip;
1050 dev->stroke = render_stroke;
1051 dev->fill = render_fill;
1052 dev->fillbitmap = render_fillbitmap;
1053 dev->fillgradient = render_fillgradient;
1054 dev->addfont = render_addfont;
1055 dev->drawchar = render_drawchar;
1056 dev->drawlink = render_drawlink;
1057 dev->endpage = render_endpage;
1058 dev->finish = render_finish;