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_generate_fake_tags;
81 int config_enablezlib;
82 int config_insertstoptag;
83 int config_flashversion;
84 int config_splinemaxerror;
85 int config_fontsplinemaxerror;
86 int config_filloverlap;
89 float config_minlinewidth;
90 double config_caplinewidth;
129 int pic_height[1024];
135 char fillstylechanged;
137 int jpeg; //next image type
142 chardata_t chardata[CHARDATAMAX];
150 double fontm11,fontm12,fontm21,fontm22;
159 } swfoutput_internal;
161 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
162 static int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
163 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);
164 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
165 static void swf_endclip(gfxdevice_t*dev);
166 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);
167 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
168 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
169 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
170 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
171 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font);
172 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
173 static void swf_startframe(gfxdevice_t*dev, int width, int height);
174 static void swf_endframe(gfxdevice_t*dev);
175 static gfxresult_t* swf_finish(gfxdevice_t*driver);
177 static swfoutput_internal* init_internal_struct()
179 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
180 memset(i, 0, sizeof(swfoutput_internal));
202 i->fillstylechanged = 0;
209 i->config_dumpfonts=0;
210 i->config_ppmsubpixels=0;
211 i->config_jpegsubpixels=0;
212 i->config_opennewwindow=1;
213 i->config_ignoredraworder=0;
214 i->config_drawonlyshapes=0;
215 i->config_jpegquality=85;
216 i->config_storeallcharacters=0;
217 i->config_enablezlib=0;
218 i->config_generate_fake_tags=0;
219 i->config_insertstoptag=0;
220 i->config_flashversion=6;
221 i->config_splinemaxerror=1;
222 i->config_fontsplinemaxerror=1;
223 i->config_filloverlap=0;
225 i->config_bboxvars=0;
226 i->config_minlinewidth=0.05;
227 i->config_caplinewidth=1;
232 static int id_error = 0;
234 static U16 getNewID(gfxdevice_t* dev)
236 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
237 if(i->currentswfid == 65535) {
239 msg("<error> ID Table overflow");
243 return ++i->currentswfid;
245 static U16 getNewDepth(gfxdevice_t* dev)
247 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
248 if(i->depth == 65535) {
250 msg("<error> Depth Table overflow");
257 static void startshape(gfxdevice_t* dev);
258 static void starttext(gfxdevice_t* dev);
259 static void endshape(gfxdevice_t* dev);
260 static void endtext(gfxdevice_t* dev);
262 // write a move-to command into the swf
263 static int moveto(gfxdevice_t*dev, TAG*tag, plotxy p0)
265 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
266 int rx = (int)(p0.x*20);
267 int ry = (int)(p0.y*20);
268 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
269 swf_ShapeSetMove (tag, i->shape, rx,ry);
270 i->fillstylechanged = 0;
277 static int moveto(gfxdevice_t*dev, TAG*tag, double x, double y)
279 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
283 return moveto(dev, tag, p);
285 static void addPointToBBox(gfxdevice_t*dev, int px, int py)
287 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
293 swf_ExpandRect(&i->bboxrect, p);
295 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
299 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
301 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
302 int width = i->linewidth/4;
306 //swf_ShapeSetLine(tag, i->shape,-width,-width);
307 //swf_ShapeSetLine(tag, i->shape,width*2,0);
308 //swf_ShapeSetLine(tag, i->shape,0,width*2);
309 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
310 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
311 //swf_ShapeSetLine(tag, i->shape,width,width);
314 swf_ShapeSetLine(tag, i->shape,-width,0);
315 swf_ShapeSetLine(tag, i->shape,width,-width);
316 swf_ShapeSetLine(tag, i->shape,width,width);
317 swf_ShapeSetLine(tag, i->shape,-width,width);
318 swf_ShapeSetLine(tag, i->shape,-width,-width);
319 swf_ShapeSetLine(tag, i->shape,width,0);
321 addPointToBBox(dev, x-width ,y-width);
322 addPointToBBox(dev, x+width ,y+width);
325 // write a line-to command into the swf
326 static void lineto(gfxdevice_t*dev, TAG*tag, plotxy p0)
328 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
329 int px = (int)(p0.x*20);
330 int py = (int)(p0.y*20);
331 int rx = (px-i->swflastx);
332 int ry = (py-i->swflasty);
334 swf_ShapeSetLine (tag, i->shape, rx,ry);
335 addPointToBBox(dev, i->swflastx,i->swflasty);
336 addPointToBBox(dev, px,py);
337 }/* else if(!i->fill) {
338 // treat lines of length 0 as plots, making them
339 // at least 1 twip wide so Flash will display them
340 plot(dev, i->swflastx, i->swflasty, tag);
347 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
355 // write a spline-to command into the swf
356 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy control,plotxy end)
358 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
359 int lastlastx = i->swflastx;
360 int lastlasty = i->swflasty;
362 int cx = ((int)(control.x*20)-i->swflastx);
363 int cy = ((int)(control.y*20)-i->swflasty);
366 int ex = ((int)(end.x*20)-i->swflastx);
367 int ey = ((int)(end.y*20)-i->swflasty);
371 if(cx || cy || ex || ey) {
372 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
373 addPointToBBox(dev, lastlastx ,lastlasty );
374 addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
375 addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
376 }/* else if(!i->fill) {
377 // treat splines of length 0 as plots
378 plot(dev, lastlastx, lastlasty, tag);
383 /* write a line, given two points and the transformation
385 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy p0, plotxy p1)
387 moveto(dev, tag, p0);
388 lineto(dev, tag, p1);
391 void resetdrawer(gfxdevice_t*dev)
393 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
398 static void stopFill(gfxdevice_t*dev)
400 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
403 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
404 i->fillstylechanged = 1;
408 static void startFill(gfxdevice_t*dev)
410 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
413 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
414 i->fillstylechanged = 1;
419 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
431 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
433 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
436 memset(&r, 0, sizeof(r));
439 if(debug) printf("\n");
440 for(t=0;t<i->chardatapos;t++)
442 if(i->chardata[t].fontid != font->id) {
443 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
446 SRECT b = font->layout->bounds[i->chardata[t].charid];
447 b.xmin *= i->chardata[t].size;
448 b.ymin *= i->chardata[t].size;
449 b.xmax *= i->chardata[t].size;
450 b.ymax *= i->chardata[t].size;
452 /* divide by 1024, rounding xmax/ymax up */
453 b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
455 b.xmin += i->chardata[t].x;
456 b.ymin += i->chardata[t].y;
457 b.xmax += i->chardata[t].x;
458 b.ymax += i->chardata[t].y;
460 /* until we solve the INTERNAL_SCALING problem (see below)
461 make sure the bounding box is big enough */
467 b = swf_TurnRect(b, m);
469 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
470 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
471 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
472 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
473 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
478 i->chardata[t].fontid,
480 i->chardata[t].charid
482 swf_ExpandRect2(&r, &b);
484 if(debug) printf("-----> (%f,%f,%f,%f)\n",
492 static void putcharacters(gfxdevice_t*dev, TAG*tag)
494 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
498 color.r = i->chardata[0].color.r^255;
507 int charadvance[128];
510 int glyphbits=1; //TODO: can this be zero?
513 if(tag->id != ST_DEFINETEXT &&
514 tag->id != ST_DEFINETEXT2) {
515 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
518 if(!i->chardatapos) {
519 msg("<warning> putcharacters called with zero characters");
522 for(pass = 0; pass < 2; pass++)
532 advancebits++; // add sign bit
533 swf_SetU8(tag, glyphbits);
534 swf_SetU8(tag, advancebits);
537 for(t=0;t<=i->chardatapos;t++)
539 if(lastfontid != i->chardata[t].fontid ||
540 lastx!=i->chardata[t].x ||
541 lasty!=i->chardata[t].y ||
542 !colorcompare(dev,&color, &i->chardata[t].color) ||
544 lastsize != i->chardata[t].size ||
547 if(charstorepos && pass==0)
550 for(s=0;s<charstorepos;s++)
552 while(charids[s]>=(1<<glyphbits))
554 while(charadvance[s]>=(1<<advancebits))
558 if(charstorepos && pass==1)
560 tag->writeBit = 0; // Q&D
561 swf_SetBits(tag, 0, 1); // GLYPH Record
562 swf_SetBits(tag, charstorepos, 7); // number of glyphs
564 for(s=0;s<charstorepos;s++)
566 swf_SetBits(tag, charids[s], glyphbits);
567 swf_SetBits(tag, charadvance[s], advancebits);
572 if(pass == 1 && t<i->chardatapos)
578 if(lastx != i->chardata[t].x ||
579 lasty != i->chardata[t].y)
581 newx = i->chardata[t].x;
582 newy = i->chardata[t].y;
588 if(!colorcompare(dev,&color, &i->chardata[t].color))
590 color = i->chardata[t].color;
593 font.id = i->chardata[t].fontid;
594 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
597 tag->writeBit = 0; // Q&D
598 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
601 lastfontid = i->chardata[t].fontid;
602 lastx = i->chardata[t].x;
603 lasty = i->chardata[t].y;
604 lastsize = i->chardata[t].size;
607 if(t==i->chardatapos)
611 int nextt = t==i->chardatapos-1?t:t+1;
612 int rel = i->chardata[nextt].x-i->chardata[t].x;
613 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
615 lastx=i->chardata[nextt].x;
619 lastx=i->chardata[t].x;
621 charids[charstorepos] = i->chardata[t].charid;
622 charadvance[charstorepos] = advance;
629 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
631 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
632 if(i->chardatapos == CHARDATAMAX)
634 msg("<warning> Character buffer too small. SWF will be slightly bigger");
638 i->chardata[i->chardatapos].fontid = fontid;
639 i->chardata[i->chardatapos].charid = charid;
640 i->chardata[i->chardatapos].x = x;
641 i->chardata[i->chardatapos].y = y;
642 i->chardata[i->chardatapos].color = color;
643 i->chardata[i->chardatapos].size = size;
647 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
648 So if we set this value to high, the char coordinates will overflow.
649 If we set it to low, however, the char positions will be inaccurate */
650 #define FONT_INTERNAL_SIZE 4
652 /* process a character. */
653 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix*m, gfxcolor_t*col)
655 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
657 msg("<warning> Font is NULL");
661 if(charid<0 || charid>=swffont->numchars) {
662 msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
665 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
666 msg("<warning> Glyph %d in current charset (%s, %d characters) is empty",
667 charid, FIXNULL((char*)swffont->name), swffont->numchars);
678 float det = ((m->m11*m->m22)-(m->m21*m->m12));
679 if(fabs(det) < 0.0005) {
680 /* x direction equals y direction- the text is invisible */
683 det = 20*FONT_INTERNAL_SIZE / det;
686 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
687 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
689 RGBA rgba = *(RGBA*)col;
690 putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
691 swf_FontUseGlyph(swffont, charid);
695 static void endtext(gfxdevice_t*dev)
697 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
701 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
702 swf_SetU16(i->tag, i->textid);
705 r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
707 swf_SetRect(i->tag,&r);
709 swf_SetMatrix(i->tag,&i->fontmatrix);
711 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
713 putcharacters(dev, i->tag);
715 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
717 swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
721 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
722 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
723 double m12,double m22)
729 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
730 if(i->fontm11 == m11 &&
743 m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
744 m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE);
750 static void endpage(gfxdevice_t*dev)
752 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
762 void swf_startframe(gfxdevice_t*dev, int width, int height)
764 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
765 if(!i->firstpage && !i->pagefinished)
767 msg("<verbose> Starting new SWF page of size %dx%d", width, height);
769 swf_GetMatrix(0, &i->page_matrix);
770 i->page_matrix.tx = 0;
771 i->page_matrix.ty = 0;
777 /* set clipping/background rectangle */
778 /* TODO: this should all be done in SWFOutputDev */
779 //setBackground(dev, x1, y1, x2, y2);
781 /* increase SWF's bounding box */
787 swf_ExpandRect2(&i->swf->movieSize, &r);
789 i->lastframeno = i->frameno;
794 void swf_endframe(gfxdevice_t*dev)
796 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
801 if(i->config_insertstoptag) {
803 atag = action_Stop(atag);
804 atag = action_End(atag);
805 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
806 swf_ActionSet(i->tag,atag);
808 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
811 for(i->depth;i->depth>i->startdepth;i->depth--) {
812 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
813 swf_SetU16(i->tag,i->depth);
815 i->depth = i->startdepth;
818 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
820 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
822 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
826 int shapeid = getNewID(dev);
831 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
833 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
834 swf_SetU16(i->tag,shapeid);
835 swf_SetRect(i->tag,&r);
836 swf_SetShapeHeader(i->tag,s);
837 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
838 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
839 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
840 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
841 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
842 swf_ShapeSetEnd(i->tag);
844 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
845 swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
846 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
847 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
850 /* initialize the swf writer */
851 void gfxdevice_swf_init(gfxdevice_t* dev)
853 memset(dev, 0, sizeof(gfxdevice_t));
854 dev->internal = init_internal_struct();
856 dev->startpage = swf_startframe;
857 dev->endpage = swf_endframe;
858 dev->finish = swf_finish;
859 dev->fillbitmap = swf_fillbitmap;
860 dev->setparameter = swf_setparameter;
861 dev->stroke = swf_stroke;
862 dev->startclip = swf_startclip;
863 dev->endclip = swf_endclip;
864 dev->fill = swf_fill;
865 dev->fillbitmap = swf_fillbitmap;
866 dev->fillgradient = swf_fillgradient;
867 dev->addfont = swf_addfont;
868 dev->drawchar = swf_drawchar;
869 dev->drawlink = swf_drawlink;
871 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
877 msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
881 i->swf = (SWF*)rfx_calloc(sizeof(SWF));
882 i->swf->fileVersion = i->config_flashversion;
883 i->swf->frameRate = 0x0040; // 1 frame per 4 seconds
884 i->swf->movieSize.xmin = 0;
885 i->swf->movieSize.ymin = 0;
886 i->swf->movieSize.xmax = 0;
887 i->swf->movieSize.ymax = 0;
889 i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
890 i->tag = i->swf->firstTag;
891 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
892 swf_SetRGB(i->tag,&rgb);
894 i->startdepth = i->depth = 0;
896 if(i->config_protect)
897 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
900 static void startshape(gfxdevice_t*dev)
902 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
911 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
913 swf_ShapeNew(&i->shape);
914 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
915 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
917 i->shapeid = getNewID(dev);
919 msg("<debug> Using shape id %d", i->shapeid);
921 swf_SetU16(i->tag,i->shapeid); // ID
923 i->bboxrectpos = i->tag->len;
927 r.xmax = 20*i->max_x;
928 r.ymax = 20*i->max_y;
929 swf_SetRect(i->tag,&r);
931 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
933 swf_SetShapeStyles(i->tag,i->shape);
934 swf_ShapeCountBits(i->shape,NULL,NULL);
935 swf_SetShapeBits(i->tag,i->shape);
937 /* TODO: do we really need this? */
938 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
939 i->swflastx=i->swflasty=0;
944 static void starttext(gfxdevice_t*dev)
946 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
950 i->textid = getNewID(dev);
952 i->swflastx=i->swflasty=0;
956 /* TODO: move to ../lib/rfxswf */
957 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
959 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
960 /* determine length of old rect */
964 swf_GetRect(tag, &old);
965 swf_ResetReadBits(tag);
966 int pos_end = tag->pos;
968 int len = tag->len - pos_end;
969 U8*data = (U8*)malloc(len);
970 memcpy(data, &tag->data[pos_end], len);
973 swf_SetRect(tag, newrect);
974 swf_SetBlock(tag, data, len);
976 tag->pos = tag->readBit = 0;
979 void cancelshape(gfxdevice_t*dev)
981 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
982 /* delete old shape tag */
984 i->tag = i->tag->prev;
985 swf_DeleteTag(todel);
986 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
991 void fixAreas(gfxdevice_t*dev)
993 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
994 if(!i->shapeisempty && i->fill &&
995 (i->bboxrect.xmin == i->bboxrect.xmax ||
996 i->bboxrect.ymin == i->bboxrect.ymax) &&
997 i->config_minlinewidth >= 0.001
999 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1000 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1001 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1004 SRECT r = i->bboxrect;
1006 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1007 /* this thing comes down to a single dot- nothing to fix here */
1013 RGBA save_col = i->strokergb;
1014 int save_width = i->linewidth;
1016 i->strokergb = i->fillrgb;
1017 i->linewidth = (int)(i->config_minlinewidth*20);
1018 if(i->linewidth==0) i->linewidth = 1;
1022 moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1023 lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1025 i->strokergb = save_col;
1026 i->linewidth = save_width;
1031 static void endshape_noput(gfxdevice_t*dev)
1033 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1036 //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1039 swf_ShapeFree(i->shape);
1047 static void endshape(gfxdevice_t*dev)
1049 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1055 if(i->shapeisempty ||
1057 (i->bboxrect.xmin == i->bboxrect.xmax &&
1058 i->bboxrect.ymin == i->bboxrect.ymax))
1060 // delete the shape again, we didn't do anything
1061 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1062 i->bboxrect.xmin /20.0,
1063 i->bboxrect.ymin /20.0,
1064 i->bboxrect.xmax /20.0,
1065 i->bboxrect.ymax /20.0
1071 swf_ShapeSetEnd(i->tag);
1073 changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1075 msg("<trace> Placing shape id %d", i->shapeid);
1077 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1078 MATRIX m = i->page_matrix;
1079 m.tx += i->shapeposx;
1080 m.ty += i->shapeposy;
1081 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1083 swf_ShapeFree(i->shape);
1086 i->bboxrectpos = -1;
1093 void wipeSWF(SWF*swf)
1095 TAG*tag = swf->firstTag;
1097 TAG*next = tag->next;
1098 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1099 tag->id != ST_END &&
1100 tag->id != ST_DOACTION &&
1101 tag->id != ST_SHOWFRAME) {
1109 void swfoutput_finalize(gfxdevice_t*dev)
1111 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1113 if(i->tag && i->tag->id == ST_END)
1114 return; //already done
1116 if(i->config_bboxvars) {
1117 TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1119 a = action_PushString(a, "xmin");
1120 a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1121 a = action_SetVariable(a);
1122 a = action_PushString(a, "ymin");
1123 a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1124 a = action_SetVariable(a);
1125 a = action_PushString(a, "xmax");
1126 a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1127 a = action_SetVariable(a);
1128 a = action_PushString(a, "ymax");
1129 a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1130 a = action_SetVariable(a);
1131 a = action_PushString(a, "width");
1132 a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1133 a = action_SetVariable(a);
1134 a = action_PushString(a, "height");
1135 a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1136 a = action_SetVariable(a);
1138 swf_ActionSet(tag, a);
1142 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1146 fontlist_t *tmp,*iterator = i->fontlist;
1148 TAG*mtag = i->swf->firstTag;
1149 if(iterator->swffont) {
1150 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1151 if(!i->config_storeallcharacters) {
1152 msg("<debug> Reducing font %s", iterator->swffont->name);
1153 swf_FontReduce(iterator->swffont);
1155 swf_FontSetDefine2(mtag, iterator->swffont);
1158 iterator = iterator->next;
1160 i->tag = swf_InsertTag(i->tag,ST_END);
1161 TAG* tag = i->tag->prev;
1163 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1164 and the ST_END- they confuse the flash player */
1165 while(tag->id == ST_REMOVEOBJECT2) {
1166 TAG* prev = tag->prev;
1174 if(i->config_enablezlib || i->config_flashversion>=6) {
1175 i->swf->compressed = 1;
1179 int swfresult_save(gfxresult_t*gfx, char*filename)
1181 SWF*swf = (SWF*)gfx->internal;
1184 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1189 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1193 if(swf->compressed) {
1194 if FAILED(swf_WriteSWC(fi,swf))
1195 msg("<error> WriteSWC() failed.\n");
1197 if FAILED(swf_WriteSWF(fi,swf))
1198 msg("<error> WriteSWF() failed.\n");
1205 void* swfresult_get(gfxresult_t*gfx, char*name)
1207 SWF*swf = (SWF*)gfx->internal;
1208 if(!strcmp(name, "swf")) {
1209 return (void*)swf_CopySWF(swf);
1210 } else if(!strcmp(name, "xmin")) {
1211 return (void*)(swf->movieSize.xmin/20);
1212 } else if(!strcmp(name, "ymin")) {
1213 return (void*)(swf->movieSize.ymin/20);
1214 } else if(!strcmp(name, "xmax")) {
1215 return (void*)(swf->movieSize.xmax/20);
1216 } else if(!strcmp(name, "ymax")) {
1217 return (void*)(swf->movieSize.ymax/20);
1221 void swfresult_destroy(gfxresult_t*gfx)
1224 swf_FreeTags((SWF*)gfx->internal);
1225 free(gfx->internal);
1228 memset(gfx, 0, sizeof(gfxresult_t));
1232 static void swfoutput_destroy(gfxdevice_t* dev);
1234 gfxresult_t* swf_finish(gfxdevice_t* dev)
1236 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1239 swfoutput_finalize(dev);
1240 SWF* swf = i->swf;i->swf = 0;
1241 swfoutput_destroy(dev);
1243 result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1244 result->internal = swf;
1245 result->save = swfresult_save;
1247 result->get = swfresult_get;
1248 result->destroy = swfresult_destroy;
1252 /* Perform cleaning up */
1253 static void swfoutput_destroy(gfxdevice_t* dev)
1255 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1257 /* not initialized yet- nothing to destroy */
1261 fontlist_t *tmp,*iterator = i->fontlist;
1263 if(iterator->swffont) {
1264 swf_FontFree(iterator->swffont);iterator->swffont=0;
1267 iterator = iterator->next;
1270 if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1273 memset(dev, 0, sizeof(gfxdevice_t));
1276 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1278 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1279 if(i->fillrgb.r == r &&
1280 i->fillrgb.g == g &&
1281 i->fillrgb.b == b &&
1282 i->fillrgb.a == a) return;
1292 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1294 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1295 if(i->strokergb.r == r &&
1296 i->strokergb.g == g &&
1297 i->strokergb.b == b &&
1298 i->strokergb.a == a) return;
1308 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1310 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1311 if(i->linewidth == (U16)(_linewidth*20+19))
1315 i->linewidth = (U16)(_linewidth*20+19);
1319 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1320 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1321 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1322 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1324 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1326 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1327 dev->drawlink(dev, points, url);
1330 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1332 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1334 if(!strncmp("http://pdf2swf:", url, 15)) {
1335 char*tmp = strdup(url);
1336 int l = strlen(tmp);
1339 swfoutput_namedlink(dev, tmp+15, points);
1342 } else if(!strncmp("page", url, 4)) {
1345 if(url[t]<'0' || url[t]>'9')
1348 int page = atoi(&url[4]) - 1;
1349 if(page<0) page = 0;
1350 swfoutput_linktopage(dev, page, points);
1353 swfoutput_linktourl(dev, url, points);
1356 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1359 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1365 if(!i->config_opennewwindow)
1366 actions = action_GetUrl(0, url, "_parent");
1368 actions = action_GetUrl(0, url, "_this");
1369 actions = action_End(actions);
1371 drawlink(dev, actions, 0, points,0);
1373 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1375 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1383 actions = action_GotoFrame(0, page);
1384 actions = action_End(actions);
1386 drawlink(dev, actions, 0, points,0);
1389 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1390 of the viewer objects, like subtitles, index elements etc.
1392 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1394 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1395 ActionTAG *actions1,*actions2;
1396 char*tmp = strdup(name);
1404 if(!strncmp(tmp, "call:", 5))
1406 char*x = strchr(&tmp[5], ':');
1408 actions1 = action_PushInt(0, 0); //number of parameters (0)
1409 actions1 = action_PushString(actions1, &tmp[5]); //function name
1410 actions1 = action_CallFunction(actions1);
1413 actions1 = action_PushString(0, x+1); //parameter
1414 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1415 actions1 = action_PushString(actions1, &tmp[5]); //function name
1416 actions1 = action_CallFunction(actions1);
1418 actions2 = action_End(0);
1423 actions1 = action_PushString(0, "/:subtitle");
1424 actions1 = action_PushString(actions1, name);
1425 actions1 = action_SetVariable(actions1);
1426 actions1 = action_End(actions1);
1428 actions2 = action_PushString(0, "/:subtitle");
1429 actions2 = action_PushString(actions2, "");
1430 actions2 = action_SetVariable(actions2);
1431 actions2 = action_End(actions2);
1434 drawlink(dev, actions1, actions2, points,mouseover);
1436 swf_ActionFree(actions1);
1437 swf_ActionFree(actions2);
1441 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1443 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1444 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1449 /* check whether the next segment is zero */
1450 if(line->type == gfx_moveTo) {
1451 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1452 moveto(dev, i->tag, line->x, line->y);
1453 px = lastx = line->x;
1454 py = lasty = line->y;
1456 } if(line->type == gfx_lineTo) {
1457 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1458 lineto(dev, i->tag, line->x, line->y);
1462 } else if(line->type == gfx_splineTo) {
1463 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1465 s.x = line->sx;p.x = line->x;
1466 s.y = line->sy;p.y = line->y;
1467 splineto(dev, i->tag, s, p);
1477 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1479 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1484 struct plotxy p1,p2,p3,p4;
1490 int buttonid = getNewID(dev);
1491 gfxbbox_t bbox = gfxline_getbbox(points);
1494 myshapeid = getNewID(dev);
1495 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1496 swf_ShapeNew(&i->shape);
1497 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1498 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1499 swf_SetU16(i->tag, myshapeid);
1500 r.xmin = (int)(bbox.xmin*20);
1501 r.ymin = (int)(bbox.ymin*20);
1502 r.xmax = (int)(bbox.xmax*20);
1503 r.ymax = (int)(bbox.ymax*20);
1504 swf_SetRect(i->tag,&r);
1505 swf_SetShapeStyles(i->tag,i->shape);
1506 swf_ShapeCountBits(i->shape,NULL,NULL);
1507 swf_SetShapeBits(i->tag,i->shape);
1508 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1509 i->swflastx = i->swflasty = 0;
1510 drawgfxline(dev, points);
1511 swf_ShapeSetEnd(i->tag);
1514 myshapeid2 = getNewID(dev);
1515 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1516 swf_ShapeNew(&i->shape);
1517 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1519 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1520 swf_SetU16(i->tag, myshapeid2);
1521 r.xmin = (int)(bbox.xmin*20);
1522 r.ymin = (int)(bbox.ymin*20);
1523 r.xmax = (int)(bbox.xmax*20);
1524 r.ymax = (int)(bbox.ymax*20);
1525 swf_SetRect(i->tag,&r);
1526 swf_SetShapeStyles(i->tag,i->shape);
1527 swf_ShapeCountBits(i->shape,NULL,NULL);
1528 swf_SetShapeBits(i->tag,i->shape);
1529 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1530 i->swflastx = i->swflasty = 0;
1531 drawgfxline(dev, points);
1532 swf_ShapeSetEnd(i->tag);
1536 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1537 swf_SetU16(i->tag,buttonid); //id
1538 swf_ButtonSetFlags(i->tag, 0); //menu=no
1539 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1540 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1541 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1542 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1543 swf_SetU8(i->tag,0);
1544 swf_ActionSet(i->tag,actions1);
1545 swf_SetU8(i->tag,0);
1549 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1550 swf_SetU16(i->tag,buttonid); //id
1551 swf_ButtonSetFlags(i->tag, 0); //menu=no
1552 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1553 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1554 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1555 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1556 swf_SetU8(i->tag,0); // end of button records
1557 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1558 swf_ActionSet(i->tag,actions1);
1560 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1561 swf_ActionSet(i->tag,actions2);
1562 swf_SetU8(i->tag,0);
1563 swf_ButtonPostProcess(i->tag, 2);
1565 swf_SetU8(i->tag,0);
1566 swf_ButtonPostProcess(i->tag, 1);
1570 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1572 if(posx!=0 || posy!=0) {
1574 p.x = (int)(posx*20);
1575 p.y = (int)(posy*20);
1576 p = swf_TurnPoint(p, &i->page_matrix);
1581 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1583 swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1590 for(t=0;t<picpos;t++)
1592 if(pic_xids[t] == xid &&
1593 pic_yids[t] == yid) {
1594 width = pic_width[t];
1595 height = pic_height[t];
1599 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1600 pic_xids[picpos] = xid;
1601 pic_yids[picpos] = yid;
1602 pic_width[picpos] = width;
1603 pic_height[picpos] = height;
1606 pic[width*y+x] = buf[0];
1610 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1611 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1615 xid += x*r+x*b*3+x*g*7+x*a*11;
1616 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1618 for(t=0;t<picpos;t++)
1620 if(pic_xids[t] == xid &&
1621 pic_yids[t] == yid) {
1630 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1632 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1634 if(!strcmp(name, "jpegsubpixels")) {
1635 i->config_jpegsubpixels = atof(value);
1636 } else if(!strcmp(name, "ppmsubpixels")) {
1637 i->config_ppmsubpixels = atof(value);
1638 } else if(!strcmp(name, "drawonlyshapes")) {
1639 i->config_drawonlyshapes = atoi(value);
1640 } else if(!strcmp(name, "ignoredraworder")) {
1641 i->config_ignoredraworder = atoi(value);
1642 } else if(!strcmp(name, "filloverlap")) {
1643 i->config_filloverlap = atoi(value);
1644 } else if(!strcmp(name, "linksopennewwindow")) {
1645 i->config_opennewwindow = atoi(value);
1646 } else if(!strcmp(name, "opennewwindow")) {
1647 i->config_opennewwindow = atoi(value);
1648 } else if(!strcmp(name, "storeallcharacters")) {
1649 i->config_storeallcharacters = atoi(value);
1650 } else if(!strcmp(name, "enablezlib")) {
1651 i->config_enablezlib = atoi(value);
1652 } else if(!strcmp(name, "bboxvars")) {
1653 i->config_bboxvars = atoi(value);
1654 } else if(!strcmp(name, "insertstop")) {
1655 i->config_insertstoptag = atoi(value);
1656 } else if(!strcmp(name, "protect")) {
1657 i->config_protect = atoi(value);
1658 if(i->config_protect && i->tag) {
1659 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1661 } else if(!strcmp(name, "faketags")) {
1662 i->config_generate_fake_tags = atoi(value);
1663 } else if(!strcmp(name, "flashversion")) {
1664 i->config_flashversion = atoi(value);
1666 i->swf->fileVersion = i->config_flashversion;
1668 } else if(!strcmp(name, "minlinewidth")) {
1669 i->config_minlinewidth = atof(value);
1670 } else if(!strcmp(name, "caplinewidth")) {
1671 i->config_caplinewidth = atof(value);
1672 } else if(!strcmp(name, "dumpfonts")) {
1673 i->config_dumpfonts = atoi(value);
1674 } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1676 } else if(!strcmp(name, "jpegquality")) {
1677 int val = atoi(value);
1679 if(val>100) val=100;
1680 i->config_jpegquality = val;
1681 } else if(!strcmp(name, "splinequality")) {
1682 int v = atoi(value);
1683 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1685 i->config_splinemaxerror = v;
1686 } else if(!strcmp(name, "fontquality")) {
1687 int v = atoi(value);
1688 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1690 i->config_fontsplinemaxerror = v;
1692 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1698 // --------------------------------------------------------------------
1700 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1703 swf_GetCXForm(0, &cx, 1);
1706 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1707 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1708 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1709 c->ar!=0 || c->ag!=0 || c->ab!=0)
1710 msg("<warning> CXForm not SWF-compatible");
1712 cx.a0 = (S16)(c->aa*256);
1713 cx.r0 = (S16)(c->rr*256);
1714 cx.g0 = (S16)(c->gg*256);
1715 cx.b0 = (S16)(c->bb*256);
1723 static ArtVpath* gfxline_to_ArtVpath(gfxline_t*line)
1725 ArtVpath *vec = NULL;
1730 /* factor which determines into how many line fragments a spline is converted */
1731 double subfraction = 2.4;//0.3
1735 if(l2->type == gfx_moveTo) {
1737 } if(l2->type == gfx_lineTo) {
1739 } if(l2->type == gfx_splineTo) {
1740 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1741 if(!parts) parts = 1;
1751 vec = art_new (ArtVpath, len);
1756 if(l2->type == gfx_moveTo) {
1757 vec[pos].code = ART_MOVETO;
1762 } else if(l2->type == gfx_lineTo) {
1763 vec[pos].code = ART_LINETO;
1768 } else if(l2->type == gfx_splineTo) {
1770 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
1771 if(!parts) parts = 1;
1772 for(i=0;i<=parts;i++) {
1773 double t = (double)i/(double)parts;
1774 vec[pos].code = ART_LINETO;
1775 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
1776 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
1785 vec[pos].code = ART_END;
1790 static ArtSVP* gfxfillToSVP(gfxline_t*line)
1792 ArtVpath* vec = gfxline_to_ArtVpath(line);
1793 ArtSVP *svp = art_svp_from_vpath(vec);
1798 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
1800 ArtVpath* vec = gfxline_to_ArtVpath(line);
1801 ArtSVP *svp = art_svp_vpath_stroke (vec,
1802 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
1803 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
1804 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
1805 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
1806 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
1807 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
1809 miterLimit, //miter_limit
1816 static gfxline_t* SVPtogfxline(ArtSVP*svp)
1821 for(t=0;t<svp->n_segs;t++) {
1822 size += svp->segs[t].n_points + 1;
1824 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
1826 for(t=0;t<svp->n_segs;t++) {
1827 ArtSVPSeg* seg = &svp->segs[t];
1829 for(p=0;p<seg->n_points;p++) {
1830 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
1831 ArtPoint* point = &seg->points[p];
1832 lines[pos].x = point->x;
1833 lines[pos].y = point->y;
1834 lines[pos].next = &lines[pos+1];
1839 lines[pos-1].next = 0;
1847 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1851 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1855 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1857 gfxdevice_t*dev = i->dev;
1859 RGBA*mem = (RGBA*)img->data;
1861 int sizex = img->width;
1862 int sizey = img->height;
1863 int is_jpeg = i->jpeg;
1866 int newsizex=sizex, newsizey=sizey;
1869 if(is_jpeg && i->config_jpegsubpixels) {
1870 newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1871 newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1872 } else if(!is_jpeg && i->config_ppmsubpixels) {
1873 newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1874 newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1878 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1881 /* TODO: cache images */
1883 if(newsizex<sizex || newsizey<sizey) {
1884 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1885 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1886 *newwidth = sizex = newsizex;
1887 *newheight = sizey = newsizey;
1890 *newwidth = newsizex = sizex;
1891 *newheight = newsizey = sizey;
1894 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1895 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1897 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
1899 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
1902 targetwidth, targetheight,
1903 /*newsizex, newsizey,*/
1904 num_colors>256?">":"", num_colors>256?256:num_colors);
1906 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1907 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1909 for(t=0;t<num_colors;t++) {
1910 printf("%02x%02x%02x%02x ",
1911 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1918 int cacheid = imageInCache(dev, mem, sizex, sizey);
1921 bitid = getNewID(dev);
1922 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1923 addImageToCache(dev, mem, sizex, sizey);
1933 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1935 gfxbbox_t bbox = gfxline_getbbox(line);
1937 r.xmin = (int)(bbox.xmin*20);
1938 r.ymin = (int)(bbox.ymin*20);
1939 r.xmax = (int)(bbox.xmax*20);
1940 r.ymax = (int)(bbox.ymax*20);
1944 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1946 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1951 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1952 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1954 int newwidth=0,newheight=0;
1955 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1958 double fx = (double)img->width / (double)newwidth;
1959 double fy = (double)img->height / (double)newheight;
1964 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1965 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1966 m.tx = (int)(matrix->tx*20);
1967 m.ty = (int)(matrix->ty*20);
1970 int myshapeid = getNewID(dev);
1971 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1973 swf_ShapeNew(&shape);
1974 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1975 swf_SetU16(i->tag, myshapeid);
1976 SRECT r = gfxline_getSWFbbox(line);
1977 swf_SetRect(i->tag,&r);
1978 swf_SetShapeStyles(i->tag,shape);
1979 swf_ShapeCountBits(shape,NULL,NULL);
1980 swf_SetShapeBits(i->tag,shape);
1981 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1982 i->swflastx = i->swflasty = UNDEFINED_COORD;
1983 drawgfxline(dev, line);
1984 swf_ShapeSetEnd(i->tag);
1985 swf_ShapeFree(shape);
1987 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1988 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
1989 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
1992 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
1994 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1999 if(i->clippos >= 127)
2001 msg("<warning> Too many clip levels.");
2005 int myshapeid = getNewID(dev);
2006 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2008 memset(&col, 0, sizeof(RGBA));
2010 swf_ShapeNew(&shape);
2011 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2012 swf_SetU16(i->tag,myshapeid);
2013 SRECT r = gfxline_getSWFbbox(line);
2014 swf_SetRect(i->tag,&r);
2015 swf_SetShapeStyles(i->tag,shape);
2016 swf_ShapeCountBits(shape,NULL,NULL);
2017 swf_SetShapeBits(i->tag,shape);
2018 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2019 i->swflastx = i->swflasty = UNDEFINED_COORD;
2020 drawgfxline(dev, line);
2021 swf_ShapeSetEnd(i->tag);
2022 swf_ShapeFree(shape);
2024 /* TODO: remember the bbox, and check all shapes against it */
2026 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2027 i->cliptags[i->clippos] = i->tag;
2028 i->clipshapes[i->clippos] = myshapeid;
2029 i->clipdepths[i->clippos] = getNewDepth(dev);
2033 static void swf_endclip(gfxdevice_t*dev)
2035 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2042 msg("<error> Invalid end of clipping region");
2046 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2047 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2049 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2051 static int gfxline_type(gfxline_t*line)
2057 int haszerosegments=0;
2059 if(line->type == gfx_moveTo) {
2062 } else if(line->type == gfx_lineTo) {
2066 } else if(line->type == gfx_splineTo) {
2068 if(tmpsplines>lines)
2073 if(lines==0 && splines==0) return 0;
2074 else if(lines==1 && splines==0) return 1;
2075 else if(lines==0 && splines==1) return 2;
2076 else if(splines==0) return 3;
2080 static int gfxline_has_dots(gfxline_t*line)
2087 if(line->type == gfx_moveTo) {
2088 if(isline && dist < 1) {
2093 } else if(line->type == gfx_lineTo) {
2094 dist += fabs(line->x - x) + fabs(line->y - y);
2096 } else if(line->type == gfx_splineTo) {
2097 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2098 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2105 if(isline && dist < 1) {
2111 static int gfxline_fix_short_edges(gfxline_t*line)
2115 if(line->type == gfx_lineTo) {
2116 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2119 } else if(line->type == gfx_splineTo) {
2120 if(fabs(line->sx - x) + fabs(line->sy - y) +
2121 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2132 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2134 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2135 if(x<i->min_x || x>i->max_x) return 0;
2136 if(y<i->min_y || y>i->max_y) return 0;
2140 static void show_path(ArtSVP*path)
2143 printf("Segments: %d\n", path->n_segs);
2144 for(t=0;t<path->n_segs;t++) {
2145 ArtSVPSeg* seg = &path->segs[t];
2146 printf("Segment %d: %d points, %s, BBox: (%f,%f,%f,%f)\n",
2147 t, seg->n_points, seg->dir==0?"UP ":"DOWN",
2148 seg->bbox.x0, seg->bbox.y0, seg->bbox.x1, seg->bbox.y1);
2150 for(p=0;p<seg->n_points;p++) {
2151 ArtPoint* point = &seg->points[p];
2152 printf(" (%f,%f)\n", point->x, point->y);
2158 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2160 gfxline_t*l = line = gfxline_clone(line);
2172 //#define NORMALIZE_POLYGON_POSITIONS
2174 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)
2176 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2177 int type = gfxline_type(line);
2178 int has_dots = gfxline_has_dots(line);
2179 gfxbbox_t r = gfxline_getbbox(line);
2180 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2182 /* TODO: * split line into segments, and perform this check for all segments */
2185 (width <= i->config_caplinewidth
2186 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2187 || (cap_style == gfx_capRound && type<=2))) {} else
2189 /* convert line to polygon */
2190 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2192 gfxline_fix_short_edges(line);
2193 /* we need to convert the line into a polygon */
2194 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2195 gfxline_t*gfxline = SVPtogfxline(svp);
2196 dev->fill(dev, gfxline, color);
2202 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2205 #ifdef NORMALIZE_POLYGON_POSITIONS
2207 double startx = 0, starty = 0;
2208 if(line && line->type == gfx_moveTo) {
2212 line = gfxline_move(line, -startx, -starty);
2213 i->shapeposx = (int)(startx*20);
2214 i->shapeposy = (int)(starty*20);
2217 swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2218 swfoutput_setlinewidth(dev, width);
2221 drawgfxline(dev, line);
2223 #ifdef NORMALIZE_POLYGON_POSITIONS
2224 free(line); //account for _move
2228 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2230 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2231 gfxbbox_t r = gfxline_getbbox(line);
2232 int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2235 if(!i->config_ignoredraworder)
2238 #ifdef NORMALIZE_POLYGON_POSITIONS
2240 double startx = 0, starty = 0;
2241 if(line && line->type == gfx_moveTo) {
2245 line = gfxline_move(line, -startx, -starty);
2246 i->shapeposx = (int)(startx*20);
2247 i->shapeposy = (int)(starty*20);
2250 swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2254 drawgfxline(dev, line);
2255 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2257 #ifdef NORMALIZE_POLYGON_POSITIONS
2258 free(line); //account for _move
2261 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2263 msg("<error> Gradient filling not implemented yet");
2266 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2268 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2271 swffont->version = 2;
2272 swffont->name = (U8*)strdup(id);
2273 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2274 swffont->layout->ascent = 0; /* ? */
2275 swffont->layout->descent = 0;
2276 swffont->layout->leading = 0;
2277 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2278 swffont->encoding = FONT_ENCODING_UNICODE;
2279 swffont->numchars = font->num_glyphs;
2280 swffont->maxascii = font->max_unicode;
2281 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2282 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2283 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2284 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2285 for(t=0;t<font->max_unicode;t++) {
2286 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2288 for(t=0;t<font->num_glyphs;t++) {
2291 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2292 if(font->glyphs[t].name) {
2293 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2295 swffont->glyphnames[t] = 0;
2297 swffont->glyph[t].advance = (int)(font->glyphs[t].advance * 20);
2299 swf_Shape01DrawerInit(&draw, 0);
2300 line = font->glyphs[t].line;
2303 c.x = line->sx; c.y = line->sy;
2304 to.x = line->x; to.y = line->y;
2305 if(line->type == gfx_moveTo) {
2306 draw.moveTo(&draw, &to);
2307 } else if(line->type == gfx_lineTo) {
2308 draw.lineTo(&draw, &to);
2309 } else if(line->type == gfx_splineTo) {
2310 draw.splineTo(&draw, &c, &to);
2315 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2316 swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2317 draw.dealloc(&draw);
2322 static void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2324 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2326 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2327 return; // the requested font is the current font
2329 fontlist_t*last=0,*l = i->fontlist;
2332 if(!strcmp((char*)l->swffont->name, fontid)) {
2333 return; // we already know this font
2337 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2338 l->swffont = gfxfont_to_swffont(font, fontid);
2345 swf_FontSetID(l->swffont, getNewID(i->dev));
2347 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2348 // print font information
2349 msg("<debug> Font %s",fontid);
2350 msg("<debug> | ID: %d", l->swffont->id);
2351 msg("<debug> | Version: %d", l->swffont->version);
2352 msg("<debug> | Name: %s", l->swffont->name);
2353 msg("<debug> | Numchars: %d", l->swffont->numchars);
2354 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2355 msg("<debug> | Style: %d", l->swffont->style);
2356 msg("<debug> | Encoding: %d", l->swffont->encoding);
2357 for(int iii=0; iii<l->swffont->numchars;iii++) {
2358 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,
2359 l->swffont->layout->bounds[iii].xmin/20.0,
2360 l->swffont->layout->bounds[iii].ymin/20.0,
2361 l->swffont->layout->bounds[iii].xmax/20.0,
2362 l->swffont->layout->bounds[iii].ymax/20.0
2365 for(t=0;t<l->swffont->maxascii;t++) {
2366 if(l->swffont->ascii2glyph[t] == iii)
2367 msg("<debug> | - maps to %d",t);
2373 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2375 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2377 if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2378 return; // the requested font is the current font
2380 fontlist_t*l = i->fontlist;
2382 if(!strcmp((char*)l->swffont->name, fontid)) {
2383 i->swffont = l->swffont;
2388 msg("<error> Unknown font id: %s", fontid);
2392 static void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2394 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2396 if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,fontid)) // not equal to current font
2398 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2399 with multiple fonts */
2402 swf_switchfont(dev, fontid); // set the current font
2404 swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2414 /* printf("%f %f\n", m.m31, m.m32);
2416 static int xpos = 40;
2417 static int ypos = 200;
2424 drawchar(dev, i->swffont, glyph, &m, color);