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"
39 #define CHARDATAMAX 8192
43 typedef struct _chardata {
45 int fontid; /* TODO: use a SWFFONT instead */
52 typedef struct _fontlist
55 struct _fontlist*next;
58 typedef long int twip;
60 typedef struct _swfmatrix {
61 double m11,m12,m21,m22,m31,m32;
64 typedef struct _swfoutput_internal
66 gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
68 double config_dumpfonts;
69 double config_ppmsubpixels;
70 double config_jpegsubpixels;
71 int config_opennewwindow;
72 int config_ignoredraworder;
73 int config_drawonlyshapes;
74 int config_jpegquality;
75 int config_storeallcharacters;
76 int config_generate_fake_tags;
77 int config_enablezlib;
78 int config_insertstoptag;
79 int config_flashversion;
80 int config_splinemaxerror;
81 int config_fontsplinemaxerror;
82 int config_filloverlap;
85 int config_disable_polygon_conversion;
86 RGBA config_linkcolor;
87 float config_minlinewidth;
88 double config_caplinewidth;
89 char* config_linktarget;
90 char*config_internallinkfunction;
129 int pic_height[1024];
135 char fillstylechanged;
137 int jpeg; //next image type
142 chardata_t chardata[CHARDATAMAX];
150 double fontm11,fontm12,fontm21,fontm22;
161 } swfoutput_internal;
163 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
164 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
165 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);
166 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
167 static void swf_endclip(gfxdevice_t*dev);
168 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);
169 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
170 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
171 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
172 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
173 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font);
174 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
175 static void swf_startframe(gfxdevice_t*dev, int width, int height);
176 static void swf_endframe(gfxdevice_t*dev);
177 static gfxresult_t* swf_finish(gfxdevice_t*driver);
179 static swfoutput_internal* init_internal_struct()
181 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
182 memset(i, 0, sizeof(swfoutput_internal));
206 i->fillstylechanged = 0;
213 i->config_dumpfonts=0;
214 i->config_ppmsubpixels=0;
215 i->config_jpegsubpixels=0;
216 i->config_opennewwindow=1;
217 i->config_ignoredraworder=0;
218 i->config_drawonlyshapes=0;
219 i->config_jpegquality=85;
220 i->config_storeallcharacters=0;
221 i->config_enablezlib=0;
222 i->config_generate_fake_tags=0;
223 i->config_insertstoptag=0;
224 i->config_flashversion=6;
225 i->config_splinemaxerror=1;
226 i->config_fontsplinemaxerror=1;
227 i->config_filloverlap=0;
229 i->config_bboxvars=0;
230 i->config_minlinewidth=0.05;
231 i->config_caplinewidth=1;
232 i->config_linktarget=0;
233 i->config_internallinkfunction=0;
235 i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
236 i->config_linkcolor.a = 0x40;
241 static int id_error = 0;
243 static U16 getNewID(gfxdevice_t* dev)
245 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
246 if(i->currentswfid == 65535) {
248 msg("<error> ID Table overflow");
253 return ++i->currentswfid;
255 static U16 getNewDepth(gfxdevice_t* dev)
257 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
258 if(i->depth == 65535) {
260 msg("<error> Depth Table overflow");
268 static void startshape(gfxdevice_t* dev);
269 static void starttext(gfxdevice_t* dev);
270 static void endshape(gfxdevice_t* dev);
271 static void endtext(gfxdevice_t* dev);
273 typedef struct _plotxy
278 // write a move-to command into the swf
279 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
281 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
282 int rx = (int)(p0.x*20);
283 int ry = (int)(p0.y*20);
284 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
285 swf_ShapeSetMove (tag, i->shape, rx,ry);
286 i->fillstylechanged = 0;
293 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
295 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
299 return movetoxy(dev, tag, p);
301 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
303 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
309 swf_ExpandRect(&i->bboxrect, p);
311 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
315 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
317 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
318 int width = i->linewidth/4;
322 //swf_ShapeSetLine(tag, i->shape,-width,-width);
323 //swf_ShapeSetLine(tag, i->shape,width*2,0);
324 //swf_ShapeSetLine(tag, i->shape,0,width*2);
325 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
326 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
327 //swf_ShapeSetLine(tag, i->shape,width,width);
330 swf_ShapeSetLine(tag, i->shape,-width,0);
331 swf_ShapeSetLine(tag, i->shape,width,-width);
332 swf_ShapeSetLine(tag, i->shape,width,width);
333 swf_ShapeSetLine(tag, i->shape,-width,width);
334 swf_ShapeSetLine(tag, i->shape,-width,-width);
335 swf_ShapeSetLine(tag, i->shape,width,0);
337 addPointToBBox(dev, x-width ,y-width);
338 addPointToBBox(dev, x+width ,y+width);
341 // write a line-to command into the swf
342 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
344 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
345 int px = (int)(p0.x*20);
346 int py = (int)(p0.y*20);
347 int rx = (px-i->swflastx);
348 int ry = (py-i->swflasty);
350 swf_ShapeSetLine (tag, i->shape, rx,ry);
351 addPointToBBox(dev, i->swflastx,i->swflasty);
352 addPointToBBox(dev, px,py);
353 }/* else if(!i->fill) {
354 // treat lines of length 0 as plots, making them
355 // at least 1 twip wide so Flash will display them
356 plot(dev, i->swflastx, i->swflasty, tag);
363 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
368 linetoxy(dev,tag, p);
371 // write a spline-to command into the swf
372 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
374 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
375 int lastlastx = i->swflastx;
376 int lastlasty = i->swflasty;
378 int cx = ((int)(control.x*20)-i->swflastx);
379 int cy = ((int)(control.y*20)-i->swflasty);
382 int ex = ((int)(end.x*20)-i->swflastx);
383 int ey = ((int)(end.y*20)-i->swflasty);
387 if((cx || cy) && (ex || ey)) {
388 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
389 addPointToBBox(dev, lastlastx ,lastlasty );
390 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
391 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
392 } else if(cx || cy || ex || ey) {
393 swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
394 addPointToBBox(dev, lastlastx ,lastlasty );
395 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
396 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
402 /* write a line, given two points and the transformation
404 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
406 moveto(dev, tag, p0);
407 lineto(dev, tag, p1);
410 void resetdrawer(gfxdevice_t*dev)
412 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
417 static void stopFill(gfxdevice_t*dev)
419 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
420 if(i->lastwasfill!=0)
422 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
423 i->fillstylechanged = 1;
427 static void startFill(gfxdevice_t*dev)
429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
430 if(i->lastwasfill!=1)
432 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
433 i->fillstylechanged = 1;
438 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
450 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
452 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
455 memset(&r, 0, sizeof(r));
458 if(debug) printf("\n");
459 for(t=0;t<i->chardatapos;t++)
461 if(i->chardata[t].fontid != font->id) {
462 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
465 SRECT b = font->layout->bounds[i->chardata[t].charid];
466 b.xmin *= i->chardata[t].size;
467 b.ymin *= i->chardata[t].size;
468 b.xmax *= i->chardata[t].size;
469 b.ymax *= i->chardata[t].size;
471 /* divide by 1024, rounding xmax/ymax up */
472 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
474 b.xmin += i->chardata[t].x;
475 b.ymin += i->chardata[t].y;
476 b.xmax += i->chardata[t].x;
477 b.ymax += i->chardata[t].y;
479 /* until we solve the INTERNAL_SCALING problem (see below)
480 make sure the bounding box is big enough */
486 b = swf_TurnRect(b, m);
488 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
489 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
490 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
491 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
492 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
497 i->chardata[t].fontid,
499 i->chardata[t].charid
501 swf_ExpandRect2(&r, &b);
503 if(debug) printf("-----> (%f,%f,%f,%f)\n",
511 static void putcharacters(gfxdevice_t*dev, TAG*tag)
513 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
517 color.r = i->chardata[0].color.r^255;
526 int charadvance[128];
529 int glyphbits=1; //TODO: can this be zero?
532 if(tag->id != ST_DEFINETEXT &&
533 tag->id != ST_DEFINETEXT2) {
534 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
537 if(!i->chardatapos) {
538 msg("<warning> putcharacters called with zero characters");
541 for(pass = 0; pass < 2; pass++)
551 advancebits++; // add sign bit
552 swf_SetU8(tag, glyphbits);
553 swf_SetU8(tag, advancebits);
556 for(t=0;t<=i->chardatapos;t++)
558 if(lastfontid != i->chardata[t].fontid ||
559 lastx!=i->chardata[t].x ||
560 lasty!=i->chardata[t].y ||
561 !colorcompare(dev,&color, &i->chardata[t].color) ||
563 lastsize != i->chardata[t].size ||
566 if(charstorepos && pass==0)
569 for(s=0;s<charstorepos;s++)
571 while(charids[s]>=(1<<glyphbits))
573 while(charadvance[s]>=(1<<advancebits))
577 if(charstorepos && pass==1)
579 tag->writeBit = 0; // Q&D
580 swf_SetBits(tag, 0, 1); // GLYPH Record
581 swf_SetBits(tag, charstorepos, 7); // number of glyphs
583 for(s=0;s<charstorepos;s++)
585 swf_SetBits(tag, charids[s], glyphbits);
586 swf_SetBits(tag, charadvance[s], advancebits);
591 if(pass == 1 && t<i->chardatapos)
597 if(lastx != i->chardata[t].x ||
598 lasty != i->chardata[t].y)
600 newx = i->chardata[t].x;
601 newy = i->chardata[t].y;
607 if(!colorcompare(dev,&color, &i->chardata[t].color))
609 color = i->chardata[t].color;
612 font.id = i->chardata[t].fontid;
613 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
616 tag->writeBit = 0; // Q&D
617 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
620 lastfontid = i->chardata[t].fontid;
621 lastx = i->chardata[t].x;
622 lasty = i->chardata[t].y;
623 lastsize = i->chardata[t].size;
626 if(t==i->chardatapos)
630 int nextt = t==i->chardatapos-1?t:t+1;
631 int rel = i->chardata[nextt].x-i->chardata[t].x;
632 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
634 lastx=i->chardata[nextt].x;
638 lastx=i->chardata[t].x;
640 charids[charstorepos] = i->chardata[t].charid;
641 charadvance[charstorepos] = advance;
648 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
650 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
651 if(i->chardatapos == CHARDATAMAX)
653 msg("<warning> Character buffer too small. SWF will be slightly bigger");
657 i->chardata[i->chardatapos].fontid = fontid;
658 i->chardata[i->chardatapos].charid = charid;
659 i->chardata[i->chardatapos].x = x;
660 i->chardata[i->chardatapos].y = y;
661 i->chardata[i->chardatapos].color = color;
662 i->chardata[i->chardatapos].size = size;
666 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
667 So if we set this value to high, the char coordinates will overflow.
668 If we set it to low, however, the char positions will be inaccurate */
669 #define FONT_INTERNAL_SIZE 4
671 /* process a character. */
672 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix_t*m, gfxcolor_t*col)
674 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
676 msg("<warning> Font is NULL");
680 if(charid<0 || charid>=swffont->numchars) {
681 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
684 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
685 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
686 charid, FIXNULL((char*)swffont->name), swffont->numchars);
697 float det = ((m->m11*m->m22)-(m->m21*m->m12));
698 if(fabs(det) < 0.0005) {
699 /* x direction equals y direction- the text is invisible */
702 det = 20*FONT_INTERNAL_SIZE / det;
705 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
706 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
708 RGBA rgba = *(RGBA*)col;
709 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
710 swf_FontUseGlyph(swffont, charid);
714 static void endtext(gfxdevice_t*dev)
716 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
720 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
721 swf_SetU16(i->tag, i->textid);
724 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
726 swf_SetRect(i->tag,&r);
728 swf_SetMatrix(i->tag,&i->fontmatrix);
730 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
732 putcharacters(dev, i->tag);
734 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
736 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
740 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
741 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
742 double m12,double m22)
748 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
749 if(i->fontm11 == m11 &&
762 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
763 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
769 static void endpage(gfxdevice_t*dev)
771 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
781 void swf_startframe(gfxdevice_t*dev, int width, int height)
783 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
784 if(!i->firstpage && !i->pagefinished)
786 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
788 swf_GetMatrix(0, &i->page_matrix);
789 i->page_matrix.tx = 0;
790 i->page_matrix.ty = 0;
796 /* set clipping/background rectangle */
797 /* TODO: this should all be done in SWFOutputDev */
798 //setBackground(dev, x1, y1, x2, y2);
800 /* increase SWF's bounding box */
806 swf_ExpandRect2(&i->swf->movieSize, &r);
808 i->lastframeno = i->frameno;
813 void swf_endframe(gfxdevice_t*dev)
815 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
820 if(i->config_insertstoptag) {
822 atag = action_Stop(atag);
823 atag = action_End(atag);
824 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
825 swf_ActionSet(i->tag,atag);
827 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
830 for(i->depth;i->depth>i->startdepth;i->depth--) {
831 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
832 swf_SetU16(i->tag,i->depth);
834 i->depth = i->startdepth;
837 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
839 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
841 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
845 int shapeid = getNewID(dev);
850 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
852 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
853 swf_SetU16(i->tag,shapeid);
854 swf_SetRect(i->tag,&r);
855 swf_SetShapeHeader(i->tag,s);
856 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
857 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
858 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
859 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
860 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
861 swf_ShapeSetEnd(i->tag);
863 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
864 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
865 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
866 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
869 /* initialize the swf writer */
870 void gfxdevice_swf_init(gfxdevice_t* dev)
872 memset(dev, 0, sizeof(gfxdevice_t));
873 dev->internal = init_internal_struct();
875 dev->startpage = swf_startframe;
876 dev->endpage = swf_endframe;
877 dev->finish = swf_finish;
878 dev->fillbitmap = swf_fillbitmap;
879 dev->setparameter = swf_setparameter;
880 dev->stroke = swf_stroke;
881 dev->startclip = swf_startclip;
882 dev->endclip = swf_endclip;
883 dev->fill = swf_fill;
884 dev->fillbitmap = swf_fillbitmap;
885 dev->fillgradient = swf_fillgradient;
886 dev->addfont = swf_addfont;
887 dev->drawchar = swf_drawchar;
888 dev->drawlink = swf_drawlink;
890 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
896 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
900 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
901 i->swf->fileVersion = i->config_flashversion;
902 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
903 i->swf->movieSize.xmin = 0;
904 i->swf->movieSize.ymin = 0;
905 i->swf->movieSize.xmax = 0;
906 i->swf->movieSize.ymax = 0;
908 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
909 i->tag = i->swf->firstTag;
910 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
911 swf_SetRGB(i->tag,&rgb);
913 i->startdepth = i->depth = 0;
915 if(i->config_protect)
916 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
919 static void startshape(gfxdevice_t*dev)
921 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
930 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
932 swf_ShapeNew(&i->shape);
933 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
934 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
936 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
937 swf_ShapeAddSolidFillStyle(i->shape,&markcol);
940 i->shapeid = getNewID(dev);
942 msg("<debug> Using shape id %d", i->shapeid);
944 swf_SetU16(i->tag,i->shapeid); // ID
946 i->bboxrectpos = i->tag->len;
950 r.xmax = 20*i->max_x;
951 r.ymax = 20*i->max_y;
952 swf_SetRect(i->tag,&r);
954 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
956 swf_SetShapeStyles(i->tag,i->shape);
957 swf_ShapeCountBits(i->shape,NULL,NULL);
958 swf_SetShapeBits(i->tag,i->shape);
960 /* TODO: do we really need this? */
961 //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
962 //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
963 i->swflastx=i->swflasty=UNDEFINED_COORD;
968 static void starttext(gfxdevice_t*dev)
970 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
974 i->textid = getNewID(dev);
976 i->swflastx=i->swflasty=0;
980 /* TODO: move to ../lib/rfxswf */
981 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
983 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
984 /* determine length of old rect */
988 swf_GetRect(tag, &old);
989 swf_ResetReadBits(tag);
990 int pos_end = tag->pos;
992 int len = tag->len - pos_end;
993 U8*data = (U8*)malloc(len);
994 memcpy(data, &tag->data[pos_end], len);
997 swf_SetRect(tag, newrect);
998 swf_SetBlock(tag, data, len);
1000 tag->pos = tag->readBit = 0;
1003 void cancelshape(gfxdevice_t*dev)
1005 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1006 /* delete old shape tag */
1008 i->tag = i->tag->prev;
1009 swf_DeleteTag(todel);
1010 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1012 i->bboxrectpos = -1;
1014 // i->currentswfid--; // this was an *exceptionally* bad idea
1017 void fixAreas(gfxdevice_t*dev)
1019 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1020 if(!i->shapeisempty && i->fill &&
1021 (i->bboxrect.xmin == i->bboxrect.xmax ||
1022 i->bboxrect.ymin == i->bboxrect.ymax) &&
1023 i->config_minlinewidth >= 0.001
1025 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1026 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1027 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1030 SRECT r = i->bboxrect;
1032 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1033 /* this thing comes down to a single dot- nothing to fix here */
1039 RGBA save_col = i->strokergb;
1040 int save_width = i->linewidth;
1042 i->strokergb = i->fillrgb;
1043 i->linewidth = (int)(i->config_minlinewidth*20);
1044 if(i->linewidth==0) i->linewidth = 1;
1049 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1050 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1052 i->strokergb = save_col;
1053 i->linewidth = save_width;
1058 static void endshape_noput(gfxdevice_t*dev)
1060 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1063 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1066 swf_ShapeFree(i->shape);
1074 static void endshape(gfxdevice_t*dev)
1076 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1082 if(i->shapeisempty ||
1084 (i->bboxrect.xmin == i->bboxrect.xmax &&
1085 i->bboxrect.ymin == i->bboxrect.ymax))
1087 // delete the shape again, we didn't do anything
1088 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1089 i->bboxrect.xmin /20.0,
1090 i->bboxrect.ymin /20.0,
1091 i->bboxrect.xmax /20.0,
1092 i->bboxrect.ymax /20.0
1098 swf_ShapeSetEnd(i->tag);
1100 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1102 msg("<trace> Placing shape id %d", i->shapeid);
1104 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1105 MATRIX m = i->page_matrix;
1106 m.tx += i->shapeposx;
1107 m.ty += i->shapeposy;
1108 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1110 swf_ShapeFree(i->shape);
1113 i->bboxrectpos = -1;
1120 void wipeSWF(SWF*swf)
1122 TAG*tag = swf->firstTag;
1124 TAG*next = tag->next;
1125 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1126 tag->id != ST_END &&
1127 tag->id != ST_DOACTION &&
1128 tag->id != ST_SHOWFRAME) {
1136 void swfoutput_finalize(gfxdevice_t*dev)
1138 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1140 if(i->tag && i->tag->id == ST_END)
1141 return; //already done
1143 if(i->config_bboxvars) {
1144 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1146 a = action_PushString(a, "xmin");
1147 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1148 a = action_SetVariable(a);
1149 a = action_PushString(a, "ymin");
1150 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1151 a = action_SetVariable(a);
1152 a = action_PushString(a, "xmax");
1153 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1154 a = action_SetVariable(a);
1155 a = action_PushString(a, "ymax");
1156 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1157 a = action_SetVariable(a);
1158 a = action_PushString(a, "width");
1159 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1160 a = action_SetVariable(a);
1161 a = action_PushString(a, "height");
1162 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1163 a = action_SetVariable(a);
1165 swf_ActionSet(tag, a);
1169 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1173 free(i->mark);i->mark = 0;
1177 fontlist_t *tmp,*iterator = i->fontlist;
1179 TAG*mtag = i->swf->firstTag;
1180 if(iterator->swffont) {
1181 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1182 if(!i->config_storeallcharacters) {
1183 msg("<debug> Reducing font %s", iterator->swffont->name);
1184 swf_FontReduce(iterator->swffont);
1186 swf_FontSetDefine2(mtag, iterator->swffont);
1189 iterator = iterator->next;
1191 i->tag = swf_InsertTag(i->tag,ST_END);
1192 TAG* tag = i->tag->prev;
1194 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1195 and the ST_END- they confuse the flash player */
1196 while(tag->id == ST_REMOVEOBJECT2) {
1197 TAG* prev = tag->prev;
1205 if(i->config_enablezlib || i->config_flashversion>=6) {
1206 i->swf->compressed = 1;
1210 int swfresult_save(gfxresult_t*gfx, char*filename)
1212 SWF*swf = (SWF*)gfx->internal;
1215 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1220 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1224 if(swf->compressed) {
1225 if FAILED(swf_WriteSWC(fi,swf))
1226 msg("<error> WriteSWC() failed.\n");
1228 if FAILED(swf_WriteSWF(fi,swf))
1229 msg("<error> WriteSWF() failed.\n");
1236 void* swfresult_get(gfxresult_t*gfx, char*name)
1238 SWF*swf = (SWF*)gfx->internal;
1239 if(!strcmp(name, "swf")) {
1240 return (void*)swf_CopySWF(swf);
1241 } else if(!strcmp(name, "xmin")) {
1242 return (void*)(swf->movieSize.xmin/20);
1243 } else if(!strcmp(name, "ymin")) {
1244 return (void*)(swf->movieSize.ymin/20);
1245 } else if(!strcmp(name, "xmax")) {
1246 return (void*)(swf->movieSize.xmax/20);
1247 } else if(!strcmp(name, "ymax")) {
1248 return (void*)(swf->movieSize.ymax/20);
1249 } else if(!strcmp(name, "width")) {
1250 return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1251 } else if(!strcmp(name, "height")) {
1252 return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1256 void swfresult_destroy(gfxresult_t*gfx)
1259 swf_FreeTags((SWF*)gfx->internal);
1260 free(gfx->internal);
1263 memset(gfx, 0, sizeof(gfxresult_t));
1267 static void swfoutput_destroy(gfxdevice_t* dev);
1269 gfxresult_t* swf_finish(gfxdevice_t* dev)
1271 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1274 if(i->config_linktarget) {
1275 free(i->config_linktarget);
1276 i->config_linktarget = 0;
1279 swfoutput_finalize(dev);
1280 SWF* swf = i->swf;i->swf = 0;
1281 swfoutput_destroy(dev);
1283 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1284 result->internal = swf;
1285 result->save = swfresult_save;
1287 result->get = swfresult_get;
1288 result->destroy = swfresult_destroy;
1292 /* Perform cleaning up */
1293 static void swfoutput_destroy(gfxdevice_t* dev)
1295 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1297 /* not initialized yet- nothing to destroy */
1301 fontlist_t *tmp,*iterator = i->fontlist;
1303 if(iterator->swffont) {
1304 swf_FontFree(iterator->swffont);iterator->swffont=0;
1307 iterator = iterator->next;
1310 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1313 memset(dev, 0, sizeof(gfxdevice_t));
1316 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1318 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1319 if(i->fillrgb.r == r &&
1320 i->fillrgb.g == g &&
1321 i->fillrgb.b == b &&
1322 i->fillrgb.a == a) return;
1332 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1334 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1335 if(i->strokergb.r == r &&
1336 i->strokergb.g == g &&
1337 i->strokergb.b == b &&
1338 i->strokergb.a == a) return;
1348 //#define ROUND_UP 19
1349 //#define ROUND_UP 10
1351 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1353 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1354 if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1358 i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1362 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1363 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1364 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1365 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1367 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1369 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1370 dev->drawlink(dev, points, url);
1373 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1375 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1377 if(!strncmp("http://pdf2swf:", url, 15)) {
1378 char*tmp = strdup(url);
1379 int l = strlen(tmp);
1382 swfoutput_namedlink(dev, tmp+15, points);
1385 } else if(!strncmp("page", url, 4)) {
1388 if(url[t]<'0' || url[t]>'9')
1391 int page = atoi(&url[4]) - 1;
1392 if(page<0) page = 0;
1393 swfoutput_linktopage(dev, page, points);
1396 swfoutput_linktourl(dev, url, points);
1399 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1402 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1408 if(!i->config_linktarget) {
1409 if(!i->config_opennewwindow)
1410 actions = action_GetUrl(0, url, "_parent");
1412 actions = action_GetUrl(0, url, "_this");
1414 actions = action_GetUrl(0, url, i->config_linktarget);
1416 actions = action_End(actions);
1418 drawlink(dev, actions, 0, points,0);
1420 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1422 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1423 ActionTAG* actions = 0;
1430 if(!i->config_internallinkfunction) {
1431 actions = action_GotoFrame(actions, page);
1432 actions = action_End(actions);
1434 actions = action_PushInt(actions, page); //parameter
1435 actions = action_PushInt(actions, 1); //number of parameters (1)
1436 actions = action_PushString(actions, i->config_internallinkfunction); //function name
1437 actions = action_CallFunction(actions);
1440 drawlink(dev, actions, 0, points,0);
1443 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1444 of the viewer objects, like subtitles, index elements etc.
1446 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1448 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1449 ActionTAG *actions1,*actions2;
1450 char*tmp = strdup(name);
1458 if(!strncmp(tmp, "call:", 5))
1460 char*x = strchr(&tmp[5], ':');
1462 actions1 = action_PushInt(0, 0); //number of parameters (0)
1463 actions1 = action_PushString(actions1, &tmp[5]); //function name
1464 actions1 = action_CallFunction(actions1);
1467 actions1 = action_PushString(0, x+1); //parameter
1468 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1469 actions1 = action_PushString(actions1, &tmp[5]); //function name
1470 actions1 = action_CallFunction(actions1);
1472 actions2 = action_End(0);
1477 actions1 = action_PushString(0, "/:subtitle");
1478 actions1 = action_PushString(actions1, name);
1479 actions1 = action_SetVariable(actions1);
1480 actions1 = action_End(actions1);
1482 actions2 = action_PushString(0, "/:subtitle");
1483 actions2 = action_PushString(actions2, "");
1484 actions2 = action_SetVariable(actions2);
1485 actions2 = action_End(actions2);
1488 drawlink(dev, actions1, actions2, points,mouseover);
1490 swf_ActionFree(actions1);
1491 swf_ActionFree(actions2);
1495 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1497 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1498 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1503 /* check whether the next segment is zero */
1504 if(line->type == gfx_moveTo) {
1505 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1506 moveto(dev, i->tag, line->x, line->y);
1507 px = lastx = line->x;
1508 py = lasty = line->y;
1510 } if(line->type == gfx_lineTo) {
1511 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1512 lineto(dev, i->tag, line->x, line->y);
1516 } else if(line->type == gfx_splineTo) {
1517 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1519 s.x = line->sx;p.x = line->x;
1520 s.y = line->sy;p.y = line->y;
1521 splineto(dev, i->tag, s, p);
1531 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1533 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1538 plotxy_t p1,p2,p3,p4;
1544 int buttonid = getNewID(dev);
1545 gfxbbox_t bbox = gfxline_getbbox(points);
1548 myshapeid = getNewID(dev);
1549 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1550 swf_ShapeNew(&i->shape);
1551 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1552 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1553 swf_SetU16(i->tag, myshapeid);
1554 r.xmin = (int)(bbox.xmin*20);
1555 r.ymin = (int)(bbox.ymin*20);
1556 r.xmax = (int)(bbox.xmax*20);
1557 r.ymax = (int)(bbox.ymax*20);
1558 swf_SetRect(i->tag,&r);
1559 swf_SetShapeStyles(i->tag,i->shape);
1560 swf_ShapeCountBits(i->shape,NULL,NULL);
1561 swf_SetShapeBits(i->tag,i->shape);
1562 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1563 i->swflastx = i->swflasty = 0;
1564 drawgfxline(dev, points);
1565 swf_ShapeSetEnd(i->tag);
1568 myshapeid2 = getNewID(dev);
1569 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1570 swf_ShapeNew(&i->shape);
1572 rgb = i->config_linkcolor;
1574 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1575 swf_SetU16(i->tag, myshapeid2);
1576 r.xmin = (int)(bbox.xmin*20);
1577 r.ymin = (int)(bbox.ymin*20);
1578 r.xmax = (int)(bbox.xmax*20);
1579 r.ymax = (int)(bbox.ymax*20);
1580 swf_SetRect(i->tag,&r);
1581 swf_SetShapeStyles(i->tag,i->shape);
1582 swf_ShapeCountBits(i->shape,NULL,NULL);
1583 swf_SetShapeBits(i->tag,i->shape);
1584 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1585 i->swflastx = i->swflasty = 0;
1586 drawgfxline(dev, points);
1587 swf_ShapeSetEnd(i->tag);
1591 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1592 swf_SetU16(i->tag,buttonid); //id
1593 swf_ButtonSetFlags(i->tag, 0); //menu=no
1594 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1595 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1596 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1597 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1598 swf_SetU8(i->tag,0);
1599 swf_ActionSet(i->tag,actions1);
1600 swf_SetU8(i->tag,0);
1604 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1605 swf_SetU16(i->tag,buttonid); //id
1606 swf_ButtonSetFlags(i->tag, 0); //menu=no
1607 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1608 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1609 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1610 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1611 swf_SetU8(i->tag,0); // end of button records
1612 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1613 swf_ActionSet(i->tag,actions1);
1615 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1616 swf_ActionSet(i->tag,actions2);
1617 swf_SetU8(i->tag,0);
1618 swf_ButtonPostProcess(i->tag, 2);
1620 swf_SetU8(i->tag,0);
1621 swf_ButtonPostProcess(i->tag, 1);
1625 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1627 if(posx!=0 || posy!=0) {
1629 p.x = (int)(posx*20);
1630 p.y = (int)(posy*20);
1631 p = swf_TurnPoint(p, &i->page_matrix);
1636 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1638 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1645 for(t=0;t<picpos;t++)
1647 if(pic_xids[t] == xid &&
1648 pic_yids[t] == yid) {
1649 width = pic_width[t];
1650 height = pic_height[t];
1654 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1655 pic_xids[picpos] = xid;
1656 pic_yids[picpos] = yid;
1657 pic_width[picpos] = width;
1658 pic_height[picpos] = height;
1661 pic[width*y+x] = buf[0];
1665 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1666 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1670 xid += x*r+x*b*3+x*g*7+x*a*11;
1671 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1673 for(t=0;t<picpos;t++)
1675 if(pic_xids[t] == xid &&
1676 pic_yids[t] == yid) {
1685 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1687 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1689 if(!strcmp(name, "jpegsubpixels")) {
1690 i->config_jpegsubpixels = atof(value);
1691 } else if(!strcmp(name, "ppmsubpixels")) {
1692 i->config_ppmsubpixels = atof(value);
1693 } else if(!strcmp(name, "subpixels")) {
1694 i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1695 } else if(!strcmp(name, "drawonlyshapes")) {
1696 i->config_drawonlyshapes = atoi(value);
1697 } else if(!strcmp(name, "ignoredraworder")) {
1698 i->config_ignoredraworder = atoi(value);
1699 } else if(!strcmp(name, "mark")) {
1700 if(!value || !value[0]) {
1701 if(i->mark) free(i->mark);
1705 i->mark = strdup("...");
1706 for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1708 } else if(!strcmp(name, "filloverlap")) {
1709 i->config_filloverlap = atoi(value);
1710 } else if(!strcmp(name, "linksopennewwindow")) {
1711 i->config_opennewwindow = atoi(value);
1712 } else if(!strcmp(name, "opennewwindow")) {
1713 i->config_opennewwindow = atoi(value);
1714 } else if(!strcmp(name, "storeallcharacters")) {
1715 i->config_storeallcharacters = atoi(value);
1716 } else if(!strcmp(name, "enablezlib")) {
1717 i->config_enablezlib = atoi(value);
1718 } else if(!strcmp(name, "bboxvars")) {
1719 i->config_bboxvars = atoi(value);
1720 } else if(!strcmp(name, "internallinkfunction")) {
1721 i->config_internallinkfunction = strdup(value);
1722 } else if(!strcmp(name, "disable_polygon_conversion")) {
1723 i->config_disable_polygon_conversion = atoi(value);
1724 } else if(!strcmp(name, "insertstop")) {
1725 i->config_insertstoptag = atoi(value);
1726 } else if(!strcmp(name, "protect")) {
1727 i->config_protect = atoi(value);
1728 if(i->config_protect && i->tag) {
1729 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1731 } else if(!strcmp(name, "faketags")) {
1732 i->config_generate_fake_tags = atoi(value);
1733 } else if(!strcmp(name, "flashversion")) {
1734 i->config_flashversion = atoi(value);
1736 i->swf->fileVersion = i->config_flashversion;
1738 } else if(!strcmp(name, "minlinewidth")) {
1739 i->config_minlinewidth = atof(value);
1740 } else if(!strcmp(name, "caplinewidth")) {
1741 i->config_caplinewidth = atof(value);
1742 } else if(!strcmp(name, "linktarget")) {
1743 i->config_linktarget = strdup(value);
1744 } else if(!strcmp(name, "dumpfonts")) {
1745 i->config_dumpfonts = atoi(value);
1746 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1748 } else if(!strcmp(name, "jpegquality")) {
1749 int val = atoi(value);
1751 if(val>100) val=100;
1752 i->config_jpegquality = val;
1753 } else if(!strcmp(name, "splinequality")) {
1754 int v = atoi(value);
1755 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1757 i->config_splinemaxerror = v;
1758 } else if(!strcmp(name, "fontquality")) {
1759 int v = atoi(value);
1760 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1762 i->config_fontsplinemaxerror = v;
1763 } else if(!strcmp(name, "linkcolor")) {
1764 if(strlen(value)!=8) {
1765 fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1768 # define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1769 i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1770 i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1771 i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1772 i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1773 printf("%02x%02x%02x%02x\n",
1774 i->config_linkcolor.r,
1775 i->config_linkcolor.g,
1776 i->config_linkcolor.b,
1777 i->config_linkcolor.a);
1780 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1786 // --------------------------------------------------------------------
1788 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1791 swf_GetCXForm(0, &cx, 1);
1794 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1795 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1796 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1797 c->ar!=0 || c->ag!=0 || c->ab!=0)
1798 msg("<warning> CXForm not SWF-compatible");
1800 cx.a0 = (S16)(c->aa*256);
1801 cx.r0 = (S16)(c->rr*256);
1802 cx.g0 = (S16)(c->gg*256);
1803 cx.b0 = (S16)(c->bb*256);
1811 static ArtVpath* gfxline_to_ArtVpath(gfxline_t*line)
1813 ArtVpath *vec = NULL;
1818 /* factor which determines into how many line fragments a spline is converted */
1819 double subfraction = 2.4;//0.3
1823 if(l2->type == gfx_moveTo) {
1825 } if(l2->type == gfx_lineTo) {
1827 } if(l2->type == gfx_splineTo) {
1828 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1829 if(!parts) parts = 1;
1839 vec = art_new (ArtVpath, len);
1844 if(l2->type == gfx_moveTo) {
1845 vec[pos].code = ART_MOVETO;
1850 } else if(l2->type == gfx_lineTo) {
1851 vec[pos].code = ART_LINETO;
1856 } else if(l2->type == gfx_splineTo) {
1858 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1859 if(!parts) parts = 1;
1860 for(i=0;i<=parts;i++) {
1861 double t = (double)i/(double)parts;
1862 vec[pos].code = ART_LINETO;
1863 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
1864 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
1873 vec[pos].code = ART_END;
1878 static ArtSVP* gfxfillToSVP(gfxline_t*line)
1880 ArtVpath* vec = gfxline_to_ArtVpath(line);
1881 ArtSVP *svp = art_svp_from_vpath(vec);
1886 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
1888 ArtVpath* vec = gfxline_to_ArtVpath(line);
1889 ArtSVP *svp = art_svp_vpath_stroke (vec,
1890 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
1891 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
1892 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
1893 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
1894 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
1895 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
1897 miterLimit, //miter_limit
1904 static gfxline_t* SVPtogfxline(ArtSVP*svp)
1909 for(t=0;t<svp->n_segs;t++) {
1910 size += svp->segs[t].n_points + 1;
1912 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
1914 for(t=0;t<svp->n_segs;t++) {
1915 ArtSVPSeg* seg = &svp->segs[t];
1917 for(p=0;p<seg->n_points;p++) {
1918 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
1919 ArtPoint* point = &seg->points[p];
1920 lines[pos].x = point->x;
1921 lines[pos].y = point->y;
1922 lines[pos].next = &lines[pos+1];
1927 lines[pos-1].next = 0;
1935 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1939 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1943 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1945 gfxdevice_t*dev = i->dev;
1947 RGBA*mem = (RGBA*)img->data;
1949 int sizex = img->width;
1950 int sizey = img->height;
1951 int is_jpeg = i->jpeg;
1954 int newsizex=sizex, newsizey=sizey;
1957 if(is_jpeg && i->config_jpegsubpixels) {
1958 newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1959 newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1960 } else if(!is_jpeg && i->config_ppmsubpixels) {
1961 newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1962 newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1966 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1969 /* TODO: cache images */
1971 if(newsizex<sizex || newsizey<sizey) {
1972 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1973 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1974 *newwidth = sizex = newsizex;
1975 *newheight = sizey = newsizey;
1978 *newwidth = newsizex = sizex;
1979 *newheight = newsizey = sizey;
1982 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1983 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1985 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
1987 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1990 targetwidth, targetheight,
1991 /*newsizex, newsizey,*/
1992 num_colors>256?">":"", num_colors>256?256:num_colors);
1994 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1995 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1997 for(t=0;t<num_colors;t++) {
1998 printf("%02x%02x%02x%02x ",
1999 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
2006 int cacheid = imageInCache(dev, mem, sizex, sizey);
2009 bitid = getNewID(dev);
2010 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
2011 addImageToCache(dev, mem, sizex, sizey);
2021 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2023 gfxbbox_t bbox = gfxline_getbbox(line);
2025 r.xmin = (int)(bbox.xmin*20);
2026 r.ymin = (int)(bbox.ymin*20);
2027 r.xmax = (int)(bbox.xmax*20);
2028 r.ymax = (int)(bbox.ymax*20);
2032 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2034 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2039 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2040 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2042 int newwidth=0,newheight=0;
2043 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2046 double fx = (double)img->width / (double)newwidth;
2047 double fy = (double)img->height / (double)newheight;
2052 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2053 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2054 m.tx = (int)(matrix->tx*20);
2055 m.ty = (int)(matrix->ty*20);
2058 int myshapeid = getNewID(dev);
2059 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2061 swf_ShapeNew(&shape);
2062 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2063 swf_SetU16(i->tag, myshapeid);
2064 SRECT r = gfxline_getSWFbbox(line);
2065 swf_SetRect(i->tag,&r);
2066 swf_SetShapeStyles(i->tag,shape);
2067 swf_ShapeCountBits(shape,NULL,NULL);
2068 swf_SetShapeBits(i->tag,shape);
2069 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2070 i->swflastx = i->swflasty = UNDEFINED_COORD;
2071 drawgfxline(dev, line);
2072 swf_ShapeSetEnd(i->tag);
2073 swf_ShapeFree(shape);
2075 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2076 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2077 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2080 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2082 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2087 if(i->clippos >= 127)
2089 msg("<warning> Too many clip levels.");
2093 int myshapeid = getNewID(dev);
2094 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2096 memset(&col, 0, sizeof(RGBA));
2098 swf_ShapeNew(&shape);
2099 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2101 RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2102 swf_ShapeAddSolidFillStyle(shape,&markcol);
2104 swf_SetU16(i->tag,myshapeid);
2105 SRECT r = gfxline_getSWFbbox(line);
2106 swf_SetRect(i->tag,&r);
2107 swf_SetShapeStyles(i->tag,shape);
2108 swf_ShapeCountBits(shape,NULL,NULL);
2109 swf_SetShapeBits(i->tag,shape);
2110 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2111 i->swflastx = i->swflasty = UNDEFINED_COORD;
2112 drawgfxline(dev, line);
2113 swf_ShapeSetEnd(i->tag);
2114 swf_ShapeFree(shape);
2116 /* TODO: remember the bbox, and check all shapes against it */
2118 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2119 i->cliptags[i->clippos] = i->tag;
2120 i->clipshapes[i->clippos] = myshapeid;
2121 i->clipdepths[i->clippos] = getNewDepth(dev);
2125 static void swf_endclip(gfxdevice_t*dev)
2127 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2134 msg("<error> Invalid end of clipping region");
2138 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2139 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2141 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2143 static int gfxline_type(gfxline_t*line)
2149 int haszerosegments=0;
2151 if(line->type == gfx_moveTo) {
2154 } else if(line->type == gfx_lineTo) {
2158 } else if(line->type == gfx_splineTo) {
2160 if(tmpsplines>lines)
2165 if(lines==0 && splines==0) return 0;
2166 else if(lines==1 && splines==0) return 1;
2167 else if(lines==0 && splines==1) return 2;
2168 else if(splines==0) return 3;
2172 static int gfxline_has_dots(gfxline_t*line)
2180 if(line->type == gfx_moveTo) {
2181 /* test the length of the preceding line, and assume it is a dot if
2182 it's length is less than 1.0. But *only* if there's a noticable
2183 gap between the previous line and the next moveTo. (I've come
2184 across a PDF where thousands of "dots" were stringed together,
2186 int last_short_gap = short_gap;
2187 if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2192 if(isline && dist < 1 && !short_gap && !last_short_gap) {
2197 } else if(line->type == gfx_lineTo) {
2198 dist += fabs(line->x - x) + fabs(line->y - y);
2200 } else if(line->type == gfx_splineTo) {
2201 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2202 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2209 if(isline && dist < 1 && !short_gap) {
2215 static int gfxline_fix_short_edges(gfxline_t*line)
2219 if(line->type == gfx_lineTo) {
2220 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2223 } else if(line->type == gfx_splineTo) {
2224 if(fabs(line->sx - x) + fabs(line->sy - y) +
2225 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2236 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2238 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2239 if(x<i->min_x || x>i->max_x) return 0;
2240 if(y<i->min_y || y>i->max_y) return 0;
2244 static void show_path(ArtSVP*path)
2247 printf("Segments: %d\n", path->n_segs);
2248 for(t=0;t<path->n_segs;t++) {
2249 ArtSVPSeg* seg = &path->segs[t];
2250 printf("Segment %d: %d points, %s, BBox: (%f,%f,%f,%f)\n",
2251 t, seg->n_points, seg->dir==0?"UP ":"DOWN",
2252 seg->bbox.x0, seg->bbox.y0, seg->bbox.x1, seg->bbox.y1);
2254 for(p=0;p<seg->n_points;p++) {
2255 ArtPoint* point = &seg->points[p];
2256 printf(" (%f,%f)\n", point->x, point->y);
2262 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2264 gfxline_t*l = line = gfxline_clone(line);
2276 //#define NORMALIZE_POLYGON_POSITIONS
2278 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)
2280 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2281 int type = gfxline_type(line);
2282 int has_dots = gfxline_has_dots(line);
2283 gfxbbox_t r = gfxline_getbbox(line);
2284 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2286 /* TODO: * split line into segments, and perform this check for all segments */
2288 if(i->config_disable_polygon_conversion ||
2290 (width <= i->config_caplinewidth
2291 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2292 || (cap_style == gfx_capRound && type<=2)))) {} else
2294 /* convert line to polygon */
2295 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2297 gfxline_fix_short_edges(line);
2298 /* we need to convert the line into a polygon */
2299 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2300 gfxline_t*gfxline = SVPtogfxline(svp);
2301 dev->fill(dev, gfxline, color);
2307 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2310 #ifdef NORMALIZE_POLYGON_POSITIONS
2312 double startx = 0, starty = 0;
2313 if(line && line->type == gfx_moveTo) {
2317 line = gfxline_move(line, -startx, -starty);
2318 i->shapeposx = (int)(startx*20);
2319 i->shapeposy = (int)(starty*20);
2322 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2323 swfoutput_setlinewidth(dev, width);
2326 drawgfxline(dev, line);
2328 #ifdef NORMALIZE_POLYGON_POSITIONS
2329 free(line); //account for _move
2333 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2335 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2336 gfxbbox_t r = gfxline_getbbox(line);
2337 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2340 if(!i->config_ignoredraworder)
2343 #ifdef NORMALIZE_POLYGON_POSITIONS
2345 double startx = 0, starty = 0;
2346 if(line && line->type == gfx_moveTo) {
2350 line = gfxline_move(line, -startx, -starty);
2351 i->shapeposx = (int)(startx*20);
2352 i->shapeposy = (int)(starty*20);
2355 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2359 drawgfxline(dev, line);
2360 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2362 #ifdef NORMALIZE_POLYGON_POSITIONS
2363 free(line); //account for _move
2366 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2368 msg("<error> Gradient filling not implemented yet");
2371 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2373 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2375 SRECT bounds = {0,0,0,0};
2377 swffont->version = 2;
2378 swffont->name = (U8*)strdup(id);
2379 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2380 swffont->layout->ascent = 0; /* ? */
2381 swffont->layout->descent = 0;
2382 swffont->layout->leading = 0;
2383 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2384 swffont->encoding = FONT_ENCODING_UNICODE;
2385 swffont->numchars = font->num_glyphs;
2386 swffont->maxascii = font->max_unicode;
2387 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2388 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2389 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2390 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2391 for(t=0;t<font->max_unicode;t++) {
2392 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2394 for(t=0;t<font->num_glyphs;t++) {
2398 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2399 if(font->glyphs[t].name) {
2400 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2402 swffont->glyphnames[t] = 0;
2404 advance = (int)(font->glyphs[t].advance);
2406 swf_Shape01DrawerInit(&draw, 0);
2407 line = font->glyphs[t].line;
2410 c.x = line->sx; c.y = line->sy;
2411 to.x = line->x; to.y = line->y;
2412 if(line->type == gfx_moveTo) {
2413 draw.moveTo(&draw, &to);
2414 } else if(line->type == gfx_lineTo) {
2415 draw.lineTo(&draw, &to);
2416 } else if(line->type == gfx_splineTo) {
2417 draw.splineTo(&draw, &c, &to);
2422 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2423 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2425 if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2426 printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2427 advance = swffont->layout->bounds[t].xmax;
2431 swffont->glyph[t].advance = advance;
2433 swffont->glyph[t].advance = 32767;
2436 draw.dealloc(&draw);
2438 swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2440 if(bounds.ymin < 0 && bounds.ymax > 0) {
2441 swffont->layout->ascent = -bounds.ymin;
2442 swffont->layout->descent = bounds.ymax;
2443 swffont->layout->leading = bounds.ymax - bounds.ymin;
2445 swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2446 swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2447 swffont->layout->leading = bounds.ymax - bounds.ymin;
2453 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2455 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2457 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2458 return; // the requested font is the current font
2460 fontlist_t*last=0,*l = i->fontlist;
2463 if(!strcmp((char*)l->swffont->name, fontid)) {
2464 return; // we already know this font
2468 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2469 l->swffont = gfxfont_to_swffont(font, fontid);
2476 swf_FontSetID(l->swffont, getNewID(i->dev));
2478 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2480 // print font information
2481 msg("<debug> Font %s",fontid);
2482 msg("<debug> | ID: %d", l->swffont->id);
2483 msg("<debug> | Version: %d", l->swffont->version);
2484 msg("<debug> | Name: %s", l->swffont->name);
2485 msg("<debug> | Numchars: %d", l->swffont->numchars);
2486 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2487 msg("<debug> | Style: %d", l->swffont->style);
2488 msg("<debug> | Encoding: %d", l->swffont->encoding);
2489 for(iii=0; iii<l->swffont->numchars;iii++) {
2490 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,
2491 l->swffont->layout->bounds[iii].xmin/20.0,
2492 l->swffont->layout->bounds[iii].ymin/20.0,
2493 l->swffont->layout->bounds[iii].xmax/20.0,
2494 l->swffont->layout->bounds[iii].ymax/20.0
2497 for(t=0;t<l->swffont->maxascii;t++) {
2498 if(l->swffont->ascii2glyph[t] == iii)
2499 msg("<debug> | - maps to %d",t);
2505 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2507 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2509 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2510 return; // the requested font is the current font
2512 fontlist_t*l = i->fontlist;
2514 if(!strcmp((char*)l->swffont->name, fontid)) {
2515 i->swffont = l->swffont;
2520 msg("<error> Unknown font id: %s", fontid);
2524 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2526 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2528 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,fontid)) // not equal to current font
2530 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2531 with multiple fonts */
2534 swf_switchfont(dev, fontid); // set the current font
2536 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2546 /* printf("%f %f\n", m.m31, m.m32);
2548 static int xpos = 40;
2549 static int ypos = 200;
2556 drawchar(dev, i->swffont, glyph, &m, color);