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_splinemaxerror;
82 int config_fontsplinemaxerror;
83 int config_filloverlap;
86 int config_disable_polygon_conversion;
87 RGBA config_linkcolor;
88 float config_minlinewidth;
89 double config_caplinewidth;
90 char* config_linktarget;
91 char*config_internallinkfunction;
92 char*config_externallinkfunction;
131 int pic_height[1024];
137 char fillstylechanged;
139 int jpeg; //next image type
144 chardata_t chardata[CHARDATAMAX];
152 double fontm11,fontm12,fontm21,fontm22;
163 } swfoutput_internal;
165 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
166 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
167 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);
168 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
169 static void swf_endclip(gfxdevice_t*dev);
170 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);
171 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
172 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
173 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
174 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
175 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
176 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
177 static void swf_startframe(gfxdevice_t*dev, int width, int height);
178 static void swf_endframe(gfxdevice_t*dev);
179 static gfxresult_t* swf_finish(gfxdevice_t*driver);
181 static swfoutput_internal* init_internal_struct()
183 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
184 memset(i, 0, sizeof(swfoutput_internal));
208 i->fillstylechanged = 0;
215 i->config_dumpfonts=0;
216 i->config_ppmsubpixels=0;
217 i->config_jpegsubpixels=0;
218 i->config_opennewwindow=1;
219 i->config_ignoredraworder=0;
220 i->config_drawonlyshapes=0;
221 i->config_jpegquality=85;
222 i->config_storeallcharacters=0;
223 i->config_enablezlib=0;
224 i->config_generate_fake_tags=0;
225 i->config_insertstoptag=0;
226 i->config_flashversion=6;
227 i->config_splinemaxerror=1;
228 i->config_fontsplinemaxerror=1;
229 i->config_filloverlap=0;
231 i->config_bboxvars=0;
232 i->config_minlinewidth=0.05;
233 i->config_caplinewidth=1;
234 i->config_linktarget=0;
235 i->config_internallinkfunction=0;
236 i->config_externallinkfunction=0;
238 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
239 i->config_linkcolor.a = 0x40;
244 static int id_error = 0;
246 static U16 getNewID(gfxdevice_t* dev)
248 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
249 if(i->currentswfid == 65535) {
251 msg("<error> ID Table overflow");
252 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
258 return ++i->currentswfid;
260 static U16 getNewDepth(gfxdevice_t* dev)
262 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
263 if(i->depth == 65535) {
265 msg("<error> Depth Table overflow");
266 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
275 static void startshape(gfxdevice_t* dev);
276 static void starttext(gfxdevice_t* dev);
277 static void endshape(gfxdevice_t* dev);
278 static void endtext(gfxdevice_t* dev);
280 typedef struct _plotxy
285 // write a move-to command into the swf
286 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
288 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
289 int rx = (int)(p0.x*20);
290 int ry = (int)(p0.y*20);
291 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
292 swf_ShapeSetMove (tag, i->shape, rx,ry);
293 i->fillstylechanged = 0;
300 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
302 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
306 return movetoxy(dev, tag, p);
308 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
310 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
316 swf_ExpandRect(&i->bboxrect, p);
318 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
322 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
324 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
325 int width = i->linewidth/4;
329 //swf_ShapeSetLine(tag, i->shape,-width,-width);
330 //swf_ShapeSetLine(tag, i->shape,width*2,0);
331 //swf_ShapeSetLine(tag, i->shape,0,width*2);
332 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
333 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
334 //swf_ShapeSetLine(tag, i->shape,width,width);
337 swf_ShapeSetLine(tag, i->shape,-width,0);
338 swf_ShapeSetLine(tag, i->shape,width,-width);
339 swf_ShapeSetLine(tag, i->shape,width,width);
340 swf_ShapeSetLine(tag, i->shape,-width,width);
341 swf_ShapeSetLine(tag, i->shape,-width,-width);
342 swf_ShapeSetLine(tag, i->shape,width,0);
344 addPointToBBox(dev, x-width ,y-width);
345 addPointToBBox(dev, x+width ,y+width);
348 // write a line-to command into the swf
349 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
351 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
352 int px = (int)(p0.x*20);
353 int py = (int)(p0.y*20);
354 int rx = (px-i->swflastx);
355 int ry = (py-i->swflasty);
357 swf_ShapeSetLine (tag, i->shape, rx,ry);
358 addPointToBBox(dev, i->swflastx,i->swflasty);
359 addPointToBBox(dev, px,py);
360 }/* else if(!i->fill) {
361 // treat lines of length 0 as plots, making them
362 // at least 1 twip wide so Flash will display them
363 plot(dev, i->swflastx, i->swflasty, tag);
370 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
375 linetoxy(dev,tag, p);
378 // write a spline-to command into the swf
379 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
381 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
382 int lastlastx = i->swflastx;
383 int lastlasty = i->swflasty;
385 int cx = ((int)(control.x*20)-i->swflastx);
386 int cy = ((int)(control.y*20)-i->swflasty);
389 int ex = ((int)(end.x*20)-i->swflastx);
390 int ey = ((int)(end.y*20)-i->swflasty);
394 if((cx || cy) && (ex || ey)) {
395 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
396 addPointToBBox(dev, lastlastx ,lastlasty );
397 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
398 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
399 } else if(cx || cy || ex || ey) {
400 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
401 addPointToBBox(dev, lastlastx ,lastlasty );
402 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
403 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
409 /* write a line, given two points and the transformation
411 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
413 moveto(dev, tag, p0);
414 lineto(dev, tag, p1);
417 void resetdrawer(gfxdevice_t*dev)
419 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
424 static void stopFill(gfxdevice_t*dev)
426 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
427 if(i->lastwasfill!=0)
429 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
430 i->fillstylechanged = 1;
434 static void startFill(gfxdevice_t*dev)
436 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
437 if(i->lastwasfill!=1)
439 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
440 i->fillstylechanged = 1;
445 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
457 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
459 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
462 memset(&r, 0, sizeof(r));
465 if(debug) printf("\n");
466 for(t=0;t<i->chardatapos;t++)
468 if(i->chardata[t].fontid != font->id) {
469 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
472 SRECT b = font->layout->bounds[i->chardata[t].charid];
473 b.xmin *= i->chardata[t].size;
474 b.ymin *= i->chardata[t].size;
475 b.xmax *= i->chardata[t].size;
476 b.ymax *= i->chardata[t].size;
478 /* divide by 1024, rounding xmax/ymax up */
479 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
481 b.xmin += i->chardata[t].x;
482 b.ymin += i->chardata[t].y;
483 b.xmax += i->chardata[t].x;
484 b.ymax += i->chardata[t].y;
486 /* until we solve the INTERNAL_SCALING problem (see below)
487 make sure the bounding box is big enough */
493 b = swf_TurnRect(b, m);
495 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
496 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
497 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
498 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
499 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
504 i->chardata[t].fontid,
506 i->chardata[t].charid
508 swf_ExpandRect2(&r, &b);
510 if(debug) printf("-----> (%f,%f,%f,%f)\n",
518 static void putcharacters(gfxdevice_t*dev, TAG*tag)
520 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
524 color.r = i->chardata[0].color.r^255;
533 int charadvance[128];
536 int glyphbits=1; //TODO: can this be zero?
539 if(tag->id != ST_DEFINETEXT &&
540 tag->id != ST_DEFINETEXT2) {
541 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
544 if(!i->chardatapos) {
545 msg("<warning> putcharacters called with zero characters");
548 for(pass = 0; pass < 2; pass++)
558 advancebits++; // add sign bit
559 swf_SetU8(tag, glyphbits);
560 swf_SetU8(tag, advancebits);
563 for(t=0;t<=i->chardatapos;t++)
565 if(lastfontid != i->chardata[t].fontid ||
566 lastx!=i->chardata[t].x ||
567 lasty!=i->chardata[t].y ||
568 !colorcompare(dev,&color, &i->chardata[t].color) ||
570 lastsize != i->chardata[t].size ||
573 if(charstorepos && pass==0)
576 for(s=0;s<charstorepos;s++)
578 while(charids[s]>=(1<<glyphbits))
580 while(charadvance[s]>=(1<<advancebits))
584 if(charstorepos && pass==1)
586 tag->writeBit = 0; // Q&D
587 swf_SetBits(tag, 0, 1); // GLYPH Record
588 swf_SetBits(tag, charstorepos, 7); // number of glyphs
590 for(s=0;s<charstorepos;s++)
592 swf_SetBits(tag, charids[s], glyphbits);
593 swf_SetBits(tag, charadvance[s], advancebits);
598 if(pass == 1 && t<i->chardatapos)
604 if(lastx != i->chardata[t].x ||
605 lasty != i->chardata[t].y)
607 newx = i->chardata[t].x;
608 newy = i->chardata[t].y;
614 if(!colorcompare(dev,&color, &i->chardata[t].color))
616 color = i->chardata[t].color;
619 font.id = i->chardata[t].fontid;
620 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
623 tag->writeBit = 0; // Q&D
624 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
627 lastfontid = i->chardata[t].fontid;
628 lastx = i->chardata[t].x;
629 lasty = i->chardata[t].y;
630 lastsize = i->chardata[t].size;
633 if(t==i->chardatapos)
637 int nextt = t==i->chardatapos-1?t:t+1;
638 int rel = i->chardata[nextt].x-i->chardata[t].x;
639 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
641 lastx=i->chardata[nextt].x;
645 lastx=i->chardata[t].x;
647 charids[charstorepos] = i->chardata[t].charid;
648 charadvance[charstorepos] = advance;
655 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
657 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
658 if(i->chardatapos == CHARDATAMAX)
660 msg("<warning> Character buffer too small. SWF will be slightly bigger");
664 i->chardata[i->chardatapos].fontid = fontid;
665 i->chardata[i->chardatapos].charid = charid;
666 i->chardata[i->chardatapos].x = x;
667 i->chardata[i->chardatapos].y = y;
668 i->chardata[i->chardatapos].color = color;
669 i->chardata[i->chardatapos].size = size;
673 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
674 So if we set this value to high, the char coordinates will overflow.
675 If we set it to low, however, the char positions will be inaccurate */
676 #define FONT_INTERNAL_SIZE 4
678 /* process a character. */
679 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix_t*m, gfxcolor_t*col)
681 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
683 msg("<warning> Font is NULL");
687 if(charid<0 || charid>=swffont->numchars) {
688 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
691 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
692 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
693 charid, FIXNULL((char*)swffont->name), swffont->numchars);
704 float det = ((m->m11*m->m22)-(m->m21*m->m12));
705 if(fabs(det) < 0.0005) {
706 /* x direction equals y direction- the text is invisible */
709 det = 20*FONT_INTERNAL_SIZE / det;
712 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
713 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
715 RGBA rgba = *(RGBA*)col;
716 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
717 swf_FontUseGlyph(swffont, charid);
721 static void endtext(gfxdevice_t*dev)
723 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
727 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
728 swf_SetU16(i->tag, i->textid);
731 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
733 swf_SetRect(i->tag,&r);
735 swf_SetMatrix(i->tag,&i->fontmatrix);
737 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
739 putcharacters(dev, i->tag);
742 if(i->swf->fileVersion >= 8) {
743 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
744 swf_SetU16(i->tag, i->textid);
745 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
746 //swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
747 //swf_SetU8(i->tag, /*grid*/(0<<3)|/*flashtype*/0x40);
748 swf_SetU8(i->tag, /*grid*/(1<<3)|/*no flashtype*/0x00);
749 swf_SetU32(i->tag, 0);//thickness
750 swf_SetU32(i->tag, 0);//sharpness
751 swf_SetU8(i->tag, 0);//reserved
753 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
755 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
759 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
760 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
761 double m12,double m22)
767 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
768 if(i->fontm11 == m11 &&
781 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
782 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
788 static void endpage(gfxdevice_t*dev)
790 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
800 void swf_startframe(gfxdevice_t*dev, int width, int height)
802 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
803 if(!i->firstpage && !i->pagefinished)
805 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
807 swf_GetMatrix(0, &i->page_matrix);
808 i->page_matrix.tx = 0;
809 i->page_matrix.ty = 0;
815 /* set clipping/background rectangle */
816 /* TODO: this should all be done in SWFOutputDev */
817 //setBackground(dev, x1, y1, x2, y2);
819 /* increase SWF's bounding box */
825 swf_ExpandRect2(&i->swf->movieSize, &r);
827 i->lastframeno = i->frameno;
832 void swf_endframe(gfxdevice_t*dev)
834 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
839 if(i->config_insertstoptag) {
841 atag = action_Stop(atag);
842 atag = action_End(atag);
843 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
844 swf_ActionSet(i->tag,atag);
846 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
849 for(i->depth;i->depth>i->startdepth;i->depth--) {
850 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
851 swf_SetU16(i->tag,i->depth);
853 i->depth = i->startdepth;
856 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
858 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
860 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
864 int shapeid = getNewID(dev);
869 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
871 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
872 swf_SetU16(i->tag,shapeid);
873 swf_SetRect(i->tag,&r);
874 swf_SetShapeHeader(i->tag,s);
875 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
876 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
877 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
878 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
879 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
880 swf_ShapeSetEnd(i->tag);
882 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
883 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
884 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
885 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
888 /* initialize the swf writer */
889 void gfxdevice_swf_init(gfxdevice_t* dev)
891 memset(dev, 0, sizeof(gfxdevice_t));
895 dev->internal = init_internal_struct();
897 dev->startpage = swf_startframe;
898 dev->endpage = swf_endframe;
899 dev->finish = swf_finish;
900 dev->fillbitmap = swf_fillbitmap;
901 dev->setparameter = swf_setparameter;
902 dev->stroke = swf_stroke;
903 dev->startclip = swf_startclip;
904 dev->endclip = swf_endclip;
905 dev->fill = swf_fill;
906 dev->fillbitmap = swf_fillbitmap;
907 dev->fillgradient = swf_fillgradient;
908 dev->addfont = swf_addfont;
909 dev->drawchar = swf_drawchar;
910 dev->drawlink = swf_drawlink;
912 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
918 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
922 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
923 i->swf->fileVersion = i->config_flashversion;
924 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
925 i->swf->movieSize.xmin = 0;
926 i->swf->movieSize.ymin = 0;
927 i->swf->movieSize.xmax = 0;
928 i->swf->movieSize.ymax = 0;
930 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
931 i->tag = i->swf->firstTag;
932 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
933 swf_SetRGB(i->tag,&rgb);
935 i->startdepth = i->depth = 0;
937 if(i->config_protect)
938 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
941 static void startshape(gfxdevice_t*dev)
943 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
952 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
954 swf_ShapeNew(&i->shape);
955 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
956 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
958 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
959 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
962 i->shapeid = getNewID(dev);
964 msg("<debug> Using shape id %d", i->shapeid);
966 swf_SetU16(i->tag,i->shapeid); // ID
968 i->bboxrectpos = i->tag->len;
972 r.xmax = 20*i->max_x;
973 r.ymax = 20*i->max_y;
974 swf_SetRect(i->tag,&r);
976 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
978 swf_SetShapeStyles(i->tag,i->shape);
979 swf_ShapeCountBits(i->shape,NULL,NULL);
980 swf_SetShapeBits(i->tag,i->shape);
982 /* TODO: do we really need this? */
983 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
984 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
985 i->swflastx=i->swflasty=UNDEFINED_COORD;
990 static void starttext(gfxdevice_t*dev)
992 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
996 i->textid = getNewID(dev);
998 i->swflastx=i->swflasty=0;
1002 /* TODO: move to ../lib/rfxswf */
1003 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1005 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1006 /* determine length of old rect */
1010 swf_GetRect(tag, &old);
1011 swf_ResetReadBits(tag);
1012 int pos_end = tag->pos;
1014 int len = tag->len - pos_end;
1015 U8*data = (U8*)malloc(len);
1016 memcpy(data, &tag->data[pos_end], len);
1019 swf_SetRect(tag, newrect);
1020 swf_SetBlock(tag, data, len);
1022 tag->pos = tag->readBit = 0;
1025 void cancelshape(gfxdevice_t*dev)
1027 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1028 /* delete old shape tag */
1030 i->tag = i->tag->prev;
1031 swf_DeleteTag(todel);
1032 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1034 i->bboxrectpos = -1;
1036 // i->currentswfid--; // this was an *exceptionally* bad idea
1039 void fixAreas(gfxdevice_t*dev)
1041 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1042 if(!i->shapeisempty && i->fill &&
1043 (i->bboxrect.xmin == i->bboxrect.xmax ||
1044 i->bboxrect.ymin == i->bboxrect.ymax) &&
1045 i->config_minlinewidth >= 0.001
1047 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1048 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1049 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1052 SRECT r = i->bboxrect;
1054 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1055 /* this thing comes down to a single dot- nothing to fix here */
1061 RGBA save_col = i->strokergb;
1062 int save_width = i->linewidth;
1064 i->strokergb = i->fillrgb;
1065 i->linewidth = (int)(i->config_minlinewidth*20);
1066 if(i->linewidth==0) i->linewidth = 1;
1071 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1072 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1074 i->strokergb = save_col;
1075 i->linewidth = save_width;
1080 static void endshape_noput(gfxdevice_t*dev)
1082 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1085 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1088 swf_ShapeFree(i->shape);
1096 static void endshape(gfxdevice_t*dev)
1098 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1104 if(i->shapeisempty ||
1106 (i->bboxrect.xmin == i->bboxrect.xmax &&
1107 i->bboxrect.ymin == i->bboxrect.ymax))
1109 // delete the shape again, we didn't do anything
1110 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1111 i->bboxrect.xmin /20.0,
1112 i->bboxrect.ymin /20.0,
1113 i->bboxrect.xmax /20.0,
1114 i->bboxrect.ymax /20.0
1120 swf_ShapeSetEnd(i->tag);
1122 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1124 msg("<trace> Placing shape id %d", i->shapeid);
1126 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1127 MATRIX m = i->page_matrix;
1128 m.tx += i->shapeposx;
1129 m.ty += i->shapeposy;
1130 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1132 swf_ShapeFree(i->shape);
1135 i->bboxrectpos = -1;
1142 void wipeSWF(SWF*swf)
1144 TAG*tag = swf->firstTag;
1146 TAG*next = tag->next;
1147 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1148 tag->id != ST_END &&
1149 tag->id != ST_DOACTION &&
1150 tag->id != ST_SHOWFRAME) {
1158 void swfoutput_finalize(gfxdevice_t*dev)
1160 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1162 if(i->tag && i->tag->id == ST_END)
1163 return; //already done
1165 if(i->config_bboxvars) {
1166 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1168 a = action_PushString(a, "xmin");
1169 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1170 a = action_SetVariable(a);
1171 a = action_PushString(a, "ymin");
1172 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1173 a = action_SetVariable(a);
1174 a = action_PushString(a, "xmax");
1175 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1176 a = action_SetVariable(a);
1177 a = action_PushString(a, "ymax");
1178 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1179 a = action_SetVariable(a);
1180 a = action_PushString(a, "width");
1181 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1182 a = action_SetVariable(a);
1183 a = action_PushString(a, "height");
1184 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1185 a = action_SetVariable(a);
1187 swf_ActionSet(tag, a);
1191 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1195 free(i->mark);i->mark = 0;
1199 fontlist_t *tmp,*iterator = i->fontlist;
1201 TAG*mtag = i->swf->firstTag;
1202 if(iterator->swffont) {
1203 if(!i->config_storeallcharacters) {
1204 msg("<debug> Reducing font %s", iterator->swffont->name);
1205 swf_FontReduce(iterator->swffont);
1207 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1208 if(i->config_storeallcharacters || used) {
1209 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1210 swf_FontSetDefine2(mtag, iterator->swffont);
1214 iterator = iterator->next;
1216 i->tag = swf_InsertTag(i->tag,ST_END);
1217 TAG* tag = i->tag->prev;
1219 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1220 and the ST_END- they confuse the flash player */
1221 while(tag->id == ST_REMOVEOBJECT2) {
1222 TAG* prev = tag->prev;
1230 if(i->config_enablezlib || i->config_flashversion>=6) {
1231 i->swf->compressed = 1;
1234 swf_Optimize(i->swf);
1237 int swfresult_save(gfxresult_t*gfx, char*filename)
1239 SWF*swf = (SWF*)gfx->internal;
1242 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1247 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1251 if(swf->compressed) {
1252 if FAILED(swf_WriteSWC(fi,swf))
1253 msg("<error> WriteSWC() failed.\n");
1255 if FAILED(swf_WriteSWF(fi,swf))
1256 msg("<error> WriteSWF() failed.\n");
1263 void* swfresult_get(gfxresult_t*gfx, char*name)
1265 SWF*swf = (SWF*)gfx->internal;
1266 if(!strcmp(name, "swf")) {
1267 return (void*)swf_CopySWF(swf);
1268 } else if(!strcmp(name, "xmin")) {
1269 return (void*)(swf->movieSize.xmin/20);
1270 } else if(!strcmp(name, "ymin")) {
1271 return (void*)(swf->movieSize.ymin/20);
1272 } else if(!strcmp(name, "xmax")) {
1273 return (void*)(swf->movieSize.xmax/20);
1274 } else if(!strcmp(name, "ymax")) {
1275 return (void*)(swf->movieSize.ymax/20);
1276 } else if(!strcmp(name, "width")) {
1277 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1278 } else if(!strcmp(name, "height")) {
1279 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1283 void swfresult_destroy(gfxresult_t*gfx)
1286 swf_FreeTags((SWF*)gfx->internal);
1287 free(gfx->internal);
1290 memset(gfx, 0, sizeof(gfxresult_t));
1294 static void swfoutput_destroy(gfxdevice_t* dev);
1296 gfxresult_t* swf_finish(gfxdevice_t* dev)
1298 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1301 if(i->config_linktarget) {
1302 free(i->config_linktarget);
1303 i->config_linktarget = 0;
1306 swfoutput_finalize(dev);
1307 SWF* swf = i->swf;i->swf = 0;
1308 swfoutput_destroy(dev);
1310 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1311 result->internal = swf;
1312 result->save = swfresult_save;
1314 result->get = swfresult_get;
1315 result->destroy = swfresult_destroy;
1319 /* Perform cleaning up */
1320 static void swfoutput_destroy(gfxdevice_t* dev)
1322 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1324 /* not initialized yet- nothing to destroy */
1328 fontlist_t *tmp,*iterator = i->fontlist;
1330 if(iterator->swffont) {
1331 swf_FontFree(iterator->swffont);iterator->swffont=0;
1334 iterator = iterator->next;
1337 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1340 memset(dev, 0, sizeof(gfxdevice_t));
1343 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1345 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1346 if(i->fillrgb.r == r &&
1347 i->fillrgb.g == g &&
1348 i->fillrgb.b == b &&
1349 i->fillrgb.a == a) return;
1359 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1361 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1362 if(i->strokergb.r == r &&
1363 i->strokergb.g == g &&
1364 i->strokergb.b == b &&
1365 i->strokergb.a == a) return;
1375 //#define ROUND_UP 19
1376 //#define ROUND_UP 10
1378 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1380 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1381 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1385 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1389 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1390 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1391 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1392 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1394 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1396 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1397 dev->drawlink(dev, points, url);
1400 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1402 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1404 if(!strncmp("http://pdf2swf:", url, 15)) {
1405 char*tmp = strdup(url);
1406 int l = strlen(tmp);
1409 swfoutput_namedlink(dev, tmp+15, points);
1412 } else if(!strncmp("page", url, 4)) {
1415 if(url[t]<'0' || url[t]>'9')
1418 int page = atoi(&url[4]);
1419 if(page<0) page = 0;
1420 swfoutput_linktopage(dev, page, points);
1423 swfoutput_linktourl(dev, url, points);
1426 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1428 ActionTAG* actions = 0;
1429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1435 if(i->config_externallinkfunction) {
1436 actions = action_PushString(actions, url); //parameter
1437 actions = action_PushInt(actions, 1); //number of parameters (1)
1438 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1439 actions = action_CallFunction(actions);
1440 } else if(!i->config_linktarget) {
1441 if(!i->config_opennewwindow)
1442 actions = action_GetUrl(actions, url, "_parent");
1444 actions = action_GetUrl(actions, url, "_this");
1446 actions = action_GetUrl(actions, url, i->config_linktarget);
1448 actions = action_End(actions);
1450 drawlink(dev, actions, 0, points,0);
1452 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1454 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1455 ActionTAG* actions = 0;
1462 if(!i->config_internallinkfunction) {
1463 actions = action_GotoFrame(actions, page-1);
1464 actions = action_End(actions);
1466 actions = action_PushInt(actions, page); //parameter
1467 actions = action_PushInt(actions, 1); //number of parameters (1)
1468 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1469 actions = action_CallFunction(actions);
1470 actions = action_End(actions);
1473 drawlink(dev, actions, 0, points,0);
1476 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1477 of the viewer objects, like subtitles, index elements etc.
1479 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1481 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1482 ActionTAG *actions1,*actions2;
1483 char*tmp = strdup(name);
1491 if(!strncmp(tmp, "call:", 5))
1493 char*x = strchr(&tmp[5], ':');
1495 actions1 = action_PushInt(0, 0); //number of parameters (0)
1496 actions1 = action_PushString(actions1, &tmp[5]); //function name
1497 actions1 = action_CallFunction(actions1);
1498 actions1 = action_End(actions1);
1501 actions1 = action_PushString(0, x+1); //parameter
1502 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1503 actions1 = action_PushString(actions1, &tmp[5]); //function name
1504 actions1 = action_CallFunction(actions1);
1505 actions1 = action_End(actions1);
1507 actions2 = action_End(0);
1512 actions1 = action_PushString(0, "/:subtitle");
1513 actions1 = action_PushString(actions1, name);
1514 actions1 = action_SetVariable(actions1);
1515 actions1 = action_End(actions1);
1517 actions2 = action_PushString(0, "/:subtitle");
1518 actions2 = action_PushString(actions2, "");
1519 actions2 = action_SetVariable(actions2);
1520 actions2 = action_End(actions2);
1523 drawlink(dev, actions1, actions2, points,mouseover);
1525 swf_ActionFree(actions1);
1526 swf_ActionFree(actions2);
1530 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1532 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1533 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1535 int lines= 0, splines=0;
1539 /* check whether the next segment is zero */
1540 if(line->type == gfx_moveTo) {
1541 moveto(dev, i->tag, line->x, line->y);
1542 px = lastx = line->x;
1543 py = lasty = line->y;
1545 } if(line->type == gfx_lineTo) {
1546 lineto(dev, i->tag, line->x, line->y);
1551 } else if(line->type == gfx_splineTo) {
1553 s.x = line->sx;p.x = line->x;
1554 s.y = line->sy;p.y = line->y;
1555 splineto(dev, i->tag, s, p);
1563 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1567 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1569 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1574 plotxy_t p1,p2,p3,p4;
1580 int buttonid = getNewID(dev);
1581 gfxbbox_t bbox = gfxline_getbbox(points);
1584 myshapeid = getNewID(dev);
1585 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1586 swf_ShapeNew(&i->shape);
1587 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1588 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1589 swf_SetU16(i->tag, myshapeid);
1590 r.xmin = (int)(bbox.xmin*20);
1591 r.ymin = (int)(bbox.ymin*20);
1592 r.xmax = (int)(bbox.xmax*20);
1593 r.ymax = (int)(bbox.ymax*20);
1594 swf_SetRect(i->tag,&r);
1595 swf_SetShapeStyles(i->tag,i->shape);
1596 swf_ShapeCountBits(i->shape,NULL,NULL);
1597 swf_SetShapeBits(i->tag,i->shape);
1598 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1599 i->swflastx = i->swflasty = 0;
1600 drawgfxline(dev, points);
1601 swf_ShapeSetEnd(i->tag);
1604 myshapeid2 = getNewID(dev);
1605 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1606 swf_ShapeNew(&i->shape);
1608 rgb = i->config_linkcolor;
1610 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1611 swf_SetU16(i->tag, myshapeid2);
1612 r.xmin = (int)(bbox.xmin*20);
1613 r.ymin = (int)(bbox.ymin*20);
1614 r.xmax = (int)(bbox.xmax*20);
1615 r.ymax = (int)(bbox.ymax*20);
1616 swf_SetRect(i->tag,&r);
1617 swf_SetShapeStyles(i->tag,i->shape);
1618 swf_ShapeCountBits(i->shape,NULL,NULL);
1619 swf_SetShapeBits(i->tag,i->shape);
1620 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1621 i->swflastx = i->swflasty = 0;
1622 drawgfxline(dev, points);
1623 swf_ShapeSetEnd(i->tag);
1627 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1628 swf_SetU16(i->tag,buttonid); //id
1629 swf_ButtonSetFlags(i->tag, 0); //menu=no
1630 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1631 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1632 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1633 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1634 swf_SetU8(i->tag,0);
1635 swf_ActionSet(i->tag,actions1);
1636 swf_SetU8(i->tag,0);
1640 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1641 swf_SetU16(i->tag,buttonid); //id
1642 swf_ButtonSetFlags(i->tag, 0); //menu=no
1643 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1644 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1645 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1646 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1647 swf_SetU8(i->tag,0); // end of button records
1648 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1649 swf_ActionSet(i->tag,actions1);
1651 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1652 swf_ActionSet(i->tag,actions2);
1653 swf_SetU8(i->tag,0);
1654 swf_ButtonPostProcess(i->tag, 2);
1656 swf_SetU8(i->tag,0);
1657 swf_ButtonPostProcess(i->tag, 1);
1661 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1663 if(posx!=0 || posy!=0) {
1665 p.x = (int)(posx*20);
1666 p.y = (int)(posy*20);
1667 p = swf_TurnPoint(p, &i->page_matrix);
1672 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1674 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1681 for(t=0;t<picpos;t++)
1683 if(pic_xids[t] == xid &&
1684 pic_yids[t] == yid) {
1685 width = pic_width[t];
1686 height = pic_height[t];
1690 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1691 pic_xids[picpos] = xid;
1692 pic_yids[picpos] = yid;
1693 pic_width[picpos] = width;
1694 pic_height[picpos] = height;
1697 pic[width*y+x] = buf[0];
1701 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1702 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1706 xid += x*r+x*b*3+x*g*7+x*a*11;
1707 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1709 for(t=0;t<picpos;t++)
1711 if(pic_xids[t] == xid &&
1712 pic_yids[t] == yid) {
1721 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1723 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1725 msg("<trace> swfdevice: %s=%s", name, value);
1726 if(!strcmp(name, "jpegsubpixels")) {
1727 i->config_jpegsubpixels = atof(value);
1728 } else if(!strcmp(name, "ppmsubpixels")) {
1729 i->config_ppmsubpixels = atof(value);
1730 } else if(!strcmp(name, "subpixels")) {
1731 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1732 } else if(!strcmp(name, "drawonlyshapes")) {
1733 i->config_drawonlyshapes = atoi(value);
1734 } else if(!strcmp(name, "ignoredraworder")) {
1735 i->config_ignoredraworder = atoi(value);
1736 } else if(!strcmp(name, "mark")) {
1737 if(!value || !value[0]) {
1738 if(i->mark) free(i->mark);
1742 i->mark = strdup("...");
1743 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1745 } else if(!strcmp(name, "filloverlap")) {
1746 i->config_filloverlap = atoi(value);
1747 } else if(!strcmp(name, "linksopennewwindow")) {
1748 i->config_opennewwindow = atoi(value);
1749 } else if(!strcmp(name, "opennewwindow")) {
1750 i->config_opennewwindow = atoi(value);
1751 } else if(!strcmp(name, "storeallcharacters")) {
1752 i->config_storeallcharacters = atoi(value);
1753 } else if(!strcmp(name, "enablezlib")) {
1754 i->config_enablezlib = atoi(value);
1755 } else if(!strcmp(name, "bboxvars")) {
1756 i->config_bboxvars = atoi(value);
1757 } else if(!strcmp(name, "internallinkfunction")) {
1758 i->config_internallinkfunction = strdup(value);
1759 } else if(!strcmp(name, "externallinkfunction")) {
1760 i->config_externallinkfunction = strdup(value);
1761 } else if(!strcmp(name, "disable_polygon_conversion")) {
1762 i->config_disable_polygon_conversion = atoi(value);
1763 } else if(!strcmp(name, "insertstop")) {
1764 i->config_insertstoptag = atoi(value);
1765 } else if(!strcmp(name, "protect")) {
1766 i->config_protect = atoi(value);
1767 if(i->config_protect && i->tag) {
1768 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1770 } else if(!strcmp(name, "faketags")) {
1771 i->config_generate_fake_tags = atoi(value);
1772 } else if(!strcmp(name, "flashversion")) {
1773 i->config_flashversion = atoi(value);
1775 i->swf->fileVersion = i->config_flashversion;
1777 } else if(!strcmp(name, "minlinewidth")) {
1778 i->config_minlinewidth = atof(value);
1779 } else if(!strcmp(name, "caplinewidth")) {
1780 i->config_caplinewidth = atof(value);
1781 } else if(!strcmp(name, "linktarget")) {
1782 i->config_linktarget = strdup(value);
1783 } else if(!strcmp(name, "dumpfonts")) {
1784 i->config_dumpfonts = atoi(value);
1785 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1787 } else if(!strcmp(name, "jpegquality")) {
1788 int val = atoi(value);
1790 if(val>100) val=100;
1791 i->config_jpegquality = val;
1792 } else if(!strcmp(name, "splinequality")) {
1793 int v = atoi(value);
1794 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1796 i->config_splinemaxerror = v;
1797 } else if(!strcmp(name, "fontquality")) {
1798 int v = atoi(value);
1799 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1801 i->config_fontsplinemaxerror = v;
1802 } else if(!strcmp(name, "linkcolor")) {
1803 if(strlen(value)!=8) {
1804 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1807 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1808 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1809 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1810 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1811 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1812 printf("%02x%02x%02x%02x\n",
1813 i->config_linkcolor.r,
1814 i->config_linkcolor.g,
1815 i->config_linkcolor.b,
1816 i->config_linkcolor.a);
1824 // --------------------------------------------------------------------
1826 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1829 swf_GetCXForm(0, &cx, 1);
1832 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1833 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1834 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1835 c->ar!=0 || c->ag!=0 || c->ab!=0)
1836 msg("<warning> CXForm not SWF-compatible");
1838 cx.a0 = (S16)(c->aa*256);
1839 cx.r0 = (S16)(c->rr*256);
1840 cx.g0 = (S16)(c->gg*256);
1841 cx.b0 = (S16)(c->bb*256);
1850 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1854 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1858 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1860 gfxdevice_t*dev = i->dev;
1862 RGBA*mem = (RGBA*)img->data;
1864 int sizex = img->width;
1865 int sizey = img->height;
1866 int is_jpeg = i->jpeg;
1869 int newsizex=sizex, newsizey=sizey;
1872 if(is_jpeg && i->config_jpegsubpixels) {
1873 newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1874 newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1875 } else if(!is_jpeg && i->config_ppmsubpixels) {
1876 newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1877 newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1881 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1884 /* TODO: cache images */
1886 if(newsizex<sizex || newsizey<sizey) {
1887 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1888 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1889 *newwidth = sizex = newsizex;
1890 *newheight = sizey = newsizey;
1893 *newwidth = newsizex = sizex;
1894 *newheight = newsizey = sizey;
1897 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1898 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1900 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1902 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1903 is_jpeg?"jpeg-":"", i->currentswfid+1,
1905 targetwidth, targetheight,
1906 /*newsizex, newsizey,*/
1907 num_colors>256?">":"", num_colors>256?256:num_colors);
1909 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1910 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1912 for(t=0;t<num_colors;t++) {
1913 printf("%02x%02x%02x%02x ",
1914 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1921 int cacheid = imageInCache(dev, mem, sizex, sizey);
1924 bitid = getNewID(dev);
1925 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1926 addImageToCache(dev, mem, sizex, sizey);
1936 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1938 gfxbbox_t bbox = gfxline_getbbox(line);
1940 r.xmin = (int)(bbox.xmin*20);
1941 r.ymin = (int)(bbox.ymin*20);
1942 r.xmax = (int)(bbox.xmax*20);
1943 r.ymax = (int)(bbox.ymax*20);
1947 int line_is_empty(gfxline_t*line)
1950 if(line->type != gfx_moveTo)
1957 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1959 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1961 if(line_is_empty(line))
1967 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1968 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1970 int newwidth=0,newheight=0;
1971 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1974 double fx = (double)img->width / (double)newwidth;
1975 double fy = (double)img->height / (double)newheight;
1980 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1981 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1982 m.tx = (int)(matrix->tx*20);
1983 m.ty = (int)(matrix->ty*20);
1986 int myshapeid = getNewID(dev);
1987 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1989 swf_ShapeNew(&shape);
1990 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1991 swf_SetU16(i->tag, myshapeid);
1992 SRECT r = gfxline_getSWFbbox(line);
1993 swf_SetRect(i->tag,&r);
1994 swf_SetShapeStyles(i->tag,shape);
1995 swf_ShapeCountBits(shape,NULL,NULL);
1996 swf_SetShapeBits(i->tag,shape);
1997 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1998 i->swflastx = i->swflasty = UNDEFINED_COORD;
1999 drawgfxline(dev, line);
2000 swf_ShapeSetEnd(i->tag);
2001 swf_ShapeFree(shape);
2003 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2004 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2005 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2008 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2010 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2015 if(i->clippos >= 127)
2017 msg("<warning> Too many clip levels.");
2021 int myshapeid = getNewID(dev);
2022 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2024 memset(&col, 0, sizeof(RGBA));
2027 swf_ShapeNew(&shape);
2028 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2030 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2031 swf_ShapeAddSolidFillStyle(shape,&markcol);
2033 swf_SetU16(i->tag,myshapeid);
2034 SRECT r = gfxline_getSWFbbox(line);
2035 swf_SetRect(i->tag,&r);
2036 swf_SetShapeStyles(i->tag,shape);
2037 swf_ShapeCountBits(shape,NULL,NULL);
2038 swf_SetShapeBits(i->tag,shape);
2039 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2040 i->swflastx = i->swflasty = UNDEFINED_COORD;
2041 drawgfxline(dev, line);
2042 swf_ShapeSetEnd(i->tag);
2043 swf_ShapeFree(shape);
2045 /* TODO: remember the bbox, and check all shapes against it */
2047 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2048 i->cliptags[i->clippos] = i->tag;
2049 i->clipshapes[i->clippos] = myshapeid;
2050 i->clipdepths[i->clippos] = getNewDepth(dev);
2054 static void swf_endclip(gfxdevice_t*dev)
2056 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2063 msg("<error> Invalid end of clipping region");
2067 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2068 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2070 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2072 static int gfxline_type(gfxline_t*line)
2078 int haszerosegments=0;
2081 if(line->type == gfx_moveTo) {
2084 } else if(line->type == gfx_lineTo) {
2088 } else if(line->type == gfx_splineTo) {
2090 if(tmpsplines>lines)
2098 if(lines==0 && splines==0) return 0;
2099 else if(lines==1 && splines==0) return 1;
2100 else if(lines==0 && splines==1) return 2;
2101 else if(splines==0) return 3;
2105 static int gfxline_has_dots(gfxline_t*line)
2113 if(line->type == gfx_moveTo) {
2114 /* test the length of the preceding line, and assume it is a dot if
2115 it's length is less than 1.0. But *only* if there's a noticable
2116 gap between the previous line and the next moveTo. (I've come
2117 across a PDF where thousands of "dots" were stringed together,
2119 int last_short_gap = short_gap;
2120 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2125 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2130 } else if(line->type == gfx_lineTo) {
2131 dist += fabs(line->x - x) + fabs(line->y - y);
2133 } else if(line->type == gfx_splineTo) {
2134 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2135 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2142 if(isline && dist < 1 && !short_gap) {
2148 static int gfxline_fix_short_edges(gfxline_t*line)
2152 if(line->type == gfx_lineTo) {
2153 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2156 } else if(line->type == gfx_splineTo) {
2157 if(fabs(line->sx - x) + fabs(line->sy - y) +
2158 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2169 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2171 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2172 if(x<i->min_x || x>i->max_x) return 0;
2173 if(y<i->min_y || y>i->max_y) return 0;
2177 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2179 gfxline_t*l = line = gfxline_clone(line);
2191 //#define NORMALIZE_POLYGON_POSITIONS
2193 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)
2195 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2196 if(line_is_empty(line))
2198 int type = gfxline_type(line);
2199 int has_dots = gfxline_has_dots(line);
2200 gfxbbox_t r = gfxline_getbbox(line);
2201 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2203 /* TODO: * split line into segments, and perform this check for all segments */
2205 if(i->config_disable_polygon_conversion || type>=5 ||
2207 (width <= i->config_caplinewidth
2208 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2209 || (cap_style == gfx_capRound && type<=2)))) {} else
2211 /* convert line to polygon */
2212 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2214 gfxline_fix_short_edges(line);
2215 /* we need to convert the line into a polygon */
2216 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2217 gfxline_t*gfxline = SVPtogfxline(svp);
2218 dev->fill(dev, gfxline, color);
2224 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2227 #ifdef NORMALIZE_POLYGON_POSITIONS
2229 double startx = 0, starty = 0;
2230 if(line && line->type == gfx_moveTo) {
2234 line = gfxline_move(line, -startx, -starty);
2235 i->shapeposx = (int)(startx*20);
2236 i->shapeposy = (int)(starty*20);
2239 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2240 swfoutput_setlinewidth(dev, width);
2243 drawgfxline(dev, line);
2245 #ifdef NORMALIZE_POLYGON_POSITIONS
2246 free(line); //account for _move
2250 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2252 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2253 if(line_is_empty(line))
2257 gfxbbox_t r = gfxline_getbbox(line);
2258 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2261 if(!i->config_ignoredraworder)
2264 #ifdef NORMALIZE_POLYGON_POSITIONS
2266 double startx = 0, starty = 0;
2267 if(line && line->type == gfx_moveTo) {
2271 line = gfxline_move(line, -startx, -starty);
2272 i->shapeposx = (int)(startx*20);
2273 i->shapeposy = (int)(starty*20);
2276 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2280 drawgfxline(dev, line);
2281 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2283 #ifdef NORMALIZE_POLYGON_POSITIONS
2284 free(line); //account for _move
2287 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2289 if(line_is_empty(line))
2291 msg("<error> Gradient filling not implemented yet");
2294 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2296 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2298 SRECT bounds = {0,0,0,0};
2300 swffont->version = 2;
2301 swffont->name = (U8*)strdup(id);
2302 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2303 swffont->layout->ascent = 0; /* ? */
2304 swffont->layout->descent = 0;
2305 swffont->layout->leading = 0;
2306 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2307 swffont->encoding = FONT_ENCODING_UNICODE;
2308 swffont->numchars = font->num_glyphs;
2309 swffont->maxascii = font->max_unicode;
2310 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2311 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2312 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2313 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2314 for(t=0;t<font->max_unicode;t++) {
2315 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2317 for(t=0;t<font->num_glyphs;t++) {
2321 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2322 if(font->glyphs[t].name) {
2323 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2325 swffont->glyphnames[t] = 0;
2327 advance = (int)(font->glyphs[t].advance);
2329 swf_Shape01DrawerInit(&draw, 0);
2330 line = font->glyphs[t].line;
2333 c.x = line->sx; c.y = line->sy;
2334 to.x = line->x; to.y = line->y;
2335 if(line->type == gfx_moveTo) {
2336 draw.moveTo(&draw, &to);
2337 } else if(line->type == gfx_lineTo) {
2338 draw.lineTo(&draw, &to);
2339 } else if(line->type == gfx_splineTo) {
2340 draw.splineTo(&draw, &c, &to);
2345 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2346 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2348 if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2349 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2350 advance = swffont->layout->bounds[t].xmax;
2354 swffont->glyph[t].advance = advance;
2356 swffont->glyph[t].advance = 32767;
2359 draw.dealloc(&draw);
2361 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2363 if(bounds.ymin < 0 && bounds.ymax > 0) {
2364 swffont->layout->ascent = -bounds.ymin;
2365 swffont->layout->descent = bounds.ymax;
2366 swffont->layout->leading = bounds.ymax - bounds.ymin;
2368 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2369 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2370 swffont->layout->leading = bounds.ymax - bounds.ymin;
2376 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2378 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2380 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2381 return; // the requested font is the current font
2383 fontlist_t*last=0,*l = i->fontlist;
2386 if(!strcmp((char*)l->swffont->name, font->id)) {
2387 return; // we already know this font
2391 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2392 l->swffont = gfxfont_to_swffont(font, font->id);
2399 swf_FontSetID(l->swffont, getNewID(i->dev));
2401 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2403 // print font information
2404 msg("<debug> Font %s",font->id);
2405 msg("<debug> | ID: %d", l->swffont->id);
2406 msg("<debug> | Version: %d", l->swffont->version);
2407 msg("<debug> | Name: %s", l->swffont->name);
2408 msg("<debug> | Numchars: %d", l->swffont->numchars);
2409 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2410 msg("<debug> | Style: %d", l->swffont->style);
2411 msg("<debug> | Encoding: %d", l->swffont->encoding);
2412 for(iii=0; iii<l->swffont->numchars;iii++) {
2413 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,
2414 l->swffont->layout->bounds[iii].xmin/20.0,
2415 l->swffont->layout->bounds[iii].ymin/20.0,
2416 l->swffont->layout->bounds[iii].xmax/20.0,
2417 l->swffont->layout->bounds[iii].ymax/20.0
2420 for(t=0;t<l->swffont->maxascii;t++) {
2421 if(l->swffont->ascii2glyph[t] == iii)
2422 msg("<debug> | - maps to %d",t);
2428 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2430 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2432 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2433 return; // the requested font is the current font
2435 fontlist_t*l = i->fontlist;
2437 if(!strcmp((char*)l->swffont->name, fontid)) {
2438 i->swffont = l->swffont;
2443 msg("<error> Unknown font id: %s", fontid);
2447 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2449 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2451 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2453 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2454 with multiple fonts */
2457 swf_switchfont(dev, font->id); // set the current font
2459 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2469 /* printf("%f %f\n", m.m31, m.m32);
2471 static int xpos = 40;
2472 static int ypos = 200;
2479 drawchar(dev, i->swffont, glyph, &m, color);