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"
30 typedef unsigned int U32;
31 typedef unsigned char U8;
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 {
74 internal_result_t*results;
75 internal_result_t*result_next;
78 typedef enum {filltype_solid,filltype_clip,filltype_bitmap} filltype_t;
80 typedef struct _fillinfo {
81 filltype_t type; //0=solid,1=clip
90 static inline void add_pixel(internal_t*i, float x, int y)
94 if(x >= i->width2 || y >= i->height2 || y<0) return;
96 if(y<i->ymin) i->ymin = y;
97 if(y>i->ymax) i->ymax = y;
99 renderline_t*l = &i->lines[y];
101 if(l->num == l->size) {
103 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
105 l->points[l->num] = p;
109 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
110 problem appears to often */
113 #define INT(x) ((int)((x)+16)-16)
115 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
117 internal_t*i = (internal_t*)dev->internal;
119 double ny1, ny2, stepx;
121 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
122 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
139 ny1 = INT(y1) + 1.0 + CUT;
142 ny2 = INT(y2) - 1.0 + CUT;
149 x1 = x1 + (ny1-y1)*stepx;
150 x2 = x2 + (ny2-y2)*stepx;
159 float xx = (float)(startx + posx);
160 add_pixel(i, xx ,posy);
166 #define PI 3.14159265358979
167 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
169 /* TODO: handle cap styles */
171 internal_t*i = (internal_t*)dev->internal;
184 /* Make sure the line is always at least one pixel wide */
186 /* That's what Macromedia's Player does at least at zoom level >= 1. */
189 /* That's what Macromedia's Player seems to do at zoom level 0. */
190 /* TODO: needs testing */
192 /* TODO: how does this interact with scaling? */
193 if(width * i->multiply < 1.0)
194 width = 1.0 / i->multiply;
197 sd = (double)dx*(double)dx+(double)dy*(double)dy;
219 add_line(dev, x1+vx, y1+vy, xx, yy);
222 for(t=1;t<segments;t++) {
223 double s = sin(t*PI/segments);
224 double c = cos(t*PI/segments);
225 xx = (x2 + vx*c - vy*s);
226 yy = (y2 + vx*s + vy*c);
227 add_line(dev, lastx, lasty, xx, yy);
234 add_line(dev, lastx, lasty, xx, yy);
239 add_line(dev, lastx, lasty, xx, yy);
242 for(t=1;t<segments;t++) {
243 double s = sin(t*PI/segments);
244 double c = cos(t*PI/segments);
245 xx = (x1 - vx*c + vy*s);
246 yy = (y1 - vx*s - vy*c);
247 add_line(dev, lastx, lasty, xx, yy);
251 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
254 static int compare_renderpoints(const void * _a, const void * _b)
256 renderpoint_t*a = (renderpoint_t*)_a;
257 renderpoint_t*b = (renderpoint_t*)_b;
258 if(a->x < b->x) return -1;
259 if(a->x > b->x) return 1;
263 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
267 U32 bit = 1<<(x1&31);
268 int bitpos = (x1/32);
271 int ainv = 255-col.a;
272 col.r = (col.r*col.a)>>8;
273 col.g = (col.g*col.a)>>8;
274 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;
301 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
305 gfxmatrix_t*m = info->matrix;
306 gfximage_t*b = info->image;
308 if(!b->width || !b->height) {
309 gfxcolor_t red = {255,255,0,0};
310 fill_line_solid(line, z, y, x1, x2, red);
314 double det = m->m00*m->m11 - m->m01*m->m10;
315 if(fabs(det) < 0.0005) {
316 /* x direction equals y direction- the image is invisible */
320 double xx1 = ( (-m->tx) * m->m11 - (y - m->ty) * m->m10) * det;
321 double yy1 = (- (-m->tx) * m->m01 + (y - m->ty) * m->m00) * det;
322 double xinc1 = m->m11 * det;
323 double yinc1 = m->m01 * det;
325 U32 bit = 1<<(x1&31);
326 int bitpos = (x1/32);
331 int xx = (int)(xx1 + x * xinc1);
332 int yy = (int)(yy1 - x * yinc1);
337 if(xx>=b->width) xx = b->width-1;
339 if(yy>=b->height) yy = b->height-1;
343 if(xx<0) xx += b->width;
344 if(yy<0) yy += b->height;
347 col = b->data[yy*b->width+xx];
350 /* needs bitmap with premultiplied alpha */
351 line[x].r = ((line[x].r*ainv)>>8)+col.r;
352 line[x].g = ((line[x].g*ainv)>>8)+col.g;
353 line[x].b = ((line[x].b*ainv)>>8)+col.b;
363 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
367 U32 bit = 1<<(x1&31);
368 int bitpos = (x1/32);
379 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
381 if(fill->type == filltype_solid)
382 fill_line_solid(line, zline, y, startx, endx, *fill->color);
383 else if(fill->type == filltype_clip)
384 fill_line_clip(line, zline, y, startx, endx);
385 else if(fill->type == filltype_bitmap)
386 fill_line_bitmap(line, zline, y, startx, endx, fill);
390 void fill(gfxdevice_t*dev, fillinfo_t*fill)
392 internal_t*i = (internal_t*)dev->internal;
395 for(y=i->ymin;y<=i->ymax;y++) {
396 renderpoint_t*points = i->lines[y].points;
397 RGBA*line = &i->img[i->width2*y];
398 U32*zline = &i->clipbuf->data[i->bitwidth*y];
401 int num = i->lines[y].num;
403 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
406 renderpoint_t*p = &points[n];
407 renderpoint_t*next= n<num-1?&points[n+1]:0;
409 int endx = next?next->x:i->width2;
418 fill_line(dev, line, zline, y, startx, endx, fill);
421 if(endx == i->width2)
424 if(fill->type == filltype_clip) {
425 if(i->clipbuf->next) {
426 U32*line2 = &i->clipbuf->next->data[i->bitwidth*y];
428 for(x=0;x<i->bitwidth;x++)
429 zline[x] &= line2[x];
437 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
440 info.type = filltype_solid;
445 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
447 internal_t*i = (internal_t*)dev->internal;
448 if(!strcmp(key, "antialize")) {
449 i->antialize = atoi(value);
450 i->zoom = i->antialize * i->multiply;
451 } else if(!strcmp(key, "multiply")) {
452 i->multiply = atoi(value);
453 i->zoom = i->antialize * i->multiply;
454 fprintf(stderr, "Warning: multiply not implemented yet\n");
459 void newclip(struct _gfxdevice*dev)
461 internal_t*i = (internal_t*)dev->internal;
463 clipbuffer_t*c = rfx_calloc(sizeof(clipbuffer_t));
464 c->data = rfx_calloc(sizeof(U32) * i->bitwidth * i->height2);
465 c->next = i->clipbuf;
468 memcpy(c->data, c->next->data, i->bitwidth*i->height2);
470 memset(c->data, 0, sizeof(U32)*i->bitwidth*i->height2);
473 void endclip(struct _gfxdevice*dev)
475 internal_t*i = (internal_t*)dev->internal;
478 fprintf(stderr, "endclip without any active clip buffers");
482 clipbuffer_t*c = i->clipbuf;
483 i->clipbuf = i->clipbuf->next;
485 free(c->data);c->data = 0;
489 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)
491 internal_t*i = (internal_t*)dev->internal;
494 /*if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
495 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
499 int x1,y1,x2,y2,x3,y3;
501 if(line->type == gfx_moveTo) {
502 } else if(line->type == gfx_lineTo) {
503 double x1=x*i->zoom,y1=y*i->zoom;
504 double x3=line->x*i->zoom,y3=line->y*i->zoom;
505 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
506 fill_solid(dev, color);
507 } else if(line->type == gfx_splineTo) {
508 int c,t,parts,qparts;
511 double x1=x*i->zoom,y1=y*i->zoom;
512 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
513 double x3=line->x*i->zoom,y3=line->y*i->zoom;
515 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
519 parts = (int)(sqrt(c)/3);
520 if(!parts) parts = 1;
522 for(t=1;t<=parts;t++) {
523 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
524 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
526 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
527 fill_solid(dev, color);
538 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
540 internal_t*i = (internal_t*)dev->internal;
545 int x1,y1,x2,y2,x3,y3;
547 if(line->type == gfx_moveTo) {
548 } else if(line->type == gfx_lineTo) {
549 double x1=x*i->zoom,y1=y*i->zoom;
550 double x3=line->x*i->zoom,y3=line->y*i->zoom;
552 add_line(dev, x1, y1, x3, y3);
553 } else if(line->type == gfx_splineTo) {
554 int c,t,parts,qparts;
557 double x1=x*i->zoom,y1=y*i->zoom;
558 double x2=line->sx*i->zoom,y2=line->sy*i->zoom;
559 double x3=line->x*i->zoom,y3=line->y*i->zoom;
561 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
565 parts = (int)(sqrt(c)/3);
566 if(!parts) parts = 1;
568 for(t=1;t<=parts;t++) {
569 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
570 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
572 add_line(dev, xx, yy, nx, ny);
583 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
585 internal_t*i = (internal_t*)dev->internal;
588 info.type = filltype_clip;
589 draw_line(dev, line);
593 void render_endclip(struct _gfxdevice*dev)
595 internal_t*i = (internal_t*)dev->internal;
599 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
601 internal_t*i = (internal_t*)dev->internal;
603 draw_line(dev, line);
604 fill_solid(dev, color);
607 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
609 internal_t*i = (internal_t*)dev->internal;
611 gfxcolor_t black = {255,0,0,0};
613 gfxmatrix_t m2 = *matrix;
615 draw_line(dev, line);
618 info.type = filltype_bitmap;
621 info.cxform = cxform;
623 m2.m00 *= i->zoom; m2.m01 *= i->zoom; m2.tx *= i->zoom;
624 m2.m10 *= i->zoom; m2.m11 *= i->zoom; m2.ty *= i->zoom;
629 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
631 internal_t*i = (internal_t*)dev->internal;
633 gfxcolor_t black = {255,0,0,0};
635 draw_line(dev, line);
636 fill_solid(dev, &black);
639 void render_addfont(struct _gfxdevice*dev, gfxfont_t*font)
643 void render_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
645 internal_t*i = (internal_t*)dev->internal;
647 gfxglyph_t*glyph = &font->glyphs[glyphnr];
648 gfxline_t*line2 = gfxline_clone(glyph->line);
649 gfxline_transform(line2, matrix);
650 draw_line(dev, line2);
651 fill_solid(dev, color);
657 void render_result_write(gfxresult_t*r, int filedesc)
659 internal_result_t*i= (internal_result_t*)r->internal;
661 int render_result_save(gfxresult_t*r, char*filename)
663 /*internal_result_t*i= (internal_result_t*)r->internal;
667 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
671 writePNG(filename, (unsigned char*)i->img.data, i->img.width, i->img.height);
675 void*render_result_get(gfxresult_t*r, char*name)
677 internal_result_t*i= (internal_result_t*)r->internal;
678 if(!strncmp(name,"page",4)) {
679 int pagenr = atoi(&name[4]);
691 void render_result_destroy(gfxresult_t*r)
693 internal_result_t*i= (internal_result_t*)r->internal;
696 internal_result_t*next = i->next;
697 free(i->img.data);i->img.data = 0;
704 gfxresult_t* render_finish(struct _gfxdevice*dev)
706 internal_t*i = (internal_t*)dev->internal;
708 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
710 res->internal = i->results;i->results = 0;
711 res->write = render_result_write;
712 res->save = render_result_save;
713 res->get = render_result_get;
714 res->destroy = render_result_destroy;
716 free(dev->internal); dev->internal = 0; i = 0;
721 void render_startpage(struct _gfxdevice*dev, int width, int height)
723 internal_t*i = (internal_t*)dev->internal;
726 if(i->width2 || i->height2) {
727 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
731 i->width = width*i->multiply;
732 i->height = height*i->multiply;
733 i->width2 = width*i->zoom;
734 i->height2 = height*i->zoom;
735 i->bitwidth = (i->width2+31)/32;
737 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
738 for(y=0;y<i->height2;y++) {
739 memset(&i->lines[y], 0, sizeof(renderline_t));
740 i->lines[y].points = 0;
743 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
744 i->ymin = 0x7fffffff;
745 i->ymax = -0x80000000;
748 /* initialize initial clipping field, which doesn't clip anything yet */
750 memset(i->clipbuf->data, 255, sizeof(U32)*i->bitwidth*i->height2);
753 static void store_image(internal_t*i, internal_result_t*ir)
755 ir->img.data = malloc(i->width*i->height*sizeof(RGBA));
756 ir->img.width = i->width;
757 ir->img.height = i->height;
759 gfxcolor_t*dest = ir->img.data;
761 if(i->antialize <= 1) /* no antializing */ {
763 for(y=0;y<i->height;y++) {
764 RGBA*line = &i->img[y*i->width];
765 memcpy(&dest[y*i->width], line, sizeof(RGBA)*i->width);
768 RGBA**lines = (RGBA**)rfx_calloc(sizeof(RGBA*)*i->antialize);
769 int q = i->antialize*i->antialize;
773 for(y=0;y<i->height2;y++) {
775 ypos = y % i->antialize;
776 lines[ypos] = &i->img[y*i->width2];
777 if(ypos == i->antialize-1) {
778 RGBA*out = &dest[(y2++)*i->width];
781 for(x=0;x<i->width;x++) {
782 int xpos = x*i->antialize;
785 for(yp=0;yp<i->antialize;yp++) {
786 RGBA*lp = &lines[yp][xpos];
788 for(xp=0;xp<i->antialize;xp++) {
807 void render_endpage(struct _gfxdevice*dev)
809 internal_t*i = (internal_t*)dev->internal;
811 if(!i->width2 || !i->height2) {
812 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
818 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
822 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
830 i->result_next->next = ir;
837 for(y=0;y<i->height2;y++) {
838 rfx_free(i->lines[y].points); i->lines[y].points = 0;
840 rfx_free(i->lines);i->lines=0;
842 if(i->img) {rfx_free(i->img);i->img = 0;}
848 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
850 /* not supported for this output device */
853 void gfxdevice_render_init(gfxdevice_t*dev)
855 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
857 memset(dev, 0, sizeof(gfxdevice_t));
859 dev->name = "render";
871 dev->setparameter = render_setparameter;
872 dev->startpage = render_startpage;
873 dev->startclip = render_startclip;
874 dev->endclip = render_endclip;
875 dev->stroke = render_stroke;
876 dev->fill = render_fill;
877 dev->fillbitmap = render_fillbitmap;
878 dev->fillgradient = render_fillgradient;
879 dev->addfont = render_addfont;
880 dev->drawchar = render_drawchar;
881 dev->drawlink = render_drawlink;
882 dev->endpage = render_endpage;
883 dev->finish = render_finish;