3 Part of the swftools package.
5 Copyright (c) 2001,2002,2003,2004,2005 Matthias Kramm <kramm@quiss.org>
7 Swftools 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 Swftools 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 swftools; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #include "../../config.h"
34 #include "../rfxswf.h"
35 #include "../gfxdevice.h"
36 #include "../gfxtools.h"
37 #include "../art/libart.h"
38 #include "artsutils.c"
40 #define CHARDATAMAX 8192
44 typedef struct _chardata {
46 int fontid; /* TODO: use a SWFFONT instead */
53 typedef struct _fontlist
56 struct _fontlist*next;
59 typedef long int twip;
61 typedef struct _swfmatrix {
62 double m11,m12,m21,m22,m31,m32;
65 typedef struct _swfoutput_internal
67 gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
69 double config_dumpfonts;
70 double config_ppmsubpixels;
71 double config_jpegsubpixels;
72 int config_opennewwindow;
73 int config_ignoredraworder;
74 int config_drawonlyshapes;
75 int config_jpegquality;
76 int config_storeallcharacters;
77 int config_generate_fake_tags;
78 int config_enablezlib;
79 int config_insertstoptag;
80 int config_flashversion;
81 int config_reordertags;
82 int config_showclipshapes;
83 int config_splinemaxerror;
84 int config_fontsplinemaxerror;
85 int config_filloverlap;
88 int config_disable_polygon_conversion;
89 int config_normalize_polygon_positions;
90 RGBA config_linkcolor;
91 float config_minlinewidth;
92 double config_caplinewidth;
93 char* config_linktarget;
94 char*config_internallinkfunction;
95 char*config_externallinkfunction;
97 double config_framerate;
101 fontlist_t* fontlist;
136 int pic_height[1024];
142 char fillstylechanged;
144 int jpeg; //next image type
149 chardata_t chardata[CHARDATAMAX];
156 int current_font_size;
158 double lastfontm11,lastfontm12,lastfontm21,lastfontm22;
169 } swfoutput_internal;
171 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
172 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
173 static void swf_drawstroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit);
174 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
175 static void swf_endclip(gfxdevice_t*dev);
176 static void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit);
177 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
178 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
179 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
180 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
181 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
182 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action);
183 static void swf_startframe(gfxdevice_t*dev, int width, int height);
184 static void swf_endframe(gfxdevice_t*dev);
185 static gfxresult_t* swf_finish(gfxdevice_t*driver);
187 static swfoutput_internal* init_internal_struct()
189 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
190 memset(i, 0, sizeof(swfoutput_internal));
214 i->fillstylechanged = 0;
221 i->config_dumpfonts=0;
222 i->config_ppmsubpixels=0;
223 i->config_jpegsubpixels=0;
224 i->config_opennewwindow=1;
225 i->config_ignoredraworder=0;
226 i->config_drawonlyshapes=0;
227 i->config_jpegquality=85;
228 i->config_storeallcharacters=0;
229 i->config_enablezlib=0;
230 i->config_generate_fake_tags=0;
231 i->config_insertstoptag=0;
232 i->config_flashversion=6;
233 i->config_framerate=0.25;
234 i->config_splinemaxerror=1;
235 i->config_fontsplinemaxerror=1;
236 i->config_filloverlap=0;
238 i->config_bboxvars=0;
239 i->config_showclipshapes=0;
240 i->config_minlinewidth=0.05;
241 i->config_caplinewidth=1;
242 i->config_linktarget=0;
243 i->config_internallinkfunction=0;
244 i->config_externallinkfunction=0;
245 i->config_reordertags=1;
247 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
248 i->config_linkcolor.a = 0x40;
253 static int id_error = 0;
255 static U16 getNewID(gfxdevice_t* dev)
257 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
258 if(i->currentswfid == 65535) {
260 msg("<error> ID Table overflow");
261 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
267 return ++i->currentswfid;
269 static U16 getNewDepth(gfxdevice_t* dev)
271 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
272 if(i->depth == 65535) {
274 msg("<error> Depth Table overflow");
275 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
284 static void startshape(gfxdevice_t* dev);
285 static void starttext(gfxdevice_t* dev);
286 static void endshape(gfxdevice_t* dev);
287 static void endtext(gfxdevice_t* dev);
289 typedef struct _plotxy
294 // write a move-to command into the swf
295 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
297 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
298 int rx = (int)(p0.x*20);
299 int ry = (int)(p0.y*20);
300 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
301 swf_ShapeSetMove (tag, i->shape, rx,ry);
302 i->fillstylechanged = 0;
309 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
311 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
315 return movetoxy(dev, tag, p);
317 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
319 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
325 swf_ExpandRect(&i->bboxrect, p);
327 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
331 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
333 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
334 int width = i->linewidth/4;
338 //swf_ShapeSetLine(tag, i->shape,-width,-width);
339 //swf_ShapeSetLine(tag, i->shape,width*2,0);
340 //swf_ShapeSetLine(tag, i->shape,0,width*2);
341 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
342 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
343 //swf_ShapeSetLine(tag, i->shape,width,width);
346 swf_ShapeSetLine(tag, i->shape,-width,0);
347 swf_ShapeSetLine(tag, i->shape,width,-width);
348 swf_ShapeSetLine(tag, i->shape,width,width);
349 swf_ShapeSetLine(tag, i->shape,-width,width);
350 swf_ShapeSetLine(tag, i->shape,-width,-width);
351 swf_ShapeSetLine(tag, i->shape,width,0);
353 addPointToBBox(dev, x-width ,y-width);
354 addPointToBBox(dev, x+width ,y+width);
357 // write a line-to command into the swf
358 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
360 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
361 int px = (int)(p0.x*20);
362 int py = (int)(p0.y*20);
363 int rx = (px-i->swflastx);
364 int ry = (py-i->swflasty);
366 swf_ShapeSetLine (tag, i->shape, rx,ry);
367 addPointToBBox(dev, i->swflastx,i->swflasty);
368 addPointToBBox(dev, px,py);
369 }/* else if(!i->fill) {
370 // treat lines of length 0 as plots, making them
371 // at least 1 twip wide so Flash will display them
372 plot(dev, i->swflastx, i->swflasty, tag);
379 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
384 linetoxy(dev,tag, p);
387 // write a spline-to command into the swf
388 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
390 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
391 int lastlastx = i->swflastx;
392 int lastlasty = i->swflasty;
394 int cx = ((int)(control.x*20)-i->swflastx);
395 int cy = ((int)(control.y*20)-i->swflasty);
398 int ex = ((int)(end.x*20)-i->swflastx);
399 int ey = ((int)(end.y*20)-i->swflasty);
403 if((cx || cy) && (ex || ey)) {
404 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
405 addPointToBBox(dev, lastlastx ,lastlasty );
406 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
407 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
408 } else if(cx || cy || ex || ey) {
409 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
410 addPointToBBox(dev, lastlastx ,lastlasty );
411 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
412 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
418 /* write a line, given two points and the transformation
420 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
422 moveto(dev, tag, p0);
423 lineto(dev, tag, p1);
426 void resetdrawer(gfxdevice_t*dev)
428 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
433 static void stopFill(gfxdevice_t*dev)
435 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
436 if(i->lastwasfill!=0)
438 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
439 i->fillstylechanged = 1;
443 static void startFill(gfxdevice_t*dev)
445 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
446 if(i->lastwasfill!=1)
448 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
449 i->fillstylechanged = 1;
454 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
466 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
468 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
471 memset(&r, 0, sizeof(r));
474 if(debug) printf("\n");
475 for(t=0;t<i->chardatapos;t++)
477 if(i->chardata[t].fontid != font->id) {
478 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
481 SRECT b = font->layout->bounds[i->chardata[t].charid];
482 b.xmin *= i->chardata[t].size;
483 b.ymin *= i->chardata[t].size;
484 b.xmax *= i->chardata[t].size;
485 b.ymax *= i->chardata[t].size;
487 /* divide by 1024, rounding xmax/ymax up */
488 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
490 b.xmin += i->chardata[t].x;
491 b.ymin += i->chardata[t].y;
492 b.xmax += i->chardata[t].x;
493 b.ymax += i->chardata[t].y;
495 /* until we solve the INTERNAL_SCALING problem (see below)
496 make sure the bounding box is big enough */
502 b = swf_TurnRect(b, m);
504 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
505 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
506 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
507 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
508 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
513 i->chardata[t].fontid,
515 i->chardata[t].charid
517 swf_ExpandRect2(&r, &b);
519 if(debug) printf("-----> (%f,%f,%f,%f)\n",
527 static void putcharacters(gfxdevice_t*dev, TAG*tag)
529 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
533 color.r = i->chardata[0].color.r^255;
542 int charadvance[128];
545 int glyphbits=1; //TODO: can this be zero?
548 if(tag->id != ST_DEFINETEXT &&
549 tag->id != ST_DEFINETEXT2) {
550 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
553 if(!i->chardatapos) {
554 msg("<warning> putcharacters called with zero characters");
557 for(pass = 0; pass < 2; pass++)
567 advancebits++; // add sign bit
568 swf_SetU8(tag, glyphbits);
569 swf_SetU8(tag, advancebits);
572 for(t=0;t<=i->chardatapos;t++)
574 if(lastfontid != i->chardata[t].fontid ||
575 lastx!=i->chardata[t].x ||
576 lasty!=i->chardata[t].y ||
577 !colorcompare(dev,&color, &i->chardata[t].color) ||
579 lastsize != i->chardata[t].size ||
582 if(charstorepos && pass==0)
585 for(s=0;s<charstorepos;s++)
587 while(charids[s]>=(1<<glyphbits))
589 while(charadvance[s]>=(1<<advancebits))
593 if(charstorepos && pass==1)
595 tag->writeBit = 0; // Q&D
596 swf_SetBits(tag, 0, 1); // GLYPH Record
597 swf_SetBits(tag, charstorepos, 7); // number of glyphs
599 for(s=0;s<charstorepos;s++)
601 swf_SetBits(tag, charids[s], glyphbits);
602 swf_SetBits(tag, charadvance[s], advancebits);
607 if(pass == 1 && t<i->chardatapos)
613 if(lastx != i->chardata[t].x ||
614 lasty != i->chardata[t].y)
616 newx = i->chardata[t].x;
617 newy = i->chardata[t].y;
623 if(!colorcompare(dev,&color, &i->chardata[t].color))
625 color = i->chardata[t].color;
628 font.id = i->chardata[t].fontid;
629 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
632 tag->writeBit = 0; // Q&D
633 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
636 lastfontid = i->chardata[t].fontid;
637 lastx = i->chardata[t].x;
638 lasty = i->chardata[t].y;
639 lastsize = i->chardata[t].size;
642 if(t==i->chardatapos)
646 int nextt = t==i->chardatapos-1?t:t+1;
647 int rel = i->chardata[nextt].x-i->chardata[t].x;
648 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
650 lastx=i->chardata[nextt].x;
654 lastx=i->chardata[t].x;
656 charids[charstorepos] = i->chardata[t].charid;
657 charadvance[charstorepos] = advance;
664 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
666 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
667 if(i->chardatapos == CHARDATAMAX)
669 msg("<warning> Character buffer too small. SWF will be slightly bigger");
673 i->chardata[i->chardatapos].fontid = fontid;
674 i->chardata[i->chardatapos].charid = charid;
675 i->chardata[i->chardatapos].x = x;
676 i->chardata[i->chardatapos].y = y;
677 i->chardata[i->chardatapos].color = color;
678 i->chardata[i->chardatapos].size = size;
682 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
683 So if we set this value to high, the char coordinates will overflow.
684 If we set it to low, however, the char positions will be inaccurate */
685 #define GLYPH_SCALE 1
687 /* process a character. */
688 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, float x, float y, gfxcolor_t*col)
690 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
692 msg("<warning> Font is NULL");
696 if(charid<0 || charid>=swffont->numchars) {
697 msg("<warning> No character %d in font %s (%d chars)", charid, FIXNULL((char*)swffont->name), swffont->numchars);
700 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
701 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
702 charid, FIXNULL((char*)swffont->name), swffont->numchars);
711 double det = i->fontmatrix.sx/65536.0 * i->fontmatrix.sy/65536.0 -
712 i->fontmatrix.r0/65536.0 * i->fontmatrix.r1/65536.0;
714 if(fabs(det) < 0.0005) {
715 /* x direction equals y direction- the text is invisible */
718 det = 20 * GLYPH_SCALE / det;
721 p.x = (SCOORD)(( x * i->fontmatrix.sy/65536.0 - y * i->fontmatrix.r1/65536.0)*det);
722 p.y = (SCOORD)((- x * i->fontmatrix.r0/65536.0 + y * i->fontmatrix.sx/65536.0)*det);
724 RGBA rgba = *(RGBA*)col;
725 putcharacter(dev, swffont->id, charid,p.x,p.y,i->current_font_size, rgba);
726 swf_FontUseGlyph(swffont, charid);
730 static void endtext(gfxdevice_t*dev)
732 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
736 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT2);
737 swf_SetU16(i->tag, i->textid);
740 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
742 swf_SetRect(i->tag,&r);
744 swf_SetMatrix(i->tag,&i->fontmatrix);
746 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
748 putcharacters(dev, i->tag);
751 if(i->swf->fileVersion >= 8) {
752 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
753 swf_SetU16(i->tag, i->textid);
755 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
756 swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
757 //swf_SetU8(i->tag, /*no grid*/(0<<3)|/*flashtype*/0x40);
759 swf_SetU32(i->tag, 0);//thickness
760 swf_SetU32(i->tag, 0);//sharpness
761 swf_SetU8(i->tag, 0);//reserved
763 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
765 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
769 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
770 static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22)
776 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
777 if(i->lastfontm11 == m11 &&
778 i->lastfontm12 == m12 &&
779 i->lastfontm21 == m21 &&
780 i->lastfontm22 == m22)
785 i->lastfontm11 = m11;
786 i->lastfontm12 = m12;
787 i->lastfontm21 = m21;
788 i->lastfontm22 = m22;
790 double xsize = sqrt(m11*m11 + m12*m12);
791 double ysize = sqrt(m21*m21 + m22*m22);
792 i->current_font_size = (xsize>ysize?xsize:ysize)*1;
793 if(i->current_font_size < 1)
794 i->current_font_size = 1;
795 double ifs = 1.0 / (i->current_font_size*GLYPH_SCALE);
798 m.sx = (U32)((m11*ifs)*65536); m.r1 = (U32)((m21*ifs)*65536);
799 m.r0 = (U32)((m12*ifs)*65536); m.sy = (U32)((m22*ifs)*65536);
805 static void endpage(gfxdevice_t*dev)
807 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
817 void swf_startframe(gfxdevice_t*dev, int width, int height)
819 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
820 if(!i->firstpage && !i->pagefinished)
822 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
824 swf_GetMatrix(0, &i->page_matrix);
825 i->page_matrix.tx = 0;
826 i->page_matrix.ty = 0;
832 /* set clipping/background rectangle */
833 /* TODO: this should all be done in SWFOutputDev */
834 //setBackground(dev, x1, y1, x2, y2);
836 /* increase SWF's bounding box */
842 swf_ExpandRect2(&i->swf->movieSize, &r);
844 i->lastframeno = i->frameno;
849 void swf_endframe(gfxdevice_t*dev)
851 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
856 if(i->config_insertstoptag) {
858 atag = action_Stop(atag);
859 atag = action_End(atag);
860 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
861 swf_ActionSet(i->tag,atag);
863 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
866 for(i->depth;i->depth>i->startdepth;i->depth--) {
867 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
868 swf_SetU16(i->tag,i->depth);
870 i->depth = i->startdepth;
873 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
875 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
877 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
881 int shapeid = getNewID(dev);
886 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
888 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
889 swf_SetU16(i->tag,shapeid);
890 swf_SetRect(i->tag,&r);
891 swf_SetShapeHeader(i->tag,s);
892 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
893 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
894 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
895 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
896 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
897 swf_ShapeSetEnd(i->tag);
899 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
900 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
901 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
902 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
905 /* initialize the swf writer */
906 void gfxdevice_swf_init(gfxdevice_t* dev)
908 memset(dev, 0, sizeof(gfxdevice_t));
912 dev->internal = init_internal_struct();
914 dev->startpage = swf_startframe;
915 dev->endpage = swf_endframe;
916 dev->finish = swf_finish;
917 dev->fillbitmap = swf_fillbitmap;
918 dev->setparameter = swf_setparameter;
919 dev->stroke = swf_stroke;
920 dev->startclip = swf_startclip;
921 dev->endclip = swf_endclip;
922 dev->fill = swf_fill;
923 dev->fillbitmap = swf_fillbitmap;
924 dev->fillgradient = swf_fillgradient;
925 dev->addfont = swf_addfont;
926 dev->drawchar = swf_drawchar;
927 dev->drawlink = swf_drawlink;
929 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
935 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
939 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
940 i->swf->fileVersion = i->config_flashversion;
941 i->swf->frameRate = i->config_framerate*0x100;
942 i->swf->movieSize.xmin = 0;
943 i->swf->movieSize.ymin = 0;
944 i->swf->movieSize.xmax = 0;
945 i->swf->movieSize.ymax = 0;
947 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
948 i->tag = i->swf->firstTag;
949 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
950 swf_SetRGB(i->tag,&rgb);
952 i->startdepth = i->depth = 0;
954 if(i->config_protect)
955 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
958 static void startshape(gfxdevice_t*dev)
960 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
965 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a)
968 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
970 swf_ShapeNew(&i->shape);
971 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
972 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
974 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
975 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
978 i->shapeid = getNewID(dev);
980 msg("<debug> Using shape id %d", i->shapeid);
982 swf_SetU16(i->tag,i->shapeid); // ID
984 i->bboxrectpos = i->tag->len;
988 r.xmax = 20*i->max_x;
989 r.ymax = 20*i->max_y;
990 swf_SetRect(i->tag,&r);
992 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
994 swf_SetShapeStyles(i->tag,i->shape);
995 swf_ShapeCountBits(i->shape,NULL,NULL);
996 swf_SetShapeBits(i->tag,i->shape);
998 /* TODO: do we really need this? */
999 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1000 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
1001 i->swflastx=i->swflasty=UNDEFINED_COORD;
1002 i->lastwasfill = -1;
1003 i->shapeisempty = 1;
1006 static void starttext(gfxdevice_t*dev)
1008 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1012 i->textid = getNewID(dev);
1014 i->swflastx=i->swflasty=0;
1018 /* TODO: move to ../lib/rfxswf */
1019 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1021 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1022 /* determine length of old rect */
1026 swf_GetRect(tag, &old);
1027 swf_ResetReadBits(tag);
1028 int pos_end = tag->pos;
1030 int len = tag->len - pos_end;
1031 U8*data = (U8*)malloc(len);
1032 memcpy(data, &tag->data[pos_end], len);
1035 swf_SetRect(tag, newrect);
1036 swf_SetBlock(tag, data, len);
1038 tag->pos = tag->readBit = 0;
1041 void cancelshape(gfxdevice_t*dev)
1043 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1044 /* delete old shape tag */
1046 i->tag = i->tag->prev;
1047 swf_DeleteTag(todel);
1048 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1050 i->bboxrectpos = -1;
1052 // i->currentswfid--; // this was an *exceptionally* bad idea
1055 void fixAreas(gfxdevice_t*dev)
1057 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1058 if(!i->shapeisempty && i->fill &&
1059 (i->bboxrect.xmin == i->bboxrect.xmax ||
1060 i->bboxrect.ymin == i->bboxrect.ymax) &&
1061 i->config_minlinewidth >= 0.001
1063 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1064 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1065 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1068 SRECT r = i->bboxrect;
1070 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1071 /* this thing comes down to a single dot- nothing to fix here */
1077 RGBA save_col = i->strokergb;
1078 int save_width = i->linewidth;
1080 i->strokergb = i->fillrgb;
1081 i->linewidth = (int)(i->config_minlinewidth*20);
1082 if(i->linewidth==0) i->linewidth = 1;
1087 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1088 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1090 i->strokergb = save_col;
1091 i->linewidth = save_width;
1096 static void endshape_noput(gfxdevice_t*dev)
1098 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1101 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1104 swf_ShapeFree(i->shape);
1112 static void endshape(gfxdevice_t*dev)
1114 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1120 if(i->shapeisempty ||
1122 (i->bboxrect.xmin == i->bboxrect.xmax &&
1123 i->bboxrect.ymin == i->bboxrect.ymax))
1125 // delete the shape again, we didn't do anything
1126 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1127 i->bboxrect.xmin /20.0,
1128 i->bboxrect.ymin /20.0,
1129 i->bboxrect.xmax /20.0,
1130 i->bboxrect.ymax /20.0
1136 swf_ShapeSetEnd(i->tag);
1138 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1140 msg("<trace> Placing shape ID %d", i->shapeid);
1142 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1143 MATRIX m = i->page_matrix;
1144 m.tx += i->shapeposx;
1145 m.ty += i->shapeposy;
1146 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1148 if(i->config_animate) {
1149 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1152 swf_ShapeFree(i->shape);
1155 i->bboxrectpos = -1;
1162 void wipeSWF(SWF*swf)
1164 TAG*tag = swf->firstTag;
1166 TAG*next = tag->next;
1167 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1168 tag->id != ST_END &&
1169 tag->id != ST_DOACTION &&
1170 tag->id != ST_SHOWFRAME) {
1178 void swfoutput_finalize(gfxdevice_t*dev)
1180 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1182 if(i->tag && i->tag->id == ST_END)
1183 return; //already done
1185 if(i->config_bboxvars) {
1186 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1188 a = action_PushString(a, "xmin");
1189 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1190 a = action_SetVariable(a);
1191 a = action_PushString(a, "ymin");
1192 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1193 a = action_SetVariable(a);
1194 a = action_PushString(a, "xmax");
1195 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1196 a = action_SetVariable(a);
1197 a = action_PushString(a, "ymax");
1198 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1199 a = action_SetVariable(a);
1200 a = action_PushString(a, "width");
1201 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1202 a = action_SetVariable(a);
1203 a = action_PushString(a, "height");
1204 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1205 a = action_SetVariable(a);
1207 swf_ActionSet(tag, a);
1211 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1215 free(i->mark);i->mark = 0;
1219 fontlist_t *tmp,*iterator = i->fontlist;
1221 TAG*mtag = i->swf->firstTag;
1222 if(iterator->swffont) {
1223 if(!i->config_storeallcharacters) {
1224 msg("<debug> Reducing font %s", iterator->swffont->name);
1225 swf_FontReduce(iterator->swffont);
1227 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1228 if(i->config_storeallcharacters || used) {
1229 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1230 swf_FontSetDefine2(mtag, iterator->swffont);
1234 iterator = iterator->next;
1236 i->tag = swf_InsertTag(i->tag,ST_END);
1237 TAG* tag = i->tag->prev;
1239 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1240 and the ST_END- they confuse the flash player */
1241 while(tag->id == ST_REMOVEOBJECT2) {
1242 TAG* prev = tag->prev;
1250 if(i->config_enablezlib || i->config_flashversion>=6) {
1251 i->swf->compressed = 1;
1254 // if(i->config_reordertags)
1255 // swf_Optimize(i->swf);
1258 int swfresult_save(gfxresult_t*gfx, const char*filename)
1260 SWF*swf = (SWF*)gfx->internal;
1263 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1268 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1272 if(swf->compressed) {
1273 if FAILED(swf_WriteSWC(fi,swf))
1274 msg("<error> WriteSWC() failed.\n");
1276 if FAILED(swf_WriteSWF(fi,swf))
1277 msg("<error> WriteSWF() failed.\n");
1284 void* swfresult_get(gfxresult_t*gfx, const char*name)
1286 SWF*swf = (SWF*)gfx->internal;
1287 if(!strcmp(name, "swf")) {
1288 return (void*)swf_CopySWF(swf);
1289 } else if(!strcmp(name, "xmin")) {
1290 return (void*)(swf->movieSize.xmin/20);
1291 } else if(!strcmp(name, "ymin")) {
1292 return (void*)(swf->movieSize.ymin/20);
1293 } else if(!strcmp(name, "xmax")) {
1294 return (void*)(swf->movieSize.xmax/20);
1295 } else if(!strcmp(name, "ymax")) {
1296 return (void*)(swf->movieSize.ymax/20);
1297 } else if(!strcmp(name, "width")) {
1298 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1299 } else if(!strcmp(name, "height")) {
1300 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1304 void swfresult_destroy(gfxresult_t*gfx)
1307 swf_FreeTags((SWF*)gfx->internal);
1308 free(gfx->internal);
1311 memset(gfx, 0, sizeof(gfxresult_t));
1315 static void swfoutput_destroy(gfxdevice_t* dev);
1317 gfxresult_t* swf_finish(gfxdevice_t* dev)
1319 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1322 if(i->config_linktarget) {
1323 free(i->config_linktarget);
1324 i->config_linktarget = 0;
1327 swfoutput_finalize(dev);
1328 SWF* swf = i->swf;i->swf = 0;
1329 swfoutput_destroy(dev);
1331 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1332 result->internal = swf;
1333 result->save = swfresult_save;
1335 result->get = swfresult_get;
1336 result->destroy = swfresult_destroy;
1340 /* Perform cleaning up */
1341 static void swfoutput_destroy(gfxdevice_t* dev)
1343 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1345 /* not initialized yet- nothing to destroy */
1349 fontlist_t *tmp,*iterator = i->fontlist;
1351 if(iterator->swffont) {
1352 swf_FontFree(iterator->swffont);iterator->swffont=0;
1355 iterator = iterator->next;
1358 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1361 memset(dev, 0, sizeof(gfxdevice_t));
1364 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1366 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1367 if(i->fillrgb.r == r &&
1368 i->fillrgb.g == g &&
1369 i->fillrgb.b == b &&
1370 i->fillrgb.a == a) return;
1380 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1382 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1383 if(i->strokergb.r == r &&
1384 i->strokergb.g == g &&
1385 i->strokergb.b == b &&
1386 i->strokergb.a == a) return;
1396 //#define ROUND_UP 19
1397 //#define ROUND_UP 10
1399 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1401 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1402 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1406 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1410 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1411 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1412 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1413 static void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points);
1415 /*void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1417 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1418 dev->drawlink(dev, points, url);
1421 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, const char*url)
1423 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1425 if(!strncmp("http://pdf2swf:", url, 15)) {
1426 char*tmp = strdup(url);
1427 int l = strlen(tmp);
1430 swfoutput_namedlink(dev, tmp+15, points);
1433 } else if(!strncmp("page", url, 4)) {
1436 if(url[t]<'0' || url[t]>'9')
1439 int page = atoi(&url[4]);
1440 if(page<0) page = 0;
1441 swfoutput_linktopage(dev, page, points);
1444 swfoutput_linktourl(dev, url, points);
1447 void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points)
1449 ActionTAG* actions = 0;
1450 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1456 if(i->config_externallinkfunction) {
1457 actions = action_PushString(actions, url); //parameter
1458 actions = action_PushInt(actions, 1); //number of parameters (1)
1459 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1460 actions = action_CallFunction(actions);
1461 } else if(!i->config_linktarget) {
1462 if(!i->config_opennewwindow)
1463 actions = action_GetUrl(actions, url, "_parent");
1465 actions = action_GetUrl(actions, url, "_this");
1467 actions = action_GetUrl(actions, url, i->config_linktarget);
1469 actions = action_End(actions);
1471 drawlink(dev, actions, 0, points,0);
1473 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1475 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1476 ActionTAG* actions = 0;
1483 if(!i->config_internallinkfunction) {
1484 actions = action_GotoFrame(actions, page-1);
1485 actions = action_End(actions);
1487 actions = action_PushInt(actions, page); //parameter
1488 actions = action_PushInt(actions, 1); //number of parameters (1)
1489 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1490 actions = action_CallFunction(actions);
1491 actions = action_End(actions);
1494 drawlink(dev, actions, 0, points,0);
1497 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1498 of the viewer objects, like subtitles, index elements etc.
1500 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1502 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1503 ActionTAG *actions1,*actions2;
1504 char*tmp = strdup(name);
1512 if(!strncmp(tmp, "call:", 5))
1514 char*x = strchr(&tmp[5], ':');
1516 actions1 = action_PushInt(0, 0); //number of parameters (0)
1517 actions1 = action_PushString(actions1, &tmp[5]); //function name
1518 actions1 = action_CallFunction(actions1);
1519 actions1 = action_End(actions1);
1522 actions1 = action_PushString(0, x+1); //parameter
1523 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1524 actions1 = action_PushString(actions1, &tmp[5]); //function name
1525 actions1 = action_CallFunction(actions1);
1526 actions1 = action_End(actions1);
1528 actions2 = action_End(0);
1533 actions1 = action_PushString(0, "/:subtitle");
1534 actions1 = action_PushString(actions1, name);
1535 actions1 = action_SetVariable(actions1);
1536 actions1 = action_End(actions1);
1538 actions2 = action_PushString(0, "/:subtitle");
1539 actions2 = action_PushString(actions2, "");
1540 actions2 = action_SetVariable(actions2);
1541 actions2 = action_End(actions2);
1544 drawlink(dev, actions1, actions2, points,mouseover);
1546 swf_ActionFree(actions1);
1547 swf_ActionFree(actions2);
1551 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1553 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1554 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1556 int lines= 0, splines=0;
1560 /* check whether the next segment is zero */
1561 if(line->type == gfx_moveTo) {
1562 moveto(dev, i->tag, line->x, line->y);
1563 px = lastx = line->x;
1564 py = lasty = line->y;
1566 } if(line->type == gfx_lineTo) {
1567 lineto(dev, i->tag, line->x, line->y);
1572 } else if(line->type == gfx_splineTo) {
1574 s.x = line->sx;p.x = line->x;
1575 s.y = line->sy;p.y = line->y;
1576 splineto(dev, i->tag, s, p);
1584 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1588 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1590 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1595 plotxy_t p1,p2,p3,p4;
1601 int buttonid = getNewID(dev);
1602 gfxbbox_t bbox = gfxline_getbbox(points);
1605 myshapeid = getNewID(dev);
1606 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1607 swf_ShapeNew(&i->shape);
1608 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1609 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1610 swf_SetU16(i->tag, myshapeid);
1611 r.xmin = (int)(bbox.xmin*20);
1612 r.ymin = (int)(bbox.ymin*20);
1613 r.xmax = (int)(bbox.xmax*20);
1614 r.ymax = (int)(bbox.ymax*20);
1615 swf_SetRect(i->tag,&r);
1616 swf_SetShapeStyles(i->tag,i->shape);
1617 swf_ShapeCountBits(i->shape,NULL,NULL);
1618 swf_SetShapeBits(i->tag,i->shape);
1619 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1620 i->swflastx = i->swflasty = 0;
1621 drawgfxline(dev, points);
1622 swf_ShapeSetEnd(i->tag);
1625 myshapeid2 = getNewID(dev);
1626 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1627 swf_ShapeNew(&i->shape);
1629 rgb = i->config_linkcolor;
1631 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1632 swf_SetU16(i->tag, myshapeid2);
1633 r.xmin = (int)(bbox.xmin*20);
1634 r.ymin = (int)(bbox.ymin*20);
1635 r.xmax = (int)(bbox.xmax*20);
1636 r.ymax = (int)(bbox.ymax*20);
1637 swf_SetRect(i->tag,&r);
1638 swf_SetShapeStyles(i->tag,i->shape);
1639 swf_ShapeCountBits(i->shape,NULL,NULL);
1640 swf_SetShapeBits(i->tag,i->shape);
1641 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1642 i->swflastx = i->swflasty = 0;
1643 drawgfxline(dev, points);
1644 swf_ShapeSetEnd(i->tag);
1648 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1649 swf_SetU16(i->tag,buttonid); //id
1650 swf_ButtonSetFlags(i->tag, 0); //menu=no
1651 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1652 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1653 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1654 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1655 swf_SetU8(i->tag,0);
1656 swf_ActionSet(i->tag,actions1);
1657 swf_SetU8(i->tag,0);
1661 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1662 swf_SetU16(i->tag,buttonid); //id
1663 swf_ButtonSetFlags(i->tag, 0); //menu=no
1664 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1665 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1666 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1667 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1668 swf_SetU8(i->tag,0); // end of button records
1669 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1670 swf_ActionSet(i->tag,actions1);
1672 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1673 swf_ActionSet(i->tag,actions2);
1674 swf_SetU8(i->tag,0);
1675 swf_ButtonPostProcess(i->tag, 2);
1677 swf_SetU8(i->tag,0);
1678 swf_ButtonPostProcess(i->tag, 1);
1682 msg("<trace> Placing link ID %d", buttonid);
1683 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1685 if(posx!=0 || posy!=0) {
1687 p.x = (int)(posx*20);
1688 p.y = (int)(posy*20);
1689 p = swf_TurnPoint(p, &i->page_matrix);
1694 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1696 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1703 for(t=0;t<picpos;t++)
1705 if(pic_xids[t] == xid &&
1706 pic_yids[t] == yid) {
1707 width = pic_width[t];
1708 height = pic_height[t];
1712 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1713 pic_xids[picpos] = xid;
1714 pic_yids[picpos] = yid;
1715 pic_width[picpos] = width;
1716 pic_height[picpos] = height;
1719 pic[width*y+x] = buf[0];
1723 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1724 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1728 xid += x*r+x*b*3+x*g*7+x*a*11;
1729 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1731 for(t=0;t<picpos;t++)
1733 if(pic_xids[t] == xid &&
1734 pic_yids[t] == yid) {
1743 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1745 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1747 msg("<trace> swfdevice: %s=%s", name, value);
1748 if(!strcmp(name, "jpegsubpixels")) {
1749 i->config_jpegsubpixels = atof(value);
1750 } else if(!strcmp(name, "ppmsubpixels")) {
1751 i->config_ppmsubpixels = atof(value);
1752 } else if(!strcmp(name, "subpixels")) {
1753 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1754 } else if(!strcmp(name, "drawonlyshapes")) {
1755 i->config_drawonlyshapes = atoi(value);
1756 } else if(!strcmp(name, "ignoredraworder")) {
1757 i->config_ignoredraworder = atoi(value);
1758 } else if(!strcmp(name, "mark")) {
1759 if(!value || !value[0]) {
1760 if(i->mark) free(i->mark);
1764 i->mark = strdup("...");
1765 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1767 } else if(!strcmp(name, "filloverlap")) {
1768 i->config_filloverlap = atoi(value);
1769 } else if(!strcmp(name, "linksopennewwindow")) {
1770 i->config_opennewwindow = atoi(value);
1771 } else if(!strcmp(name, "opennewwindow")) {
1772 i->config_opennewwindow = atoi(value);
1773 } else if(!strcmp(name, "storeallcharacters")) {
1774 i->config_storeallcharacters = atoi(value);
1775 } else if(!strcmp(name, "enablezlib")) {
1776 i->config_enablezlib = atoi(value);
1777 } else if(!strcmp(name, "bboxvars")) {
1778 i->config_bboxvars = atoi(value);
1779 } else if(!strcmp(name, "showclipshapes")) {
1780 i->config_showclipshapes = atoi(value);
1781 } else if(!strcmp(name, "reordertags")) {
1782 i->config_reordertags = atoi(value);
1783 } else if(!strcmp(name, "internallinkfunction")) {
1784 i->config_internallinkfunction = strdup(value);
1785 } else if(!strcmp(name, "externallinkfunction")) {
1786 i->config_externallinkfunction = strdup(value);
1787 } else if(!strcmp(name, "linkfunction")) { //sets both internallinkfunction and externallinkfunction
1788 i->config_internallinkfunction = strdup(value);
1789 i->config_externallinkfunction = strdup(value);
1790 } else if(!strcmp(name, "disable_polygon_conversion")) {
1791 i->config_disable_polygon_conversion = atoi(value);
1792 } else if(!strcmp(name, "normalize_polygon_positions")) {
1793 i->config_normalize_polygon_positions = atoi(value);
1794 } else if(!strcmp(name, "insertstop")) {
1795 i->config_insertstoptag = atoi(value);
1796 } else if(!strcmp(name, "protect")) {
1797 i->config_protect = atoi(value);
1798 if(i->config_protect && i->tag) {
1799 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1801 } else if(!strcmp(name, "faketags")) {
1802 i->config_generate_fake_tags = atoi(value);
1803 } else if(!strcmp(name, "flashversion")) {
1804 i->config_flashversion = atoi(value);
1806 i->swf->fileVersion = i->config_flashversion;
1808 } else if(!strcmp(name, "framerate")) {
1809 i->config_framerate = atoi(value);
1811 i->swf->frameRate = i->config_framerate*0x100;
1813 } else if(!strcmp(name, "minlinewidth")) {
1814 i->config_minlinewidth = atof(value);
1815 } else if(!strcmp(name, "caplinewidth")) {
1816 i->config_caplinewidth = atof(value);
1817 } else if(!strcmp(name, "linktarget")) {
1818 i->config_linktarget = strdup(value);
1819 } else if(!strcmp(name, "dumpfonts")) {
1820 i->config_dumpfonts = atoi(value);
1821 } else if(!strcmp(name, "animate")) {
1822 i->config_animate = atoi(value);
1823 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1825 } else if(!strcmp(name, "jpegquality")) {
1826 int val = atoi(value);
1828 if(val>101) val=101;
1829 i->config_jpegquality = val;
1830 } else if(!strcmp(name, "splinequality")) {
1831 int v = atoi(value);
1832 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1834 i->config_splinemaxerror = v;
1835 } else if(!strcmp(name, "fontquality")) {
1836 int v = atoi(value);
1837 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1839 i->config_fontsplinemaxerror = v;
1840 } else if(!strcmp(name, "linkcolor")) {
1841 if(strlen(value)!=8) {
1842 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1845 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1846 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1847 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1848 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1849 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1850 printf("%02x%02x%02x%02x\n",
1851 i->config_linkcolor.r,
1852 i->config_linkcolor.g,
1853 i->config_linkcolor.b,
1854 i->config_linkcolor.a);
1856 } else if(!strcmp(name, "help")) {
1857 printf("\nSWF layer options:\n");
1858 printf("jpegdpi=<dpi> resolution adjustment for jpeg images\n");
1859 printf("jpegsubpixels=<pixels> resolution adjustment for jpeg images (same as jpegdpi, but in pixels)\n");
1860 printf("ppmdpi=<dpi> resolution adjustment for lossless images\n");
1861 printf("ppmsubpixels=<pixels resolution adjustment for lossless images (same as ppmdpi, but in pixels)\n");
1862 printf("subpixels=<pixels> shortcut for setting both jpegsubpixels and ppmsubpixels\n");
1863 printf("drawonlyshapes convert everything to shapes (currently broken)\n");
1864 printf("ignoredraworder allow to perform a few optimizations for creating smaller SWFs\n");
1865 printf("linksopennewwindow make links open a new browser window\n");
1866 printf("linktarget target window name of new links\n");
1867 printf("linkcolor=<color) color of links (format: RRGGBBAA)\n");
1868 printf("storeallcharacters don't reduce the fonts to used characters in the output file\n");
1869 printf("enablezlib switch on zlib compression (also done if flashversion>=7)\n");
1870 printf("bboxvars store the bounding box of the SWF file in actionscript variables\n");
1871 printf("reordertags=0/1 (default: 1) perform some tag optimizations\n");
1872 printf("internallinkfunction=<name> when the user clicks a internal link (to a different page) in the converted file, this actionscript function is called\n");
1873 printf("externallinkfunction=<name> when the user clicks an external link (e.g. http://www.foo.bar/) on the converted file, this actionscript function is called\n");
1874 printf("disable_polygon_conversion never convert strokes to polygons (will remove capstyles and joint styles)\n");
1875 printf("caplinewidth=<width> the minimum thichness a line needs to have so that capstyles become visible (and are converted)\n");
1876 printf("insertstop put an ActionScript \"STOP\" tag in every frame\n");
1877 printf("protect add a \"protect\" tag to the file, to prevent loading in the Flash editor\n");
1878 printf("flashversion=<version> the SWF fileversion (6)\n");
1879 printf("minlinewidth=<width> convert horizontal/vertical boxes smaller than this width to lines (0.05) \n");
1880 printf("animate insert a showframe tag after each placeobject (animate draw order of PDF files)\n");
1881 printf("jpegquality=<quality> set compression quality of jpeg images\n");
1888 // --------------------------------------------------------------------
1890 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1893 swf_GetCXForm(0, &cx, 1);
1896 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1897 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1898 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1899 c->ar!=0 || c->ag!=0 || c->ab!=0)
1900 msg("<warning> CXForm not SWF-compatible");
1902 cx.a0 = (S16)(c->aa*256);
1903 cx.r0 = (S16)(c->rr*256);
1904 cx.g0 = (S16)(c->gg*256);
1905 cx.b0 = (S16)(c->bb*256);
1914 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1918 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1922 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1924 gfxdevice_t*dev = i->dev;
1926 RGBA*mem = (RGBA*)img->data;
1928 int sizex = img->width;
1929 int sizey = img->height;
1930 int is_jpeg = i->jpeg;
1933 int newsizex=sizex, newsizey=sizey;
1936 if(is_jpeg && i->config_jpegsubpixels) {
1937 newsizex = (int)(targetwidth*i->config_jpegsubpixels + 0.5);
1938 newsizey = (int)(targetheight*i->config_jpegsubpixels + 0.5);
1939 } else if(!is_jpeg && i->config_ppmsubpixels) {
1940 newsizex = (int)(targetwidth*i->config_ppmsubpixels + 0.5);
1941 newsizey = (int)(targetheight*i->config_ppmsubpixels + 0.5);
1945 if(sizex<=0 || sizey<=0)
1952 /* TODO: cache images */
1954 if(newsizex<sizex || newsizey<sizey) {
1955 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1956 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1957 *newwidth = sizex = newsizex;
1958 *newheight = sizey = newsizey;
1961 *newwidth = newsizex = sizex;
1962 *newheight = newsizey = sizey;
1965 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1966 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1968 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1970 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1971 is_jpeg?"jpeg-":"", i->currentswfid+1,
1973 targetwidth, targetheight,
1974 /*newsizex, newsizey,*/
1975 num_colors>256?">":"", num_colors>256?256:num_colors);
1977 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1978 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1980 for(t=0;t<num_colors;t++) {
1981 printf("%02x%02x%02x%02x ",
1982 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1989 int cacheid = imageInCache(dev, mem, sizex, sizey);
1992 bitid = getNewID(dev);
1993 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1994 addImageToCache(dev, mem, sizex, sizey);
2004 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2006 gfxbbox_t bbox = gfxline_getbbox(line);
2008 r.xmin = (int)(bbox.xmin*20);
2009 r.ymin = (int)(bbox.ymin*20);
2010 r.xmax = (int)(bbox.xmax*20);
2011 r.ymax = (int)(bbox.ymax*20);
2015 int line_is_empty(gfxline_t*line)
2018 if(line->type != gfx_moveTo)
2025 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2027 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2029 if(line_is_empty(line))
2035 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2036 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2038 int newwidth=0,newheight=0;
2039 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2042 double fx = (double)img->width / (double)newwidth;
2043 double fy = (double)img->height / (double)newheight;
2048 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2049 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2050 m.tx = (int)(matrix->tx*20);
2051 m.ty = (int)(matrix->ty*20);
2054 int myshapeid = getNewID(dev);
2055 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2057 swf_ShapeNew(&shape);
2058 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2059 swf_SetU16(i->tag, myshapeid);
2060 SRECT r = gfxline_getSWFbbox(line);
2061 swf_SetRect(i->tag,&r);
2062 swf_SetShapeStyles(i->tag,shape);
2063 swf_ShapeCountBits(shape,NULL,NULL);
2064 swf_SetShapeBits(i->tag,shape);
2065 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2066 i->swflastx = i->swflasty = UNDEFINED_COORD;
2067 drawgfxline(dev, line);
2068 swf_ShapeSetEnd(i->tag);
2069 swf_ShapeFree(shape);
2071 msg("<trace> Placing bitmap ID %d", myshapeid);
2072 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2073 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2074 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2077 static RGBA col_black = {255,0,0,0};
2079 static void drawoutline(gfxdevice_t*dev, gfxline_t*line)
2081 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2083 int myshapeid = getNewID(dev);
2084 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2087 swf_ShapeNew(&shape);
2088 int lsid = swf_ShapeAddLineStyle(shape,1,&col_black);
2090 swf_SetU16(i->tag,myshapeid);
2091 SRECT r = gfxline_getSWFbbox(line);
2092 swf_SetRect(i->tag,&r);
2093 swf_SetShapeStyles(i->tag,shape);
2094 swf_ShapeCountBits(shape,NULL,NULL);
2095 swf_SetShapeBits(i->tag,shape);
2096 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,lsid,0,0);
2097 drawgfxline(dev, line);
2098 swf_ShapeSetEnd(i->tag);
2099 swf_ShapeFree(shape);
2101 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2102 swf_ObjectPlace(i->tag, myshapeid, getNewDepth(dev), 0,0,0);
2105 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2107 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2112 if(i->clippos >= 127)
2114 msg("<warning> Too many clip levels.");
2118 if(i->config_showclipshapes)
2119 drawoutline(dev, line);
2121 int myshapeid = getNewID(dev);
2122 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2124 memset(&col, 0, sizeof(RGBA));
2127 swf_ShapeNew(&shape);
2128 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2130 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2131 swf_ShapeAddSolidFillStyle(shape,&markcol);
2133 swf_SetU16(i->tag,myshapeid);
2134 SRECT r = gfxline_getSWFbbox(line);
2135 swf_SetRect(i->tag,&r);
2136 swf_SetShapeStyles(i->tag,shape);
2137 swf_ShapeCountBits(shape,NULL,NULL);
2138 swf_SetShapeBits(i->tag,shape);
2139 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2140 i->swflastx = i->swflasty = UNDEFINED_COORD;
2141 i->shapeisempty = 1;
2142 drawgfxline(dev, line);
2143 if(i->shapeisempty) {
2144 /* an empty clip shape is equivalent to a shape with no area */
2145 int x = line?line->x:0;
2146 int y = line?line->y:0;
2147 moveto(dev, i->tag, x,y);
2148 lineto(dev, i->tag, x,y);
2149 lineto(dev, i->tag, x,y);
2151 swf_ShapeSetEnd(i->tag);
2152 swf_ShapeFree(shape);
2154 /* TODO: remember the bbox, and check all shapes against it */
2156 msg("<trace> Placing clip ID %d", myshapeid);
2157 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2158 i->cliptags[i->clippos] = i->tag;
2159 i->clipshapes[i->clippos] = myshapeid;
2160 i->clipdepths[i->clippos] = getNewDepth(dev);
2164 static void swf_endclip(gfxdevice_t*dev)
2166 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2173 msg("<error> Invalid end of clipping region");
2177 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2178 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2180 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2182 static int gfxline_type(gfxline_t*line)
2188 int haszerosegments=0;
2191 if(line->type == gfx_moveTo) {
2194 } else if(line->type == gfx_lineTo) {
2198 } else if(line->type == gfx_splineTo) {
2200 if(tmpsplines>lines)
2208 if(lines==0 && splines==0) return 0;
2209 else if(lines==1 && splines==0) return 1;
2210 else if(lines==0 && splines==1) return 2;
2211 else if(splines==0) return 3;
2215 static int gfxline_has_dots(gfxline_t*line)
2223 if(line->type == gfx_moveTo) {
2224 /* test the length of the preceding line, and assume it is a dot if
2225 it's length is less than 1.0. But *only* if there's a noticable
2226 gap between the previous line and the next moveTo. (I've come
2227 across a PDF where thousands of "dots" were stringed together,
2229 int last_short_gap = short_gap;
2230 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2235 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2240 } else if(line->type == gfx_lineTo) {
2241 dist += fabs(line->x - x) + fabs(line->y - y);
2243 } else if(line->type == gfx_splineTo) {
2244 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2245 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2252 if(isline && dist < 1 && !short_gap) {
2258 static int gfxline_fix_short_edges(gfxline_t*line)
2262 if(line->type == gfx_lineTo) {
2263 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2266 } else if(line->type == gfx_splineTo) {
2267 if(fabs(line->sx - x) + fabs(line->sy - y) +
2268 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2279 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2281 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2282 if(x<i->min_x || x>i->max_x) return 0;
2283 if(y<i->min_y || y>i->max_y) return 0;
2287 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2289 gfxline_t*l = line = gfxline_clone(line);
2301 //#define NORMALIZE_POLYGON_POSITIONS
2303 static void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
2305 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2306 if(line_is_empty(line))
2308 int type = gfxline_type(line);
2309 int has_dots = gfxline_has_dots(line);
2310 gfxbbox_t r = gfxline_getbbox(line);
2311 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2313 /* TODO: * split line into segments, and perform this check for all segments */
2315 if(i->config_disable_polygon_conversion || type>=5 ||
2317 (width <= i->config_caplinewidth
2318 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2319 || (cap_style == gfx_capRound && type<=2)))) {} else
2321 /* convert line to polygon */
2322 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2324 gfxline_fix_short_edges(line);
2325 /* we need to convert the line into a polygon */
2326 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2327 gfxline_t*gfxline = SVPtogfxline(svp);
2328 dev->fill(dev, gfxline, color);
2334 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2337 if(i->config_normalize_polygon_positions) {
2339 double startx = 0, starty = 0;
2340 if(line && line->type == gfx_moveTo) {
2344 line = gfxline_move(line, -startx, -starty);
2345 i->shapeposx = (int)(startx*20);
2346 i->shapeposy = (int)(starty*20);
2349 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2350 swfoutput_setlinewidth(dev, width);
2353 drawgfxline(dev, line);
2355 if(i->config_normalize_polygon_positions) {
2356 free(line); //account for _move
2360 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2362 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2363 if(line_is_empty(line))
2367 gfxbbox_t r = gfxline_getbbox(line);
2368 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2370 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a) {
2373 if(!i->config_ignoredraworder)
2376 if(i->config_normalize_polygon_positions) {
2378 double startx = 0, starty = 0;
2379 if(line && line->type == gfx_moveTo) {
2383 line = gfxline_move(line, -startx, -starty);
2384 i->shapeposx = (int)(startx*20);
2385 i->shapeposy = (int)(starty*20);
2388 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2392 drawgfxline(dev, line);
2393 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2395 if(i->config_normalize_polygon_positions) {
2396 free(line); //account for _move
2399 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2401 if(line_is_empty(line))
2403 msg("<error> Gradient filling not implemented yet");
2406 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
2408 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2410 SRECT bounds = {0,0,0,0};
2412 swffont->version = 2;
2413 swffont->name = (U8*)strdup(id);
2414 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2415 swffont->layout->ascent = 0; /* ? */
2416 swffont->layout->descent = 0;
2417 swffont->layout->leading = 0;
2418 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2419 swffont->encoding = FONT_ENCODING_UNICODE;
2420 swffont->numchars = font->num_glyphs;
2421 swffont->maxascii = font->max_unicode;
2422 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2423 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2424 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2425 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2426 for(t=0;t<font->max_unicode;t++) {
2427 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2429 for(t=0;t<font->num_glyphs;t++) {
2433 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2434 if(swffont->glyph2ascii[t] == 0xffff || swffont->glyph2ascii[t] == 0x0000) {
2435 /* flash 8 flashtype requires unique unicode IDs for each character.
2436 We use the Unicode private user area to assign characters, hoping that
2437 the font doesn't contain more than 2048 glyphs */
2438 swffont->glyph2ascii[t] = 0xe000 + (t&0x1fff);
2441 if(font->glyphs[t].name) {
2442 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2444 swffont->glyphnames[t] = 0;
2446 advance = (int)(font->glyphs[t].advance);
2448 swf_Shape01DrawerInit(&draw, 0);
2449 line = font->glyphs[t].line;
2452 c.x = line->sx * GLYPH_SCALE; c.y = line->sy * GLYPH_SCALE;
2453 to.x = line->x * GLYPH_SCALE; to.y = line->y * GLYPH_SCALE;
2454 if(line->type == gfx_moveTo) {
2455 draw.moveTo(&draw, &to);
2456 } else if(line->type == gfx_lineTo) {
2457 draw.lineTo(&draw, &to);
2458 } else if(line->type == gfx_splineTo) {
2459 draw.splineTo(&draw, &c, &to);
2464 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2465 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2467 int xmax = swffont->layout->bounds[t].xmax / 20;
2468 if(xmax>0 && xmax*2 < advance) {
2469 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", xmax, advance, font->glyphs[t].advance);
2474 swffont->glyph[t].advance = advance;
2476 swffont->glyph[t].advance = 32767;
2479 draw.dealloc(&draw);
2481 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2483 if(bounds.ymin < 0 && bounds.ymax > 0) {
2484 swffont->layout->ascent = -bounds.ymin;
2485 swffont->layout->descent = bounds.ymax;
2486 swffont->layout->leading = bounds.ymax - bounds.ymin;
2488 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2489 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2490 swffont->layout->leading = bounds.ymax - bounds.ymin;
2496 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2498 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2500 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2501 return; // the requested font is the current font
2503 fontlist_t*last=0,*l = i->fontlist;
2506 if(!strcmp((char*)l->swffont->name, font->id)) {
2507 return; // we already know this font
2511 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2512 l->swffont = gfxfont_to_swffont(font, font->id);
2519 swf_FontSetID(l->swffont, getNewID(i->dev));
2521 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2523 // print font information
2524 msg("<debug> Font %s",font->id);
2525 msg("<debug> | ID: %d", l->swffont->id);
2526 msg("<debug> | Version: %d", l->swffont->version);
2527 msg("<debug> | Name: %s", l->swffont->name);
2528 msg("<debug> | Numchars: %d", l->swffont->numchars);
2529 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2530 msg("<debug> | Style: %d", l->swffont->style);
2531 msg("<debug> | Encoding: %d", l->swffont->encoding);
2532 for(iii=0; iii<l->swffont->numchars;iii++) {
2533 msg("<debug> | Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, l->swffont->glyphnames?l->swffont->glyphnames[iii]:"<nonames>", l->swffont->glyph2ascii[iii], l->swffont->glyph[iii].shape->bitlen,
2534 l->swffont->layout->bounds[iii].xmin/20.0,
2535 l->swffont->layout->bounds[iii].ymin/20.0,
2536 l->swffont->layout->bounds[iii].xmax/20.0,
2537 l->swffont->layout->bounds[iii].ymax/20.0
2540 for(t=0;t<l->swffont->maxascii;t++) {
2541 if(l->swffont->ascii2glyph[t] == iii)
2542 msg("<debug> | - maps to %d",t);
2548 static void swf_switchfont(gfxdevice_t*dev, const char*fontid)
2550 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2552 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2553 return; // the requested font is the current font
2555 fontlist_t*l = i->fontlist;
2557 if(!strcmp((char*)l->swffont->name, fontid)) {
2558 i->swffont = l->swffont;
2563 msg("<error> Unknown font id: %s", fontid);
2567 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2569 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2573 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2575 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2576 with multiple fonts */
2579 swf_switchfont(dev, font->id); // set the current font
2581 setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2583 /* printf("%f %f\n", m.m31, m.m32);
2585 static int xpos = 40;
2586 static int ypos = 200;
2593 drawchar(dev, i->swffont, glyph, matrix->tx, matrix->ty, color);