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_splinemaxerror;
83 int config_fontsplinemaxerror;
84 int config_filloverlap;
87 int config_disable_polygon_conversion;
88 RGBA config_linkcolor;
89 float config_minlinewidth;
90 double config_caplinewidth;
91 char* config_linktarget;
92 char*config_internallinkfunction;
93 char*config_externallinkfunction;
95 double config_framerate;
134 int pic_height[1024];
140 char fillstylechanged;
142 int jpeg; //next image type
147 chardata_t chardata[CHARDATAMAX];
154 int current_font_size;
156 double lastfontm11,lastfontm12,lastfontm21,lastfontm22;
167 } swfoutput_internal;
169 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
170 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
171 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);
172 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
173 static void swf_endclip(gfxdevice_t*dev);
174 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);
175 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
176 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
177 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
178 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
179 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
180 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
181 static void swf_startframe(gfxdevice_t*dev, int width, int height);
182 static void swf_endframe(gfxdevice_t*dev);
183 static gfxresult_t* swf_finish(gfxdevice_t*driver);
185 static swfoutput_internal* init_internal_struct()
187 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
188 memset(i, 0, sizeof(swfoutput_internal));
212 i->fillstylechanged = 0;
219 i->config_dumpfonts=0;
220 i->config_ppmsubpixels=0;
221 i->config_jpegsubpixels=0;
222 i->config_opennewwindow=1;
223 i->config_ignoredraworder=0;
224 i->config_drawonlyshapes=0;
225 i->config_jpegquality=85;
226 i->config_storeallcharacters=0;
227 i->config_enablezlib=0;
228 i->config_generate_fake_tags=0;
229 i->config_insertstoptag=0;
230 i->config_flashversion=6;
231 i->config_framerate=0.25;
232 i->config_splinemaxerror=1;
233 i->config_fontsplinemaxerror=1;
234 i->config_filloverlap=0;
236 i->config_bboxvars=0;
237 i->config_minlinewidth=0.05;
238 i->config_caplinewidth=1;
239 i->config_linktarget=0;
240 i->config_internallinkfunction=0;
241 i->config_externallinkfunction=0;
242 i->config_reordertags=1;
244 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
245 i->config_linkcolor.a = 0x40;
250 static int id_error = 0;
252 static U16 getNewID(gfxdevice_t* dev)
254 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
255 if(i->currentswfid == 65535) {
257 msg("<error> ID Table overflow");
258 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
264 return ++i->currentswfid;
266 static U16 getNewDepth(gfxdevice_t* dev)
268 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
269 if(i->depth == 65535) {
271 msg("<error> Depth Table overflow");
272 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
281 static void startshape(gfxdevice_t* dev);
282 static void starttext(gfxdevice_t* dev);
283 static void endshape(gfxdevice_t* dev);
284 static void endtext(gfxdevice_t* dev);
286 typedef struct _plotxy
291 // write a move-to command into the swf
292 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
294 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
295 int rx = (int)(p0.x*20);
296 int ry = (int)(p0.y*20);
297 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
298 swf_ShapeSetMove (tag, i->shape, rx,ry);
299 i->fillstylechanged = 0;
306 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
308 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
312 return movetoxy(dev, tag, p);
314 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
316 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
322 swf_ExpandRect(&i->bboxrect, p);
324 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
328 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
330 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
331 int width = i->linewidth/4;
335 //swf_ShapeSetLine(tag, i->shape,-width,-width);
336 //swf_ShapeSetLine(tag, i->shape,width*2,0);
337 //swf_ShapeSetLine(tag, i->shape,0,width*2);
338 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
339 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
340 //swf_ShapeSetLine(tag, i->shape,width,width);
343 swf_ShapeSetLine(tag, i->shape,-width,0);
344 swf_ShapeSetLine(tag, i->shape,width,-width);
345 swf_ShapeSetLine(tag, i->shape,width,width);
346 swf_ShapeSetLine(tag, i->shape,-width,width);
347 swf_ShapeSetLine(tag, i->shape,-width,-width);
348 swf_ShapeSetLine(tag, i->shape,width,0);
350 addPointToBBox(dev, x-width ,y-width);
351 addPointToBBox(dev, x+width ,y+width);
354 // write a line-to command into the swf
355 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
357 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
358 int px = (int)(p0.x*20);
359 int py = (int)(p0.y*20);
360 int rx = (px-i->swflastx);
361 int ry = (py-i->swflasty);
363 swf_ShapeSetLine (tag, i->shape, rx,ry);
364 addPointToBBox(dev, i->swflastx,i->swflasty);
365 addPointToBBox(dev, px,py);
366 }/* else if(!i->fill) {
367 // treat lines of length 0 as plots, making them
368 // at least 1 twip wide so Flash will display them
369 plot(dev, i->swflastx, i->swflasty, tag);
376 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
381 linetoxy(dev,tag, p);
384 // write a spline-to command into the swf
385 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
387 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
388 int lastlastx = i->swflastx;
389 int lastlasty = i->swflasty;
391 int cx = ((int)(control.x*20)-i->swflastx);
392 int cy = ((int)(control.y*20)-i->swflasty);
395 int ex = ((int)(end.x*20)-i->swflastx);
396 int ey = ((int)(end.y*20)-i->swflasty);
400 if((cx || cy) && (ex || ey)) {
401 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
402 addPointToBBox(dev, lastlastx ,lastlasty );
403 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
404 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
405 } else if(cx || cy || ex || ey) {
406 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
407 addPointToBBox(dev, lastlastx ,lastlasty );
408 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
409 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
415 /* write a line, given two points and the transformation
417 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
419 moveto(dev, tag, p0);
420 lineto(dev, tag, p1);
423 void resetdrawer(gfxdevice_t*dev)
425 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
430 static void stopFill(gfxdevice_t*dev)
432 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
433 if(i->lastwasfill!=0)
435 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
436 i->fillstylechanged = 1;
440 static void startFill(gfxdevice_t*dev)
442 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
443 if(i->lastwasfill!=1)
445 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
446 i->fillstylechanged = 1;
451 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
463 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
465 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
468 memset(&r, 0, sizeof(r));
471 if(debug) printf("\n");
472 for(t=0;t<i->chardatapos;t++)
474 if(i->chardata[t].fontid != font->id) {
475 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
478 SRECT b = font->layout->bounds[i->chardata[t].charid];
479 b.xmin *= i->chardata[t].size;
480 b.ymin *= i->chardata[t].size;
481 b.xmax *= i->chardata[t].size;
482 b.ymax *= i->chardata[t].size;
484 /* divide by 1024, rounding xmax/ymax up */
485 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
487 b.xmin += i->chardata[t].x;
488 b.ymin += i->chardata[t].y;
489 b.xmax += i->chardata[t].x;
490 b.ymax += i->chardata[t].y;
492 /* until we solve the INTERNAL_SCALING problem (see below)
493 make sure the bounding box is big enough */
499 b = swf_TurnRect(b, m);
501 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
502 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
503 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
504 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
505 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
510 i->chardata[t].fontid,
512 i->chardata[t].charid
514 swf_ExpandRect2(&r, &b);
516 if(debug) printf("-----> (%f,%f,%f,%f)\n",
524 static void putcharacters(gfxdevice_t*dev, TAG*tag)
526 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
530 color.r = i->chardata[0].color.r^255;
539 int charadvance[128];
542 int glyphbits=1; //TODO: can this be zero?
545 if(tag->id != ST_DEFINETEXT &&
546 tag->id != ST_DEFINETEXT2) {
547 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
550 if(!i->chardatapos) {
551 msg("<warning> putcharacters called with zero characters");
554 for(pass = 0; pass < 2; pass++)
564 advancebits++; // add sign bit
565 swf_SetU8(tag, glyphbits);
566 swf_SetU8(tag, advancebits);
569 for(t=0;t<=i->chardatapos;t++)
571 if(lastfontid != i->chardata[t].fontid ||
572 lastx!=i->chardata[t].x ||
573 lasty!=i->chardata[t].y ||
574 !colorcompare(dev,&color, &i->chardata[t].color) ||
576 lastsize != i->chardata[t].size ||
579 if(charstorepos && pass==0)
582 for(s=0;s<charstorepos;s++)
584 while(charids[s]>=(1<<glyphbits))
586 while(charadvance[s]>=(1<<advancebits))
590 if(charstorepos && pass==1)
592 tag->writeBit = 0; // Q&D
593 swf_SetBits(tag, 0, 1); // GLYPH Record
594 swf_SetBits(tag, charstorepos, 7); // number of glyphs
596 for(s=0;s<charstorepos;s++)
598 swf_SetBits(tag, charids[s], glyphbits);
599 swf_SetBits(tag, charadvance[s], advancebits);
604 if(pass == 1 && t<i->chardatapos)
610 if(lastx != i->chardata[t].x ||
611 lasty != i->chardata[t].y)
613 newx = i->chardata[t].x;
614 newy = i->chardata[t].y;
620 if(!colorcompare(dev,&color, &i->chardata[t].color))
622 color = i->chardata[t].color;
625 font.id = i->chardata[t].fontid;
626 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
629 tag->writeBit = 0; // Q&D
630 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
633 lastfontid = i->chardata[t].fontid;
634 lastx = i->chardata[t].x;
635 lasty = i->chardata[t].y;
636 lastsize = i->chardata[t].size;
639 if(t==i->chardatapos)
643 int nextt = t==i->chardatapos-1?t:t+1;
644 int rel = i->chardata[nextt].x-i->chardata[t].x;
645 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
647 lastx=i->chardata[nextt].x;
651 lastx=i->chardata[t].x;
653 charids[charstorepos] = i->chardata[t].charid;
654 charadvance[charstorepos] = advance;
661 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
663 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
664 if(i->chardatapos == CHARDATAMAX)
666 msg("<warning> Character buffer too small. SWF will be slightly bigger");
670 i->chardata[i->chardatapos].fontid = fontid;
671 i->chardata[i->chardatapos].charid = charid;
672 i->chardata[i->chardatapos].x = x;
673 i->chardata[i->chardatapos].y = y;
674 i->chardata[i->chardatapos].color = color;
675 i->chardata[i->chardatapos].size = size;
679 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
680 So if we set this value to high, the char coordinates will overflow.
681 If we set it to low, however, the char positions will be inaccurate */
682 #define GLYPH_SCALE 1
684 /* process a character. */
685 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, float x, float y, gfxcolor_t*col)
687 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
689 msg("<warning> Font is NULL");
693 if(charid<0 || charid>=swffont->numchars) {
694 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
697 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
698 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
699 charid, FIXNULL((char*)swffont->name), swffont->numchars);
708 double det = i->fontmatrix.sx/65536.0 * i->fontmatrix.sy/65536.0 -
709 i->fontmatrix.r0/65536.0 * i->fontmatrix.r1/65536.0;
711 if(fabs(det) < 0.0005) {
712 /* x direction equals y direction- the text is invisible */
715 det = 20 * GLYPH_SCALE / det;
718 p.x = (SCOORD)(( x * i->fontmatrix.sy/65536.0 - y * i->fontmatrix.r1/65536.0)*det);
719 p.y = (SCOORD)((- x * i->fontmatrix.r0/65536.0 + y * i->fontmatrix.sx/65536.0)*det);
721 RGBA rgba = *(RGBA*)col;
722 putcharacter(dev, swffont->id, charid,p.x,p.y,i->current_font_size, rgba);
723 swf_FontUseGlyph(swffont, charid);
727 static void endtext(gfxdevice_t*dev)
729 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
733 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
734 swf_SetU16(i->tag, i->textid);
737 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
739 swf_SetRect(i->tag,&r);
741 swf_SetMatrix(i->tag,&i->fontmatrix);
743 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
745 putcharacters(dev, i->tag);
748 if(i->swf->fileVersion >= 8) {
749 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
750 swf_SetU16(i->tag, i->textid);
752 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
753 swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
754 //swf_SetU8(i->tag, /*no grid*/(0<<3)|/*flashtype*/0x40);
756 swf_SetU32(i->tag, 0);//thickness
757 swf_SetU32(i->tag, 0);//sharpness
758 swf_SetU8(i->tag, 0);//reserved
760 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
762 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
766 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
767 static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22)
773 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
774 if(i->lastfontm11 == m11 &&
775 i->lastfontm12 == m12 &&
776 i->lastfontm21 == m21 &&
777 i->lastfontm22 == m22)
782 i->lastfontm11 = m11;
783 i->lastfontm12 = m12;
784 i->lastfontm21 = m21;
785 i->lastfontm22 = m22;
787 double xsize = sqrt(m11*m11 + m12*m12);
788 double ysize = sqrt(m21*m21 + m22*m22);
789 i->current_font_size = (xsize>ysize?xsize:ysize)*1;
790 if(i->current_font_size < 1)
791 i->current_font_size = 1;
792 double ifs = 1.0 / (i->current_font_size*GLYPH_SCALE);
795 m.sx = (U32)((m11*ifs)*65536); m.r1 = (U32)((m21*ifs)*65536);
796 m.r0 = (U32)((m12*ifs)*65536); m.sy = (U32)((m22*ifs)*65536);
802 static void endpage(gfxdevice_t*dev)
804 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
814 void swf_startframe(gfxdevice_t*dev, int width, int height)
816 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
817 if(!i->firstpage && !i->pagefinished)
819 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
821 swf_GetMatrix(0, &i->page_matrix);
822 i->page_matrix.tx = 0;
823 i->page_matrix.ty = 0;
829 /* set clipping/background rectangle */
830 /* TODO: this should all be done in SWFOutputDev */
831 //setBackground(dev, x1, y1, x2, y2);
833 /* increase SWF's bounding box */
839 swf_ExpandRect2(&i->swf->movieSize, &r);
841 i->lastframeno = i->frameno;
846 void swf_endframe(gfxdevice_t*dev)
848 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
853 if(i->config_insertstoptag) {
855 atag = action_Stop(atag);
856 atag = action_End(atag);
857 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
858 swf_ActionSet(i->tag,atag);
860 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
863 for(i->depth;i->depth>i->startdepth;i->depth--) {
864 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
865 swf_SetU16(i->tag,i->depth);
867 i->depth = i->startdepth;
870 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
872 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
874 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
878 int shapeid = getNewID(dev);
883 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
885 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
886 swf_SetU16(i->tag,shapeid);
887 swf_SetRect(i->tag,&r);
888 swf_SetShapeHeader(i->tag,s);
889 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
890 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
891 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
892 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
893 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
894 swf_ShapeSetEnd(i->tag);
896 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
897 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
898 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
899 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
902 /* initialize the swf writer */
903 void gfxdevice_swf_init(gfxdevice_t* dev)
905 memset(dev, 0, sizeof(gfxdevice_t));
909 dev->internal = init_internal_struct();
911 dev->startpage = swf_startframe;
912 dev->endpage = swf_endframe;
913 dev->finish = swf_finish;
914 dev->fillbitmap = swf_fillbitmap;
915 dev->setparameter = swf_setparameter;
916 dev->stroke = swf_stroke;
917 dev->startclip = swf_startclip;
918 dev->endclip = swf_endclip;
919 dev->fill = swf_fill;
920 dev->fillbitmap = swf_fillbitmap;
921 dev->fillgradient = swf_fillgradient;
922 dev->addfont = swf_addfont;
923 dev->drawchar = swf_drawchar;
924 dev->drawlink = swf_drawlink;
926 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
932 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
936 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
937 i->swf->fileVersion = i->config_flashversion;
938 i->swf->frameRate = i->config_framerate*0x100;
939 i->swf->movieSize.xmin = 0;
940 i->swf->movieSize.ymin = 0;
941 i->swf->movieSize.xmax = 0;
942 i->swf->movieSize.ymax = 0;
944 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
945 i->tag = i->swf->firstTag;
946 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
947 swf_SetRGB(i->tag,&rgb);
949 i->startdepth = i->depth = 0;
951 if(i->config_protect)
952 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
955 static void startshape(gfxdevice_t*dev)
957 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
966 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
968 swf_ShapeNew(&i->shape);
969 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
970 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
972 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
973 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
976 i->shapeid = getNewID(dev);
978 msg("<debug> Using shape id %d", i->shapeid);
980 swf_SetU16(i->tag,i->shapeid); // ID
982 i->bboxrectpos = i->tag->len;
986 r.xmax = 20*i->max_x;
987 r.ymax = 20*i->max_y;
988 swf_SetRect(i->tag,&r);
990 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
992 swf_SetShapeStyles(i->tag,i->shape);
993 swf_ShapeCountBits(i->shape,NULL,NULL);
994 swf_SetShapeBits(i->tag,i->shape);
996 /* TODO: do we really need this? */
997 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
998 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
999 i->swflastx=i->swflasty=UNDEFINED_COORD;
1000 i->lastwasfill = -1;
1001 i->shapeisempty = 1;
1004 static void starttext(gfxdevice_t*dev)
1006 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1010 i->textid = getNewID(dev);
1012 i->swflastx=i->swflasty=0;
1016 /* TODO: move to ../lib/rfxswf */
1017 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1019 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1020 /* determine length of old rect */
1024 swf_GetRect(tag, &old);
1025 swf_ResetReadBits(tag);
1026 int pos_end = tag->pos;
1028 int len = tag->len - pos_end;
1029 U8*data = (U8*)malloc(len);
1030 memcpy(data, &tag->data[pos_end], len);
1033 swf_SetRect(tag, newrect);
1034 swf_SetBlock(tag, data, len);
1036 tag->pos = tag->readBit = 0;
1039 void cancelshape(gfxdevice_t*dev)
1041 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1042 /* delete old shape tag */
1044 i->tag = i->tag->prev;
1045 swf_DeleteTag(todel);
1046 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1048 i->bboxrectpos = -1;
1050 // i->currentswfid--; // this was an *exceptionally* bad idea
1053 void fixAreas(gfxdevice_t*dev)
1055 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1056 if(!i->shapeisempty && i->fill &&
1057 (i->bboxrect.xmin == i->bboxrect.xmax ||
1058 i->bboxrect.ymin == i->bboxrect.ymax) &&
1059 i->config_minlinewidth >= 0.001
1061 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1062 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1063 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1066 SRECT r = i->bboxrect;
1068 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1069 /* this thing comes down to a single dot- nothing to fix here */
1075 RGBA save_col = i->strokergb;
1076 int save_width = i->linewidth;
1078 i->strokergb = i->fillrgb;
1079 i->linewidth = (int)(i->config_minlinewidth*20);
1080 if(i->linewidth==0) i->linewidth = 1;
1085 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1086 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1088 i->strokergb = save_col;
1089 i->linewidth = save_width;
1094 static void endshape_noput(gfxdevice_t*dev)
1096 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1099 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1102 swf_ShapeFree(i->shape);
1110 static void endshape(gfxdevice_t*dev)
1112 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1118 if(i->shapeisempty ||
1120 (i->bboxrect.xmin == i->bboxrect.xmax &&
1121 i->bboxrect.ymin == i->bboxrect.ymax))
1123 // delete the shape again, we didn't do anything
1124 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1125 i->bboxrect.xmin /20.0,
1126 i->bboxrect.ymin /20.0,
1127 i->bboxrect.xmax /20.0,
1128 i->bboxrect.ymax /20.0
1134 swf_ShapeSetEnd(i->tag);
1136 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1138 msg("<trace> Placing shape ID %d", i->shapeid);
1140 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1141 MATRIX m = i->page_matrix;
1142 m.tx += i->shapeposx;
1143 m.ty += i->shapeposy;
1144 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1146 if(i->config_animate) {
1147 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1150 swf_ShapeFree(i->shape);
1153 i->bboxrectpos = -1;
1160 void wipeSWF(SWF*swf)
1162 TAG*tag = swf->firstTag;
1164 TAG*next = tag->next;
1165 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1166 tag->id != ST_END &&
1167 tag->id != ST_DOACTION &&
1168 tag->id != ST_SHOWFRAME) {
1176 void swfoutput_finalize(gfxdevice_t*dev)
1178 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1180 if(i->tag && i->tag->id == ST_END)
1181 return; //already done
1183 if(i->config_bboxvars) {
1184 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1186 a = action_PushString(a, "xmin");
1187 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1188 a = action_SetVariable(a);
1189 a = action_PushString(a, "ymin");
1190 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1191 a = action_SetVariable(a);
1192 a = action_PushString(a, "xmax");
1193 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1194 a = action_SetVariable(a);
1195 a = action_PushString(a, "ymax");
1196 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1197 a = action_SetVariable(a);
1198 a = action_PushString(a, "width");
1199 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1200 a = action_SetVariable(a);
1201 a = action_PushString(a, "height");
1202 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1203 a = action_SetVariable(a);
1205 swf_ActionSet(tag, a);
1209 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1213 free(i->mark);i->mark = 0;
1217 fontlist_t *tmp,*iterator = i->fontlist;
1219 TAG*mtag = i->swf->firstTag;
1220 if(iterator->swffont) {
1221 if(!i->config_storeallcharacters) {
1222 msg("<debug> Reducing font %s", iterator->swffont->name);
1223 swf_FontReduce(iterator->swffont);
1225 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1226 if(i->config_storeallcharacters || used) {
1227 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1228 swf_FontSetDefine2(mtag, iterator->swffont);
1232 iterator = iterator->next;
1234 i->tag = swf_InsertTag(i->tag,ST_END);
1235 TAG* tag = i->tag->prev;
1237 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1238 and the ST_END- they confuse the flash player */
1239 while(tag->id == ST_REMOVEOBJECT2) {
1240 TAG* prev = tag->prev;
1248 if(i->config_enablezlib || i->config_flashversion>=6) {
1249 i->swf->compressed = 1;
1252 if(i->config_reordertags)
1253 swf_Optimize(i->swf);
1256 int swfresult_save(gfxresult_t*gfx, char*filename)
1258 SWF*swf = (SWF*)gfx->internal;
1261 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1266 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1270 if(swf->compressed) {
1271 if FAILED(swf_WriteSWC(fi,swf))
1272 msg("<error> WriteSWC() failed.\n");
1274 if FAILED(swf_WriteSWF(fi,swf))
1275 msg("<error> WriteSWF() failed.\n");
1282 void* swfresult_get(gfxresult_t*gfx, char*name)
1284 SWF*swf = (SWF*)gfx->internal;
1285 if(!strcmp(name, "swf")) {
1286 return (void*)swf_CopySWF(swf);
1287 } else if(!strcmp(name, "xmin")) {
1288 return (void*)(swf->movieSize.xmin/20);
1289 } else if(!strcmp(name, "ymin")) {
1290 return (void*)(swf->movieSize.ymin/20);
1291 } else if(!strcmp(name, "xmax")) {
1292 return (void*)(swf->movieSize.xmax/20);
1293 } else if(!strcmp(name, "ymax")) {
1294 return (void*)(swf->movieSize.ymax/20);
1295 } else if(!strcmp(name, "width")) {
1296 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1297 } else if(!strcmp(name, "height")) {
1298 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1302 void swfresult_destroy(gfxresult_t*gfx)
1305 swf_FreeTags((SWF*)gfx->internal);
1306 free(gfx->internal);
1309 memset(gfx, 0, sizeof(gfxresult_t));
1313 static void swfoutput_destroy(gfxdevice_t* dev);
1315 gfxresult_t* swf_finish(gfxdevice_t* dev)
1317 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1320 if(i->config_linktarget) {
1321 free(i->config_linktarget);
1322 i->config_linktarget = 0;
1325 swfoutput_finalize(dev);
1326 SWF* swf = i->swf;i->swf = 0;
1327 swfoutput_destroy(dev);
1329 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1330 result->internal = swf;
1331 result->save = swfresult_save;
1333 result->get = swfresult_get;
1334 result->destroy = swfresult_destroy;
1338 /* Perform cleaning up */
1339 static void swfoutput_destroy(gfxdevice_t* dev)
1341 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1343 /* not initialized yet- nothing to destroy */
1347 fontlist_t *tmp,*iterator = i->fontlist;
1349 if(iterator->swffont) {
1350 swf_FontFree(iterator->swffont);iterator->swffont=0;
1353 iterator = iterator->next;
1356 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1359 memset(dev, 0, sizeof(gfxdevice_t));
1362 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1364 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1365 if(i->fillrgb.r == r &&
1366 i->fillrgb.g == g &&
1367 i->fillrgb.b == b &&
1368 i->fillrgb.a == a) return;
1378 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1380 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1381 if(i->strokergb.r == r &&
1382 i->strokergb.g == g &&
1383 i->strokergb.b == b &&
1384 i->strokergb.a == a) return;
1394 //#define ROUND_UP 19
1395 //#define ROUND_UP 10
1397 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1399 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1400 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1404 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1408 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1409 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1410 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1411 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1413 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1415 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1416 dev->drawlink(dev, points, url);
1419 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1421 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1423 if(!strncmp("http://pdf2swf:", url, 15)) {
1424 char*tmp = strdup(url);
1425 int l = strlen(tmp);
1428 swfoutput_namedlink(dev, tmp+15, points);
1431 } else if(!strncmp("page", url, 4)) {
1434 if(url[t]<'0' || url[t]>'9')
1437 int page = atoi(&url[4]);
1438 if(page<0) page = 0;
1439 swfoutput_linktopage(dev, page, points);
1442 swfoutput_linktourl(dev, url, points);
1445 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1447 ActionTAG* actions = 0;
1448 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1454 if(i->config_externallinkfunction) {
1455 actions = action_PushString(actions, url); //parameter
1456 actions = action_PushInt(actions, 1); //number of parameters (1)
1457 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1458 actions = action_CallFunction(actions);
1459 } else if(!i->config_linktarget) {
1460 if(!i->config_opennewwindow)
1461 actions = action_GetUrl(actions, url, "_parent");
1463 actions = action_GetUrl(actions, url, "_this");
1465 actions = action_GetUrl(actions, url, i->config_linktarget);
1467 actions = action_End(actions);
1469 drawlink(dev, actions, 0, points,0);
1471 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1473 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1474 ActionTAG* actions = 0;
1481 if(!i->config_internallinkfunction) {
1482 actions = action_GotoFrame(actions, page-1);
1483 actions = action_End(actions);
1485 actions = action_PushInt(actions, page); //parameter
1486 actions = action_PushInt(actions, 1); //number of parameters (1)
1487 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1488 actions = action_CallFunction(actions);
1489 actions = action_End(actions);
1492 drawlink(dev, actions, 0, points,0);
1495 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1496 of the viewer objects, like subtitles, index elements etc.
1498 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1500 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1501 ActionTAG *actions1,*actions2;
1502 char*tmp = strdup(name);
1510 if(!strncmp(tmp, "call:", 5))
1512 char*x = strchr(&tmp[5], ':');
1514 actions1 = action_PushInt(0, 0); //number of parameters (0)
1515 actions1 = action_PushString(actions1, &tmp[5]); //function name
1516 actions1 = action_CallFunction(actions1);
1517 actions1 = action_End(actions1);
1520 actions1 = action_PushString(0, x+1); //parameter
1521 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1522 actions1 = action_PushString(actions1, &tmp[5]); //function name
1523 actions1 = action_CallFunction(actions1);
1524 actions1 = action_End(actions1);
1526 actions2 = action_End(0);
1531 actions1 = action_PushString(0, "/:subtitle");
1532 actions1 = action_PushString(actions1, name);
1533 actions1 = action_SetVariable(actions1);
1534 actions1 = action_End(actions1);
1536 actions2 = action_PushString(0, "/:subtitle");
1537 actions2 = action_PushString(actions2, "");
1538 actions2 = action_SetVariable(actions2);
1539 actions2 = action_End(actions2);
1542 drawlink(dev, actions1, actions2, points,mouseover);
1544 swf_ActionFree(actions1);
1545 swf_ActionFree(actions2);
1549 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1551 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1552 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1554 int lines= 0, splines=0;
1558 /* check whether the next segment is zero */
1559 if(line->type == gfx_moveTo) {
1560 moveto(dev, i->tag, line->x, line->y);
1561 px = lastx = line->x;
1562 py = lasty = line->y;
1564 } if(line->type == gfx_lineTo) {
1565 lineto(dev, i->tag, line->x, line->y);
1570 } else if(line->type == gfx_splineTo) {
1572 s.x = line->sx;p.x = line->x;
1573 s.y = line->sy;p.y = line->y;
1574 splineto(dev, i->tag, s, p);
1582 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1586 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1588 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1593 plotxy_t p1,p2,p3,p4;
1599 int buttonid = getNewID(dev);
1600 gfxbbox_t bbox = gfxline_getbbox(points);
1603 myshapeid = getNewID(dev);
1604 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1605 swf_ShapeNew(&i->shape);
1606 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1607 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1608 swf_SetU16(i->tag, myshapeid);
1609 r.xmin = (int)(bbox.xmin*20);
1610 r.ymin = (int)(bbox.ymin*20);
1611 r.xmax = (int)(bbox.xmax*20);
1612 r.ymax = (int)(bbox.ymax*20);
1613 swf_SetRect(i->tag,&r);
1614 swf_SetShapeStyles(i->tag,i->shape);
1615 swf_ShapeCountBits(i->shape,NULL,NULL);
1616 swf_SetShapeBits(i->tag,i->shape);
1617 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1618 i->swflastx = i->swflasty = 0;
1619 drawgfxline(dev, points);
1620 swf_ShapeSetEnd(i->tag);
1623 myshapeid2 = getNewID(dev);
1624 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1625 swf_ShapeNew(&i->shape);
1627 rgb = i->config_linkcolor;
1629 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1630 swf_SetU16(i->tag, myshapeid2);
1631 r.xmin = (int)(bbox.xmin*20);
1632 r.ymin = (int)(bbox.ymin*20);
1633 r.xmax = (int)(bbox.xmax*20);
1634 r.ymax = (int)(bbox.ymax*20);
1635 swf_SetRect(i->tag,&r);
1636 swf_SetShapeStyles(i->tag,i->shape);
1637 swf_ShapeCountBits(i->shape,NULL,NULL);
1638 swf_SetShapeBits(i->tag,i->shape);
1639 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1640 i->swflastx = i->swflasty = 0;
1641 drawgfxline(dev, points);
1642 swf_ShapeSetEnd(i->tag);
1646 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1647 swf_SetU16(i->tag,buttonid); //id
1648 swf_ButtonSetFlags(i->tag, 0); //menu=no
1649 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1650 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1651 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1652 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1653 swf_SetU8(i->tag,0);
1654 swf_ActionSet(i->tag,actions1);
1655 swf_SetU8(i->tag,0);
1659 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1660 swf_SetU16(i->tag,buttonid); //id
1661 swf_ButtonSetFlags(i->tag, 0); //menu=no
1662 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1663 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1664 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1665 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1666 swf_SetU8(i->tag,0); // end of button records
1667 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1668 swf_ActionSet(i->tag,actions1);
1670 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1671 swf_ActionSet(i->tag,actions2);
1672 swf_SetU8(i->tag,0);
1673 swf_ButtonPostProcess(i->tag, 2);
1675 swf_SetU8(i->tag,0);
1676 swf_ButtonPostProcess(i->tag, 1);
1680 msg("<trace> Placing link ID %d", buttonid);
1681 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1683 if(posx!=0 || posy!=0) {
1685 p.x = (int)(posx*20);
1686 p.y = (int)(posy*20);
1687 p = swf_TurnPoint(p, &i->page_matrix);
1692 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1694 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1701 for(t=0;t<picpos;t++)
1703 if(pic_xids[t] == xid &&
1704 pic_yids[t] == yid) {
1705 width = pic_width[t];
1706 height = pic_height[t];
1710 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1711 pic_xids[picpos] = xid;
1712 pic_yids[picpos] = yid;
1713 pic_width[picpos] = width;
1714 pic_height[picpos] = height;
1717 pic[width*y+x] = buf[0];
1721 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1722 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1726 xid += x*r+x*b*3+x*g*7+x*a*11;
1727 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1729 for(t=0;t<picpos;t++)
1731 if(pic_xids[t] == xid &&
1732 pic_yids[t] == yid) {
1741 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1743 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1745 msg("<trace> swfdevice: %s=%s", name, value);
1746 if(!strcmp(name, "jpegsubpixels")) {
1747 i->config_jpegsubpixels = atof(value);
1748 } else if(!strcmp(name, "ppmsubpixels")) {
1749 i->config_ppmsubpixels = atof(value);
1750 } else if(!strcmp(name, "subpixels")) {
1751 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1752 } else if(!strcmp(name, "drawonlyshapes")) {
1753 i->config_drawonlyshapes = atoi(value);
1754 } else if(!strcmp(name, "ignoredraworder")) {
1755 i->config_ignoredraworder = atoi(value);
1756 } else if(!strcmp(name, "mark")) {
1757 if(!value || !value[0]) {
1758 if(i->mark) free(i->mark);
1762 i->mark = strdup("...");
1763 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1765 } else if(!strcmp(name, "filloverlap")) {
1766 i->config_filloverlap = atoi(value);
1767 } else if(!strcmp(name, "linksopennewwindow")) {
1768 i->config_opennewwindow = atoi(value);
1769 } else if(!strcmp(name, "opennewwindow")) {
1770 i->config_opennewwindow = atoi(value);
1771 } else if(!strcmp(name, "storeallcharacters")) {
1772 i->config_storeallcharacters = atoi(value);
1773 } else if(!strcmp(name, "enablezlib")) {
1774 i->config_enablezlib = atoi(value);
1775 } else if(!strcmp(name, "bboxvars")) {
1776 i->config_bboxvars = atoi(value);
1777 } else if(!strcmp(name, "reordertags")) {
1778 i->config_reordertags = atoi(value);
1779 } else if(!strcmp(name, "internallinkfunction")) {
1780 i->config_internallinkfunction = strdup(value);
1781 } else if(!strcmp(name, "externallinkfunction")) {
1782 i->config_externallinkfunction = strdup(value);
1783 } else if(!strcmp(name, "linkfunction")) { //sets both internallinkfunction and externallinkfunction
1784 i->config_internallinkfunction = strdup(value);
1785 i->config_externallinkfunction = strdup(value);
1786 } else if(!strcmp(name, "disable_polygon_conversion")) {
1787 i->config_disable_polygon_conversion = atoi(value);
1788 } else if(!strcmp(name, "insertstop")) {
1789 i->config_insertstoptag = atoi(value);
1790 } else if(!strcmp(name, "protect")) {
1791 i->config_protect = atoi(value);
1792 if(i->config_protect && i->tag) {
1793 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1795 } else if(!strcmp(name, "faketags")) {
1796 i->config_generate_fake_tags = atoi(value);
1797 } else if(!strcmp(name, "flashversion")) {
1798 i->config_flashversion = atoi(value);
1800 i->swf->fileVersion = i->config_flashversion;
1802 } else if(!strcmp(name, "framerate")) {
1803 i->config_framerate = atoi(value);
1805 i->swf->frameRate = i->config_framerate*0x100;
1807 } else if(!strcmp(name, "minlinewidth")) {
1808 i->config_minlinewidth = atof(value);
1809 } else if(!strcmp(name, "caplinewidth")) {
1810 i->config_caplinewidth = atof(value);
1811 } else if(!strcmp(name, "linktarget")) {
1812 i->config_linktarget = strdup(value);
1813 } else if(!strcmp(name, "dumpfonts")) {
1814 i->config_dumpfonts = atoi(value);
1815 } else if(!strcmp(name, "animate")) {
1816 i->config_animate = atoi(value);
1817 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1819 } else if(!strcmp(name, "jpegquality")) {
1820 int val = atoi(value);
1822 if(val>101) val=101;
1823 i->config_jpegquality = val;
1824 } else if(!strcmp(name, "splinequality")) {
1825 int v = atoi(value);
1826 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1828 i->config_splinemaxerror = v;
1829 } else if(!strcmp(name, "fontquality")) {
1830 int v = atoi(value);
1831 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1833 i->config_fontsplinemaxerror = v;
1834 } else if(!strcmp(name, "linkcolor")) {
1835 if(strlen(value)!=8) {
1836 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1839 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1840 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1841 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1842 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1843 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1844 printf("%02x%02x%02x%02x\n",
1845 i->config_linkcolor.r,
1846 i->config_linkcolor.g,
1847 i->config_linkcolor.b,
1848 i->config_linkcolor.a);
1856 // --------------------------------------------------------------------
1858 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1861 swf_GetCXForm(0, &cx, 1);
1864 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1865 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1866 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1867 c->ar!=0 || c->ag!=0 || c->ab!=0)
1868 msg("<warning> CXForm not SWF-compatible");
1870 cx.a0 = (S16)(c->aa*256);
1871 cx.r0 = (S16)(c->rr*256);
1872 cx.g0 = (S16)(c->gg*256);
1873 cx.b0 = (S16)(c->bb*256);
1882 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1886 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1890 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1892 gfxdevice_t*dev = i->dev;
1894 RGBA*mem = (RGBA*)img->data;
1896 int sizex = img->width;
1897 int sizey = img->height;
1898 int is_jpeg = i->jpeg;
1901 int newsizex=sizex, newsizey=sizey;
1904 if(is_jpeg && i->config_jpegsubpixels) {
1905 newsizex = (int)(targetwidth*i->config_jpegsubpixels + 0.5);
1906 newsizey = (int)(targetheight*i->config_jpegsubpixels + 0.5);
1907 } else if(!is_jpeg && i->config_ppmsubpixels) {
1908 newsizex = (int)(targetwidth*i->config_ppmsubpixels + 0.5);
1909 newsizey = (int)(targetheight*i->config_ppmsubpixels + 0.5);
1913 if(sizex<=0 || sizey<=0)
1920 /* TODO: cache images */
1922 if(newsizex<sizex || newsizey<sizey) {
1923 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1924 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1925 *newwidth = sizex = newsizex;
1926 *newheight = sizey = newsizey;
1929 *newwidth = newsizex = sizex;
1930 *newheight = newsizey = sizey;
1933 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1934 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1936 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1938 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1939 is_jpeg?"jpeg-":"", i->currentswfid+1,
1941 targetwidth, targetheight,
1942 /*newsizex, newsizey,*/
1943 num_colors>256?">":"", num_colors>256?256:num_colors);
1945 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1946 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1948 for(t=0;t<num_colors;t++) {
1949 printf("%02x%02x%02x%02x ",
1950 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1957 int cacheid = imageInCache(dev, mem, sizex, sizey);
1960 bitid = getNewID(dev);
1961 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1962 addImageToCache(dev, mem, sizex, sizey);
1972 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1974 gfxbbox_t bbox = gfxline_getbbox(line);
1976 r.xmin = (int)(bbox.xmin*20);
1977 r.ymin = (int)(bbox.ymin*20);
1978 r.xmax = (int)(bbox.xmax*20);
1979 r.ymax = (int)(bbox.ymax*20);
1983 int line_is_empty(gfxline_t*line)
1986 if(line->type != gfx_moveTo)
1993 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1995 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1997 if(line_is_empty(line))
2003 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2004 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2006 int newwidth=0,newheight=0;
2007 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2010 double fx = (double)img->width / (double)newwidth;
2011 double fy = (double)img->height / (double)newheight;
2016 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2017 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2018 m.tx = (int)(matrix->tx*20);
2019 m.ty = (int)(matrix->ty*20);
2022 int myshapeid = getNewID(dev);
2023 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2025 swf_ShapeNew(&shape);
2026 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2027 swf_SetU16(i->tag, myshapeid);
2028 SRECT r = gfxline_getSWFbbox(line);
2029 swf_SetRect(i->tag,&r);
2030 swf_SetShapeStyles(i->tag,shape);
2031 swf_ShapeCountBits(shape,NULL,NULL);
2032 swf_SetShapeBits(i->tag,shape);
2033 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2034 i->swflastx = i->swflasty = UNDEFINED_COORD;
2035 drawgfxline(dev, line);
2036 swf_ShapeSetEnd(i->tag);
2037 swf_ShapeFree(shape);
2039 msg("<trace> Placing bitmap ID %d", myshapeid);
2040 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2041 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2042 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2045 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2047 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2052 if(i->clippos >= 127)
2054 msg("<warning> Too many clip levels.");
2058 int myshapeid = getNewID(dev);
2059 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2061 memset(&col, 0, sizeof(RGBA));
2064 swf_ShapeNew(&shape);
2065 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2067 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2068 swf_ShapeAddSolidFillStyle(shape,&markcol);
2070 swf_SetU16(i->tag,myshapeid);
2071 SRECT r = gfxline_getSWFbbox(line);
2072 swf_SetRect(i->tag,&r);
2073 swf_SetShapeStyles(i->tag,shape);
2074 swf_ShapeCountBits(shape,NULL,NULL);
2075 swf_SetShapeBits(i->tag,shape);
2076 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2077 i->swflastx = i->swflasty = UNDEFINED_COORD;
2078 drawgfxline(dev, line);
2079 swf_ShapeSetEnd(i->tag);
2080 swf_ShapeFree(shape);
2082 /* TODO: remember the bbox, and check all shapes against it */
2084 msg("<trace> Placing clip ID %d", myshapeid);
2085 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2086 i->cliptags[i->clippos] = i->tag;
2087 i->clipshapes[i->clippos] = myshapeid;
2088 i->clipdepths[i->clippos] = getNewDepth(dev);
2092 static void swf_endclip(gfxdevice_t*dev)
2094 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2101 msg("<error> Invalid end of clipping region");
2105 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2106 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2108 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2110 static int gfxline_type(gfxline_t*line)
2116 int haszerosegments=0;
2119 if(line->type == gfx_moveTo) {
2122 } else if(line->type == gfx_lineTo) {
2126 } else if(line->type == gfx_splineTo) {
2128 if(tmpsplines>lines)
2136 if(lines==0 && splines==0) return 0;
2137 else if(lines==1 && splines==0) return 1;
2138 else if(lines==0 && splines==1) return 2;
2139 else if(splines==0) return 3;
2143 static int gfxline_has_dots(gfxline_t*line)
2151 if(line->type == gfx_moveTo) {
2152 /* test the length of the preceding line, and assume it is a dot if
2153 it's length is less than 1.0. But *only* if there's a noticable
2154 gap between the previous line and the next moveTo. (I've come
2155 across a PDF where thousands of "dots" were stringed together,
2157 int last_short_gap = short_gap;
2158 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2163 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2168 } else if(line->type == gfx_lineTo) {
2169 dist += fabs(line->x - x) + fabs(line->y - y);
2171 } else if(line->type == gfx_splineTo) {
2172 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2173 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2180 if(isline && dist < 1 && !short_gap) {
2186 static int gfxline_fix_short_edges(gfxline_t*line)
2190 if(line->type == gfx_lineTo) {
2191 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2194 } else if(line->type == gfx_splineTo) {
2195 if(fabs(line->sx - x) + fabs(line->sy - y) +
2196 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2207 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2209 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2210 if(x<i->min_x || x>i->max_x) return 0;
2211 if(y<i->min_y || y>i->max_y) return 0;
2215 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2217 gfxline_t*l = line = gfxline_clone(line);
2229 //#define NORMALIZE_POLYGON_POSITIONS
2231 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)
2233 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2234 if(line_is_empty(line))
2236 int type = gfxline_type(line);
2237 int has_dots = gfxline_has_dots(line);
2238 gfxbbox_t r = gfxline_getbbox(line);
2239 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2241 /* TODO: * split line into segments, and perform this check for all segments */
2243 if(i->config_disable_polygon_conversion || type>=5 ||
2245 (width <= i->config_caplinewidth
2246 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2247 || (cap_style == gfx_capRound && type<=2)))) {} else
2249 /* convert line to polygon */
2250 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2252 gfxline_fix_short_edges(line);
2253 /* we need to convert the line into a polygon */
2254 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2255 gfxline_t*gfxline = SVPtogfxline(svp);
2256 dev->fill(dev, gfxline, color);
2262 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2265 #ifdef NORMALIZE_POLYGON_POSITIONS
2267 double startx = 0, starty = 0;
2268 if(line && line->type == gfx_moveTo) {
2272 line = gfxline_move(line, -startx, -starty);
2273 i->shapeposx = (int)(startx*20);
2274 i->shapeposy = (int)(starty*20);
2277 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2278 swfoutput_setlinewidth(dev, width);
2281 drawgfxline(dev, line);
2283 #ifdef NORMALIZE_POLYGON_POSITIONS
2284 free(line); //account for _move
2288 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2290 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2291 if(line_is_empty(line))
2295 gfxbbox_t r = gfxline_getbbox(line);
2296 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2299 if(!i->config_ignoredraworder)
2302 #ifdef NORMALIZE_POLYGON_POSITIONS
2304 double startx = 0, starty = 0;
2305 if(line && line->type == gfx_moveTo) {
2309 line = gfxline_move(line, -startx, -starty);
2310 i->shapeposx = (int)(startx*20);
2311 i->shapeposy = (int)(starty*20);
2314 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2318 drawgfxline(dev, line);
2319 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2321 #ifdef NORMALIZE_POLYGON_POSITIONS
2322 free(line); //account for _move
2325 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2327 if(line_is_empty(line))
2329 msg("<error> Gradient filling not implemented yet");
2332 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2334 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2336 SRECT bounds = {0,0,0,0};
2338 swffont->version = 2;
2339 swffont->name = (U8*)strdup(id);
2340 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2341 swffont->layout->ascent = 0; /* ? */
2342 swffont->layout->descent = 0;
2343 swffont->layout->leading = 0;
2344 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2345 swffont->encoding = FONT_ENCODING_UNICODE;
2346 swffont->numchars = font->num_glyphs;
2347 swffont->maxascii = font->max_unicode;
2348 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2349 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2350 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2351 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2352 for(t=0;t<font->max_unicode;t++) {
2353 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2355 for(t=0;t<font->num_glyphs;t++) {
2359 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2360 if(swffont->glyph2ascii[t] == 0xffff || swffont->glyph2ascii[t] == 0x0000) {
2361 /* flash 8 flashtype requires unique unicode IDs for each character.
2362 We use the Unicode private user area to assign characters, hoping that
2363 the font doesn't contain more than 2048 glyphs */
2364 swffont->glyph2ascii[t] = 0xe000 + (t&0x1fff);
2367 if(font->glyphs[t].name) {
2368 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2370 swffont->glyphnames[t] = 0;
2372 advance = (int)(font->glyphs[t].advance);
2374 swf_Shape01DrawerInit(&draw, 0);
2375 line = font->glyphs[t].line;
2378 c.x = line->sx * GLYPH_SCALE; c.y = line->sy * GLYPH_SCALE;
2379 to.x = line->x * GLYPH_SCALE; to.y = line->y * GLYPH_SCALE;
2380 if(line->type == gfx_moveTo) {
2381 draw.moveTo(&draw, &to);
2382 } else if(line->type == gfx_lineTo) {
2383 draw.lineTo(&draw, &to);
2384 } else if(line->type == gfx_splineTo) {
2385 draw.splineTo(&draw, &c, &to);
2390 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2391 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2393 if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2394 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2395 advance = swffont->layout->bounds[t].xmax;
2399 swffont->glyph[t].advance = advance;
2401 swffont->glyph[t].advance = 32767;
2404 draw.dealloc(&draw);
2406 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2408 if(bounds.ymin < 0 && bounds.ymax > 0) {
2409 swffont->layout->ascent = -bounds.ymin;
2410 swffont->layout->descent = bounds.ymax;
2411 swffont->layout->leading = bounds.ymax - bounds.ymin;
2413 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2414 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2415 swffont->layout->leading = bounds.ymax - bounds.ymin;
2421 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2423 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2425 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2426 return; // the requested font is the current font
2428 fontlist_t*last=0,*l = i->fontlist;
2431 if(!strcmp((char*)l->swffont->name, font->id)) {
2432 return; // we already know this font
2436 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2437 l->swffont = gfxfont_to_swffont(font, font->id);
2444 swf_FontSetID(l->swffont, getNewID(i->dev));
2446 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2448 // print font information
2449 msg("<debug> Font %s",font->id);
2450 msg("<debug> | ID: %d", l->swffont->id);
2451 msg("<debug> | Version: %d", l->swffont->version);
2452 msg("<debug> | Name: %s", l->swffont->name);
2453 msg("<debug> | Numchars: %d", l->swffont->numchars);
2454 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2455 msg("<debug> | Style: %d", l->swffont->style);
2456 msg("<debug> | Encoding: %d", l->swffont->encoding);
2457 for(iii=0; iii<l->swffont->numchars;iii++) {
2458 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,
2459 l->swffont->layout->bounds[iii].xmin/20.0,
2460 l->swffont->layout->bounds[iii].ymin/20.0,
2461 l->swffont->layout->bounds[iii].xmax/20.0,
2462 l->swffont->layout->bounds[iii].ymax/20.0
2465 for(t=0;t<l->swffont->maxascii;t++) {
2466 if(l->swffont->ascii2glyph[t] == iii)
2467 msg("<debug> | - maps to %d",t);
2473 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2475 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2477 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2478 return; // the requested font is the current font
2480 fontlist_t*l = i->fontlist;
2482 if(!strcmp((char*)l->swffont->name, fontid)) {
2483 i->swffont = l->swffont;
2488 msg("<error> Unknown font id: %s", fontid);
2492 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2494 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2498 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2500 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2501 with multiple fonts */
2504 swf_switchfont(dev, font->id); // set the current font
2506 setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2508 /* printf("%f %f\n", m.m31, m.m32);
2510 static int xpos = 40;
2511 static int ypos = 200;
2518 drawchar(dev, i->swffont, glyph, matrix->tx, matrix->ty, color);