2 Implements generation of swf files using the rfxswf lib. The routines
3 in this file are called from pdf2swf.
5 This file is part of swftools.
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"
33 #include "swfoutput.h"
36 #include "../lib/log.h"
37 #include "../lib/rfxswf.h"
38 #include "../lib/gfxdevice.h"
39 #include "../lib/gfxtools.h"
41 #include "../lib/art/libart.h"
43 #define CHARDATAMAX 8192
47 typedef struct _chardata {
49 int fontid; /* TODO: use a SWFFONT instead */
62 typedef long int twip;
65 double m11,m12,m21,m22,m31,m32;
68 typedef struct _swfoutput_internal
70 gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
72 double config_dumpfonts;
73 double config_ppmsubpixels;
74 double config_jpegsubpixels;
75 int config_opennewwindow;
76 int config_ignoredraworder;
77 int config_drawonlyshapes;
78 int config_jpegquality;
79 int config_storeallcharacters;
80 int config_enablezlib;
81 int config_insertstoptag;
82 int config_flashversion;
83 int config_splinemaxerror;
84 int config_fontsplinemaxerror;
85 int config_filloverlap;
88 float config_minlinewidth;
89 double config_caplinewidth;
128 int pic_height[1024];
134 char fillstylechanged;
136 int jpeg; //next image type
141 chardata_t chardata[CHARDATAMAX];
149 double fontm11,fontm12,fontm21,fontm22;
156 } swfoutput_internal;
158 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
159 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
160 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);
161 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
162 static void swf_endclip(gfxdevice_t*dev);
163 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);
164 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
165 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
166 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
167 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
168 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font);
169 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
170 static gfxresult_t* swf_finish(gfxdevice_t*driver);
172 int getCharID(SWFFONT *font, int charnr, char *charname, int u);
174 static swfoutput_internal* init_internal_struct()
176 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
177 memset(i, 0, sizeof(swfoutput_internal));
199 i->fillstylechanged = 0;
206 i->config_dumpfonts=0;
207 i->config_ppmsubpixels=0;
208 i->config_jpegsubpixels=0;
209 i->config_opennewwindow=1;
210 i->config_ignoredraworder=0;
211 i->config_drawonlyshapes=0;
212 i->config_jpegquality=85;
213 i->config_storeallcharacters=0;
214 i->config_enablezlib=0;
215 i->config_insertstoptag=0;
216 i->config_flashversion=5;
217 i->config_splinemaxerror=1;
218 i->config_fontsplinemaxerror=1;
219 i->config_filloverlap=0;
221 i->config_bboxvars=0;
222 i->config_minlinewidth=0.05;
223 i->config_caplinewidth=1;
228 static int id_error = 0;
230 static U16 getNewID(gfxdevice_t* dev)
232 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
233 if(i->currentswfid == 65535) {
235 msg("<error> ID Table overflow");
239 return ++i->currentswfid;
241 static U16 getNewDepth(gfxdevice_t* dev)
243 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
244 if(i->depth == 65535) {
246 msg("<error> Depth Table overflow");
253 static void startshape(gfxdevice_t* dev);
254 static void starttext(gfxdevice_t* dev);
255 static void endshape(gfxdevice_t* dev);
256 static void endtext(gfxdevice_t* dev);
258 // matrix multiplication. changes p0
259 static void transform (plotxy*p0,struct swfmatrix*m)
262 x = m->m11*p0->x+m->m12*p0->y;
263 y = m->m21*p0->x+m->m22*p0->y;
268 // write a move-to command into the swf
269 static int moveto(gfxdevice_t*dev, TAG*tag, plotxy p0)
271 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
272 int rx = (int)(p0.x*20);
273 int ry = (int)(p0.y*20);
274 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
275 swf_ShapeSetMove (tag, i->shape, rx,ry);
276 i->fillstylechanged = 0;
283 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
285 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
289 return moveto(dev, tag, p);
291 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
293 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
299 swf_ExpandRect(&i->bboxrect, p);
301 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
305 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
307 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
308 int width = i->linewidth/4;
312 //swf_ShapeSetLine(tag, i->shape,-width,-width);
313 //swf_ShapeSetLine(tag, i->shape,width*2,0);
314 //swf_ShapeSetLine(tag, i->shape,0,width*2);
315 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
316 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
317 //swf_ShapeSetLine(tag, i->shape,width,width);
320 swf_ShapeSetLine(tag, i->shape,-width,0);
321 swf_ShapeSetLine(tag, i->shape,width,-width);
322 swf_ShapeSetLine(tag, i->shape,width,width);
323 swf_ShapeSetLine(tag, i->shape,-width,width);
324 swf_ShapeSetLine(tag, i->shape,-width,-width);
325 swf_ShapeSetLine(tag, i->shape,width,0);
327 addPointToBBox(dev, x-width ,y-width);
328 addPointToBBox(dev, x+width ,y+width);
331 // write a line-to command into the swf
332 static void lineto(gfxdevice_t*dev, TAG*tag, plotxy p0)
334 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
335 int px = (int)(p0.x*20);
336 int py = (int)(p0.y*20);
337 int rx = (px-i->swflastx);
338 int ry = (py-i->swflasty);
340 swf_ShapeSetLine (tag, i->shape, rx,ry);
341 addPointToBBox(dev, i->swflastx,i->swflasty);
342 addPointToBBox(dev, px,py);
343 }/* else if(!i->fill) {
344 // treat lines of length 0 as plots, making them
345 // at least 1 twip wide so Flash will display them
346 plot(dev, i->swflastx, i->swflasty, tag);
353 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
361 // write a spline-to command into the swf
362 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy control,plotxy end)
364 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
365 int lastlastx = i->swflastx;
366 int lastlasty = i->swflasty;
368 int cx = ((int)(control.x*20)-i->swflastx);
369 int cy = ((int)(control.y*20)-i->swflasty);
372 int ex = ((int)(end.x*20)-i->swflastx);
373 int ey = ((int)(end.y*20)-i->swflasty);
377 if(cx || cy || ex || ey) {
378 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
379 addPointToBBox(dev, lastlastx ,lastlasty );
380 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
381 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
382 }/* else if(!i->fill) {
383 // treat splines of length 0 as plots
384 plot(dev, lastlastx, lastlasty, tag);
389 /* write a line, given two points and the transformation
391 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy p0, plotxy p1)
393 moveto(dev, tag, p0);
394 lineto(dev, tag, p1);
397 void resetdrawer(gfxdevice_t*dev)
399 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
404 static void stopFill(gfxdevice_t*dev)
406 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
409 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
410 i->fillstylechanged = 1;
414 static void startFill(gfxdevice_t*dev)
416 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
419 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
420 i->fillstylechanged = 1;
425 /* draw an outline. These are generated by pdf2swf and by t1lib
426 (representing characters). */
427 /*void drawpath(gfxdevice_t*dev, SWF_OUTLINE*outline, struct swfmatrix*m, int log)
429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
430 if( i->tag->id != ST_DEFINESHAPE &&
431 i->tag->id != ST_DEFINESHAPE2 &&
432 i->tag->id != ST_DEFINESHAPE3)
434 msg("<error> internal error: drawpath needs a shape tag, not %d\n",i->tag->id);
438 double lastx=0,lasty=0;
439 double firstx=0,firsty=0;
444 x += (outline->dest.x/(float)0xffff);
445 y += (outline->dest.y/(float)0xffff);
446 if(outline->type == SWF_PATHTYPE_MOVE)
448 //if(!init && fill && dev->drawmode != DRAWMODE_EOFILL && !ignoredraworder)
449 if(i->config_filloverlap && !init && i->fill && dev->drawmode != DRAWMODE_EOFILL) {
450 // drawmode=FILL (not EOFILL) means that
451 // seperate shapes do not cancel each other out.
452 // On SWF side, we need to start a new shape for each
453 // closed polygon, because SWF only knows EOFILL.
460 if(((int)(lastx*20) != (int)(firstx*20) ||
461 (int)(lasty*20) != (int)(firsty*20)) &&
470 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
471 line(dev,i->tag, p0, p1);
477 else if(outline->type == SWF_PATHTYPE_LINE)
485 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
486 line(dev,i->tag, p0,p1);
489 msg("<error> drawpath: unknown outline type:%d\n", outline->type);
493 outline = outline->link;
495 if(((int)(lastx*20) != (int)(firstx*20) ||
496 (int)(lasty*20) != (int)(firsty*20)) &&
505 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
506 line(dev, i->tag, p0, p1);
510 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
522 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font)
524 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
527 memset(&r, 0, sizeof(r));
530 if(debug) printf("\n");
531 for(t=0;t<i->chardatapos;t++)
533 if(i->chardata[t].fontid != font->id) {
534 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
537 SRECT b = font->layout->bounds[i->chardata[t].charid];
538 b.xmin *= i->chardata[t].size;
539 b.ymin *= i->chardata[t].size;
540 b.xmax *= i->chardata[t].size;
541 b.ymax *= i->chardata[t].size;
543 /* divide by 1024, rounding xmax/ymax up */
544 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
546 b.xmin += i->chardata[t].x;
547 b.ymin += i->chardata[t].y;
548 b.xmax += i->chardata[t].x;
549 b.ymax += i->chardata[t].y;
551 /* until we solve the INTERNAL_SCALING problem (see below)
552 make sure the bounding box is big enough */
558 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
559 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
560 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
561 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
562 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
567 i->chardata[t].fontid,
569 i->chardata[t].charid
571 swf_ExpandRect2(&r, &b);
573 if(debug) printf("-----> (%f,%f,%f,%f)\n",
581 static void putcharacters(gfxdevice_t*dev, TAG*tag)
583 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
587 color.r = i->chardata[0].color.r^255;
596 int charadvance[128];
599 int glyphbits=1; //TODO: can this be zero?
602 if(tag->id != ST_DEFINETEXT &&
603 tag->id != ST_DEFINETEXT2) {
604 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
607 if(!i->chardatapos) {
608 msg("<warning> putcharacters called with zero characters");
611 for(pass = 0; pass < 2; pass++)
621 advancebits++; // add sign bit
622 swf_SetU8(tag, glyphbits);
623 swf_SetU8(tag, advancebits);
626 for(t=0;t<=i->chardatapos;t++)
628 if(lastfontid != i->chardata[t].fontid ||
629 lastx!=i->chardata[t].x ||
630 lasty!=i->chardata[t].y ||
631 !colorcompare(dev,&color, &i->chardata[t].color) ||
633 lastsize != i->chardata[t].size ||
636 if(charstorepos && pass==0)
639 for(s=0;s<charstorepos;s++)
641 while(charids[s]>=(1<<glyphbits))
643 while(charadvance[s]>=(1<<advancebits))
647 if(charstorepos && pass==1)
649 tag->writeBit = 0; // Q&D
650 swf_SetBits(tag, 0, 1); // GLYPH Record
651 swf_SetBits(tag, charstorepos, 7); // number of glyphs
653 for(s=0;s<charstorepos;s++)
655 swf_SetBits(tag, charids[s], glyphbits);
656 swf_SetBits(tag, charadvance[s], advancebits);
661 if(pass == 1 && t<i->chardatapos)
667 if(lastx != i->chardata[t].x ||
668 lasty != i->chardata[t].y)
670 newx = i->chardata[t].x;
671 newy = i->chardata[t].y;
677 if(!colorcompare(dev,&color, &i->chardata[t].color))
679 color = i->chardata[t].color;
682 font.id = i->chardata[t].fontid;
683 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
686 tag->writeBit = 0; // Q&D
687 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
690 lastfontid = i->chardata[t].fontid;
691 lastx = i->chardata[t].x;
692 lasty = i->chardata[t].y;
693 lastsize = i->chardata[t].size;
696 if(t==i->chardatapos)
700 int nextt = t==i->chardatapos-1?t:t+1;
701 int rel = i->chardata[nextt].x-i->chardata[t].x;
702 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
704 lastx=i->chardata[nextt].x;
708 lastx=i->chardata[t].x;
710 charids[charstorepos] = i->chardata[t].charid;
711 charadvance[charstorepos] = advance;
718 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
720 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
721 if(i->chardatapos == CHARDATAMAX)
723 msg("<warning> Character buffer too small. SWF will be slightly bigger");
727 i->chardata[i->chardatapos].fontid = fontid;
728 i->chardata[i->chardatapos].charid = charid;
729 i->chardata[i->chardatapos].x = x;
730 i->chardata[i->chardatapos].y = y;
731 i->chardata[i->chardatapos].color = color;
732 i->chardata[i->chardatapos].size = size;
736 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
737 So if we set this value to high, the char coordinates will overflow.
738 If we set it to low, however, the char positions will be inaccurate */
739 #define FONT_INTERNAL_SIZE 4
741 static int font_active = 0;
742 static char* font_active_filename = 0;
744 /* process a character. */
745 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m, gfxcolor_t*col)
747 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
749 msg("<warning> Font is NULL");
753 int charid = getCharID(swffont, charnr, character, u);
756 sprintf(buf, "%s.usage", font_active_filename);
757 FILE*fi = fopen(buf, "ab+");
759 fprintf(fi, "%d %d %d %s\n", charnr, u, charid, character);
762 msg("<error> Couldn't write to %s", buf);
766 msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
767 FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
770 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
771 msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty",
772 FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
784 float det = ((m->m11*m->m22)-(m->m21*m->m12));
785 if(fabs(det) < 0.0005) {
786 /* x direction equals y direction- the text is invisible */
789 det = 20*FONT_INTERNAL_SIZE / det;
792 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
793 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
795 RGBA rgba = *(RGBA*)col;
796 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
797 swf_FontUseGlyph(swffont, charid);
802 SWF_OUTLINE*outline = font->getOutline(character, charnr);
803 char* charname = character;
806 msg("<warning> Didn't find character '%s' (%d) in current charset (%s)",
807 FIXNULL(character),charnr,FIXNULL(font->getName()));
826 drawpath(tag, outline, &m2, 0);
831 static void endtext(gfxdevice_t*dev)
833 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
837 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
838 swf_SetU16(i->tag, i->textid);
841 r = getcharacterbbox(dev, i->swffont);
843 swf_SetRect(i->tag,&r);
846 swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
847 swf_SetMatrix(i->tag,&m);
849 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
851 putcharacters(dev, i->tag);
853 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
855 swf_MatrixJoin(&m2,&i->fontmatrix, &i->page_matrix);
857 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&m2,NULL,NULL);
861 int getCharID(SWFFONT *font, int charnr, char *charname, int u)
864 if(charname && font->glyphnames) {
865 for(t=0;t<font->numchars;t++) {
866 if(font->glyphnames[t] && !strcmp(font->glyphnames[t],charname)) {
867 msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
871 /* if we didn't find the character, maybe
872 we can find the capitalized version */
873 for(t=0;t<font->numchars;t++) {
874 if(font->glyphnames[t] && !strcasecmp(font->glyphnames[t],charname)) {
875 msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
881 if(u>0 && font->encoding != 255) {
882 /* try to use the unicode id */
883 if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
884 msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
885 return font->ascii2glyph[u];
889 if(font->encoding != FONT_ENCODING_UNICODE) {
890 /* the following only works if the font encoding
891 is US-ASCII based. It's needed for fonts which return broken unicode
893 if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
894 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, font->ascii2glyph[charnr]);
895 return font->ascii2glyph[charnr];
899 if(charnr>=0 && charnr<font->numchars) {
900 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
907 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
908 static void swfoutput_setfont(gfxdevice_t*dev, char*fontid, char*filename)
910 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
912 fontlist_t*last=0,*iterator;
914 msg("<error> No fontid");
918 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
921 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
922 with multiple fonts */
925 iterator = i->fontlist;
927 if(!strcmp((char*)iterator->swffont->name,fontid)) {
928 i->swffont = iterator->swffont;
932 iterator = iterator->next;
936 msg("<error> No filename given for font- internal error?");
940 swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
941 SWFFONT*swffont = swf_LoadFont(filename);
943 if(i->config_dumpfonts) {
945 font_active_filename = strdup(filename);
949 msg("<warning> Couldn't load font %s (%s)", fontid, filename);
950 swffont = swf_LoadFont(0);
953 if(swffont->glyph2ascii) {
956 /* check whether the Unicode indices look o.k.
957 If they don't, disable the unicode lookup by setting
958 the encoding to 255 */
959 for(t=0;t<swffont->numchars;t++) {
960 int c = swffont->glyph2ascii[t];
961 if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
962 // the character maps into the unicode control character range
963 // between 0001-001f. Yet it is not empty. Treat the one
964 // mapping as broken, and look how many of those we find.
969 msg("<warning> Font %s has bad unicode mapping", fontid);
970 swffont->encoding = 255;
974 if(swffont->encoding != FONT_ENCODING_UNICODE && swffont->encoding != 255) {
975 msg("<warning> Non-unicode mapping for font %s (%s)", fontid, filename);
978 swf_FontSetID(swffont, getNewID(dev));
980 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
981 // print font information
982 msg("<debug> Font %s (%s)",fontid, filename);
983 msg("<debug> | ID: %d", swffont->id);
984 msg("<debug> | Version: %d", swffont->version);
985 msg("<debug> | Name: %s", swffont->name);
986 msg("<debug> | Numchars: %d", swffont->numchars);
987 msg("<debug> | Maxascii: %d", swffont->maxascii);
988 msg("<debug> | Style: %d", swffont->style);
989 msg("<debug> | Encoding: %d", swffont->encoding);
990 for(int iii=0; iii<swffont->numchars;iii++) {
991 msg("<debug> | Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, swffont->glyphnames?swffont->glyphnames[iii]:"<nonames>", swffont->glyph2ascii[iii], swffont->glyph[iii].shape->bitlen,
992 swffont->layout->bounds[iii].xmin/20.0,
993 swffont->layout->bounds[iii].ymin/20.0,
994 swffont->layout->bounds[iii].xmax/20.0,
995 swffont->layout->bounds[iii].ymax/20.0
998 for(t=0;t<swffont->maxascii;t++) {
999 if(swffont->ascii2glyph[t] == iii)
1000 msg("<debug> | - maps to %d",t);
1005 /* set the font name to the ID we use here */
1006 if(swffont->name) free(swffont->name);
1007 swffont->name = (U8*)strdup(fontid);
1009 iterator = new fontlist_t;
1010 iterator->swffont = swffont;
1014 last->next = iterator;
1016 i->fontlist = iterator;
1018 i->swffont = swffont;
1021 static int swfoutput_queryfont(gfxdevice_t*dev, char*fontid)
1023 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1024 fontlist_t *iterator = i->fontlist;
1026 if(!strcmp((char*)iterator->swffont->name,fontid))
1028 iterator = iterator->next;
1033 /* set's the matrix which is to be applied to characters drawn by
1034 swfoutput_drawchar() */
1035 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
1036 double m12,double m22)
1042 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1043 if(i->fontm11 == m11 &&
1044 i->fontm12 == m12 &&
1045 i->fontm21 == m21 &&
1056 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
1057 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
1063 /* draws a character at x,y. */
1064 int swfoutput_drawchar(gfxdevice_t* dev,double x,double y,char*character, int charnr, int u, gfxcolor_t* color)
1066 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1074 return drawchar(dev, i->swffont, character, charnr, u, &m, color);
1077 static void endpage(gfxdevice_t*dev)
1079 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1086 i->pagefinished = 1;
1089 void swfoutput_pagefeed(gfxdevice_t*dev)
1091 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1093 if(!i->pagefinished)
1096 if(i->config_insertstoptag) {
1098 atag = action_Stop(atag);
1099 atag = action_End(atag);
1100 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
1101 swf_ActionSet(i->tag,atag);
1103 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1106 for(i->depth;i->depth>i->startdepth;i->depth--) {
1107 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
1108 swf_SetU16(i->tag,i->depth);
1110 i->depth = i->startdepth;
1113 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
1115 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1117 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1121 int shapeid = getNewID(dev);
1126 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
1128 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
1129 swf_SetU16(i->tag,shapeid);
1130 swf_SetRect(i->tag,&r);
1131 swf_SetShapeHeader(i->tag,s);
1132 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
1133 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
1134 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
1135 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
1136 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
1137 swf_ShapeSetEnd(i->tag);
1139 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1140 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
1141 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1142 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
1145 void swfoutput_newpage(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
1147 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1148 if(!i->firstpage && !i->pagefinished)
1151 swf_GetMatrix(0, &i->page_matrix);
1152 i->page_matrix.tx = 0;
1153 i->page_matrix.ty = 0;
1159 x1*=20;y1*=20;x2*=20;y2*=20;
1161 /* set clipping/background rectangle */
1162 /* TODO: this should all be done in SWFOutputDev */
1163 //setBackground(dev, x1, y1, x2, y2);
1165 /* increase SWF's bounding box */
1171 swf_ExpandRect2(&i->swf->movieSize, &r);
1173 i->lastframeno = i->frameno;
1175 i->pagefinished = 0;
1178 /* initialize the swf writer */
1179 void gfxdevice_swf_init(gfxdevice_t* dev)
1181 memset(dev, 0, sizeof(gfxdevice_t));
1182 dev->internal = init_internal_struct();
1186 dev->finish = swf_finish;
1187 dev->fillbitmap = swf_fillbitmap;
1188 dev->setparameter = swf_setparameter;
1189 dev->stroke = swf_stroke;
1190 dev->startclip = swf_startclip;
1191 dev->endclip = swf_endclip;
1192 dev->fill = swf_fill;
1193 dev->fillbitmap = swf_fillbitmap;
1194 dev->fillgradient = swf_fillgradient;
1195 dev->addfont = swf_addfont;
1196 dev->drawchar = swf_drawchar;
1197 dev->drawlink = swf_drawlink;
1199 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1205 msg("<verbose> initializing swf output for size %d*%d\n", i->max_x,i->max_y);
1209 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
1210 i->swf->fileVersion = i->config_flashversion;
1211 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
1212 i->swf->movieSize.xmin = 0;
1213 i->swf->movieSize.ymin = 0;
1214 i->swf->movieSize.xmax = 0;
1215 i->swf->movieSize.ymax = 0;
1217 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1218 i->tag = i->swf->firstTag;
1219 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1220 swf_SetRGB(i->tag,&rgb);
1222 i->startdepth = i->depth = 0;
1224 if(i->config_protect)
1225 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1228 static void startshape(gfxdevice_t*dev)
1230 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1239 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1241 swf_ShapeNew(&i->shape);
1242 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
1243 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
1245 i->shapeid = getNewID(dev);
1247 msg("<debug> Using shape id %d", i->shapeid);
1249 swf_SetU16(i->tag,i->shapeid); // ID
1251 i->bboxrectpos = i->tag->len;
1255 r.xmax = 20*i->max_x;
1256 r.ymax = 20*i->max_y;
1257 swf_SetRect(i->tag,&r);
1259 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
1261 swf_SetShapeStyles(i->tag,i->shape);
1262 swf_ShapeCountBits(i->shape,NULL,NULL);
1263 swf_SetShapeBits(i->tag,i->shape);
1265 /* TODO: do we really need this? */
1266 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1267 i->swflastx=i->swflasty=0;
1269 i->shapeisempty = 1;
1272 static void starttext(gfxdevice_t*dev)
1274 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1278 i->textid = getNewID(dev);
1280 i->swflastx=i->swflasty=0;
1284 /* TODO: move to ../lib/rfxswf */
1285 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
1287 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1288 /* determine length of old rect */
1292 swf_GetRect(tag, &old);
1293 swf_ResetReadBits(tag);
1294 int pos_end = tag->pos;
1296 int len = tag->len - pos_end;
1297 U8*data = (U8*)malloc(len);
1298 memcpy(data, &tag->data[pos_end], len);
1301 swf_SetRect(tag, newrect);
1302 swf_SetBlock(tag, data, len);
1304 tag->pos = tag->readBit = 0;
1307 void cancelshape(gfxdevice_t*dev)
1309 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1310 /* delete old shape tag */
1312 i->tag = i->tag->prev;
1313 swf_DeleteTag(todel);
1314 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1316 i->bboxrectpos = -1;
1319 void fixAreas(gfxdevice_t*dev)
1321 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1322 if(!i->shapeisempty && i->fill &&
1323 (i->bboxrect.xmin == i->bboxrect.xmax ||
1324 i->bboxrect.ymin == i->bboxrect.ymax) &&
1325 i->config_minlinewidth >= 0.001
1327 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1328 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1329 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1332 SRECT r = i->bboxrect;
1334 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1335 /* this thing comes down to a single dot- nothing to fix here */
1341 RGBA save_col = i->strokergb;
1342 int save_width = i->linewidth;
1344 i->strokergb = i->fillrgb;
1345 i->linewidth = (int)(i->config_minlinewidth*20);
1346 if(i->linewidth==0) i->linewidth = 1;
1350 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1351 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1353 i->strokergb = save_col;
1354 i->linewidth = save_width;
1359 static void endshape_noput(gfxdevice_t*dev)
1361 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1364 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1367 swf_ShapeFree(i->shape);
1373 static void endshape(gfxdevice_t*dev)
1375 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1381 if(i->shapeisempty ||
1383 (i->bboxrect.xmin == i->bboxrect.xmax &&
1384 i->bboxrect.ymin == i->bboxrect.ymax))
1386 // delete the shape again, we didn't do anything
1387 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1388 i->bboxrect.xmin /20.0,
1389 i->bboxrect.ymin /20.0,
1390 i->bboxrect.xmax /20.0,
1391 i->bboxrect.ymax /20.0
1397 swf_ShapeSetEnd(i->tag);
1399 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1401 msg("<trace> Placing shape id %d", i->shapeid);
1403 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1404 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
1406 swf_ShapeFree(i->shape);
1409 i->bboxrectpos = -1;
1414 void wipeSWF(SWF*swf)
1416 TAG*tag = swf->firstTag;
1418 TAG*next = tag->next;
1419 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1420 tag->id != ST_END &&
1421 tag->id != ST_DOACTION &&
1422 tag->id != ST_SHOWFRAME) {
1430 void swfoutput_finalize(gfxdevice_t*dev)
1432 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1434 if(i->tag && i->tag->id == ST_END)
1435 return; //already done
1437 if(i->config_bboxvars) {
1438 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1440 a = action_PushString(a, "xmin");
1441 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1442 a = action_SetVariable(a);
1443 a = action_PushString(a, "ymin");
1444 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1445 a = action_SetVariable(a);
1446 a = action_PushString(a, "xmax");
1447 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1448 a = action_SetVariable(a);
1449 a = action_PushString(a, "ymax");
1450 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1451 a = action_SetVariable(a);
1452 a = action_PushString(a, "width");
1453 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1454 a = action_SetVariable(a);
1455 a = action_PushString(a, "height");
1456 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1457 a = action_SetVariable(a);
1459 swf_ActionSet(tag, a);
1463 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1464 swfoutput_pagefeed(dev);
1467 fontlist_t *tmp,*iterator = i->fontlist;
1469 TAG*mtag = i->swf->firstTag;
1470 if(iterator->swffont) {
1471 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1472 if(!i->config_storeallcharacters)
1473 swf_FontReduce(iterator->swffont);
1474 swf_FontSetDefine2(mtag, iterator->swffont);
1477 iterator = iterator->next;
1479 i->tag = swf_InsertTag(i->tag,ST_END);
1480 TAG* tag = i->tag->prev;
1482 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1483 and the ST_END- they confuse the flash player */
1484 while(tag->id == ST_REMOVEOBJECT2) {
1485 TAG* prev = tag->prev;
1493 if(i->config_enablezlib || i->config_flashversion>=6) {
1494 i->swf->compressed = 1;
1498 int swfresult_save(gfxresult_t*gfx, char*filename)
1500 SWF*swf = (SWF*)gfx->internal;
1503 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1508 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1512 if(swf->compressed) {
1513 if FAILED(swf_WriteSWC(fi,swf))
1514 msg("<error> WriteSWC() failed.\n");
1516 if FAILED(swf_WriteSWF(fi,swf))
1517 msg("<error> WriteSWF() failed.\n");
1524 void* swfresult_get(gfxresult_t*gfx, char*name)
1526 SWF*swf = (SWF*)gfx->internal;
1527 if(!strcmp(name, "swf")) {
1528 return (void*)swf_CopySWF(swf);
1529 } else if(!strcmp(name, "xmin")) {
1530 return (void*)(swf->movieSize.xmin/20);
1531 } else if(!strcmp(name, "ymin")) {
1532 return (void*)(swf->movieSize.ymin/20);
1533 } else if(!strcmp(name, "xmax")) {
1534 return (void*)(swf->movieSize.xmax/20);
1535 } else if(!strcmp(name, "ymax")) {
1536 return (void*)(swf->movieSize.ymax/20);
1540 void swfresult_destroy(gfxresult_t*gfx)
1543 swf_FreeTags((SWF*)gfx->internal);
1544 free(gfx->internal);
1547 memset(gfx, 0, sizeof(gfxresult_t));
1551 static void swfoutput_destroy(gfxdevice_t* dev);
1553 gfxresult_t* swf_finish(gfxdevice_t* dev)
1555 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1558 swfoutput_finalize(dev);
1559 SWF* swf = i->swf;i->swf = 0;
1560 swfoutput_destroy(dev);
1562 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1563 result->internal = swf;
1564 result->save = swfresult_save;
1566 result->get = swfresult_get;
1567 result->destroy = swfresult_destroy;
1571 /* Perform cleaning up */
1572 static void swfoutput_destroy(gfxdevice_t* dev)
1574 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1576 /* not initialized yet- nothing to destroy */
1580 fontlist_t *tmp,*iterator = i->fontlist;
1582 if(iterator->swffont) {
1583 swf_FontFree(iterator->swffont);iterator->swffont=0;
1586 iterator = iterator->next;
1589 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1592 memset(dev, 0, sizeof(gfxdevice_t));
1595 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1597 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1598 if(i->fillrgb.r == r &&
1599 i->fillrgb.g == g &&
1600 i->fillrgb.b == b &&
1601 i->fillrgb.a == a) return;
1611 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1613 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1614 if(i->strokergb.r == r &&
1615 i->strokergb.g == g &&
1616 i->strokergb.b == b &&
1617 i->strokergb.a == a) return;
1627 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1629 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1630 if(i->linewidth == (U16)(_linewidth*20))
1635 i->linewidth = (U16)(_linewidth*20);
1639 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1640 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1641 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1642 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1644 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1646 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1647 dev->drawlink(dev, points, url);
1650 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1652 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1654 if(!strncmp("http://pdf2swf:", url, 15)) {
1655 char*tmp = strdup(url);
1656 int l = strlen(tmp);
1659 swfoutput_namedlink(dev, tmp+15, points);
1662 } else if(!strncmp("page", url, 4)) {
1665 if(url[t]<'0' || url[t]>'9')
1668 int page = atoi(&url[4]);
1669 swfoutput_linktopage(dev, page, points);
1672 swfoutput_linktourl(dev, url, points);
1675 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1678 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1684 if(!i->config_opennewwindow)
1685 actions = action_GetUrl(0, url, "_parent");
1687 actions = action_GetUrl(0, url, "_this");
1688 actions = action_End(actions);
1690 drawlink(dev, actions, 0, points,0);
1692 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1694 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1702 actions = action_GotoFrame(0, page);
1703 actions = action_End(actions);
1705 drawlink(dev, actions, 0, points,0);
1708 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1709 of the viewer objects, like subtitles, index elements etc.
1711 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1713 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1714 ActionTAG *actions1,*actions2;
1715 char*tmp = strdup(name);
1723 if(!strncmp(tmp, "call:", 5))
1725 char*x = strchr(&tmp[5], ':');
1727 actions1 = action_PushInt(0, 0); //number of parameters (0)
1728 actions1 = action_PushString(actions1, &tmp[5]); //function name
1729 actions1 = action_CallFunction(actions1);
1732 actions1 = action_PushString(0, x+1); //parameter
1733 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1734 actions1 = action_PushString(actions1, &tmp[5]); //function name
1735 actions1 = action_CallFunction(actions1);
1737 actions2 = action_End(0);
1742 actions1 = action_PushString(0, "/:subtitle");
1743 actions1 = action_PushString(actions1, name);
1744 actions1 = action_SetVariable(actions1);
1745 actions1 = action_End(actions1);
1747 actions2 = action_PushString(0, "/:subtitle");
1748 actions2 = action_PushString(actions2, "");
1749 actions2 = action_SetVariable(actions2);
1750 actions2 = action_End(actions2);
1753 drawlink(dev, actions1, actions2, points,mouseover);
1755 swf_ActionFree(actions1);
1756 swf_ActionFree(actions2);
1760 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1762 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1763 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1768 /* check whether the next segment is zero */
1769 if(line->type == gfx_moveTo) {
1770 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1771 moveto(dev, i->tag, line->x, line->y);
1772 px = lastx = line->x;
1773 py = lasty = line->y;
1775 } if(line->type == gfx_lineTo) {
1776 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1777 lineto(dev, i->tag, line->x, line->y);
1781 } else if(line->type == gfx_splineTo) {
1782 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1784 s.x = line->sx;p.x = line->x;
1785 s.y = line->sy;p.y = line->y;
1786 splineto(dev, i->tag, s, p);
1796 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1798 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1803 struct plotxy p1,p2,p3,p4;
1809 int buttonid = getNewID(dev);
1810 gfxbbox_t bbox = gfxline_getbbox(points);
1813 myshapeid = getNewID(dev);
1814 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1815 swf_ShapeNew(&i->shape);
1816 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1817 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1818 swf_SetU16(i->tag, myshapeid);
1819 r.xmin = (int)(bbox.xmin*20);
1820 r.ymin = (int)(bbox.ymin*20);
1821 r.xmax = (int)(bbox.xmax*20);
1822 r.ymax = (int)(bbox.ymax*20);
1823 swf_SetRect(i->tag,&r);
1824 swf_SetShapeStyles(i->tag,i->shape);
1825 swf_ShapeCountBits(i->shape,NULL,NULL);
1826 swf_SetShapeBits(i->tag,i->shape);
1827 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1828 i->swflastx = i->swflasty = 0;
1829 drawgfxline(dev, points);
1830 swf_ShapeSetEnd(i->tag);
1833 myshapeid2 = getNewID(dev);
1834 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1835 swf_ShapeNew(&i->shape);
1836 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1838 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1839 swf_SetU16(i->tag, myshapeid2);
1840 r.xmin = (int)(bbox.xmin*20);
1841 r.ymin = (int)(bbox.ymin*20);
1842 r.xmax = (int)(bbox.xmax*20);
1843 r.ymax = (int)(bbox.ymax*20);
1844 swf_SetRect(i->tag,&r);
1845 swf_SetShapeStyles(i->tag,i->shape);
1846 swf_ShapeCountBits(i->shape,NULL,NULL);
1847 swf_SetShapeBits(i->tag,i->shape);
1848 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1849 i->swflastx = i->swflasty = 0;
1850 drawgfxline(dev, points);
1851 swf_ShapeSetEnd(i->tag);
1855 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1856 swf_SetU16(i->tag,buttonid); //id
1857 swf_ButtonSetFlags(i->tag, 0); //menu=no
1858 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1859 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1860 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1861 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1862 swf_SetU8(i->tag,0);
1863 swf_ActionSet(i->tag,actions1);
1864 swf_SetU8(i->tag,0);
1868 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1869 swf_SetU16(i->tag,buttonid); //id
1870 swf_ButtonSetFlags(i->tag, 0); //menu=no
1871 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1872 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1873 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1874 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1875 swf_SetU8(i->tag,0); // end of button records
1876 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1877 swf_ActionSet(i->tag,actions1);
1879 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1880 swf_ActionSet(i->tag,actions2);
1881 swf_SetU8(i->tag,0);
1882 swf_ButtonPostProcess(i->tag, 2);
1884 swf_SetU8(i->tag,0);
1885 swf_ButtonPostProcess(i->tag, 1);
1889 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1891 if(posx!=0 || posy!=0) {
1893 p.x = (int)(posx*20);
1894 p.y = (int)(posy*20);
1895 p = swf_TurnPoint(p, &i->page_matrix);
1900 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1902 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1909 for(t=0;t<picpos;t++)
1911 if(pic_xids[t] == xid &&
1912 pic_yids[t] == yid) {
1913 width = pic_width[t];
1914 height = pic_height[t];
1918 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1919 pic_xids[picpos] = xid;
1920 pic_yids[picpos] = yid;
1921 pic_width[picpos] = width;
1922 pic_height[picpos] = height;
1925 pic[width*y+x] = buf[0];
1929 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1930 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1934 xid += x*r+x*b*3+x*g*7+x*a*11;
1935 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1937 for(t=0;t<picpos;t++)
1939 if(pic_xids[t] == xid &&
1940 pic_yids[t] == yid) {
1948 void swfoutput_drawgfxline(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*col, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
1950 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1951 dev->stroke(dev, line, width, col, cap_style, joint_style, miterLimit);
1953 void swfoutput_fillgfxline(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*col)
1955 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1956 dev->fill(dev, line, col);
1958 void swfoutput_startclip(gfxdevice_t*dev, gfxline_t*line)
1960 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1961 dev->startclip(dev, line);
1963 void swfoutput_endclip(gfxdevice_t*dev)
1965 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1968 void swfoutput_gfxaddfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
1970 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1971 dev->addfont(dev, fontid, font);
1973 void swfoutput_gfxdrawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*c, gfxmatrix_t*m)
1975 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1976 dev->drawchar(dev, fontid, glyph, c, m);
1979 void swfoutput_setparameter(gfxdevice_t*dev, char*name, char*value)
1981 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1983 if(!strcmp(name, "jpegsubpixels")) {
1984 i->config_jpegsubpixels = atof(value);
1985 } else if(!strcmp(name, "ppmsubpixels")) {
1986 i->config_ppmsubpixels = atof(value);
1987 } else if(!strcmp(name, "drawonlyshapes")) {
1988 i->config_drawonlyshapes = atoi(value);
1989 } else if(!strcmp(name, "ignoredraworder")) {
1990 i->config_ignoredraworder = atoi(value);
1991 } else if(!strcmp(name, "filloverlap")) {
1992 i->config_filloverlap = atoi(value);
1993 } else if(!strcmp(name, "linksopennewwindow")) {
1994 i->config_opennewwindow = atoi(value);
1995 } else if(!strcmp(name, "opennewwindow")) {
1996 i->config_opennewwindow = atoi(value);
1997 } else if(!strcmp(name, "storeallcharacters")) {
1998 i->config_storeallcharacters = atoi(value);
1999 } else if(!strcmp(name, "enablezlib")) {
2000 i->config_enablezlib = atoi(value);
2001 } else if(!strcmp(name, "bboxvars")) {
2002 i->config_bboxvars = atoi(value);
2003 } else if(!strcmp(name, "insertstop")) {
2004 i->config_insertstoptag = atoi(value);
2005 } else if(!strcmp(name, "protected")) {
2006 i->config_protect = atoi(value);
2007 } else if(!strcmp(name, "flashversion")) {
2008 i->config_flashversion = atoi(value);
2009 } else if(!strcmp(name, "minlinewidth")) {
2010 i->config_minlinewidth = atof(value);
2011 } else if(!strcmp(name, "caplinewidth")) {
2012 i->config_caplinewidth = atof(value);
2013 } else if(!strcmp(name, "dumpfonts")) {
2014 i->config_dumpfonts = atoi(value);
2015 } else if(!strcmp(name, "jpegquality")) {
2016 int val = atoi(value);
2018 if(val>100) val=100;
2019 i->config_jpegquality = val;
2020 } else if(!strcmp(name, "splinequality")) {
2021 int v = atoi(value);
2022 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2024 i->config_splinemaxerror = v;
2025 } else if(!strcmp(name, "fontquality")) {
2026 int v = atoi(value);
2027 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2029 i->config_fontsplinemaxerror = v;
2031 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
2035 // --------------------------------------------------------------------
2037 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
2040 swf_GetCXForm(0, &cx, 1);
2043 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
2044 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
2045 c->br!=0 || c->bg!=0 || c->ba!=0 ||
2046 c->ar!=0 || c->ag!=0 || c->ab!=0)
2047 msg("<warning> CXForm not SWF-compatible");
2049 cx.a0 = (S16)(c->aa*256);
2050 cx.r0 = (S16)(c->rr*256);
2051 cx.g0 = (S16)(c->gg*256);
2052 cx.b0 = (S16)(c->bb*256);
2060 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
2062 ArtVpath *vec = NULL;
2070 if(l2->type == gfx_moveTo) {
2072 } if(l2->type == gfx_lineTo) {
2074 } if(l2->type == gfx_splineTo) {
2075 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2076 if(!parts) parts = 1;
2086 vec = art_new (ArtVpath, len);
2091 if(l2->type == gfx_moveTo) {
2092 vec[pos].code = ART_MOVETO;
2097 } else if(l2->type == gfx_lineTo) {
2098 vec[pos].code = ART_LINETO;
2103 } else if(l2->type == gfx_splineTo) {
2105 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2106 if(!parts) parts = 1;
2107 for(i=0;i<=parts;i++) {
2108 double t = (double)i/(double)parts;
2109 vec[pos].code = ART_LINETO;
2110 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
2111 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
2120 vec[pos].code = ART_END;
2122 svp = art_svp_vpath_stroke (vec,
2123 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
2124 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
2125 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
2126 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
2127 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
2128 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
2130 miterLimit, //miter_limit
2137 static gfxline_t* SVPtogfxline(ArtSVP*svp)
2142 for(t=0;t<svp->n_segs;t++) {
2143 size += svp->segs[t].n_points + 1;
2145 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
2147 for(t=0;t<svp->n_segs;t++) {
2148 ArtSVPSeg* seg = &svp->segs[t];
2150 for(p=0;p<seg->n_points;p++) {
2151 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
2152 ArtPoint* point = &seg->points[p];
2153 lines[pos].x = point->x;
2154 lines[pos].y = point->y;
2155 lines[pos].next = &lines[pos+1];
2160 lines[pos-1].next = 0;
2168 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
2172 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
2176 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
2178 gfxdevice_t*dev = i->dev;
2180 RGBA*mem = (RGBA*)img->data;
2182 int sizex = img->width;
2183 int sizey = img->height;
2184 int is_jpeg = i->jpeg;
2187 int newsizex=sizex, newsizey=sizey;
2190 if(is_jpeg && i->config_jpegsubpixels) {
2191 newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
2192 newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
2193 } else if(!is_jpeg && i->config_ppmsubpixels) {
2194 newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
2195 newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
2199 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
2202 /* TODO: cache images */
2206 if(newsizex<sizex || newsizey<sizey) {
2207 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
2208 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
2209 *newwidth = sizex = newsizex;
2210 *newheight = sizey = newsizey;
2215 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
2216 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
2218 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
2220 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
2223 targetwidth, targetheight,
2224 /*newsizex, newsizey,*/
2225 num_colors>256?">":"", num_colors>256?256:num_colors);
2227 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
2228 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
2230 for(t=0;t<num_colors;t++) {
2231 printf("%02x%02x%02x%02x ",
2232 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
2239 int cacheid = imageInCache(dev, mem, sizex, sizey);
2242 bitid = getNewID(dev);
2243 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
2244 addImageToCache(dev, mem, sizex, sizey);
2254 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2256 gfxbbox_t bbox = gfxline_getbbox(line);
2258 r.xmin = (int)(bbox.xmin*20);
2259 r.ymin = (int)(bbox.ymin*20);
2260 r.xmax = (int)(bbox.xmax*20);
2261 r.ymax = (int)(bbox.ymax*20);
2265 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2267 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2272 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2273 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2275 int newwidth=0,newheight=0;
2276 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2279 double fx = (double)img->width / (double)newwidth;
2280 double fy = (double)img->height / (double)newheight;
2285 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2286 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2287 m.tx = (int)(matrix->tx*20);
2288 m.ty = (int)(matrix->ty*20);
2291 int myshapeid = getNewID(dev);
2292 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2294 swf_ShapeNew(&shape);
2295 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2296 swf_SetU16(i->tag, myshapeid);
2297 SRECT r = gfxline_getSWFbbox(line);
2298 swf_SetRect(i->tag,&r);
2299 swf_SetShapeStyles(i->tag,shape);
2300 swf_ShapeCountBits(shape,NULL,NULL);
2301 swf_SetShapeBits(i->tag,shape);
2302 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2303 i->swflastx = i->swflasty = UNDEFINED_COORD;
2304 drawgfxline(dev, line);
2305 swf_ShapeSetEnd(i->tag);
2306 swf_ShapeFree(shape);
2308 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2309 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2310 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
2313 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2315 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2320 if(i->clippos >= 127)
2322 msg("<warning> Too many clip levels.");
2326 int myshapeid = getNewID(dev);
2327 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2329 memset(&col, 0, sizeof(RGBA));
2331 swf_ShapeNew(&shape);
2332 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2333 swf_SetU16(i->tag,myshapeid);
2334 SRECT r = gfxline_getSWFbbox(line);
2335 swf_SetRect(i->tag,&r);
2336 swf_SetShapeStyles(i->tag,shape);
2337 swf_ShapeCountBits(shape,NULL,NULL);
2338 swf_SetShapeBits(i->tag,shape);
2339 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2340 i->swflastx = i->swflasty = UNDEFINED_COORD;
2341 drawgfxline(dev, line);
2342 swf_ShapeSetEnd(i->tag);
2343 swf_ShapeFree(shape);
2345 /* TODO: remember the bbox, and check all shapes against it */
2347 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2348 i->cliptags[i->clippos] = i->tag;
2349 i->clipshapes[i->clippos] = myshapeid;
2350 i->clipdepths[i->clippos] = getNewDepth(dev);
2354 static void swf_endclip(gfxdevice_t*dev)
2356 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2363 msg("<error> Invalid end of clipping region");
2367 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2368 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2370 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2372 static int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
2374 if(!strcmp(key, "next_bitmap_is_jpeg")) {
2375 ((swfoutput_internal*)dev->internal)->jpeg = 1;
2381 static int gfxline_type(gfxline_t*line)
2387 int haszerosegments=0;
2389 if(line->type == gfx_moveTo) {
2392 } else if(line->type == gfx_lineTo) {
2396 } else if(line->type == gfx_splineTo) {
2398 if(tmpsplines>lines)
2403 if(lines==0 && splines==0) return 0;
2404 else if(lines==1 && splines==0) return 1;
2405 else if(lines==0 && splines==1) return 2;
2406 else if(splines==0) return 3;
2410 static int gfxline_has_dots(gfxline_t*line)
2417 if(line->type == gfx_moveTo) {
2418 if(isline && dist < 1) {
2423 } else if(line->type == gfx_lineTo) {
2424 dist += fabs(line->x - x) + fabs(line->y - y);
2426 } else if(line->type == gfx_splineTo) {
2427 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2428 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2435 if(isline && dist < 1) {
2441 static int gfxline_fix_short_edges(gfxline_t*line)
2445 if(line->type == gfx_lineTo) {
2446 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2449 } else if(line->type == gfx_splineTo) {
2450 if(fabs(line->sx - x) + fabs(line->sy - y) +
2451 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2462 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)
2464 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2465 int type = gfxline_type(line);
2466 int has_dots = gfxline_has_dots(line);
2468 /* TODO: * split line into segments, and perform this check for all segments */
2470 (width <= i->config_caplinewidth
2471 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2472 || (cap_style == gfx_capRound && type<=2))) {
2473 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2475 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2476 swfoutput_setlinewidth(dev, width);
2479 drawgfxline(dev, line);
2481 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2483 gfxline_fix_short_edges(line);
2484 /* we need to convert the line into a polygon */
2485 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2486 gfxline_t*gfxline = SVPtogfxline(svp);
2487 dev->fill(dev, gfxline, color);
2492 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2494 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2496 if(!i->config_ignoredraworder)
2498 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2502 drawgfxline(dev, line);
2503 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2505 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2507 msg("<error> Gradient filling not implemented yet");
2510 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2512 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2515 swffont->version = 2;
2516 swffont->name = (U8*)strdup(id);
2517 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2518 swffont->layout->ascent = 0; /* ? */
2519 swffont->layout->descent = 0;
2520 swffont->layout->leading = 0;
2521 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2522 swffont->encoding = FONT_ENCODING_UNICODE;
2523 swffont->numchars = font->num_glyphs;
2524 swffont->maxascii = font->max_unicode;
2525 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2526 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2527 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2528 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2529 for(t=0;t<font->max_unicode;t++) {
2530 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2532 for(t=0;t<font->num_glyphs;t++) {
2535 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2536 if(font->glyphs[t].name) {
2537 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2539 swffont->glyphnames[t] = 0;
2541 swffont->glyph[t].advance = (int)(font->glyphs[t].advance * 20);
2543 swf_Shape01DrawerInit(&draw, 0);
2544 line = font->glyphs[t].line;
2547 c.x = line->sx; c.y = line->sy;
2548 to.x = line->x; to.y = line->y;
2549 if(line->type == gfx_moveTo) {
2550 draw.moveTo(&draw, &to);
2551 } else if(line->type == gfx_lineTo) {
2552 draw.lineTo(&draw, &to);
2553 } else if(line->type == gfx_splineTo) {
2554 draw.splineTo(&draw, &c, &to);
2559 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2560 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2561 draw.dealloc(&draw);
2566 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2568 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2570 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2571 return; // the requested font is the current font
2573 fontlist_t*last=0,*l = i->fontlist;
2576 if(!strcmp((char*)l->swffont->name, fontid)) {
2577 return; // we already know this font
2581 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2582 l->swffont = gfxfont_to_swffont(font, fontid);
2589 swf_FontSetID(l->swffont, getNewID(i->dev));
2591 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2592 // print font information
2593 msg("<debug> Font %s",fontid);
2594 msg("<debug> | ID: %d", l->swffont->id);
2595 msg("<debug> | Version: %d", l->swffont->version);
2596 msg("<debug> | Name: %s", l->swffont->name);
2597 msg("<debug> | Numchars: %d", l->swffont->numchars);
2598 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2599 msg("<debug> | Style: %d", l->swffont->style);
2600 msg("<debug> | Encoding: %d", l->swffont->encoding);
2601 for(int iii=0; iii<l->swffont->numchars;iii++) {
2602 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,
2603 l->swffont->layout->bounds[iii].xmin/20.0,
2604 l->swffont->layout->bounds[iii].ymin/20.0,
2605 l->swffont->layout->bounds[iii].xmax/20.0,
2606 l->swffont->layout->bounds[iii].ymax/20.0
2609 for(t=0;t<l->swffont->maxascii;t++) {
2610 if(l->swffont->ascii2glyph[t] == iii)
2611 msg("<debug> | - maps to %d",t);
2617 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2619 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2621 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2622 return; // the requested font is the current font
2624 fontlist_t*l = i->fontlist;
2626 if(!strcmp((char*)l->swffont->name, fontid)) {
2627 i->swffont = l->swffont;
2632 msg("<error> Unknown font id: %s", fontid);
2636 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2638 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2640 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,fontid)) // not equal to current font
2642 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2643 with multiple fonts */
2646 swf_switchfont(dev, fontid); // set the current font
2648 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2657 drawchar(dev, i->swffont, 0, glyph, -1, &m, color);