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;
155 } swfoutput_internal;
157 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
158 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
159 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);
160 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
161 static void swf_endclip(gfxdevice_t*dev);
162 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);
163 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
164 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
165 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
166 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
167 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font);
168 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
169 static void swf_startframe(gfxdevice_t*dev, int width, int height);
170 static void swf_endframe(gfxdevice_t*dev);
171 static gfxresult_t* swf_finish(gfxdevice_t*driver);
173 static swfoutput_internal* init_internal_struct()
175 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
176 memset(i, 0, sizeof(swfoutput_internal));
198 i->fillstylechanged = 0;
205 i->config_dumpfonts=0;
206 i->config_ppmsubpixels=0;
207 i->config_jpegsubpixels=0;
208 i->config_opennewwindow=1;
209 i->config_ignoredraworder=0;
210 i->config_drawonlyshapes=0;
211 i->config_jpegquality=85;
212 i->config_storeallcharacters=0;
213 i->config_enablezlib=0;
214 i->config_insertstoptag=0;
215 i->config_flashversion=5;
216 i->config_splinemaxerror=1;
217 i->config_fontsplinemaxerror=1;
218 i->config_filloverlap=0;
220 i->config_bboxvars=0;
221 i->config_minlinewidth=0.05;
222 i->config_caplinewidth=1;
227 static int id_error = 0;
229 static U16 getNewID(gfxdevice_t* dev)
231 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
232 if(i->currentswfid == 65535) {
234 msg("<error> ID Table overflow");
238 return ++i->currentswfid;
240 static U16 getNewDepth(gfxdevice_t* dev)
242 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
243 if(i->depth == 65535) {
245 msg("<error> Depth Table overflow");
252 static void startshape(gfxdevice_t* dev);
253 static void starttext(gfxdevice_t* dev);
254 static void endshape(gfxdevice_t* dev);
255 static void endtext(gfxdevice_t* dev);
257 // write a move-to command into the swf
258 static int moveto(gfxdevice_t*dev, TAG*tag, plotxy p0)
260 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
261 int rx = (int)(p0.x*20);
262 int ry = (int)(p0.y*20);
263 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
264 swf_ShapeSetMove (tag, i->shape, rx,ry);
265 i->fillstylechanged = 0;
272 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
274 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
278 return moveto(dev, tag, p);
280 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
282 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
288 swf_ExpandRect(&i->bboxrect, p);
290 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
294 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
296 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
297 int width = i->linewidth/4;
301 //swf_ShapeSetLine(tag, i->shape,-width,-width);
302 //swf_ShapeSetLine(tag, i->shape,width*2,0);
303 //swf_ShapeSetLine(tag, i->shape,0,width*2);
304 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
305 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
306 //swf_ShapeSetLine(tag, i->shape,width,width);
309 swf_ShapeSetLine(tag, i->shape,-width,0);
310 swf_ShapeSetLine(tag, i->shape,width,-width);
311 swf_ShapeSetLine(tag, i->shape,width,width);
312 swf_ShapeSetLine(tag, i->shape,-width,width);
313 swf_ShapeSetLine(tag, i->shape,-width,-width);
314 swf_ShapeSetLine(tag, i->shape,width,0);
316 addPointToBBox(dev, x-width ,y-width);
317 addPointToBBox(dev, x+width ,y+width);
320 // write a line-to command into the swf
321 static void lineto(gfxdevice_t*dev, TAG*tag, plotxy p0)
323 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
324 int px = (int)(p0.x*20);
325 int py = (int)(p0.y*20);
326 int rx = (px-i->swflastx);
327 int ry = (py-i->swflasty);
329 swf_ShapeSetLine (tag, i->shape, rx,ry);
330 addPointToBBox(dev, i->swflastx,i->swflasty);
331 addPointToBBox(dev, px,py);
332 }/* else if(!i->fill) {
333 // treat lines of length 0 as plots, making them
334 // at least 1 twip wide so Flash will display them
335 plot(dev, i->swflastx, i->swflasty, tag);
342 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
350 // write a spline-to command into the swf
351 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy control,plotxy end)
353 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
354 int lastlastx = i->swflastx;
355 int lastlasty = i->swflasty;
357 int cx = ((int)(control.x*20)-i->swflastx);
358 int cy = ((int)(control.y*20)-i->swflasty);
361 int ex = ((int)(end.x*20)-i->swflastx);
362 int ey = ((int)(end.y*20)-i->swflasty);
366 if(cx || cy || ex || ey) {
367 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
368 addPointToBBox(dev, lastlastx ,lastlasty );
369 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
370 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
371 }/* else if(!i->fill) {
372 // treat splines of length 0 as plots
373 plot(dev, lastlastx, lastlasty, tag);
378 /* write a line, given two points and the transformation
380 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy p0, plotxy p1)
382 moveto(dev, tag, p0);
383 lineto(dev, tag, p1);
386 void resetdrawer(gfxdevice_t*dev)
388 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
393 static void stopFill(gfxdevice_t*dev)
395 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
398 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
399 i->fillstylechanged = 1;
403 static void startFill(gfxdevice_t*dev)
405 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
408 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
409 i->fillstylechanged = 1;
414 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
426 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
428 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
431 memset(&r, 0, sizeof(r));
434 if(debug) printf("\n");
435 for(t=0;t<i->chardatapos;t++)
437 if(i->chardata[t].fontid != font->id) {
438 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
441 SRECT b = font->layout->bounds[i->chardata[t].charid];
442 b.xmin *= i->chardata[t].size;
443 b.ymin *= i->chardata[t].size;
444 b.xmax *= i->chardata[t].size;
445 b.ymax *= i->chardata[t].size;
447 /* divide by 1024, rounding xmax/ymax up */
448 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
450 b.xmin += i->chardata[t].x;
451 b.ymin += i->chardata[t].y;
452 b.xmax += i->chardata[t].x;
453 b.ymax += i->chardata[t].y;
455 /* until we solve the INTERNAL_SCALING problem (see below)
456 make sure the bounding box is big enough */
462 b = swf_TurnRect(b, m);
464 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
465 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
466 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
467 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
468 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
473 i->chardata[t].fontid,
475 i->chardata[t].charid
477 swf_ExpandRect2(&r, &b);
479 if(debug) printf("-----> (%f,%f,%f,%f)\n",
487 static void putcharacters(gfxdevice_t*dev, TAG*tag)
489 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
493 color.r = i->chardata[0].color.r^255;
502 int charadvance[128];
505 int glyphbits=1; //TODO: can this be zero?
508 if(tag->id != ST_DEFINETEXT &&
509 tag->id != ST_DEFINETEXT2) {
510 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
513 if(!i->chardatapos) {
514 msg("<warning> putcharacters called with zero characters");
517 for(pass = 0; pass < 2; pass++)
527 advancebits++; // add sign bit
528 swf_SetU8(tag, glyphbits);
529 swf_SetU8(tag, advancebits);
532 for(t=0;t<=i->chardatapos;t++)
534 if(lastfontid != i->chardata[t].fontid ||
535 lastx!=i->chardata[t].x ||
536 lasty!=i->chardata[t].y ||
537 !colorcompare(dev,&color, &i->chardata[t].color) ||
539 lastsize != i->chardata[t].size ||
542 if(charstorepos && pass==0)
545 for(s=0;s<charstorepos;s++)
547 while(charids[s]>=(1<<glyphbits))
549 while(charadvance[s]>=(1<<advancebits))
553 if(charstorepos && pass==1)
555 tag->writeBit = 0; // Q&D
556 swf_SetBits(tag, 0, 1); // GLYPH Record
557 swf_SetBits(tag, charstorepos, 7); // number of glyphs
559 for(s=0;s<charstorepos;s++)
561 swf_SetBits(tag, charids[s], glyphbits);
562 swf_SetBits(tag, charadvance[s], advancebits);
567 if(pass == 1 && t<i->chardatapos)
573 if(lastx != i->chardata[t].x ||
574 lasty != i->chardata[t].y)
576 newx = i->chardata[t].x;
577 newy = i->chardata[t].y;
583 if(!colorcompare(dev,&color, &i->chardata[t].color))
585 color = i->chardata[t].color;
588 font.id = i->chardata[t].fontid;
589 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
592 tag->writeBit = 0; // Q&D
593 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
596 lastfontid = i->chardata[t].fontid;
597 lastx = i->chardata[t].x;
598 lasty = i->chardata[t].y;
599 lastsize = i->chardata[t].size;
602 if(t==i->chardatapos)
606 int nextt = t==i->chardatapos-1?t:t+1;
607 int rel = i->chardata[nextt].x-i->chardata[t].x;
608 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
610 lastx=i->chardata[nextt].x;
614 lastx=i->chardata[t].x;
616 charids[charstorepos] = i->chardata[t].charid;
617 charadvance[charstorepos] = advance;
624 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
626 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
627 if(i->chardatapos == CHARDATAMAX)
629 msg("<warning> Character buffer too small. SWF will be slightly bigger");
633 i->chardata[i->chardatapos].fontid = fontid;
634 i->chardata[i->chardatapos].charid = charid;
635 i->chardata[i->chardatapos].x = x;
636 i->chardata[i->chardatapos].y = y;
637 i->chardata[i->chardatapos].color = color;
638 i->chardata[i->chardatapos].size = size;
642 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
643 So if we set this value to high, the char coordinates will overflow.
644 If we set it to low, however, the char positions will be inaccurate */
645 #define FONT_INTERNAL_SIZE 4
647 /* process a character. */
648 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix*m, gfxcolor_t*col)
650 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
652 msg("<warning> Font is NULL");
656 if(charid<0 || charid>=swffont->numchars) {
657 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
660 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
661 msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty",
662 FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
673 float det = ((m->m11*m->m22)-(m->m21*m->m12));
674 if(fabs(det) < 0.0005) {
675 /* x direction equals y direction- the text is invisible */
678 det = 20*FONT_INTERNAL_SIZE / det;
681 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
682 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
684 RGBA rgba = *(RGBA*)col;
685 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
686 swf_FontUseGlyph(swffont, charid);
690 static void endtext(gfxdevice_t*dev)
692 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
696 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
697 swf_SetU16(i->tag, i->textid);
700 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
702 swf_SetRect(i->tag,&r);
704 swf_SetMatrix(i->tag,&i->fontmatrix);
706 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
708 putcharacters(dev, i->tag);
710 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
712 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
716 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
717 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
718 double m12,double m22)
724 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
725 if(i->fontm11 == m11 &&
738 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
739 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
745 static void endpage(gfxdevice_t*dev)
747 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
757 void swf_startframe(gfxdevice_t*dev, int width, int height)
759 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
760 if(!i->firstpage && !i->pagefinished)
762 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
764 swf_GetMatrix(0, &i->page_matrix);
765 i->page_matrix.tx = 0;
766 i->page_matrix.ty = 0;
772 /* set clipping/background rectangle */
773 /* TODO: this should all be done in SWFOutputDev */
774 //setBackground(dev, x1, y1, x2, y2);
776 /* increase SWF's bounding box */
782 swf_ExpandRect2(&i->swf->movieSize, &r);
784 i->lastframeno = i->frameno;
789 void swf_endframe(gfxdevice_t*dev)
791 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
796 if(i->config_insertstoptag) {
798 atag = action_Stop(atag);
799 atag = action_End(atag);
800 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
801 swf_ActionSet(i->tag,atag);
803 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
806 for(i->depth;i->depth>i->startdepth;i->depth--) {
807 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
808 swf_SetU16(i->tag,i->depth);
810 i->depth = i->startdepth;
813 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
815 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
817 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
821 int shapeid = getNewID(dev);
826 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
828 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
829 swf_SetU16(i->tag,shapeid);
830 swf_SetRect(i->tag,&r);
831 swf_SetShapeHeader(i->tag,s);
832 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
833 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
834 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
835 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
836 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
837 swf_ShapeSetEnd(i->tag);
839 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
840 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
841 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
842 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
845 /* initialize the swf writer */
846 void gfxdevice_swf_init(gfxdevice_t* dev)
848 memset(dev, 0, sizeof(gfxdevice_t));
849 dev->internal = init_internal_struct();
851 dev->startpage = swf_startframe;
852 dev->endpage = swf_endframe;
853 dev->finish = swf_finish;
854 dev->fillbitmap = swf_fillbitmap;
855 dev->setparameter = swf_setparameter;
856 dev->stroke = swf_stroke;
857 dev->startclip = swf_startclip;
858 dev->endclip = swf_endclip;
859 dev->fill = swf_fill;
860 dev->fillbitmap = swf_fillbitmap;
861 dev->fillgradient = swf_fillgradient;
862 dev->addfont = swf_addfont;
863 dev->drawchar = swf_drawchar;
864 dev->drawlink = swf_drawlink;
866 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
872 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
876 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
877 i->swf->fileVersion = i->config_flashversion;
878 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
879 i->swf->movieSize.xmin = 0;
880 i->swf->movieSize.ymin = 0;
881 i->swf->movieSize.xmax = 0;
882 i->swf->movieSize.ymax = 0;
884 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
885 i->tag = i->swf->firstTag;
886 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
887 swf_SetRGB(i->tag,&rgb);
889 i->startdepth = i->depth = 0;
891 if(i->config_protect)
892 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
895 static void startshape(gfxdevice_t*dev)
897 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
906 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
908 swf_ShapeNew(&i->shape);
909 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
910 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
912 i->shapeid = getNewID(dev);
914 msg("<debug> Using shape id %d", i->shapeid);
916 swf_SetU16(i->tag,i->shapeid); // ID
918 i->bboxrectpos = i->tag->len;
922 r.xmax = 20*i->max_x;
923 r.ymax = 20*i->max_y;
924 swf_SetRect(i->tag,&r);
926 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
928 swf_SetShapeStyles(i->tag,i->shape);
929 swf_ShapeCountBits(i->shape,NULL,NULL);
930 swf_SetShapeBits(i->tag,i->shape);
932 /* TODO: do we really need this? */
933 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
934 i->swflastx=i->swflasty=0;
939 static void starttext(gfxdevice_t*dev)
941 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
945 i->textid = getNewID(dev);
947 i->swflastx=i->swflasty=0;
951 /* TODO: move to ../lib/rfxswf */
952 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
954 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
955 /* determine length of old rect */
959 swf_GetRect(tag, &old);
960 swf_ResetReadBits(tag);
961 int pos_end = tag->pos;
963 int len = tag->len - pos_end;
964 U8*data = (U8*)malloc(len);
965 memcpy(data, &tag->data[pos_end], len);
968 swf_SetRect(tag, newrect);
969 swf_SetBlock(tag, data, len);
971 tag->pos = tag->readBit = 0;
974 void cancelshape(gfxdevice_t*dev)
976 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
977 /* delete old shape tag */
979 i->tag = i->tag->prev;
980 swf_DeleteTag(todel);
981 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
986 void fixAreas(gfxdevice_t*dev)
988 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
989 if(!i->shapeisempty && i->fill &&
990 (i->bboxrect.xmin == i->bboxrect.xmax ||
991 i->bboxrect.ymin == i->bboxrect.ymax) &&
992 i->config_minlinewidth >= 0.001
994 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
995 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
996 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
999 SRECT r = i->bboxrect;
1001 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1002 /* this thing comes down to a single dot- nothing to fix here */
1008 RGBA save_col = i->strokergb;
1009 int save_width = i->linewidth;
1011 i->strokergb = i->fillrgb;
1012 i->linewidth = (int)(i->config_minlinewidth*20);
1013 if(i->linewidth==0) i->linewidth = 1;
1017 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1018 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1020 i->strokergb = save_col;
1021 i->linewidth = save_width;
1026 static void endshape_noput(gfxdevice_t*dev)
1028 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1031 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1034 swf_ShapeFree(i->shape);
1040 static void endshape(gfxdevice_t*dev)
1042 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1048 if(i->shapeisempty ||
1050 (i->bboxrect.xmin == i->bboxrect.xmax &&
1051 i->bboxrect.ymin == i->bboxrect.ymax))
1053 // delete the shape again, we didn't do anything
1054 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1055 i->bboxrect.xmin /20.0,
1056 i->bboxrect.ymin /20.0,
1057 i->bboxrect.xmax /20.0,
1058 i->bboxrect.ymax /20.0
1064 swf_ShapeSetEnd(i->tag);
1066 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1068 msg("<trace> Placing shape id %d", i->shapeid);
1070 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1071 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
1073 swf_ShapeFree(i->shape);
1076 i->bboxrectpos = -1;
1081 void wipeSWF(SWF*swf)
1083 TAG*tag = swf->firstTag;
1085 TAG*next = tag->next;
1086 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1087 tag->id != ST_END &&
1088 tag->id != ST_DOACTION &&
1089 tag->id != ST_SHOWFRAME) {
1097 void swfoutput_finalize(gfxdevice_t*dev)
1099 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1101 if(i->tag && i->tag->id == ST_END)
1102 return; //already done
1104 if(i->config_bboxvars) {
1105 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1107 a = action_PushString(a, "xmin");
1108 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1109 a = action_SetVariable(a);
1110 a = action_PushString(a, "ymin");
1111 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1112 a = action_SetVariable(a);
1113 a = action_PushString(a, "xmax");
1114 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1115 a = action_SetVariable(a);
1116 a = action_PushString(a, "ymax");
1117 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1118 a = action_SetVariable(a);
1119 a = action_PushString(a, "width");
1120 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1121 a = action_SetVariable(a);
1122 a = action_PushString(a, "height");
1123 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1124 a = action_SetVariable(a);
1126 swf_ActionSet(tag, a);
1130 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1134 fontlist_t *tmp,*iterator = i->fontlist;
1136 TAG*mtag = i->swf->firstTag;
1137 if(iterator->swffont) {
1138 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1139 if(!i->config_storeallcharacters)
1140 swf_FontReduce(iterator->swffont);
1141 swf_FontSetDefine2(mtag, iterator->swffont);
1144 iterator = iterator->next;
1146 i->tag = swf_InsertTag(i->tag,ST_END);
1147 TAG* tag = i->tag->prev;
1149 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1150 and the ST_END- they confuse the flash player */
1151 while(tag->id == ST_REMOVEOBJECT2) {
1152 TAG* prev = tag->prev;
1160 if(i->config_enablezlib || i->config_flashversion>=6) {
1161 i->swf->compressed = 1;
1165 int swfresult_save(gfxresult_t*gfx, char*filename)
1167 SWF*swf = (SWF*)gfx->internal;
1170 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1175 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1179 if(swf->compressed) {
1180 if FAILED(swf_WriteSWC(fi,swf))
1181 msg("<error> WriteSWC() failed.\n");
1183 if FAILED(swf_WriteSWF(fi,swf))
1184 msg("<error> WriteSWF() failed.\n");
1191 void* swfresult_get(gfxresult_t*gfx, char*name)
1193 SWF*swf = (SWF*)gfx->internal;
1194 if(!strcmp(name, "swf")) {
1195 return (void*)swf_CopySWF(swf);
1196 } else if(!strcmp(name, "xmin")) {
1197 return (void*)(swf->movieSize.xmin/20);
1198 } else if(!strcmp(name, "ymin")) {
1199 return (void*)(swf->movieSize.ymin/20);
1200 } else if(!strcmp(name, "xmax")) {
1201 return (void*)(swf->movieSize.xmax/20);
1202 } else if(!strcmp(name, "ymax")) {
1203 return (void*)(swf->movieSize.ymax/20);
1207 void swfresult_destroy(gfxresult_t*gfx)
1210 swf_FreeTags((SWF*)gfx->internal);
1211 free(gfx->internal);
1214 memset(gfx, 0, sizeof(gfxresult_t));
1218 static void swfoutput_destroy(gfxdevice_t* dev);
1220 gfxresult_t* swf_finish(gfxdevice_t* dev)
1222 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1225 swfoutput_finalize(dev);
1226 SWF* swf = i->swf;i->swf = 0;
1227 swfoutput_destroy(dev);
1229 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1230 result->internal = swf;
1231 result->save = swfresult_save;
1233 result->get = swfresult_get;
1234 result->destroy = swfresult_destroy;
1238 /* Perform cleaning up */
1239 static void swfoutput_destroy(gfxdevice_t* dev)
1241 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1243 /* not initialized yet- nothing to destroy */
1247 fontlist_t *tmp,*iterator = i->fontlist;
1249 if(iterator->swffont) {
1250 swf_FontFree(iterator->swffont);iterator->swffont=0;
1253 iterator = iterator->next;
1256 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1259 memset(dev, 0, sizeof(gfxdevice_t));
1262 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1264 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1265 if(i->fillrgb.r == r &&
1266 i->fillrgb.g == g &&
1267 i->fillrgb.b == b &&
1268 i->fillrgb.a == a) return;
1278 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1280 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1281 if(i->strokergb.r == r &&
1282 i->strokergb.g == g &&
1283 i->strokergb.b == b &&
1284 i->strokergb.a == a) return;
1294 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1296 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1297 if(i->linewidth == (U16)(_linewidth*20))
1302 i->linewidth = (U16)(_linewidth*20);
1306 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1307 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1308 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1309 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1311 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1313 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1314 dev->drawlink(dev, points, url);
1317 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1319 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1321 if(!strncmp("http://pdf2swf:", url, 15)) {
1322 char*tmp = strdup(url);
1323 int l = strlen(tmp);
1326 swfoutput_namedlink(dev, tmp+15, points);
1329 } else if(!strncmp("page", url, 4)) {
1332 if(url[t]<'0' || url[t]>'9')
1335 int page = atoi(&url[4]);
1336 swfoutput_linktopage(dev, page, points);
1339 swfoutput_linktourl(dev, url, points);
1342 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1345 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1351 if(!i->config_opennewwindow)
1352 actions = action_GetUrl(0, url, "_parent");
1354 actions = action_GetUrl(0, url, "_this");
1355 actions = action_End(actions);
1357 drawlink(dev, actions, 0, points,0);
1359 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1361 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1369 actions = action_GotoFrame(0, page);
1370 actions = action_End(actions);
1372 drawlink(dev, actions, 0, points,0);
1375 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1376 of the viewer objects, like subtitles, index elements etc.
1378 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1380 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1381 ActionTAG *actions1,*actions2;
1382 char*tmp = strdup(name);
1390 if(!strncmp(tmp, "call:", 5))
1392 char*x = strchr(&tmp[5], ':');
1394 actions1 = action_PushInt(0, 0); //number of parameters (0)
1395 actions1 = action_PushString(actions1, &tmp[5]); //function name
1396 actions1 = action_CallFunction(actions1);
1399 actions1 = action_PushString(0, x+1); //parameter
1400 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1401 actions1 = action_PushString(actions1, &tmp[5]); //function name
1402 actions1 = action_CallFunction(actions1);
1404 actions2 = action_End(0);
1409 actions1 = action_PushString(0, "/:subtitle");
1410 actions1 = action_PushString(actions1, name);
1411 actions1 = action_SetVariable(actions1);
1412 actions1 = action_End(actions1);
1414 actions2 = action_PushString(0, "/:subtitle");
1415 actions2 = action_PushString(actions2, "");
1416 actions2 = action_SetVariable(actions2);
1417 actions2 = action_End(actions2);
1420 drawlink(dev, actions1, actions2, points,mouseover);
1422 swf_ActionFree(actions1);
1423 swf_ActionFree(actions2);
1427 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1429 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1430 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1435 /* check whether the next segment is zero */
1436 if(line->type == gfx_moveTo) {
1437 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1438 moveto(dev, i->tag, line->x, line->y);
1439 px = lastx = line->x;
1440 py = lasty = line->y;
1442 } if(line->type == gfx_lineTo) {
1443 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1444 lineto(dev, i->tag, line->x, line->y);
1448 } else if(line->type == gfx_splineTo) {
1449 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1451 s.x = line->sx;p.x = line->x;
1452 s.y = line->sy;p.y = line->y;
1453 splineto(dev, i->tag, s, p);
1463 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1465 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1470 struct plotxy p1,p2,p3,p4;
1476 int buttonid = getNewID(dev);
1477 gfxbbox_t bbox = gfxline_getbbox(points);
1480 myshapeid = getNewID(dev);
1481 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1482 swf_ShapeNew(&i->shape);
1483 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1484 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1485 swf_SetU16(i->tag, myshapeid);
1486 r.xmin = (int)(bbox.xmin*20);
1487 r.ymin = (int)(bbox.ymin*20);
1488 r.xmax = (int)(bbox.xmax*20);
1489 r.ymax = (int)(bbox.ymax*20);
1490 swf_SetRect(i->tag,&r);
1491 swf_SetShapeStyles(i->tag,i->shape);
1492 swf_ShapeCountBits(i->shape,NULL,NULL);
1493 swf_SetShapeBits(i->tag,i->shape);
1494 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1495 i->swflastx = i->swflasty = 0;
1496 drawgfxline(dev, points);
1497 swf_ShapeSetEnd(i->tag);
1500 myshapeid2 = getNewID(dev);
1501 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1502 swf_ShapeNew(&i->shape);
1503 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1505 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1506 swf_SetU16(i->tag, myshapeid2);
1507 r.xmin = (int)(bbox.xmin*20);
1508 r.ymin = (int)(bbox.ymin*20);
1509 r.xmax = (int)(bbox.xmax*20);
1510 r.ymax = (int)(bbox.ymax*20);
1511 swf_SetRect(i->tag,&r);
1512 swf_SetShapeStyles(i->tag,i->shape);
1513 swf_ShapeCountBits(i->shape,NULL,NULL);
1514 swf_SetShapeBits(i->tag,i->shape);
1515 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1516 i->swflastx = i->swflasty = 0;
1517 drawgfxline(dev, points);
1518 swf_ShapeSetEnd(i->tag);
1522 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1523 swf_SetU16(i->tag,buttonid); //id
1524 swf_ButtonSetFlags(i->tag, 0); //menu=no
1525 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1526 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1527 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1528 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1529 swf_SetU8(i->tag,0);
1530 swf_ActionSet(i->tag,actions1);
1531 swf_SetU8(i->tag,0);
1535 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1536 swf_SetU16(i->tag,buttonid); //id
1537 swf_ButtonSetFlags(i->tag, 0); //menu=no
1538 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1539 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1540 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1541 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1542 swf_SetU8(i->tag,0); // end of button records
1543 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1544 swf_ActionSet(i->tag,actions1);
1546 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1547 swf_ActionSet(i->tag,actions2);
1548 swf_SetU8(i->tag,0);
1549 swf_ButtonPostProcess(i->tag, 2);
1551 swf_SetU8(i->tag,0);
1552 swf_ButtonPostProcess(i->tag, 1);
1556 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1558 if(posx!=0 || posy!=0) {
1560 p.x = (int)(posx*20);
1561 p.y = (int)(posy*20);
1562 p = swf_TurnPoint(p, &i->page_matrix);
1567 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1569 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1576 for(t=0;t<picpos;t++)
1578 if(pic_xids[t] == xid &&
1579 pic_yids[t] == yid) {
1580 width = pic_width[t];
1581 height = pic_height[t];
1585 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1586 pic_xids[picpos] = xid;
1587 pic_yids[picpos] = yid;
1588 pic_width[picpos] = width;
1589 pic_height[picpos] = height;
1592 pic[width*y+x] = buf[0];
1596 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1597 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1601 xid += x*r+x*b*3+x*g*7+x*a*11;
1602 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1604 for(t=0;t<picpos;t++)
1606 if(pic_xids[t] == xid &&
1607 pic_yids[t] == yid) {
1616 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1618 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1620 if(!strcmp(name, "jpegsubpixels")) {
1621 i->config_jpegsubpixels = atof(value);
1622 } else if(!strcmp(name, "ppmsubpixels")) {
1623 i->config_ppmsubpixels = atof(value);
1624 } else if(!strcmp(name, "drawonlyshapes")) {
1625 i->config_drawonlyshapes = atoi(value);
1626 } else if(!strcmp(name, "ignoredraworder")) {
1627 i->config_ignoredraworder = atoi(value);
1628 } else if(!strcmp(name, "filloverlap")) {
1629 i->config_filloverlap = atoi(value);
1630 } else if(!strcmp(name, "linksopennewwindow")) {
1631 i->config_opennewwindow = atoi(value);
1632 } else if(!strcmp(name, "opennewwindow")) {
1633 i->config_opennewwindow = atoi(value);
1634 } else if(!strcmp(name, "storeallcharacters")) {
1635 i->config_storeallcharacters = atoi(value);
1636 } else if(!strcmp(name, "enablezlib")) {
1637 i->config_enablezlib = atoi(value);
1638 } else if(!strcmp(name, "bboxvars")) {
1639 i->config_bboxvars = atoi(value);
1640 } else if(!strcmp(name, "insertstop")) {
1641 i->config_insertstoptag = atoi(value);
1642 } else if(!strcmp(name, "protected")) {
1643 i->config_protect = atoi(value);
1644 } else if(!strcmp(name, "flashversion")) {
1645 i->config_flashversion = atoi(value);
1646 } else if(!strcmp(name, "minlinewidth")) {
1647 i->config_minlinewidth = atof(value);
1648 } else if(!strcmp(name, "caplinewidth")) {
1649 i->config_caplinewidth = atof(value);
1650 } else if(!strcmp(name, "dumpfonts")) {
1651 i->config_dumpfonts = atoi(value);
1652 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1654 } else if(!strcmp(name, "jpegquality")) {
1655 int val = atoi(value);
1657 if(val>100) val=100;
1658 i->config_jpegquality = val;
1659 } else if(!strcmp(name, "splinequality")) {
1660 int v = atoi(value);
1661 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1663 i->config_splinemaxerror = v;
1664 } else if(!strcmp(name, "fontquality")) {
1665 int v = atoi(value);
1666 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1668 i->config_fontsplinemaxerror = v;
1670 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1676 // --------------------------------------------------------------------
1678 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1681 swf_GetCXForm(0, &cx, 1);
1684 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1685 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1686 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1687 c->ar!=0 || c->ag!=0 || c->ab!=0)
1688 msg("<warning> CXForm not SWF-compatible");
1690 cx.a0 = (S16)(c->aa*256);
1691 cx.r0 = (S16)(c->rr*256);
1692 cx.g0 = (S16)(c->gg*256);
1693 cx.b0 = (S16)(c->bb*256);
1701 static ArtVpath* gfxline_to_ArtVpath(gfxline_t*line)
1703 ArtVpath *vec = NULL;
1708 /* factor which determines into how many line fragments a spline is converted */
1709 double subfraction = 2.4;//0.3
1713 if(l2->type == gfx_moveTo) {
1715 } if(l2->type == gfx_lineTo) {
1717 } if(l2->type == gfx_splineTo) {
1718 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1719 if(!parts) parts = 1;
1729 vec = art_new (ArtVpath, len);
1734 if(l2->type == gfx_moveTo) {
1735 vec[pos].code = ART_MOVETO;
1740 } else if(l2->type == gfx_lineTo) {
1741 vec[pos].code = ART_LINETO;
1746 } else if(l2->type == gfx_splineTo) {
1748 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1749 if(!parts) parts = 1;
1750 for(i=0;i<=parts;i++) {
1751 double t = (double)i/(double)parts;
1752 vec[pos].code = ART_LINETO;
1753 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
1754 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
1763 vec[pos].code = ART_END;
1768 static ArtSVP* gfxfillToSVP(gfxline_t*line)
1770 ArtVpath* vec = gfxline_to_ArtVpath(line);
1771 ArtSVP *svp = art_svp_from_vpath(vec);
1776 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
1778 ArtVpath* vec = gfxline_to_ArtVpath(line);
1779 ArtSVP *svp = art_svp_vpath_stroke (vec,
1780 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
1781 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
1782 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
1783 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
1784 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
1785 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
1787 miterLimit, //miter_limit
1794 static gfxline_t* SVPtogfxline(ArtSVP*svp)
1799 for(t=0;t<svp->n_segs;t++) {
1800 size += svp->segs[t].n_points + 1;
1802 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
1804 for(t=0;t<svp->n_segs;t++) {
1805 ArtSVPSeg* seg = &svp->segs[t];
1807 for(p=0;p<seg->n_points;p++) {
1808 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
1809 ArtPoint* point = &seg->points[p];
1810 lines[pos].x = point->x;
1811 lines[pos].y = point->y;
1812 lines[pos].next = &lines[pos+1];
1817 lines[pos-1].next = 0;
1825 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1829 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1833 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1835 gfxdevice_t*dev = i->dev;
1837 RGBA*mem = (RGBA*)img->data;
1839 int sizex = img->width;
1840 int sizey = img->height;
1841 int is_jpeg = i->jpeg;
1844 int newsizex=sizex, newsizey=sizey;
1847 if(is_jpeg && i->config_jpegsubpixels) {
1848 newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1849 newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1850 } else if(!is_jpeg && i->config_ppmsubpixels) {
1851 newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1852 newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1856 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1859 /* TODO: cache images */
1861 if(newsizex<sizex || newsizey<sizey) {
1862 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1863 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1864 *newwidth = sizex = newsizex;
1865 *newheight = sizey = newsizey;
1868 *newwidth = newsizex = sizex;
1869 *newheight = newsizey = sizey;
1872 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1873 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1875 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
1877 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1880 targetwidth, targetheight,
1881 /*newsizex, newsizey,*/
1882 num_colors>256?">":"", num_colors>256?256:num_colors);
1884 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1885 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1887 for(t=0;t<num_colors;t++) {
1888 printf("%02x%02x%02x%02x ",
1889 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1896 int cacheid = imageInCache(dev, mem, sizex, sizey);
1899 bitid = getNewID(dev);
1900 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1901 addImageToCache(dev, mem, sizex, sizey);
1911 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1913 gfxbbox_t bbox = gfxline_getbbox(line);
1915 r.xmin = (int)(bbox.xmin*20);
1916 r.ymin = (int)(bbox.ymin*20);
1917 r.xmax = (int)(bbox.xmax*20);
1918 r.ymax = (int)(bbox.ymax*20);
1922 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1924 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1929 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1930 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1932 int newwidth=0,newheight=0;
1933 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1936 double fx = (double)img->width / (double)newwidth;
1937 double fy = (double)img->height / (double)newheight;
1942 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1943 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1944 m.tx = (int)(matrix->tx*20);
1945 m.ty = (int)(matrix->ty*20);
1948 int myshapeid = getNewID(dev);
1949 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1951 swf_ShapeNew(&shape);
1952 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1953 swf_SetU16(i->tag, myshapeid);
1954 SRECT r = gfxline_getSWFbbox(line);
1955 swf_SetRect(i->tag,&r);
1956 swf_SetShapeStyles(i->tag,shape);
1957 swf_ShapeCountBits(shape,NULL,NULL);
1958 swf_SetShapeBits(i->tag,shape);
1959 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1960 i->swflastx = i->swflasty = UNDEFINED_COORD;
1961 drawgfxline(dev, line);
1962 swf_ShapeSetEnd(i->tag);
1963 swf_ShapeFree(shape);
1965 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1966 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
1967 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
1970 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
1972 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1977 if(i->clippos >= 127)
1979 msg("<warning> Too many clip levels.");
1983 int myshapeid = getNewID(dev);
1984 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1986 memset(&col, 0, sizeof(RGBA));
1988 swf_ShapeNew(&shape);
1989 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
1990 swf_SetU16(i->tag,myshapeid);
1991 SRECT r = gfxline_getSWFbbox(line);
1992 swf_SetRect(i->tag,&r);
1993 swf_SetShapeStyles(i->tag,shape);
1994 swf_ShapeCountBits(shape,NULL,NULL);
1995 swf_SetShapeBits(i->tag,shape);
1996 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1997 i->swflastx = i->swflasty = UNDEFINED_COORD;
1998 drawgfxline(dev, line);
1999 swf_ShapeSetEnd(i->tag);
2000 swf_ShapeFree(shape);
2002 /* TODO: remember the bbox, and check all shapes against it */
2004 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2005 i->cliptags[i->clippos] = i->tag;
2006 i->clipshapes[i->clippos] = myshapeid;
2007 i->clipdepths[i->clippos] = getNewDepth(dev);
2011 static void swf_endclip(gfxdevice_t*dev)
2013 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2020 msg("<error> Invalid end of clipping region");
2024 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2025 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2027 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2029 static int gfxline_type(gfxline_t*line)
2035 int haszerosegments=0;
2037 if(line->type == gfx_moveTo) {
2040 } else if(line->type == gfx_lineTo) {
2044 } else if(line->type == gfx_splineTo) {
2046 if(tmpsplines>lines)
2051 if(lines==0 && splines==0) return 0;
2052 else if(lines==1 && splines==0) return 1;
2053 else if(lines==0 && splines==1) return 2;
2054 else if(splines==0) return 3;
2058 static int gfxline_has_dots(gfxline_t*line)
2065 if(line->type == gfx_moveTo) {
2066 if(isline && dist < 1) {
2071 } else if(line->type == gfx_lineTo) {
2072 dist += fabs(line->x - x) + fabs(line->y - y);
2074 } else if(line->type == gfx_splineTo) {
2075 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2076 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2083 if(isline && dist < 1) {
2089 static int gfxline_fix_short_edges(gfxline_t*line)
2093 if(line->type == gfx_lineTo) {
2094 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2097 } else if(line->type == gfx_splineTo) {
2098 if(fabs(line->sx - x) + fabs(line->sy - y) +
2099 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2110 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2112 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2113 if(x<i->min_x || x>i->max_x) return 0;
2114 if(y<i->min_y || y>i->max_y) return 0;
2118 static void show_path(ArtSVP*path)
2121 printf("Segments: %d\n", path->n_segs);
2122 for(t=0;t<path->n_segs;t++) {
2123 ArtSVPSeg* seg = &path->segs[t];
2124 printf("Segment %d: %d points, %s, BBox: (%f,%f,%f,%f)\n",
2125 t, seg->n_points, seg->dir==0?"UP ":"DOWN",
2126 seg->bbox.x0, seg->bbox.y0, seg->bbox.x1, seg->bbox.y1);
2128 for(p=0;p<seg->n_points;p++) {
2129 ArtPoint* point = &seg->points[p];
2130 printf(" (%f,%f)\n", point->x, point->y);
2137 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)
2139 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2140 int type = gfxline_type(line);
2141 int has_dots = gfxline_has_dots(line);
2142 gfxbbox_t r = gfxline_getbbox(line);
2143 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2145 /* TODO: * split line into segments, and perform this check for all segments */
2148 (width <= i->config_caplinewidth
2149 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2150 || (cap_style == gfx_capRound && type<=2))) {
2151 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2153 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2154 swfoutput_setlinewidth(dev, width);
2157 drawgfxline(dev, line);
2159 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2161 gfxline_fix_short_edges(line);
2162 /* we need to convert the line into a polygon */
2163 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2164 gfxline_t*gfxline = SVPtogfxline(svp);
2165 dev->fill(dev, gfxline, color);
2170 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2172 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2173 gfxbbox_t r = gfxline_getbbox(line);
2174 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2177 if(!i->config_ignoredraworder)
2179 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2183 drawgfxline(dev, line);
2184 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2186 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2188 msg("<error> Gradient filling not implemented yet");
2191 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2193 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2196 swffont->version = 2;
2197 swffont->name = (U8*)strdup(id);
2198 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2199 swffont->layout->ascent = 0; /* ? */
2200 swffont->layout->descent = 0;
2201 swffont->layout->leading = 0;
2202 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2203 swffont->encoding = FONT_ENCODING_UNICODE;
2204 swffont->numchars = font->num_glyphs;
2205 swffont->maxascii = font->max_unicode;
2206 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2207 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2208 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2209 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2210 for(t=0;t<font->max_unicode;t++) {
2211 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2213 for(t=0;t<font->num_glyphs;t++) {
2216 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2217 if(font->glyphs[t].name) {
2218 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2220 swffont->glyphnames[t] = 0;
2222 swffont->glyph[t].advance = (int)(font->glyphs[t].advance * 20);
2224 swf_Shape01DrawerInit(&draw, 0);
2225 line = font->glyphs[t].line;
2228 c.x = line->sx; c.y = line->sy;
2229 to.x = line->x; to.y = line->y;
2230 if(line->type == gfx_moveTo) {
2231 draw.moveTo(&draw, &to);
2232 } else if(line->type == gfx_lineTo) {
2233 draw.lineTo(&draw, &to);
2234 } else if(line->type == gfx_splineTo) {
2235 draw.splineTo(&draw, &c, &to);
2240 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2241 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2242 draw.dealloc(&draw);
2247 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2249 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2251 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2252 return; // the requested font is the current font
2254 fontlist_t*last=0,*l = i->fontlist;
2257 if(!strcmp((char*)l->swffont->name, fontid)) {
2258 return; // we already know this font
2262 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2263 l->swffont = gfxfont_to_swffont(font, fontid);
2270 swf_FontSetID(l->swffont, getNewID(i->dev));
2272 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2273 // print font information
2274 msg("<debug> Font %s",fontid);
2275 msg("<debug> | ID: %d", l->swffont->id);
2276 msg("<debug> | Version: %d", l->swffont->version);
2277 msg("<debug> | Name: %s", l->swffont->name);
2278 msg("<debug> | Numchars: %d", l->swffont->numchars);
2279 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2280 msg("<debug> | Style: %d", l->swffont->style);
2281 msg("<debug> | Encoding: %d", l->swffont->encoding);
2282 for(int iii=0; iii<l->swffont->numchars;iii++) {
2283 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,
2284 l->swffont->layout->bounds[iii].xmin/20.0,
2285 l->swffont->layout->bounds[iii].ymin/20.0,
2286 l->swffont->layout->bounds[iii].xmax/20.0,
2287 l->swffont->layout->bounds[iii].ymax/20.0
2290 for(t=0;t<l->swffont->maxascii;t++) {
2291 if(l->swffont->ascii2glyph[t] == iii)
2292 msg("<debug> | - maps to %d",t);
2298 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2300 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2302 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2303 return; // the requested font is the current font
2305 fontlist_t*l = i->fontlist;
2307 if(!strcmp((char*)l->swffont->name, fontid)) {
2308 i->swffont = l->swffont;
2313 msg("<error> Unknown font id: %s", fontid);
2317 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2319 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2321 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,fontid)) // not equal to current font
2323 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2324 with multiple fonts */
2327 swf_switchfont(dev, fontid); // set the current font
2329 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2338 drawchar(dev, i->swffont, glyph, &m, color);