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);
162 int getCharID(SWFFONT *font, int charnr, char *charname, int u);
164 static swfoutput_internal* init_internal_struct()
166 swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
167 memset(i, 0, sizeof(swfoutput_internal));
189 i->fillstylechanged = 0;
196 i->device.internal = (void*)i;
197 i->device.fillbitmap = swf_fillbitmap;
198 i->device.setparameter = swf_setparameter;
199 i->device.stroke = swf_stroke;
200 i->device.startclip = swf_startclip;
201 i->device.endclip = swf_endclip;
202 i->device.fill = swf_fill;
203 i->device.fillbitmap = swf_fillbitmap;
204 i->device.fillgradient = swf_fillgradient;
205 i->device.addfont = swf_addfont;
206 i->device.drawchar = swf_drawchar;
211 static int id_error = 0;
213 static U16 getNewID(struct swfoutput* obj)
215 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
216 if(i->currentswfid == 65535) {
218 msg("<error> ID Table overflow");
222 return ++i->currentswfid;
224 static U16 getNewDepth(struct swfoutput* obj)
226 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
227 if(i->depth == 65535) {
229 msg("<error> Depth Table overflow");
236 static void startshape(struct swfoutput* obj);
237 static void starttext(struct swfoutput* obj);
238 static void endshape(struct swfoutput* obj);
239 static void endtext(struct swfoutput* obj);
241 // matrix multiplication. changes p0
242 static void transform (plotxy*p0,struct swfmatrix*m)
245 x = m->m11*p0->x+m->m12*p0->y;
246 y = m->m21*p0->x+m->m22*p0->y;
251 // write a move-to command into the swf
252 static int moveto(struct swfoutput*obj, TAG*tag, plotxy p0)
254 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
255 int rx = (int)(p0.x*20);
256 int ry = (int)(p0.y*20);
257 if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
258 swf_ShapeSetMove (tag, i->shape, rx,ry);
259 i->fillstylechanged = 0;
266 static int moveto(struct swfoutput*obj, TAG*tag, double x, double y)
268 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
272 return moveto(obj, tag, p);
274 static void addPointToBBox(struct swfoutput*obj, int px, int py)
276 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
282 swf_ExpandRect(&i->bboxrect, p);
284 swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
288 /*static void plot(struct swfoutput*obj, int x, int y, TAG*tag)
290 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
291 int width = i->linewidth/4;
295 //swf_ShapeSetLine(tag, i->shape,-width,-width);
296 //swf_ShapeSetLine(tag, i->shape,width*2,0);
297 //swf_ShapeSetLine(tag, i->shape,0,width*2);
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,width);
303 swf_ShapeSetLine(tag, i->shape,-width,0);
304 swf_ShapeSetLine(tag, i->shape,width,-width);
305 swf_ShapeSetLine(tag, i->shape,width,width);
306 swf_ShapeSetLine(tag, i->shape,-width,width);
307 swf_ShapeSetLine(tag, i->shape,-width,-width);
308 swf_ShapeSetLine(tag, i->shape,width,0);
310 addPointToBBox(obj, x-width ,y-width);
311 addPointToBBox(obj, x+width ,y+width);
314 // write a line-to command into the swf
315 static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0)
317 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
318 int px = (int)(p0.x*20);
319 int py = (int)(p0.y*20);
320 int rx = (px-i->swflastx);
321 int ry = (py-i->swflasty);
323 swf_ShapeSetLine (tag, i->shape, rx,ry);
324 addPointToBBox(obj, i->swflastx,i->swflasty);
325 addPointToBBox(obj, px,py);
326 }/* else if(!i->fill) {
327 // treat lines of length 0 as plots, making them
328 // at least 1 twip wide so Flash will display them
329 plot(obj, i->swflastx, i->swflasty, tag);
336 static void lineto(struct swfoutput*obj, TAG*tag, double x, double y)
344 // write a spline-to command into the swf
345 static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end)
347 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
348 int lastlastx = i->swflastx;
349 int lastlasty = i->swflasty;
351 int cx = ((int)(control.x*20)-i->swflastx);
352 int cy = ((int)(control.y*20)-i->swflasty);
355 int ex = ((int)(end.x*20)-i->swflastx);
356 int ey = ((int)(end.y*20)-i->swflasty);
360 if(cx || cy || ex || ey) {
361 swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
362 addPointToBBox(obj, lastlastx ,lastlasty );
363 addPointToBBox(obj, lastlastx+cx,lastlasty+cy);
364 addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey);
365 }/* else if(!i->fill) {
366 // treat splines of length 0 as plots
367 plot(obj, lastlastx, lastlasty, tag);
372 /* write a line, given two points and the transformation
374 /*static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1)
376 moveto(obj, tag, p0);
377 lineto(obj, tag, p1);
380 void resetdrawer(struct swfoutput*obj)
382 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
387 static void stopFill(struct swfoutput*obj)
389 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
392 swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
393 i->fillstylechanged = 1;
397 static void startFill(struct swfoutput*obj)
399 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
402 swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
403 i->fillstylechanged = 1;
408 /* draw an outline. These are generated by pdf2swf and by t1lib
409 (representing characters). */
410 /*void drawpath(struct swfoutput*obj, SWF_OUTLINE*outline, struct swfmatrix*m, int log)
412 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
413 if( i->tag->id != ST_DEFINESHAPE &&
414 i->tag->id != ST_DEFINESHAPE2 &&
415 i->tag->id != ST_DEFINESHAPE3)
417 msg("<error> internal error: drawpath needs a shape tag, not %d\n",i->tag->id);
421 double lastx=0,lasty=0;
422 double firstx=0,firsty=0;
427 x += (outline->dest.x/(float)0xffff);
428 y += (outline->dest.y/(float)0xffff);
429 if(outline->type == SWF_PATHTYPE_MOVE)
431 //if(!init && fill && obj->drawmode != DRAWMODE_EOFILL && !ignoredraworder)
432 if(config_filloverlap && !init && i->fill && obj->drawmode != DRAWMODE_EOFILL) {
433 // drawmode=FILL (not EOFILL) means that
434 // seperate shapes do not cancel each other out.
435 // On SWF side, we need to start a new shape for each
436 // closed polygon, because SWF only knows EOFILL.
443 if(((int)(lastx*20) != (int)(firstx*20) ||
444 (int)(lasty*20) != (int)(firsty*20)) &&
453 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
454 line(obj,i->tag, p0, p1);
460 else if(outline->type == SWF_PATHTYPE_LINE)
468 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
469 line(obj,i->tag, p0,p1);
472 msg("<error> drawpath: unknown outline type:%d\n", outline->type);
476 outline = outline->link;
478 if(((int)(lastx*20) != (int)(firstx*20) ||
479 (int)(lasty*20) != (int)(firsty*20)) &&
488 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
489 line(obj, i->tag, p0, p1);
493 static inline int colorcompare(struct swfoutput*obj, RGBA*a,RGBA*b)
505 static SRECT getcharacterbbox(struct swfoutput*obj, SWFFONT*font)
507 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
510 memset(&r, 0, sizeof(r));
513 if(debug) printf("\n");
514 for(t=0;t<i->chardatapos;t++)
516 if(i->chardata[t].fontid != font->id) {
517 msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
520 SRECT b = font->layout->bounds[i->chardata[t].charid];
521 b.xmin *= i->chardata[t].size;
522 b.ymin *= i->chardata[t].size;
523 b.xmax *= i->chardata[t].size;
524 b.ymax *= i->chardata[t].size;
529 b.xmin += i->chardata[t].x;
530 b.ymin += i->chardata[t].y;
531 b.xmax += i->chardata[t].x;
532 b.ymax += i->chardata[t].y;
534 /* until we solve the INTERNAL_SCALING problem (see below)
535 make sure the bounding box is big enough */
541 if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
542 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
543 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
544 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
545 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
550 i->chardata[t].fontid,
552 i->chardata[t].charid
554 swf_ExpandRect2(&r, &b);
556 if(debug) printf("-----> (%f,%f,%f,%f)\n",
564 static void putcharacters(struct swfoutput*obj, TAG*tag)
566 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
570 color.r = i->chardata[0].color.r^255;
579 int charadvance[128];
582 int glyphbits=1; //TODO: can this be zero?
585 if(tag->id != ST_DEFINETEXT &&
586 tag->id != ST_DEFINETEXT2) {
587 msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
590 if(!i->chardatapos) {
591 msg("<warning> putcharacters called with zero characters");
594 for(pass = 0; pass < 2; pass++)
604 advancebits++; // add sign bit
605 swf_SetU8(tag, glyphbits);
606 swf_SetU8(tag, advancebits);
609 for(t=0;t<=i->chardatapos;t++)
611 if(lastfontid != i->chardata[t].fontid ||
612 lastx!=i->chardata[t].x ||
613 lasty!=i->chardata[t].y ||
614 !colorcompare(obj,&color, &i->chardata[t].color) ||
616 lastsize != i->chardata[t].size ||
619 if(charstorepos && pass==0)
622 for(s=0;s<charstorepos;s++)
624 while(charids[s]>=(1<<glyphbits))
626 while(charadvance[s]>=(1<<advancebits))
630 if(charstorepos && pass==1)
632 tag->writeBit = 0; // Q&D
633 swf_SetBits(tag, 0, 1); // GLYPH Record
634 swf_SetBits(tag, charstorepos, 7); // number of glyphs
636 for(s=0;s<charstorepos;s++)
638 swf_SetBits(tag, charids[s], glyphbits);
639 swf_SetBits(tag, charadvance[s], advancebits);
644 if(pass == 1 && t<i->chardatapos)
650 if(lastx != i->chardata[t].x ||
651 lasty != i->chardata[t].y)
653 newx = i->chardata[t].x;
654 newy = i->chardata[t].y;
660 if(!colorcompare(obj,&color, &i->chardata[t].color))
662 color = i->chardata[t].color;
665 font.id = i->chardata[t].fontid;
666 if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
669 tag->writeBit = 0; // Q&D
670 swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
673 lastfontid = i->chardata[t].fontid;
674 lastx = i->chardata[t].x;
675 lasty = i->chardata[t].y;
676 lastsize = i->chardata[t].size;
679 if(t==i->chardatapos)
683 int nextt = t==i->chardatapos-1?t:t+1;
684 int rel = i->chardata[nextt].x-i->chardata[t].x;
685 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
687 lastx=i->chardata[nextt].x;
691 lastx=i->chardata[t].x;
693 charids[charstorepos] = i->chardata[t].charid;
694 charadvance[charstorepos] = advance;
701 static void putcharacter(struct swfoutput*obj, int fontid, int charid, int x,int y, int size, RGBA color)
703 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
704 if(i->chardatapos == CHARDATAMAX)
706 msg("<warning> Character buffer too small. SWF will be slightly bigger");
710 i->chardata[i->chardatapos].fontid = fontid;
711 i->chardata[i->chardatapos].charid = charid;
712 i->chardata[i->chardatapos].x = x;
713 i->chardata[i->chardatapos].y = y;
714 i->chardata[i->chardatapos].color = color;
715 i->chardata[i->chardatapos].size = size;
719 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
720 So if we set this value to high, the char coordinates will overflow.
721 If we set it to low, however, the char positions will be inaccurate */
722 #define FONT_INTERNAL_SIZE 4
724 static int font_active = 0;
725 static char* font_active_filename = 0;
727 /* process a character. */
728 static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int charnr, int u, swfmatrix*m, gfxcolor_t*col)
730 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
732 msg("<warning> Font is NULL");
736 int charid = getCharID(swffont, charnr, character, u);
739 sprintf(buf, "%s.usage", font_active_filename);
740 FILE*fi = fopen(buf, "ab+");
742 fprintf(fi, "%d %d %d %s\n", charnr, u, charid, character);
745 msg("<error> Couldn't write to %s", buf);
749 msg("<warning> Didn't find character '%s' (c=%d,u=%d) in current charset (%s, %d characters)",
750 FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
753 /*if(swffont->glyph[charid].shape->bitlen <= 16) {
754 msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty",
755 FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
767 float det = ((m->m11*m->m22)-(m->m21*m->m12));
768 if(fabs(det) < 0.0005) {
769 /* x direction equals y direction- the text is invisible */
772 det = 20*FONT_INTERNAL_SIZE / det;
775 p.x = (SCOORD)(( x * m->m22 - y * m->m12)*det);
776 p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
778 RGBA rgba = *(RGBA*)col;
779 putcharacter(obj, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
780 swf_FontUseGlyph(swffont, charid);
785 SWF_OUTLINE*outline = font->getOutline(character, charnr);
786 char* charname = character;
789 msg("<warning> Didn't find character '%s' (%d) in current charset (%s)",
790 FIXNULL(character),charnr,FIXNULL(font->getName()));
809 drawpath(tag, outline, &m2, 0);
814 static void endtext(swfoutput*obj)
816 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
820 i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
821 swf_SetU16(i->tag, i->textid);
824 r = getcharacterbbox(obj, obj->swffont);
826 swf_SetRect(i->tag,&r);
829 swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
830 swf_SetMatrix(i->tag,&m);
832 msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
834 putcharacters(obj, i->tag);
836 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
838 swf_MatrixJoin(&m2,&obj->fontmatrix, &i->page_matrix);
840 swf_ObjectPlace(i->tag,i->textid,getNewDepth(obj),&m2,NULL,NULL);
844 int getCharID(SWFFONT *font, int charnr, char *charname, int u)
847 if(charname && font->glyphnames) {
848 for(t=0;t<font->numchars;t++) {
849 if(font->glyphnames[t] && !strcmp(font->glyphnames[t],charname)) {
850 msg("<debug> Char [%d,>%s<,%d] maps to %d\n", charnr, charname, u, t);
854 /* if we didn't find the character, maybe
855 we can find the capitalized version */
856 for(t=0;t<font->numchars;t++) {
857 if(font->glyphnames[t] && !strcasecmp(font->glyphnames[t],charname)) {
858 msg("<debug> Char [%d,>>%s<<,%d] maps to %d\n", charnr, charname, u, t);
864 if(u>0 && font->encoding != 255) {
865 /* try to use the unicode id */
866 if(u>=0 && u<font->maxascii && font->ascii2glyph[u]>=0) {
867 msg("<debug> Char [%d,%s,>%d<] maps to %d\n", charnr, charname, u, font->ascii2glyph[u]);
868 return font->ascii2glyph[u];
872 if(font->encoding != FONT_ENCODING_UNICODE) {
873 /* the following only works if the font encoding
874 is US-ASCII based. It's needed for fonts which return broken unicode
876 if(charnr>=0 && charnr<font->maxascii && font->ascii2glyph[charnr]>=0) {
877 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, font->ascii2glyph[charnr]);
878 return font->ascii2glyph[charnr];
882 if(charnr>=0 && charnr<font->numchars) {
883 msg("<debug> Char [>%d<,%s,%d] maps to %d\n", charnr, charname, u, charnr);
890 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
891 void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
893 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
895 fontlist_t*last=0,*iterator;
897 msg("<error> No fontid");
901 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
904 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
905 with multiple fonts */
908 iterator = i->fontlist;
910 if(!strcmp((char*)iterator->swffont->name,fontid)) {
911 obj->swffont = iterator->swffont;
915 iterator = iterator->next;
919 msg("<error> No filename given for font- internal error?");
923 swf_SetLoadFontParameters(64,/*skip unused*/0,/*full unicode*/1);
924 SWFFONT*swffont = swf_LoadFont(filename);
926 if(config_dumpfonts) {
928 font_active_filename = strdup(filename);
932 msg("<warning> Couldn't load font %s (%s)", fontid, filename);
933 swffont = swf_LoadFont(0);
936 if(swffont->glyph2ascii) {
939 /* check whether the Unicode indices look o.k.
940 If they don't, disable the unicode lookup by setting
941 the encoding to 255 */
942 for(t=0;t<swffont->numchars;t++) {
943 int c = swffont->glyph2ascii[t];
944 if(c && c < 32 && swffont->glyph[t].shape->bitlen > 16) {
945 // the character maps into the unicode control character range
946 // between 0001-001f. Yet it is not empty. Treat the one
947 // mapping as broken, and look how many of those we find.
952 msg("<warning> Font %s has bad unicode mapping", fontid);
953 swffont->encoding = 255;
957 if(swffont->encoding != FONT_ENCODING_UNICODE && swffont->encoding != 255) {
958 msg("<warning> Non-unicode mapping for font %s (%s)", fontid, filename);
961 swf_FontSetID(swffont, getNewID(obj));
963 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
964 // print font information
965 msg("<debug> Font %s (%s)",fontid, filename);
966 msg("<debug> | ID: %d", swffont->id);
967 msg("<debug> | Version: %d", swffont->version);
968 msg("<debug> | Name: %s", swffont->name);
969 msg("<debug> | Numchars: %d", swffont->numchars);
970 msg("<debug> | Maxascii: %d", swffont->maxascii);
971 msg("<debug> | Style: %d", swffont->style);
972 msg("<debug> | Encoding: %d", swffont->encoding);
973 for(int iii=0; iii<swffont->numchars;iii++) {
974 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,
975 swffont->layout->bounds[iii].xmin/20.0,
976 swffont->layout->bounds[iii].ymin/20.0,
977 swffont->layout->bounds[iii].xmax/20.0,
978 swffont->layout->bounds[iii].ymax/20.0
981 for(t=0;t<swffont->maxascii;t++) {
982 if(swffont->ascii2glyph[t] == iii)
983 msg("<debug> | - maps to %d",t);
988 /* set the font name to the ID we use here */
989 if(swffont->name) free(swffont->name);
990 swffont->name = (U8*)strdup(fontid);
992 iterator = new fontlist_t;
993 iterator->swffont = swffont;
997 last->next = iterator;
999 i->fontlist = iterator;
1001 obj->swffont = swffont;
1004 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
1006 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1007 fontlist_t *iterator = i->fontlist;
1009 if(!strcmp((char*)iterator->swffont->name,fontid))
1011 iterator = iterator->next;
1016 /* set's the matrix which is to be applied to characters drawn by
1017 swfoutput_drawchar() */
1018 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m21,
1019 double m12,double m22)
1025 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1026 if(obj->fontm11 == m11 &&
1027 obj->fontm12 == m12 &&
1028 obj->fontm21 == m21 &&
1029 obj->fontm22 == m22)
1039 m.sx = (U32)(((obj->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((obj->fontm12)*65536)/FONT_INTERNAL_SIZE);
1040 m.r0 = (U32)(((obj->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((obj->fontm22)*65536)/FONT_INTERNAL_SIZE);
1043 obj->fontmatrix = m;
1046 /* draws a character at x,y. */
1047 int swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr, int u, gfxcolor_t* color)
1049 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1051 m.m11 = obj->fontm11;
1052 m.m12 = obj->fontm12;
1053 m.m21 = obj->fontm21;
1054 m.m22 = obj->fontm22;
1057 return drawchar(obj, obj->swffont, character, charnr, u, &m, color);
1060 static void endpage(struct swfoutput*obj)
1062 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1068 swfoutput_endclip(obj);
1069 i->pagefinished = 1;
1072 void swfoutput_pagefeed(struct swfoutput*obj)
1074 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1076 if(!i->pagefinished)
1079 if(config_insertstoptag) {
1081 atag = action_Stop(atag);
1082 atag = action_End(atag);
1083 i->tag = swf_InsertTag(i->tag,ST_DOACTION);
1084 swf_ActionSet(i->tag,atag);
1086 i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
1089 for(i->depth;i->depth>i->startdepth;i->depth--) {
1090 i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
1091 swf_SetU16(i->tag,i->depth);
1093 i->depth = i->startdepth;
1096 static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
1098 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1100 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1104 int shapeid = getNewID(obj);
1109 i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
1111 fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
1112 swf_SetU16(i->tag,shapeid);
1113 swf_SetRect(i->tag,&r);
1114 swf_SetShapeHeader(i->tag,s);
1115 swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
1116 swf_ShapeSetLine(i->tag,s,(x2-x1),0);
1117 swf_ShapeSetLine(i->tag,s,0,(y2-y1));
1118 swf_ShapeSetLine(i->tag,s,(x1-x2),0);
1119 swf_ShapeSetLine(i->tag,s,0,(y1-y2));
1120 swf_ShapeSetEnd(i->tag);
1122 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1123 swf_ObjectPlace(i->tag,shapeid,getNewDepth(obj),0,0,0);
1124 i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
1125 swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(obj),0,0,0,65535);
1126 i->cliptag = i->tag;
1129 void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey, int x1, int y1, int x2, int y2)
1131 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1132 if(!i->firstpage && !i->pagefinished)
1135 swf_GetMatrix(0, &i->page_matrix);
1136 i->page_matrix.tx = movex*20;
1137 i->page_matrix.ty = movey*20;
1139 if(i->cliptag && i->frameno == i->lastframeno) {
1141 swf_GetPlaceObject(i->cliptag, &obj);
1142 obj.clipdepth = i->depth;
1143 swf_ResetTag(i->cliptag, i->cliptag->id);
1144 swf_SetPlaceObject(i->cliptag, &obj);
1145 swf_PlaceObjectFree(&obj);
1153 msg("<notice> processing page %d (%dx%d:%d:%d)", pageNum,x2-x1,y2-y1, x1, y1);
1155 x1*=20;y1*=20;x2*=20;y2*=20;
1157 /* set clipping/background rectangle */
1158 /* TODO: this should all be done in SWFOutputDev */
1159 setBackground(obj, x1, y1, x2, y2);
1161 /* increase SWF's bounding box */
1167 swf_ExpandRect2(&i->swf.movieSize, &r);
1169 i->lastframeno = i->frameno;
1171 i->pagefinished = 0;
1174 /* initialize the swf writer */
1175 void swfoutput_init(struct swfoutput* obj)
1177 memset(obj, 0, sizeof(struct swfoutput));
1178 obj->internal = init_internal_struct();
1179 ((swfoutput_internal*)obj->internal)->obj = obj;
1181 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1186 msg("<verbose> initializing swf output for size %d*%d\n", i->max_x,i->max_y);
1190 memset(&i->swf,0x00,sizeof(SWF));
1192 i->swf.fileVersion = config_flashversion;
1193 i->swf.frameRate = 0x0040; // 1 frame per 4 seconds
1194 i->swf.movieSize.xmin = 0;
1195 i->swf.movieSize.ymin = 0;
1196 i->swf.movieSize.xmax = 0;
1197 i->swf.movieSize.ymax = 0;
1199 i->swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1200 i->tag = i->swf.firstTag;
1201 rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
1202 swf_SetRGB(i->tag,&rgb);
1204 i->startdepth = i->depth = 0;
1207 i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1210 static void startshape(struct swfoutput*obj)
1212 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1221 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1223 swf_ShapeNew(&i->shape);
1224 i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&obj->strokergb);
1225 i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb);
1227 i->shapeid = getNewID(obj);
1229 msg("<debug> Using shape id %d", i->shapeid);
1231 swf_SetU16(i->tag,i->shapeid); // ID
1233 i->bboxrectpos = i->tag->len;
1237 r.xmax = 20*i->max_x;
1238 r.ymax = 20*i->max_y;
1239 swf_SetRect(i->tag,&r);
1241 memset(&i->bboxrect, 0, sizeof(i->bboxrect));
1243 swf_SetShapeStyles(i->tag,i->shape);
1244 swf_ShapeCountBits(i->shape,NULL,NULL);
1245 swf_SetShapeBits(i->tag,i->shape);
1247 /* TODO: do we really need this? */
1248 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
1249 i->swflastx=i->swflasty=0;
1251 i->shapeisempty = 1;
1254 static void starttext(struct swfoutput*obj)
1256 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1260 i->textid = getNewID(obj);
1262 i->swflastx=i->swflasty=0;
1266 /* TODO: move to ../lib/rfxswf */
1267 void changeRect(struct swfoutput*obj, TAG*tag, int pos, SRECT*newrect)
1269 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1270 /* determine length of old rect */
1274 swf_GetRect(tag, &old);
1275 swf_ResetReadBits(tag);
1276 int pos_end = tag->pos;
1278 int len = tag->len - pos_end;
1279 U8*data = (U8*)malloc(len);
1280 memcpy(data, &tag->data[pos_end], len);
1283 swf_SetRect(tag, newrect);
1284 swf_SetBlock(tag, data, len);
1286 tag->pos = tag->readBit = 0;
1289 void cancelshape(swfoutput*obj)
1291 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1292 /* delete old shape tag */
1294 i->tag = i->tag->prev;
1295 swf_DeleteTag(todel);
1296 if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1298 i->bboxrectpos = -1;
1301 void fixAreas(swfoutput*obj)
1303 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1304 if(!i->shapeisempty && i->fill &&
1305 (i->bboxrect.xmin == i->bboxrect.xmax ||
1306 i->bboxrect.ymin == i->bboxrect.ymax) &&
1307 config_minlinewidth >= 0.001
1309 msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1310 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1311 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1314 SRECT r = i->bboxrect;
1316 if(r.xmin == r.xmax && r.ymin == r.ymax) {
1317 /* this thing comes down to a single dot- nothing to fix here */
1323 RGBA save_col = obj->strokergb;
1324 int save_width = i->linewidth;
1326 obj->strokergb = obj->fillrgb;
1327 i->linewidth = (int)(config_minlinewidth*20);
1328 if(i->linewidth==0) i->linewidth = 1;
1332 moveto(obj, i->tag, r.xmin/20.0,r.ymin/20.0);
1333 lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0);
1335 obj->strokergb = save_col;
1336 i->linewidth = save_width;
1341 static void endshape_noput(swfoutput*obj)
1343 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1346 //changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1349 swf_ShapeFree(i->shape);
1355 static void endshape(swfoutput*obj)
1357 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1363 if(i->shapeisempty ||
1365 (i->bboxrect.xmin == i->bboxrect.xmax &&
1366 i->bboxrect.ymin == i->bboxrect.ymax))
1368 // delete the shape again, we didn't do anything
1369 msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1370 i->bboxrect.xmin /20.0,
1371 i->bboxrect.ymin /20.0,
1372 i->bboxrect.xmax /20.0,
1373 i->bboxrect.ymax /20.0
1379 swf_ShapeSetEnd(i->tag);
1381 changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect);
1383 msg("<trace> Placing shape id %d", i->shapeid);
1385 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1386 swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(obj),&i->page_matrix,NULL,NULL);
1388 swf_ShapeFree(i->shape);
1391 i->bboxrectpos = -1;
1397 sprintf(text, "id%d", i->shapeid);
1399 points[0].x = i->bboxrect.xmin;
1400 points[0].y = i->bboxrect.ymin;
1401 points[1].x = i->bboxrect.xmax;
1402 points[1].y = i->bboxrect.ymin;
1403 points[2].x = i->bboxrect.xmax;
1404 points[2].y = i->bboxrect.ymax;
1405 points[3].x = i->bboxrect.xmin;
1406 points[3].y = i->bboxrect.ymax;
1407 swfoutput_namedlink(obj,text,points);
1411 void wipeSWF(SWF*swf)
1413 TAG*tag = swf->firstTag;
1415 TAG*next = tag->next;
1416 if(tag->id != ST_SETBACKGROUNDCOLOR &&
1417 tag->id != ST_END &&
1418 tag->id != ST_DOACTION &&
1419 tag->id != ST_SHOWFRAME) {
1427 void swfoutput_finalize(struct swfoutput*obj)
1429 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1431 if(i->tag && i->tag->id == ST_END)
1432 return; //already done
1434 if(config_bboxvars) {
1435 TAG* tag = swf_InsertTag(i->swf.firstTag, ST_DOACTION);
1437 a = action_PushString(a, "xmin");
1438 a = action_PushFloat(a, i->swf.movieSize.xmin / 20.0);
1439 a = action_SetVariable(a);
1440 a = action_PushString(a, "ymin");
1441 a = action_PushFloat(a, i->swf.movieSize.ymin / 20.0);
1442 a = action_SetVariable(a);
1443 a = action_PushString(a, "xmax");
1444 a = action_PushFloat(a, i->swf.movieSize.xmax / 20.0);
1445 a = action_SetVariable(a);
1446 a = action_PushString(a, "ymax");
1447 a = action_PushFloat(a, i->swf.movieSize.ymax / 20.0);
1448 a = action_SetVariable(a);
1449 a = action_PushString(a, "width");
1450 a = action_PushFloat(a, (i->swf.movieSize.xmax - i->swf.movieSize.xmin) / 20.0);
1451 a = action_SetVariable(a);
1452 a = action_PushString(a, "height");
1453 a = action_PushFloat(a, (i->swf.movieSize.ymax - i->swf.movieSize.ymin) / 20.0);
1454 a = action_SetVariable(a);
1456 swf_ActionSet(tag, a);
1460 if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1461 swfoutput_pagefeed(obj);
1464 fontlist_t *tmp,*iterator = i->fontlist;
1466 TAG*mtag = i->swf.firstTag;
1467 if(iterator->swffont) {
1468 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1469 if(!config_storeallcharacters)
1470 swf_FontReduce(iterator->swffont);
1471 swf_FontSetDefine2(mtag, iterator->swffont);
1474 iterator = iterator->next;
1476 i->tag = swf_InsertTag(i->tag,ST_END);
1477 TAG* tag = i->tag->prev;
1479 /* remove the removeobject2 tags between the last ST_SHOWFRAME
1480 and the ST_END- they confuse the flash player */
1481 while(tag->id == ST_REMOVEOBJECT2) {
1482 TAG* prev = tag->prev;
1492 SWF* swfoutput_get(struct swfoutput*obj)
1494 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1496 swfoutput_finalize(obj);
1498 return swf_CopySWF(&i->swf);
1501 void swfoutput_getdimensions(struct swfoutput*obj, int*x1, int*y1, int*x2, int*y2)
1503 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1504 if(x1) *x1 = i->swf.movieSize.xmin/20;
1505 if(y1) *y1 = i->swf.movieSize.ymin/20;
1506 if(x2) *x2 = i->swf.movieSize.xmax/20;
1507 if(y2) *y2 = i->swf.movieSize.ymax/20;
1510 int swfoutput_save(struct swfoutput* obj, char*filename)
1512 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1513 swfoutput_finalize(obj);
1517 fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1522 msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1526 if(config_enablezlib || config_flashversion>=6) {
1527 if FAILED(swf_WriteSWC(fi,&i->swf))
1528 msg("<error> WriteSWC() failed.\n");
1530 if FAILED(swf_WriteSWF(fi,&i->swf))
1531 msg("<error> WriteSWF() failed.\n");
1539 /* Perform cleaning up, complete the swf, and write it out. */
1540 void swfoutput_destroy(struct swfoutput* obj)
1542 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1544 /* not initialized yet- nothing to destroy */
1548 fontlist_t *tmp,*iterator = i->fontlist;
1550 if(iterator->swffont) {
1551 swf_FontFree(iterator->swffont);iterator->swffont=0;
1554 iterator = iterator->next;
1557 swf_FreeTags(&i->swf);
1560 memset(obj, 0, sizeof(swfoutput));
1563 static void swfoutput_setfillcolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1565 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1566 if(obj->fillrgb.r == r &&
1567 obj->fillrgb.g == g &&
1568 obj->fillrgb.b == b &&
1569 obj->fillrgb.a == a) return;
1579 static void swfoutput_setstrokecolor(swfoutput* obj, U8 r, U8 g, U8 b, U8 a)
1581 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1582 if(obj->strokergb.r == r &&
1583 obj->strokergb.g == g &&
1584 obj->strokergb.b == b &&
1585 obj->strokergb.a == a) return;
1589 obj->strokergb.r = r;
1590 obj->strokergb.g = g;
1591 obj->strokergb.b = b;
1592 obj->strokergb.a = a;
1595 static void swfoutput_setlinewidth(struct swfoutput*obj, double _linewidth)
1597 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1598 if(i->linewidth == (U16)(_linewidth*20))
1603 i->linewidth = (U16)(_linewidth*20);
1607 static void drawlink(struct swfoutput*obj, ActionTAG*,ActionTAG*, swfcoord*points, char mouseover);
1609 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1611 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1613 if(!strncmp("http://pdf2swf:", url, 15)) {
1614 char*tmp = strdup(url);
1615 int l = strlen(tmp);
1618 swfoutput_namedlink(obj, tmp+15, points);
1628 if(!config_opennewwindow)
1629 actions = action_GetUrl(0, url, "_parent");
1631 actions = action_GetUrl(0, url, "_this");
1632 actions = action_End(actions);
1634 drawlink(obj, actions, 0, points,0);
1636 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1638 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1646 actions = action_GotoFrame(0, page);
1647 actions = action_End(actions);
1649 drawlink(obj, actions, 0, points,0);
1652 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1653 of the viewer objects, like subtitles, index elements etc.
1655 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1657 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1658 ActionTAG *actions1,*actions2;
1659 char*tmp = strdup(name);
1667 if(!strncmp(tmp, "call:", 5))
1669 char*x = strchr(&tmp[5], ':');
1671 actions1 = action_PushInt(0, 0); //number of parameters (0)
1672 actions1 = action_PushString(actions1, &tmp[5]); //function name
1673 actions1 = action_CallFunction(actions1);
1676 actions1 = action_PushString(0, x+1); //parameter
1677 actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1678 actions1 = action_PushString(actions1, &tmp[5]); //function name
1679 actions1 = action_CallFunction(actions1);
1681 actions2 = action_End(0);
1686 actions1 = action_PushString(0, "/:subtitle");
1687 actions1 = action_PushString(actions1, name);
1688 actions1 = action_SetVariable(actions1);
1689 actions1 = action_End(actions1);
1691 actions2 = action_PushString(0, "/:subtitle");
1692 actions2 = action_PushString(actions2, "");
1693 actions2 = action_SetVariable(actions2);
1694 actions2 = action_End(actions2);
1697 drawlink(obj, actions1, actions2, points,mouseover);
1699 swf_ActionFree(actions1);
1700 swf_ActionFree(actions2);
1704 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1706 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1711 struct plotxy p1,p2,p3,p4;
1715 double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1719 int buttonid = getNewID(obj);
1722 if(points[t].x>xmax) xmax=points[t].x;
1723 if(points[t].y>ymax) ymax=points[t].y;
1724 if(points[t].x<xmin) xmin=points[t].x;
1725 if(points[t].y<ymin) ymin=points[t].y;
1728 p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y;
1729 p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1731 /* the following code subtracts the upper left edge from all coordinates,
1732 and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1733 Necessary for preprocessing with swfcombine. */
1734 posx = xmin; posy = ymin;
1735 p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1736 p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1737 xmin -= posx; ymin -= posy;
1738 xmax -= posx; ymax -= posy;
1741 myshapeid = getNewID(obj);
1742 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1743 swf_ShapeNew(&i->shape);
1744 rgb.r = rgb.b = rgb.a = rgb.g = 0;
1745 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1746 swf_SetU16(i->tag, myshapeid);
1747 r.xmin = (int)(xmin*20);
1748 r.ymin = (int)(ymin*20);
1749 r.xmax = (int)(xmax*20);
1750 r.ymax = (int)(ymax*20);
1751 swf_SetRect(i->tag,&r);
1752 swf_SetShapeStyles(i->tag,i->shape);
1753 swf_ShapeCountBits(i->shape,NULL,NULL);
1754 swf_SetShapeBits(i->tag,i->shape);
1755 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1756 i->swflastx = i->swflasty = 0;
1757 moveto(obj, i->tag, p1);
1758 lineto(obj, i->tag, p2);
1759 lineto(obj, i->tag, p3);
1760 lineto(obj, i->tag, p4);
1761 lineto(obj, i->tag, p1);
1762 swf_ShapeSetEnd(i->tag);
1765 myshapeid2 = getNewID(obj);
1766 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1767 swf_ShapeNew(&i->shape);
1768 rgb.r = rgb.b = rgb.a = rgb.g = 255;
1770 fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1771 swf_SetU16(i->tag, myshapeid2);
1772 r.xmin = (int)(xmin*20);
1773 r.ymin = (int)(ymin*20);
1774 r.xmax = (int)(xmax*20);
1775 r.ymax = (int)(ymax*20);
1776 swf_SetRect(i->tag,&r);
1777 swf_SetShapeStyles(i->tag,i->shape);
1778 swf_ShapeCountBits(i->shape,NULL,NULL);
1779 swf_SetShapeBits(i->tag,i->shape);
1780 swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1781 i->swflastx = i->swflasty = 0;
1782 moveto(obj, i->tag, p1);
1783 lineto(obj, i->tag, p2);
1784 lineto(obj, i->tag, p3);
1785 lineto(obj, i->tag, p4);
1786 lineto(obj, i->tag, p1);
1787 swf_ShapeSetEnd(i->tag);
1791 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1792 swf_SetU16(i->tag,buttonid); //id
1793 swf_ButtonSetFlags(i->tag, 0); //menu=no
1794 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1795 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1796 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1797 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1798 swf_SetU8(i->tag,0);
1799 swf_ActionSet(i->tag,actions1);
1800 swf_SetU8(i->tag,0);
1804 i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1805 swf_SetU16(i->tag,buttonid); //id
1806 swf_ButtonSetFlags(i->tag, 0); //menu=no
1807 swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1808 swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1809 swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1810 swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1811 swf_SetU8(i->tag,0); // end of button records
1812 swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1813 swf_ActionSet(i->tag,actions1);
1815 swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1816 swf_ActionSet(i->tag,actions2);
1817 swf_SetU8(i->tag,0);
1818 swf_ButtonPostProcess(i->tag, 2);
1820 swf_SetU8(i->tag,0);
1821 swf_ButtonPostProcess(i->tag, 1);
1825 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1827 if(posx!=0 || posy!=0) {
1829 p.x = (int)(posx*20);
1830 p.y = (int)(posy*20);
1831 p = swf_TurnPoint(p, &i->page_matrix);
1836 swf_ObjectPlace(i->tag, buttonid, getNewDepth(obj),&m,0,0);
1838 swf_ObjectPlace(i->tag, buttonid, getNewDepth(obj),&i->page_matrix,0,0);
1845 for(t=0;t<picpos;t++)
1847 if(pic_xids[t] == xid &&
1848 pic_yids[t] == yid) {
1849 width = pic_width[t];
1850 height = pic_height[t];
1854 pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1855 pic_xids[picpos] = xid;
1856 pic_yids[picpos] = yid;
1857 pic_width[picpos] = width;
1858 pic_height[picpos] = height;
1861 pic[width*y+x] = buf[0];
1865 xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1866 yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1870 xid += x*r+x*b*3+x*g*7+x*a*11;
1871 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1873 for(t=0;t<picpos;t++)
1875 if(pic_xids[t] == xid &&
1876 pic_yids[t] == yid) {
1884 static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
1886 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1887 gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1892 /* check whether the next segment is zero */
1893 if(line->type == gfx_moveTo) {
1894 msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
1895 moveto(obj, i->tag, line->x, line->y);
1896 px = lastx = line->x;
1897 py = lasty = line->y;
1899 } if(line->type == gfx_lineTo) {
1900 msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
1901 lineto(obj, i->tag, line->x, line->y);
1905 } else if(line->type == gfx_splineTo) {
1906 msg("<trace> ======== splineTo %.2f %.2f", line->x, line->y);
1908 s.x = line->sx;p.x = line->x;
1909 s.y = line->sy;p.y = line->y;
1910 splineto(obj, i->tag, s, p);
1919 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)
1921 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1922 gfxdevice_t*dev = &i->device;
1923 dev->stroke(dev, line, width, col, cap_style, joint_style, miterLimit);
1925 void swfoutput_fillgfxline(struct swfoutput*obj, gfxline_t*line, gfxcolor_t*col)
1927 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1928 gfxdevice_t*dev = &i->device;
1929 dev->fill(dev, line, col);
1931 void swfoutput_startclip(struct swfoutput*obj, gfxline_t*line)
1933 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1934 gfxdevice_t*dev = &i->device;
1935 dev->startclip(dev, line);
1937 void swfoutput_endclip(struct swfoutput*obj)
1939 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1940 gfxdevice_t*dev = &i->device;
1943 void swfoutput_gfxaddfont(struct swfoutput*obj, char*fontid, gfxfont_t*font)
1945 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1946 gfxdevice_t*dev = &i->device;
1947 dev->addfont(dev, fontid, font);
1949 void swfoutput_gfxdrawchar(struct swfoutput*obj, char*fontid, int glyph, gfxcolor_t*c, gfxmatrix_t*m)
1951 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1952 gfxdevice_t*dev = &i->device;
1953 dev->drawchar(dev, fontid, glyph, c, m);
1956 #define IMAGE_TYPE_JPEG 0
1957 #define IMAGE_TYPE_LOSSLESS 1
1959 static void swfoutput_drawimage(struct swfoutput*obj, RGBA* data, int sizex,int sizey,
1960 double x1,double y1,
1961 double x2,double y2,
1962 double x3,double y3,
1963 double x4,double y4, int type)
1965 swfoutput_internal*i = (swfoutput_internal*)obj->internal;
1969 double l1 = sqrt((x4-x1)*(x4-x1) + (y4-y1)*(y4-y1));
1970 double l2 = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
1972 gfxline_t p1,p2,p3,p4,p5;
1973 p1.type=gfx_moveTo;p1.x=x1; p1.y=y1;p1.next=&p2;
1974 p2.type=gfx_lineTo;p2.x=x2; p2.y=y2;p2.next=&p3;
1975 p3.type=gfx_lineTo;p3.x=x3; p3.y=y3;p3.next=&p4;
1976 p4.type=gfx_lineTo;p4.x=x4; p4.y=y4;p4.next=&p5;
1977 p5.type=gfx_lineTo;p5.x=x1; p5.y=y1;p5.next=0;
1979 {p1.x = (int)(p1.x*20)/20.0;
1980 p1.y = (int)(p1.y*20)/20.0;
1981 p2.x = (int)(p2.x*20)/20.0;
1982 p2.y = (int)(p2.y*20)/20.0;
1983 p3.x = (int)(p3.x*20)/20.0;
1984 p3.y = (int)(p3.y*20)/20.0;
1985 p4.x = (int)(p4.x*20)/20.0;
1986 p4.y = (int)(p4.y*20)/20.0;
1987 p5.x = (int)(p5.x*20)/20.0;
1988 p5.y = (int)(p5.y*20)/20.0;
1995 m.m00 = (p4.x-p1.x)/sizex; m.m10 = (p2.x-p1.x)/sizey;
1996 m.m01 = (p4.y-p1.y)/sizex; m.m11 = (p2.y-p1.y)/sizey;
2001 img.data = (gfxcolor_t*)data;
2005 if(type == IMAGE_TYPE_JPEG)
2006 /* TODO: pass image_dpi to device instead */
2007 i->device.setparameter(&i->device, "next_bitmap_is_jpeg", "1");
2009 i->device.fillbitmap(&i->device, &p1, &img, &m, 0);
2012 void swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
2013 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
2015 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_JPEG);
2018 void swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
2019 double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
2021 swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_LOSSLESS);
2024 void swfoutput_setparameter(char*name, char*value)
2026 if(!strcmp(name, "jpegsubpixels")) {
2027 config_jpegsubpixels = atof(value);
2028 } else if(!strcmp(name, "ppmsubpixels")) {
2029 config_ppmsubpixels = atof(value);
2030 } else if(!strcmp(name, "drawonlyshapes")) {
2031 config_drawonlyshapes = atoi(value);
2032 } else if(!strcmp(name, "ignoredraworder")) {
2033 config_ignoredraworder = atoi(value);
2034 } else if(!strcmp(name, "filloverlap")) {
2035 config_filloverlap = atoi(value);
2036 } else if(!strcmp(name, "linksopennewwindow")) {
2037 config_opennewwindow = atoi(value);
2038 } else if(!strcmp(name, "opennewwindow")) {
2039 config_opennewwindow = atoi(value);
2040 } else if(!strcmp(name, "storeallcharacters")) {
2041 config_storeallcharacters = atoi(value);
2042 } else if(!strcmp(name, "enablezlib")) {
2043 config_enablezlib = atoi(value);
2044 } else if(!strcmp(name, "bboxvars")) {
2045 config_bboxvars = atoi(value);
2046 } else if(!strcmp(name, "insertstop")) {
2047 config_insertstoptag = atoi(value);
2048 } else if(!strcmp(name, "protected")) {
2049 config_protect = atoi(value);
2050 } else if(!strcmp(name, "flashversion")) {
2051 config_flashversion = atoi(value);
2052 } else if(!strcmp(name, "minlinewidth")) {
2053 config_minlinewidth = atof(value);
2054 } else if(!strcmp(name, "caplinewidth")) {
2055 config_caplinewidth = atof(value);
2056 } else if(!strcmp(name, "dumpfonts")) {
2057 config_dumpfonts = atoi(value);
2058 } else if(!strcmp(name, "jpegquality")) {
2059 int val = atoi(value);
2061 if(val>100) val=100;
2062 config_jpegquality = val;
2063 } else if(!strcmp(name, "splinequality")) {
2064 int v = atoi(value);
2065 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2067 config_splinemaxerror = v;
2068 } else if(!strcmp(name, "fontquality")) {
2069 int v = atoi(value);
2070 v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
2072 config_fontsplinemaxerror = v;
2074 fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
2078 // --------------------------------------------------------------------
2080 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
2083 swf_GetCXForm(0, &cx, 1);
2086 if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
2087 c->gr!=0 || c->gb!=0 || c->ga!=0 ||
2088 c->br!=0 || c->bg!=0 || c->ba!=0 ||
2089 c->ar!=0 || c->ag!=0 || c->ab!=0)
2090 msg("<warning> CXForm not SWF-compatible");
2092 cx.a0 = (S16)(c->aa*256);
2093 cx.r0 = (S16)(c->rr*256);
2094 cx.g0 = (S16)(c->gg*256);
2095 cx.b0 = (S16)(c->bb*256);
2103 ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
2105 ArtVpath *vec = NULL;
2113 if(l2->type == gfx_moveTo) {
2115 } if(l2->type == gfx_lineTo) {
2117 } if(l2->type == gfx_splineTo) {
2118 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2119 if(!parts) parts = 1;
2129 vec = art_new (ArtVpath, len);
2134 if(l2->type == gfx_moveTo) {
2135 vec[pos].code = ART_MOVETO;
2140 } else if(l2->type == gfx_lineTo) {
2141 vec[pos].code = ART_LINETO;
2146 } else if(l2->type == gfx_splineTo) {
2148 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
2149 if(!parts) parts = 1;
2150 for(i=0;i<=parts;i++) {
2151 double t = (double)i/(double)parts;
2152 vec[pos].code = ART_LINETO;
2153 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
2154 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
2163 vec[pos].code = ART_END;
2165 svp = art_svp_vpath_stroke (vec,
2166 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
2167 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
2168 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
2169 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
2170 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
2171 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
2173 miterLimit, //miter_limit
2180 gfxline_t* SVPtogfxline(ArtSVP*svp)
2185 for(t=0;t<svp->n_segs;t++) {
2186 size += svp->segs[t].n_points + 1;
2188 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
2190 for(t=0;t<svp->n_segs;t++) {
2191 ArtSVPSeg* seg = &svp->segs[t];
2193 for(p=0;p<seg->n_points;p++) {
2194 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
2195 ArtPoint* point = &seg->points[p];
2196 lines[pos].x = point->x;
2197 lines[pos].y = point->y;
2198 lines[pos].next = &lines[pos+1];
2203 lines[pos-1].next = 0;
2211 static int imageInCache(swfoutput*obj, void*data, int width, int height)
2215 static void addImageToCache(swfoutput*obj, void*data, int width, int height)
2219 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
2221 swfoutput*obj = i->obj;
2223 RGBA*mem = (RGBA*)img->data;
2225 int sizex = img->width;
2226 int sizey = img->height;
2227 int is_jpeg = i->jpeg;
2230 int newsizex=sizex, newsizey=sizey;
2233 if(is_jpeg && config_jpegsubpixels) {
2234 newsizex = (int)(targetwidth*config_jpegsubpixels+0.5);
2235 newsizey = (int)(targetheight*config_jpegsubpixels+0.5);
2236 } else if(!is_jpeg && config_ppmsubpixels) {
2237 newsizex = (int)(targetwidth*config_ppmsubpixels+0.5);
2238 newsizey = (int)(targetheight*config_ppmsubpixels+0.5);
2242 if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
2245 /* TODO: cache images */
2249 if(newsizex<sizex || newsizey<sizey) {
2250 msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
2251 newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
2252 *newwidth = sizex = newsizex;
2253 *newheight = sizey = newsizey;
2258 int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
2259 int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
2261 msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
2263 has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
2266 targetwidth, targetheight,
2267 /*newsizex, newsizey,*/
2268 num_colors>256?">":"", num_colors>256?256:num_colors);
2270 /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
2271 swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
2273 for(t=0;t<num_colors;t++) {
2274 printf("%02x%02x%02x%02x ",
2275 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
2282 int cacheid = imageInCache(obj, mem, sizex, sizey);
2285 bitid = getNewID(obj);
2286 i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, config_jpegquality);
2287 addImageToCache(obj, mem, sizex, sizey);
2297 static SRECT gfxline_getSWFbbox(gfxline_t*line)
2299 gfxbbox_t bbox = gfxline_getbbox(line);
2301 r.xmin = (int)(bbox.xmin*20);
2302 r.ymin = (int)(bbox.ymin*20);
2303 r.xmax = (int)(bbox.xmax*20);
2304 r.ymax = (int)(bbox.ymax*20);
2308 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
2310 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2311 swfoutput*obj = i->obj;
2316 int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
2317 int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
2319 int newwidth=0,newheight=0;
2320 int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
2323 double fx = (double)img->width / (double)newwidth;
2324 double fy = (double)img->height / (double)newheight;
2329 m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
2330 m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
2331 m.tx = (int)(matrix->tx*20);
2332 m.ty = (int)(matrix->ty*20);
2335 int myshapeid = getNewID(obj);
2336 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2338 swf_ShapeNew(&shape);
2339 int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
2340 swf_SetU16(i->tag, myshapeid);
2341 SRECT r = gfxline_getSWFbbox(line);
2342 swf_SetRect(i->tag,&r);
2343 swf_SetShapeStyles(i->tag,shape);
2344 swf_ShapeCountBits(shape,NULL,NULL);
2345 swf_SetShapeBits(i->tag,shape);
2346 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2347 i->swflastx = i->swflasty = UNDEFINED_COORD;
2348 drawgfxline(obj, line);
2349 swf_ShapeSetEnd(i->tag);
2350 swf_ShapeFree(shape);
2352 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2353 CXFORM cxform2 = gfxcxform_to_cxform(cxform);
2354 swf_ObjectPlace(i->tag,myshapeid,getNewDepth(obj),&i->page_matrix,&cxform2,NULL);
2357 void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
2359 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2360 swfoutput*obj = i->obj;
2365 if(i->clippos >= 127)
2367 msg("<warning> Too many clip levels.");
2371 int myshapeid = getNewID(obj);
2372 i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
2374 memset(&col, 0, sizeof(RGBA));
2376 swf_ShapeNew(&shape);
2377 int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2378 swf_SetU16(i->tag,myshapeid);
2379 SRECT r = gfxline_getSWFbbox(line);
2380 swf_SetRect(i->tag,&r);
2381 swf_SetShapeStyles(i->tag,shape);
2382 swf_ShapeCountBits(shape,NULL,NULL);
2383 swf_SetShapeBits(i->tag,shape);
2384 swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2385 i->swflastx = i->swflasty = UNDEFINED_COORD;
2386 drawgfxline(obj, line);
2387 swf_ShapeSetEnd(i->tag);
2388 swf_ShapeFree(shape);
2390 /* TODO: remember the bbox, and check all shapes against it */
2392 i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2393 i->cliptags[i->clippos] = i->tag;
2394 i->clipshapes[i->clippos] = myshapeid;
2395 i->clipdepths[i->clippos] = getNewDepth(obj);
2399 void swf_endclip(gfxdevice_t*dev)
2401 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2402 swfoutput*obj = i->obj;
2409 msg("<error> Invalid end of clipping region");
2413 /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2414 / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2416 swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2418 int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
2420 if(!strcmp(key, "next_bitmap_is_jpeg")) {
2421 ((swfoutput_internal*)dev->internal)->jpeg = 1;
2427 int gfxline_type(gfxline_t*line)
2433 int haszerosegments=0;
2435 if(line->type == gfx_moveTo) {
2438 } else if(line->type == gfx_lineTo) {
2442 } else if(line->type == gfx_splineTo) {
2444 if(tmpsplines>lines)
2449 if(lines==0 && splines==0) return 0;
2450 else if(lines==1 && splines==0) return 1;
2451 else if(lines==0 && splines==1) return 2;
2452 else if(splines==0) return 3;
2456 int gfxline_has_dots(gfxline_t*line)
2463 if(line->type == gfx_moveTo) {
2464 if(isline && dist < 1) {
2469 } else if(line->type == gfx_lineTo) {
2470 dist += fabs(line->x - x) + fabs(line->y - y);
2472 } else if(line->type == gfx_splineTo) {
2473 dist += fabs(line->sx - x) + fabs(line->sy - y) +
2474 fabs(line->x - line->sx) + fabs(line->y - line->sy);
2481 if(isline && dist < 1) {
2487 int gfxline_fix_short_edges(gfxline_t*line)
2491 if(line->type == gfx_lineTo) {
2492 if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2495 } else if(line->type == gfx_splineTo) {
2496 if(fabs(line->sx - x) + fabs(line->sy - y) +
2497 fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2510 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)
2512 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2513 swfoutput*obj = i->obj;
2514 int type = gfxline_type(line);
2515 int has_dots = gfxline_has_dots(line);
2517 /* TODO: * split line into segments, and perform this check for all segments */
2519 (width <= config_caplinewidth
2520 || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2521 || (cap_style == gfx_capRound && type<=2))) {
2522 msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2524 swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
2525 swfoutput_setlinewidth(obj, width);
2528 drawgfxline(obj, line);
2530 msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2532 gfxline_fix_short_edges(line);
2533 /* we need to convert the line into a polygon */
2534 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2535 gfxline_t*gfxline = SVPtogfxline(svp);
2536 dev->fill(dev, gfxline, color);
2541 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2543 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2544 swfoutput*obj = i->obj;
2546 if(!config_ignoredraworder)
2548 swfoutput_setfillcolor(obj, color->r, color->g, color->b, color->a);
2552 drawgfxline(obj, line);
2553 msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2555 void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2557 msg("<error> Gradient filling not implemented yet");
2560 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2562 SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2565 swffont->version = 2;
2566 swffont->name = (U8*)strdup(id);
2567 swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2568 swffont->layout->ascent = 0; /* ? */
2569 swffont->layout->descent = 0;
2570 swffont->layout->leading = 0;
2571 swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2572 swffont->encoding = FONT_ENCODING_UNICODE;
2573 swffont->numchars = font->num_glyphs;
2574 swffont->maxascii = font->max_unicode;
2575 swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2576 swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2577 swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2578 swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2579 for(t=0;t<font->max_unicode;t++) {
2580 swffont->ascii2glyph[t] = font->unicode2glyph[t];
2582 for(t=0;t<font->num_glyphs;t++) {
2585 swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2586 if(font->glyphs[t].name) {
2587 swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2589 swffont->glyphnames[t] = 0;
2591 swffont->glyph[t].advance = (int)(font->glyphs[t].advance * 20);
2593 swf_Shape01DrawerInit(&draw, 0);
2594 line = font->glyphs[t].line;
2597 c.x = line->sx; c.y = line->sy;
2598 to.x = line->x; to.y = line->y;
2599 if(line->type == gfx_moveTo) {
2600 draw.moveTo(&draw, &to);
2601 } else if(line->type == gfx_lineTo) {
2602 draw.lineTo(&draw, &to);
2603 } else if(line->type == gfx_splineTo) {
2604 draw.splineTo(&draw, &c, &to);
2609 swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2610 draw.dealloc(&draw);
2615 void swf_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font)
2617 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2619 if(i->obj->swffont && i->obj->swffont->name && !strcmp((char*)i->obj->swffont->name,fontid))
2620 return; // the requested font is the current font
2622 fontlist_t*last=0,*l = i->fontlist;
2625 if(!strcmp((char*)l->swffont->name, fontid)) {
2626 return; // we already know this font
2630 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2631 l->swffont = gfxfont_to_swffont(font, fontid);
2638 swf_FontSetID(l->swffont, getNewID(i->obj));
2640 if(getScreenLogLevel() >= LOGLEVEL_DEBUG) {
2641 // print font information
2642 msg("<debug> Font %s",fontid);
2643 msg("<debug> | ID: %d", l->swffont->id);
2644 msg("<debug> | Version: %d", l->swffont->version);
2645 msg("<debug> | Name: %s", l->swffont->name);
2646 msg("<debug> | Numchars: %d", l->swffont->numchars);
2647 msg("<debug> | Maxascii: %d", l->swffont->maxascii);
2648 msg("<debug> | Style: %d", l->swffont->style);
2649 msg("<debug> | Encoding: %d", l->swffont->encoding);
2650 for(int iii=0; iii<l->swffont->numchars;iii++) {
2651 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,
2652 l->swffont->layout->bounds[iii].xmin/20.0,
2653 l->swffont->layout->bounds[iii].ymin/20.0,
2654 l->swffont->layout->bounds[iii].xmax/20.0,
2655 l->swffont->layout->bounds[iii].ymax/20.0
2658 for(t=0;t<l->swffont->maxascii;t++) {
2659 if(l->swffont->ascii2glyph[t] == iii)
2660 msg("<debug> | - maps to %d",t);
2666 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2668 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2669 swfoutput*obj = i->obj;
2671 if(obj->swffont && obj->swffont->name && !strcmp((char*)obj->swffont->name,fontid))
2672 return; // the requested font is the current font
2674 fontlist_t*l = i->fontlist;
2676 if(!strcmp((char*)l->swffont->name, fontid)) {
2677 obj->swffont = l->swffont;
2682 msg("<error> Unknown font id: %s", fontid);
2686 void swf_drawchar(gfxdevice_t*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2688 swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2689 swfoutput*obj = i->obj;
2691 if(!obj->swffont || !obj->swffont->name || strcmp((char*)obj->swffont->name,fontid)) // not equal to current font
2693 /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2694 with multiple fonts */
2697 swf_switchfont(dev, fontid); // set the current font
2699 swfoutput_setfontmatrix(obj, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2702 m.m11 = obj->fontm11;
2703 m.m12 = obj->fontm12;
2704 m.m21 = obj->fontm21;
2705 m.m22 = obj->fontm22;
2708 drawchar(obj, obj->swffont, 0, glyph, -1, &m, color);