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;
133 int pic_height[1024];
139 char fillstylechanged;
141 int jpeg; //next image type
146 chardata_t chardata[CHARDATAMAX];
154 double fontm11,fontm12,fontm21,fontm22;
165 } swfoutput_internal;
167 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
168 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
169 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);
170 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
171 static void swf_endclip(gfxdevice_t*dev);
172 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);
173 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
174 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
175 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
176 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
177 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
178 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
179 static void swf_startframe(gfxdevice_t*dev, int width, int height);
180 static void swf_endframe(gfxdevice_t*dev);
181 static gfxresult_t* swf_finish(gfxdevice_t*driver);
183 static swfoutput_internal* init_internal_struct()
185 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
186 memset(i, 0, sizeof(swfoutput_internal));
210 i->fillstylechanged = 0;
217 i->config_dumpfonts=0;
218 i->config_ppmsubpixels=0;
219 i->config_jpegsubpixels=0;
220 i->config_opennewwindow=1;
221 i->config_ignoredraworder=0;
222 i->config_drawonlyshapes=0;
223 i->config_jpegquality=85;
224 i->config_storeallcharacters=0;
225 i->config_enablezlib=0;
226 i->config_generate_fake_tags=0;
227 i->config_insertstoptag=0;
228 i->config_flashversion=6;
229 i->config_splinemaxerror=1;
230 i->config_fontsplinemaxerror=1;
231 i->config_filloverlap=0;
233 i->config_bboxvars=0;
234 i->config_minlinewidth=0.05;
235 i->config_caplinewidth=1;
236 i->config_linktarget=0;
237 i->config_internallinkfunction=0;
238 i->config_externallinkfunction=0;
239 i->config_reordertags=1;
241 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
242 i->config_linkcolor.a = 0x40;
247 static int id_error = 0;
249 static U16 getNewID(gfxdevice_t* dev)
251 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
252 if(i->currentswfid == 65535) {
254 msg("<error> ID Table overflow");
255 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
261 return ++i->currentswfid;
263 static U16 getNewDepth(gfxdevice_t* dev)
265 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
266 if(i->depth == 65535) {
268 msg("<error> Depth Table overflow");
269 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
278 static void startshape(gfxdevice_t* dev);
279 static void starttext(gfxdevice_t* dev);
280 static void endshape(gfxdevice_t* dev);
281 static void endtext(gfxdevice_t* dev);
283 typedef struct _plotxy
288 // write a move-to command into the swf
289 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
291 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
292 int rx = (int)(p0.x*20);
293 int ry = (int)(p0.y*20);
294 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
295 swf_ShapeSetMove (tag, i->shape, rx,ry);
296 i->fillstylechanged = 0;
303 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
305 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
309 return movetoxy(dev, tag, p);
311 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
313 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
319 swf_ExpandRect(&i->bboxrect, p);
321 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
325 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
327 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
328 int width = i->linewidth/4;
332 //swf_ShapeSetLine(tag, i->shape,-width,-width);
333 //swf_ShapeSetLine(tag, i->shape,width*2,0);
334 //swf_ShapeSetLine(tag, i->shape,0,width*2);
335 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
336 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
337 //swf_ShapeSetLine(tag, i->shape,width,width);
340 swf_ShapeSetLine(tag, i->shape,-width,0);
341 swf_ShapeSetLine(tag, i->shape,width,-width);
342 swf_ShapeSetLine(tag, i->shape,width,width);
343 swf_ShapeSetLine(tag, i->shape,-width,width);
344 swf_ShapeSetLine(tag, i->shape,-width,-width);
345 swf_ShapeSetLine(tag, i->shape,width,0);
347 addPointToBBox(dev, x-width ,y-width);
348 addPointToBBox(dev, x+width ,y+width);
351 // write a line-to command into the swf
352 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
354 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
355 int px = (int)(p0.x*20);
356 int py = (int)(p0.y*20);
357 int rx = (px-i->swflastx);
358 int ry = (py-i->swflasty);
360 swf_ShapeSetLine (tag, i->shape, rx,ry);
361 addPointToBBox(dev, i->swflastx,i->swflasty);
362 addPointToBBox(dev, px,py);
363 }/* else if(!i->fill) {
364 // treat lines of length 0 as plots, making them
365 // at least 1 twip wide so Flash will display them
366 plot(dev, i->swflastx, i->swflasty, tag);
373 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
378 linetoxy(dev,tag, p);
381 // write a spline-to command into the swf
382 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
384 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
385 int lastlastx = i->swflastx;
386 int lastlasty = i->swflasty;
388 int cx = ((int)(control.x*20)-i->swflastx);
389 int cy = ((int)(control.y*20)-i->swflasty);
392 int ex = ((int)(end.x*20)-i->swflastx);
393 int ey = ((int)(end.y*20)-i->swflasty);
397 if((cx || cy) && (ex || ey)) {
398 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
399 addPointToBBox(dev, lastlastx ,lastlasty );
400 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
401 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
402 } else if(cx || cy || ex || ey) {
403 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
404 addPointToBBox(dev, lastlastx ,lastlasty );
405 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
406 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
412 /* write a line, given two points and the transformation
414 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
416 moveto(dev, tag, p0);
417 lineto(dev, tag, p1);
420 void resetdrawer(gfxdevice_t*dev)
422 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
427 static void stopFill(gfxdevice_t*dev)
429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
430 if(i->lastwasfill!=0)
432 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
433 i->fillstylechanged = 1;
437 static void startFill(gfxdevice_t*dev)
439 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
440 if(i->lastwasfill!=1)
442 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
443 i->fillstylechanged = 1;
448 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
460 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
462 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
465 memset(&r, 0, sizeof(r));
468 if(debug) printf("\n");
469 for(t=0;t<i->chardatapos;t++)
471 if(i->chardata[t].fontid != font->id) {
472 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
475 SRECT b = font->layout->bounds[i->chardata[t].charid];
476 b.xmin *= i->chardata[t].size;
477 b.ymin *= i->chardata[t].size;
478 b.xmax *= i->chardata[t].size;
479 b.ymax *= i->chardata[t].size;
481 /* divide by 1024, rounding xmax/ymax up */
482 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
484 b.xmin += i->chardata[t].x;
485 b.ymin += i->chardata[t].y;
486 b.xmax += i->chardata[t].x;
487 b.ymax += i->chardata[t].y;
489 /* until we solve the INTERNAL_SCALING problem (see below)
490 make sure the bounding box is big enough */
496 b = swf_TurnRect(b, m);
498 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
499 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
500 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
501 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
502 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
507 i->chardata[t].fontid,
509 i->chardata[t].charid
511 swf_ExpandRect2(&r, &b);
513 if(debug) printf("-----> (%f,%f,%f,%f)\n",
521 static void putcharacters(gfxdevice_t*dev, TAG*tag)
523 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
527 color.r = i->chardata[0].color.r^255;
536 int charadvance[128];
539 int glyphbits=1; //TODO: can this be zero?
542 if(tag->id != ST_DEFINETEXT &&
543 tag->id != ST_DEFINETEXT2) {
544 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
547 if(!i->chardatapos) {
548 msg("<warning> putcharacters called with zero characters");
551 for(pass = 0; pass < 2; pass++)
561 advancebits++; // add sign bit
562 swf_SetU8(tag, glyphbits);
563 swf_SetU8(tag, advancebits);
566 for(t=0;t<=i->chardatapos;t++)
568 if(lastfontid != i->chardata[t].fontid ||
569 lastx!=i->chardata[t].x ||
570 lasty!=i->chardata[t].y ||
571 !colorcompare(dev,&color, &i->chardata[t].color) ||
573 lastsize != i->chardata[t].size ||
576 if(charstorepos && pass==0)
579 for(s=0;s<charstorepos;s++)
581 while(charids[s]>=(1<<glyphbits))
583 while(charadvance[s]>=(1<<advancebits))
587 if(charstorepos && pass==1)
589 tag->writeBit = 0; // Q&D
590 swf_SetBits(tag, 0, 1); // GLYPH Record
591 swf_SetBits(tag, charstorepos, 7); // number of glyphs
593 for(s=0;s<charstorepos;s++)
595 swf_SetBits(tag, charids[s], glyphbits);
596 swf_SetBits(tag, charadvance[s], advancebits);
601 if(pass == 1 && t<i->chardatapos)
607 if(lastx != i->chardata[t].x ||
608 lasty != i->chardata[t].y)
610 newx = i->chardata[t].x;
611 newy = i->chardata[t].y;
617 if(!colorcompare(dev,&color, &i->chardata[t].color))
619 color = i->chardata[t].color;
622 font.id = i->chardata[t].fontid;
623 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
626 tag->writeBit = 0; // Q&D
627 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
630 lastfontid = i->chardata[t].fontid;
631 lastx = i->chardata[t].x;
632 lasty = i->chardata[t].y;
633 lastsize = i->chardata[t].size;
636 if(t==i->chardatapos)
640 int nextt = t==i->chardatapos-1?t:t+1;
641 int rel = i->chardata[nextt].x-i->chardata[t].x;
642 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
644 lastx=i->chardata[nextt].x;
648 lastx=i->chardata[t].x;
650 charids[charstorepos] = i->chardata[t].charid;
651 charadvance[charstorepos] = advance;
658 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
660 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
661 if(i->chardatapos == CHARDATAMAX)
663 msg("<warning> Character buffer too small. SWF will be slightly bigger");
667 i->chardata[i->chardatapos].fontid = fontid;
668 i->chardata[i->chardatapos].charid = charid;
669 i->chardata[i->chardatapos].x = x;
670 i->chardata[i->chardatapos].y = y;
671 i->chardata[i->chardatapos].color = color;
672 i->chardata[i->chardatapos].size = size;
676 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
677 So if we set this value to high, the char coordinates will overflow.
678 If we set it to low, however, the char positions will be inaccurate */
679 #define FONT_INTERNAL_SIZE 4
681 /* process a character. */
682 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix_t*m, gfxcolor_t*col)
684 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
686 msg("<warning> Font is NULL");
690 if(charid<0 || charid>=swffont->numchars) {
691 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
694 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
695 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
696 charid, FIXNULL((char*)swffont->name), swffont->numchars);
707 float det = ((m->m11*m->m22)-(m->m21*m->m12));
708 if(fabs(det) < 0.0005) {
709 /* x direction equals y direction- the text is invisible */
712 det = 20*FONT_INTERNAL_SIZE / det;
715 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
716 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
718 RGBA rgba = *(RGBA*)col;
719 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
720 swf_FontUseGlyph(swffont, charid);
724 static void endtext(gfxdevice_t*dev)
726 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
730 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
731 swf_SetU16(i->tag, i->textid);
734 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
736 swf_SetRect(i->tag,&r);
738 swf_SetMatrix(i->tag,&i->fontmatrix);
740 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
742 putcharacters(dev, i->tag);
745 if(i->swf->fileVersion >= 8) {
746 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
747 swf_SetU16(i->tag, i->textid);
748 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
749 //swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
750 //swf_SetU8(i->tag, /*grid*/(0<<3)|/*flashtype*/0x40);
751 swf_SetU8(i->tag, /*grid*/(1<<3)|/*no flashtype*/0x00);
752 swf_SetU32(i->tag, 0);//thickness
753 swf_SetU32(i->tag, 0);//sharpness
754 swf_SetU8(i->tag, 0);//reserved
756 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
758 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
762 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
763 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
764 double m12,double m22)
770 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
771 if(i->fontm11 == m11 &&
784 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
785 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
791 static void endpage(gfxdevice_t*dev)
793 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
803 void swf_startframe(gfxdevice_t*dev, int width, int height)
805 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
806 if(!i->firstpage && !i->pagefinished)
808 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
810 swf_GetMatrix(0, &i->page_matrix);
811 i->page_matrix.tx = 0;
812 i->page_matrix.ty = 0;
818 /* set clipping/background rectangle */
819 /* TODO: this should all be done in SWFOutputDev */
820 //setBackground(dev, x1, y1, x2, y2);
822 /* increase SWF's bounding box */
828 swf_ExpandRect2(&i->swf->movieSize, &r);
830 i->lastframeno = i->frameno;
835 void swf_endframe(gfxdevice_t*dev)
837 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
842 if(i->config_insertstoptag) {
844 atag = action_Stop(atag);
845 atag = action_End(atag);
846 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
847 swf_ActionSet(i->tag,atag);
849 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
852 for(i->depth;i->depth>i->startdepth;i->depth--) {
853 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
854 swf_SetU16(i->tag,i->depth);
856 i->depth = i->startdepth;
859 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
861 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
863 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
867 int shapeid = getNewID(dev);
872 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
874 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
875 swf_SetU16(i->tag,shapeid);
876 swf_SetRect(i->tag,&r);
877 swf_SetShapeHeader(i->tag,s);
878 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
879 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
880 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
881 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
882 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
883 swf_ShapeSetEnd(i->tag);
885 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
886 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
887 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
888 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
891 /* initialize the swf writer */
892 void gfxdevice_swf_init(gfxdevice_t* dev)
894 memset(dev, 0, sizeof(gfxdevice_t));
898 dev->internal = init_internal_struct();
900 dev->startpage = swf_startframe;
901 dev->endpage = swf_endframe;
902 dev->finish = swf_finish;
903 dev->fillbitmap = swf_fillbitmap;
904 dev->setparameter = swf_setparameter;
905 dev->stroke = swf_stroke;
906 dev->startclip = swf_startclip;
907 dev->endclip = swf_endclip;
908 dev->fill = swf_fill;
909 dev->fillbitmap = swf_fillbitmap;
910 dev->fillgradient = swf_fillgradient;
911 dev->addfont = swf_addfont;
912 dev->drawchar = swf_drawchar;
913 dev->drawlink = swf_drawlink;
915 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
921 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
925 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
926 i->swf->fileVersion = i->config_flashversion;
927 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
928 i->swf->movieSize.xmin = 0;
929 i->swf->movieSize.ymin = 0;
930 i->swf->movieSize.xmax = 0;
931 i->swf->movieSize.ymax = 0;
933 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
934 i->tag = i->swf->firstTag;
935 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
936 swf_SetRGB(i->tag,&rgb);
938 i->startdepth = i->depth = 0;
940 if(i->config_protect)
941 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
944 static void startshape(gfxdevice_t*dev)
946 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
955 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
957 swf_ShapeNew(&i->shape);
958 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
959 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
961 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
962 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
965 i->shapeid = getNewID(dev);
967 msg("<debug> Using shape id %d", i->shapeid);
969 swf_SetU16(i->tag,i->shapeid); // ID
971 i->bboxrectpos = i->tag->len;
975 r.xmax = 20*i->max_x;
976 r.ymax = 20*i->max_y;
977 swf_SetRect(i->tag,&r);
979 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
981 swf_SetShapeStyles(i->tag,i->shape);
982 swf_ShapeCountBits(i->shape,NULL,NULL);
983 swf_SetShapeBits(i->tag,i->shape);
985 /* TODO: do we really need this? */
986 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
987 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
988 i->swflastx=i->swflasty=UNDEFINED_COORD;
993 static void starttext(gfxdevice_t*dev)
995 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
999 i->textid = getNewID(dev);
1001 i->swflastx=i->swflasty=0;
1005 /* TODO: move to ../lib/rfxswf */
1006 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1008 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1009 /* determine length of old rect */
1013 swf_GetRect(tag, &old);
1014 swf_ResetReadBits(tag);
1015 int pos_end = tag->pos;
1017 int len = tag->len - pos_end;
1018 U8*data = (U8*)malloc(len);
1019 memcpy(data, &tag->data[pos_end], len);
1022 swf_SetRect(tag, newrect);
1023 swf_SetBlock(tag, data, len);
1025 tag->pos = tag->readBit = 0;
1028 void cancelshape(gfxdevice_t*dev)
1030 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1031 /* delete old shape tag */
1033 i->tag = i->tag->prev;
1034 swf_DeleteTag(todel);
1035 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1037 i->bboxrectpos = -1;
1039 // i->currentswfid--; // this was an *exceptionally* bad idea
1042 void fixAreas(gfxdevice_t*dev)
1044 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1045 if(!i->shapeisempty && i->fill &&
1046 (i->bboxrect.xmin == i->bboxrect.xmax ||
1047 i->bboxrect.ymin == i->bboxrect.ymax) &&
1048 i->config_minlinewidth >= 0.001
1050 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1051 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1052 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1055 SRECT r = i->bboxrect;
1057 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1058 /* this thing comes down to a single dot- nothing to fix here */
1064 RGBA save_col = i->strokergb;
1065 int save_width = i->linewidth;
1067 i->strokergb = i->fillrgb;
1068 i->linewidth = (int)(i->config_minlinewidth*20);
1069 if(i->linewidth==0) i->linewidth = 1;
1074 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1075 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1077 i->strokergb = save_col;
1078 i->linewidth = save_width;
1083 static void endshape_noput(gfxdevice_t*dev)
1085 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1088 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1091 swf_ShapeFree(i->shape);
1099 static void endshape(gfxdevice_t*dev)
1101 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1107 if(i->shapeisempty ||
1109 (i->bboxrect.xmin == i->bboxrect.xmax &&
1110 i->bboxrect.ymin == i->bboxrect.ymax))
1112 // delete the shape again, we didn't do anything
1113 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1114 i->bboxrect.xmin /20.0,
1115 i->bboxrect.ymin /20.0,
1116 i->bboxrect.xmax /20.0,
1117 i->bboxrect.ymax /20.0
1123 swf_ShapeSetEnd(i->tag);
1125 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1127 msg("<trace> Placing shape ID %d", i->shapeid);
1129 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1130 MATRIX m = i->page_matrix;
1131 m.tx += i->shapeposx;
1132 m.ty += i->shapeposy;
1133 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1135 if(i->config_animate) {
1136 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1139 swf_ShapeFree(i->shape);
1142 i->bboxrectpos = -1;
1149 void wipeSWF(SWF*swf)
1151 TAG*tag = swf->firstTag;
1153 TAG*next = tag->next;
1154 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1155 tag->id != ST_END &&
1156 tag->id != ST_DOACTION &&
1157 tag->id != ST_SHOWFRAME) {
1165 void swfoutput_finalize(gfxdevice_t*dev)
1167 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1169 if(i->tag && i->tag->id == ST_END)
1170 return; //already done
1172 if(i->config_bboxvars) {
1173 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1175 a = action_PushString(a, "xmin");
1176 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1177 a = action_SetVariable(a);
1178 a = action_PushString(a, "ymin");
1179 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1180 a = action_SetVariable(a);
1181 a = action_PushString(a, "xmax");
1182 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1183 a = action_SetVariable(a);
1184 a = action_PushString(a, "ymax");
1185 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1186 a = action_SetVariable(a);
1187 a = action_PushString(a, "width");
1188 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1189 a = action_SetVariable(a);
1190 a = action_PushString(a, "height");
1191 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1192 a = action_SetVariable(a);
1194 swf_ActionSet(tag, a);
1198 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1202 free(i->mark);i->mark = 0;
1206 fontlist_t *tmp,*iterator = i->fontlist;
1208 TAG*mtag = i->swf->firstTag;
1209 if(iterator->swffont) {
1210 if(!i->config_storeallcharacters) {
1211 msg("<debug> Reducing font %s", iterator->swffont->name);
1212 swf_FontReduce(iterator->swffont);
1214 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1215 if(i->config_storeallcharacters || used) {
1216 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1217 swf_FontSetDefine2(mtag, iterator->swffont);
1221 iterator = iterator->next;
1223 i->tag = swf_InsertTag(i->tag,ST_END);
1224 TAG* tag = i->tag->prev;
1226 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1227 and the ST_END- they confuse the flash player */
1228 while(tag->id == ST_REMOVEOBJECT2) {
1229 TAG* prev = tag->prev;
1237 if(i->config_enablezlib || i->config_flashversion>=6) {
1238 i->swf->compressed = 1;
1241 if(i->config_reordertags)
1242 swf_Optimize(i->swf);
1245 int swfresult_save(gfxresult_t*gfx, char*filename)
1247 SWF*swf = (SWF*)gfx->internal;
1250 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1255 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1259 if(swf->compressed) {
1260 if FAILED(swf_WriteSWC(fi,swf))
1261 msg("<error> WriteSWC() failed.\n");
1263 if FAILED(swf_WriteSWF(fi,swf))
1264 msg("<error> WriteSWF() failed.\n");
1271 void* swfresult_get(gfxresult_t*gfx, char*name)
1273 SWF*swf = (SWF*)gfx->internal;
1274 if(!strcmp(name, "swf")) {
1275 return (void*)swf_CopySWF(swf);
1276 } else if(!strcmp(name, "xmin")) {
1277 return (void*)(swf->movieSize.xmin/20);
1278 } else if(!strcmp(name, "ymin")) {
1279 return (void*)(swf->movieSize.ymin/20);
1280 } else if(!strcmp(name, "xmax")) {
1281 return (void*)(swf->movieSize.xmax/20);
1282 } else if(!strcmp(name, "ymax")) {
1283 return (void*)(swf->movieSize.ymax/20);
1284 } else if(!strcmp(name, "width")) {
1285 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1286 } else if(!strcmp(name, "height")) {
1287 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1291 void swfresult_destroy(gfxresult_t*gfx)
1294 swf_FreeTags((SWF*)gfx->internal);
1295 free(gfx->internal);
1298 memset(gfx, 0, sizeof(gfxresult_t));
1302 static void swfoutput_destroy(gfxdevice_t* dev);
1304 gfxresult_t* swf_finish(gfxdevice_t* dev)
1306 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1309 if(i->config_linktarget) {
1310 free(i->config_linktarget);
1311 i->config_linktarget = 0;
1314 swfoutput_finalize(dev);
1315 SWF* swf = i->swf;i->swf = 0;
1316 swfoutput_destroy(dev);
1318 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1319 result->internal = swf;
1320 result->save = swfresult_save;
1322 result->get = swfresult_get;
1323 result->destroy = swfresult_destroy;
1327 /* Perform cleaning up */
1328 static void swfoutput_destroy(gfxdevice_t* dev)
1330 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1332 /* not initialized yet- nothing to destroy */
1336 fontlist_t *tmp,*iterator = i->fontlist;
1338 if(iterator->swffont) {
1339 swf_FontFree(iterator->swffont);iterator->swffont=0;
1342 iterator = iterator->next;
1345 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1348 memset(dev, 0, sizeof(gfxdevice_t));
1351 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1353 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1354 if(i->fillrgb.r == r &&
1355 i->fillrgb.g == g &&
1356 i->fillrgb.b == b &&
1357 i->fillrgb.a == a) return;
1367 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1369 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1370 if(i->strokergb.r == r &&
1371 i->strokergb.g == g &&
1372 i->strokergb.b == b &&
1373 i->strokergb.a == a) return;
1383 //#define ROUND_UP 19
1384 //#define ROUND_UP 10
1386 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1388 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1389 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1393 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1397 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1398 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1399 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1400 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1402 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1404 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1405 dev->drawlink(dev, points, url);
1408 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1410 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1412 if(!strncmp("http://pdf2swf:", url, 15)) {
1413 char*tmp = strdup(url);
1414 int l = strlen(tmp);
1417 swfoutput_namedlink(dev, tmp+15, points);
1420 } else if(!strncmp("page", url, 4)) {
1423 if(url[t]<'0' || url[t]>'9')
1426 int page = atoi(&url[4]);
1427 if(page<0) page = 0;
1428 swfoutput_linktopage(dev, page, points);
1431 swfoutput_linktourl(dev, url, points);
1434 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1436 ActionTAG* actions = 0;
1437 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1443 if(i->config_externallinkfunction) {
1444 actions = action_PushString(actions, url); //parameter
1445 actions = action_PushInt(actions, 1); //number of parameters (1)
1446 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1447 actions = action_CallFunction(actions);
1448 } else if(!i->config_linktarget) {
1449 if(!i->config_opennewwindow)
1450 actions = action_GetUrl(actions, url, "_parent");
1452 actions = action_GetUrl(actions, url, "_this");
1454 actions = action_GetUrl(actions, url, i->config_linktarget);
1456 actions = action_End(actions);
1458 drawlink(dev, actions, 0, points,0);
1460 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1462 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1463 ActionTAG* actions = 0;
1470 if(!i->config_internallinkfunction) {
1471 actions = action_GotoFrame(actions, page-1);
1472 actions = action_End(actions);
1474 actions = action_PushInt(actions, page); //parameter
1475 actions = action_PushInt(actions, 1); //number of parameters (1)
1476 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1477 actions = action_CallFunction(actions);
1478 actions = action_End(actions);
1481 drawlink(dev, actions, 0, points,0);
1484 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1485 of the viewer objects, like subtitles, index elements etc.
1487 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1489 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1490 ActionTAG *actions1,*actions2;
1491 char*tmp = strdup(name);
1499 if(!strncmp(tmp, "call:", 5))
1501 char*x = strchr(&tmp[5], ':');
1503 actions1 = action_PushInt(0, 0); //number of parameters (0)
1504 actions1 = action_PushString(actions1, &tmp[5]); //function name
1505 actions1 = action_CallFunction(actions1);
1506 actions1 = action_End(actions1);
1509 actions1 = action_PushString(0, x+1); //parameter
1510 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1511 actions1 = action_PushString(actions1, &tmp[5]); //function name
1512 actions1 = action_CallFunction(actions1);
1513 actions1 = action_End(actions1);
1515 actions2 = action_End(0);
1520 actions1 = action_PushString(0, "/:subtitle");
1521 actions1 = action_PushString(actions1, name);
1522 actions1 = action_SetVariable(actions1);
1523 actions1 = action_End(actions1);
1525 actions2 = action_PushString(0, "/:subtitle");
1526 actions2 = action_PushString(actions2, "");
1527 actions2 = action_SetVariable(actions2);
1528 actions2 = action_End(actions2);
1531 drawlink(dev, actions1, actions2, points,mouseover);
1533 swf_ActionFree(actions1);
1534 swf_ActionFree(actions2);
1538 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1540 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1541 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1543 int lines= 0, splines=0;
1547 /* check whether the next segment is zero */
1548 if(line->type == gfx_moveTo) {
1549 moveto(dev, i->tag, line->x, line->y);
1550 px = lastx = line->x;
1551 py = lasty = line->y;
1553 } if(line->type == gfx_lineTo) {
1554 lineto(dev, i->tag, line->x, line->y);
1559 } else if(line->type == gfx_splineTo) {
1561 s.x = line->sx;p.x = line->x;
1562 s.y = line->sy;p.y = line->y;
1563 splineto(dev, i->tag, s, p);
1571 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1575 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1577 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1582 plotxy_t p1,p2,p3,p4;
1588 int buttonid = getNewID(dev);
1589 gfxbbox_t bbox = gfxline_getbbox(points);
1592 myshapeid = getNewID(dev);
1593 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1594 swf_ShapeNew(&i->shape);
1595 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1596 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1597 swf_SetU16(i->tag, myshapeid);
1598 r.xmin = (int)(bbox.xmin*20);
1599 r.ymin = (int)(bbox.ymin*20);
1600 r.xmax = (int)(bbox.xmax*20);
1601 r.ymax = (int)(bbox.ymax*20);
1602 swf_SetRect(i->tag,&r);
1603 swf_SetShapeStyles(i->tag,i->shape);
1604 swf_ShapeCountBits(i->shape,NULL,NULL);
1605 swf_SetShapeBits(i->tag,i->shape);
1606 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1607 i->swflastx = i->swflasty = 0;
1608 drawgfxline(dev, points);
1609 swf_ShapeSetEnd(i->tag);
1612 myshapeid2 = getNewID(dev);
1613 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1614 swf_ShapeNew(&i->shape);
1616 rgb = i->config_linkcolor;
1618 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1619 swf_SetU16(i->tag, myshapeid2);
1620 r.xmin = (int)(bbox.xmin*20);
1621 r.ymin = (int)(bbox.ymin*20);
1622 r.xmax = (int)(bbox.xmax*20);
1623 r.ymax = (int)(bbox.ymax*20);
1624 swf_SetRect(i->tag,&r);
1625 swf_SetShapeStyles(i->tag,i->shape);
1626 swf_ShapeCountBits(i->shape,NULL,NULL);
1627 swf_SetShapeBits(i->tag,i->shape);
1628 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1629 i->swflastx = i->swflasty = 0;
1630 drawgfxline(dev, points);
1631 swf_ShapeSetEnd(i->tag);
1635 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1636 swf_SetU16(i->tag,buttonid); //id
1637 swf_ButtonSetFlags(i->tag, 0); //menu=no
1638 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1639 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1640 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1641 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1642 swf_SetU8(i->tag,0);
1643 swf_ActionSet(i->tag,actions1);
1644 swf_SetU8(i->tag,0);
1648 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1649 swf_SetU16(i->tag,buttonid); //id
1650 swf_ButtonSetFlags(i->tag, 0); //menu=no
1651 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1652 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1653 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1654 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1655 swf_SetU8(i->tag,0); // end of button records
1656 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1657 swf_ActionSet(i->tag,actions1);
1659 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1660 swf_ActionSet(i->tag,actions2);
1661 swf_SetU8(i->tag,0);
1662 swf_ButtonPostProcess(i->tag, 2);
1664 swf_SetU8(i->tag,0);
1665 swf_ButtonPostProcess(i->tag, 1);
1669 msg("<trace> Placing link ID %d", buttonid);
1670 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1672 if(posx!=0 || posy!=0) {
1674 p.x = (int)(posx*20);
1675 p.y = (int)(posy*20);
1676 p = swf_TurnPoint(p, &i->page_matrix);
1681 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1683 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1690 for(t=0;t<picpos;t++)
1692 if(pic_xids[t] == xid &&
1693 pic_yids[t] == yid) {
1694 width = pic_width[t];
1695 height = pic_height[t];
1699 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1700 pic_xids[picpos] = xid;
1701 pic_yids[picpos] = yid;
1702 pic_width[picpos] = width;
1703 pic_height[picpos] = height;
1706 pic[width*y+x] = buf[0];
1710 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1711 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1715 xid += x*r+x*b*3+x*g*7+x*a*11;
1716 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1718 for(t=0;t<picpos;t++)
1720 if(pic_xids[t] == xid &&
1721 pic_yids[t] == yid) {
1730 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1732 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1734 msg("<trace> swfdevice: %s=%s", name, value);
1735 if(!strcmp(name, "jpegsubpixels")) {
1736 i->config_jpegsubpixels = atof(value);
1737 } else if(!strcmp(name, "ppmsubpixels")) {
1738 i->config_ppmsubpixels = atof(value);
1739 } else if(!strcmp(name, "subpixels")) {
1740 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1741 } else if(!strcmp(name, "drawonlyshapes")) {
1742 i->config_drawonlyshapes = atoi(value);
1743 } else if(!strcmp(name, "ignoredraworder")) {
1744 i->config_ignoredraworder = atoi(value);
1745 } else if(!strcmp(name, "mark")) {
1746 if(!value || !value[0]) {
1747 if(i->mark) free(i->mark);
1751 i->mark = strdup("...");
1752 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1754 } else if(!strcmp(name, "filloverlap")) {
1755 i->config_filloverlap = atoi(value);
1756 } else if(!strcmp(name, "linksopennewwindow")) {
1757 i->config_opennewwindow = atoi(value);
1758 } else if(!strcmp(name, "opennewwindow")) {
1759 i->config_opennewwindow = atoi(value);
1760 } else if(!strcmp(name, "storeallcharacters")) {
1761 i->config_storeallcharacters = atoi(value);
1762 } else if(!strcmp(name, "enablezlib")) {
1763 i->config_enablezlib = atoi(value);
1764 } else if(!strcmp(name, "bboxvars")) {
1765 i->config_bboxvars = atoi(value);
1766 } else if(!strcmp(name, "reordertags")) {
1767 i->config_reordertags = atoi(value);
1768 } else if(!strcmp(name, "internallinkfunction")) {
1769 i->config_internallinkfunction = strdup(value);
1770 } else if(!strcmp(name, "externallinkfunction")) {
1771 i->config_externallinkfunction = strdup(value);
1772 } else if(!strcmp(name, "disable_polygon_conversion")) {
1773 i->config_disable_polygon_conversion = atoi(value);
1774 } else if(!strcmp(name, "insertstop")) {
1775 i->config_insertstoptag = atoi(value);
1776 } else if(!strcmp(name, "protect")) {
1777 i->config_protect = atoi(value);
1778 if(i->config_protect && i->tag) {
1779 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1781 } else if(!strcmp(name, "faketags")) {
1782 i->config_generate_fake_tags = atoi(value);
1783 } else if(!strcmp(name, "flashversion")) {
1784 i->config_flashversion = atoi(value);
1786 i->swf->fileVersion = i->config_flashversion;
1788 } else if(!strcmp(name, "minlinewidth")) {
1789 i->config_minlinewidth = atof(value);
1790 } else if(!strcmp(name, "caplinewidth")) {
1791 i->config_caplinewidth = atof(value);
1792 } else if(!strcmp(name, "linktarget")) {
1793 i->config_linktarget = strdup(value);
1794 } else if(!strcmp(name, "dumpfonts")) {
1795 i->config_dumpfonts = atoi(value);
1796 } else if(!strcmp(name, "animate")) {
1797 i->config_animate = atoi(value);
1798 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1800 } else if(!strcmp(name, "jpegquality")) {
1801 int val = atoi(value);
1803 if(val>100) val=100;
1804 i->config_jpegquality = val;
1805 } else if(!strcmp(name, "splinequality")) {
1806 int v = atoi(value);
1807 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1809 i->config_splinemaxerror = v;
1810 } else if(!strcmp(name, "fontquality")) {
1811 int v = atoi(value);
1812 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1814 i->config_fontsplinemaxerror = v;
1815 } else if(!strcmp(name, "linkcolor")) {
1816 if(strlen(value)!=8) {
1817 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1820 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1821 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1822 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1823 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1824 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1825 printf("%02x%02x%02x%02x\n",
1826 i->config_linkcolor.r,
1827 i->config_linkcolor.g,
1828 i->config_linkcolor.b,
1829 i->config_linkcolor.a);
1837 // --------------------------------------------------------------------
1839 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1842 swf_GetCXForm(0, &cx, 1);
1845 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1846 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1847 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1848 c->ar!=0 || c->ag!=0 || c->ab!=0)
1849 msg("<warning> CXForm not SWF-compatible");
1851 cx.a0 = (S16)(c->aa*256);
1852 cx.r0 = (S16)(c->rr*256);
1853 cx.g0 = (S16)(c->gg*256);
1854 cx.b0 = (S16)(c->bb*256);
1863 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1867 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1871 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1873 gfxdevice_t*dev = i->dev;
1875 RGBA*mem = (RGBA*)img->data;
1877 int sizex = img->width;
1878 int sizey = img->height;
1879 int is_jpeg = i->jpeg;
1882 int newsizex=sizex, newsizey=sizey;
1885 if(is_jpeg && i->config_jpegsubpixels) {
1886 newsizex = (int)(targetwidth*i->config_jpegsubpixels + 0.5);
1887 newsizey = (int)(targetheight*i->config_jpegsubpixels + 0.5);
1888 } else if(!is_jpeg && i->config_ppmsubpixels) {
1889 newsizex = (int)(targetwidth*i->config_ppmsubpixels + 0.5);
1890 newsizey = (int)(targetheight*i->config_ppmsubpixels + 0.5);
1894 if(sizex<=0 || sizey<=0)
1901 /* TODO: cache images */
1903 if(newsizex<sizex || newsizey<sizey) {
1904 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1905 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1906 *newwidth = sizex = newsizex;
1907 *newheight = sizey = newsizey;
1910 *newwidth = newsizex = sizex;
1911 *newheight = newsizey = sizey;
1914 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1915 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1917 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1919 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1920 is_jpeg?"jpeg-":"", i->currentswfid+1,
1922 targetwidth, targetheight,
1923 /*newsizex, newsizey,*/
1924 num_colors>256?">":"", num_colors>256?256:num_colors);
1926 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1927 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1929 for(t=0;t<num_colors;t++) {
1930 printf("%02x%02x%02x%02x ",
1931 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1938 int cacheid = imageInCache(dev, mem, sizex, sizey);
1941 bitid = getNewID(dev);
1942 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1943 addImageToCache(dev, mem, sizex, sizey);
1953 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1955 gfxbbox_t bbox = gfxline_getbbox(line);
1957 r.xmin = (int)(bbox.xmin*20);
1958 r.ymin = (int)(bbox.ymin*20);
1959 r.xmax = (int)(bbox.xmax*20);
1960 r.ymax = (int)(bbox.ymax*20);
1964 int line_is_empty(gfxline_t*line)
1967 if(line->type != gfx_moveTo)
1974 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1976 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1978 if(line_is_empty(line))
1984 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1985 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1987 int newwidth=0,newheight=0;
1988 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1991 double fx = (double)img->width / (double)newwidth;
1992 double fy = (double)img->height / (double)newheight;
1997 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1998 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1999 m.tx = (int)(matrix->tx*20);
2000 m.ty = (int)(matrix->ty*20);
2003 int myshapeid = getNewID(dev);
2004 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2006 swf_ShapeNew(&shape);
2007 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2008 swf_SetU16(i->tag, myshapeid);
2009 SRECT r = gfxline_getSWFbbox(line);
2010 swf_SetRect(i->tag,&r);
2011 swf_SetShapeStyles(i->tag,shape);
2012 swf_ShapeCountBits(shape,NULL,NULL);
2013 swf_SetShapeBits(i->tag,shape);
2014 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2015 i->swflastx = i->swflasty = UNDEFINED_COORD;
2016 drawgfxline(dev, line);
2017 swf_ShapeSetEnd(i->tag);
2018 swf_ShapeFree(shape);
2020 msg("<trace> Placing bitmap ID %d", myshapeid);
2021 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2022 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2023 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2026 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2028 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2033 if(i->clippos >= 127)
2035 msg("<warning> Too many clip levels.");
2039 int myshapeid = getNewID(dev);
2040 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2042 memset(&col, 0, sizeof(RGBA));
2045 swf_ShapeNew(&shape);
2046 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2048 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2049 swf_ShapeAddSolidFillStyle(shape,&markcol);
2051 swf_SetU16(i->tag,myshapeid);
2052 SRECT r = gfxline_getSWFbbox(line);
2053 swf_SetRect(i->tag,&r);
2054 swf_SetShapeStyles(i->tag,shape);
2055 swf_ShapeCountBits(shape,NULL,NULL);
2056 swf_SetShapeBits(i->tag,shape);
2057 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2058 i->swflastx = i->swflasty = UNDEFINED_COORD;
2059 drawgfxline(dev, line);
2060 swf_ShapeSetEnd(i->tag);
2061 swf_ShapeFree(shape);
2063 /* TODO: remember the bbox, and check all shapes against it */
2065 msg("<trace> Placing clip ID %d", myshapeid);
2066 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2067 i->cliptags[i->clippos] = i->tag;
2068 i->clipshapes[i->clippos] = myshapeid;
2069 i->clipdepths[i->clippos] = getNewDepth(dev);
2073 static void swf_endclip(gfxdevice_t*dev)
2075 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2082 msg("<error> Invalid end of clipping region");
2086 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2087 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2089 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2091 static int gfxline_type(gfxline_t*line)
2097 int haszerosegments=0;
2100 if(line->type == gfx_moveTo) {
2103 } else if(line->type == gfx_lineTo) {
2107 } else if(line->type == gfx_splineTo) {
2109 if(tmpsplines>lines)
2117 if(lines==0 && splines==0) return 0;
2118 else if(lines==1 && splines==0) return 1;
2119 else if(lines==0 && splines==1) return 2;
2120 else if(splines==0) return 3;
2124 static int gfxline_has_dots(gfxline_t*line)
2132 if(line->type == gfx_moveTo) {
2133 /* test the length of the preceding line, and assume it is a dot if
2134 it's length is less than 1.0. But *only* if there's a noticable
2135 gap between the previous line and the next moveTo. (I've come
2136 across a PDF where thousands of "dots" were stringed together,
2138 int last_short_gap = short_gap;
2139 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2144 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2149 } else if(line->type == gfx_lineTo) {
2150 dist += fabs(line->x - x) + fabs(line->y - y);
2152 } else if(line->type == gfx_splineTo) {
2153 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2154 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2161 if(isline && dist < 1 && !short_gap) {
2167 static int gfxline_fix_short_edges(gfxline_t*line)
2171 if(line->type == gfx_lineTo) {
2172 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2175 } else if(line->type == gfx_splineTo) {
2176 if(fabs(line->sx - x) + fabs(line->sy - y) +
2177 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2188 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2190 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2191 if(x<i->min_x || x>i->max_x) return 0;
2192 if(y<i->min_y || y>i->max_y) return 0;
2196 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2198 gfxline_t*l = line = gfxline_clone(line);
2210 //#define NORMALIZE_POLYGON_POSITIONS
2212 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)
2214 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2215 if(line_is_empty(line))
2217 int type = gfxline_type(line);
2218 int has_dots = gfxline_has_dots(line);
2219 gfxbbox_t r = gfxline_getbbox(line);
2220 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2222 /* TODO: * split line into segments, and perform this check for all segments */
2224 if(i->config_disable_polygon_conversion || type>=5 ||
2226 (width <= i->config_caplinewidth
2227 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2228 || (cap_style == gfx_capRound && type<=2)))) {} else
2230 /* convert line to polygon */
2231 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2233 gfxline_fix_short_edges(line);
2234 /* we need to convert the line into a polygon */
2235 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2236 gfxline_t*gfxline = SVPtogfxline(svp);
2237 dev->fill(dev, gfxline, color);
2243 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2246 #ifdef NORMALIZE_POLYGON_POSITIONS
2248 double startx = 0, starty = 0;
2249 if(line && line->type == gfx_moveTo) {
2253 line = gfxline_move(line, -startx, -starty);
2254 i->shapeposx = (int)(startx*20);
2255 i->shapeposy = (int)(starty*20);
2258 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2259 swfoutput_setlinewidth(dev, width);
2262 drawgfxline(dev, line);
2264 #ifdef NORMALIZE_POLYGON_POSITIONS
2265 free(line); //account for _move
2269 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2271 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2272 if(line_is_empty(line))
2276 gfxbbox_t r = gfxline_getbbox(line);
2277 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2280 if(!i->config_ignoredraworder)
2283 #ifdef NORMALIZE_POLYGON_POSITIONS
2285 double startx = 0, starty = 0;
2286 if(line && line->type == gfx_moveTo) {
2290 line = gfxline_move(line, -startx, -starty);
2291 i->shapeposx = (int)(startx*20);
2292 i->shapeposy = (int)(starty*20);
2295 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2299 drawgfxline(dev, line);
2300 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2302 #ifdef NORMALIZE_POLYGON_POSITIONS
2303 free(line); //account for _move
2306 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2308 if(line_is_empty(line))
2310 msg("<error> Gradient filling not implemented yet");
2313 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2315 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2317 SRECT bounds = {0,0,0,0};
2319 swffont->version = 2;
2320 swffont->name = (U8*)strdup(id);
2321 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2322 swffont->layout->ascent = 0; /* ? */
2323 swffont->layout->descent = 0;
2324 swffont->layout->leading = 0;
2325 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2326 swffont->encoding = FONT_ENCODING_UNICODE;
2327 swffont->numchars = font->num_glyphs;
2328 swffont->maxascii = font->max_unicode;
2329 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2330 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2331 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2332 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2333 for(t=0;t<font->max_unicode;t++) {
2334 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2336 for(t=0;t<font->num_glyphs;t++) {
2340 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2341 if(font->glyphs[t].name) {
2342 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2344 swffont->glyphnames[t] = 0;
2346 advance = (int)(font->glyphs[t].advance);
2348 swf_Shape01DrawerInit(&draw, 0);
2349 line = font->glyphs[t].line;
2352 c.x = line->sx; c.y = line->sy;
2353 to.x = line->x; to.y = line->y;
2354 if(line->type == gfx_moveTo) {
2355 draw.moveTo(&draw, &to);
2356 } else if(line->type == gfx_lineTo) {
2357 draw.lineTo(&draw, &to);
2358 } else if(line->type == gfx_splineTo) {
2359 draw.splineTo(&draw, &c, &to);
2364 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2365 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2367 if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2368 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2369 advance = swffont->layout->bounds[t].xmax;
2373 swffont->glyph[t].advance = advance;
2375 swffont->glyph[t].advance = 32767;
2378 draw.dealloc(&draw);
2380 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2382 if(bounds.ymin < 0 && bounds.ymax > 0) {
2383 swffont->layout->ascent = -bounds.ymin;
2384 swffont->layout->descent = bounds.ymax;
2385 swffont->layout->leading = bounds.ymax - bounds.ymin;
2387 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2388 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2389 swffont->layout->leading = bounds.ymax - bounds.ymin;
2395 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2397 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2399 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2400 return; // the requested font is the current font
2402 fontlist_t*last=0,*l = i->fontlist;
2405 if(!strcmp((char*)l->swffont->name, font->id)) {
2406 return; // we already know this font
2410 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2411 l->swffont = gfxfont_to_swffont(font, font->id);
2418 swf_FontSetID(l->swffont, getNewID(i->dev));
2420 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2422 // print font information
2423 msg("<debug> Font %s",font->id);
2424 msg("<debug> | ID: %d", l->swffont->id);
2425 msg("<debug> | Version: %d", l->swffont->version);
2426 msg("<debug> | Name: %s", l->swffont->name);
2427 msg("<debug> | Numchars: %d", l->swffont->numchars);
2428 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2429 msg("<debug> | Style: %d", l->swffont->style);
2430 msg("<debug> | Encoding: %d", l->swffont->encoding);
2431 for(iii=0; iii<l->swffont->numchars;iii++) {
2432 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,
2433 l->swffont->layout->bounds[iii].xmin/20.0,
2434 l->swffont->layout->bounds[iii].ymin/20.0,
2435 l->swffont->layout->bounds[iii].xmax/20.0,
2436 l->swffont->layout->bounds[iii].ymax/20.0
2439 for(t=0;t<l->swffont->maxascii;t++) {
2440 if(l->swffont->ascii2glyph[t] == iii)
2441 msg("<debug> | - maps to %d",t);
2447 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2449 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2451 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2452 return; // the requested font is the current font
2454 fontlist_t*l = i->fontlist;
2456 if(!strcmp((char*)l->swffont->name, fontid)) {
2457 i->swffont = l->swffont;
2462 msg("<error> Unknown font id: %s", fontid);
2466 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2468 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2470 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2472 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2473 with multiple fonts */
2476 swf_switchfont(dev, font->id); // set the current font
2478 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2488 /* printf("%f %f\n", m.m31, m.m32);
2490 static int xpos = 40;
2491 static int ypos = 200;
2498 drawchar(dev, i->swffont, glyph, &m, color);