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, float x, float 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 // write a line-to command into the swf
253 static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0)
255 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
256 int px = (int)(p0.x*20);
257 int py = (int)(p0.y*20);
258 int rx = (px-i->swflastx);
259 int ry = (py-i->swflasty);
260 /* we can't skip this for rx=0,ry=0, those
262 swf_ShapeSetLine (tag, i->shape, rx,ry);
264 addPointToBBox(obj, i->swflastx,i->swflasty);
265 addPointToBBox(obj, px,py);
271 static void lineto(struct swfoutput*obj, TAG*tag, double x, double y)
279 // write a spline-to command into the swf
280 static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end)
282 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
283 int lastlastx = i->swflastx;
284 int lastlasty = i->swflasty;
286 int cx = ((int)(control.x*20)-i->swflastx);
287 int cy = ((int)(control.y*20)-i->swflasty);
290 int ex = ((int)(end.x*20)-i->swflastx);
291 int ey = ((int)(end.y*20)-i->swflasty);
295 if(cx || cy || ex || ey) {
296 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
297 addPointToBBox(obj, lastlastx ,lastlasty );
298 addPointToBBox(obj, lastlastx+cx,lastlasty+cy);
299 addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey);
304 /* write a line, given two points and the transformation
306 static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1)
308 moveto(obj, tag, p0);
309 lineto(obj, tag, p1);
312 void resetdrawer(struct swfoutput*obj)
314 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
319 static void stopFill(struct swfoutput*obj)
321 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
324 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
325 i->fillstylechanged = 1;
329 static void startFill(struct swfoutput*obj)
331 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
334 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
335 i->fillstylechanged = 1;
340 /* draw an outline. These are generated by pdf2swf and by t1lib
341 (representing characters). */
342 /*void drawpath(struct swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m, int log)
344 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
345 if( i->tag->id != ST_DEFINESHAPE &&
346 i->tag->id != ST_DEFINESHAPE2 &&
347 i->tag->id != ST_DEFINESHAPE3)
349 msg("<error> internal error: drawpath needs a shape tag, not %d\n",i->tag->id);
353 double lastx=0,lasty=0;
354 double firstx=0,firsty=0;
359 x += (outline->dest.x/(float)0xffff);
360 y += (outline->dest.y/(float)0xffff);
361 if(outline->type == SWF_PATHTYPE_MOVE)
363 //if(!init && fill && obj->drawmode != DRAWMODE_EOFILL && !ignoredraworder)
364 if(config_filloverlap && !init && i->fill && obj->drawmode != DRAWMODE_EOFILL) {
365 // drawmode=FILL (not EOFILL) means that
366 // seperate shapes do not cancel each other out.
367 // On SWF side, we need to start a new shape for each
368 // closed polygon, because SWF only knows EOFILL.
375 if(((int)(lastx*20) != (int)(firstx*20) ||
376 (int)(lasty*20) != (int)(firsty*20)) &&
385 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
386 line(obj,i->tag, p0, p1);
392 else if(outline->type == SWF_PATHTYPE_LINE)
400 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
401 line(obj,i->tag, p0,p1);
404 msg("<error> drawpath: unknown outline type:%d\n", outline->type);
408 outline = outline->link;
410 if(((int)(lastx*20) != (int)(firstx*20) ||
411 (int)(lasty*20) != (int)(firsty*20)) &&
420 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
421 line(obj, i->tag, p0, p1);
425 static inline int colorcompare(struct swfoutput*obj, RGBA*a,RGBA*b)
437 static SRECT getcharacterbbox(struct swfoutput*obj, SWFFONT*font)
439 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
442 memset(&r, 0, sizeof(r));
445 if(debug) printf("\n");
446 for(t=0;t<i->chardatapos;t++)
448 if(i->chardata[t].fontid != font->id) {
449 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
452 SRECT b = font->layout->bounds[i->chardata[t].charid];
453 b.xmin *= i->chardata[t].size;
454 b.ymin *= i->chardata[t].size;
455 b.xmax *= i->chardata[t].size;
456 b.ymax *= i->chardata[t].size;
461 b.xmin += i->chardata[t].x;
462 b.ymin += i->chardata[t].y;
463 b.xmax += i->chardata[t].x;
464 b.ymax += i->chardata[t].y;
466 /* until we solve the INTERNAL_SCALING problem (see below)
467 make sure the bounding box is big enough */
473 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
474 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
475 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
476 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
477 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
482 i->chardata[t].fontid,
484 i->chardata[t].charid
486 swf_ExpandRect2(&r, &b);
488 if(debug) printf("-----> (%f,%f,%f,%f)\n",
496 static void putcharacters(struct swfoutput*obj, TAG*tag)
498 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
502 color.r = i->chardata[0].color.r^255;
511 int charadvance[128];
514 int glyphbits=1; //TODO: can this be zero?
517 if(tag->id != ST_DEFINETEXT &&
518 tag->id != ST_DEFINETEXT2) {
519 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
522 if(!i->chardatapos) {
523 msg("<warning> putcharacters called with zero characters");
526 for(pass = 0; pass < 2; pass++)
536 advancebits++; // add sign bit
537 swf_SetU8(tag, glyphbits);
538 swf_SetU8(tag, advancebits);
541 for(t=0;t<=i->chardatapos;t++)
543 if(lastfontid != i->chardata[t].fontid ||
544 lastx!=i->chardata[t].x ||
545 lasty!=i->chardata[t].y ||
546 !colorcompare(obj,&color, &i->chardata[t].color) ||
548 lastsize != i->chardata[t].size ||
551 if(charstorepos && pass==0)
554 for(s=0;s<charstorepos;s++)
556 while(charids[s]>=(1<<glyphbits))
558 while(charadvance[s]>=(1<<advancebits))
562 if(charstorepos && pass==1)
564 tag->writeBit = 0; // Q&D
565 swf_SetBits(tag, 0, 1); // GLYPH Record
566 swf_SetBits(tag, charstorepos, 7); // number of glyphs
568 for(s=0;s<charstorepos;s++)
570 swf_SetBits(tag, charids[s], glyphbits);
571 swf_SetBits(tag, charadvance[s], advancebits);
576 if(pass == 1 && t<i->chardatapos)
582 if(lastx != i->chardata[t].x ||
583 lasty != i->chardata[t].y)
585 newx = i->chardata[t].x;
586 newy = i->chardata[t].y;
592 if(!colorcompare(obj,&color, &i->chardata[t].color))
594 color = i->chardata[t].color;
597 font.id = i->chardata[t].fontid;
598 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
601 tag->writeBit = 0; // Q&D
602 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
605 lastfontid = i->chardata[t].fontid;
606 lastx = i->chardata[t].x;
607 lasty = i->chardata[t].y;
608 lastsize = i->chardata[t].size;
611 if(t==i->chardatapos)
615 int nextt = t==i->chardatapos-1?t:t+1;
616 int rel = i->chardata[nextt].x-i->chardata[t].x;
617 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
619 lastx=i->chardata[nextt].x;
623 lastx=i->chardata[t].x;
625 charids[charstorepos] = i->chardata[t].charid;
626 charadvance[charstorepos] = advance;
633 static void putcharacter(struct swfoutput*obj, int fontid, int charid, int x,int y, int size, RGBA color)
635 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
636 if(i->chardatapos == CHARDATAMAX)
638 msg("<warning> Character buffer too small. SWF will be slightly bigger");
642 i->chardata[i->chardatapos].fontid = fontid;
643 i->chardata[i->chardatapos].charid = charid;
644 i->chardata[i->chardatapos].x = x;
645 i->chardata[i->chardatapos].y = y;
646 i->chardata[i->chardatapos].color = color;
647 i->chardata[i->chardatapos].size = size;
651 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
652 So if we set this value to high, the char coordinates will overflow.
653 If we set it to low, however, the char positions will be inaccurate */
654 #define FONT_INTERNAL_SIZE 4
656 /* process a character. */
657 static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m, gfxcolor_t*col)
659 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
661 msg("<warning> Font is NULL");
665 int charid = getCharID(swffont, charnr, character, u);
668 msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
669 FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
680 float det = ((m->m11*m->m22)-(m->m21*m->m12));
681 if(fabs(det) < 0.0005) {
682 /* x direction equals y direction- the text is invisible */
685 det = 20*FONT_INTERNAL_SIZE / det;
688 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
689 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
691 RGBA rgba = *(RGBA*)col;
692 putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
693 swf_FontUseGlyph(swffont, charid);
698 SWF_OUTLINE*outline = font->getOutline(character, charnr);
699 char* charname = character;
702 msg("<warning> Didn't find character '%s' (%d) in current charset (%s)",
703 FIXNULL(character),charnr,FIXNULL(font->getName()));
722 drawpath(tag, outline, &m2, 0);
727 static void endtext(swfoutput*obj)
729 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
733 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
734 swf_SetU16(i->tag, i->textid);
737 r = getcharacterbbox(obj, obj->swffont);
739 swf_SetRect(i->tag,&r);
742 swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
743 swf_SetMatrix(i->tag,&m);
745 putcharacters(obj, i->tag);
747 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
748 //swf_ObjectPlace(i->tag,i->textid,/*depth*/i->depth++,&i->page_matrix,NULL,NULL);
750 swf_MatrixJoin(&m2,&obj->fontmatrix, &i->page_matrix);
752 swf_ObjectPlace(i->tag,i->textid,/*depth*/i->depth++,&m2,NULL,NULL);
756 int getCharID(SWFFONT *font, int charnr, char *charname, int u)
759 if(charname && font->glyphnames) {
760 for(t=0;t<font->numchars;t++) {
761 if(font->glyphnames[t] && !strcmp(font->glyphnames[t],charname)) {
762 msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
766 /* if we didn't find the character, maybe
767 we can find the capitalized version */
768 for(t=0;t<font->numchars;t++) {
769 if(font->glyphnames[t] && !strcasecmp(font->glyphnames[t],charname)) {
770 msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
776 if(u>0 && font->encoding != 255) {
777 /* try to use the unicode id */
778 if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
779 msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
780 return font->ascii2glyph[u];
784 if(font->encoding != FONT_ENCODING_UNICODE) {
785 /* the following only works if the font encoding
786 is US-ASCII based. It's needed for fonts which return broken unicode
788 if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
789 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, font->ascii2glyph[charnr]);
790 return font->ascii2glyph[charnr];
794 if(charnr>=0 && charnr<font->numchars) {
795 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
803 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
804 void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
806 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
807 fontlist_t*last=0,*iterator;
809 msg("<error> No fontid");
813 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
816 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
817 with multiple fonts */
820 iterator = i->fontlist;
822 if(!strcmp((char*)iterator->swffont->name,fontid)) {
823 obj->swffont = iterator->swffont;
827 iterator = iterator->next;
831 msg("<error> No filename given for font- internal error?");
835 swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
836 SWFFONT*swffont = swf_LoadFont(filename);
839 msg("<warning> Couldn't load font %s (%s)", fontid, filename);
840 swffont = swf_LoadFont(0);
843 if(swffont->glyph2ascii) {
846 /* check whether the Unicode indices look o.k.
847 If they don't, disable the unicode lookup by setting
848 the encoding to 255 */
849 for(t=0;t<swffont->numchars;t++) {
850 int c = swffont->glyph2ascii[t];
851 if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
852 // the character maps into the unicode control character range
853 // between 0001-001f. Yet it is not empty. Treat the one
854 // mapping as broken, and look how many of those we find.
859 msg("<warning> Font %s has bad unicode mapping", fontid);
860 swffont->encoding = 255;
864 swf_FontSetID(swffont, ++i->currentswfid);
866 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
867 // print font information
868 msg("<debug> Font %s (%s)",swffont->name, filename);
869 msg("<debug> | ID: %d", swffont->id);
870 msg("<debug> | Version: %d", swffont->version);
871 msg("<debug> | Name: %s", fontid);
872 msg("<debug> | Numchars: %d", swffont->numchars);
873 msg("<debug> | Maxascii: %d", swffont->maxascii);
874 msg("<debug> | Style: %d", swffont->style);
875 msg("<debug> | Encoding: %d", swffont->encoding);
876 for(int iii=0; iii<swffont->numchars;iii++) {
877 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,
878 swffont->layout->bounds[iii].xmin/20.0,
879 swffont->layout->bounds[iii].ymin/20.0,
880 swffont->layout->bounds[iii].xmax/20.0,
881 swffont->layout->bounds[iii].ymax/20.0
884 for(t=0;t<swffont->maxascii;t++) {
885 if(swffont->ascii2glyph[t] == iii)
886 msg("<debug> | - maps to %d",t);
891 /* set the font name to the ID we use here */
892 if(swffont->name) free(swffont->name);
893 swffont->name = (U8*)strdup(fontid);
895 iterator = new fontlist_t;
896 iterator->swffont = swffont;
900 last->next = iterator;
902 i->fontlist = iterator;
904 obj->swffont = swffont;
907 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
909 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
910 fontlist_t *iterator = i->fontlist;
912 if(!strcmp((char*)iterator->swffont->name,fontid))
914 iterator = iterator->next;
919 /* set's the matrix which is to be applied to characters drawn by
920 swfoutput_drawchar() */
921 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
922 double m21,double m22)
924 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
925 if(obj->fontm11 == m11 &&
926 obj->fontm12 == m12 &&
927 obj->fontm21 == m21 &&
938 m.sx = (U32)(((obj->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((obj->fontm12)*65536)/FONT_INTERNAL_SIZE);
939 m.r0 = (U32)(((obj->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((obj->fontm22)*65536)/FONT_INTERNAL_SIZE);
945 /* draws a character at x,y. */
946 int swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr, int u, gfxcolor_t* color)
948 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
950 m.m11 = obj->fontm11;
951 m.m12 = obj->fontm12;
952 m.m21 = obj->fontm21;
953 m.m22 = obj->fontm22;
956 return drawchar(obj, obj->swffont, character, charnr, u, &m, color);
959 static void endpage(struct swfoutput*obj)
961 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
967 swfoutput_endclip(obj);
971 void swfoutput_pagefeed(struct swfoutput*obj)
973 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
978 if(config_insertstoptag) {
980 atag = action_Stop(atag);
981 atag = action_End(atag);
982 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
983 swf_ActionSet(i->tag,atag);
985 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
988 for(i->depth--;i->depth>=i->startdepth;i->depth--) {
989 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
990 swf_SetU16(i->tag,i->depth);
992 i->depth = i->startdepth;
995 static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
997 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
999 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1003 int shapeid = ++i->currentswfid;
1008 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
1010 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
1011 swf_SetU16(i->tag,shapeid);
1012 swf_SetRect(i->tag,&r);
1013 swf_SetShapeHeader(i->tag,s);
1014 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
1015 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
1016 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
1017 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
1018 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
1019 swf_ShapeSetEnd(i->tag);
1021 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1022 swf_ObjectPlace(i->tag,shapeid,i->depth++,0,0,0);
1023 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1024 swf_ObjectPlaceClip(i->tag,shapeid,i->depth++,0,0,0,65535);
1025 i->cliptag = i->tag;
1028 void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey, int x1, int y1, int x2, int y2)
1030 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1031 if(!i->firstpage && !i->pagefinished)
1034 swf_GetMatrix(0, &i->page_matrix);
1035 i->page_matrix.tx = movex*20;
1036 i->page_matrix.ty = movey*20;
1038 if(i->cliptag && i->frameno == i->lastframeno) {
1040 swf_GetPlaceObject(i->cliptag, &obj);
1041 obj.clipdepth = i->depth++;
1042 swf_ResetTag(i->cliptag, i->cliptag->id);
1043 swf_SetPlaceObject(i->cliptag, &obj);
1044 swf_PlaceObjectFree(&obj);
1052 msg("<notice> processing page %d (%dx%d:%d:%d)", pageNum,x2-x1,y2-y1, x1, y1);
1054 x1*=20;y1*=20;x2*=20;y2*=20;
1056 /* set clipping/background rectangle */
1057 /* TODO: this should all be done in SWFOutputDev */
1058 setBackground(obj, x1, y1, x2, y2);
1060 /* increase SWF's bounding box */
1066 swf_ExpandRect2(&i->swf.movieSize, &r);
1068 i->lastframeno = i->frameno;
1070 i->pagefinished = 0;
1073 /* initialize the swf writer */
1074 void swfoutput_init(struct swfoutput* obj)
1076 memset(obj, 0, sizeof(struct swfoutput));
1077 obj->internal = init_internal_struct();
1078 ((swfoutput_internal*)obj->internal)->obj = obj;
1080 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1085 msg("<verbose> initializing swf output for size %d*%d\n", i->max_x,i->max_y);
1089 memset(&i->swf,0x00,sizeof(SWF));
1091 i->swf.fileVersion = config_flashversion;
1092 i->swf.frameRate = 0x0040; // 1 frame per 4 seconds
1093 i->swf.movieSize.xmin = 0;
1094 i->swf.movieSize.ymin = 0;
1095 i->swf.movieSize.xmax = 0;
1096 i->swf.movieSize.ymax = 0;
1098 i->swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1099 i->tag = i->swf.firstTag;
1100 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1101 swf_SetRGB(i->tag,&rgb);
1103 i->startdepth = i->depth = 3; /* leave room for clip and background rectangle */
1106 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1109 static void startshape(struct swfoutput*obj)
1111 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1120 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1122 swf_ShapeNew(&i->shape);
1123 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&obj->strokergb);
1124 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb);
1126 i->shapeid = ++i->currentswfid;
1127 swf_SetU16(i->tag,i->shapeid); // ID
1129 i->bboxrectpos = i->tag->len;
1133 r.xmax = 20*i->max_x;
1134 r.ymax = 20*i->max_y;
1135 swf_SetRect(i->tag,&r);
1137 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
1139 swf_SetShapeStyles(i->tag,i->shape);
1140 swf_ShapeCountBits(i->shape,NULL,NULL);
1141 swf_SetShapeBits(i->tag,i->shape);
1143 /* TODO: do we really need this? */
1144 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1145 i->swflastx=i->swflasty=0;
1147 i->shapeisempty = 1;
1150 static void starttext(struct swfoutput*obj)
1152 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1156 i->textid = ++i->currentswfid;
1158 i->swflastx=i->swflasty=0;
1162 /* TODO: move to ../lib/rfxswf */
1163 void changeRect(struct swfoutput*obj, TAG*tag, int pos, SRECT*newrect)
1165 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1166 /* determine length of old rect */
1170 swf_GetRect(tag, &old);
1171 swf_ResetReadBits(tag);
1172 int pos_end = tag->pos;
1174 int len = tag->len - pos_end;
1175 U8*data = (U8*)malloc(len);
1176 memcpy(data, &tag->data[pos_end], len);
1179 swf_SetRect(tag, newrect);
1180 swf_SetBlock(tag, data, len);
1182 tag->pos = tag->readBit = 0;
1185 void cancelshape(swfoutput*obj)
1187 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1188 /* delete old shape tag */
1190 i->tag = i->tag->prev;
1191 swf_DeleteTag(todel);
1192 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1194 i->bboxrectpos = -1;
1197 void fixAreas(swfoutput*obj)
1199 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1200 if(!i->shapeisempty && i->fill &&
1201 (i->bboxrect.xmin == i->bboxrect.xmax ||
1202 i->bboxrect.ymin == i->bboxrect.ymax) &&
1203 config_minlinewidth >= 0.001
1205 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1206 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1207 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1210 SRECT r = i->bboxrect;
1212 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1213 /* this thing comes down to a single dot- nothing to fix here */
1219 RGBA save_col = obj->strokergb;
1220 int save_width = i->linewidth;
1222 obj->strokergb = obj->fillrgb;
1223 i->linewidth = (int)(config_minlinewidth*20);
1224 if(i->linewidth==0) i->linewidth = 1;
1228 moveto(obj, i->tag, r.xmin/20.0,r.ymin/20.0);
1229 lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0);
1231 obj->strokergb = save_col;
1232 i->linewidth = save_width;
1237 static void endshape_noput(swfoutput*obj)
1239 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1242 //changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1245 swf_ShapeFree(i->shape);
1251 static void endshape(swfoutput*obj)
1253 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1259 if(i->shapeisempty ||
1261 (i->bboxrect.xmin == i->bboxrect.xmax &&
1262 i->bboxrect.ymin == i->bboxrect.ymax))
1264 // delete the shape again, we didn't do anything
1269 swf_ShapeSetEnd(i->tag);
1271 changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1273 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1274 swf_ObjectPlace(i->tag,i->shapeid,/*depth*/i->depth++,&i->page_matrix,NULL,NULL);
1276 swf_ShapeFree(i->shape);
1279 i->bboxrectpos = -1;
1283 void swfoutput_finalize(struct swfoutput*obj)
1285 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1287 if(i->tag && i->tag->id == ST_END)
1288 return; //already done
1290 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1291 swfoutput_pagefeed(obj);
1294 fontlist_t *tmp,*iterator = i->fontlist;
1296 TAG*mtag = i->swf.firstTag;
1297 if(iterator->swffont) {
1298 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1299 /*if(!storeallcharacters)
1300 swf_FontReduce(iterator->swffont);*/
1301 swf_FontSetDefine2(mtag, iterator->swffont);
1304 iterator = iterator->next;
1306 i->tag = swf_InsertTag(i->tag,ST_END);
1307 TAG* tag = i->tag->prev;
1309 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1310 and the ST_END- they confuse the flash player */
1311 while(tag->id == ST_REMOVEOBJECT2) {
1312 TAG* prev = tag->prev;
1318 SWF* swfoutput_get(struct swfoutput*obj)
1320 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1322 swfoutput_finalize(obj);
1324 return swf_CopySWF(&i->swf);
1327 void swfoutput_getdimensions(struct swfoutput*obj, int*x1, int*y1, int*x2, int*y2)
1329 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1330 if(x1) *x1 = i->swf.movieSize.xmin/20;
1331 if(y1) *y1 = i->swf.movieSize.ymin/20;
1332 if(x2) *x2 = i->swf.movieSize.xmax/20;
1333 if(y2) *y2 = i->swf.movieSize.ymax/20;
1336 int swfoutput_save(struct swfoutput* obj, char*filename)
1338 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1339 swfoutput_finalize(obj);
1343 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1348 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1352 if(config_enablezlib || config_flashversion>=6) {
1353 if FAILED(swf_WriteSWC(fi,&i->swf))
1354 msg("<error> WriteSWC() failed.\n");
1356 if FAILED(swf_WriteSWF(fi,&i->swf))
1357 msg("<error> WriteSWF() failed.\n");
1362 msg("<notice> SWF written\n");
1366 /* Perform cleaning up, complete the swf, and write it out. */
1367 void swfoutput_destroy(struct swfoutput* obj)
1369 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1371 /* not initialized yet- nothing to destroy */
1375 fontlist_t *tmp,*iterator = i->fontlist;
1377 if(iterator->swffont) {
1378 swf_FontFree(iterator->swffont);iterator->swffont=0;
1381 iterator = iterator->next;
1384 swf_FreeTags(&i->swf);
1387 memset(obj, 0, sizeof(swfoutput));
1390 static void swfoutput_setfillcolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1392 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1393 if(obj->fillrgb.r == r &&
1394 obj->fillrgb.g == g &&
1395 obj->fillrgb.b == b &&
1396 obj->fillrgb.a == a) return;
1406 static void swfoutput_setstrokecolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1408 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1409 if(obj->strokergb.r == r &&
1410 obj->strokergb.g == g &&
1411 obj->strokergb.b == b &&
1412 obj->strokergb.a == a) return;
1416 obj->strokergb.r = r;
1417 obj->strokergb.g = g;
1418 obj->strokergb.b = b;
1419 obj->strokergb.a = a;
1422 static void swfoutput_setlinewidth(struct swfoutput*obj, double _linewidth)
1424 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1425 if(i->linewidth == (U16)(_linewidth*20))
1430 i->linewidth = (U16)(_linewidth*20);
1434 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1436 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1438 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1440 if(!strncmp("http://pdf2swf:", url, 15)) {
1441 char*tmp = strdup(url);
1442 int l = strlen(tmp);
1445 swfoutput_namedlink(obj, tmp+15, points);
1455 if(config_opennewwindow)
1456 actions = action_GetUrl(0, url, "_parent");
1458 actions = action_GetUrl(0, url, "_this");
1459 actions = action_End(actions);
1461 drawlink(obj, actions, 0, points,0);
1463 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1465 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1473 actions = action_GotoFrame(0, page);
1474 actions = action_End(actions);
1476 drawlink(obj, actions, 0, points,0);
1479 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1480 of the viewer objects, like subtitles, index elements etc.
1482 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1484 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1485 ActionTAG *actions1,*actions2;
1486 char*tmp = strdup(name);
1494 if(!strncmp(tmp, "call:", 5))
1496 char*x = strchr(&tmp[5], ':');
1498 actions1 = action_PushInt(0, 0); //number of parameters (0)
1499 actions1 = action_PushString(actions1, &tmp[5]); //function name
1500 actions1 = action_CallFunction(actions1);
1503 actions1 = action_PushString(0, x+1); //parameter
1504 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1505 actions1 = action_PushString(actions1, &tmp[5]); //function name
1506 actions1 = action_CallFunction(actions1);
1508 actions2 = action_End(0);
1513 actions1 = action_PushString(0, "/:subtitle");
1514 actions1 = action_PushString(actions1, name);
1515 actions1 = action_SetVariable(actions1);
1516 actions1 = action_End(actions1);
1518 actions2 = action_PushString(0, "/:subtitle");
1519 actions2 = action_PushString(actions2, "");
1520 actions2 = action_SetVariable(actions2);
1521 actions2 = action_End(actions2);
1524 drawlink(obj, actions1, actions2, points,mouseover);
1526 swf_ActionFree(actions1);
1527 swf_ActionFree(actions2);
1531 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1533 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1538 struct plotxy p1,p2,p3,p4;
1542 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1546 int buttonid = ++i->currentswfid;
1549 if(points[t].x>xmax) xmax=points[t].x;
1550 if(points[t].y>ymax) ymax=points[t].y;
1551 if(points[t].x<xmin) xmin=points[t].x;
1552 if(points[t].y<ymin) ymin=points[t].y;
1555 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1556 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1558 /* the following code subtracts the upper left edge from all coordinates,
1559 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1560 Necessary for preprocessing with swfcombine. */
1561 posx = xmin; posy = ymin;
1562 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1563 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1564 xmin -= posx; ymin -= posy;
1565 xmax -= posx; ymax -= posy;
1568 myshapeid = ++i->currentswfid;
1569 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1570 swf_ShapeNew(&i->shape);
1571 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1572 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1573 swf_SetU16(i->tag, myshapeid);
1574 r.xmin = (int)(xmin*20);
1575 r.ymin = (int)(ymin*20);
1576 r.xmax = (int)(xmax*20);
1577 r.ymax = (int)(ymax*20);
1578 swf_SetRect(i->tag,&r);
1579 swf_SetShapeStyles(i->tag,i->shape);
1580 swf_ShapeCountBits(i->shape,NULL,NULL);
1581 swf_SetShapeBits(i->tag,i->shape);
1582 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1583 i->swflastx = i->swflasty = 0;
1584 moveto(obj, i->tag, p1);
1585 lineto(obj, i->tag, p2);
1586 lineto(obj, i->tag, p3);
1587 lineto(obj, i->tag, p4);
1588 lineto(obj, i->tag, p1);
1589 swf_ShapeSetEnd(i->tag);
1592 myshapeid2 = ++i->currentswfid;
1593 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1594 swf_ShapeNew(&i->shape);
1595 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1597 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1598 swf_SetU16(i->tag, myshapeid2);
1599 r.xmin = (int)(xmin*20);
1600 r.ymin = (int)(ymin*20);
1601 r.xmax = (int)(xmax*20);
1602 r.ymax = (int)(ymax*20);
1603 swf_SetRect(i->tag,&r);
1604 swf_SetShapeStyles(i->tag,i->shape);
1605 swf_ShapeCountBits(i->shape,NULL,NULL);
1606 swf_SetShapeBits(i->tag,i->shape);
1607 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1608 i->swflastx = i->swflasty = 0;
1609 moveto(obj, i->tag, p1);
1610 lineto(obj, i->tag, p2);
1611 lineto(obj, i->tag, p3);
1612 lineto(obj, i->tag, p4);
1613 lineto(obj, i->tag, p1);
1614 swf_ShapeSetEnd(i->tag);
1618 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1619 swf_SetU16(i->tag,buttonid); //id
1620 swf_ButtonSetFlags(i->tag, 0); //menu=no
1621 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1622 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1623 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1624 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1625 swf_SetU8(i->tag,0);
1626 swf_ActionSet(i->tag,actions1);
1627 swf_SetU8(i->tag,0);
1631 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1632 swf_SetU16(i->tag,buttonid); //id
1633 swf_ButtonSetFlags(i->tag, 0); //menu=no
1634 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1635 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1636 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1637 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1638 swf_SetU8(i->tag,0); // end of button records
1639 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1640 swf_ActionSet(i->tag,actions1);
1642 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1643 swf_ActionSet(i->tag,actions2);
1644 swf_SetU8(i->tag,0);
1645 swf_ButtonPostProcess(i->tag, 2);
1647 swf_SetU8(i->tag,0);
1648 swf_ButtonPostProcess(i->tag, 1);
1652 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1654 if(posx!=0 || posy!=0) {
1656 p.x = (int)(posx*20);
1657 p.y = (int)(posy*20);
1658 p = swf_TurnPoint(p, &i->page_matrix);
1663 swf_ObjectPlace(i->tag, buttonid, i->depth++,&m,0,0);
1666 swf_ObjectPlace(i->tag, buttonid, i->depth++,&i->page_matrix,0,0);
1673 for(t=0;t<picpos;t++)
1675 if(pic_xids[t] == xid &&
1676 pic_yids[t] == yid) {
1677 width = pic_width[t];
1678 height = pic_height[t];
1682 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1683 pic_xids[picpos] = xid;
1684 pic_yids[picpos] = yid;
1685 pic_width[picpos] = width;
1686 pic_height[picpos] = height;
1689 pic[width*y+x] = buf[0];
1693 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1694 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1698 xid += x*r+x*b*3+x*g*7+x*a*11;
1699 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1701 for(t=0;t<picpos;t++)
1703 if(pic_xids[t] == xid &&
1704 pic_yids[t] == yid) {
1712 static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
1714 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1715 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1720 if(line->type == gfx_moveTo) {
1721 moveto(obj, i->tag, line->x, line->y);
1722 px = lastx = line->x;
1723 py = lasty = line->y;
1725 } if(line->type == gfx_lineTo) {
1726 lineto(obj, i->tag, line->x, line->y);
1730 } else if(line->type == gfx_splineTo) {
1732 s.x = line->sx;p.x = line->x;
1733 s.y = line->sy;p.y = line->y;
1734 splineto(obj, i->tag, s, p);
1743 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)
1745 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1746 gfxdevice_t*dev = &i->device;
1747 dev->stroke(dev, line, width, col, cap_style, joint_style, miterLimit);
1749 void swfoutput_fillgfxline(struct swfoutput*obj, gfxline_t*line, gfxcolor_t*col)
1751 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1752 gfxdevice_t*dev = &i->device;
1753 dev->fill(dev, line, col);
1755 void swfoutput_startclip(struct swfoutput*obj, gfxline_t*line)
1757 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1758 gfxdevice_t*dev = &i->device;
1759 dev->startclip(dev, line);
1761 void swfoutput_endclip(struct swfoutput*obj)
1763 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1764 gfxdevice_t*dev = &i->device;
1768 #define IMAGE_TYPE_JPEG 0
1769 #define IMAGE_TYPE_LOSSLESS 1
1771 static void swfoutput_drawimage(struct swfoutput*obj, RGBA* data, int sizex,int sizey,
1772 double x1,double y1,
1773 double x2,double y2,
1774 double x3,double y3,
1775 double x4,double y4, int type)
1777 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1781 double l1 = sqrt((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1));
1782 double l2 = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
1784 gfxline_t p1,p2,p3,p4,p5;
1785 p1.type=gfx_moveTo;p1.x=x1; p1.y=y1;p1.next=&p2;
1786 p2.type=gfx_lineTo;p2.x=x2; p2.y=y2;p2.next=&p3;
1787 p3.type=gfx_lineTo;p3.x=x3; p3.y=y3;p3.next=&p4;
1788 p4.type=gfx_lineTo;p4.x=x4; p4.y=y4;p4.next=&p5;
1789 p5.type=gfx_lineTo;p5.x=x1; p5.y=y1;p5.next=0;
1791 {p1.x = (int)(p1.x*20)/20.0;
1792 p1.y = (int)(p1.y*20)/20.0;
1793 p2.x = (int)(p2.x*20)/20.0;
1794 p2.y = (int)(p2.y*20)/20.0;
1795 p3.x = (int)(p3.x*20)/20.0;
1796 p3.y = (int)(p3.y*20)/20.0;
1797 p4.x = (int)(p4.x*20)/20.0;
1798 p4.y = (int)(p4.y*20)/20.0;
1799 p5.x = (int)(p5.x*20)/20.0;
1800 p5.y = (int)(p5.y*20)/20.0;
1807 m.m00 = (p4.x-p1.x)/sizex; m.m10 = (p2.x-p1.x)/sizey;
1808 m.m01 = (p4.y-p1.y)/sizex; m.m11 = (p2.y-p1.y)/sizey;
1813 img.data = (gfxcolor_t*)data;
1817 if(type == IMAGE_TYPE_JPEG)
1818 /* TODO: pass image_dpi to device instead */
1819 i->device.setparameter(&i->device, "next_bitmap_is_jpeg", "1");
1821 i->device.fillbitmap(&i->device, &p1, &img, &m, 0);
1824 void swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1825 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
1827 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_JPEG);
1830 void swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
1831 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
1833 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_LOSSLESS);
1836 void swfoutput_setparameter(char*name, char*value)
1838 if(!strcmp(name, "jpegsubpixels")) {
1839 config_jpegsubpixels = atof(value);
1840 } else if(!strcmp(name, "ppmsubpixels")) {
1841 config_ppmsubpixels = atof(value);
1842 } else if(!strcmp(name, "drawonlyshapes")) {
1843 config_drawonlyshapes = atoi(value);
1844 } else if(!strcmp(name, "ignoredraworder")) {
1845 config_ignoredraworder = atoi(value);
1846 } else if(!strcmp(name, "filloverlap")) {
1847 config_filloverlap = atoi(value);
1848 } else if(!strcmp(name, "linksopennewwindow")) {
1849 config_opennewwindow = atoi(value);
1850 } else if(!strcmp(name, "opennewwindow")) {
1851 config_opennewwindow = atoi(value);
1852 } else if(!strcmp(name, "storeallcharacters")) {
1853 config_storeallcharacters = atoi(value);
1854 } else if(!strcmp(name, "enablezlib")) {
1855 config_enablezlib = atoi(value);
1856 } else if(!strcmp(name, "insertstop")) {
1857 config_insertstoptag = atoi(value);
1858 } else if(!strcmp(name, "protected")) {
1859 config_protect = atoi(value);
1860 } else if(!strcmp(name, "flashversion")) {
1861 config_flashversion = atoi(value);
1862 } else if(!strcmp(name, "minlinewidth")) {
1863 config_minlinewidth = atof(value);
1864 } else if(!strcmp(name, "caplinewidth")) {
1865 config_caplinewidth = atof(value);
1866 } else if(!strcmp(name, "jpegquality")) {
1867 int val = atoi(value);
1869 if(val>100) val=100;
1870 config_jpegquality = val;
1871 } else if(!strcmp(name, "splinequality")) {
1872 int v = atoi(value);
1873 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1875 config_splinemaxerror = v;
1876 } else if(!strcmp(name, "fontquality")) {
1877 int v = atoi(value);
1878 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1880 config_fontsplinemaxerror = v;
1882 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1886 // --------------------------------------------------------------------
1888 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1891 swf_GetCXForm(0, &cx, 1);
1894 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1895 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1896 c->br!=0 || c->bg!=0 || c->ba!=0 ||
1897 c->ar!=0 || c->ag!=0 || c->ab!=0)
1898 msg("<warning> CXForm not SWF-compatible");
1900 cx.a0 = (S16)(c->aa*256);
1901 cx.r0 = (S16)(c->rr*256);
1902 cx.g0 = (S16)(c->gg*256);
1903 cx.b0 = (S16)(c->bb*256);
1911 ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
1913 ArtVpath *vec = NULL;
1921 if(l2->type == gfx_moveTo) {
1923 } if(l2->type == gfx_lineTo) {
1925 } if(l2->type == gfx_splineTo) {
1926 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
1927 if(!parts) parts = 1;
1937 vec = art_new (ArtVpath, len);
1942 if(l2->type == gfx_moveTo) {
1943 vec[pos].code = ART_MOVETO;
1948 } else if(l2->type == gfx_lineTo) {
1949 vec[pos].code = ART_LINETO;
1954 } else if(l2->type == gfx_splineTo) {
1956 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
1957 if(!parts) parts = 1;
1958 for(i=0;i<=parts;i++) {
1959 double t = (double)i/(double)parts;
1960 vec[pos].code = ART_LINETO;
1961 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
1962 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
1971 vec[pos].code = ART_END;
1973 svp = art_svp_vpath_stroke (vec,
1974 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
1975 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
1976 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
1977 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
1978 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
1979 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
1981 miterLimit, //miter_limit
1988 gfxline_t* SVPtogfxline(ArtSVP*svp)
1993 for(t=0;t<svp->n_segs;t++) {
1994 size += svp->segs[t].n_points + 1;
1996 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
1998 for(t=0;t<svp->n_segs;t++) {
1999 ArtSVPSeg* seg = &svp->segs[t];
2001 for(p=0;p<seg->n_points;p++) {
2002 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
2003 ArtPoint* point = &seg->points[p];
2004 lines[pos].x = point->x;
2005 lines[pos].y = point->y;
2006 lines[pos].next = &lines[pos+1];
2011 lines[pos-1].next = 0;
2020 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
2023 RGBA*mem = (RGBA*)img->data;
2024 int bitid = ++i->currentswfid;
2026 int sizex = img->width;
2027 int sizey = img->height;
2028 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
2029 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
2030 int is_jpeg = i->jpeg;
2033 int newsizex=sizex, newsizey=sizey;
2036 if(is_jpeg && config_jpegsubpixels) {
2037 newsizex = (int)(targetwidth*config_jpegsubpixels+0.5);
2038 newsizey = (int)(targetheight*config_jpegsubpixels+0.5);
2039 } else if(!is_jpeg && config_ppmsubpixels) {
2040 newsizex = (int)(targetwidth*config_ppmsubpixels+0.5);
2041 newsizey = (int)(targetheight*config_ppmsubpixels+0.5);
2045 /* TODO: cache images */
2047 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
2049 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
2052 targetwidth, targetheight,
2053 /*newsizex, newsizey,*/
2054 num_colors>256?">":"", num_colors>256?256:num_colors);
2056 if(newsizex>sizex || newsizey>sizey) {
2057 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
2058 *newwidth = sizex = newsizex;
2059 *newheight = sizey = newsizey;
2064 if(!is_jpeg || num_colors<=256 || sizex<8 || sizey<8) {
2065 i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSLOSSLESS2);
2066 swf_SetU16(i->tag, bitid);
2067 swf_SetLosslessImage(i->tag,mem,sizex,sizey);
2069 /*TODO: check what is smaller */
2070 i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSJPEG3);
2071 swf_SetU16(i->tag, bitid);
2072 swf_SetJPEGBits3(i->tag,sizex,sizey,mem,config_jpegquality);
2073 //swf_SetLosslessImage(i->tag,mem,sizex,sizey);
2076 if(!is_jpeg || num_colors<=256 || sizex<8 || sizey<8) {
2077 i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSLOSSLESS);
2078 swf_SetU16(i->tag, bitid);
2079 swf_SetLosslessImage(i->tag,mem,sizex,sizey);
2081 /*TODO: check what is smaller */
2082 i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSJPEG2);
2083 swf_SetU16(i->tag, bitid);
2084 swf_SetJPEGBits2(i->tag,sizex,sizey,mem,config_jpegquality);
2085 //swf_SetLosslessImage(i->tag,mem,sizex,sizey);
2095 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2097 gfxbbox_t bbox = gfxline_getbbox(line);
2099 r.xmin = (int)(bbox.xmin*20);
2100 r.ymin = (int)(bbox.ymin*20);
2101 r.xmax = (int)(bbox.xmax*20);
2102 r.ymax = (int)(bbox.ymax*20);
2106 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2108 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2109 swfoutput*obj = i->obj;
2114 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2115 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2117 int newwidth=0,newheight=0;
2118 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2119 double fx = (double)img->width / (double)newwidth;
2120 double fy = (double)img->height / (double)newheight;
2125 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2126 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2127 m.tx = (int)(matrix->tx*20);
2128 m.ty = (int)(matrix->ty*20);
2131 int myshapeid = ++i->currentswfid;
2132 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2134 swf_ShapeNew(&shape);
2135 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2136 swf_SetU16(i->tag, myshapeid);
2137 SRECT r = gfxline_getSWFbbox(line);
2138 swf_SetRect(i->tag,&r);
2139 swf_SetShapeStyles(i->tag,shape);
2140 swf_ShapeCountBits(shape,NULL,NULL);
2141 swf_SetShapeBits(i->tag,shape);
2142 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2143 i->swflastx = i->swflasty = UNDEFINED_COORD;
2144 drawgfxline(obj, line);
2145 swf_ShapeSetEnd(i->tag);
2146 swf_ShapeFree(shape);
2148 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2149 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2150 swf_ObjectPlace(i->tag,myshapeid,/*depth*/i->depth++,&i->page_matrix,&cxform2,NULL);
2153 void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2155 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2156 swfoutput*obj = i->obj;
2161 if(i->clippos >= 127)
2163 msg("<warning> Too many clip levels.");
2167 int myshapeid = ++i->currentswfid;
2168 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2170 memset(&col, 0, sizeof(RGBA));
2172 swf_ShapeNew(&shape);
2173 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2174 swf_SetU16(i->tag,myshapeid);
2175 SRECT r = gfxline_getSWFbbox(line);
2176 swf_SetRect(i->tag,&r);
2177 swf_SetShapeStyles(i->tag,shape);
2178 swf_ShapeCountBits(shape,NULL,NULL);
2179 swf_SetShapeBits(i->tag,shape);
2180 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2181 i->swflastx = i->swflasty = UNDEFINED_COORD;
2182 drawgfxline(obj, line);
2183 swf_ShapeSetEnd(i->tag);
2184 swf_ShapeFree(shape);
2186 /* TODO: remember the bbox, and check all shapes against it */
2188 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2189 i->cliptags[i->clippos] = i->tag;
2190 i->clipshapes[i->clippos] = myshapeid;
2191 i->clipdepths[i->clippos] = i->depth++;
2195 void swf_endclip(gfxdevice_t*dev)
2197 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2198 swfoutput*obj = i->obj;
2205 msg("<error> Invalid end of clipping region");
2209 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth++);
2211 int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
2213 if(!strcmp(key, "next_bitmap_is_jpeg")) {
2214 ((swfoutput_internal*)dev->internal)->jpeg = 1;
2220 int gfxline_type(gfxline_t*line)
2227 if(line->type == gfx_moveTo) {
2230 } else if(line->type == gfx_lineTo) {
2234 } else if(line->type == gfx_splineTo) {
2236 if(tmpsplines>lines)
2241 if(lines==0 && splines==0) return 0;
2242 else if(lines==1 && splines==0) return 1;
2243 else if(lines==0 && splines==1) return 2;
2244 else if(splines==0) return 3;
2248 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)
2250 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2251 swfoutput*obj = i->obj;
2252 int type = gfxline_type(line);
2254 /* TODO: * split line into segments, and perform this check for all segments */
2255 if(width <= config_caplinewidth
2256 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2257 || (cap_style == gfx_capRound && type<=2)) {
2259 swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
2260 swfoutput_setlinewidth(obj, width);
2263 drawgfxline(obj, line);
2265 /* we need to convert the line into a polygon */
2266 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2267 gfxline_t*gfxline = SVPtogfxline(svp);
2268 dev->fill(dev, gfxline, color);
2271 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2273 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2274 swfoutput*obj = i->obj;
2276 if(!config_ignoredraworder)
2278 swfoutput_setfillcolor(obj, color->r, color->g, color->b, color->a);
2282 drawgfxline(obj, line);
2284 void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2286 msg("<error> Gradient filling not implemented yet");