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"
40 #include "../rfxswf.h"
41 #include "../gfxdevice.h"
42 #include "../gfxtools.h"
43 #include "../art/libart.h"
45 #include "artsutils.h"
47 #define CHARDATAMAX 8192
51 typedef struct _chardata {
53 int fontid; /* TODO: use a SWFFONT instead */
60 typedef struct _fontlist
63 struct _fontlist*next;
66 typedef long int twip;
68 typedef struct _swfmatrix {
69 double m11,m12,m21,m22,m31,m32;
72 typedef struct _swfoutput_internal
74 gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
76 double config_dumpfonts;
77 double config_ppmsubpixels;
78 double config_jpegsubpixels;
79 int config_opennewwindow;
80 int config_ignoredraworder;
81 int config_drawonlyshapes;
82 int config_jpegquality;
83 int config_storeallcharacters;
84 int config_enablezlib;
85 int config_insertstoptag;
86 int config_flashversion;
87 int config_reordertags;
88 int config_showclipshapes;
89 int config_splinemaxerror;
90 int config_fontsplinemaxerror;
91 int config_filloverlap;
94 int config_disable_polygon_conversion;
95 int config_normalize_polygon_positions;
96 RGBA config_linkcolor;
97 float config_minlinewidth;
98 double config_caplinewidth;
99 char* config_linktarget;
100 char*config_internallinkfunction;
101 char*config_externallinkfunction;
103 double config_framerate;
107 fontlist_t* fontlist;
142 int pic_height[1024];
148 char fillstylechanged;
150 int jpeg; //next image type
155 chardata_t chardata[CHARDATAMAX];
162 int current_font_size;
164 double lastfontm11,lastfontm12,lastfontm21,lastfontm22;
175 } swfoutput_internal;
177 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
178 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
179 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);
180 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
181 static void swf_endclip(gfxdevice_t*dev);
182 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);
183 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
184 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
185 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
186 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
187 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
188 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action);
189 static void swf_startframe(gfxdevice_t*dev, int width, int height);
190 static void swf_endframe(gfxdevice_t*dev);
191 static gfxresult_t* swf_finish(gfxdevice_t*driver);
193 static swfoutput_internal* init_internal_struct()
195 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
196 memset(i, 0, sizeof(swfoutput_internal));
220 i->fillstylechanged = 0;
227 i->config_dumpfonts=0;
228 i->config_ppmsubpixels=0;
229 i->config_jpegsubpixels=0;
230 i->config_opennewwindow=1;
231 i->config_ignoredraworder=0;
232 i->config_drawonlyshapes=0;
233 i->config_jpegquality=85;
234 i->config_storeallcharacters=0;
235 i->config_enablezlib=0;
236 i->config_insertstoptag=0;
237 i->config_flashversion=6;
238 i->config_framerate=0.25;
239 i->config_splinemaxerror=1;
240 i->config_fontsplinemaxerror=1;
241 i->config_filloverlap=0;
243 i->config_bboxvars=0;
244 i->config_showclipshapes=0;
245 i->config_minlinewidth=0.05;
246 i->config_caplinewidth=1;
247 i->config_linktarget=0;
248 i->config_internallinkfunction=0;
249 i->config_externallinkfunction=0;
250 i->config_reordertags=1;
252 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
253 i->config_linkcolor.a = 0x40;
258 static int id_error = 0;
260 static U16 getNewID(gfxdevice_t* dev)
262 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
263 if(i->currentswfid == 65535) {
265 msg("<error> ID Table overflow");
266 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
272 return ++i->currentswfid;
274 static U16 getNewDepth(gfxdevice_t* dev)
276 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
277 if(i->depth == 65535) {
279 msg("<error> Depth Table overflow");
280 msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
289 static void startshape(gfxdevice_t* dev);
290 static void starttext(gfxdevice_t* dev);
291 static void endshape(gfxdevice_t* dev);
292 static void endtext(gfxdevice_t* dev);
294 typedef struct _plotxy
299 // write a move-to command into the swf
300 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
302 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
303 int rx = (int)(p0.x*20);
304 int ry = (int)(p0.y*20);
305 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
306 swf_ShapeSetMove (tag, i->shape, rx,ry);
307 i->fillstylechanged = 0;
314 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
316 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
320 return movetoxy(dev, tag, p);
322 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
324 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
330 swf_ExpandRect(&i->bboxrect, p);
332 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
336 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
338 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
339 int width = i->linewidth/4;
343 //swf_ShapeSetLine(tag, i->shape,-width,-width);
344 //swf_ShapeSetLine(tag, i->shape,width*2,0);
345 //swf_ShapeSetLine(tag, i->shape,0,width*2);
346 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
347 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
348 //swf_ShapeSetLine(tag, i->shape,width,width);
351 swf_ShapeSetLine(tag, i->shape,-width,0);
352 swf_ShapeSetLine(tag, i->shape,width,-width);
353 swf_ShapeSetLine(tag, i->shape,width,width);
354 swf_ShapeSetLine(tag, i->shape,-width,width);
355 swf_ShapeSetLine(tag, i->shape,-width,-width);
356 swf_ShapeSetLine(tag, i->shape,width,0);
358 addPointToBBox(dev, x-width ,y-width);
359 addPointToBBox(dev, x+width ,y+width);
362 // write a line-to command into the swf
363 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
365 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
366 int px = (int)(p0.x*20);
367 int py = (int)(p0.y*20);
368 int rx = (px-i->swflastx);
369 int ry = (py-i->swflasty);
371 swf_ShapeSetLine (tag, i->shape, rx,ry);
372 addPointToBBox(dev, i->swflastx,i->swflasty);
373 addPointToBBox(dev, px,py);
374 }/* else if(!i->fill) {
375 // treat lines of length 0 as plots, making them
376 // at least 1 twip wide so Flash will display them
377 plot(dev, i->swflastx, i->swflasty, tag);
384 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
389 linetoxy(dev,tag, p);
392 // write a spline-to command into the swf
393 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
395 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
396 int lastlastx = i->swflastx;
397 int lastlasty = i->swflasty;
399 int cx = ((int)(control.x*20)-i->swflastx);
400 int cy = ((int)(control.y*20)-i->swflasty);
403 int ex = ((int)(end.x*20)-i->swflastx);
404 int ey = ((int)(end.y*20)-i->swflasty);
408 if((cx || cy) && (ex || ey)) {
409 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
410 addPointToBBox(dev, lastlastx ,lastlasty );
411 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
412 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
413 } else if(cx || cy || ex || ey) {
414 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
415 addPointToBBox(dev, lastlastx ,lastlasty );
416 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
417 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
423 /* write a line, given two points and the transformation
425 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
427 moveto(dev, tag, p0);
428 lineto(dev, tag, p1);
431 void resetdrawer(gfxdevice_t*dev)
433 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
438 static void stopFill(gfxdevice_t*dev)
440 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
441 if(i->lastwasfill!=0)
443 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
444 i->fillstylechanged = 1;
448 static void startFill(gfxdevice_t*dev)
450 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
451 if(i->lastwasfill!=1)
453 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
454 i->fillstylechanged = 1;
459 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
471 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
473 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
476 memset(&r, 0, sizeof(r));
479 if(debug) printf("\n");
480 for(t=0;t<i->chardatapos;t++)
482 if(i->chardata[t].fontid != font->id) {
483 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
486 SRECT b = font->layout->bounds[i->chardata[t].charid];
487 b.xmin *= i->chardata[t].size;
488 b.ymin *= i->chardata[t].size;
489 b.xmax *= i->chardata[t].size;
490 b.ymax *= i->chardata[t].size;
492 /* divide by 1024, rounding xmax/ymax up */
493 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
495 b.xmin += i->chardata[t].x;
496 b.ymin += i->chardata[t].y;
497 b.xmax += i->chardata[t].x;
498 b.ymax += i->chardata[t].y;
500 /* until we solve the INTERNAL_SCALING problem (see below)
501 make sure the bounding box is big enough */
507 b = swf_TurnRect(b, m);
509 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
510 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
511 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
512 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
513 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
518 i->chardata[t].fontid,
520 i->chardata[t].charid
522 swf_ExpandRect2(&r, &b);
524 if(debug) printf("-----> (%f,%f,%f,%f)\n",
532 static void putcharacters(gfxdevice_t*dev, TAG*tag)
534 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
538 color.r = i->chardata[0].color.r^255;
547 int charadvance[128];
550 int glyphbits=1; //TODO: can this be zero?
553 if(tag->id != ST_DEFINETEXT &&
554 tag->id != ST_DEFINETEXT2) {
555 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
558 if(!i->chardatapos) {
559 msg("<warning> putcharacters called with zero characters");
562 for(pass = 0; pass < 2; pass++)
572 advancebits++; // add sign bit
573 swf_SetU8(tag, glyphbits);
574 swf_SetU8(tag, advancebits);
577 for(t=0;t<=i->chardatapos;t++)
579 if(lastfontid != i->chardata[t].fontid ||
580 lastx!=i->chardata[t].x ||
581 lasty!=i->chardata[t].y ||
582 !colorcompare(dev,&color, &i->chardata[t].color) ||
584 lastsize != i->chardata[t].size ||
587 if(charstorepos && pass==0)
590 for(s=0;s<charstorepos;s++)
592 while(charids[s]>=(1<<glyphbits))
594 while(charadvance[s]>=(1<<advancebits))
598 if(charstorepos && pass==1)
600 tag->writeBit = 0; // Q&D
601 swf_SetBits(tag, 0, 1); // GLYPH Record
602 swf_SetBits(tag, charstorepos, 7); // number of glyphs
604 for(s=0;s<charstorepos;s++)
606 swf_SetBits(tag, charids[s], glyphbits);
607 swf_SetBits(tag, charadvance[s], advancebits);
612 if(pass == 1 && t<i->chardatapos)
618 if(lastx != i->chardata[t].x ||
619 lasty != i->chardata[t].y)
621 newx = i->chardata[t].x;
622 newy = i->chardata[t].y;
628 if(!colorcompare(dev,&color, &i->chardata[t].color))
630 color = i->chardata[t].color;
633 font.id = i->chardata[t].fontid;
634 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
637 tag->writeBit = 0; // Q&D
638 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
641 lastfontid = i->chardata[t].fontid;
642 lastx = i->chardata[t].x;
643 lasty = i->chardata[t].y;
644 lastsize = i->chardata[t].size;
647 if(t==i->chardatapos)
651 int nextt = t==i->chardatapos-1?t:t+1;
652 int rel = i->chardata[nextt].x-i->chardata[t].x;
653 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
655 lastx=i->chardata[nextt].x;
659 lastx=i->chardata[t].x;
661 charids[charstorepos] = i->chardata[t].charid;
662 charadvance[charstorepos] = advance;
669 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
671 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
672 if(i->chardatapos == CHARDATAMAX)
674 msg("<warning> Character buffer too small. SWF will be slightly bigger");
678 i->chardata[i->chardatapos].fontid = fontid;
679 i->chardata[i->chardatapos].charid = charid;
680 i->chardata[i->chardatapos].x = x;
681 i->chardata[i->chardatapos].y = y;
682 i->chardata[i->chardatapos].color = color;
683 i->chardata[i->chardatapos].size = size;
687 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
688 So if we set this value to high, the char coordinates will overflow.
689 If we set it to low, however, the char positions will be inaccurate */
690 #define GLYPH_SCALE 1
692 static void endtext(gfxdevice_t*dev)
694 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
698 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT2);
699 swf_SetU16(i->tag, i->textid);
702 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
704 swf_SetRect(i->tag,&r);
706 swf_SetMatrix(i->tag,&i->fontmatrix);
708 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
710 putcharacters(dev, i->tag);
713 if(i->swf->fileVersion >= 8) {
714 i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
715 swf_SetU16(i->tag, i->textid);
717 //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
718 swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
719 //swf_SetU8(i->tag, /*no grid*/(0<<3)|/*flashtype*/0x40);
721 swf_SetU32(i->tag, 0);//thickness
722 swf_SetU32(i->tag, 0);//sharpness
723 swf_SetU8(i->tag, 0);//reserved
725 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
727 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
731 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
732 static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22,double x, double y, char force)
738 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
739 if(i->lastfontm11 == m11 &&
740 i->lastfontm12 == m12 &&
741 i->lastfontm21 == m21 &&
742 i->lastfontm22 == m22 && !force)
747 i->lastfontm11 = m11;
748 i->lastfontm12 = m12;
749 i->lastfontm21 = m21;
750 i->lastfontm22 = m22;
752 double xsize = sqrt(m11*m11 + m12*m12);
753 double ysize = sqrt(m21*m21 + m22*m22);
754 i->current_font_size = (xsize>ysize?xsize:ysize)*1;
755 if(i->current_font_size < 1)
756 i->current_font_size = 1;
757 double ifs = 1.0 / (i->current_font_size*GLYPH_SCALE);
760 m.sx = (U32)((m11*ifs)*65536); m.r1 = (U32)((m21*ifs)*65536);
761 m.r0 = (U32)((m12*ifs)*65536); m.sy = (U32)((m22*ifs)*65536);
762 /* this is the position of the first char to set a new fontmatrix-
763 we hope that it's close enough to all other characters using the
764 font, so we use its position as origin for the matrix */
770 static void endpage(gfxdevice_t*dev)
772 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
782 void swf_startframe(gfxdevice_t*dev, int width, int height)
784 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
785 if(!i->firstpage && !i->pagefinished)
787 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
789 swf_GetMatrix(0, &i->page_matrix);
790 i->page_matrix.tx = 0;
791 i->page_matrix.ty = 0;
797 /* set clipping/background rectangle */
798 /* TODO: this should all be done in SWFOutputDev */
799 //setBackground(dev, x1, y1, x2, y2);
801 /* increase SWF's bounding box */
807 swf_ExpandRect2(&i->swf->movieSize, &r);
809 i->lastframeno = i->frameno;
814 void swf_endframe(gfxdevice_t*dev)
816 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
821 if( (i->swf->fileVersion <= 8) && (i->config_insertstoptag) ) {
823 atag = action_Stop(atag);
824 atag = action_End(atag);
825 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
826 swf_ActionSet(i->tag,atag);
828 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
831 for(i->depth;i->depth>i->startdepth;i->depth--) {
832 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
833 swf_SetU16(i->tag,i->depth);
835 i->depth = i->startdepth;
838 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
840 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
842 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
846 int shapeid = getNewID(dev);
851 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
853 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
854 swf_SetU16(i->tag,shapeid);
855 swf_SetRect(i->tag,&r);
856 swf_SetShapeHeader(i->tag,s);
857 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
858 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
859 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
860 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
861 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
862 swf_ShapeSetEnd(i->tag);
864 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
865 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
866 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
867 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
870 /* initialize the swf writer */
871 void gfxdevice_swf_init(gfxdevice_t* dev)
873 memset(dev, 0, sizeof(gfxdevice_t));
877 dev->internal = init_internal_struct();
879 dev->startpage = swf_startframe;
880 dev->endpage = swf_endframe;
881 dev->finish = swf_finish;
882 dev->fillbitmap = swf_fillbitmap;
883 dev->setparameter = swf_setparameter;
884 dev->stroke = swf_stroke;
885 dev->startclip = swf_startclip;
886 dev->endclip = swf_endclip;
887 dev->fill = swf_fill;
888 dev->fillbitmap = swf_fillbitmap;
889 dev->fillgradient = swf_fillgradient;
890 dev->addfont = swf_addfont;
891 dev->drawchar = swf_drawchar;
892 dev->drawlink = swf_drawlink;
894 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
899 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
903 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
904 i->swf->fileVersion = i->config_flashversion;
905 i->swf->frameRate = i->config_framerate*0x100;
906 i->swf->movieSize.xmin = 0;
907 i->swf->movieSize.ymin = 0;
908 i->swf->movieSize.xmax = 0;
909 i->swf->movieSize.ymax = 0;
911 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
912 i->tag = i->swf->firstTag;
913 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
914 swf_SetRGB(i->tag,&rgb);
916 i->startdepth = i->depth = 0;
918 if(i->config_protect)
919 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
922 static void startshape(gfxdevice_t*dev)
924 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
929 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a)
932 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
934 swf_ShapeNew(&i->shape);
935 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
936 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
938 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
939 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
942 i->shapeid = getNewID(dev);
944 msg("<debug> Using shape id %d", i->shapeid);
946 swf_SetU16(i->tag,i->shapeid); // ID
948 i->bboxrectpos = i->tag->len;
952 r.xmax = 20*i->max_x;
953 r.ymax = 20*i->max_y;
954 swf_SetRect(i->tag,&r);
956 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
958 swf_SetShapeStyles(i->tag,i->shape);
959 swf_ShapeCountBits(i->shape,NULL,NULL);
960 swf_SetShapeBits(i->tag,i->shape);
962 /* TODO: do we really need this? */
963 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
964 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
965 i->swflastx=i->swflasty=UNDEFINED_COORD;
970 static void starttext(gfxdevice_t*dev)
972 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
976 i->textid = getNewID(dev);
978 i->swflastx=i->swflasty=0;
982 /* TODO: move to ../lib/rfxswf */
983 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
985 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
986 /* determine length of old rect */
990 swf_GetRect(tag, &old);
991 swf_ResetReadBits(tag);
992 int pos_end = tag->pos;
994 int len = tag->len - pos_end;
995 U8*data = (U8*)malloc(len);
996 memcpy(data, &tag->data[pos_end], len);
999 swf_SetRect(tag, newrect);
1000 swf_SetBlock(tag, data, len);
1002 tag->pos = tag->readBit = 0;
1005 void cancelshape(gfxdevice_t*dev)
1007 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1008 /* delete old shape tag */
1010 i->tag = i->tag->prev;
1011 swf_DeleteTag(todel);
1012 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1014 i->bboxrectpos = -1;
1016 // i->currentswfid--; // this was an *exceptionally* bad idea
1019 void fixAreas(gfxdevice_t*dev)
1021 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1022 if(!i->shapeisempty && i->fill &&
1023 (i->bboxrect.xmin == i->bboxrect.xmax ||
1024 i->bboxrect.ymin == i->bboxrect.ymax) &&
1025 i->config_minlinewidth >= 0.001
1027 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1028 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1029 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1032 SRECT r = i->bboxrect;
1034 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1035 /* this thing comes down to a single dot- nothing to fix here */
1041 RGBA save_col = i->strokergb;
1042 int save_width = i->linewidth;
1044 i->strokergb = i->fillrgb;
1045 i->linewidth = (int)(i->config_minlinewidth*20);
1046 if(i->linewidth==0) i->linewidth = 1;
1051 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1052 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1054 i->strokergb = save_col;
1055 i->linewidth = save_width;
1060 static void endshape_noput(gfxdevice_t*dev)
1062 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1065 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1068 swf_ShapeFree(i->shape);
1076 static void endshape(gfxdevice_t*dev)
1078 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1084 if(i->shapeisempty ||
1086 (i->bboxrect.xmin == i->bboxrect.xmax &&
1087 i->bboxrect.ymin == i->bboxrect.ymax))
1089 // delete the shape again, we didn't do anything
1090 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1091 i->bboxrect.xmin /20.0,
1092 i->bboxrect.ymin /20.0,
1093 i->bboxrect.xmax /20.0,
1094 i->bboxrect.ymax /20.0
1100 swf_ShapeSetEnd(i->tag);
1102 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1104 msg("<trace> Placing shape ID %d", i->shapeid);
1106 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1107 MATRIX m = i->page_matrix;
1108 m.tx += i->shapeposx;
1109 m.ty += i->shapeposy;
1110 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1112 if(i->config_animate) {
1113 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1116 swf_ShapeFree(i->shape);
1119 i->bboxrectpos = -1;
1126 void wipeSWF(SWF*swf)
1128 TAG*tag = swf->firstTag;
1130 TAG*next = tag->next;
1131 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1132 tag->id != ST_END &&
1133 tag->id != ST_DOACTION &&
1134 tag->id != ST_SHOWFRAME) {
1141 void swfoutput_finalize(gfxdevice_t*dev)
1143 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1145 if(i->tag && i->tag->id == ST_END)
1146 return; //already done
1148 if(i->config_bboxvars) {
1149 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1151 a = action_PushString(a, "xmin");
1152 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1153 a = action_SetVariable(a);
1154 a = action_PushString(a, "ymin");
1155 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1156 a = action_SetVariable(a);
1157 a = action_PushString(a, "xmax");
1158 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1159 a = action_SetVariable(a);
1160 a = action_PushString(a, "ymax");
1161 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1162 a = action_SetVariable(a);
1163 a = action_PushString(a, "width");
1164 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1165 a = action_SetVariable(a);
1166 a = action_PushString(a, "height");
1167 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1168 a = action_SetVariable(a);
1170 swf_ActionSet(tag, a);
1174 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1178 free(i->mark);i->mark = 0;
1182 fontlist_t *iterator = i->fontlist;
1184 TAG*mtag = i->swf->firstTag;
1185 if(iterator->swffont) {
1186 if(!i->config_storeallcharacters) {
1187 msg("<debug> Reducing font %s", iterator->swffont->name);
1188 swf_FontReduce(iterator->swffont);
1190 int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1192 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1193 swf_FontSetDefine2(mtag, iterator->swffont);
1197 iterator = iterator->next;
1199 i->tag = swf_InsertTag(i->tag,ST_END);
1200 TAG* tag = i->tag->prev;
1202 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1203 and the ST_END- they confuse the flash player */
1204 while(tag->id == ST_REMOVEOBJECT2) {
1205 TAG* prev = tag->prev;
1213 if(i->config_enablezlib || i->config_flashversion>=6) {
1214 i->swf->compressed = 1;
1217 /* Initialize AVM2 if it is a Flash9 file */
1218 if(i->config_flashversion>=9 && i->config_insertstoptag) {
1219 AVM2_InsertStops(i->swf);
1221 // if(i->config_reordertags)
1222 // swf_Optimize(i->swf);
1225 int swfresult_save(gfxresult_t*gfx, const char*filename)
1227 SWF*swf = (SWF*)gfx->internal;
1230 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1235 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1239 if FAILED(swf_WriteSWF(fi,swf))
1240 msg("<error> WriteSWF() failed.\n");
1246 void* swfresult_get(gfxresult_t*gfx, const char*name)
1248 SWF*swf = (SWF*)gfx->internal;
1249 if(!strcmp(name, "swf")) {
1250 return (void*)swf_CopySWF(swf);
1251 } else if(!strcmp(name, "xmin")) {
1252 return (void*)(swf->movieSize.xmin/20);
1253 } else if(!strcmp(name, "ymin")) {
1254 return (void*)(swf->movieSize.ymin/20);
1255 } else if(!strcmp(name, "xmax")) {
1256 return (void*)(swf->movieSize.xmax/20);
1257 } else if(!strcmp(name, "ymax")) {
1258 return (void*)(swf->movieSize.ymax/20);
1259 } else if(!strcmp(name, "width")) {
1260 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1261 } else if(!strcmp(name, "height")) {
1262 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1266 void swfresult_destroy(gfxresult_t*gfx)
1269 swf_FreeTags((SWF*)gfx->internal);
1270 free(gfx->internal);
1273 memset(gfx, 0, sizeof(gfxresult_t));
1277 static void swfoutput_destroy(gfxdevice_t* dev);
1279 gfxresult_t* swf_finish(gfxdevice_t* dev)
1281 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1284 if(i->config_linktarget) {
1285 free(i->config_linktarget);
1286 i->config_linktarget = 0;
1289 swfoutput_finalize(dev);
1290 SWF* swf = i->swf;i->swf = 0;
1291 swfoutput_destroy(dev);
1293 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1294 result->internal = swf;
1295 result->save = swfresult_save;
1297 result->get = swfresult_get;
1298 result->destroy = swfresult_destroy;
1302 /* Perform cleaning up */
1303 static void swfoutput_destroy(gfxdevice_t* dev)
1305 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1307 /* not initialized yet- nothing to destroy */
1311 fontlist_t *tmp,*iterator = i->fontlist;
1313 if(iterator->swffont) {
1314 swf_FontFree(iterator->swffont);iterator->swffont=0;
1317 iterator = iterator->next;
1320 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1323 memset(dev, 0, sizeof(gfxdevice_t));
1326 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1328 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1329 if(i->fillrgb.r == r &&
1330 i->fillrgb.g == g &&
1331 i->fillrgb.b == b &&
1332 i->fillrgb.a == a) return;
1342 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1344 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1345 if(i->strokergb.r == r &&
1346 i->strokergb.g == g &&
1347 i->strokergb.b == b &&
1348 i->strokergb.a == a) return;
1358 //#define ROUND_UP 19
1359 //#define ROUND_UP 10
1361 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1363 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1364 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1368 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1372 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1373 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1374 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1375 static void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points);
1377 /*void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1379 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1380 dev->drawlink(dev, points, url);
1383 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, const char*url)
1385 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1387 if(!strncmp("http://pdf2swf:", url, 15)) {
1388 char*tmp = strdup(url);
1389 int l = strlen(tmp);
1392 swfoutput_namedlink(dev, tmp+15, points);
1395 } else if(!strncmp("page", url, 4)) {
1398 if(url[t]<'0' || url[t]>'9')
1401 int page = atoi(&url[4]);
1402 if(page<0) page = 0;
1403 swfoutput_linktopage(dev, page, points);
1406 swfoutput_linktourl(dev, url, points);
1409 void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points)
1411 ActionTAG* actions = 0;
1412 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1418 if(i->config_externallinkfunction) {
1419 actions = action_PushString(actions, url); //parameter
1420 actions = action_PushInt(actions, 1); //number of parameters (1)
1421 actions = action_PushString(actions, i->config_externallinkfunction); //function name
1422 actions = action_CallFunction(actions);
1423 } else if(!i->config_linktarget) {
1424 if(!i->config_opennewwindow)
1425 actions = action_GetUrl(actions, url, "_parent");
1427 actions = action_GetUrl(actions, url, "_this");
1429 actions = action_GetUrl(actions, url, i->config_linktarget);
1431 actions = action_End(actions);
1433 drawlink(dev, actions, 0, points,0);
1435 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1437 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1438 ActionTAG* actions = 0;
1445 if(!i->config_internallinkfunction) {
1446 actions = action_GotoFrame(actions, page-1);
1447 actions = action_End(actions);
1449 actions = action_PushInt(actions, page); //parameter
1450 actions = action_PushInt(actions, 1); //number of parameters (1)
1451 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1452 actions = action_CallFunction(actions);
1453 actions = action_End(actions);
1456 drawlink(dev, actions, 0, points,0);
1459 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1460 of the viewer objects, like subtitles, index elements etc.
1462 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1464 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1465 ActionTAG *actions1,*actions2;
1466 char*tmp = strdup(name);
1474 if(!strncmp(tmp, "call:", 5))
1476 char*x = strchr(&tmp[5], ':');
1478 actions1 = action_PushInt(0, 0); //number of parameters (0)
1479 actions1 = action_PushString(actions1, &tmp[5]); //function name
1480 actions1 = action_CallFunction(actions1);
1481 actions1 = action_End(actions1);
1484 actions1 = action_PushString(0, x+1); //parameter
1485 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1486 actions1 = action_PushString(actions1, &tmp[5]); //function name
1487 actions1 = action_CallFunction(actions1);
1488 actions1 = action_End(actions1);
1490 actions2 = action_End(0);
1495 actions1 = action_PushString(0, "/:subtitle");
1496 actions1 = action_PushString(actions1, name);
1497 actions1 = action_SetVariable(actions1);
1498 actions1 = action_End(actions1);
1500 actions2 = action_PushString(0, "/:subtitle");
1501 actions2 = action_PushString(actions2, "");
1502 actions2 = action_SetVariable(actions2);
1503 actions2 = action_End(actions2);
1506 drawlink(dev, actions1, actions2, points,mouseover);
1508 swf_ActionFree(actions1);
1509 swf_ActionFree(actions2);
1513 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1515 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1516 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1518 int lines= 0, splines=0;
1522 /* check whether the next segment is zero */
1523 if(line->type == gfx_moveTo) {
1524 moveto(dev, i->tag, line->x, line->y);
1525 px = lastx = line->x;
1526 py = lasty = line->y;
1528 } if(line->type == gfx_lineTo) {
1529 lineto(dev, i->tag, line->x, line->y);
1534 } else if(line->type == gfx_splineTo) {
1536 s.x = line->sx;p.x = line->x;
1537 s.y = line->sy;p.y = line->y;
1538 splineto(dev, i->tag, s, p);
1546 msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1550 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1552 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1561 int buttonid = getNewID(dev);
1562 gfxbbox_t bbox = gfxline_getbbox(points);
1565 myshapeid = getNewID(dev);
1566 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1567 swf_ShapeNew(&i->shape);
1568 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1569 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1570 swf_SetU16(i->tag, myshapeid);
1571 r.xmin = (int)(bbox.xmin*20);
1572 r.ymin = (int)(bbox.ymin*20);
1573 r.xmax = (int)(bbox.xmax*20);
1574 r.ymax = (int)(bbox.ymax*20);
1575 swf_SetRect(i->tag,&r);
1576 swf_SetShapeStyles(i->tag,i->shape);
1577 swf_ShapeCountBits(i->shape,NULL,NULL);
1578 swf_SetShapeBits(i->tag,i->shape);
1579 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1580 i->swflastx = i->swflasty = 0;
1581 drawgfxline(dev, points);
1582 swf_ShapeSetEnd(i->tag);
1585 myshapeid2 = getNewID(dev);
1586 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1587 swf_ShapeNew(&i->shape);
1589 rgb = i->config_linkcolor;
1591 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1592 swf_SetU16(i->tag, myshapeid2);
1593 r.xmin = (int)(bbox.xmin*20);
1594 r.ymin = (int)(bbox.ymin*20);
1595 r.xmax = (int)(bbox.xmax*20);
1596 r.ymax = (int)(bbox.ymax*20);
1597 swf_SetRect(i->tag,&r);
1598 swf_SetShapeStyles(i->tag,i->shape);
1599 swf_ShapeCountBits(i->shape,NULL,NULL);
1600 swf_SetShapeBits(i->tag,i->shape);
1601 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1602 i->swflastx = i->swflasty = 0;
1603 drawgfxline(dev, points);
1604 swf_ShapeSetEnd(i->tag);
1608 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1609 swf_SetU16(i->tag,buttonid); //id
1610 swf_ButtonSetFlags(i->tag, 0); //menu=no
1611 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1612 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1613 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1614 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1615 swf_SetU8(i->tag,0);
1616 swf_ActionSet(i->tag,actions1);
1617 swf_SetU8(i->tag,0);
1621 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1622 swf_SetU16(i->tag,buttonid); //id
1623 swf_ButtonSetFlags(i->tag, 0); //menu=no
1624 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1625 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1626 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1627 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1628 swf_SetU8(i->tag,0); // end of button records
1629 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1630 swf_ActionSet(i->tag,actions1);
1632 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1633 swf_ActionSet(i->tag,actions2);
1634 swf_SetU8(i->tag,0);
1635 swf_ButtonPostProcess(i->tag, 2);
1637 swf_SetU8(i->tag,0);
1638 swf_ButtonPostProcess(i->tag, 1);
1642 msg("<trace> Placing link ID %d", buttonid);
1643 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1645 if(posx!=0 || posy!=0) {
1647 p.x = (int)(posx*20);
1648 p.y = (int)(posy*20);
1649 p = swf_TurnPoint(p, &i->page_matrix);
1654 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1656 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1663 for(t=0;t<picpos;t++)
1665 if(pic_xids[t] == xid &&
1666 pic_yids[t] == yid) {
1667 width = pic_width[t];
1668 height = pic_height[t];
1672 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1673 pic_xids[picpos] = xid;
1674 pic_yids[picpos] = yid;
1675 pic_width[picpos] = width;
1676 pic_height[picpos] = height;
1679 pic[width*y+x] = buf[0];
1683 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1684 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1688 xid += x*r+x*b*3+x*g*7+x*a*11;
1689 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1691 for(t=0;t<picpos;t++)
1693 if(pic_xids[t] == xid &&
1694 pic_yids[t] == yid) {
1703 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1705 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1707 msg("<trace> swfdevice: %s=%s", name, value);
1708 if(!strcmp(name, "jpegsubpixels")) {
1709 i->config_jpegsubpixels = atof(value);
1710 } else if(!strcmp(name, "ppmsubpixels")) {
1711 i->config_ppmsubpixels = atof(value);
1712 } else if(!strcmp(name, "subpixels")) {
1713 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1714 } else if(!strcmp(name, "drawonlyshapes")) {
1715 i->config_drawonlyshapes = atoi(value);
1716 } else if(!strcmp(name, "ignoredraworder")) {
1717 i->config_ignoredraworder = atoi(value);
1718 } else if(!strcmp(name, "mark")) {
1719 if(!value || !value[0]) {
1720 if(i->mark) free(i->mark);
1724 i->mark = strdup("...");
1725 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1727 } else if(!strcmp(name, "filloverlap")) {
1728 i->config_filloverlap = atoi(value);
1729 } else if(!strcmp(name, "linksopennewwindow")) {
1730 i->config_opennewwindow = atoi(value);
1731 } else if(!strcmp(name, "opennewwindow")) {
1732 i->config_opennewwindow = atoi(value);
1733 } else if(!strcmp(name, "storeallcharacters")) {
1734 i->config_storeallcharacters = atoi(value);
1735 } else if(!strcmp(name, "enablezlib")) {
1736 i->config_enablezlib = atoi(value);
1737 } else if(!strcmp(name, "bboxvars")) {
1738 i->config_bboxvars = atoi(value);
1739 } else if(!strcmp(name, "showclipshapes")) {
1740 i->config_showclipshapes = atoi(value);
1741 } else if(!strcmp(name, "reordertags")) {
1742 i->config_reordertags = atoi(value);
1743 } else if(!strcmp(name, "internallinkfunction")) {
1744 i->config_internallinkfunction = strdup(value);
1745 } else if(!strcmp(name, "externallinkfunction")) {
1746 i->config_externallinkfunction = strdup(value);
1747 } else if(!strcmp(name, "linkfunction")) { //sets both internallinkfunction and externallinkfunction
1748 i->config_internallinkfunction = strdup(value);
1749 i->config_externallinkfunction = strdup(value);
1750 } else if(!strcmp(name, "disable_polygon_conversion")) {
1751 i->config_disable_polygon_conversion = atoi(value);
1752 } else if(!strcmp(name, "normalize_polygon_positions")) {
1753 i->config_normalize_polygon_positions = atoi(value);
1754 } else if(!strcmp(name, "insertstop")) {
1755 i->config_insertstoptag = atoi(value);
1756 } else if(!strcmp(name, "protect")) {
1757 i->config_protect = atoi(value);
1758 if(i->config_protect && i->tag) {
1759 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1761 } else if(!strcmp(name, "flashversion")) {
1762 i->config_flashversion = atoi(value);
1764 i->swf->fileVersion = i->config_flashversion;
1766 } else if(!strcmp(name, "framerate")) {
1767 i->config_framerate = atoi(value);
1769 i->swf->frameRate = i->config_framerate*0x100;
1771 } else if(!strcmp(name, "minlinewidth")) {
1772 i->config_minlinewidth = atof(value);
1773 } else if(!strcmp(name, "caplinewidth")) {
1774 i->config_caplinewidth = atof(value);
1775 } else if(!strcmp(name, "linktarget")) {
1776 i->config_linktarget = strdup(value);
1777 } else if(!strcmp(name, "dumpfonts")) {
1778 i->config_dumpfonts = atoi(value);
1779 } else if(!strcmp(name, "animate")) {
1780 i->config_animate = atoi(value);
1781 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1783 } else if(!strcmp(name, "jpegquality")) {
1784 int val = atoi(value);
1786 if(val>101) val=101;
1787 i->config_jpegquality = val;
1788 } else if(!strcmp(name, "splinequality")) {
1789 int v = atoi(value);
1790 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1792 i->config_splinemaxerror = v;
1793 } else if(!strcmp(name, "fontquality")) {
1794 int v = atoi(value);
1795 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1797 i->config_fontsplinemaxerror = v;
1798 } else if(!strcmp(name, "linkcolor")) {
1799 if(strlen(value)!=8) {
1800 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1803 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1804 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1805 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1806 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1807 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1808 printf("%02x%02x%02x%02x\n",
1809 i->config_linkcolor.r,
1810 i->config_linkcolor.g,
1811 i->config_linkcolor.b,
1812 i->config_linkcolor.a);
1814 } else if(!strcmp(name, "help")) {
1815 printf("\nSWF layer options:\n");
1816 printf("jpegdpi=<dpi> resolution adjustment for jpeg images\n");
1817 printf("jpegsubpixels=<pixels> resolution adjustment for jpeg images (same as jpegdpi, but in pixels)\n");
1818 printf("ppmdpi=<dpi> resolution adjustment for lossless images\n");
1819 printf("ppmsubpixels=<pixels resolution adjustment for lossless images (same as ppmdpi, but in pixels)\n");
1820 printf("subpixels=<pixels> shortcut for setting both jpegsubpixels and ppmsubpixels\n");
1821 printf("drawonlyshapes convert everything to shapes (currently broken)\n");
1822 printf("ignoredraworder allow to perform a few optimizations for creating smaller SWFs\n");
1823 printf("linksopennewwindow make links open a new browser window\n");
1824 printf("linktarget target window name of new links\n");
1825 printf("linkcolor=<color) color of links (format: RRGGBBAA)\n");
1826 printf("storeallcharacters don't reduce the fonts to used characters in the output file\n");
1827 printf("enablezlib switch on zlib compression (also done if flashversion>=7)\n");
1828 printf("bboxvars store the bounding box of the SWF file in actionscript variables\n");
1829 printf("reordertags=0/1 (default: 1) perform some tag optimizations\n");
1830 printf("internallinkfunction=<name> when the user clicks a internal link (to a different page) in the converted file, this actionscript function is called\n");
1831 printf("externallinkfunction=<name> when the user clicks an external link (e.g. http://www.foo.bar/) on the converted file, this actionscript function is called\n");
1832 printf("disable_polygon_conversion never convert strokes to polygons (will remove capstyles and joint styles)\n");
1833 printf("caplinewidth=<width> the minimum thichness a line needs to have so that capstyles become visible (and are converted)\n");
1834 printf("insertstop put an ActionScript \"STOP\" tag in every frame\n");
1835 printf("protect add a \"protect\" tag to the file, to prevent loading in the Flash editor\n");
1836 printf("flashversion=<version> the SWF fileversion (6)\n");
1837 printf("minlinewidth=<width> convert horizontal/vertical boxes smaller than this width to lines (0.05) \n");
1838 printf("animate insert a showframe tag after each placeobject (animate draw order of PDF files)\n");
1839 printf("jpegquality=<quality> set compression quality of jpeg images\n");
1846 // --------------------------------------------------------------------
1848 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1851 swf_GetCXForm(0, &cx, 1);
1854 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1855 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1856 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1857 c->ar!=0 || c->ag!=0 || c->ab!=0)
1858 msg("<warning> CXForm not SWF-compatible");
1860 cx.a0 = (S16)(c->aa*256);
1861 cx.r0 = (S16)(c->rr*256);
1862 cx.g0 = (S16)(c->gg*256);
1863 cx.b0 = (S16)(c->bb*256);
1872 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1876 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1880 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1882 gfxdevice_t*dev = i->dev;
1884 RGBA*mem = (RGBA*)img->data;
1886 int sizex = img->width;
1887 int sizey = img->height;
1888 int is_jpeg = i->jpeg;
1891 int newsizex=sizex, newsizey=sizey;
1894 if(is_jpeg && i->config_jpegsubpixels) {
1895 newsizex = (int)(targetwidth*i->config_jpegsubpixels + 0.5);
1896 newsizey = (int)(targetheight*i->config_jpegsubpixels + 0.5);
1897 } else if(!is_jpeg && i->config_ppmsubpixels) {
1898 newsizex = (int)(targetwidth*i->config_ppmsubpixels + 0.5);
1899 newsizey = (int)(targetheight*i->config_ppmsubpixels + 0.5);
1903 if(sizex<=0 || sizey<=0)
1910 /* TODO: cache images */
1912 if(newsizex<sizex || newsizey<sizey) {
1913 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1914 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1915 *newwidth = sizex = newsizex;
1916 *newheight = sizey = newsizey;
1919 *newwidth = newsizex = sizex;
1920 *newheight = newsizey = sizey;
1923 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1924 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1926 msg("<verbose> Drawing %dx%d %s%simage (id %d) at size %dx%d (%dx%d), %s%d colors",
1928 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1929 is_jpeg?"jpeg-":"", i->currentswfid+1,
1931 targetwidth, targetheight,
1932 /*newsizex, newsizey,*/
1933 num_colors>256?">":"", num_colors>256?256:num_colors);
1935 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1936 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1938 for(t=0;t<num_colors;t++) {
1939 printf("%02x%02x%02x%02x ",
1940 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1947 int cacheid = imageInCache(dev, mem, sizex, sizey);
1950 bitid = getNewID(dev);
1951 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1952 addImageToCache(dev, mem, sizex, sizey);
1962 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1964 gfxbbox_t bbox = gfxline_getbbox(line);
1966 r.xmin = (int)(bbox.xmin*20);
1967 r.ymin = (int)(bbox.ymin*20);
1968 r.xmax = (int)(bbox.xmax*20);
1969 r.ymax = (int)(bbox.ymax*20);
1973 int line_is_empty(gfxline_t*line)
1976 if(line->type != gfx_moveTo)
1983 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1985 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1987 if(line_is_empty(line))
1993 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1994 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1996 int newwidth=0,newheight=0;
1997 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2000 double fx = (double)img->width / (double)newwidth;
2001 double fy = (double)img->height / (double)newheight;
2004 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2005 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2006 m.tx = (int)(matrix->tx*20);
2007 m.ty = (int)(matrix->ty*20);
2010 int myshapeid = getNewID(dev);
2011 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2013 swf_ShapeNew(&shape);
2014 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2015 swf_SetU16(i->tag, myshapeid);
2016 SRECT r = gfxline_getSWFbbox(line);
2017 swf_SetRect(i->tag,&r);
2018 swf_SetShapeStyles(i->tag,shape);
2019 swf_ShapeCountBits(shape,NULL,NULL);
2020 swf_SetShapeBits(i->tag,shape);
2021 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2022 i->swflastx = i->swflasty = UNDEFINED_COORD;
2023 drawgfxline(dev, line);
2024 swf_ShapeSetEnd(i->tag);
2025 swf_ShapeFree(shape);
2027 msg("<trace> Placing bitmap ID %d", myshapeid);
2028 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2029 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2030 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2033 static RGBA col_black = {255,0,0,0};
2035 static void drawoutline(gfxdevice_t*dev, gfxline_t*line)
2037 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2039 int myshapeid = getNewID(dev);
2040 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2043 swf_ShapeNew(&shape);
2044 int lsid = swf_ShapeAddLineStyle(shape,1,&col_black);
2046 swf_SetU16(i->tag,myshapeid);
2047 SRECT r = gfxline_getSWFbbox(line);
2048 swf_SetRect(i->tag,&r);
2049 swf_SetShapeStyles(i->tag,shape);
2050 swf_ShapeCountBits(shape,NULL,NULL);
2051 swf_SetShapeBits(i->tag,shape);
2052 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,lsid,0,0);
2053 drawgfxline(dev, line);
2054 swf_ShapeSetEnd(i->tag);
2055 swf_ShapeFree(shape);
2057 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2058 swf_ObjectPlace(i->tag, myshapeid, getNewDepth(dev), 0,0,0);
2061 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2063 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2068 if(i->clippos >= 127)
2070 msg("<warning> Too many clip levels.");
2074 if(i->config_showclipshapes)
2075 drawoutline(dev, line);
2077 int myshapeid = getNewID(dev);
2078 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2080 memset(&col, 0, sizeof(RGBA));
2083 swf_ShapeNew(&shape);
2084 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2086 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2087 swf_ShapeAddSolidFillStyle(shape,&markcol);
2089 swf_SetU16(i->tag,myshapeid);
2090 SRECT r = gfxline_getSWFbbox(line);
2091 swf_SetRect(i->tag,&r);
2092 swf_SetShapeStyles(i->tag,shape);
2093 swf_ShapeCountBits(shape,NULL,NULL);
2094 swf_SetShapeBits(i->tag,shape);
2095 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2096 i->swflastx = i->swflasty = UNDEFINED_COORD;
2097 i->shapeisempty = 1;
2098 drawgfxline(dev, line);
2099 if(i->shapeisempty) {
2100 /* an empty clip shape is equivalent to a shape with no area */
2101 int x = line?line->x:0;
2102 int y = line?line->y:0;
2103 moveto(dev, i->tag, x,y);
2104 lineto(dev, i->tag, x,y);
2105 lineto(dev, i->tag, x,y);
2107 swf_ShapeSetEnd(i->tag);
2108 swf_ShapeFree(shape);
2110 /* TODO: remember the bbox, and check all shapes against it */
2112 msg("<trace> Placing clip ID %d", myshapeid);
2113 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2114 i->cliptags[i->clippos] = i->tag;
2115 i->clipshapes[i->clippos] = myshapeid;
2116 i->clipdepths[i->clippos] = getNewDepth(dev);
2120 static void swf_endclip(gfxdevice_t*dev)
2122 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2129 msg("<error> Invalid end of clipping region");
2133 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2134 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2136 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2138 static int gfxline_type(gfxline_t*line)
2144 int haszerosegments=0;
2147 if(line->type == gfx_moveTo) {
2150 } else if(line->type == gfx_lineTo) {
2154 } else if(line->type == gfx_splineTo) {
2156 if(tmpsplines>lines)
2164 if(lines==0 && splines==0) return 0;
2165 else if(lines==1 && splines==0) return 1;
2166 else if(lines==0 && splines==1) return 2;
2167 else if(splines==0) return 3;
2171 static int gfxline_has_dots(gfxline_t*line)
2179 if(line->type == gfx_moveTo) {
2180 /* test the length of the preceding line, and assume it is a dot if
2181 it's length is less than 1.0. But *only* if there's a noticable
2182 gap between the previous line and the next moveTo. (I've come
2183 across a PDF where thousands of "dots" were stringed together,
2185 int last_short_gap = short_gap;
2186 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2191 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2196 } else if(line->type == gfx_lineTo) {
2197 dist += fabs(line->x - x) + fabs(line->y - y);
2199 } else if(line->type == gfx_splineTo) {
2200 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2201 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2208 if(isline && dist < 1 && !short_gap) {
2214 static int gfxline_fix_short_edges(gfxline_t*line)
2218 if(line->type == gfx_lineTo) {
2219 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2222 } else if(line->type == gfx_splineTo) {
2223 if(fabs(line->sx - x) + fabs(line->sy - y) +
2224 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2235 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2237 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2238 if(x<i->min_x || x>i->max_x) return 0;
2239 if(y<i->min_y || y>i->max_y) return 0;
2243 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2245 gfxline_t*l = line = gfxline_clone(line);
2257 //#define NORMALIZE_POLYGON_POSITIONS
2259 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)
2261 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2262 if(line_is_empty(line))
2264 int type = gfxline_type(line);
2265 int has_dots = gfxline_has_dots(line);
2266 gfxbbox_t r = gfxline_getbbox(line);
2267 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2269 /* TODO: * split line into segments, and perform this check for all segments */
2271 if(i->config_disable_polygon_conversion || type>=5 ||
2273 (width <= i->config_caplinewidth
2274 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2275 || (cap_style == gfx_capRound && type<=2)))) {} else
2277 /* convert line to polygon */
2278 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2280 gfxline_fix_short_edges(line);
2281 /* we need to convert the line into a polygon */
2282 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2283 gfxline_t*gfxline = SVPtogfxline(svp);
2284 dev->fill(dev, gfxline, color);
2290 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2293 if(i->config_normalize_polygon_positions) {
2295 double startx = 0, starty = 0;
2296 if(line && line->type == gfx_moveTo) {
2300 line = gfxline_move(line, -startx, -starty);
2301 i->shapeposx = (int)(startx*20);
2302 i->shapeposy = (int)(starty*20);
2305 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2306 swfoutput_setlinewidth(dev, width);
2309 drawgfxline(dev, line);
2311 if(i->config_normalize_polygon_positions) {
2312 free(line); //account for _move
2316 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2318 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2319 if(line_is_empty(line))
2323 gfxbbox_t r = gfxline_getbbox(line);
2324 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2326 //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a) {
2329 if(!i->config_ignoredraworder)
2332 if(i->config_normalize_polygon_positions) {
2334 double startx = 0, starty = 0;
2335 if(line && line->type == gfx_moveTo) {
2339 line = gfxline_move(line, -startx, -starty);
2340 i->shapeposx = (int)(startx*20);
2341 i->shapeposy = (int)(starty*20);
2344 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2348 drawgfxline(dev, line);
2349 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2351 if(i->config_normalize_polygon_positions) {
2352 free(line); //account for _move
2355 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2357 if(line_is_empty(line))
2359 msg("<error> Gradient filling not implemented yet");
2362 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
2364 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2366 SRECT bounds = {0,0,0,0};
2368 swffont->version = 2;
2369 swffont->name = (U8*)strdup(id);
2370 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2371 swffont->layout->ascent = 0; /* ? */
2372 swffont->layout->descent = 0;
2373 swffont->layout->leading = 0;
2374 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2375 swffont->encoding = FONT_ENCODING_UNICODE;
2376 swffont->numchars = font->num_glyphs;
2377 swffont->maxascii = font->max_unicode;
2378 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2379 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2380 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2381 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2382 for(t=0;t<font->max_unicode;t++) {
2383 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2385 for(t=0;t<font->num_glyphs;t++) {
2389 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2390 if(swffont->glyph2ascii[t] == 0xffff || swffont->glyph2ascii[t] == 0x0000) {
2391 /* flash 8 flashtype requires unique unicode IDs for each character.
2392 We use the Unicode private user area to assign characters, hoping that
2393 the font doesn't contain more than 2048 glyphs */
2394 swffont->glyph2ascii[t] = 0xe000 + (t&0x1fff);
2397 if(font->glyphs[t].name) {
2398 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2400 swffont->glyphnames[t] = 0;
2402 advance = (int)(font->glyphs[t].advance);
2404 swf_Shape01DrawerInit(&draw, 0);
2405 line = font->glyphs[t].line;
2408 c.x = line->sx * GLYPH_SCALE; c.y = line->sy * GLYPH_SCALE;
2409 to.x = line->x * GLYPH_SCALE; to.y = line->y * GLYPH_SCALE;
2410 if(line->type == gfx_moveTo) {
2411 draw.moveTo(&draw, &to);
2412 } else if(line->type == gfx_lineTo) {
2413 draw.lineTo(&draw, &to);
2414 } else if(line->type == gfx_splineTo) {
2415 draw.splineTo(&draw, &c, &to);
2420 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2421 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2423 int xmax = swffont->layout->bounds[t].xmax / 20;
2424 if(xmax>0 && xmax*2 < advance) {
2425 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", xmax, advance, font->glyphs[t].advance);
2429 if(advance<32768/20) {
2430 swffont->glyph[t].advance = advance*20;
2432 swffont->glyph[t].advance = 32767;
2435 draw.dealloc(&draw);
2437 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2439 if(bounds.ymin < 0 && bounds.ymax > 0) {
2440 swffont->layout->ascent = -bounds.ymin;
2441 swffont->layout->descent = bounds.ymax;
2442 swffont->layout->leading = bounds.ymax - bounds.ymin;
2444 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2445 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2446 swffont->layout->leading = bounds.ymax - bounds.ymin;
2448 swffont->layout->descent= (bounds.ymax - bounds.ymin);
2449 swffont->layout->ascent = 0;
2450 swffont->layout->leading = bounds.ymax - bounds.ymin;
2455 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2457 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2459 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2460 return; // the requested font is the current font
2462 fontlist_t*last=0,*l = i->fontlist;
2465 if(!strcmp((char*)l->swffont->name, font->id)) {
2466 return; // we already know this font
2470 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2471 l->swffont = gfxfont_to_swffont(font, font->id);
2478 swf_FontSetID(l->swffont, getNewID(i->dev));
2480 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2482 // print font information
2483 msg("<debug> Font %s",font->id);
2484 msg("<debug> | ID: %d", l->swffont->id);
2485 msg("<debug> | Version: %d", l->swffont->version);
2486 msg("<debug> | Name: %s", l->swffont->name);
2487 msg("<debug> | Numchars: %d", l->swffont->numchars);
2488 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2489 msg("<debug> | Style: %d", l->swffont->style);
2490 msg("<debug> | Encoding: %d", l->swffont->encoding);
2491 for(iii=0; iii<l->swffont->numchars;iii++) {
2492 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,
2493 l->swffont->layout->bounds[iii].xmin/20.0,
2494 l->swffont->layout->bounds[iii].ymin/20.0,
2495 l->swffont->layout->bounds[iii].xmax/20.0,
2496 l->swffont->layout->bounds[iii].ymax/20.0
2499 for(t=0;t<l->swffont->maxascii;t++) {
2500 if(l->swffont->ascii2glyph[t] == iii)
2501 msg("<debug> | - maps to %d",t);
2507 static void swf_switchfont(gfxdevice_t*dev, const char*fontid)
2509 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2511 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2512 return; // the requested font is the current font
2514 fontlist_t*l = i->fontlist;
2516 if(!strcmp((char*)l->swffont->name, fontid)) {
2517 i->swffont = l->swffont;
2522 msg("<error> Unknown font id: %s", fontid);
2526 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2528 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2530 msg("<error> swf_drawchar called (glyph %d) without font", glyph);
2533 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2535 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2536 with multiple fonts */
2538 swf_switchfont(dev, font->id); // set the current font
2541 msg("<warning> Font is NULL");
2544 if(glyph<0 || glyph>=i->swffont->numchars) {
2545 msg("<warning> No character %d in font %s (%d chars)", glyph, FIXNULL((char*)i->swffont->name), i->swffont->numchars);
2549 setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11, matrix->tx, matrix->ty, 0);
2551 double det = i->fontmatrix.sx/65536.0 * i->fontmatrix.sy/65536.0 -
2552 i->fontmatrix.r0/65536.0 * i->fontmatrix.r1/65536.0;
2553 if(fabs(det) < 0.0005) {
2554 /* x direction equals y direction- the text is invisible */
2555 msg("<verbose> Not drawing invisible character character %d (det=%f, m=[%f %f;%f %f]\n", glyph,
2556 i->fontmatrix.sx/65536.0, i->fontmatrix.r1/65536.0,
2557 i->fontmatrix.r0/65536.0, i->fontmatrix.sy/65536.0);
2561 /*if(i->swffont->glyph[glyph].shape->bitlen <= 16) {
2562 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
2563 glyph, FIXNULL((char*)i->swffont->name), i->swffont->numchars);
2567 /* calculate character position with respect to the current font matrix */
2568 double s = 20 * GLYPH_SCALE / det;
2569 double px = matrix->tx - i->fontmatrix.tx/20.0;
2570 double py = matrix->ty - i->fontmatrix.ty/20.0;
2571 int x = (SCOORD)(( px * i->fontmatrix.sy/65536.0 - py * i->fontmatrix.r1/65536.0)*s);
2572 int y = (SCOORD)((- px * i->fontmatrix.r0/65536.0 + py * i->fontmatrix.sx/65536.0)*s);
2573 if(x>32767 || x<-32768 || y>32767 || y<-32768) {
2574 msg("<verbose> Moving character origin to %f %f\n", matrix->tx, matrix->ty);
2576 setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11, matrix->tx, matrix->ty, 1);
2584 msg("<trace> Drawing char %d in font %d at %d,%d in color %02x%02x%02x%02x",
2585 glyph, i->swffont->id, x, y, color->r, color->g, color->b, color->a);
2587 putcharacter(dev, i->swffont->id, glyph, x, y, i->current_font_size, *(RGBA*)color);
2588 swf_FontUseGlyph(i->swffont, glyph);