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 _internal {
77 clipbuffer_t*clipbufs;
82 internal_result_t*results;
83 internal_result_t*result_next;
86 typedef enum {filltype_solid,filltype_clip,filltype_bitmap} filltype_t;
88 typedef struct _fillinfo {
89 filltype_t type; //0=solid,1=clip
98 static inline void add_pixel(internal_t*i, float x, int y)
102 if(x >= i->width2 || y >= i->height2 || y<0) return;
104 if(y<i->ymin) i->ymin = y;
105 if(y>i->ymax) i->ymax = y;
107 renderline_t*l = &i->lines[y];
109 if(l->num == l->size) {
111 l->points = (renderpoint_t*)rfx_realloc(l->points, l->size * sizeof(renderpoint_t));
113 l->points[l->num] = p;
117 /* set this to 0.777777 or something if the "both fillstyles set while not inside shape"
118 problem appears to often */
121 #define INT(x) ((int)((x)+16)-16)
123 static void add_line(gfxdevice_t*dev , double x1, double y1, double x2, double y2)
125 internal_t*i = (internal_t*)dev->internal;
127 double ny1, ny2, stepx;
129 int l = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
130 printf(" l[%d - %.2f/%.2f -> %.2f/%.2f]\n", l, x1/20.0, y1/20.0, x2/20.0, y2/20.0);
147 ny1 = INT(y1) + 1.0 + CUT;
150 ny2 = INT(y2) - 1.0 + CUT;
157 x1 = x1 + (ny1-y1)*stepx;
158 x2 = x2 + (ny2-y2)*stepx;
167 float xx = (float)(startx + posx);
168 add_pixel(i, xx ,posy);
174 #define PI 3.14159265358979
175 static void add_solidline(gfxdevice_t*dev, double x1, double y1, double x2, double y2, double width)
177 internal_t*i = (internal_t*)dev->internal;
190 /* Make sure the line is always at least one pixel wide */
192 /* That's what Macromedia's Player does at least at zoom level >= 1. */
195 /* That's what Macromedia's Player seems to do at zoom level 0. */
196 /* TODO: needs testing */
198 /* TODO: how does this interact with scaling? */
199 if(width * i->multiply < 1.0)
200 width = 1.0 / i->multiply;
203 sd = (double)dx*(double)dx+(double)dy*(double)dy;
225 add_line(dev, x1+vx, y1+vy, xx, yy);
228 for(t=1;t<segments;t++) {
229 double s = sin(t*PI/segments);
230 double c = cos(t*PI/segments);
231 xx = (x2 + vx*c - vy*s);
232 yy = (y2 + vx*s + vy*c);
233 add_line(dev, lastx, lasty, xx, yy);
240 add_line(dev, lastx, lasty, xx, yy);
245 add_line(dev, lastx, lasty, xx, yy);
248 for(t=1;t<segments;t++) {
249 double s = sin(t*PI/segments);
250 double c = cos(t*PI/segments);
251 xx = (x1 - vx*c + vy*s);
252 yy = (y1 - vx*s - vy*c);
253 add_line(dev, lastx, lasty, xx, yy);
257 add_line(dev, lastx, lasty, (x1+vx), (y1+vy));
260 static int compare_renderpoints(const void * _a, const void * _b)
262 renderpoint_t*a = (renderpoint_t*)_a;
263 renderpoint_t*b = (renderpoint_t*)_b;
264 if(a->x < b->x) return -1;
265 if(a->x > b->x) return 1;
269 static void fill_line_solid(RGBA*line, U32*z, int y, int x1, int x2, RGBA col)
273 U32 bit = 1<<(x1&31);
274 int bitpos = (x1/32);
277 int ainv = 255-col.a;
278 col.r = (col.r*col.a)>>8;
279 col.g = (col.g*col.a)>>8;
280 col.b = (col.b*col.a)>>8;
284 line[x].r = ((line[x].r*ainv)>>8)+col.r;
285 line[x].g = ((line[x].g*ainv)>>8)+col.g;
286 line[x].b = ((line[x].b*ainv)>>8)+col.b;
307 static void fill_line_bitmap(RGBA*line, U32*z, int y, int x1, int x2, fillinfo_t*info)
311 gfxmatrix_t*m = info->matrix;
312 gfximage_t*b = info->image;
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 */
321 if(!b->width || !b->height) {
322 gfxcolor_t red = {255,255,0,0};
323 fill_line_solid(line, z, y, x1, x2, red);
327 U32 bit = 1<<(x1&31);
328 int bitpos = (x1/32);
333 int xx = (int)(( (x - m->tx) * m->m11 - (y - m->ty) * m->m10)*det);
334 int yy = (int)((- (x - m->tx) * m->m01 + (y - m->ty) * m->m00)*det);
339 if(xx>=b->width) xx = b->width-1;
341 if(yy>=b->height) yy = b->height-1;
345 if(xx<0) xx += b->width;
346 if(yy<0) yy += b->height;
349 col = b->data[yy*b->width+xx];
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 int*zline = &i->zbuf[i->width2*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)
428 void fill_solid(gfxdevice_t*dev, gfxcolor_t* color)
431 info.type = filltype_solid;
436 int render_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
438 internal_t*i = (internal_t*)dev->internal;
439 if(!strcmp(key, "antialize")) {
440 i->antialize = atoi(value);
441 } else if(!strcmp(key, "multiply")) {
442 i->multiply = atoi(value);
447 void newclip(struct _gfxdevice*dev)
449 internal_t*i = (internal_t*)dev->internal;
451 clipbuffer_t*c = rfx_calloc(sizeof(clipbuffer_t));
452 c->linesize = ((i->width2+31) / 32);
453 c->data = rfx_calloc(c->linesize * i->height2);
456 i->clipbufs = i->clipbuf = c;
458 clipbuffer_t*old = i->clipbuf;
460 i->clipbuf->prev = old;
464 void endclip(struct _gfxdevice*dev)
466 internal_t*i = (internal_t*)dev->internal;
469 fprintf(stderr, "endclip without any active clip buffers");
473 clipbuffer_t*old = i->clipbuf;
475 if(i->clipbuf == i->clipbufs)
478 i->clipbuf = i->clipbuf->prev;
481 free(old->data);old->data = 0;
485 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)
487 internal_t*i = (internal_t*)dev->internal;
490 if(cap_style != gfx_capRound || joint_style != gfx_joinRound) {
491 fprintf(stderr, "Warning: cap/joint style != round not yet supported\n");
495 int x1,y1,x2,y2,x3,y3;
497 if(line->type == gfx_moveTo) {
498 } else if(line->type == gfx_lineTo) {
500 double x3=line->x,y3=line->y;
501 add_solidline(dev, x1, y1, x3, y3, width * i->multiply);
502 fill_solid(dev, color);
503 } else if(line->type == gfx_splineTo) {
504 int c,t,parts,qparts;
508 double x2=line->sx,y2=line->sy;
509 double x3=line->x,y3=line->y;
511 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
515 parts = (int)(sqrt(c)/3);
516 if(!parts) parts = 1;
518 for(t=1;t<=parts;t++) {
519 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
520 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
522 add_solidline(dev, xx, yy, nx, ny, width * i->multiply);
523 fill_solid(dev, color);
534 static void draw_line(gfxdevice_t*dev, gfxline_t*line)
536 internal_t*i = (internal_t*)dev->internal;
541 int x1,y1,x2,y2,x3,y3;
543 if(line->type == gfx_moveTo) {
544 } else if(line->type == gfx_lineTo) {
546 double x3=line->x,y3=line->y;
548 add_line(dev, x1, y1, x3, y3);
549 } else if(line->type == gfx_splineTo) {
550 int c,t,parts,qparts;
554 double x2=line->sx,y2=line->sy;
555 double x3=line->x,y3=line->y;
557 c = abs(x3-2*x2+x1) + abs(y3-2*y2+y1);
561 parts = (int)(sqrt(c)/3);
562 if(!parts) parts = 1;
564 for(t=1;t<=parts;t++) {
565 double nx = (double)(t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts);
566 double ny = (double)(t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts);
568 add_line(dev, xx, yy, nx, ny);
579 void render_startclip(struct _gfxdevice*dev, gfxline_t*line)
581 internal_t*i = (internal_t*)dev->internal;
584 info.type = filltype_clip;
585 draw_line(dev, line);
589 void render_endclip(struct _gfxdevice*dev)
591 internal_t*i = (internal_t*)dev->internal;
595 void render_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
597 internal_t*i = (internal_t*)dev->internal;
599 draw_line(dev, line);
600 fill_solid(dev, color);
603 void render_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
605 internal_t*i = (internal_t*)dev->internal;
607 gfxcolor_t black = {255,0,0,0};
609 draw_line(dev, line);
612 info.type = filltype_bitmap;
614 info.matrix = matrix;
615 info.cxform = cxform;
619 void render_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
621 internal_t*i = (internal_t*)dev->internal;
623 gfxcolor_t black = {255,0,0,0};
625 draw_line(dev, line);
626 fill_solid(dev, &black);
629 void render_addfont(struct _gfxdevice*dev, gfxfont_t*font)
633 void render_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
635 internal_t*i = (internal_t*)dev->internal;
637 gfxglyph_t*glyph = &font->glyphs[glyphnr];
638 gfxline_t*line2 = gfxline_clone(glyph->line);
639 gfxline_transform(line2, matrix);
640 draw_line(dev, line2);
641 fill_solid(dev, color);
647 void render_result_write(gfxresult_t*r, int filedesc)
649 internal_result_t*i= (internal_result_t*)r->internal;
651 int render_result_save(gfxresult_t*r, char*filename)
653 internal_result_t*i= (internal_result_t*)r->internal;
657 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
661 writePNG(filename, (unsigned char*)i->img, i->width, i->height);
665 void*render_result_get(gfxresult_t*r, char*name)
667 internal_result_t*i= (internal_result_t*)r->internal;
670 void render_result_destroy(gfxresult_t*r)
672 internal_result_t*i= (internal_result_t*)r->internal;
673 free(i); r->internal = 0;
677 gfxresult_t* render_finish(struct _gfxdevice*dev)
679 internal_t*i = (internal_t*)dev->internal;
681 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
683 res->internal = i->results;i->results = 0;
684 res->write = render_result_write;
685 res->save = render_result_save;
686 res->get = render_result_get;
687 res->destroy = render_result_destroy;
689 free(dev->internal); dev->internal = 0; i = 0;
691 /* TODO: free fonts */
696 void render_startpage(struct _gfxdevice*dev, int width, int height)
698 internal_t*i = (internal_t*)dev->internal;
701 if(i->width2 || i->height2) {
702 fprintf(stderr, "Error: startpage() called twice (no endpage()?)\n");
708 i->width2 = width*i->antialize*i->multiply;
709 i->height2 = height*i->antialize*i->multiply;
711 i->lines = (renderline_t*)rfx_alloc(i->height2*sizeof(renderline_t));
712 for(y=0;y<i->height2;y++) {
713 memset(&i->lines[y], 0, sizeof(renderline_t));
714 i->lines[y].points = 0;
717 i->zbuf = (int*)rfx_calloc(sizeof(int)*i->width2*i->height2);
718 i->img = (RGBA*)rfx_calloc(sizeof(RGBA)*i->width2*i->height2);
719 i->ymin = 0x7fffffff;
720 i->ymax = -0x80000000;
725 void render_endpage(struct _gfxdevice*dev)
727 internal_t*i = (internal_t*)dev->internal;
729 if(!i->width2 || !i->height2) {
730 fprintf(stderr, "Error: endpage() called without corresponding startpage()\n");
736 fprintf(stderr, "Warning: unclosed clip while processing endpage()\n");
740 internal_result_t*ir= (internal_result_t*)rfx_calloc(sizeof(internal_result_t));
741 ir->width = i->width;
742 ir->height = i->height;
743 ir->img = i->img; i->img = 0;
746 i->result_next->next = ir;
753 rfx_free(i->lines);i->lines=0; //FIXME
754 rfx_free(i->zbuf);i->zbuf = 0;
755 if(i->img) {rfx_free(i->img);i->img = 0;}
761 void render_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
763 /* not supported for this output device */
766 void gfxdevice_render_init(gfxdevice_t*dev)
768 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
770 memset(dev, 0, sizeof(gfxdevice_t));
780 dev->setparameter = render_setparameter;
781 dev->startpage = render_startpage;
782 dev->startclip = render_startclip;
783 dev->endclip = render_endclip;
784 dev->stroke = render_stroke;
785 dev->fill = render_fill;
786 dev->fillbitmap = render_fillbitmap;
787 dev->fillgradient = render_fillgradient;
788 dev->addfont = render_addfont;
789 dev->drawchar = render_drawchar;
790 dev->drawlink = render_drawlink;
791 dev->endpage = render_endpage;
792 dev->finish = render_finish;