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
33 typedef gfxcolor_t RGBA;
35 typedef struct _renderpoint
40 typedef struct _renderline
47 typedef struct _internal_result {
49 struct _internal_result*next;
52 typedef struct _clipbuffer {
54 struct _clipbuffer*next;
57 typedef struct _internal {
75 internal_result_t*results;
76 internal_result_t*result_next;
79 typedef enum {filltype_solid,filltype_clip,filltype_bitmap} filltype_t;
81 typedef struct _fillinfo {
82 filltype_t type; //0=solid,1=clip
91 static inline void add_pixel(internal_t*i, float x, int y)
95 if(x >= i->width2 || y >= i->height2 || y<0) return;
97 if(y<i->ymin) i->ymin = y;
98 if(y>i->ymax) i->ymax = y;
100 renderline_t*l = &i->lines[y];
102 if(l->num == l->size) {
104 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
106 l->points[l->num] = p;
110 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
111 problem appears to often */
114 #define INT(x) ((int)((x)+16)-16)
116 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
118 internal_t*i = (internal_t*)dev->internal;
120 double ny1, ny2, stepx;
122 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
123 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
140 ny1 = INT(y1) + 1.0 + CUT;
143 ny2 = INT(y2) - 1.0 + CUT;
150 x1 = x1 + (ny1-y1)*stepx;
151 x2 = x2 + (ny2-y2)*stepx;
160 float xx = (float)(startx + posx);
161 add_pixel(i, xx ,posy);
167 #define PI 3.14159265358979
168 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
170 /* TODO: handle cap styles */
172 internal_t*i = (internal_t*)dev->internal;
185 /* Make sure the line is always at least one pixel wide */
187 /* That's what Macromedia's Player does at least at zoom level >= 1. */
190 /* That's what Macromedia's Player seems to do at zoom level 0. */
191 /* TODO: needs testing */
193 /* TODO: how does this interact with scaling? */
194 if(width * i->multiply < 1.0)
195 width = 1.0 / i->multiply;
198 sd = (double)dx*(double)dx+(double)dy*(double)dy;
220 add_line(dev, x1+vx, y1+vy, xx, yy);
223 for(t=1;t<segments;t++) {
224 double s = sin(t*PI/segments);
225 double c = cos(t*PI/segments);
226 xx = (x2 + vx*c - vy*s);
227 yy = (y2 + vx*s + vy*c);
228 add_line(dev, lastx, lasty, xx, yy);
235 add_line(dev, lastx, lasty, xx, yy);
240 add_line(dev, lastx, lasty, xx, yy);
243 for(t=1;t<segments;t++) {
244 double s = sin(t*PI/segments);
245 double c = cos(t*PI/segments);
246 xx = (x1 - vx*c + vy*s);
247 yy = (y1 - vx*s - vy*c);
248 add_line(dev, lastx, lasty, xx, yy);
252 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
255 static int compare_renderpoints(const void * _a, const void * _b)
257 renderpoint_t*a = (renderpoint_t*)_a;
258 renderpoint_t*b = (renderpoint_t*)_b;
259 if(a->x < b->x) return -1;
260 if(a->x > b->x) return 1;
264 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
268 U32 bit = 1<<(x1&31);
269 int bitpos = (x1/32);
272 int ainv = 255-col.a;
273 col.r = (col.r*col.a)>>8;
274 col.g = (col.g*col.a)>>8;
275 col.b = (col.b*col.a)>>8;
278 line[x].r = ((line[x].r*ainv)>>8)+col.r;
279 line[x].g = ((line[x].g*ainv)>>8)+col.g;
280 line[x].b = ((line[x].b*ainv)>>8)+col.b;
282 line[x].a = ((line[x].a*ainv)>>8)+col.a;
302 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
306 gfxmatrix_t*m = info->matrix;
307 gfximage_t*b = info->image;
309 if(!b->width || !b->height) {
310 gfxcolor_t red = {255,255,0,0};
311 fill_line_solid(line, z, y, x1, x2, red);
315 double det = m->m00*m->m11 - m->m01*m->m10;
316 if(fabs(det) < 0.0005) {
317 /* x direction equals y direction- the image is invisible */
321 double xx1 = ( (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
322 double yy1 = (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
323 double xinc1 = m->m11 * det;
324 double yinc1 = m->m01 * det;
326 U32 bit = 1<<(x1&31);
327 int bitpos = (x1/32);
332 int xx = (int)(xx1 + x * xinc1);
333 int yy = (int)(yy1 - x * yinc1);
338 if(xx>=b->width) xx = b->width-1;
340 if(yy>=b->height) yy = b->height-1;
344 if(xx<0) xx += b->width;
345 if(yy<0) yy += b->height;
348 col = b->data[yy*b->width+xx];
351 /* needs bitmap with premultiplied alpha */
352 line[x].r = ((line[x].r*ainv)>>8)+col.r;
353 line[x].g = ((line[x].g*ainv)>>8)+col.g;
354 line[x].b = ((line[x].b*ainv)>>8)+col.b;
364 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
368 U32 bit = 1<<(x1&31);
369 int bitpos = (x1/32);
380 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
382 if(fill->type == filltype_solid)
383 fill_line_solid(line, zline, y, startx, endx, *fill->color);
384 else if(fill->type == filltype_clip)
385 fill_line_clip(line, zline, y, startx, endx);
386 else if(fill->type == filltype_bitmap)
387 fill_line_bitmap(line, zline, y, startx, endx, fill);
391 void fill(gfxdevice_t*dev, fillinfo_t*fill)
393 internal_t*i = (internal_t*)dev->internal;
396 for(y=i->ymin;y<=i->ymax;y++) {
397 renderpoint_t*points = i->lines[y].points;
398 RGBA*line = &i->img[i->width2*y];
399 U32*zline = &i->clipbuf->data[i->bitwidth*y];
402 int num = i->lines[y].num;
404 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
407 renderpoint_t*p = &points[n];
408 renderpoint_t*next= n<num-1?&points[n+1]:0;
410 int endx = next?next->x:i->width2;
419 fill_line(dev, line, zline, y, startx, endx, fill);
422 if(endx == i->width2)
425 if(fill->type == filltype_clip) {
426 if(i->clipbuf->next) {
427 U32*line2 = &i->clipbuf->next->data[i->bitwidth*y];
429 for(x=0;x<i->bitwidth;x++)
430 zline[x] &= line2[x];
438 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
441 memset(&info, 0, sizeof(info));
442 info.type = filltype_solid;
447 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
449 internal_t*i = (internal_t*)dev->internal;
450 if(!strcmp(key, "antialize") || !strcmp(key, "antialise")) {
451 i->antialize = atoi(value);
452 i->zoom = i->antialize * i->multiply;
454 } else if(!strcmp(key, "multiply")) {
455 i->multiply = atoi(value);
456 i->zoom = i->antialize * i->multiply;
457 fprintf(stderr, "Warning: multiply not implemented yet\n");
459 } else if(!strcmp(key, "fillwhite")) {
460 i->fillwhite = atoi(value);
466 void newclip(struct _gfxdevice*dev)
468 internal_t*i = (internal_t*)dev->internal;
470 clipbuffer_t*c = (clipbuffer_t*)rfx_calloc(sizeof(clipbuffer_t));
471 c->data = (U32*)rfx_calloc(sizeof(U32) * i->bitwidth * i->height2);
472 c->next = i->clipbuf;
475 memcpy(c->data, c->next->data, i->bitwidth*i->height2);
477 memset(c->data, 0, sizeof(U32)*i->bitwidth*i->height2);
480 void endclip(struct _gfxdevice*dev, char removelast)
482 internal_t*i = (internal_t*)dev->internal;
484 /* test for at least one cliplevel (the one we created ourselves) */
485 if(!i->clipbuf || (!i->clipbuf->next && !removelast)) {
486 fprintf(stderr, "endclip without any active clip buffers\n");
490 clipbuffer_t*c = i->clipbuf;
491 i->clipbuf = i->clipbuf->next;
493 free(c->data);c->data = 0;
497 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)
499 internal_t*i = (internal_t*)dev->internal;
502 /*if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
503 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
507 if(line->type == gfx_moveTo) {
508 } else if(line->type == gfx_lineTo) {
509 double x1=x*i->zoom,y1=y*i->zoom;
510 double x3=line->x*i->zoom,y3=line->y*i->zoom;
511 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
512 fill_solid(dev, color);
513 } else if(line->type == gfx_splineTo) {
517 double x1=x*i->zoom,y1=y*i->zoom;
518 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
519 double x3=line->x*i->zoom,y3=line->y*i->zoom;
521 double c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
525 parts = (int)(sqrt(c)/3);
526 if(!parts) parts = 1;
528 for(t=1;t<=parts;t++) {
529 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
530 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
532 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
533 fill_solid(dev, color);
544 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
546 internal_t*i = (internal_t*)dev->internal;
551 int x1,y1,x2,y2,x3,y3;
553 if(line->type == gfx_moveTo) {
554 } else if(line->type == gfx_lineTo) {
555 double x1=x*i->zoom,y1=y*i->zoom;
556 double x3=line->x*i->zoom,y3=line->y*i->zoom;
558 add_line(dev, x1, y1, x3, y3);
559 } else if(line->type == gfx_splineTo) {
560 int c,t,parts,qparts;
563 double x1=x*i->zoom,y1=y*i->zoom;
564 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
565 double x3=line->x*i->zoom,y3=line->y*i->zoom;
567 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
571 parts = (int)(sqrt(c));
572 if(!parts) parts = 1;
574 for(t=1;t<=parts;t++) {
575 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
576 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
578 add_line(dev, xx, yy, nx, ny);
589 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
591 internal_t*i = (internal_t*)dev->internal;
593 memset(&info, 0, sizeof(info));
595 info.type = filltype_clip;
596 draw_line(dev, line);
600 void render_endclip(struct _gfxdevice*dev)
602 internal_t*i = (internal_t*)dev->internal;
606 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
608 internal_t*i = (internal_t*)dev->internal;
610 draw_line(dev, line);
611 fill_solid(dev, color);
614 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
616 internal_t*i = (internal_t*)dev->internal;
618 gfxcolor_t black = {255,0,0,0};
620 gfxmatrix_t m2 = *matrix;
622 draw_line(dev, line);
625 memset(&info, 0, sizeof(info));
626 info.type = filltype_bitmap;
629 info.cxform = cxform;
631 m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
632 m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
637 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
639 internal_t*i = (internal_t*)dev->internal;
641 gfxcolor_t black = {255,0,0,0};
643 draw_line(dev, line);
644 fill_solid(dev, &black);
647 void render_addfont(struct _gfxdevice*dev, gfxfont_t*font)
651 void render_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
653 internal_t*i = (internal_t*)dev->internal;
657 /* align characters to whole pixels */
658 matrix->tx = (int)(matrix->tx * i->antialize) / i->antialize;
659 matrix->ty = (int)(matrix->ty * i->antialize) / i->antialize;
661 gfxglyph_t*glyph = &font->glyphs[glyphnr];
662 gfxline_t*line2 = gfxline_clone(glyph->line);
663 gfxline_transform(line2, matrix);
664 draw_line(dev, line2);
665 fill_solid(dev, color);
671 void render_result_write(gfxresult_t*r, int filedesc)
673 internal_result_t*i= (internal_result_t*)r->internal;
675 int render_result_save(gfxresult_t*r, const char*filename)
677 internal_result_t*i= (internal_result_t*)r->internal;
679 return 0; // no pages drawn
683 char filenamebuf[256];
684 char*origname = strdup(filename);
685 int l = strlen(origname);
686 if(l>3 && strchr("gG",origname[l-1]) && strchr("nN",filename[l-2]) &&
687 strchr("pP",origname[l-3]) && filename[l-4]=='.') {
691 sprintf(filenamebuf, "%s.%d.png", origname, nr);
692 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
697 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
701 char*gfximage_asXPM(gfximage_t*img, int depth)
704 char*str = (char*)malloc(img->width*img->height*4 + 500 + 16*depth*depth*depth);
706 p+= sprintf(p, "static char *noname[] = {\n\"%d %d 262144 3\",\n");
710 for(b=0;b<depth;b++) {
711 p += sprintf(p, "\"%c%c%c c #%02x%02x%02x\",\n", r+32,g+32,b+32, r*d,g*d,b*d);
714 for(y=0;y<img->height;y++) {
716 gfxcolor_t*col = &img->data[y*img->height];
718 for(x=0;x<img->width;x++) {
719 p+=sprintf(p, "%c%c%c", 32+(col->r/d), 32+(col->g/d), 32+(col->b/d));
721 p+=sprintf(p, "\",\n");
726 void*render_result_get(gfxresult_t*r, const char*name)
728 internal_result_t*i= (internal_result_t*)r->internal;
729 if(!strncmp(name,"xpm",3)) {
730 int pagenr = atoi(&name[3]);
739 return gfximage_asXPM(&i->img, 64);
740 } else if(!strncmp(name,"page",4)) {
741 int pagenr = atoi(&name[4]);
754 void render_result_destroy(gfxresult_t*r)
756 internal_result_t*i= (internal_result_t*)r->internal;
759 internal_result_t*next = i->next;
760 free(i->img.data);i->img.data = 0;
763 the following rfx_free causes a segfault on WIN32 machines,
772 gfxresult_t* render_finish(struct _gfxdevice*dev)
774 internal_t*i = (internal_t*)dev->internal;
776 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
778 res->internal = i->results;i->results = 0;
779 res->write = render_result_write;
780 res->save = render_result_save;
781 res->get = render_result_get;
782 res->destroy = render_result_destroy;
784 free(dev->internal); dev->internal = 0; i = 0;
789 void render_startpage(struct _gfxdevice*dev, int width, int height)
791 internal_t*i = (internal_t*)dev->internal;
794 if(i->width2 || i->height2) {
795 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
799 i->width = width*i->multiply;
800 i->height = height*i->multiply;
801 i->width2 = width*i->zoom;
802 i->height2 = height*i->zoom;
803 i->bitwidth = (i->width2+31)/32;
805 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
806 for(y=0;y<i->height2;y++) {
807 memset(&i->lines[y], 0, sizeof(renderline_t));
808 i->lines[y].points = 0;
811 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
813 memset(i->img, 0xff, sizeof(RGBA)*i->width2*i->height2);
816 i->ymin = 0x7fffffff;
817 i->ymax = -0x80000000;
820 /* initialize initial clipping field, which doesn't clip anything yet */
822 memset(i->clipbuf->data, 255, sizeof(U32)*i->bitwidth*i->height2);
825 static void store_image(internal_t*i, internal_result_t*ir)
827 ir->img.data = (gfxcolor_t*)malloc(i->width*i->height*sizeof(gfxcolor_t));
828 ir->img.width = i->width;
829 ir->img.height = i->height;
831 gfxcolor_t*dest = ir->img.data;
833 if(i->antialize <= 1) /* no antializing */ {
835 for(y=0;y<i->height;y++) {
836 RGBA*line = &i->img[y*i->width];
837 memcpy(&dest[y*i->width], line, sizeof(RGBA)*i->width);
840 RGBA**lines = (RGBA**)rfx_calloc(sizeof(RGBA*)*i->antialize);
841 int q = i->antialize*i->antialize;
845 for(y=0;y<i->height2;y++) {
847 ypos = y % i->antialize;
848 lines[ypos] = &i->img[y*i->width2];
849 if(ypos == i->antialize-1) {
850 RGBA*out = &dest[(y2++)*i->width];
853 for(x=0;x<i->width;x++) {
854 int xpos = x*i->antialize;
857 for(yp=0;yp<i->antialize;yp++) {
858 RGBA*lp = &lines[yp][xpos];
860 for(xp=0;xp<i->antialize;xp++) {
879 void render_endpage(struct _gfxdevice*dev)
881 internal_t*i = (internal_t*)dev->internal;
883 if(!i->width2 || !i->height2) {
884 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
890 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
894 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
902 i->result_next->next = ir;
909 for(y=0;y<i->height2;y++) {
910 rfx_free(i->lines[y].points); i->lines[y].points = 0;
912 rfx_free(i->lines);i->lines=0;
914 if(i->img) {rfx_free(i->img);i->img = 0;}
920 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
922 /* not supported for this output device */
925 void gfxdevice_render_init(gfxdevice_t*dev)
927 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
928 memset(dev, 0, sizeof(gfxdevice_t));
930 dev->name = "render";
942 dev->setparameter = render_setparameter;
943 dev->startpage = render_startpage;
944 dev->startclip = render_startclip;
945 dev->endclip = render_endclip;
946 dev->stroke = render_stroke;
947 dev->fill = render_fill;
948 dev->fillbitmap = render_fillbitmap;
949 dev->fillgradient = render_fillgradient;
950 dev->addfont = render_addfont;
951 dev->drawchar = render_drawchar;
952 dev->drawlink = render_drawlink;
953 dev->endpage = render_endpage;
954 dev->finish = render_finish;