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 double config_ppmsubpixels=0;
63 double config_jpegsubpixels=0;
64 int config_opennewwindow=0;
65 int config_ignoredraworder=0;
66 int config_drawonlyshapes=0;
67 int config_jpegquality=85;
68 int config_storeallcharacters=0;
69 int config_enablezlib=0;
70 int config_insertstoptag=0;
71 int config_flashversion=5;
72 int config_splinemaxerror=1;
73 int config_fontsplinemaxerror=1;
74 int config_filloverlap=0;
76 float config_minlinewidth=0.05;
77 double config_caplinewidth=1;
79 typedef struct _swfoutput_internal
81 swfoutput*obj; // the swfoutput object where this internal struct resides
120 int pic_height[1024];
126 char fillstylechanged;
128 int jpeg; //next image type
135 chardata_t chardata[CHARDATAMAX];
140 /* during the transition to the gfxdevice interface:
141 a device which uses this swfoutput as target */
144 } swfoutput_internal;
146 void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
147 int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
148 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);
149 void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
150 void swf_endclip(gfxdevice_t*dev);
151 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);
152 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
153 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
154 void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
156 static swfoutput_internal* init_internal_struct()
158 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
159 memset(i, 0, sizeof(swfoutput_internal));
180 i->fillstylechanged = 0;
187 i->device.internal = (void*)i;
188 i->device.fillbitmap = swf_fillbitmap;
189 i->device.setparameter = swf_setparameter;
190 i->device.stroke = swf_stroke;
191 i->device.startclip = swf_startclip;
192 i->device.endclip = swf_endclip;
193 i->device.fill = swf_fill;
194 i->device.fillbitmap = swf_fillbitmap;
195 i->device.fillgradient = swf_fillgradient;
200 static void startshape(struct swfoutput* obj);
201 static void starttext(struct swfoutput* obj);
202 static void endshape(struct swfoutput* obj);
203 static void endtext(struct swfoutput* obj);
205 // matrix multiplication. changes p0
206 static void transform (plotxy*p0,struct swfmatrix*m)
209 x = m->m11*p0->x+m->m12*p0->y;
210 y = m->m21*p0->x+m->m22*p0->y;
215 // write a move-to command into the swf
216 static int moveto(struct swfoutput*obj, TAG*tag, plotxy p0)
218 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
219 int rx = (int)(p0.x*20);
220 int ry = (int)(p0.y*20);
221 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
222 swf_ShapeSetMove (tag, i->shape, rx,ry);
223 i->fillstylechanged = 0;
230 static int moveto(struct swfoutput*obj, TAG*tag, double x, double y)
232 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
236 return moveto(obj, tag, p);
238 static void addPointToBBox(struct swfoutput*obj, int px, int py)
240 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
246 swf_ExpandRect(&i->bboxrect, p);
248 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
252 /*static void plot(struct swfoutput*obj, int x, int y, TAG*tag)
254 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
255 int width = i->linewidth/4;
259 //swf_ShapeSetLine(tag, i->shape,-width,-width);
260 //swf_ShapeSetLine(tag, i->shape,width*2,0);
261 //swf_ShapeSetLine(tag, i->shape,0,width*2);
262 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
263 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
264 //swf_ShapeSetLine(tag, i->shape,width,width);
267 swf_ShapeSetLine(tag, i->shape,-width,0);
268 swf_ShapeSetLine(tag, i->shape,width,-width);
269 swf_ShapeSetLine(tag, i->shape,width,width);
270 swf_ShapeSetLine(tag, i->shape,-width,width);
271 swf_ShapeSetLine(tag, i->shape,-width,-width);
272 swf_ShapeSetLine(tag, i->shape,width,0);
274 addPointToBBox(obj, x-width ,y-width);
275 addPointToBBox(obj, x+width ,y+width);
278 // write a line-to command into the swf
279 static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0)
281 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
282 int px = (int)(p0.x*20);
283 int py = (int)(p0.y*20);
284 int rx = (px-i->swflastx);
285 int ry = (py-i->swflasty);
287 swf_ShapeSetLine (tag, i->shape, rx,ry);
288 addPointToBBox(obj, i->swflastx,i->swflasty);
289 addPointToBBox(obj, px,py);
290 }/* else if(!i->fill) {
291 // treat lines of length 0 as plots, making them
292 // at least 1 twip wide so Flash will display them
293 plot(obj, i->swflastx, i->swflasty, tag);
300 static void lineto(struct swfoutput*obj, TAG*tag, double x, double y)
308 // write a spline-to command into the swf
309 static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end)
311 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
312 int lastlastx = i->swflastx;
313 int lastlasty = i->swflasty;
315 int cx = ((int)(control.x*20)-i->swflastx);
316 int cy = ((int)(control.y*20)-i->swflasty);
319 int ex = ((int)(end.x*20)-i->swflastx);
320 int ey = ((int)(end.y*20)-i->swflasty);
324 if(cx || cy || ex || ey) {
325 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
326 addPointToBBox(obj, lastlastx ,lastlasty );
327 addPointToBBox(obj, lastlastx+cx,lastlasty+cy);
328 addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey);
329 }/* else if(!i->fill) {
330 // treat splines of length 0 as plots
331 plot(obj, lastlastx, lastlasty, tag);
336 /* write a line, given two points and the transformation
338 /*static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1)
340 moveto(obj, tag, p0);
341 lineto(obj, tag, p1);
344 void resetdrawer(struct swfoutput*obj)
346 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
351 static void stopFill(struct swfoutput*obj)
353 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
356 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
357 i->fillstylechanged = 1;
361 static void startFill(struct swfoutput*obj)
363 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
366 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
367 i->fillstylechanged = 1;
372 /* draw an outline. These are generated by pdf2swf and by t1lib
373 (representing characters). */
374 /*void drawpath(struct swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m, int log)
376 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
377 if( i->tag->id != ST_DEFINESHAPE &&
378 i->tag->id != ST_DEFINESHAPE2 &&
379 i->tag->id != ST_DEFINESHAPE3)
381 msg("<error> internal error: drawpath needs a shape tag, not %d\n",i->tag->id);
385 double lastx=0,lasty=0;
386 double firstx=0,firsty=0;
391 x += (outline->dest.x/(float)0xffff);
392 y += (outline->dest.y/(float)0xffff);
393 if(outline->type == SWF_PATHTYPE_MOVE)
395 //if(!init && fill && obj->drawmode != DRAWMODE_EOFILL && !ignoredraworder)
396 if(config_filloverlap && !init && i->fill && obj->drawmode != DRAWMODE_EOFILL) {
397 // drawmode=FILL (not EOFILL) means that
398 // seperate shapes do not cancel each other out.
399 // On SWF side, we need to start a new shape for each
400 // closed polygon, because SWF only knows EOFILL.
407 if(((int)(lastx*20) != (int)(firstx*20) ||
408 (int)(lasty*20) != (int)(firsty*20)) &&
417 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
418 line(obj,i->tag, p0, p1);
424 else if(outline->type == SWF_PATHTYPE_LINE)
432 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
433 line(obj,i->tag, p0,p1);
436 msg("<error> drawpath: unknown outline type:%d\n", outline->type);
440 outline = outline->link;
442 if(((int)(lastx*20) != (int)(firstx*20) ||
443 (int)(lasty*20) != (int)(firsty*20)) &&
452 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
453 line(obj, i->tag, p0, p1);
457 static inline int colorcompare(struct swfoutput*obj, RGBA*a,RGBA*b)
469 static SRECT getcharacterbbox(struct swfoutput*obj, SWFFONT*font)
471 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
474 memset(&r, 0, sizeof(r));
477 if(debug) printf("\n");
478 for(t=0;t<i->chardatapos;t++)
480 if(i->chardata[t].fontid != font->id) {
481 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
484 SRECT b = font->layout->bounds[i->chardata[t].charid];
485 b.xmin *= i->chardata[t].size;
486 b.ymin *= i->chardata[t].size;
487 b.xmax *= i->chardata[t].size;
488 b.ymax *= i->chardata[t].size;
493 b.xmin += i->chardata[t].x;
494 b.ymin += i->chardata[t].y;
495 b.xmax += i->chardata[t].x;
496 b.ymax += i->chardata[t].y;
498 /* until we solve the INTERNAL_SCALING problem (see below)
499 make sure the bounding box is big enough */
505 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
506 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
507 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
508 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
509 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
514 i->chardata[t].fontid,
516 i->chardata[t].charid
518 swf_ExpandRect2(&r, &b);
520 if(debug) printf("-----> (%f,%f,%f,%f)\n",
528 static void putcharacters(struct swfoutput*obj, TAG*tag)
530 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
534 color.r = i->chardata[0].color.r^255;
543 int charadvance[128];
546 int glyphbits=1; //TODO: can this be zero?
549 if(tag->id != ST_DEFINETEXT &&
550 tag->id != ST_DEFINETEXT2) {
551 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
554 if(!i->chardatapos) {
555 msg("<warning> putcharacters called with zero characters");
558 for(pass = 0; pass < 2; pass++)
568 advancebits++; // add sign bit
569 swf_SetU8(tag, glyphbits);
570 swf_SetU8(tag, advancebits);
573 for(t=0;t<=i->chardatapos;t++)
575 if(lastfontid != i->chardata[t].fontid ||
576 lastx!=i->chardata[t].x ||
577 lasty!=i->chardata[t].y ||
578 !colorcompare(obj,&color, &i->chardata[t].color) ||
580 lastsize != i->chardata[t].size ||
583 if(charstorepos && pass==0)
586 for(s=0;s<charstorepos;s++)
588 while(charids[s]>=(1<<glyphbits))
590 while(charadvance[s]>=(1<<advancebits))
594 if(charstorepos && pass==1)
596 tag->writeBit = 0; // Q&D
597 swf_SetBits(tag, 0, 1); // GLYPH Record
598 swf_SetBits(tag, charstorepos, 7); // number of glyphs
600 for(s=0;s<charstorepos;s++)
602 swf_SetBits(tag, charids[s], glyphbits);
603 swf_SetBits(tag, charadvance[s], advancebits);
608 if(pass == 1 && t<i->chardatapos)
614 if(lastx != i->chardata[t].x ||
615 lasty != i->chardata[t].y)
617 newx = i->chardata[t].x;
618 newy = i->chardata[t].y;
624 if(!colorcompare(obj,&color, &i->chardata[t].color))
626 color = i->chardata[t].color;
629 font.id = i->chardata[t].fontid;
630 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
633 tag->writeBit = 0; // Q&D
634 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
637 lastfontid = i->chardata[t].fontid;
638 lastx = i->chardata[t].x;
639 lasty = i->chardata[t].y;
640 lastsize = i->chardata[t].size;
643 if(t==i->chardatapos)
647 int nextt = t==i->chardatapos-1?t:t+1;
648 int rel = i->chardata[nextt].x-i->chardata[t].x;
649 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
651 lastx=i->chardata[nextt].x;
655 lastx=i->chardata[t].x;
657 charids[charstorepos] = i->chardata[t].charid;
658 charadvance[charstorepos] = advance;
665 static void putcharacter(struct swfoutput*obj, int fontid, int charid, int x,int y, int size, RGBA color)
667 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
668 if(i->chardatapos == CHARDATAMAX)
670 msg("<warning> Character buffer too small. SWF will be slightly bigger");
674 i->chardata[i->chardatapos].fontid = fontid;
675 i->chardata[i->chardatapos].charid = charid;
676 i->chardata[i->chardatapos].x = x;
677 i->chardata[i->chardatapos].y = y;
678 i->chardata[i->chardatapos].color = color;
679 i->chardata[i->chardatapos].size = size;
683 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
684 So if we set this value to high, the char coordinates will overflow.
685 If we set it to low, however, the char positions will be inaccurate */
686 #define FONT_INTERNAL_SIZE 4
688 /* process a character. */
689 static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m, gfxcolor_t*col)
691 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
693 msg("<warning> Font is NULL");
697 int charid = getCharID(swffont, charnr, character, u);
700 msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
701 FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
704 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
705 msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty",
706 FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
718 float det = ((m->m11*m->m22)-(m->m21*m->m12));
719 if(fabs(det) < 0.0005) {
720 /* x direction equals y direction- the text is invisible */
723 det = 20*FONT_INTERNAL_SIZE / det;
726 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
727 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
729 RGBA rgba = *(RGBA*)col;
730 putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
731 swf_FontUseGlyph(swffont, charid);
736 SWF_OUTLINE*outline = font->getOutline(character, charnr);
737 char* charname = character;
740 msg("<warning> Didn't find character '%s' (%d) in current charset (%s)",
741 FIXNULL(character),charnr,FIXNULL(font->getName()));
760 drawpath(tag, outline, &m2, 0);
765 static void endtext(swfoutput*obj)
767 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
771 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
772 swf_SetU16(i->tag, i->textid);
775 r = getcharacterbbox(obj, obj->swffont);
777 swf_SetRect(i->tag,&r);
780 swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
781 swf_SetMatrix(i->tag,&m);
783 putcharacters(obj, i->tag);
785 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
786 //swf_ObjectPlace(i->tag,i->textid,/*depth*/i->depth++,&i->page_matrix,NULL,NULL);
788 swf_MatrixJoin(&m2,&obj->fontmatrix, &i->page_matrix);
790 swf_ObjectPlace(i->tag,i->textid,/*depth*/i->depth++,&m2,NULL,NULL);
794 int getCharID(SWFFONT *font, int charnr, char *charname, int u)
797 if(charname && font->glyphnames) {
798 for(t=0;t<font->numchars;t++) {
799 if(font->glyphnames[t] && !strcmp(font->glyphnames[t],charname)) {
800 msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
804 /* if we didn't find the character, maybe
805 we can find the capitalized version */
806 for(t=0;t<font->numchars;t++) {
807 if(font->glyphnames[t] && !strcasecmp(font->glyphnames[t],charname)) {
808 msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
814 if(u>0 && font->encoding != 255) {
815 /* try to use the unicode id */
816 if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
817 msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
818 return font->ascii2glyph[u];
822 if(font->encoding != FONT_ENCODING_UNICODE) {
823 /* the following only works if the font encoding
824 is US-ASCII based. It's needed for fonts which return broken unicode
826 if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
827 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, font->ascii2glyph[charnr]);
828 return font->ascii2glyph[charnr];
832 if(charnr>=0 && charnr<font->numchars) {
833 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
841 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
842 void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
844 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
845 fontlist_t*last=0,*iterator;
847 msg("<error> No fontid");
851 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
854 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
855 with multiple fonts */
858 iterator = i->fontlist;
860 if(!strcmp((char*)iterator->swffont->name,fontid)) {
861 obj->swffont = iterator->swffont;
865 iterator = iterator->next;
869 msg("<error> No filename given for font- internal error?");
873 swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
874 SWFFONT*swffont = swf_LoadFont(filename);
877 msg("<warning> Couldn't load font %s (%s)", fontid, filename);
878 swffont = swf_LoadFont(0);
881 if(swffont->glyph2ascii) {
884 /* check whether the Unicode indices look o.k.
885 If they don't, disable the unicode lookup by setting
886 the encoding to 255 */
887 for(t=0;t<swffont->numchars;t++) {
888 int c = swffont->glyph2ascii[t];
889 if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
890 // the character maps into the unicode control character range
891 // between 0001-001f. Yet it is not empty. Treat the one
892 // mapping as broken, and look how many of those we find.
897 msg("<warning> Font %s has bad unicode mapping", fontid);
898 swffont->encoding = 255;
902 swf_FontSetID(swffont, ++i->currentswfid);
904 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
905 // print font information
906 msg("<debug> Font %s (%s)",swffont->name, filename);
907 msg("<debug> | ID: %d", swffont->id);
908 msg("<debug> | Version: %d", swffont->version);
909 msg("<debug> | Name: %s", fontid);
910 msg("<debug> | Numchars: %d", swffont->numchars);
911 msg("<debug> | Maxascii: %d", swffont->maxascii);
912 msg("<debug> | Style: %d", swffont->style);
913 msg("<debug> | Encoding: %d", swffont->encoding);
914 for(int iii=0; iii<swffont->numchars;iii++) {
915 msg("<debug> | Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, swffont->glyphnames?swffont->glyphnames[iii]:"<nonames>", swffont->glyph2ascii[iii], swffont->glyph[iii].shape->bitlen,
916 swffont->layout->bounds[iii].xmin/20.0,
917 swffont->layout->bounds[iii].ymin/20.0,
918 swffont->layout->bounds[iii].xmax/20.0,
919 swffont->layout->bounds[iii].ymax/20.0
922 for(t=0;t<swffont->maxascii;t++) {
923 if(swffont->ascii2glyph[t] == iii)
924 msg("<debug> | - maps to %d",t);
929 /* set the font name to the ID we use here */
930 if(swffont->name) free(swffont->name);
931 swffont->name = (U8*)strdup(fontid);
933 iterator = new fontlist_t;
934 iterator->swffont = swffont;
938 last->next = iterator;
940 i->fontlist = iterator;
942 obj->swffont = swffont;
945 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
947 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
948 fontlist_t *iterator = i->fontlist;
950 if(!strcmp((char*)iterator->swffont->name,fontid))
952 iterator = iterator->next;
957 /* set's the matrix which is to be applied to characters drawn by
958 swfoutput_drawchar() */
959 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
960 double m21,double m22)
962 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
963 if(obj->fontm11 == m11 &&
964 obj->fontm12 == m12 &&
965 obj->fontm21 == m21 &&
976 m.sx = (U32)(((obj->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((obj->fontm12)*65536)/FONT_INTERNAL_SIZE);
977 m.r0 = (U32)(((obj->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((obj->fontm22)*65536)/FONT_INTERNAL_SIZE);
983 /* draws a character at x,y. */
984 int swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr, int u, gfxcolor_t* color)
986 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
988 m.m11 = obj->fontm11;
989 m.m12 = obj->fontm12;
990 m.m21 = obj->fontm21;
991 m.m22 = obj->fontm22;
994 return drawchar(obj, obj->swffont, character, charnr, u, &m, color);
997 static void endpage(struct swfoutput*obj)
999 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1005 swfoutput_endclip(obj);
1006 i->pagefinished = 1;
1009 void swfoutput_pagefeed(struct swfoutput*obj)
1011 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1013 if(!i->pagefinished)
1016 if(config_insertstoptag) {
1018 atag = action_Stop(atag);
1019 atag = action_End(atag);
1020 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
1021 swf_ActionSet(i->tag,atag);
1023 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1026 for(i->depth--;i->depth>=i->startdepth;i->depth--) {
1027 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
1028 swf_SetU16(i->tag,i->depth);
1030 i->depth = i->startdepth;
1033 static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
1035 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1037 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1041 int shapeid = ++i->currentswfid;
1046 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
1048 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
1049 swf_SetU16(i->tag,shapeid);
1050 swf_SetRect(i->tag,&r);
1051 swf_SetShapeHeader(i->tag,s);
1052 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
1053 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
1054 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
1055 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
1056 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
1057 swf_ShapeSetEnd(i->tag);
1059 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1060 swf_ObjectPlace(i->tag,shapeid,i->depth++,0,0,0);
1061 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1062 swf_ObjectPlaceClip(i->tag,shapeid,i->depth++,0,0,0,65535);
1063 i->cliptag = i->tag;
1066 void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey, int x1, int y1, int x2, int y2)
1068 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1069 if(!i->firstpage && !i->pagefinished)
1072 swf_GetMatrix(0, &i->page_matrix);
1073 i->page_matrix.tx = movex*20;
1074 i->page_matrix.ty = movey*20;
1076 if(i->cliptag && i->frameno == i->lastframeno) {
1078 swf_GetPlaceObject(i->cliptag, &obj);
1079 obj.clipdepth = i->depth++;
1080 swf_ResetTag(i->cliptag, i->cliptag->id);
1081 swf_SetPlaceObject(i->cliptag, &obj);
1082 swf_PlaceObjectFree(&obj);
1090 msg("<notice> processing page %d (%dx%d:%d:%d)", pageNum,x2-x1,y2-y1, x1, y1);
1092 x1*=20;y1*=20;x2*=20;y2*=20;
1094 /* set clipping/background rectangle */
1095 /* TODO: this should all be done in SWFOutputDev */
1096 setBackground(obj, x1, y1, x2, y2);
1098 /* increase SWF's bounding box */
1104 swf_ExpandRect2(&i->swf.movieSize, &r);
1106 i->lastframeno = i->frameno;
1108 i->pagefinished = 0;
1111 /* initialize the swf writer */
1112 void swfoutput_init(struct swfoutput* obj)
1114 memset(obj, 0, sizeof(struct swfoutput));
1115 obj->internal = init_internal_struct();
1116 ((swfoutput_internal*)obj->internal)->obj = obj;
1118 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1123 msg("<verbose> initializing swf output for size %d*%d\n", i->max_x,i->max_y);
1127 memset(&i->swf,0x00,sizeof(SWF));
1129 i->swf.fileVersion = config_flashversion;
1130 i->swf.frameRate = 0x0040; // 1 frame per 4 seconds
1131 i->swf.movieSize.xmin = 0;
1132 i->swf.movieSize.ymin = 0;
1133 i->swf.movieSize.xmax = 0;
1134 i->swf.movieSize.ymax = 0;
1136 i->swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1137 i->tag = i->swf.firstTag;
1138 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1139 swf_SetRGB(i->tag,&rgb);
1141 i->startdepth = i->depth = 3; /* leave room for clip and background rectangle */
1144 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1147 static void startshape(struct swfoutput*obj)
1149 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1158 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1160 swf_ShapeNew(&i->shape);
1161 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&obj->strokergb);
1162 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb);
1164 i->shapeid = ++i->currentswfid;
1166 msg("<debug> Using shape id %d", i->shapeid);
1168 swf_SetU16(i->tag,i->shapeid); // ID
1170 i->bboxrectpos = i->tag->len;
1174 r.xmax = 20*i->max_x;
1175 r.ymax = 20*i->max_y;
1176 swf_SetRect(i->tag,&r);
1178 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
1180 swf_SetShapeStyles(i->tag,i->shape);
1181 swf_ShapeCountBits(i->shape,NULL,NULL);
1182 swf_SetShapeBits(i->tag,i->shape);
1184 /* TODO: do we really need this? */
1185 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1186 i->swflastx=i->swflasty=0;
1188 i->shapeisempty = 1;
1191 static void starttext(struct swfoutput*obj)
1193 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1197 i->textid = ++i->currentswfid;
1199 i->swflastx=i->swflasty=0;
1203 /* TODO: move to ../lib/rfxswf */
1204 void changeRect(struct swfoutput*obj, TAG*tag, int pos, SRECT*newrect)
1206 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1207 /* determine length of old rect */
1211 swf_GetRect(tag, &old);
1212 swf_ResetReadBits(tag);
1213 int pos_end = tag->pos;
1215 int len = tag->len - pos_end;
1216 U8*data = (U8*)malloc(len);
1217 memcpy(data, &tag->data[pos_end], len);
1220 swf_SetRect(tag, newrect);
1221 swf_SetBlock(tag, data, len);
1223 tag->pos = tag->readBit = 0;
1226 void cancelshape(swfoutput*obj)
1228 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1229 /* delete old shape tag */
1231 i->tag = i->tag->prev;
1232 swf_DeleteTag(todel);
1233 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1235 i->bboxrectpos = -1;
1238 void fixAreas(swfoutput*obj)
1240 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1241 if(!i->shapeisempty && i->fill &&
1242 (i->bboxrect.xmin == i->bboxrect.xmax ||
1243 i->bboxrect.ymin == i->bboxrect.ymax) &&
1244 config_minlinewidth >= 0.001
1246 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1247 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1248 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1251 SRECT r = i->bboxrect;
1253 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1254 /* this thing comes down to a single dot- nothing to fix here */
1260 RGBA save_col = obj->strokergb;
1261 int save_width = i->linewidth;
1263 obj->strokergb = obj->fillrgb;
1264 i->linewidth = (int)(config_minlinewidth*20);
1265 if(i->linewidth==0) i->linewidth = 1;
1269 moveto(obj, i->tag, r.xmin/20.0,r.ymin/20.0);
1270 lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0);
1272 obj->strokergb = save_col;
1273 i->linewidth = save_width;
1278 static void endshape_noput(swfoutput*obj)
1280 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1283 //changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1286 swf_ShapeFree(i->shape);
1292 static void endshape(swfoutput*obj)
1294 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1300 if(i->shapeisempty ||
1302 (i->bboxrect.xmin == i->bboxrect.xmax &&
1303 i->bboxrect.ymin == i->bboxrect.ymax))
1305 // delete the shape again, we didn't do anything
1306 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1307 i->bboxrect.xmin /20.0,
1308 i->bboxrect.ymin /20.0,
1309 i->bboxrect.xmax /20.0,
1310 i->bboxrect.ymax /20.0
1316 swf_ShapeSetEnd(i->tag);
1318 changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1320 msg("<trace> Placing shape id %d", i->shapeid);
1322 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1323 swf_ObjectPlace(i->tag,i->shapeid,/*depth*/i->depth++,&i->page_matrix,NULL,NULL);
1325 swf_ShapeFree(i->shape);
1328 i->bboxrectpos = -1;
1334 sprintf(text, "id%d", i->shapeid);
1336 points[0].x = i->bboxrect.xmin;
1337 points[0].y = i->bboxrect.ymin;
1338 points[1].x = i->bboxrect.xmax;
1339 points[1].y = i->bboxrect.ymin;
1340 points[2].x = i->bboxrect.xmax;
1341 points[2].y = i->bboxrect.ymax;
1342 points[3].x = i->bboxrect.xmin;
1343 points[3].y = i->bboxrect.ymax;
1344 swfoutput_namedlink(obj,text,points);
1348 void swfoutput_finalize(struct swfoutput*obj)
1350 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1352 if(i->tag && i->tag->id == ST_END)
1353 return; //already done
1355 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1356 swfoutput_pagefeed(obj);
1359 fontlist_t *tmp,*iterator = i->fontlist;
1361 TAG*mtag = i->swf.firstTag;
1362 if(iterator->swffont) {
1363 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1364 /*if(!storeallcharacters)
1365 swf_FontReduce(iterator->swffont);*/
1366 swf_FontSetDefine2(mtag, iterator->swffont);
1369 iterator = iterator->next;
1371 i->tag = swf_InsertTag(i->tag,ST_END);
1372 TAG* tag = i->tag->prev;
1374 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1375 and the ST_END- they confuse the flash player */
1376 while(tag->id == ST_REMOVEOBJECT2) {
1377 TAG* prev = tag->prev;
1383 SWF* swfoutput_get(struct swfoutput*obj)
1385 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1387 swfoutput_finalize(obj);
1389 return swf_CopySWF(&i->swf);
1392 void swfoutput_getdimensions(struct swfoutput*obj, int*x1, int*y1, int*x2, int*y2)
1394 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1395 if(x1) *x1 = i->swf.movieSize.xmin/20;
1396 if(y1) *y1 = i->swf.movieSize.ymin/20;
1397 if(x2) *x2 = i->swf.movieSize.xmax/20;
1398 if(y2) *y2 = i->swf.movieSize.ymax/20;
1401 int swfoutput_save(struct swfoutput* obj, char*filename)
1403 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1404 swfoutput_finalize(obj);
1408 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1413 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1417 if(config_enablezlib || config_flashversion>=6) {
1418 if FAILED(swf_WriteSWC(fi,&i->swf))
1419 msg("<error> WriteSWC() failed.\n");
1421 if FAILED(swf_WriteSWF(fi,&i->swf))
1422 msg("<error> WriteSWF() failed.\n");
1427 msg("<notice> SWF written\n");
1431 /* Perform cleaning up, complete the swf, and write it out. */
1432 void swfoutput_destroy(struct swfoutput* obj)
1434 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1436 /* not initialized yet- nothing to destroy */
1440 fontlist_t *tmp,*iterator = i->fontlist;
1442 if(iterator->swffont) {
1443 swf_FontFree(iterator->swffont);iterator->swffont=0;
1446 iterator = iterator->next;
1449 swf_FreeTags(&i->swf);
1452 memset(obj, 0, sizeof(swfoutput));
1455 static void swfoutput_setfillcolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1457 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1458 if(obj->fillrgb.r == r &&
1459 obj->fillrgb.g == g &&
1460 obj->fillrgb.b == b &&
1461 obj->fillrgb.a == a) return;
1471 static void swfoutput_setstrokecolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1473 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1474 if(obj->strokergb.r == r &&
1475 obj->strokergb.g == g &&
1476 obj->strokergb.b == b &&
1477 obj->strokergb.a == a) return;
1481 obj->strokergb.r = r;
1482 obj->strokergb.g = g;
1483 obj->strokergb.b = b;
1484 obj->strokergb.a = a;
1487 static void swfoutput_setlinewidth(struct swfoutput*obj, double _linewidth)
1489 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1490 if(i->linewidth == (U16)(_linewidth*20))
1495 i->linewidth = (U16)(_linewidth*20);
1499 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1501 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1503 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1505 if(!strncmp("http://pdf2swf:", url, 15)) {
1506 char*tmp = strdup(url);
1507 int l = strlen(tmp);
1510 swfoutput_namedlink(obj, tmp+15, points);
1520 if(config_opennewwindow)
1521 actions = action_GetUrl(0, url, "_parent");
1523 actions = action_GetUrl(0, url, "_this");
1524 actions = action_End(actions);
1526 drawlink(obj, actions, 0, points,0);
1528 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1530 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1538 actions = action_GotoFrame(0, page);
1539 actions = action_End(actions);
1541 drawlink(obj, actions, 0, points,0);
1544 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1545 of the viewer objects, like subtitles, index elements etc.
1547 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1549 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1550 ActionTAG *actions1,*actions2;
1551 char*tmp = strdup(name);
1559 if(!strncmp(tmp, "call:", 5))
1561 char*x = strchr(&tmp[5], ':');
1563 actions1 = action_PushInt(0, 0); //number of parameters (0)
1564 actions1 = action_PushString(actions1, &tmp[5]); //function name
1565 actions1 = action_CallFunction(actions1);
1568 actions1 = action_PushString(0, x+1); //parameter
1569 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1570 actions1 = action_PushString(actions1, &tmp[5]); //function name
1571 actions1 = action_CallFunction(actions1);
1573 actions2 = action_End(0);
1578 actions1 = action_PushString(0, "/:subtitle");
1579 actions1 = action_PushString(actions1, name);
1580 actions1 = action_SetVariable(actions1);
1581 actions1 = action_End(actions1);
1583 actions2 = action_PushString(0, "/:subtitle");
1584 actions2 = action_PushString(actions2, "");
1585 actions2 = action_SetVariable(actions2);
1586 actions2 = action_End(actions2);
1589 drawlink(obj, actions1, actions2, points,mouseover);
1591 swf_ActionFree(actions1);
1592 swf_ActionFree(actions2);
1596 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1598 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1603 struct plotxy p1,p2,p3,p4;
1607 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1611 int buttonid = ++i->currentswfid;
1614 if(points[t].x>xmax) xmax=points[t].x;
1615 if(points[t].y>ymax) ymax=points[t].y;
1616 if(points[t].x<xmin) xmin=points[t].x;
1617 if(points[t].y<ymin) ymin=points[t].y;
1620 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1621 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1623 /* the following code subtracts the upper left edge from all coordinates,
1624 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1625 Necessary for preprocessing with swfcombine. */
1626 posx = xmin; posy = ymin;
1627 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1628 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1629 xmin -= posx; ymin -= posy;
1630 xmax -= posx; ymax -= posy;
1633 myshapeid = ++i->currentswfid;
1634 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1635 swf_ShapeNew(&i->shape);
1636 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1637 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1638 swf_SetU16(i->tag, myshapeid);
1639 r.xmin = (int)(xmin*20);
1640 r.ymin = (int)(ymin*20);
1641 r.xmax = (int)(xmax*20);
1642 r.ymax = (int)(ymax*20);
1643 swf_SetRect(i->tag,&r);
1644 swf_SetShapeStyles(i->tag,i->shape);
1645 swf_ShapeCountBits(i->shape,NULL,NULL);
1646 swf_SetShapeBits(i->tag,i->shape);
1647 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1648 i->swflastx = i->swflasty = 0;
1649 moveto(obj, i->tag, p1);
1650 lineto(obj, i->tag, p2);
1651 lineto(obj, i->tag, p3);
1652 lineto(obj, i->tag, p4);
1653 lineto(obj, i->tag, p1);
1654 swf_ShapeSetEnd(i->tag);
1657 myshapeid2 = ++i->currentswfid;
1658 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1659 swf_ShapeNew(&i->shape);
1660 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1662 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1663 swf_SetU16(i->tag, myshapeid2);
1664 r.xmin = (int)(xmin*20);
1665 r.ymin = (int)(ymin*20);
1666 r.xmax = (int)(xmax*20);
1667 r.ymax = (int)(ymax*20);
1668 swf_SetRect(i->tag,&r);
1669 swf_SetShapeStyles(i->tag,i->shape);
1670 swf_ShapeCountBits(i->shape,NULL,NULL);
1671 swf_SetShapeBits(i->tag,i->shape);
1672 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1673 i->swflastx = i->swflasty = 0;
1674 moveto(obj, i->tag, p1);
1675 lineto(obj, i->tag, p2);
1676 lineto(obj, i->tag, p3);
1677 lineto(obj, i->tag, p4);
1678 lineto(obj, i->tag, p1);
1679 swf_ShapeSetEnd(i->tag);
1683 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1684 swf_SetU16(i->tag,buttonid); //id
1685 swf_ButtonSetFlags(i->tag, 0); //menu=no
1686 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1687 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1688 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1689 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1690 swf_SetU8(i->tag,0);
1691 swf_ActionSet(i->tag,actions1);
1692 swf_SetU8(i->tag,0);
1696 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1697 swf_SetU16(i->tag,buttonid); //id
1698 swf_ButtonSetFlags(i->tag, 0); //menu=no
1699 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1700 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1701 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1702 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1703 swf_SetU8(i->tag,0); // end of button records
1704 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1705 swf_ActionSet(i->tag,actions1);
1707 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1708 swf_ActionSet(i->tag,actions2);
1709 swf_SetU8(i->tag,0);
1710 swf_ButtonPostProcess(i->tag, 2);
1712 swf_SetU8(i->tag,0);
1713 swf_ButtonPostProcess(i->tag, 1);
1717 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1719 if(posx!=0 || posy!=0) {
1721 p.x = (int)(posx*20);
1722 p.y = (int)(posy*20);
1723 p = swf_TurnPoint(p, &i->page_matrix);
1728 swf_ObjectPlace(i->tag, buttonid, i->depth++,&m,0,0);
1731 swf_ObjectPlace(i->tag, buttonid, i->depth++,&i->page_matrix,0,0);
1738 for(t=0;t<picpos;t++)
1740 if(pic_xids[t] == xid &&
1741 pic_yids[t] == yid) {
1742 width = pic_width[t];
1743 height = pic_height[t];
1747 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1748 pic_xids[picpos] = xid;
1749 pic_yids[picpos] = yid;
1750 pic_width[picpos] = width;
1751 pic_height[picpos] = height;
1754 pic[width*y+x] = buf[0];
1758 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1759 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1763 xid += x*r+x*b*3+x*g*7+x*a*11;
1764 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1766 for(t=0;t<picpos;t++)
1768 if(pic_xids[t] == xid &&
1769 pic_yids[t] == yid) {
1777 static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
1779 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1780 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1785 /* check whether the next segment is zero */
1786 if(line->type == gfx_moveTo) {
1787 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1788 moveto(obj, i->tag, line->x, line->y);
1789 px = lastx = line->x;
1790 py = lasty = line->y;
1792 } if(line->type == gfx_lineTo) {
1793 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1794 lineto(obj, i->tag, line->x, line->y);
1798 } else if(line->type == gfx_splineTo) {
1799 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1801 s.x = line->sx;p.x = line->x;
1802 s.y = line->sy;p.y = line->y;
1803 splineto(obj, i->tag, s, p);
1812 void swfoutput_drawgfxline(struct swfoutput*obj, gfxline_t*line, gfxcoord_t width, gfxcolor_t*col, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
1814 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1815 gfxdevice_t*dev = &i->device;
1816 dev->stroke(dev, line, width, col, cap_style, joint_style, miterLimit);
1818 void swfoutput_fillgfxline(struct swfoutput*obj, gfxline_t*line, gfxcolor_t*col)
1820 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1821 gfxdevice_t*dev = &i->device;
1822 dev->fill(dev, line, col);
1824 void swfoutput_startclip(struct swfoutput*obj, gfxline_t*line)
1826 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1827 gfxdevice_t*dev = &i->device;
1828 dev->startclip(dev, line);
1830 void swfoutput_endclip(struct swfoutput*obj)
1832 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1833 gfxdevice_t*dev = &i->device;
1837 #define IMAGE_TYPE_JPEG 0
1838 #define IMAGE_TYPE_LOSSLESS 1
1840 static void swfoutput_drawimage(struct swfoutput*obj, RGBA* data, int sizex,int sizey,
1841 double x1,double y1,
1842 double x2,double y2,
1843 double x3,double y3,
1844 double x4,double y4, int type)
1846 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1850 double l1 = sqrt((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1));
1851 double l2 = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
1853 gfxline_t p1,p2,p3,p4,p5;
1854 p1.type=gfx_moveTo;p1.x=x1; p1.y=y1;p1.next=&p2;
1855 p2.type=gfx_lineTo;p2.x=x2; p2.y=y2;p2.next=&p3;
1856 p3.type=gfx_lineTo;p3.x=x3; p3.y=y3;p3.next=&p4;
1857 p4.type=gfx_lineTo;p4.x=x4; p4.y=y4;p4.next=&p5;
1858 p5.type=gfx_lineTo;p5.x=x1; p5.y=y1;p5.next=0;
1860 {p1.x = (int)(p1.x*20)/20.0;
1861 p1.y = (int)(p1.y*20)/20.0;
1862 p2.x = (int)(p2.x*20)/20.0;
1863 p2.y = (int)(p2.y*20)/20.0;
1864 p3.x = (int)(p3.x*20)/20.0;
1865 p3.y = (int)(p3.y*20)/20.0;
1866 p4.x = (int)(p4.x*20)/20.0;
1867 p4.y = (int)(p4.y*20)/20.0;
1868 p5.x = (int)(p5.x*20)/20.0;
1869 p5.y = (int)(p5.y*20)/20.0;
1876 m.m00 = (p4.x-p1.x)/sizex; m.m10 = (p2.x-p1.x)/sizey;
1877 m.m01 = (p4.y-p1.y)/sizex; m.m11 = (p2.y-p1.y)/sizey;
1882 img.data = (gfxcolor_t*)data;
1886 if(type == IMAGE_TYPE_JPEG)
1887 /* TODO: pass image_dpi to device instead */
1888 i->device.setparameter(&i->device, "next_bitmap_is_jpeg", "1");
1890 i->device.fillbitmap(&i->device, &p1, &img, &m, 0);
1893 void swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1894 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
1896 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_JPEG);
1899 void swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1900 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
1902 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_LOSSLESS);
1905 void swfoutput_setparameter(char*name, char*value)
1907 if(!strcmp(name, "jpegsubpixels")) {
1908 config_jpegsubpixels = atof(value);
1909 } else if(!strcmp(name, "ppmsubpixels")) {
1910 config_ppmsubpixels = atof(value);
1911 } else if(!strcmp(name, "drawonlyshapes")) {
1912 config_drawonlyshapes = atoi(value);
1913 } else if(!strcmp(name, "ignoredraworder")) {
1914 config_ignoredraworder = atoi(value);
1915 } else if(!strcmp(name, "filloverlap")) {
1916 config_filloverlap = atoi(value);
1917 } else if(!strcmp(name, "linksopennewwindow")) {
1918 config_opennewwindow = atoi(value);
1919 } else if(!strcmp(name, "opennewwindow")) {
1920 config_opennewwindow = atoi(value);
1921 } else if(!strcmp(name, "storeallcharacters")) {
1922 config_storeallcharacters = atoi(value);
1923 } else if(!strcmp(name, "enablezlib")) {
1924 config_enablezlib = atoi(value);
1925 } else if(!strcmp(name, "insertstop")) {
1926 config_insertstoptag = atoi(value);
1927 } else if(!strcmp(name, "protected")) {
1928 config_protect = atoi(value);
1929 } else if(!strcmp(name, "flashversion")) {
1930 config_flashversion = atoi(value);
1931 } else if(!strcmp(name, "minlinewidth")) {
1932 config_minlinewidth = atof(value);
1933 } else if(!strcmp(name, "caplinewidth")) {
1934 config_caplinewidth = atof(value);
1935 } else if(!strcmp(name, "jpegquality")) {
1936 int val = atoi(value);
1938 if(val>100) val=100;
1939 config_jpegquality = val;
1940 } else if(!strcmp(name, "splinequality")) {
1941 int v = atoi(value);
1942 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1944 config_splinemaxerror = v;
1945 } else if(!strcmp(name, "fontquality")) {
1946 int v = atoi(value);
1947 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1949 config_fontsplinemaxerror = v;
1951 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1955 // --------------------------------------------------------------------
1957 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1960 swf_GetCXForm(0, &cx, 1);
1963 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1964 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1965 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1966 c->ar!=0 || c->ag!=0 || c->ab!=0)
1967 msg("<warning> CXForm not SWF-compatible");
1969 cx.a0 = (S16)(c->aa*256);
1970 cx.r0 = (S16)(c->rr*256);
1971 cx.g0 = (S16)(c->gg*256);
1972 cx.b0 = (S16)(c->bb*256);
1980 ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
1982 ArtVpath *vec = NULL;
1990 if(l2->type == gfx_moveTo) {
1992 } if(l2->type == gfx_lineTo) {
1994 } if(l2->type == gfx_splineTo) {
1995 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
1996 if(!parts) parts = 1;
2006 vec = art_new (ArtVpath, len);
2011 if(l2->type == gfx_moveTo) {
2012 vec[pos].code = ART_MOVETO;
2017 } else if(l2->type == gfx_lineTo) {
2018 vec[pos].code = ART_LINETO;
2023 } else if(l2->type == gfx_splineTo) {
2025 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2026 if(!parts) parts = 1;
2027 for(i=0;i<=parts;i++) {
2028 double t = (double)i/(double)parts;
2029 vec[pos].code = ART_LINETO;
2030 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
2031 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
2040 vec[pos].code = ART_END;
2042 svp = art_svp_vpath_stroke (vec,
2043 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
2044 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
2045 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
2046 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
2047 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
2048 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
2050 miterLimit, //miter_limit
2057 gfxline_t* SVPtogfxline(ArtSVP*svp)
2062 for(t=0;t<svp->n_segs;t++) {
2063 size += svp->segs[t].n_points + 1;
2065 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
2067 for(t=0;t<svp->n_segs;t++) {
2068 ArtSVPSeg* seg = &svp->segs[t];
2070 for(p=0;p<seg->n_points;p++) {
2071 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
2072 ArtPoint* point = &seg->points[p];
2073 lines[pos].x = point->x;
2074 lines[pos].y = point->y;
2075 lines[pos].next = &lines[pos+1];
2080 lines[pos-1].next = 0;
2087 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
2090 RGBA*mem = (RGBA*)img->data;
2091 int bitid = ++i->currentswfid;
2093 int sizex = img->width;
2094 int sizey = img->height;
2095 int is_jpeg = i->jpeg;
2098 int newsizex=sizex, newsizey=sizey;
2101 if(is_jpeg && config_jpegsubpixels) {
2102 newsizex = (int)(targetwidth*config_jpegsubpixels+0.5);
2103 newsizey = (int)(targetheight*config_jpegsubpixels+0.5);
2104 } else if(!is_jpeg && config_ppmsubpixels) {
2105 newsizex = (int)(targetwidth*config_ppmsubpixels+0.5);
2106 newsizey = (int)(targetheight*config_ppmsubpixels+0.5);
2110 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
2113 /* TODO: cache images */
2117 if(newsizex<sizex || newsizey<sizey) {
2118 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
2119 *newwidth = sizex = newsizex;
2120 *newheight = sizey = newsizey;
2125 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
2126 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
2128 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
2130 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
2133 targetwidth, targetheight,
2134 /*newsizex, newsizey,*/
2135 num_colors>256?">":"", num_colors>256?256:num_colors);
2137 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
2138 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
2140 for(t=0;t<num_colors;t++) {
2141 printf("%02x%02x%02x%02x ",
2142 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
2148 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, config_jpegquality);
2156 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2158 gfxbbox_t bbox = gfxline_getbbox(line);
2160 r.xmin = (int)(bbox.xmin*20);
2161 r.ymin = (int)(bbox.ymin*20);
2162 r.xmax = (int)(bbox.xmax*20);
2163 r.ymax = (int)(bbox.ymax*20);
2167 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2169 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2170 swfoutput*obj = i->obj;
2175 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2176 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2178 int newwidth=0,newheight=0;
2179 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2182 double fx = (double)img->width / (double)newwidth;
2183 double fy = (double)img->height / (double)newheight;
2188 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2189 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2190 m.tx = (int)(matrix->tx*20);
2191 m.ty = (int)(matrix->ty*20);
2194 int myshapeid = ++i->currentswfid;
2195 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2197 swf_ShapeNew(&shape);
2198 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2199 swf_SetU16(i->tag, myshapeid);
2200 SRECT r = gfxline_getSWFbbox(line);
2201 swf_SetRect(i->tag,&r);
2202 swf_SetShapeStyles(i->tag,shape);
2203 swf_ShapeCountBits(shape,NULL,NULL);
2204 swf_SetShapeBits(i->tag,shape);
2205 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2206 i->swflastx = i->swflasty = UNDEFINED_COORD;
2207 drawgfxline(obj, line);
2208 swf_ShapeSetEnd(i->tag);
2209 swf_ShapeFree(shape);
2211 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2212 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2213 swf_ObjectPlace(i->tag,myshapeid,/*depth*/i->depth++,&i->page_matrix,&cxform2,NULL);
2216 void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2218 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2219 swfoutput*obj = i->obj;
2224 if(i->clippos >= 127)
2226 msg("<warning> Too many clip levels.");
2230 int myshapeid = ++i->currentswfid;
2231 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2233 memset(&col, 0, sizeof(RGBA));
2235 swf_ShapeNew(&shape);
2236 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2237 swf_SetU16(i->tag,myshapeid);
2238 SRECT r = gfxline_getSWFbbox(line);
2239 swf_SetRect(i->tag,&r);
2240 swf_SetShapeStyles(i->tag,shape);
2241 swf_ShapeCountBits(shape,NULL,NULL);
2242 swf_SetShapeBits(i->tag,shape);
2243 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2244 i->swflastx = i->swflasty = UNDEFINED_COORD;
2245 drawgfxline(obj, line);
2246 swf_ShapeSetEnd(i->tag);
2247 swf_ShapeFree(shape);
2249 /* TODO: remember the bbox, and check all shapes against it */
2251 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2252 i->cliptags[i->clippos] = i->tag;
2253 i->clipshapes[i->clippos] = myshapeid;
2254 i->clipdepths[i->clippos] = i->depth++;
2258 void swf_endclip(gfxdevice_t*dev)
2260 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2261 swfoutput*obj = i->obj;
2268 msg("<error> Invalid end of clipping region");
2272 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth++);
2274 int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
2276 if(!strcmp(key, "next_bitmap_is_jpeg")) {
2277 ((swfoutput_internal*)dev->internal)->jpeg = 1;
2283 int gfxline_type(gfxline_t*line)
2289 int haszerosegments=0;
2291 if(line->type == gfx_moveTo) {
2294 } else if(line->type == gfx_lineTo) {
2298 } else if(line->type == gfx_splineTo) {
2300 if(tmpsplines>lines)
2305 if(lines==0 && splines==0) return 0;
2306 else if(lines==1 && splines==0) return 1;
2307 else if(lines==0 && splines==1) return 2;
2308 else if(splines==0) return 3;
2312 int gfxline_has_dots(gfxline_t*line)
2319 if(line->type == gfx_moveTo) {
2320 if(isline && dist < 1) {
2325 } else if(line->type == gfx_lineTo) {
2326 dist += fabs(line->x - x) + fabs(line->y - y);
2328 } else if(line->type == gfx_splineTo) {
2329 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2330 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2337 if(isline && dist < 1) {
2343 int gfxline_fix_short_edges(gfxline_t*line)
2347 if(line->type == gfx_lineTo) {
2348 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2351 } else if(line->type == gfx_splineTo) {
2352 if(fabs(line->sx - x) + fabs(line->sy - y) +
2353 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2366 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)
2368 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2369 swfoutput*obj = i->obj;
2370 int type = gfxline_type(line);
2371 int has_dots = gfxline_has_dots(line);
2373 /* TODO: * split line into segments, and perform this check for all segments */
2375 (width <= config_caplinewidth
2376 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2377 || (cap_style == gfx_capRound && type<=2))) {
2378 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2380 swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
2381 swfoutput_setlinewidth(obj, width);
2384 drawgfxline(obj, line);
2386 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2388 gfxline_fix_short_edges(line);
2389 /* we need to convert the line into a polygon */
2390 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2391 gfxline_t*gfxline = SVPtogfxline(svp);
2392 dev->fill(dev, gfxline, color);
2395 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2397 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2398 swfoutput*obj = i->obj;
2400 if(!config_ignoredraworder)
2402 swfoutput_setfillcolor(obj, color->r, color->g, color->b, color->a);
2406 drawgfxline(obj, line);
2407 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2409 void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2411 msg("<error> Gradient filling not implemented yet");