3 Part of the swftools package.
5 Copyright (c) 2005 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 */
24 #include "../gfxdevice.h"
25 #include "../gfxtools.h"
29 typedef unsigned int U32;
30 typedef unsigned char U8;
32 typedef gfxcolor_t RGBA;
34 typedef struct _renderpoint
39 typedef struct _renderline
46 typedef struct _internal_result {
50 struct _internal_result*next;
53 typedef struct _clipbuffer {
56 struct _clipbuffer*prev;
59 typedef struct _fontlist
63 struct _fontlist*next;
66 typedef struct _internal {
85 clipbuffer_t*clipbufs;
90 internal_result_t*results;
91 internal_result_t*result_next;
94 typedef enum {filltype_solid,filltype_clip,filltype_bitmap} filltype_t;
96 typedef struct _fillinfo {
97 filltype_t type; //0=solid,1=clip
106 static inline void add_pixel(internal_t*i, float x, int y)
110 if(x >= i->width2 || y >= i->height2 || y<0) return;
112 if(y<i->ymin) i->ymin = y;
113 if(y>i->ymax) i->ymax = y;
115 renderline_t*l = &i->lines[y];
117 if(l->num == l->size) {
119 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
121 l->points[l->num] = p;
125 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
126 problem appears to often */
129 #define INT(x) ((int)((x)+16)-16)
131 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
133 internal_t*i = (internal_t*)dev->internal;
135 double ny1, ny2, stepx;
137 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
138 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
155 ny1 = INT(y1) + 1.0 + CUT;
158 ny2 = INT(y2) - 1.0 + CUT;
165 x1 = x1 + (ny1-y1)*stepx;
166 x2 = x2 + (ny2-y2)*stepx;
175 float xx = (float)(startx + posx);
176 add_pixel(i, xx ,posy);
182 #define PI 3.14159265358979
183 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
185 internal_t*i = (internal_t*)dev->internal;
198 /* Make sure the line is always at least one pixel wide */
200 /* That's what Macromedia's Player does at least at zoom level >= 1. */
203 /* That's what Macromedia's Player seems to do at zoom level 0. */
204 /* TODO: needs testing */
206 /* TODO: how does this interact with scaling? */
207 if(width * i->multiply < 1.0)
208 width = 1.0 / i->multiply;
211 sd = (double)dx*(double)dx+(double)dy*(double)dy;
233 add_line(dev, x1+vx, y1+vy, xx, yy);
236 for(t=1;t<segments;t++) {
237 double s = sin(t*PI/segments);
238 double c = cos(t*PI/segments);
239 xx = (x2 + vx*c - vy*s);
240 yy = (y2 + vx*s + vy*c);
241 add_line(dev, lastx, lasty, xx, yy);
248 add_line(dev, lastx, lasty, xx, yy);
253 add_line(dev, lastx, lasty, xx, yy);
256 for(t=1;t<segments;t++) {
257 double s = sin(t*PI/segments);
258 double c = cos(t*PI/segments);
259 xx = (x1 - vx*c + vy*s);
260 yy = (y1 - vx*s - vy*c);
261 add_line(dev, lastx, lasty, xx, yy);
265 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
268 static int compare_renderpoints(const void * _a, const void * _b)
270 renderpoint_t*a = (renderpoint_t*)_a;
271 renderpoint_t*b = (renderpoint_t*)_b;
272 if(a->x < b->x) return -1;
273 if(a->x > b->x) return 1;
277 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
281 U32 bit = 1<<(x1&31);
282 int bitpos = (x1/32);
285 int ainv = 255-col.a;
286 col.r = (col.r*col.a)>>8;
287 col.g = (col.g*col.a)>>8;
288 col.b = (col.b*col.a)>>8;
292 line[x].r = ((line[x].r*ainv)>>8)+col.r;
293 line[x].g = ((line[x].g*ainv)>>8)+col.g;
294 line[x].b = ((line[x].b*ainv)>>8)+col.b;
315 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
319 gfxmatrix_t*m = info->matrix;
320 gfximage_t*b = info->image;
322 double det = m->m00*m->m11 - m->m01*m->m10;
323 if(fabs(det) < 0.0005) {
324 /* x direction equals y direction- the image is invisible */
329 if(!b->width || !b->height) {
330 gfxcolor_t red = {255,255,0,0};
331 fill_line_solid(line, z, y, x1, x2, red);
335 U32 bit = 1<<(x1&31);
336 int bitpos = (x1/32);
341 int xx = (int)(( (x - m->tx) * m->m11 - (y - m->ty) * m->m10)*det);
342 int yy = (int)((- (x - m->tx) * m->m01 + (y - m->ty) * m->m00)*det);
347 if(xx>=b->width) xx = b->width-1;
349 if(yy>=b->height) yy = b->height-1;
353 if(xx<0) xx += b->width;
354 if(yy<0) yy += b->height;
357 col = b->data[yy*b->width+xx];
360 line[x].r = ((line[x].r*ainv)>>8)+col.r;
361 line[x].g = ((line[x].g*ainv)>>8)+col.g;
362 line[x].b = ((line[x].b*ainv)>>8)+col.b;
372 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
376 U32 bit = 1<<(x1&31);
377 int bitpos = (x1/32);
388 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
390 if(fill->type == filltype_solid)
391 fill_line_solid(line, zline, y, startx, endx, *fill->color);
392 else if(fill->type == filltype_clip)
393 fill_line_clip(line, zline, y, startx, endx);
394 else if(fill->type == filltype_bitmap)
395 fill_line_bitmap(line, zline, y, startx, endx, fill);
399 void fill(gfxdevice_t*dev, fillinfo_t*fill)
401 internal_t*i = (internal_t*)dev->internal;
404 for(y=i->ymin;y<=i->ymax;y++) {
405 renderpoint_t*points = i->lines[y].points;
406 RGBA*line = &i->img[i->width2*y];
407 int*zline = &i->zbuf[i->width2*y];
409 int num = i->lines[y].num;
411 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
414 renderpoint_t*p = &points[n];
415 renderpoint_t*next= n<num-1?&points[n+1]:0;
417 int endx = next?next->x:i->width2;
426 fill_line(dev, line, zline, y, startx, endx, fill);
429 if(endx == i->width2)
436 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
439 info.type = filltype_solid;
444 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
446 internal_t*i = (internal_t*)dev->internal;
447 if(!strcmp(key, "antialize")) {
448 i->antialize = atoi(value);
449 } else if(!strcmp(key, "multiply")) {
450 i->multiply = atoi(value);
455 void newclip(struct _gfxdevice*dev)
457 internal_t*i = (internal_t*)dev->internal;
459 clipbuffer_t*c = rfx_calloc(sizeof(clipbuffer_t));
460 c->linesize = ((i->width2+31) / 32);
461 c->data = rfx_calloc(c->linesize * i->height2);
464 i->clipbufs = i->clipbuf = c;
466 clipbuffer_t*old = i->clipbuf;
468 i->clipbuf->prev = old;
472 void endclip(struct _gfxdevice*dev)
474 internal_t*i = (internal_t*)dev->internal;
477 fprintf(stderr, "endclip without any active clip buffers");
481 clipbuffer_t*old = i->clipbuf;
483 if(i->clipbuf == i->clipbufs)
486 i->clipbuf = i->clipbuf->prev;
489 free(old->data);old->data = 0;
493 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)
495 internal_t*i = (internal_t*)dev->internal;
498 if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
499 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
503 int x1,y1,x2,y2,x3,y3;
505 if(line->type == gfx_moveTo) {
506 } else if(line->type == gfx_lineTo) {
508 double x3=line->x,y3=line->y;
509 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
510 fill_solid(dev, color);
511 } else if(line->type == gfx_splineTo) {
512 int c,t,parts,qparts;
516 double x2=line->sx,y2=line->sy;
517 double x3=line->x,y3=line->y;
519 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
523 parts = (int)(sqrt(c)/3);
524 if(!parts) parts = 1;
526 for(t=1;t<=parts;t++) {
527 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
528 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
530 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
531 fill_solid(dev, color);
542 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
544 internal_t*i = (internal_t*)dev->internal;
549 int x1,y1,x2,y2,x3,y3;
551 if(line->type == gfx_moveTo) {
552 } else if(line->type == gfx_lineTo) {
554 double x3=line->x,y3=line->y;
556 add_line(dev, x1, y1, x3, y3);
557 } else if(line->type == gfx_splineTo) {
558 int c,t,parts,qparts;
562 double x2=line->sx,y2=line->sy;
563 double x3=line->x,y3=line->y;
565 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
569 parts = (int)(sqrt(c)/3);
570 if(!parts) parts = 1;
572 for(t=1;t<=parts;t++) {
573 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
574 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
576 add_line(dev, xx, yy, nx, ny);
587 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
589 internal_t*i = (internal_t*)dev->internal;
592 info.type = filltype_clip;
593 draw_line(dev, line);
597 void render_endclip(struct _gfxdevice*dev)
599 internal_t*i = (internal_t*)dev->internal;
603 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
605 internal_t*i = (internal_t*)dev->internal;
607 draw_line(dev, line);
608 fill_solid(dev, color);
611 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
613 internal_t*i = (internal_t*)dev->internal;
615 gfxcolor_t black = {255,0,0,0};
617 draw_line(dev, line);
620 info.type = filltype_bitmap;
622 info.matrix = matrix;
623 info.cxform = cxform;
627 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
629 internal_t*i = (internal_t*)dev->internal;
631 gfxcolor_t black = {255,0,0,0};
633 draw_line(dev, line);
634 fill_solid(dev, &black);
637 void render_addfont(struct _gfxdevice*dev, char*fontid, gfxfont_t*font)
639 internal_t*i = (internal_t*)dev->internal;
641 fontlist_t*last=0,*l = i->fontlist;
644 if(!strcmp((char*)l->id, fontid)) {
645 return; // we already know this font
649 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
659 void render_drawchar(struct _gfxdevice*dev, char*fontid, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
661 internal_t*i = (internal_t*)dev->internal;
663 if(i->font && i->fontid && !strcmp(fontid, i->fontid)) {
664 // current font is correct
666 fontlist_t*l = i->fontlist;
670 if(!strcmp((char*)l->id, i->fontid)) {
678 fprintf(stderr, "Unknown font id: %s", fontid);
683 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
685 gfxline_t*line2 = gfxline_clone(glyph->line);
686 gfxline_transform(line2, matrix);
687 draw_line(dev, line2);
688 fill_solid(dev, color);
694 void render_result_write(gfxresult_t*r, int filedesc)
696 internal_result_t*i= (internal_result_t*)r->internal;
698 int render_result_save(gfxresult_t*r, char*filename)
700 internal_result_t*i= (internal_result_t*)r->internal;
704 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
708 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
711 void*render_result_get(gfxresult_t*r, char*name)
713 internal_result_t*i= (internal_result_t*)r->internal;
716 void render_result_destroy(gfxresult_t*r)
718 internal_result_t*i= (internal_result_t*)r->internal;
719 free(i); r->internal = 0;
723 gfxresult_t* render_finish(struct _gfxdevice*dev)
725 internal_t*i = (internal_t*)dev->internal;
727 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
729 res->internal = i->results;i->results = 0;
730 res->write = render_result_write;
731 res->save = render_result_save;
732 res->get = render_result_get;
733 res->destroy = render_result_destroy;
735 free(dev->internal); dev->internal = 0; i = 0;
739 void render_startpage(struct _gfxdevice*dev, int width, int height)
741 internal_t*i = (internal_t*)dev->internal;
744 if(i->width2 || i->height2) {
745 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
751 i->width2 = width*i->antialize*i->multiply;
752 i->height2 = height*i->antialize*i->multiply;
754 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
755 for(y=0;y<i->height2;y++) {
756 memset(&i->lines[y], 0, sizeof(renderline_t));
757 i->lines[y].points = 0;
760 i->zbuf = (int*)rfx_calloc(sizeof(int)*i->width2*i->height2);
761 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
762 i->ymin = 0x7fffffff;
763 i->ymax = -0x80000000;
768 void render_endpage(struct _gfxdevice*dev)
770 internal_t*i = (internal_t*)dev->internal;
772 if(!i->width2 || !i->height2) {
773 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
779 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
783 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
784 ir->width = i->width;
785 ir->height = i->height;
786 ir->img = i->img; i->img = 0;
789 i->result_next->next = ir;
796 rfx_free(i->lines);i->lines=0; //FIXME
797 rfx_free(i->zbuf);i->zbuf = 0;
798 if(i->img) {rfx_free(i->img);i->img = 0;}
804 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
806 /* not supported for this output device */
809 void gfxdevice_render_init(gfxdevice_t*dev)
811 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
813 memset(dev, 0, sizeof(gfxdevice_t));
823 dev->setparameter = render_setparameter;
824 dev->startpage = render_startpage;
825 dev->startclip = render_startclip;
826 dev->endclip = render_endclip;
827 dev->stroke = render_stroke;
828 dev->fill = render_fill;
829 dev->fillbitmap = render_fillbitmap;
830 dev->fillgradient = render_fillgradient;
831 dev->addfont = render_addfont;
832 dev->drawchar = render_drawchar;
833 dev->drawlink = render_drawlink;
834 dev->endpage = render_endpage;
835 dev->finish = render_finish;