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 */
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 {
51 struct _internal_result*next;
54 typedef struct _clipbuffer {
57 struct _clipbuffer*prev;
60 typedef struct _fontlist
64 struct _fontlist*next;
67 typedef struct _internal {
86 clipbuffer_t*clipbufs;
91 internal_result_t*results;
92 internal_result_t*result_next;
95 typedef enum {filltype_solid,filltype_clip,filltype_bitmap} filltype_t;
97 typedef struct _fillinfo {
98 filltype_t type; //0=solid,1=clip
107 static inline void add_pixel(internal_t*i, float x, int y)
111 if(x >= i->width2 || y >= i->height2 || y<0) return;
113 if(y<i->ymin) i->ymin = y;
114 if(y>i->ymax) i->ymax = y;
116 renderline_t*l = &i->lines[y];
118 if(l->num == l->size) {
120 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
122 l->points[l->num] = p;
126 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
127 problem appears to often */
130 #define INT(x) ((int)((x)+16)-16)
132 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
134 internal_t*i = (internal_t*)dev->internal;
136 double ny1, ny2, stepx;
138 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
139 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
156 ny1 = INT(y1) + 1.0 + CUT;
159 ny2 = INT(y2) - 1.0 + CUT;
166 x1 = x1 + (ny1-y1)*stepx;
167 x2 = x2 + (ny2-y2)*stepx;
176 float xx = (float)(startx + posx);
177 add_pixel(i, xx ,posy);
183 #define PI 3.14159265358979
184 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
186 internal_t*i = (internal_t*)dev->internal;
199 /* Make sure the line is always at least one pixel wide */
201 /* That's what Macromedia's Player does at least at zoom level >= 1. */
204 /* That's what Macromedia's Player seems to do at zoom level 0. */
205 /* TODO: needs testing */
207 /* TODO: how does this interact with scaling? */
208 if(width * i->multiply < 1.0)
209 width = 1.0 / i->multiply;
212 sd = (double)dx*(double)dx+(double)dy*(double)dy;
234 add_line(dev, x1+vx, y1+vy, xx, yy);
237 for(t=1;t<segments;t++) {
238 double s = sin(t*PI/segments);
239 double c = cos(t*PI/segments);
240 xx = (x2 + vx*c - vy*s);
241 yy = (y2 + vx*s + vy*c);
242 add_line(dev, lastx, lasty, xx, yy);
249 add_line(dev, lastx, lasty, xx, yy);
254 add_line(dev, lastx, lasty, xx, yy);
257 for(t=1;t<segments;t++) {
258 double s = sin(t*PI/segments);
259 double c = cos(t*PI/segments);
260 xx = (x1 - vx*c + vy*s);
261 yy = (y1 - vx*s - vy*c);
262 add_line(dev, lastx, lasty, xx, yy);
266 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
269 static int compare_renderpoints(const void * _a, const void * _b)
271 renderpoint_t*a = (renderpoint_t*)_a;
272 renderpoint_t*b = (renderpoint_t*)_b;
273 if(a->x < b->x) return -1;
274 if(a->x > b->x) return 1;
278 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
282 U32 bit = 1<<(x1&31);
283 int bitpos = (x1/32);
286 int ainv = 255-col.a;
287 col.r = (col.r*col.a)>>8;
288 col.g = (col.g*col.a)>>8;
289 col.b = (col.b*col.a)>>8;
293 line[x].r = ((line[x].r*ainv)>>8)+col.r;
294 line[x].g = ((line[x].g*ainv)>>8)+col.g;
295 line[x].b = ((line[x].b*ainv)>>8)+col.b;
316 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
320 gfxmatrix_t*m = info->matrix;
321 gfximage_t*b = info->image;
323 double det = m->m00*m->m11 - m->m01*m->m10;
324 if(fabs(det) < 0.0005) {
325 /* x direction equals y direction- the image is invisible */
330 if(!b->width || !b->height) {
331 gfxcolor_t red = {255,255,0,0};
332 fill_line_solid(line, z, y, x1, x2, red);
336 U32 bit = 1<<(x1&31);
337 int bitpos = (x1/32);
342 int xx = (int)(( (x - m->tx) * m->m11 - (y - m->ty) * m->m10)*det);
343 int yy = (int)((- (x - m->tx) * m->m01 + (y - m->ty) * m->m00)*det);
348 if(xx>=b->width) xx = b->width-1;
350 if(yy>=b->height) yy = b->height-1;
354 if(xx<0) xx += b->width;
355 if(yy<0) yy += b->height;
358 col = b->data[yy*b->width+xx];
361 line[x].r = ((line[x].r*ainv)>>8)+col.r;
362 line[x].g = ((line[x].g*ainv)>>8)+col.g;
363 line[x].b = ((line[x].b*ainv)>>8)+col.b;
373 static void fill_line_clip(RGBA*line, U32*z, int y, int x1, int x2)
377 U32 bit = 1<<(x1&31);
378 int bitpos = (x1/32);
389 void fill_line(gfxdevice_t*dev, RGBA*line, U32*zline, int y, int startx, int endx, fillinfo_t*fill)
391 if(fill->type == filltype_solid)
392 fill_line_solid(line, zline, y, startx, endx, *fill->color);
393 else if(fill->type == filltype_clip)
394 fill_line_clip(line, zline, y, startx, endx);
395 else if(fill->type == filltype_bitmap)
396 fill_line_bitmap(line, zline, y, startx, endx, fill);
400 void fill(gfxdevice_t*dev, fillinfo_t*fill)
402 internal_t*i = (internal_t*)dev->internal;
405 for(y=i->ymin;y<=i->ymax;y++) {
406 renderpoint_t*points = i->lines[y].points;
407 RGBA*line = &i->img[i->width2*y];
408 int*zline = &i->zbuf[i->width2*y];
410 int num = i->lines[y].num;
412 qsort(points, num, sizeof(renderpoint_t), compare_renderpoints);
415 renderpoint_t*p = &points[n];
416 renderpoint_t*next= n<num-1?&points[n+1]:0;
418 int endx = next?next->x:i->width2;
427 fill_line(dev, line, zline, y, startx, endx, fill);
430 if(endx == i->width2)
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 } else if(!strcmp(key, "multiply")) {
451 i->multiply = atoi(value);
456 void newclip(struct _gfxdevice*dev)
458 internal_t*i = (internal_t*)dev->internal;
460 clipbuffer_t*c = rfx_calloc(sizeof(clipbuffer_t));
461 c->linesize = ((i->width2+31) / 32);
462 c->data = rfx_calloc(c->linesize * i->height2);
465 i->clipbufs = i->clipbuf = c;
467 clipbuffer_t*old = i->clipbuf;
469 i->clipbuf->prev = old;
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*old = i->clipbuf;
484 if(i->clipbuf == i->clipbufs)
487 i->clipbuf = i->clipbuf->prev;
490 free(old->data);old->data = 0;
494 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)
496 internal_t*i = (internal_t*)dev->internal;
499 if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
500 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
504 int x1,y1,x2,y2,x3,y3;
506 if(line->type == gfx_moveTo) {
507 } else if(line->type == gfx_lineTo) {
509 double x3=line->x,y3=line->y;
510 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
511 fill_solid(dev, color);
512 } else if(line->type == gfx_splineTo) {
513 int c,t,parts,qparts;
517 double x2=line->sx,y2=line->sy;
518 double x3=line->x,y3=line->y;
520 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
524 parts = (int)(sqrt(c)/3);
525 if(!parts) parts = 1;
527 for(t=1;t<=parts;t++) {
528 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
529 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
531 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
532 fill_solid(dev, color);
543 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
545 internal_t*i = (internal_t*)dev->internal;
550 int x1,y1,x2,y2,x3,y3;
552 if(line->type == gfx_moveTo) {
553 } else if(line->type == gfx_lineTo) {
555 double x3=line->x,y3=line->y;
557 add_line(dev, x1, y1, x3, y3);
558 } else if(line->type == gfx_splineTo) {
559 int c,t,parts,qparts;
563 double x2=line->sx,y2=line->sy;
564 double x3=line->x,y3=line->y;
566 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
570 parts = (int)(sqrt(c)/3);
571 if(!parts) parts = 1;
573 for(t=1;t<=parts;t++) {
574 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
575 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
577 add_line(dev, xx, yy, nx, ny);
588 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
590 internal_t*i = (internal_t*)dev->internal;
593 info.type = filltype_clip;
594 draw_line(dev, line);
598 void render_endclip(struct _gfxdevice*dev)
600 internal_t*i = (internal_t*)dev->internal;
604 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
606 internal_t*i = (internal_t*)dev->internal;
608 draw_line(dev, line);
609 fill_solid(dev, color);
612 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
614 internal_t*i = (internal_t*)dev->internal;
616 gfxcolor_t black = {255,0,0,0};
618 draw_line(dev, line);
621 info.type = filltype_bitmap;
623 info.matrix = matrix;
624 info.cxform = cxform;
628 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
630 internal_t*i = (internal_t*)dev->internal;
632 gfxcolor_t black = {255,0,0,0};
634 draw_line(dev, line);
635 fill_solid(dev, &black);
638 void render_addfont(struct _gfxdevice*dev, char*fontid, gfxfont_t*font)
640 internal_t*i = (internal_t*)dev->internal;
642 fontlist_t*last=0,*l = i->fontlist;
645 if(!strcmp((char*)l->id, fontid)) {
646 return; // we already know this font
650 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
660 void render_drawchar(struct _gfxdevice*dev, char*fontid, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
662 internal_t*i = (internal_t*)dev->internal;
664 if(i->font && i->fontid && !strcmp(fontid, i->fontid)) {
665 // current font is correct
667 fontlist_t*l = i->fontlist;
671 if(!strcmp((char*)l->id, i->fontid)) {
679 fprintf(stderr, "Unknown font id: %s", fontid);
684 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
686 gfxline_t*line2 = gfxline_clone(glyph->line);
687 gfxline_transform(line2, matrix);
688 draw_line(dev, line2);
689 fill_solid(dev, color);
695 void render_result_write(gfxresult_t*r, int filedesc)
697 internal_result_t*i= (internal_result_t*)r->internal;
699 int render_result_save(gfxresult_t*r, char*filename)
701 internal_result_t*i= (internal_result_t*)r->internal;
705 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
709 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
713 void*render_result_get(gfxresult_t*r, char*name)
715 internal_result_t*i= (internal_result_t*)r->internal;
718 void render_result_destroy(gfxresult_t*r)
720 internal_result_t*i= (internal_result_t*)r->internal;
721 free(i); r->internal = 0;
725 gfxresult_t* render_finish(struct _gfxdevice*dev)
727 internal_t*i = (internal_t*)dev->internal;
729 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
731 res->internal = i->results;i->results = 0;
732 res->write = render_result_write;
733 res->save = render_result_save;
734 res->get = render_result_get;
735 res->destroy = render_result_destroy;
737 free(dev->internal); dev->internal = 0; i = 0;
741 void render_startpage(struct _gfxdevice*dev, int width, int height)
743 internal_t*i = (internal_t*)dev->internal;
746 if(i->width2 || i->height2) {
747 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
753 i->width2 = width*i->antialize*i->multiply;
754 i->height2 = height*i->antialize*i->multiply;
756 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
757 for(y=0;y<i->height2;y++) {
758 memset(&i->lines[y], 0, sizeof(renderline_t));
759 i->lines[y].points = 0;
762 i->zbuf = (int*)rfx_calloc(sizeof(int)*i->width2*i->height2);
763 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
764 i->ymin = 0x7fffffff;
765 i->ymax = -0x80000000;
770 void render_endpage(struct _gfxdevice*dev)
772 internal_t*i = (internal_t*)dev->internal;
774 if(!i->width2 || !i->height2) {
775 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
781 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
785 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
786 ir->width = i->width;
787 ir->height = i->height;
788 ir->img = i->img; i->img = 0;
791 i->result_next->next = ir;
798 rfx_free(i->lines);i->lines=0; //FIXME
799 rfx_free(i->zbuf);i->zbuf = 0;
800 if(i->img) {rfx_free(i->img);i->img = 0;}
806 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
808 /* not supported for this output device */
811 void gfxdevice_render_init(gfxdevice_t*dev)
813 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
815 memset(dev, 0, sizeof(gfxdevice_t));
825 dev->setparameter = render_setparameter;
826 dev->startpage = render_startpage;
827 dev->startclip = render_startclip;
828 dev->endclip = render_endclip;
829 dev->stroke = render_stroke;
830 dev->fill = render_fill;
831 dev->fillbitmap = render_fillbitmap;
832 dev->fillgradient = render_fillgradient;
833 dev->addfont = render_addfont;
834 dev->drawchar = render_drawchar;
835 dev->drawlink = render_drawlink;
836 dev->endpage = render_endpage;
837 dev->finish = render_finish;