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_dumpfonts=0;
63 double config_ppmsubpixels=0;
64 double config_jpegsubpixels=0;
65 int config_opennewwindow=1;
66 int config_ignoredraworder=0;
67 int config_drawonlyshapes=0;
68 int config_jpegquality=85;
69 int config_storeallcharacters=0;
70 int config_enablezlib=0;
71 int config_insertstoptag=0;
72 int config_flashversion=5;
73 int config_splinemaxerror=1;
74 int config_fontsplinemaxerror=1;
75 int config_filloverlap=0;
77 int config_bboxvars=0;
78 float config_minlinewidth=0.05;
79 double config_caplinewidth=1;
81 typedef struct _swfoutput_internal
83 swfoutput*obj; // the swfoutput object where this internal struct resides
122 int pic_height[1024];
128 char fillstylechanged;
130 int jpeg; //next image type
137 chardata_t chardata[CHARDATAMAX];
144 /* during the transition to the gfxdevice interface:
145 a device which uses this swfoutput as target */
148 } swfoutput_internal;
150 void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
151 int swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
152 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);
153 void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
154 void swf_endclip(gfxdevice_t*dev);
155 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);
156 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
157 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
158 void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
159 void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
160 void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font);
161 void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
163 int getCharID(SWFFONT *font, int charnr, char *charname, int u);
165 static swfoutput_internal* init_internal_struct()
167 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
168 memset(i, 0, sizeof(swfoutput_internal));
190 i->fillstylechanged = 0;
197 i->device.internal = (void*)i;
198 i->device.fillbitmap = swf_fillbitmap;
199 i->device.setparameter = swf_setparameter;
200 i->device.stroke = swf_stroke;
201 i->device.startclip = swf_startclip;
202 i->device.endclip = swf_endclip;
203 i->device.fill = swf_fill;
204 i->device.fillbitmap = swf_fillbitmap;
205 i->device.fillgradient = swf_fillgradient;
206 i->device.addfont = swf_addfont;
207 i->device.drawchar = swf_drawchar;
208 i->device.drawlink = swf_drawlink;
213 static int id_error = 0;
215 static U16 getNewID(struct swfoutput* obj)
217 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
218 if(i->currentswfid == 65535) {
220 msg("<error> ID Table overflow");
224 return ++i->currentswfid;
226 static U16 getNewDepth(struct swfoutput* obj)
228 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
229 if(i->depth == 65535) {
231 msg("<error> Depth Table overflow");
238 static void startshape(struct swfoutput* obj);
239 static void starttext(struct swfoutput* obj);
240 static void endshape(struct swfoutput* obj);
241 static void endtext(struct swfoutput* obj);
243 // matrix multiplication. changes p0
244 static void transform (plotxy*p0,struct swfmatrix*m)
247 x = m->m11*p0->x+m->m12*p0->y;
248 y = m->m21*p0->x+m->m22*p0->y;
253 // write a move-to command into the swf
254 static int moveto(struct swfoutput*obj, TAG*tag, plotxy p0)
256 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
257 int rx = (int)(p0.x*20);
258 int ry = (int)(p0.y*20);
259 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
260 swf_ShapeSetMove (tag, i->shape, rx,ry);
261 i->fillstylechanged = 0;
268 static int moveto(struct swfoutput*obj, TAG*tag, double x, double y)
270 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
274 return moveto(obj, tag, p);
276 static void addPointToBBox(struct swfoutput*obj, int px, int py)
278 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
284 swf_ExpandRect(&i->bboxrect, p);
286 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
290 /*static void plot(struct swfoutput*obj, int x, int y, TAG*tag)
292 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
293 int width = i->linewidth/4;
297 //swf_ShapeSetLine(tag, i->shape,-width,-width);
298 //swf_ShapeSetLine(tag, i->shape,width*2,0);
299 //swf_ShapeSetLine(tag, i->shape,0,width*2);
300 //swf_ShapeSetLine(tag, i->shape,-width*2,0);
301 //swf_ShapeSetLine(tag, i->shape,0,-width*2);
302 //swf_ShapeSetLine(tag, i->shape,width,width);
305 swf_ShapeSetLine(tag, i->shape,-width,0);
306 swf_ShapeSetLine(tag, i->shape,width,-width);
307 swf_ShapeSetLine(tag, i->shape,width,width);
308 swf_ShapeSetLine(tag, i->shape,-width,width);
309 swf_ShapeSetLine(tag, i->shape,-width,-width);
310 swf_ShapeSetLine(tag, i->shape,width,0);
312 addPointToBBox(obj, x-width ,y-width);
313 addPointToBBox(obj, x+width ,y+width);
316 // write a line-to command into the swf
317 static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0)
319 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
320 int px = (int)(p0.x*20);
321 int py = (int)(p0.y*20);
322 int rx = (px-i->swflastx);
323 int ry = (py-i->swflasty);
325 swf_ShapeSetLine (tag, i->shape, rx,ry);
326 addPointToBBox(obj, i->swflastx,i->swflasty);
327 addPointToBBox(obj, px,py);
328 }/* else if(!i->fill) {
329 // treat lines of length 0 as plots, making them
330 // at least 1 twip wide so Flash will display them
331 plot(obj, i->swflastx, i->swflasty, tag);
338 static void lineto(struct swfoutput*obj, TAG*tag, double x, double y)
346 // write a spline-to command into the swf
347 static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end)
349 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
350 int lastlastx = i->swflastx;
351 int lastlasty = i->swflasty;
353 int cx = ((int)(control.x*20)-i->swflastx);
354 int cy = ((int)(control.y*20)-i->swflasty);
357 int ex = ((int)(end.x*20)-i->swflastx);
358 int ey = ((int)(end.y*20)-i->swflasty);
362 if(cx || cy || ex || ey) {
363 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
364 addPointToBBox(obj, lastlastx ,lastlasty );
365 addPointToBBox(obj, lastlastx+cx,lastlasty+cy);
366 addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey);
367 }/* else if(!i->fill) {
368 // treat splines of length 0 as plots
369 plot(obj, lastlastx, lastlasty, tag);
374 /* write a line, given two points and the transformation
376 /*static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1)
378 moveto(obj, tag, p0);
379 lineto(obj, tag, p1);
382 void resetdrawer(struct swfoutput*obj)
384 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
389 static void stopFill(struct swfoutput*obj)
391 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
394 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
395 i->fillstylechanged = 1;
399 static void startFill(struct swfoutput*obj)
401 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
404 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
405 i->fillstylechanged = 1;
410 /* draw an outline. These are generated by pdf2swf and by t1lib
411 (representing characters). */
412 /*void drawpath(struct swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m, int log)
414 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
415 if( i->tag->id != ST_DEFINESHAPE &&
416 i->tag->id != ST_DEFINESHAPE2 &&
417 i->tag->id != ST_DEFINESHAPE3)
419 msg("<error> internal error: drawpath needs a shape tag, not %d\n",i->tag->id);
423 double lastx=0,lasty=0;
424 double firstx=0,firsty=0;
429 x += (outline->dest.x/(float)0xffff);
430 y += (outline->dest.y/(float)0xffff);
431 if(outline->type == SWF_PATHTYPE_MOVE)
433 //if(!init && fill && obj->drawmode != DRAWMODE_EOFILL && !ignoredraworder)
434 if(config_filloverlap && !init && i->fill && obj->drawmode != DRAWMODE_EOFILL) {
435 // drawmode=FILL (not EOFILL) means that
436 // seperate shapes do not cancel each other out.
437 // On SWF side, we need to start a new shape for each
438 // closed polygon, because SWF only knows EOFILL.
445 if(((int)(lastx*20) != (int)(firstx*20) ||
446 (int)(lasty*20) != (int)(firsty*20)) &&
455 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
456 line(obj,i->tag, p0, p1);
462 else if(outline->type == SWF_PATHTYPE_LINE)
470 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
471 line(obj,i->tag, p0,p1);
474 msg("<error> drawpath: unknown outline type:%d\n", outline->type);
478 outline = outline->link;
480 if(((int)(lastx*20) != (int)(firstx*20) ||
481 (int)(lasty*20) != (int)(firsty*20)) &&
490 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
491 line(obj, i->tag, p0, p1);
495 static inline int colorcompare(struct swfoutput*obj, RGBA*a,RGBA*b)
507 static SRECT getcharacterbbox(struct swfoutput*obj, SWFFONT*font)
509 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
512 memset(&r, 0, sizeof(r));
515 if(debug) printf("\n");
516 for(t=0;t<i->chardatapos;t++)
518 if(i->chardata[t].fontid != font->id) {
519 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
522 SRECT b = font->layout->bounds[i->chardata[t].charid];
523 b.xmin *= i->chardata[t].size;
524 b.ymin *= i->chardata[t].size;
525 b.xmax *= i->chardata[t].size;
526 b.ymax *= i->chardata[t].size;
531 b.xmin += i->chardata[t].x;
532 b.ymin += i->chardata[t].y;
533 b.xmax += i->chardata[t].x;
534 b.ymax += i->chardata[t].y;
536 /* until we solve the INTERNAL_SCALING problem (see below)
537 make sure the bounding box is big enough */
543 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
544 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
545 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
546 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
547 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
552 i->chardata[t].fontid,
554 i->chardata[t].charid
556 swf_ExpandRect2(&r, &b);
558 if(debug) printf("-----> (%f,%f,%f,%f)\n",
566 static void putcharacters(struct swfoutput*obj, TAG*tag)
568 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
572 color.r = i->chardata[0].color.r^255;
581 int charadvance[128];
584 int glyphbits=1; //TODO: can this be zero?
587 if(tag->id != ST_DEFINETEXT &&
588 tag->id != ST_DEFINETEXT2) {
589 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
592 if(!i->chardatapos) {
593 msg("<warning> putcharacters called with zero characters");
596 for(pass = 0; pass < 2; pass++)
606 advancebits++; // add sign bit
607 swf_SetU8(tag, glyphbits);
608 swf_SetU8(tag, advancebits);
611 for(t=0;t<=i->chardatapos;t++)
613 if(lastfontid != i->chardata[t].fontid ||
614 lastx!=i->chardata[t].x ||
615 lasty!=i->chardata[t].y ||
616 !colorcompare(obj,&color, &i->chardata[t].color) ||
618 lastsize != i->chardata[t].size ||
621 if(charstorepos && pass==0)
624 for(s=0;s<charstorepos;s++)
626 while(charids[s]>=(1<<glyphbits))
628 while(charadvance[s]>=(1<<advancebits))
632 if(charstorepos && pass==1)
634 tag->writeBit = 0; // Q&D
635 swf_SetBits(tag, 0, 1); // GLYPH Record
636 swf_SetBits(tag, charstorepos, 7); // number of glyphs
638 for(s=0;s<charstorepos;s++)
640 swf_SetBits(tag, charids[s], glyphbits);
641 swf_SetBits(tag, charadvance[s], advancebits);
646 if(pass == 1 && t<i->chardatapos)
652 if(lastx != i->chardata[t].x ||
653 lasty != i->chardata[t].y)
655 newx = i->chardata[t].x;
656 newy = i->chardata[t].y;
662 if(!colorcompare(obj,&color, &i->chardata[t].color))
664 color = i->chardata[t].color;
667 font.id = i->chardata[t].fontid;
668 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
671 tag->writeBit = 0; // Q&D
672 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
675 lastfontid = i->chardata[t].fontid;
676 lastx = i->chardata[t].x;
677 lasty = i->chardata[t].y;
678 lastsize = i->chardata[t].size;
681 if(t==i->chardatapos)
685 int nextt = t==i->chardatapos-1?t:t+1;
686 int rel = i->chardata[nextt].x-i->chardata[t].x;
687 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
689 lastx=i->chardata[nextt].x;
693 lastx=i->chardata[t].x;
695 charids[charstorepos] = i->chardata[t].charid;
696 charadvance[charstorepos] = advance;
703 static void putcharacter(struct swfoutput*obj, int fontid, int charid, int x,int y, int size, RGBA color)
705 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
706 if(i->chardatapos == CHARDATAMAX)
708 msg("<warning> Character buffer too small. SWF will be slightly bigger");
712 i->chardata[i->chardatapos].fontid = fontid;
713 i->chardata[i->chardatapos].charid = charid;
714 i->chardata[i->chardatapos].x = x;
715 i->chardata[i->chardatapos].y = y;
716 i->chardata[i->chardatapos].color = color;
717 i->chardata[i->chardatapos].size = size;
721 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
722 So if we set this value to high, the char coordinates will overflow.
723 If we set it to low, however, the char positions will be inaccurate */
724 #define FONT_INTERNAL_SIZE 4
726 static int font_active = 0;
727 static char* font_active_filename = 0;
729 /* process a character. */
730 static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m, gfxcolor_t*col)
732 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
734 msg("<warning> Font is NULL");
738 int charid = getCharID(swffont, charnr, character, u);
741 sprintf(buf, "%s.usage", font_active_filename);
742 FILE*fi = fopen(buf, "ab+");
744 fprintf(fi, "%d %d %d %s\n", charnr, u, charid, character);
747 msg("<error> Couldn't write to %s", buf);
751 msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
752 FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
755 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
756 msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty",
757 FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
769 float det = ((m->m11*m->m22)-(m->m21*m->m12));
770 if(fabs(det) < 0.0005) {
771 /* x direction equals y direction- the text is invisible */
774 det = 20*FONT_INTERNAL_SIZE / det;
777 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
778 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
780 RGBA rgba = *(RGBA*)col;
781 putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
782 swf_FontUseGlyph(swffont, charid);
787 SWF_OUTLINE*outline = font->getOutline(character, charnr);
788 char* charname = character;
791 msg("<warning> Didn't find character '%s' (%d) in current charset (%s)",
792 FIXNULL(character),charnr,FIXNULL(font->getName()));
811 drawpath(tag, outline, &m2, 0);
816 static void endtext(swfoutput*obj)
818 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
822 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
823 swf_SetU16(i->tag, i->textid);
826 r = getcharacterbbox(obj, obj->swffont);
828 swf_SetRect(i->tag,&r);
831 swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
832 swf_SetMatrix(i->tag,&m);
834 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
836 putcharacters(obj, i->tag);
838 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
840 swf_MatrixJoin(&m2,&obj->fontmatrix, &i->page_matrix);
842 swf_ObjectPlace(i->tag,i->textid,getNewDepth(obj),&m2,NULL,NULL);
846 int getCharID(SWFFONT *font, int charnr, char *charname, int u)
849 if(charname && font->glyphnames) {
850 for(t=0;t<font->numchars;t++) {
851 if(font->glyphnames[t] && !strcmp(font->glyphnames[t],charname)) {
852 msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
856 /* if we didn't find the character, maybe
857 we can find the capitalized version */
858 for(t=0;t<font->numchars;t++) {
859 if(font->glyphnames[t] && !strcasecmp(font->glyphnames[t],charname)) {
860 msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
866 if(u>0 && font->encoding != 255) {
867 /* try to use the unicode id */
868 if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
869 msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
870 return font->ascii2glyph[u];
874 if(font->encoding != FONT_ENCODING_UNICODE) {
875 /* the following only works if the font encoding
876 is US-ASCII based. It's needed for fonts which return broken unicode
878 if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
879 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, font->ascii2glyph[charnr]);
880 return font->ascii2glyph[charnr];
884 if(charnr>=0 && charnr<font->numchars) {
885 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
892 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
893 void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
895 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
897 fontlist_t*last=0,*iterator;
899 msg("<error> No fontid");
903 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
906 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
907 with multiple fonts */
910 iterator = i->fontlist;
912 if(!strcmp((char*)iterator->swffont->name,fontid)) {
913 obj->swffont = iterator->swffont;
917 iterator = iterator->next;
921 msg("<error> No filename given for font- internal error?");
925 swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
926 SWFFONT*swffont = swf_LoadFont(filename);
928 if(config_dumpfonts) {
930 font_active_filename = strdup(filename);
934 msg("<warning> Couldn't load font %s (%s)", fontid, filename);
935 swffont = swf_LoadFont(0);
938 if(swffont->glyph2ascii) {
941 /* check whether the Unicode indices look o.k.
942 If they don't, disable the unicode lookup by setting
943 the encoding to 255 */
944 for(t=0;t<swffont->numchars;t++) {
945 int c = swffont->glyph2ascii[t];
946 if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
947 // the character maps into the unicode control character range
948 // between 0001-001f. Yet it is not empty. Treat the one
949 // mapping as broken, and look how many of those we find.
954 msg("<warning> Font %s has bad unicode mapping", fontid);
955 swffont->encoding = 255;
959 if(swffont->encoding != FONT_ENCODING_UNICODE && swffont->encoding != 255) {
960 msg("<warning> Non-unicode mapping for font %s (%s)", fontid, filename);
963 swf_FontSetID(swffont, getNewID(obj));
965 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
966 // print font information
967 msg("<debug> Font %s (%s)",fontid, filename);
968 msg("<debug> | ID: %d", swffont->id);
969 msg("<debug> | Version: %d", swffont->version);
970 msg("<debug> | Name: %s", swffont->name);
971 msg("<debug> | Numchars: %d", swffont->numchars);
972 msg("<debug> | Maxascii: %d", swffont->maxascii);
973 msg("<debug> | Style: %d", swffont->style);
974 msg("<debug> | Encoding: %d", swffont->encoding);
975 for(int iii=0; iii<swffont->numchars;iii++) {
976 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,
977 swffont->layout->bounds[iii].xmin/20.0,
978 swffont->layout->bounds[iii].ymin/20.0,
979 swffont->layout->bounds[iii].xmax/20.0,
980 swffont->layout->bounds[iii].ymax/20.0
983 for(t=0;t<swffont->maxascii;t++) {
984 if(swffont->ascii2glyph[t] == iii)
985 msg("<debug> | - maps to %d",t);
990 /* set the font name to the ID we use here */
991 if(swffont->name) free(swffont->name);
992 swffont->name = (U8*)strdup(fontid);
994 iterator = new fontlist_t;
995 iterator->swffont = swffont;
999 last->next = iterator;
1001 i->fontlist = iterator;
1003 obj->swffont = swffont;
1006 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
1008 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1009 fontlist_t *iterator = i->fontlist;
1011 if(!strcmp((char*)iterator->swffont->name,fontid))
1013 iterator = iterator->next;
1018 /* set's the matrix which is to be applied to characters drawn by
1019 swfoutput_drawchar() */
1020 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m21,
1021 double m12,double m22)
1027 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1028 if(obj->fontm11 == m11 &&
1029 obj->fontm12 == m12 &&
1030 obj->fontm21 == m21 &&
1031 obj->fontm22 == m22)
1041 m.sx = (U32)(((obj->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((obj->fontm12)*65536)/FONT_INTERNAL_SIZE);
1042 m.r0 = (U32)(((obj->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((obj->fontm22)*65536)/FONT_INTERNAL_SIZE);
1045 obj->fontmatrix = m;
1048 /* draws a character at x,y. */
1049 int swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr, int u, gfxcolor_t* color)
1051 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1053 m.m11 = obj->fontm11;
1054 m.m12 = obj->fontm12;
1055 m.m21 = obj->fontm21;
1056 m.m22 = obj->fontm22;
1059 return drawchar(obj, obj->swffont, character, charnr, u, &m, color);
1062 static void endpage(struct swfoutput*obj)
1064 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1070 swfoutput_endclip(obj);
1071 i->pagefinished = 1;
1074 void swfoutput_pagefeed(struct swfoutput*obj)
1076 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1078 if(!i->pagefinished)
1081 if(config_insertstoptag) {
1083 atag = action_Stop(atag);
1084 atag = action_End(atag);
1085 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
1086 swf_ActionSet(i->tag,atag);
1088 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1091 for(i->depth;i->depth>i->startdepth;i->depth--) {
1092 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
1093 swf_SetU16(i->tag,i->depth);
1095 i->depth = i->startdepth;
1098 static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
1100 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1102 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1106 int shapeid = getNewID(obj);
1111 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
1113 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
1114 swf_SetU16(i->tag,shapeid);
1115 swf_SetRect(i->tag,&r);
1116 swf_SetShapeHeader(i->tag,s);
1117 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
1118 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
1119 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
1120 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
1121 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
1122 swf_ShapeSetEnd(i->tag);
1124 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1125 swf_ObjectPlace(i->tag,shapeid,getNewDepth(obj),0,0,0);
1126 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1127 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(obj),0,0,0,65535);
1128 i->cliptag = i->tag;
1131 void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey, int x1, int y1, int x2, int y2)
1133 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1134 if(!i->firstpage && !i->pagefinished)
1137 swf_GetMatrix(0, &i->page_matrix);
1138 i->page_matrix.tx = movex*20;
1139 i->page_matrix.ty = movey*20;
1141 if(i->cliptag && i->frameno == i->lastframeno) {
1143 swf_GetPlaceObject(i->cliptag, &obj);
1144 obj.clipdepth = i->depth;
1145 swf_ResetTag(i->cliptag, i->cliptag->id);
1146 swf_SetPlaceObject(i->cliptag, &obj);
1147 swf_PlaceObjectFree(&obj);
1155 msg("<notice> processing page %d (%dx%d:%d:%d)", pageNum,x2-x1,y2-y1, x1, y1);
1157 x1*=20;y1*=20;x2*=20;y2*=20;
1159 /* set clipping/background rectangle */
1160 /* TODO: this should all be done in SWFOutputDev */
1161 setBackground(obj, x1, y1, x2, y2);
1163 /* increase SWF's bounding box */
1169 swf_ExpandRect2(&i->swf.movieSize, &r);
1171 i->lastframeno = i->frameno;
1173 i->pagefinished = 0;
1176 /* initialize the swf writer */
1177 void swfoutput_init(struct swfoutput* obj)
1179 memset(obj, 0, sizeof(struct swfoutput));
1180 obj->internal = init_internal_struct();
1181 ((swfoutput_internal*)obj->internal)->obj = obj;
1183 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1188 msg("<verbose> initializing swf output for size %d*%d\n", i->max_x,i->max_y);
1192 memset(&i->swf,0x00,sizeof(SWF));
1194 i->swf.fileVersion = config_flashversion;
1195 i->swf.frameRate = 0x0040; // 1 frame per 4 seconds
1196 i->swf.movieSize.xmin = 0;
1197 i->swf.movieSize.ymin = 0;
1198 i->swf.movieSize.xmax = 0;
1199 i->swf.movieSize.ymax = 0;
1201 i->swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1202 i->tag = i->swf.firstTag;
1203 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1204 swf_SetRGB(i->tag,&rgb);
1206 i->startdepth = i->depth = 0;
1209 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1212 static void startshape(struct swfoutput*obj)
1214 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1223 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1225 swf_ShapeNew(&i->shape);
1226 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&obj->strokergb);
1227 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb);
1229 i->shapeid = getNewID(obj);
1231 msg("<debug> Using shape id %d", i->shapeid);
1233 swf_SetU16(i->tag,i->shapeid); // ID
1235 i->bboxrectpos = i->tag->len;
1239 r.xmax = 20*i->max_x;
1240 r.ymax = 20*i->max_y;
1241 swf_SetRect(i->tag,&r);
1243 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
1245 swf_SetShapeStyles(i->tag,i->shape);
1246 swf_ShapeCountBits(i->shape,NULL,NULL);
1247 swf_SetShapeBits(i->tag,i->shape);
1249 /* TODO: do we really need this? */
1250 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1251 i->swflastx=i->swflasty=0;
1253 i->shapeisempty = 1;
1256 static void starttext(struct swfoutput*obj)
1258 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1262 i->textid = getNewID(obj);
1264 i->swflastx=i->swflasty=0;
1268 /* TODO: move to ../lib/rfxswf */
1269 void changeRect(struct swfoutput*obj, TAG*tag, int pos, SRECT*newrect)
1271 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1272 /* determine length of old rect */
1276 swf_GetRect(tag, &old);
1277 swf_ResetReadBits(tag);
1278 int pos_end = tag->pos;
1280 int len = tag->len - pos_end;
1281 U8*data = (U8*)malloc(len);
1282 memcpy(data, &tag->data[pos_end], len);
1285 swf_SetRect(tag, newrect);
1286 swf_SetBlock(tag, data, len);
1288 tag->pos = tag->readBit = 0;
1291 void cancelshape(swfoutput*obj)
1293 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1294 /* delete old shape tag */
1296 i->tag = i->tag->prev;
1297 swf_DeleteTag(todel);
1298 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1300 i->bboxrectpos = -1;
1303 void fixAreas(swfoutput*obj)
1305 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1306 if(!i->shapeisempty && i->fill &&
1307 (i->bboxrect.xmin == i->bboxrect.xmax ||
1308 i->bboxrect.ymin == i->bboxrect.ymax) &&
1309 config_minlinewidth >= 0.001
1311 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1312 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1313 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1316 SRECT r = i->bboxrect;
1318 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1319 /* this thing comes down to a single dot- nothing to fix here */
1325 RGBA save_col = obj->strokergb;
1326 int save_width = i->linewidth;
1328 obj->strokergb = obj->fillrgb;
1329 i->linewidth = (int)(config_minlinewidth*20);
1330 if(i->linewidth==0) i->linewidth = 1;
1334 moveto(obj, i->tag, r.xmin/20.0,r.ymin/20.0);
1335 lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0);
1337 obj->strokergb = save_col;
1338 i->linewidth = save_width;
1343 static void endshape_noput(swfoutput*obj)
1345 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1348 //changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1351 swf_ShapeFree(i->shape);
1357 static void endshape(swfoutput*obj)
1359 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1365 if(i->shapeisempty ||
1367 (i->bboxrect.xmin == i->bboxrect.xmax &&
1368 i->bboxrect.ymin == i->bboxrect.ymax))
1370 // delete the shape again, we didn't do anything
1371 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1372 i->bboxrect.xmin /20.0,
1373 i->bboxrect.ymin /20.0,
1374 i->bboxrect.xmax /20.0,
1375 i->bboxrect.ymax /20.0
1381 swf_ShapeSetEnd(i->tag);
1383 changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1385 msg("<trace> Placing shape id %d", i->shapeid);
1387 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1388 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(obj),&i->page_matrix,NULL,NULL);
1390 swf_ShapeFree(i->shape);
1393 i->bboxrectpos = -1;
1399 sprintf(text, "id%d", i->shapeid);
1401 points[0].x = i->bboxrect.xmin;
1402 points[0].y = i->bboxrect.ymin;
1403 points[1].x = i->bboxrect.xmax;
1404 points[1].y = i->bboxrect.ymin;
1405 points[2].x = i->bboxrect.xmax;
1406 points[2].y = i->bboxrect.ymax;
1407 points[3].x = i->bboxrect.xmin;
1408 points[3].y = i->bboxrect.ymax;
1409 swfoutput_namedlink(obj,text,points);
1413 void wipeSWF(SWF*swf)
1415 TAG*tag = swf->firstTag;
1417 TAG*next = tag->next;
1418 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1419 tag->id != ST_END &&
1420 tag->id != ST_DOACTION &&
1421 tag->id != ST_SHOWFRAME) {
1429 void swfoutput_finalize(struct swfoutput*obj)
1431 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1433 if(i->tag && i->tag->id == ST_END)
1434 return; //already done
1436 if(config_bboxvars) {
1437 TAG* tag = swf_InsertTag(i->swf.firstTag, ST_DOACTION);
1439 a = action_PushString(a, "xmin");
1440 a = action_PushFloat(a, i->swf.movieSize.xmin / 20.0);
1441 a = action_SetVariable(a);
1442 a = action_PushString(a, "ymin");
1443 a = action_PushFloat(a, i->swf.movieSize.ymin / 20.0);
1444 a = action_SetVariable(a);
1445 a = action_PushString(a, "xmax");
1446 a = action_PushFloat(a, i->swf.movieSize.xmax / 20.0);
1447 a = action_SetVariable(a);
1448 a = action_PushString(a, "ymax");
1449 a = action_PushFloat(a, i->swf.movieSize.ymax / 20.0);
1450 a = action_SetVariable(a);
1451 a = action_PushString(a, "width");
1452 a = action_PushFloat(a, (i->swf.movieSize.xmax - i->swf.movieSize.xmin) / 20.0);
1453 a = action_SetVariable(a);
1454 a = action_PushString(a, "height");
1455 a = action_PushFloat(a, (i->swf.movieSize.ymax - i->swf.movieSize.ymin) / 20.0);
1456 a = action_SetVariable(a);
1458 swf_ActionSet(tag, a);
1462 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1463 swfoutput_pagefeed(obj);
1466 fontlist_t *tmp,*iterator = i->fontlist;
1468 TAG*mtag = i->swf.firstTag;
1469 if(iterator->swffont) {
1470 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1471 if(!config_storeallcharacters)
1472 swf_FontReduce(iterator->swffont);
1473 swf_FontSetDefine2(mtag, iterator->swffont);
1476 iterator = iterator->next;
1478 i->tag = swf_InsertTag(i->tag,ST_END);
1479 TAG* tag = i->tag->prev;
1481 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1482 and the ST_END- they confuse the flash player */
1483 while(tag->id == ST_REMOVEOBJECT2) {
1484 TAG* prev = tag->prev;
1494 SWF* swfoutput_get(struct swfoutput*obj)
1496 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1498 swfoutput_finalize(obj);
1500 return swf_CopySWF(&i->swf);
1503 void swfoutput_getdimensions(struct swfoutput*obj, int*x1, int*y1, int*x2, int*y2)
1505 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1506 if(x1) *x1 = i->swf.movieSize.xmin/20;
1507 if(y1) *y1 = i->swf.movieSize.ymin/20;
1508 if(x2) *x2 = i->swf.movieSize.xmax/20;
1509 if(y2) *y2 = i->swf.movieSize.ymax/20;
1512 int swfoutput_save(struct swfoutput* obj, char*filename)
1514 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1515 swfoutput_finalize(obj);
1519 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1524 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1528 if(config_enablezlib || config_flashversion>=6) {
1529 if FAILED(swf_WriteSWC(fi,&i->swf))
1530 msg("<error> WriteSWC() failed.\n");
1532 if FAILED(swf_WriteSWF(fi,&i->swf))
1533 msg("<error> WriteSWF() failed.\n");
1541 /* Perform cleaning up, complete the swf, and write it out. */
1542 void swfoutput_destroy(struct swfoutput* obj)
1544 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1546 /* not initialized yet- nothing to destroy */
1550 fontlist_t *tmp,*iterator = i->fontlist;
1552 if(iterator->swffont) {
1553 swf_FontFree(iterator->swffont);iterator->swffont=0;
1556 iterator = iterator->next;
1559 swf_FreeTags(&i->swf);
1562 memset(obj, 0, sizeof(swfoutput));
1565 static void swfoutput_setfillcolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1567 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1568 if(obj->fillrgb.r == r &&
1569 obj->fillrgb.g == g &&
1570 obj->fillrgb.b == b &&
1571 obj->fillrgb.a == a) return;
1581 static void swfoutput_setstrokecolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1583 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1584 if(obj->strokergb.r == r &&
1585 obj->strokergb.g == g &&
1586 obj->strokergb.b == b &&
1587 obj->strokergb.a == a) return;
1591 obj->strokergb.r = r;
1592 obj->strokergb.g = g;
1593 obj->strokergb.b = b;
1594 obj->strokergb.a = a;
1597 static void swfoutput_setlinewidth(struct swfoutput*obj, double _linewidth)
1599 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1600 if(i->linewidth == (U16)(_linewidth*20))
1605 i->linewidth = (U16)(_linewidth*20);
1609 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1611 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1613 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1615 if(!strncmp("http://pdf2swf:", url, 15)) {
1616 char*tmp = strdup(url);
1617 int l = strlen(tmp);
1620 swfoutput_namedlink(obj, tmp+15, points);
1630 if(!config_opennewwindow)
1631 actions = action_GetUrl(0, url, "_parent");
1633 actions = action_GetUrl(0, url, "_this");
1634 actions = action_End(actions);
1636 drawlink(obj, actions, 0, points,0);
1638 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1640 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1648 actions = action_GotoFrame(0, page);
1649 actions = action_End(actions);
1651 drawlink(obj, actions, 0, points,0);
1654 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1655 of the viewer objects, like subtitles, index elements etc.
1657 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1659 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1660 ActionTAG *actions1,*actions2;
1661 char*tmp = strdup(name);
1669 if(!strncmp(tmp, "call:", 5))
1671 char*x = strchr(&tmp[5], ':');
1673 actions1 = action_PushInt(0, 0); //number of parameters (0)
1674 actions1 = action_PushString(actions1, &tmp[5]); //function name
1675 actions1 = action_CallFunction(actions1);
1678 actions1 = action_PushString(0, x+1); //parameter
1679 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1680 actions1 = action_PushString(actions1, &tmp[5]); //function name
1681 actions1 = action_CallFunction(actions1);
1683 actions2 = action_End(0);
1688 actions1 = action_PushString(0, "/:subtitle");
1689 actions1 = action_PushString(actions1, name);
1690 actions1 = action_SetVariable(actions1);
1691 actions1 = action_End(actions1);
1693 actions2 = action_PushString(0, "/:subtitle");
1694 actions2 = action_PushString(actions2, "");
1695 actions2 = action_SetVariable(actions2);
1696 actions2 = action_End(actions2);
1699 drawlink(obj, actions1, actions2, points,mouseover);
1701 swf_ActionFree(actions1);
1702 swf_ActionFree(actions2);
1706 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1708 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1713 struct plotxy p1,p2,p3,p4;
1717 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1721 int buttonid = getNewID(obj);
1724 if(points[t].x>xmax) xmax=points[t].x;
1725 if(points[t].y>ymax) ymax=points[t].y;
1726 if(points[t].x<xmin) xmin=points[t].x;
1727 if(points[t].y<ymin) ymin=points[t].y;
1730 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1731 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1733 /* the following code subtracts the upper left edge from all coordinates,
1734 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1735 Necessary for preprocessing with swfcombine. */
1736 posx = xmin; posy = ymin;
1737 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1738 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1739 xmin -= posx; ymin -= posy;
1740 xmax -= posx; ymax -= posy;
1743 myshapeid = getNewID(obj);
1744 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1745 swf_ShapeNew(&i->shape);
1746 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1747 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1748 swf_SetU16(i->tag, myshapeid);
1749 r.xmin = (int)(xmin*20);
1750 r.ymin = (int)(ymin*20);
1751 r.xmax = (int)(xmax*20);
1752 r.ymax = (int)(ymax*20);
1753 swf_SetRect(i->tag,&r);
1754 swf_SetShapeStyles(i->tag,i->shape);
1755 swf_ShapeCountBits(i->shape,NULL,NULL);
1756 swf_SetShapeBits(i->tag,i->shape);
1757 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1758 i->swflastx = i->swflasty = 0;
1759 moveto(obj, i->tag, p1);
1760 lineto(obj, i->tag, p2);
1761 lineto(obj, i->tag, p3);
1762 lineto(obj, i->tag, p4);
1763 lineto(obj, i->tag, p1);
1764 swf_ShapeSetEnd(i->tag);
1767 myshapeid2 = getNewID(obj);
1768 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1769 swf_ShapeNew(&i->shape);
1770 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1772 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1773 swf_SetU16(i->tag, myshapeid2);
1774 r.xmin = (int)(xmin*20);
1775 r.ymin = (int)(ymin*20);
1776 r.xmax = (int)(xmax*20);
1777 r.ymax = (int)(ymax*20);
1778 swf_SetRect(i->tag,&r);
1779 swf_SetShapeStyles(i->tag,i->shape);
1780 swf_ShapeCountBits(i->shape,NULL,NULL);
1781 swf_SetShapeBits(i->tag,i->shape);
1782 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1783 i->swflastx = i->swflasty = 0;
1784 moveto(obj, i->tag, p1);
1785 lineto(obj, i->tag, p2);
1786 lineto(obj, i->tag, p3);
1787 lineto(obj, i->tag, p4);
1788 lineto(obj, i->tag, p1);
1789 swf_ShapeSetEnd(i->tag);
1793 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1794 swf_SetU16(i->tag,buttonid); //id
1795 swf_ButtonSetFlags(i->tag, 0); //menu=no
1796 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1797 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1798 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1799 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1800 swf_SetU8(i->tag,0);
1801 swf_ActionSet(i->tag,actions1);
1802 swf_SetU8(i->tag,0);
1806 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1807 swf_SetU16(i->tag,buttonid); //id
1808 swf_ButtonSetFlags(i->tag, 0); //menu=no
1809 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1810 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1811 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1812 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1813 swf_SetU8(i->tag,0); // end of button records
1814 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1815 swf_ActionSet(i->tag,actions1);
1817 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1818 swf_ActionSet(i->tag,actions2);
1819 swf_SetU8(i->tag,0);
1820 swf_ButtonPostProcess(i->tag, 2);
1822 swf_SetU8(i->tag,0);
1823 swf_ButtonPostProcess(i->tag, 1);
1827 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1829 if(posx!=0 || posy!=0) {
1831 p.x = (int)(posx*20);
1832 p.y = (int)(posy*20);
1833 p = swf_TurnPoint(p, &i->page_matrix);
1838 swf_ObjectPlace(i->tag, buttonid, getNewDepth(obj),&m,0,0);
1840 swf_ObjectPlace(i->tag, buttonid, getNewDepth(obj),&i->page_matrix,0,0);
1847 for(t=0;t<picpos;t++)
1849 if(pic_xids[t] == xid &&
1850 pic_yids[t] == yid) {
1851 width = pic_width[t];
1852 height = pic_height[t];
1856 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1857 pic_xids[picpos] = xid;
1858 pic_yids[picpos] = yid;
1859 pic_width[picpos] = width;
1860 pic_height[picpos] = height;
1863 pic[width*y+x] = buf[0];
1867 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1868 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1872 xid += x*r+x*b*3+x*g*7+x*a*11;
1873 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1875 for(t=0;t<picpos;t++)
1877 if(pic_xids[t] == xid &&
1878 pic_yids[t] == yid) {
1886 static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
1888 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1889 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1894 /* check whether the next segment is zero */
1895 if(line->type == gfx_moveTo) {
1896 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1897 moveto(obj, i->tag, line->x, line->y);
1898 px = lastx = line->x;
1899 py = lasty = line->y;
1901 } if(line->type == gfx_lineTo) {
1902 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1903 lineto(obj, i->tag, line->x, line->y);
1907 } else if(line->type == gfx_splineTo) {
1908 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1910 s.x = line->sx;p.x = line->x;
1911 s.y = line->sy;p.y = line->y;
1912 splineto(obj, i->tag, s, p);
1921 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)
1923 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1924 gfxdevice_t*dev = &i->device;
1925 dev->stroke(dev, line, width, col, cap_style, joint_style, miterLimit);
1927 void swfoutput_fillgfxline(struct swfoutput*obj, gfxline_t*line, gfxcolor_t*col)
1929 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1930 gfxdevice_t*dev = &i->device;
1931 dev->fill(dev, line, col);
1933 void swfoutput_startclip(struct swfoutput*obj, gfxline_t*line)
1935 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1936 gfxdevice_t*dev = &i->device;
1937 dev->startclip(dev, line);
1939 void swfoutput_endclip(struct swfoutput*obj)
1941 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1942 gfxdevice_t*dev = &i->device;
1945 void swfoutput_gfxaddfont(struct swfoutput*obj, char*fontid, gfxfont_t*font)
1947 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1948 gfxdevice_t*dev = &i->device;
1949 dev->addfont(dev, fontid, font);
1951 void swfoutput_gfxdrawchar(struct swfoutput*obj, char*fontid, int glyph, gfxcolor_t*c, gfxmatrix_t*m)
1953 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1954 gfxdevice_t*dev = &i->device;
1955 dev->drawchar(dev, fontid, glyph, c, m);
1958 #define IMAGE_TYPE_JPEG 0
1959 #define IMAGE_TYPE_LOSSLESS 1
1961 static void swfoutput_drawimage(struct swfoutput*obj, RGBA* data, int sizex,int sizey,
1962 double x1,double y1,
1963 double x2,double y2,
1964 double x3,double y3,
1965 double x4,double y4, int type)
1967 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1971 double l1 = sqrt((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1));
1972 double l2 = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
1974 gfxline_t p1,p2,p3,p4,p5;
1975 p1.type=gfx_moveTo;p1.x=x1; p1.y=y1;p1.next=&p2;
1976 p2.type=gfx_lineTo;p2.x=x2; p2.y=y2;p2.next=&p3;
1977 p3.type=gfx_lineTo;p3.x=x3; p3.y=y3;p3.next=&p4;
1978 p4.type=gfx_lineTo;p4.x=x4; p4.y=y4;p4.next=&p5;
1979 p5.type=gfx_lineTo;p5.x=x1; p5.y=y1;p5.next=0;
1981 {p1.x = (int)(p1.x*20)/20.0;
1982 p1.y = (int)(p1.y*20)/20.0;
1983 p2.x = (int)(p2.x*20)/20.0;
1984 p2.y = (int)(p2.y*20)/20.0;
1985 p3.x = (int)(p3.x*20)/20.0;
1986 p3.y = (int)(p3.y*20)/20.0;
1987 p4.x = (int)(p4.x*20)/20.0;
1988 p4.y = (int)(p4.y*20)/20.0;
1989 p5.x = (int)(p5.x*20)/20.0;
1990 p5.y = (int)(p5.y*20)/20.0;
1997 m.m00 = (p4.x-p1.x)/sizex; m.m10 = (p2.x-p1.x)/sizey;
1998 m.m01 = (p4.y-p1.y)/sizex; m.m11 = (p2.y-p1.y)/sizey;
2003 img.data = (gfxcolor_t*)data;
2007 if(type == IMAGE_TYPE_JPEG)
2008 /* TODO: pass image_dpi to device instead */
2009 i->device.setparameter(&i->device, "next_bitmap_is_jpeg", "1");
2011 i->device.fillbitmap(&i->device, &p1, &img, &m, 0);
2014 void swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
2015 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
2017 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_JPEG);
2020 void swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
2021 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
2023 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_LOSSLESS);
2026 void swfoutput_setparameter(char*name, char*value)
2028 if(!strcmp(name, "jpegsubpixels")) {
2029 config_jpegsubpixels = atof(value);
2030 } else if(!strcmp(name, "ppmsubpixels")) {
2031 config_ppmsubpixels = atof(value);
2032 } else if(!strcmp(name, "drawonlyshapes")) {
2033 config_drawonlyshapes = atoi(value);
2034 } else if(!strcmp(name, "ignoredraworder")) {
2035 config_ignoredraworder = atoi(value);
2036 } else if(!strcmp(name, "filloverlap")) {
2037 config_filloverlap = atoi(value);
2038 } else if(!strcmp(name, "linksopennewwindow")) {
2039 config_opennewwindow = atoi(value);
2040 } else if(!strcmp(name, "opennewwindow")) {
2041 config_opennewwindow = atoi(value);
2042 } else if(!strcmp(name, "storeallcharacters")) {
2043 config_storeallcharacters = atoi(value);
2044 } else if(!strcmp(name, "enablezlib")) {
2045 config_enablezlib = atoi(value);
2046 } else if(!strcmp(name, "bboxvars")) {
2047 config_bboxvars = atoi(value);
2048 } else if(!strcmp(name, "insertstop")) {
2049 config_insertstoptag = atoi(value);
2050 } else if(!strcmp(name, "protected")) {
2051 config_protect = atoi(value);
2052 } else if(!strcmp(name, "flashversion")) {
2053 config_flashversion = atoi(value);
2054 } else if(!strcmp(name, "minlinewidth")) {
2055 config_minlinewidth = atof(value);
2056 } else if(!strcmp(name, "caplinewidth")) {
2057 config_caplinewidth = atof(value);
2058 } else if(!strcmp(name, "dumpfonts")) {
2059 config_dumpfonts = atoi(value);
2060 } else if(!strcmp(name, "jpegquality")) {
2061 int val = atoi(value);
2063 if(val>100) val=100;
2064 config_jpegquality = val;
2065 } else if(!strcmp(name, "splinequality")) {
2066 int v = atoi(value);
2067 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2069 config_splinemaxerror = v;
2070 } else if(!strcmp(name, "fontquality")) {
2071 int v = atoi(value);
2072 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2074 config_fontsplinemaxerror = v;
2076 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
2080 // --------------------------------------------------------------------
2082 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
2085 swf_GetCXForm(0, &cx, 1);
2088 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
2089 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
2090 c->br!=0 || c->bg!=0 || c->ba!=0 ||
2091 c->ar!=0 || c->ag!=0 || c->ab!=0)
2092 msg("<warning> CXForm not SWF-compatible");
2094 cx.a0 = (S16)(c->aa*256);
2095 cx.r0 = (S16)(c->rr*256);
2096 cx.g0 = (S16)(c->gg*256);
2097 cx.b0 = (S16)(c->bb*256);
2105 ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
2107 ArtVpath *vec = NULL;
2115 if(l2->type == gfx_moveTo) {
2117 } if(l2->type == gfx_lineTo) {
2119 } if(l2->type == gfx_splineTo) {
2120 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2121 if(!parts) parts = 1;
2131 vec = art_new (ArtVpath, len);
2136 if(l2->type == gfx_moveTo) {
2137 vec[pos].code = ART_MOVETO;
2142 } else if(l2->type == gfx_lineTo) {
2143 vec[pos].code = ART_LINETO;
2148 } else if(l2->type == gfx_splineTo) {
2150 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2151 if(!parts) parts = 1;
2152 for(i=0;i<=parts;i++) {
2153 double t = (double)i/(double)parts;
2154 vec[pos].code = ART_LINETO;
2155 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
2156 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
2165 vec[pos].code = ART_END;
2167 svp = art_svp_vpath_stroke (vec,
2168 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
2169 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
2170 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
2171 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
2172 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
2173 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
2175 miterLimit, //miter_limit
2182 gfxline_t* SVPtogfxline(ArtSVP*svp)
2187 for(t=0;t<svp->n_segs;t++) {
2188 size += svp->segs[t].n_points + 1;
2190 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
2192 for(t=0;t<svp->n_segs;t++) {
2193 ArtSVPSeg* seg = &svp->segs[t];
2195 for(p=0;p<seg->n_points;p++) {
2196 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
2197 ArtPoint* point = &seg->points[p];
2198 lines[pos].x = point->x;
2199 lines[pos].y = point->y;
2200 lines[pos].next = &lines[pos+1];
2205 lines[pos-1].next = 0;
2213 static int imageInCache(swfoutput*obj, void*data, int width, int height)
2217 static void addImageToCache(swfoutput*obj, void*data, int width, int height)
2221 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
2223 swfoutput*obj = i->obj;
2225 RGBA*mem = (RGBA*)img->data;
2227 int sizex = img->width;
2228 int sizey = img->height;
2229 int is_jpeg = i->jpeg;
2232 int newsizex=sizex, newsizey=sizey;
2235 if(is_jpeg && config_jpegsubpixels) {
2236 newsizex = (int)(targetwidth*config_jpegsubpixels+0.5);
2237 newsizey = (int)(targetheight*config_jpegsubpixels+0.5);
2238 } else if(!is_jpeg && config_ppmsubpixels) {
2239 newsizex = (int)(targetwidth*config_ppmsubpixels+0.5);
2240 newsizey = (int)(targetheight*config_ppmsubpixels+0.5);
2244 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
2247 /* TODO: cache images */
2251 if(newsizex<sizex || newsizey<sizey) {
2252 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
2253 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
2254 *newwidth = sizex = newsizex;
2255 *newheight = sizey = newsizey;
2260 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
2261 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
2263 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
2265 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
2268 targetwidth, targetheight,
2269 /*newsizex, newsizey,*/
2270 num_colors>256?">":"", num_colors>256?256:num_colors);
2272 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
2273 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
2275 for(t=0;t<num_colors;t++) {
2276 printf("%02x%02x%02x%02x ",
2277 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
2284 int cacheid = imageInCache(obj, mem, sizex, sizey);
2287 bitid = getNewID(obj);
2288 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, config_jpegquality);
2289 addImageToCache(obj, mem, sizex, sizey);
2299 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2301 gfxbbox_t bbox = gfxline_getbbox(line);
2303 r.xmin = (int)(bbox.xmin*20);
2304 r.ymin = (int)(bbox.ymin*20);
2305 r.xmax = (int)(bbox.xmax*20);
2306 r.ymax = (int)(bbox.ymax*20);
2310 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2312 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2313 swfoutput*obj = i->obj;
2318 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2319 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2321 int newwidth=0,newheight=0;
2322 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2325 double fx = (double)img->width / (double)newwidth;
2326 double fy = (double)img->height / (double)newheight;
2331 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2332 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2333 m.tx = (int)(matrix->tx*20);
2334 m.ty = (int)(matrix->ty*20);
2337 int myshapeid = getNewID(obj);
2338 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2340 swf_ShapeNew(&shape);
2341 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2342 swf_SetU16(i->tag, myshapeid);
2343 SRECT r = gfxline_getSWFbbox(line);
2344 swf_SetRect(i->tag,&r);
2345 swf_SetShapeStyles(i->tag,shape);
2346 swf_ShapeCountBits(shape,NULL,NULL);
2347 swf_SetShapeBits(i->tag,shape);
2348 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2349 i->swflastx = i->swflasty = UNDEFINED_COORD;
2350 drawgfxline(obj, line);
2351 swf_ShapeSetEnd(i->tag);
2352 swf_ShapeFree(shape);
2354 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2355 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2356 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(obj),&i->page_matrix,&cxform2,NULL);
2359 void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2361 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2362 swfoutput*obj = i->obj;
2367 if(i->clippos >= 127)
2369 msg("<warning> Too many clip levels.");
2373 int myshapeid = getNewID(obj);
2374 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2376 memset(&col, 0, sizeof(RGBA));
2378 swf_ShapeNew(&shape);
2379 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2380 swf_SetU16(i->tag,myshapeid);
2381 SRECT r = gfxline_getSWFbbox(line);
2382 swf_SetRect(i->tag,&r);
2383 swf_SetShapeStyles(i->tag,shape);
2384 swf_ShapeCountBits(shape,NULL,NULL);
2385 swf_SetShapeBits(i->tag,shape);
2386 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2387 i->swflastx = i->swflasty = UNDEFINED_COORD;
2388 drawgfxline(obj, line);
2389 swf_ShapeSetEnd(i->tag);
2390 swf_ShapeFree(shape);
2392 /* TODO: remember the bbox, and check all shapes against it */
2394 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2395 i->cliptags[i->clippos] = i->tag;
2396 i->clipshapes[i->clippos] = myshapeid;
2397 i->clipdepths[i->clippos] = getNewDepth(obj);
2401 void swf_endclip(gfxdevice_t*dev)
2403 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2404 swfoutput*obj = i->obj;
2411 msg("<error> Invalid end of clipping region");
2415 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2416 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2418 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2420 int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
2422 if(!strcmp(key, "next_bitmap_is_jpeg")) {
2423 ((swfoutput_internal*)dev->internal)->jpeg = 1;
2429 int gfxline_type(gfxline_t*line)
2435 int haszerosegments=0;
2437 if(line->type == gfx_moveTo) {
2440 } else if(line->type == gfx_lineTo) {
2444 } else if(line->type == gfx_splineTo) {
2446 if(tmpsplines>lines)
2451 if(lines==0 && splines==0) return 0;
2452 else if(lines==1 && splines==0) return 1;
2453 else if(lines==0 && splines==1) return 2;
2454 else if(splines==0) return 3;
2458 int gfxline_has_dots(gfxline_t*line)
2465 if(line->type == gfx_moveTo) {
2466 if(isline && dist < 1) {
2471 } else if(line->type == gfx_lineTo) {
2472 dist += fabs(line->x - x) + fabs(line->y - y);
2474 } else if(line->type == gfx_splineTo) {
2475 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2476 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2483 if(isline && dist < 1) {
2489 int gfxline_fix_short_edges(gfxline_t*line)
2493 if(line->type == gfx_lineTo) {
2494 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2497 } else if(line->type == gfx_splineTo) {
2498 if(fabs(line->sx - x) + fabs(line->sy - y) +
2499 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2512 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)
2514 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2515 swfoutput*obj = i->obj;
2516 int type = gfxline_type(line);
2517 int has_dots = gfxline_has_dots(line);
2519 /* TODO: * split line into segments, and perform this check for all segments */
2521 (width <= config_caplinewidth
2522 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2523 || (cap_style == gfx_capRound && type<=2))) {
2524 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2526 swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
2527 swfoutput_setlinewidth(obj, width);
2530 drawgfxline(obj, line);
2532 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2534 gfxline_fix_short_edges(line);
2535 /* we need to convert the line into a polygon */
2536 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2537 gfxline_t*gfxline = SVPtogfxline(svp);
2538 dev->fill(dev, gfxline, color);
2543 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2545 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2546 swfoutput*obj = i->obj;
2548 if(!config_ignoredraworder)
2550 swfoutput_setfillcolor(obj, color->r, color->g, color->b, color->a);
2554 drawgfxline(obj, line);
2555 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2557 void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2559 msg("<error> Gradient filling not implemented yet");
2562 void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
2564 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2565 swfoutput*obj = i->obj;
2571 int myshapeid = getNewID(obj);
2573 black.r = black.g = black.b = black.a = 0;
2574 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2576 swf_ShapeNew(&shape);
2577 int fsid = swf_ShapeAddSolidFillStyle(shape,&black);
2578 swf_SetU16(i->tag, myshapeid);
2579 SRECT r = gfxline_getSWFbbox(line);
2580 swf_SetRect(i->tag,&r);
2581 swf_SetShapeStyles(i->tag,shape);
2582 swf_ShapeCountBits(shape,NULL,NULL);
2583 swf_SetShapeBits(i->tag,shape);
2584 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2585 i->swflastx = i->swflasty = UNDEFINED_COORD;
2586 drawgfxline(obj, line);
2587 swf_ShapeSetEnd(i->tag);
2588 swf_ShapeFree(shape);
2590 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2591 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(obj),&i->page_matrix,0,NULL);
2594 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2596 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2599 swffont->version = 2;
2600 swffont->name = (U8*)strdup(id);
2601 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2602 swffont->layout->ascent = 0; /* ? */
2603 swffont->layout->descent = 0;
2604 swffont->layout->leading = 0;
2605 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2606 swffont->encoding = FONT_ENCODING_UNICODE;
2607 swffont->numchars = font->num_glyphs;
2608 swffont->maxascii = font->max_unicode;
2609 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2610 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2611 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2612 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2613 for(t=0;t<font->max_unicode;t++) {
2614 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2616 for(t=0;t<font->num_glyphs;t++) {
2619 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2620 if(font->glyphs[t].name) {
2621 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2623 swffont->glyphnames[t] = 0;
2625 swffont->glyph[t].advance = (int)(font->glyphs[t].advance * 20);
2627 swf_Shape01DrawerInit(&draw, 0);
2628 line = font->glyphs[t].line;
2631 c.x = line->sx; c.y = line->sy;
2632 to.x = line->x; to.y = line->y;
2633 if(line->type == gfx_moveTo) {
2634 draw.moveTo(&draw, &to);
2635 } else if(line->type == gfx_lineTo) {
2636 draw.lineTo(&draw, &to);
2637 } else if(line->type == gfx_splineTo) {
2638 draw.splineTo(&draw, &c, &to);
2643 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2644 draw.dealloc(&draw);
2649 void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2651 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2653 if(i->obj->swffont && i->obj->swffont->name && !strcmp((char*)i->obj->swffont->name,fontid))
2654 return; // the requested font is the current font
2656 fontlist_t*last=0,*l = i->fontlist;
2659 if(!strcmp((char*)l->swffont->name, fontid)) {
2660 return; // we already know this font
2664 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2665 l->swffont = gfxfont_to_swffont(font, fontid);
2672 swf_FontSetID(l->swffont, getNewID(i->obj));
2674 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2675 // print font information
2676 msg("<debug> Font %s",fontid);
2677 msg("<debug> | ID: %d", l->swffont->id);
2678 msg("<debug> | Version: %d", l->swffont->version);
2679 msg("<debug> | Name: %s", l->swffont->name);
2680 msg("<debug> | Numchars: %d", l->swffont->numchars);
2681 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2682 msg("<debug> | Style: %d", l->swffont->style);
2683 msg("<debug> | Encoding: %d", l->swffont->encoding);
2684 for(int iii=0; iii<l->swffont->numchars;iii++) {
2685 msg("<debug> | Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, l->swffont->glyphnames?l->swffont->glyphnames[iii]:"<nonames>", l->swffont->glyph2ascii[iii], l->swffont->glyph[iii].shape->bitlen,
2686 l->swffont->layout->bounds[iii].xmin/20.0,
2687 l->swffont->layout->bounds[iii].ymin/20.0,
2688 l->swffont->layout->bounds[iii].xmax/20.0,
2689 l->swffont->layout->bounds[iii].ymax/20.0
2692 for(t=0;t<l->swffont->maxascii;t++) {
2693 if(l->swffont->ascii2glyph[t] == iii)
2694 msg("<debug> | - maps to %d",t);
2700 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2702 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2703 swfoutput*obj = i->obj;
2705 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
2706 return; // the requested font is the current font
2708 fontlist_t*l = i->fontlist;
2710 if(!strcmp((char*)l->swffont->name, fontid)) {
2711 obj->swffont = l->swffont;
2716 msg("<error> Unknown font id: %s", fontid);
2720 void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2722 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2723 swfoutput*obj = i->obj;
2725 if(!obj->swffont || !obj->swffont->name || strcmp((char*)obj->swffont->name,fontid)) // not equal to current font
2727 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2728 with multiple fonts */
2731 swf_switchfont(dev, fontid); // set the current font
2733 swfoutput_setfontmatrix(obj, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2736 m.m11 = obj->fontm11;
2737 m.m12 = obj->fontm12;
2738 m.m21 = obj->fontm21;
2739 m.m22 = obj->fontm22;
2742 drawchar(obj, obj->swffont, 0, glyph, -1, &m, color);