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"
37 #include "../rfxswf.h"
38 #include "../gfxdevice.h"
39 #include "../gfxtools.h"
40 #include "../art/libart.h"
42 #include "artsutils.h"
44 #define CHARDATAMAX 8192
48 typedef struct _chardata {
50 int fontid; /* TODO: use a SWFFONT instead */
57 typedef struct _fontlist
60 struct _fontlist*next;
63 typedef long int twip;
65 typedef struct _swfmatrix {
66 double m11,m12,m21,m22,m31,m32;
69 typedef struct _swfoutput_internal
71 gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
73 double config_dumpfonts;
74 double config_ppmsubpixels;
75 double config_jpegsubpixels;
76 int config_opennewwindow;
77 int config_ignoredraworder;
78 int config_drawonlyshapes;
79 int config_jpegquality;
80 int config_storeallcharacters;
81 int config_generate_fake_tags;
82 int config_enablezlib;
83 int config_insertstoptag;
84 int config_flashversion;
85 int config_reordertags;
86 int config_showclipshapes;
87 int config_splinemaxerror;
88 int config_fontsplinemaxerror;
89 int config_filloverlap;
92 int config_disable_polygon_conversion;
93 int config_normalize_polygon_positions;
94 RGBA config_linkcolor;
95 float config_minlinewidth;
96 double config_caplinewidth;
97 char* config_linktarget;
98 char*config_internallinkfunction;
99 char*config_externallinkfunction;
101 double config_framerate;
105 fontlist_t* fontlist;
140 int pic_height[1024];
146 char fillstylechanged;
148 int jpeg; //next image type
153 chardata_t chardata[CHARDATAMAX];
160 int current_font_size;
162 double lastfontm11,lastfontm12,lastfontm21,lastfontm22;
173 } swfoutput_internal;
175 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
176 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
177 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);
178 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
179 static void swf_endclip(gfxdevice_t*dev);
180 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);
181 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
182 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
183 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
184 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
185 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
186 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action);
187 static void swf_startframe(gfxdevice_t*dev, int width, int height);
188 static void swf_endframe(gfxdevice_t*dev);
189 static gfxresult_t* swf_finish(gfxdevice_t*driver);
191 static swfoutput_internal* init_internal_struct()
193 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
194 memset(i, 0, sizeof(swfoutput_internal));
218 i->fillstylechanged = 0;
225 i->config_dumpfonts=0;
226 i->config_ppmsubpixels=0;
227 i->config_jpegsubpixels=0;
228 i->config_opennewwindow=1;
229 i->config_ignoredraworder=0;
230 i->config_drawonlyshapes=0;
231 i->config_jpegquality=85;
232 i->config_storeallcharacters=0;
233 i->config_enablezlib=0;
234 i->config_generate_fake_tags=0;
235 i->config_insertstoptag=0;
236 i->config_flashversion=6;
237 i->config_framerate=0.25;
238 i->config_splinemaxerror=1;
239 i->config_fontsplinemaxerror=1;
240 i->config_filloverlap=0;
242 i->config_bboxvars=0;
243 i->config_showclipshapes=0;
244 i->config_minlinewidth=0.05;
245 i->config_caplinewidth=1;
246 i->config_linktarget=0;
247 i->config_internallinkfunction=0;
248 i->config_externallinkfunction=0;
249 i->config_reordertags=1;
251 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
252 i->config_linkcolor.a = 0x40;
257 static int id_error = 0;
259 static U16 getNewID(gfxdevice_t* dev)
261 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
262 if(i->currentswfid == 65535) {
264 msg("<error> ID Table overflow");
265 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
271 return ++i->currentswfid;
273 static U16 getNewDepth(gfxdevice_t* dev)
275 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
276 if(i->depth == 65535) {
278 msg("<error> Depth Table overflow");
279 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
288 static void startshape(gfxdevice_t* dev);
289 static void starttext(gfxdevice_t* dev);
290 static void endshape(gfxdevice_t* dev);
291 static void endtext(gfxdevice_t* dev);
293 typedef struct _plotxy
298 // write a move-to command into the swf
299 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
301 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
302 int rx = (int)(p0.x*20);
303 int ry = (int)(p0.y*20);
304 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
305 swf_ShapeSetMove (tag, i->shape, rx,ry);
306 i->fillstylechanged = 0;
313 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
315 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
319 return movetoxy(dev, tag, p);
321 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
323 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
329 swf_ExpandRect(&i->bboxrect, p);
331 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
335 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
337 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
338 int width = i->linewidth/4;
342 //swf_ShapeSetLine(tag, i->shape,-width,-width);
343 //swf_ShapeSetLine(tag, i->shape,width*2,0);
344 //swf_ShapeSetLine(tag, i->shape,0,width*2);
345 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
346 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
347 //swf_ShapeSetLine(tag, i->shape,width,width);
350 swf_ShapeSetLine(tag, i->shape,-width,0);
351 swf_ShapeSetLine(tag, i->shape,width,-width);
352 swf_ShapeSetLine(tag, i->shape,width,width);
353 swf_ShapeSetLine(tag, i->shape,-width,width);
354 swf_ShapeSetLine(tag, i->shape,-width,-width);
355 swf_ShapeSetLine(tag, i->shape,width,0);
357 addPointToBBox(dev, x-width ,y-width);
358 addPointToBBox(dev, x+width ,y+width);
361 // write a line-to command into the swf
362 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
364 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
365 int px = (int)(p0.x*20);
366 int py = (int)(p0.y*20);
367 int rx = (px-i->swflastx);
368 int ry = (py-i->swflasty);
370 swf_ShapeSetLine (tag, i->shape, rx,ry);
371 addPointToBBox(dev, i->swflastx,i->swflasty);
372 addPointToBBox(dev, px,py);
373 }/* else if(!i->fill) {
374 // treat lines of length 0 as plots, making them
375 // at least 1 twip wide so Flash will display them
376 plot(dev, i->swflastx, i->swflasty, tag);
383 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
388 linetoxy(dev,tag, p);
391 // write a spline-to command into the swf
392 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
394 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
395 int lastlastx = i->swflastx;
396 int lastlasty = i->swflasty;
398 int cx = ((int)(control.x*20)-i->swflastx);
399 int cy = ((int)(control.y*20)-i->swflasty);
402 int ex = ((int)(end.x*20)-i->swflastx);
403 int ey = ((int)(end.y*20)-i->swflasty);
407 if((cx || cy) && (ex || ey)) {
408 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
409 addPointToBBox(dev, lastlastx ,lastlasty );
410 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
411 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
412 } else if(cx || cy || ex || ey) {
413 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
414 addPointToBBox(dev, lastlastx ,lastlasty );
415 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
416 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
422 /* write a line, given two points and the transformation
424 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
426 moveto(dev, tag, p0);
427 lineto(dev, tag, p1);
430 void resetdrawer(gfxdevice_t*dev)
432 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
437 static void stopFill(gfxdevice_t*dev)
439 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
440 if(i->lastwasfill!=0)
442 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
443 i->fillstylechanged = 1;
447 static void startFill(gfxdevice_t*dev)
449 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
450 if(i->lastwasfill!=1)
452 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
453 i->fillstylechanged = 1;
458 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
470 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
472 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
475 memset(&r, 0, sizeof(r));
478 if(debug) printf("\n");
479 for(t=0;t<i->chardatapos;t++)
481 if(i->chardata[t].fontid != font->id) {
482 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
485 SRECT b = font->layout->bounds[i->chardata[t].charid];
486 b.xmin *= i->chardata[t].size;
487 b.ymin *= i->chardata[t].size;
488 b.xmax *= i->chardata[t].size;
489 b.ymax *= i->chardata[t].size;
491 /* divide by 1024, rounding xmax/ymax up */
492 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
494 b.xmin += i->chardata[t].x;
495 b.ymin += i->chardata[t].y;
496 b.xmax += i->chardata[t].x;
497 b.ymax += i->chardata[t].y;
499 /* until we solve the INTERNAL_SCALING problem (see below)
500 make sure the bounding box is big enough */
506 b = swf_TurnRect(b, m);
508 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
509 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
510 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
511 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
512 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
517 i->chardata[t].fontid,
519 i->chardata[t].charid
521 swf_ExpandRect2(&r, &b);
523 if(debug) printf("-----> (%f,%f,%f,%f)\n",
531 static void putcharacters(gfxdevice_t*dev, TAG*tag)
533 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
537 color.r = i->chardata[0].color.r^255;
546 int charadvance[128];
549 int glyphbits=1; //TODO: can this be zero?
552 if(tag->id != ST_DEFINETEXT &&
553 tag->id != ST_DEFINETEXT2) {
554 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
557 if(!i->chardatapos) {
558 msg("<warning> putcharacters called with zero characters");
561 for(pass = 0; pass < 2; pass++)
571 advancebits++; // add sign bit
572 swf_SetU8(tag, glyphbits);
573 swf_SetU8(tag, advancebits);
576 for(t=0;t<=i->chardatapos;t++)
578 if(lastfontid != i->chardata[t].fontid ||
579 lastx!=i->chardata[t].x ||
580 lasty!=i->chardata[t].y ||
581 !colorcompare(dev,&color, &i->chardata[t].color) ||
583 lastsize != i->chardata[t].size ||
586 if(charstorepos && pass==0)
589 for(s=0;s<charstorepos;s++)
591 while(charids[s]>=(1<<glyphbits))
593 while(charadvance[s]>=(1<<advancebits))
597 if(charstorepos && pass==1)
599 tag->writeBit = 0; // Q&D
600 swf_SetBits(tag, 0, 1); // GLYPH Record
601 swf_SetBits(tag, charstorepos, 7); // number of glyphs
603 for(s=0;s<charstorepos;s++)
605 swf_SetBits(tag, charids[s], glyphbits);
606 swf_SetBits(tag, charadvance[s], advancebits);
611 if(pass == 1 && t<i->chardatapos)
617 if(lastx != i->chardata[t].x ||
618 lasty != i->chardata[t].y)
620 newx = i->chardata[t].x;
621 newy = i->chardata[t].y;
627 if(!colorcompare(dev,&color, &i->chardata[t].color))
629 color = i->chardata[t].color;
632 font.id = i->chardata[t].fontid;
633 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
636 tag->writeBit = 0; // Q&D
637 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
640 lastfontid = i->chardata[t].fontid;
641 lastx = i->chardata[t].x;
642 lasty = i->chardata[t].y;
643 lastsize = i->chardata[t].size;
646 if(t==i->chardatapos)
650 int nextt = t==i->chardatapos-1?t:t+1;
651 int rel = i->chardata[nextt].x-i->chardata[t].x;
652 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
654 lastx=i->chardata[nextt].x;
658 lastx=i->chardata[t].x;
660 charids[charstorepos] = i->chardata[t].charid;
661 charadvance[charstorepos] = advance;
668 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
670 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
671 if(i->chardatapos == CHARDATAMAX)
673 msg("<warning> Character buffer too small. SWF will be slightly bigger");
677 i->chardata[i->chardatapos].fontid = fontid;
678 i->chardata[i->chardatapos].charid = charid;
679 i->chardata[i->chardatapos].x = x;
680 i->chardata[i->chardatapos].y = y;
681 i->chardata[i->chardatapos].color = color;
682 i->chardata[i->chardatapos].size = size;
686 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
687 So if we set this value to high, the char coordinates will overflow.
688 If we set it to low, however, the char positions will be inaccurate */
689 #define GLYPH_SCALE 1
691 /* process a character. */
692 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, float x, float y, gfxcolor_t*col)
694 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
696 msg("<warning> Font is NULL");
700 if(charid<0 || charid>=swffont->numchars) {
701 msg("<warning> No character %d in font %s (%d chars)", charid, FIXNULL((char*)swffont->name), swffont->numchars);
704 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
705 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
706 charid, FIXNULL((char*)swffont->name), swffont->numchars);
715 double det = i->fontmatrix.sx/65536.0 * i->fontmatrix.sy/65536.0 -
716 i->fontmatrix.r0/65536.0 * i->fontmatrix.r1/65536.0;
718 if(fabs(det) < 0.0005) {
719 /* x direction equals y direction- the text is invisible */
722 det = 20 * GLYPH_SCALE / det;
725 p.x = (SCOORD)(( x * i->fontmatrix.sy/65536.0 - y * i->fontmatrix.r1/65536.0)*det);
726 p.y = (SCOORD)((- x * i->fontmatrix.r0/65536.0 + y * i->fontmatrix.sx/65536.0)*det);
728 RGBA rgba = *(RGBA*)col;
729 putcharacter(dev, swffont->id, charid,p.x,p.y,i->current_font_size, rgba);
730 swf_FontUseGlyph(swffont, charid);
734 static void endtext(gfxdevice_t*dev)
736 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
740 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT2);
741 swf_SetU16(i->tag, i->textid);
744 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
746 swf_SetRect(i->tag,&r);
748 swf_SetMatrix(i->tag,&i->fontmatrix);
750 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
752 putcharacters(dev, i->tag);
755 if(i->swf->fileVersion >= 8) {
756 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
757 swf_SetU16(i->tag, i->textid);
759 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
760 swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
761 //swf_SetU8(i->tag, /*no grid*/(0<<3)|/*flashtype*/0x40);
763 swf_SetU32(i->tag, 0);//thickness
764 swf_SetU32(i->tag, 0);//sharpness
765 swf_SetU8(i->tag, 0);//reserved
767 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
769 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
773 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
774 static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22)
780 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
781 if(i->lastfontm11 == m11 &&
782 i->lastfontm12 == m12 &&
783 i->lastfontm21 == m21 &&
784 i->lastfontm22 == m22)
789 i->lastfontm11 = m11;
790 i->lastfontm12 = m12;
791 i->lastfontm21 = m21;
792 i->lastfontm22 = m22;
794 double xsize = sqrt(m11*m11 + m12*m12);
795 double ysize = sqrt(m21*m21 + m22*m22);
796 i->current_font_size = (xsize>ysize?xsize:ysize)*1;
797 if(i->current_font_size < 1)
798 i->current_font_size = 1;
799 double ifs = 1.0 / (i->current_font_size*GLYPH_SCALE);
802 m.sx = (U32)((m11*ifs)*65536); m.r1 = (U32)((m21*ifs)*65536);
803 m.r0 = (U32)((m12*ifs)*65536); m.sy = (U32)((m22*ifs)*65536);
809 static void endpage(gfxdevice_t*dev)
811 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
821 void swf_startframe(gfxdevice_t*dev, int width, int height)
823 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
824 if(!i->firstpage && !i->pagefinished)
826 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
828 swf_GetMatrix(0, &i->page_matrix);
829 i->page_matrix.tx = 0;
830 i->page_matrix.ty = 0;
836 /* set clipping/background rectangle */
837 /* TODO: this should all be done in SWFOutputDev */
838 //setBackground(dev, x1, y1, x2, y2);
840 /* increase SWF's bounding box */
846 swf_ExpandRect2(&i->swf->movieSize, &r);
848 i->lastframeno = i->frameno;
853 void swf_endframe(gfxdevice_t*dev)
855 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
860 if( (i->swf->fileVersion <= 8) && (i->config_insertstoptag) ) {
862 atag = action_Stop(atag);
863 atag = action_End(atag);
864 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
865 swf_ActionSet(i->tag,atag);
867 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
870 for(i->depth;i->depth>i->startdepth;i->depth--) {
871 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
872 swf_SetU16(i->tag,i->depth);
874 i->depth = i->startdepth;
877 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
879 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
881 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
885 int shapeid = getNewID(dev);
890 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
892 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
893 swf_SetU16(i->tag,shapeid);
894 swf_SetRect(i->tag,&r);
895 swf_SetShapeHeader(i->tag,s);
896 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
897 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
898 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
899 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
900 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
901 swf_ShapeSetEnd(i->tag);
903 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
904 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
905 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
906 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
909 /* initialize the swf writer */
910 void gfxdevice_swf_init(gfxdevice_t* dev)
912 memset(dev, 0, sizeof(gfxdevice_t));
916 dev->internal = init_internal_struct();
918 dev->startpage = swf_startframe;
919 dev->endpage = swf_endframe;
920 dev->finish = swf_finish;
921 dev->fillbitmap = swf_fillbitmap;
922 dev->setparameter = swf_setparameter;
923 dev->stroke = swf_stroke;
924 dev->startclip = swf_startclip;
925 dev->endclip = swf_endclip;
926 dev->fill = swf_fill;
927 dev->fillbitmap = swf_fillbitmap;
928 dev->fillgradient = swf_fillgradient;
929 dev->addfont = swf_addfont;
930 dev->drawchar = swf_drawchar;
931 dev->drawlink = swf_drawlink;
933 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
938 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
942 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
943 i->swf->fileVersion = i->config_flashversion;
944 i->swf->frameRate = i->config_framerate*0x100;
945 i->swf->movieSize.xmin = 0;
946 i->swf->movieSize.ymin = 0;
947 i->swf->movieSize.xmax = 0;
948 i->swf->movieSize.ymax = 0;
950 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
951 i->tag = i->swf->firstTag;
952 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
953 swf_SetRGB(i->tag,&rgb);
955 i->startdepth = i->depth = 0;
957 if(i->config_protect)
958 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
961 static void startshape(gfxdevice_t*dev)
963 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
968 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a)
971 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
973 swf_ShapeNew(&i->shape);
974 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
975 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
977 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
978 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
981 i->shapeid = getNewID(dev);
983 msg("<debug> Using shape id %d", i->shapeid);
985 swf_SetU16(i->tag,i->shapeid); // ID
987 i->bboxrectpos = i->tag->len;
991 r.xmax = 20*i->max_x;
992 r.ymax = 20*i->max_y;
993 swf_SetRect(i->tag,&r);
995 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
997 swf_SetShapeStyles(i->tag,i->shape);
998 swf_ShapeCountBits(i->shape,NULL,NULL);
999 swf_SetShapeBits(i->tag,i->shape);
1001 /* TODO: do we really need this? */
1002 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1003 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
1004 i->swflastx=i->swflasty=UNDEFINED_COORD;
1005 i->lastwasfill = -1;
1006 i->shapeisempty = 1;
1009 static void starttext(gfxdevice_t*dev)
1011 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1015 i->textid = getNewID(dev);
1017 i->swflastx=i->swflasty=0;
1021 /* TODO: move to ../lib/rfxswf */
1022 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1024 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1025 /* determine length of old rect */
1029 swf_GetRect(tag, &old);
1030 swf_ResetReadBits(tag);
1031 int pos_end = tag->pos;
1033 int len = tag->len - pos_end;
1034 U8*data = (U8*)malloc(len);
1035 memcpy(data, &tag->data[pos_end], len);
1038 swf_SetRect(tag, newrect);
1039 swf_SetBlock(tag, data, len);
1041 tag->pos = tag->readBit = 0;
1044 void cancelshape(gfxdevice_t*dev)
1046 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1047 /* delete old shape tag */
1049 i->tag = i->tag->prev;
1050 swf_DeleteTag(todel);
1051 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1053 i->bboxrectpos = -1;
1055 // i->currentswfid--; // this was an *exceptionally* bad idea
1058 void fixAreas(gfxdevice_t*dev)
1060 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1061 if(!i->shapeisempty && i->fill &&
1062 (i->bboxrect.xmin == i->bboxrect.xmax ||
1063 i->bboxrect.ymin == i->bboxrect.ymax) &&
1064 i->config_minlinewidth >= 0.001
1066 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1067 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1068 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1071 SRECT r = i->bboxrect;
1073 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1074 /* this thing comes down to a single dot- nothing to fix here */
1080 RGBA save_col = i->strokergb;
1081 int save_width = i->linewidth;
1083 i->strokergb = i->fillrgb;
1084 i->linewidth = (int)(i->config_minlinewidth*20);
1085 if(i->linewidth==0) i->linewidth = 1;
1090 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1091 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1093 i->strokergb = save_col;
1094 i->linewidth = save_width;
1099 static void endshape_noput(gfxdevice_t*dev)
1101 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1104 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1107 swf_ShapeFree(i->shape);
1115 static void endshape(gfxdevice_t*dev)
1117 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1123 if(i->shapeisempty ||
1125 (i->bboxrect.xmin == i->bboxrect.xmax &&
1126 i->bboxrect.ymin == i->bboxrect.ymax))
1128 // delete the shape again, we didn't do anything
1129 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1130 i->bboxrect.xmin /20.0,
1131 i->bboxrect.ymin /20.0,
1132 i->bboxrect.xmax /20.0,
1133 i->bboxrect.ymax /20.0
1139 swf_ShapeSetEnd(i->tag);
1141 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1143 msg("<trace> Placing shape ID %d", i->shapeid);
1145 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1146 MATRIX m = i->page_matrix;
1147 m.tx += i->shapeposx;
1148 m.ty += i->shapeposy;
1149 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1151 if(i->config_animate) {
1152 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1155 swf_ShapeFree(i->shape);
1158 i->bboxrectpos = -1;
1165 void wipeSWF(SWF*swf)
1167 TAG*tag = swf->firstTag;
1169 TAG*next = tag->next;
1170 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1171 tag->id != ST_END &&
1172 tag->id != ST_DOACTION &&
1173 tag->id != ST_SHOWFRAME) {
1180 void swfoutput_finalize(gfxdevice_t*dev)
1182 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1184 if(i->tag && i->tag->id == ST_END)
1185 return; //already done
1187 if(i->config_bboxvars) {
1188 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1190 a = action_PushString(a, "xmin");
1191 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1192 a = action_SetVariable(a);
1193 a = action_PushString(a, "ymin");
1194 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1195 a = action_SetVariable(a);
1196 a = action_PushString(a, "xmax");
1197 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1198 a = action_SetVariable(a);
1199 a = action_PushString(a, "ymax");
1200 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1201 a = action_SetVariable(a);
1202 a = action_PushString(a, "width");
1203 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1204 a = action_SetVariable(a);
1205 a = action_PushString(a, "height");
1206 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1207 a = action_SetVariable(a);
1209 swf_ActionSet(tag, a);
1213 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1217 free(i->mark);i->mark = 0;
1221 fontlist_t *iterator = i->fontlist;
1223 TAG*mtag = i->swf->firstTag;
1224 if(iterator->swffont) {
1225 if(!i->config_storeallcharacters) {
1226 msg("<debug> Reducing font %s", iterator->swffont->name);
1227 swf_FontReduce(iterator->swffont);
1229 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1231 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1232 swf_FontSetDefine2(mtag, iterator->swffont);
1236 iterator = iterator->next;
1238 i->tag = swf_InsertTag(i->tag,ST_END);
1239 TAG* tag = i->tag->prev;
1241 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1242 and the ST_END- they confuse the flash player */
1243 while(tag->id == ST_REMOVEOBJECT2) {
1244 TAG* prev = tag->prev;
1252 if(i->config_enablezlib || i->config_flashversion>=6) {
1253 i->swf->compressed = 1;
1256 /* Initialize AVM2 if it is a Flash9 file */
1257 if(i->config_flashversion>=9 && i->config_insertstoptag) {
1258 AVM2_InsertStops(i->swf);
1260 // if(i->config_reordertags)
1261 // swf_Optimize(i->swf);
1264 int swfresult_save(gfxresult_t*gfx, const char*filename)
1266 SWF*swf = (SWF*)gfx->internal;
1269 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1274 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1278 if(swf->compressed) {
1279 if FAILED(swf_WriteSWC(fi,swf))
1280 msg("<error> WriteSWC() failed.\n");
1282 if FAILED(swf_WriteSWF(fi,swf))
1283 msg("<error> WriteSWF() failed.\n");
1290 void* swfresult_get(gfxresult_t*gfx, const char*name)
1292 SWF*swf = (SWF*)gfx->internal;
1293 if(!strcmp(name, "swf")) {
1294 return (void*)swf_CopySWF(swf);
1295 } else if(!strcmp(name, "xmin")) {
1296 return (void*)(swf->movieSize.xmin/20);
1297 } else if(!strcmp(name, "ymin")) {
1298 return (void*)(swf->movieSize.ymin/20);
1299 } else if(!strcmp(name, "xmax")) {
1300 return (void*)(swf->movieSize.xmax/20);
1301 } else if(!strcmp(name, "ymax")) {
1302 return (void*)(swf->movieSize.ymax/20);
1303 } else if(!strcmp(name, "width")) {
1304 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1305 } else if(!strcmp(name, "height")) {
1306 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1310 void swfresult_destroy(gfxresult_t*gfx)
1313 swf_FreeTags((SWF*)gfx->internal);
1314 free(gfx->internal);
1317 memset(gfx, 0, sizeof(gfxresult_t));
1321 static void swfoutput_destroy(gfxdevice_t* dev);
1323 gfxresult_t* swf_finish(gfxdevice_t* dev)
1325 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1328 if(i->config_linktarget) {
1329 free(i->config_linktarget);
1330 i->config_linktarget = 0;
1333 swfoutput_finalize(dev);
1334 SWF* swf = i->swf;i->swf = 0;
1335 swfoutput_destroy(dev);
1337 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1338 result->internal = swf;
1339 result->save = swfresult_save;
1341 result->get = swfresult_get;
1342 result->destroy = swfresult_destroy;
1346 /* Perform cleaning up */
1347 static void swfoutput_destroy(gfxdevice_t* dev)
1349 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1351 /* not initialized yet- nothing to destroy */
1355 fontlist_t *tmp,*iterator = i->fontlist;
1357 if(iterator->swffont) {
1358 swf_FontFree(iterator->swffont);iterator->swffont=0;
1361 iterator = iterator->next;
1364 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1367 memset(dev, 0, sizeof(gfxdevice_t));
1370 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1372 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1373 if(i->fillrgb.r == r &&
1374 i->fillrgb.g == g &&
1375 i->fillrgb.b == b &&
1376 i->fillrgb.a == a) return;
1386 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1388 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1389 if(i->strokergb.r == r &&
1390 i->strokergb.g == g &&
1391 i->strokergb.b == b &&
1392 i->strokergb.a == a) return;
1402 //#define ROUND_UP 19
1403 //#define ROUND_UP 10
1405 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1407 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1408 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1412 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1416 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1417 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1418 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1419 static void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points);
1421 /*void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1423 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1424 dev->drawlink(dev, points, url);
1427 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, const char*url)
1429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1431 if(!strncmp("http://pdf2swf:", url, 15)) {
1432 char*tmp = strdup(url);
1433 int l = strlen(tmp);
1436 swfoutput_namedlink(dev, tmp+15, points);
1439 } else if(!strncmp("page", url, 4)) {
1442 if(url[t]<'0' || url[t]>'9')
1445 int page = atoi(&url[4]);
1446 if(page<0) page = 0;
1447 swfoutput_linktopage(dev, page, points);
1450 swfoutput_linktourl(dev, url, points);
1453 void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points)
1455 ActionTAG* actions = 0;
1456 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1462 if(i->config_externallinkfunction) {
1463 actions = action_PushString(actions, url); //parameter
1464 actions = action_PushInt(actions, 1); //number of parameters (1)
1465 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1466 actions = action_CallFunction(actions);
1467 } else if(!i->config_linktarget) {
1468 if(!i->config_opennewwindow)
1469 actions = action_GetUrl(actions, url, "_parent");
1471 actions = action_GetUrl(actions, url, "_this");
1473 actions = action_GetUrl(actions, url, i->config_linktarget);
1475 actions = action_End(actions);
1477 drawlink(dev, actions, 0, points,0);
1479 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1481 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1482 ActionTAG* actions = 0;
1489 if(!i->config_internallinkfunction) {
1490 actions = action_GotoFrame(actions, page-1);
1491 actions = action_End(actions);
1493 actions = action_PushInt(actions, page); //parameter
1494 actions = action_PushInt(actions, 1); //number of parameters (1)
1495 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1496 actions = action_CallFunction(actions);
1497 actions = action_End(actions);
1500 drawlink(dev, actions, 0, points,0);
1503 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1504 of the viewer objects, like subtitles, index elements etc.
1506 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1508 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1509 ActionTAG *actions1,*actions2;
1510 char*tmp = strdup(name);
1518 if(!strncmp(tmp, "call:", 5))
1520 char*x = strchr(&tmp[5], ':');
1522 actions1 = action_PushInt(0, 0); //number of parameters (0)
1523 actions1 = action_PushString(actions1, &tmp[5]); //function name
1524 actions1 = action_CallFunction(actions1);
1525 actions1 = action_End(actions1);
1528 actions1 = action_PushString(0, x+1); //parameter
1529 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1530 actions1 = action_PushString(actions1, &tmp[5]); //function name
1531 actions1 = action_CallFunction(actions1);
1532 actions1 = action_End(actions1);
1534 actions2 = action_End(0);
1539 actions1 = action_PushString(0, "/:subtitle");
1540 actions1 = action_PushString(actions1, name);
1541 actions1 = action_SetVariable(actions1);
1542 actions1 = action_End(actions1);
1544 actions2 = action_PushString(0, "/:subtitle");
1545 actions2 = action_PushString(actions2, "");
1546 actions2 = action_SetVariable(actions2);
1547 actions2 = action_End(actions2);
1550 drawlink(dev, actions1, actions2, points,mouseover);
1552 swf_ActionFree(actions1);
1553 swf_ActionFree(actions2);
1557 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1559 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1560 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1562 int lines= 0, splines=0;
1566 /* check whether the next segment is zero */
1567 if(line->type == gfx_moveTo) {
1568 moveto(dev, i->tag, line->x, line->y);
1569 px = lastx = line->x;
1570 py = lasty = line->y;
1572 } if(line->type == gfx_lineTo) {
1573 lineto(dev, i->tag, line->x, line->y);
1578 } else if(line->type == gfx_splineTo) {
1580 s.x = line->sx;p.x = line->x;
1581 s.y = line->sy;p.y = line->y;
1582 splineto(dev, i->tag, s, p);
1590 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1594 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1596 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1605 int buttonid = getNewID(dev);
1606 gfxbbox_t bbox = gfxline_getbbox(points);
1609 myshapeid = getNewID(dev);
1610 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1611 swf_ShapeNew(&i->shape);
1612 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1613 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1614 swf_SetU16(i->tag, myshapeid);
1615 r.xmin = (int)(bbox.xmin*20);
1616 r.ymin = (int)(bbox.ymin*20);
1617 r.xmax = (int)(bbox.xmax*20);
1618 r.ymax = (int)(bbox.ymax*20);
1619 swf_SetRect(i->tag,&r);
1620 swf_SetShapeStyles(i->tag,i->shape);
1621 swf_ShapeCountBits(i->shape,NULL,NULL);
1622 swf_SetShapeBits(i->tag,i->shape);
1623 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1624 i->swflastx = i->swflasty = 0;
1625 drawgfxline(dev, points);
1626 swf_ShapeSetEnd(i->tag);
1629 myshapeid2 = getNewID(dev);
1630 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1631 swf_ShapeNew(&i->shape);
1633 rgb = i->config_linkcolor;
1635 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1636 swf_SetU16(i->tag, myshapeid2);
1637 r.xmin = (int)(bbox.xmin*20);
1638 r.ymin = (int)(bbox.ymin*20);
1639 r.xmax = (int)(bbox.xmax*20);
1640 r.ymax = (int)(bbox.ymax*20);
1641 swf_SetRect(i->tag,&r);
1642 swf_SetShapeStyles(i->tag,i->shape);
1643 swf_ShapeCountBits(i->shape,NULL,NULL);
1644 swf_SetShapeBits(i->tag,i->shape);
1645 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1646 i->swflastx = i->swflasty = 0;
1647 drawgfxline(dev, points);
1648 swf_ShapeSetEnd(i->tag);
1652 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1653 swf_SetU16(i->tag,buttonid); //id
1654 swf_ButtonSetFlags(i->tag, 0); //menu=no
1655 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1656 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1657 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1658 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1659 swf_SetU8(i->tag,0);
1660 swf_ActionSet(i->tag,actions1);
1661 swf_SetU8(i->tag,0);
1665 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1666 swf_SetU16(i->tag,buttonid); //id
1667 swf_ButtonSetFlags(i->tag, 0); //menu=no
1668 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1669 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1670 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1671 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1672 swf_SetU8(i->tag,0); // end of button records
1673 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1674 swf_ActionSet(i->tag,actions1);
1676 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1677 swf_ActionSet(i->tag,actions2);
1678 swf_SetU8(i->tag,0);
1679 swf_ButtonPostProcess(i->tag, 2);
1681 swf_SetU8(i->tag,0);
1682 swf_ButtonPostProcess(i->tag, 1);
1686 msg("<trace> Placing link ID %d", buttonid);
1687 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1689 if(posx!=0 || posy!=0) {
1691 p.x = (int)(posx*20);
1692 p.y = (int)(posy*20);
1693 p = swf_TurnPoint(p, &i->page_matrix);
1698 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1700 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1707 for(t=0;t<picpos;t++)
1709 if(pic_xids[t] == xid &&
1710 pic_yids[t] == yid) {
1711 width = pic_width[t];
1712 height = pic_height[t];
1716 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1717 pic_xids[picpos] = xid;
1718 pic_yids[picpos] = yid;
1719 pic_width[picpos] = width;
1720 pic_height[picpos] = height;
1723 pic[width*y+x] = buf[0];
1727 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1728 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1732 xid += x*r+x*b*3+x*g*7+x*a*11;
1733 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1735 for(t=0;t<picpos;t++)
1737 if(pic_xids[t] == xid &&
1738 pic_yids[t] == yid) {
1747 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1749 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1751 msg("<trace> swfdevice: %s=%s", name, value);
1752 if(!strcmp(name, "jpegsubpixels")) {
1753 i->config_jpegsubpixels = atof(value);
1754 } else if(!strcmp(name, "ppmsubpixels")) {
1755 i->config_ppmsubpixels = atof(value);
1756 } else if(!strcmp(name, "subpixels")) {
1757 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1758 } else if(!strcmp(name, "drawonlyshapes")) {
1759 i->config_drawonlyshapes = atoi(value);
1760 } else if(!strcmp(name, "ignoredraworder")) {
1761 i->config_ignoredraworder = atoi(value);
1762 } else if(!strcmp(name, "mark")) {
1763 if(!value || !value[0]) {
1764 if(i->mark) free(i->mark);
1768 i->mark = strdup("...");
1769 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1771 } else if(!strcmp(name, "filloverlap")) {
1772 i->config_filloverlap = atoi(value);
1773 } else if(!strcmp(name, "linksopennewwindow")) {
1774 i->config_opennewwindow = atoi(value);
1775 } else if(!strcmp(name, "opennewwindow")) {
1776 i->config_opennewwindow = atoi(value);
1777 } else if(!strcmp(name, "storeallcharacters")) {
1778 i->config_storeallcharacters = atoi(value);
1779 } else if(!strcmp(name, "enablezlib")) {
1780 i->config_enablezlib = atoi(value);
1781 } else if(!strcmp(name, "bboxvars")) {
1782 i->config_bboxvars = atoi(value);
1783 } else if(!strcmp(name, "showclipshapes")) {
1784 i->config_showclipshapes = atoi(value);
1785 } else if(!strcmp(name, "reordertags")) {
1786 i->config_reordertags = atoi(value);
1787 } else if(!strcmp(name, "internallinkfunction")) {
1788 i->config_internallinkfunction = strdup(value);
1789 } else if(!strcmp(name, "externallinkfunction")) {
1790 i->config_externallinkfunction = strdup(value);
1791 } else if(!strcmp(name, "linkfunction")) { //sets both internallinkfunction and externallinkfunction
1792 i->config_internallinkfunction = strdup(value);
1793 i->config_externallinkfunction = strdup(value);
1794 } else if(!strcmp(name, "disable_polygon_conversion")) {
1795 i->config_disable_polygon_conversion = atoi(value);
1796 } else if(!strcmp(name, "normalize_polygon_positions")) {
1797 i->config_normalize_polygon_positions = atoi(value);
1798 } else if(!strcmp(name, "insertstop")) {
1799 i->config_insertstoptag = atoi(value);
1800 } else if(!strcmp(name, "protect")) {
1801 i->config_protect = atoi(value);
1802 if(i->config_protect && i->tag) {
1803 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1805 } else if(!strcmp(name, "faketags")) {
1806 i->config_generate_fake_tags = atoi(value);
1807 } else if(!strcmp(name, "flashversion")) {
1808 i->config_flashversion = atoi(value);
1810 i->swf->fileVersion = i->config_flashversion;
1812 } else if(!strcmp(name, "framerate")) {
1813 i->config_framerate = atoi(value);
1815 i->swf->frameRate = i->config_framerate*0x100;
1817 } else if(!strcmp(name, "minlinewidth")) {
1818 i->config_minlinewidth = atof(value);
1819 } else if(!strcmp(name, "caplinewidth")) {
1820 i->config_caplinewidth = atof(value);
1821 } else if(!strcmp(name, "linktarget")) {
1822 i->config_linktarget = strdup(value);
1823 } else if(!strcmp(name, "dumpfonts")) {
1824 i->config_dumpfonts = atoi(value);
1825 } else if(!strcmp(name, "animate")) {
1826 i->config_animate = atoi(value);
1827 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1829 } else if(!strcmp(name, "jpegquality")) {
1830 int val = atoi(value);
1832 if(val>101) val=101;
1833 i->config_jpegquality = val;
1834 } else if(!strcmp(name, "splinequality")) {
1835 int v = atoi(value);
1836 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1838 i->config_splinemaxerror = v;
1839 } else if(!strcmp(name, "fontquality")) {
1840 int v = atoi(value);
1841 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1843 i->config_fontsplinemaxerror = v;
1844 } else if(!strcmp(name, "linkcolor")) {
1845 if(strlen(value)!=8) {
1846 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1849 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1850 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1851 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1852 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1853 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1854 printf("%02x%02x%02x%02x\n",
1855 i->config_linkcolor.r,
1856 i->config_linkcolor.g,
1857 i->config_linkcolor.b,
1858 i->config_linkcolor.a);
1860 } else if(!strcmp(name, "help")) {
1861 printf("\nSWF layer options:\n");
1862 printf("jpegdpi=<dpi> resolution adjustment for jpeg images\n");
1863 printf("jpegsubpixels=<pixels> resolution adjustment for jpeg images (same as jpegdpi, but in pixels)\n");
1864 printf("ppmdpi=<dpi> resolution adjustment for lossless images\n");
1865 printf("ppmsubpixels=<pixels resolution adjustment for lossless images (same as ppmdpi, but in pixels)\n");
1866 printf("subpixels=<pixels> shortcut for setting both jpegsubpixels and ppmsubpixels\n");
1867 printf("drawonlyshapes convert everything to shapes (currently broken)\n");
1868 printf("ignoredraworder allow to perform a few optimizations for creating smaller SWFs\n");
1869 printf("linksopennewwindow make links open a new browser window\n");
1870 printf("linktarget target window name of new links\n");
1871 printf("linkcolor=<color) color of links (format: RRGGBBAA)\n");
1872 printf("storeallcharacters don't reduce the fonts to used characters in the output file\n");
1873 printf("enablezlib switch on zlib compression (also done if flashversion>=7)\n");
1874 printf("bboxvars store the bounding box of the SWF file in actionscript variables\n");
1875 printf("reordertags=0/1 (default: 1) perform some tag optimizations\n");
1876 printf("internallinkfunction=<name> when the user clicks a internal link (to a different page) in the converted file, this actionscript function is called\n");
1877 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");
1878 printf("disable_polygon_conversion never convert strokes to polygons (will remove capstyles and joint styles)\n");
1879 printf("caplinewidth=<width> the minimum thichness a line needs to have so that capstyles become visible (and are converted)\n");
1880 printf("insertstop put an ActionScript \"STOP\" tag in every frame\n");
1881 printf("protect add a \"protect\" tag to the file, to prevent loading in the Flash editor\n");
1882 printf("flashversion=<version> the SWF fileversion (6)\n");
1883 printf("minlinewidth=<width> convert horizontal/vertical boxes smaller than this width to lines (0.05) \n");
1884 printf("animate insert a showframe tag after each placeobject (animate draw order of PDF files)\n");
1885 printf("jpegquality=<quality> set compression quality of jpeg images\n");
1892 // --------------------------------------------------------------------
1894 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1897 swf_GetCXForm(0, &cx, 1);
1900 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1901 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1902 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1903 c->ar!=0 || c->ag!=0 || c->ab!=0)
1904 msg("<warning> CXForm not SWF-compatible");
1906 cx.a0 = (S16)(c->aa*256);
1907 cx.r0 = (S16)(c->rr*256);
1908 cx.g0 = (S16)(c->gg*256);
1909 cx.b0 = (S16)(c->bb*256);
1918 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1922 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1926 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1928 gfxdevice_t*dev = i->dev;
1930 RGBA*mem = (RGBA*)img->data;
1932 int sizex = img->width;
1933 int sizey = img->height;
1934 int is_jpeg = i->jpeg;
1937 int newsizex=sizex, newsizey=sizey;
1940 if(is_jpeg && i->config_jpegsubpixels) {
1941 newsizex = (int)(targetwidth*i->config_jpegsubpixels + 0.5);
1942 newsizey = (int)(targetheight*i->config_jpegsubpixels + 0.5);
1943 } else if(!is_jpeg && i->config_ppmsubpixels) {
1944 newsizex = (int)(targetwidth*i->config_ppmsubpixels + 0.5);
1945 newsizey = (int)(targetheight*i->config_ppmsubpixels + 0.5);
1949 if(sizex<=0 || sizey<=0)
1956 /* TODO: cache images */
1958 if(newsizex<sizex || newsizey<sizey) {
1959 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1960 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1961 *newwidth = sizex = newsizex;
1962 *newheight = sizey = newsizey;
1965 *newwidth = newsizex = sizex;
1966 *newheight = newsizey = sizey;
1969 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1970 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1972 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1974 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1975 is_jpeg?"jpeg-":"", i->currentswfid+1,
1977 targetwidth, targetheight,
1978 /*newsizex, newsizey,*/
1979 num_colors>256?">":"", num_colors>256?256:num_colors);
1981 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1982 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1984 for(t=0;t<num_colors;t++) {
1985 printf("%02x%02x%02x%02x ",
1986 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1993 int cacheid = imageInCache(dev, mem, sizex, sizey);
1996 bitid = getNewID(dev);
1997 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1998 addImageToCache(dev, mem, sizex, sizey);
2008 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2010 gfxbbox_t bbox = gfxline_getbbox(line);
2012 r.xmin = (int)(bbox.xmin*20);
2013 r.ymin = (int)(bbox.ymin*20);
2014 r.xmax = (int)(bbox.xmax*20);
2015 r.ymax = (int)(bbox.ymax*20);
2019 int line_is_empty(gfxline_t*line)
2022 if(line->type != gfx_moveTo)
2029 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2031 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2033 if(line_is_empty(line))
2039 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2040 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2042 int newwidth=0,newheight=0;
2043 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2046 double fx = (double)img->width / (double)newwidth;
2047 double fy = (double)img->height / (double)newheight;
2050 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2051 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2052 m.tx = (int)(matrix->tx*20);
2053 m.ty = (int)(matrix->ty*20);
2056 int myshapeid = getNewID(dev);
2057 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2059 swf_ShapeNew(&shape);
2060 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2061 swf_SetU16(i->tag, myshapeid);
2062 SRECT r = gfxline_getSWFbbox(line);
2063 swf_SetRect(i->tag,&r);
2064 swf_SetShapeStyles(i->tag,shape);
2065 swf_ShapeCountBits(shape,NULL,NULL);
2066 swf_SetShapeBits(i->tag,shape);
2067 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2068 i->swflastx = i->swflasty = UNDEFINED_COORD;
2069 drawgfxline(dev, line);
2070 swf_ShapeSetEnd(i->tag);
2071 swf_ShapeFree(shape);
2073 msg("<trace> Placing bitmap ID %d", myshapeid);
2074 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2075 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2076 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2079 static RGBA col_black = {255,0,0,0};
2081 static void drawoutline(gfxdevice_t*dev, gfxline_t*line)
2083 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2085 int myshapeid = getNewID(dev);
2086 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2089 swf_ShapeNew(&shape);
2090 int lsid = swf_ShapeAddLineStyle(shape,1,&col_black);
2092 swf_SetU16(i->tag,myshapeid);
2093 SRECT r = gfxline_getSWFbbox(line);
2094 swf_SetRect(i->tag,&r);
2095 swf_SetShapeStyles(i->tag,shape);
2096 swf_ShapeCountBits(shape,NULL,NULL);
2097 swf_SetShapeBits(i->tag,shape);
2098 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,lsid,0,0);
2099 drawgfxline(dev, line);
2100 swf_ShapeSetEnd(i->tag);
2101 swf_ShapeFree(shape);
2103 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2104 swf_ObjectPlace(i->tag, myshapeid, getNewDepth(dev), 0,0,0);
2107 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2109 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2114 if(i->clippos >= 127)
2116 msg("<warning> Too many clip levels.");
2120 if(i->config_showclipshapes)
2121 drawoutline(dev, line);
2123 int myshapeid = getNewID(dev);
2124 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2126 memset(&col, 0, sizeof(RGBA));
2129 swf_ShapeNew(&shape);
2130 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2132 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2133 swf_ShapeAddSolidFillStyle(shape,&markcol);
2135 swf_SetU16(i->tag,myshapeid);
2136 SRECT r = gfxline_getSWFbbox(line);
2137 swf_SetRect(i->tag,&r);
2138 swf_SetShapeStyles(i->tag,shape);
2139 swf_ShapeCountBits(shape,NULL,NULL);
2140 swf_SetShapeBits(i->tag,shape);
2141 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2142 i->swflastx = i->swflasty = UNDEFINED_COORD;
2143 i->shapeisempty = 1;
2144 drawgfxline(dev, line);
2145 if(i->shapeisempty) {
2146 /* an empty clip shape is equivalent to a shape with no area */
2147 int x = line?line->x:0;
2148 int y = line?line->y:0;
2149 moveto(dev, i->tag, x,y);
2150 lineto(dev, i->tag, x,y);
2151 lineto(dev, i->tag, x,y);
2153 swf_ShapeSetEnd(i->tag);
2154 swf_ShapeFree(shape);
2156 /* TODO: remember the bbox, and check all shapes against it */
2158 msg("<trace> Placing clip ID %d", myshapeid);
2159 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2160 i->cliptags[i->clippos] = i->tag;
2161 i->clipshapes[i->clippos] = myshapeid;
2162 i->clipdepths[i->clippos] = getNewDepth(dev);
2166 static void swf_endclip(gfxdevice_t*dev)
2168 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2175 msg("<error> Invalid end of clipping region");
2179 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2180 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2182 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2184 static int gfxline_type(gfxline_t*line)
2190 int haszerosegments=0;
2193 if(line->type == gfx_moveTo) {
2196 } else if(line->type == gfx_lineTo) {
2200 } else if(line->type == gfx_splineTo) {
2202 if(tmpsplines>lines)
2210 if(lines==0 && splines==0) return 0;
2211 else if(lines==1 && splines==0) return 1;
2212 else if(lines==0 && splines==1) return 2;
2213 else if(splines==0) return 3;
2217 static int gfxline_has_dots(gfxline_t*line)
2225 if(line->type == gfx_moveTo) {
2226 /* test the length of the preceding line, and assume it is a dot if
2227 it's length is less than 1.0. But *only* if there's a noticable
2228 gap between the previous line and the next moveTo. (I've come
2229 across a PDF where thousands of "dots" were stringed together,
2231 int last_short_gap = short_gap;
2232 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2237 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2242 } else if(line->type == gfx_lineTo) {
2243 dist += fabs(line->x - x) + fabs(line->y - y);
2245 } else if(line->type == gfx_splineTo) {
2246 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2247 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2254 if(isline && dist < 1 && !short_gap) {
2260 static int gfxline_fix_short_edges(gfxline_t*line)
2264 if(line->type == gfx_lineTo) {
2265 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2268 } else if(line->type == gfx_splineTo) {
2269 if(fabs(line->sx - x) + fabs(line->sy - y) +
2270 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2281 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2283 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2284 if(x<i->min_x || x>i->max_x) return 0;
2285 if(y<i->min_y || y>i->max_y) return 0;
2289 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2291 gfxline_t*l = line = gfxline_clone(line);
2303 //#define NORMALIZE_POLYGON_POSITIONS
2305 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)
2307 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2308 if(line_is_empty(line))
2310 int type = gfxline_type(line);
2311 int has_dots = gfxline_has_dots(line);
2312 gfxbbox_t r = gfxline_getbbox(line);
2313 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2315 /* TODO: * split line into segments, and perform this check for all segments */
2317 if(i->config_disable_polygon_conversion || type>=5 ||
2319 (width <= i->config_caplinewidth
2320 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2321 || (cap_style == gfx_capRound && type<=2)))) {} else
2323 /* convert line to polygon */
2324 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2326 gfxline_fix_short_edges(line);
2327 /* we need to convert the line into a polygon */
2328 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2329 gfxline_t*gfxline = SVPtogfxline(svp);
2330 dev->fill(dev, gfxline, color);
2336 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2339 if(i->config_normalize_polygon_positions) {
2341 double startx = 0, starty = 0;
2342 if(line && line->type == gfx_moveTo) {
2346 line = gfxline_move(line, -startx, -starty);
2347 i->shapeposx = (int)(startx*20);
2348 i->shapeposy = (int)(starty*20);
2351 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2352 swfoutput_setlinewidth(dev, width);
2355 drawgfxline(dev, line);
2357 if(i->config_normalize_polygon_positions) {
2358 free(line); //account for _move
2362 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2364 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2365 if(line_is_empty(line))
2369 gfxbbox_t r = gfxline_getbbox(line);
2370 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2372 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a) {
2375 if(!i->config_ignoredraworder)
2378 if(i->config_normalize_polygon_positions) {
2380 double startx = 0, starty = 0;
2381 if(line && line->type == gfx_moveTo) {
2385 line = gfxline_move(line, -startx, -starty);
2386 i->shapeposx = (int)(startx*20);
2387 i->shapeposy = (int)(starty*20);
2390 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2394 drawgfxline(dev, line);
2395 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2397 if(i->config_normalize_polygon_positions) {
2398 free(line); //account for _move
2401 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2403 if(line_is_empty(line))
2405 msg("<error> Gradient filling not implemented yet");
2408 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
2410 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2412 SRECT bounds = {0,0,0,0};
2414 swffont->version = 2;
2415 swffont->name = (U8*)strdup(id);
2416 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2417 swffont->layout->ascent = 0; /* ? */
2418 swffont->layout->descent = 0;
2419 swffont->layout->leading = 0;
2420 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2421 swffont->encoding = FONT_ENCODING_UNICODE;
2422 swffont->numchars = font->num_glyphs;
2423 swffont->maxascii = font->max_unicode;
2424 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2425 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2426 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2427 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2428 for(t=0;t<font->max_unicode;t++) {
2429 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2431 for(t=0;t<font->num_glyphs;t++) {
2435 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2436 if(swffont->glyph2ascii[t] == 0xffff || swffont->glyph2ascii[t] == 0x0000) {
2437 /* flash 8 flashtype requires unique unicode IDs for each character.
2438 We use the Unicode private user area to assign characters, hoping that
2439 the font doesn't contain more than 2048 glyphs */
2440 swffont->glyph2ascii[t] = 0xe000 + (t&0x1fff);
2443 if(font->glyphs[t].name) {
2444 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2446 swffont->glyphnames[t] = 0;
2448 advance = (int)(font->glyphs[t].advance);
2450 swf_Shape01DrawerInit(&draw, 0);
2451 line = font->glyphs[t].line;
2454 c.x = line->sx * GLYPH_SCALE; c.y = line->sy * GLYPH_SCALE;
2455 to.x = line->x * GLYPH_SCALE; to.y = line->y * GLYPH_SCALE;
2456 if(line->type == gfx_moveTo) {
2457 draw.moveTo(&draw, &to);
2458 } else if(line->type == gfx_lineTo) {
2459 draw.lineTo(&draw, &to);
2460 } else if(line->type == gfx_splineTo) {
2461 draw.splineTo(&draw, &c, &to);
2466 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2467 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2469 int xmax = swffont->layout->bounds[t].xmax / 20;
2470 if(xmax>0 && xmax*2 < advance) {
2471 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", xmax, advance, font->glyphs[t].advance);
2476 swffont->glyph[t].advance = advance;
2478 swffont->glyph[t].advance = 32767;
2481 draw.dealloc(&draw);
2483 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2485 if(bounds.ymin < 0 && bounds.ymax > 0) {
2486 swffont->layout->ascent = -bounds.ymin;
2487 swffont->layout->descent = bounds.ymax;
2488 swffont->layout->leading = bounds.ymax - bounds.ymin;
2490 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2491 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2492 swffont->layout->leading = bounds.ymax - bounds.ymin;
2498 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2500 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2502 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2503 return; // the requested font is the current font
2505 fontlist_t*last=0,*l = i->fontlist;
2508 if(!strcmp((char*)l->swffont->name, font->id)) {
2509 return; // we already know this font
2513 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2514 l->swffont = gfxfont_to_swffont(font, font->id);
2521 swf_FontSetID(l->swffont, getNewID(i->dev));
2523 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2525 // print font information
2526 msg("<debug> Font %s",font->id);
2527 msg("<debug> | ID: %d", l->swffont->id);
2528 msg("<debug> | Version: %d", l->swffont->version);
2529 msg("<debug> | Name: %s", l->swffont->name);
2530 msg("<debug> | Numchars: %d", l->swffont->numchars);
2531 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2532 msg("<debug> | Style: %d", l->swffont->style);
2533 msg("<debug> | Encoding: %d", l->swffont->encoding);
2534 for(iii=0; iii<l->swffont->numchars;iii++) {
2535 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,
2536 l->swffont->layout->bounds[iii].xmin/20.0,
2537 l->swffont->layout->bounds[iii].ymin/20.0,
2538 l->swffont->layout->bounds[iii].xmax/20.0,
2539 l->swffont->layout->bounds[iii].ymax/20.0
2542 for(t=0;t<l->swffont->maxascii;t++) {
2543 if(l->swffont->ascii2glyph[t] == iii)
2544 msg("<debug> | - maps to %d",t);
2550 static void swf_switchfont(gfxdevice_t*dev, const char*fontid)
2552 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2554 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2555 return; // the requested font is the current font
2557 fontlist_t*l = i->fontlist;
2559 if(!strcmp((char*)l->swffont->name, fontid)) {
2560 i->swffont = l->swffont;
2565 msg("<error> Unknown font id: %s", fontid);
2569 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2571 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2575 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2577 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2578 with multiple fonts */
2581 swf_switchfont(dev, font->id); // set the current font
2583 setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2585 /* printf("%f %f\n", m.m31, m.m32);
2587 static int xpos = 40;
2588 static int ypos = 200;
2595 drawchar(dev, i->swffont, glyph, matrix->tx, matrix->ty, color);