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 "swfoutput.h"
27 #include "../lib/log.h"
28 #include "../lib/rfxswf.h"
31 int ignoredraworder=0;
34 static int flag_protected = 0;
36 typedef unsigned char u8;
37 typedef unsigned short int u16;
38 typedef unsigned long int u32;
41 static char* filename = 0;
44 static int currentswfid = 0;
46 static int startdepth = 1;
49 static int shapeid = -1;
50 static int textid = -1;
52 static int drawmode = -1;
53 static char storefont = 0;
54 static int fillstyleid;
55 static int linestyleid;
56 static int swflastx=0;
57 static int swflasty=0;
58 static int lastwasfill = 0;
70 void startshape(struct swfoutput* obj);
71 void starttext(struct swfoutput* obj);
75 // matrix multiplication. changes p0
76 void transform (plotxy*p0,struct swfmatrix*m)
79 x = m->m11*p0->x+m->m12*p0->y;
80 y = m->m21*p0->x+m->m22*p0->y;
85 // write a move-to command into the swf
86 void moveto(TAG*tag, plotxy p0)
88 int rx = (int)(p0.x*20);
89 int ry = (int)(p0.y*20);
90 if(rx!=swflastx || ry!=swflasty) {
91 ShapeSetMove (tag, shape, rx,ry);
97 // write a line-to command into the swf
98 void lineto(TAG*tag, plotxy p0)
100 int rx = ((int)(p0.x*20)-swflastx);
101 int ry = ((int)(p0.y*20)-swflasty);
102 /* we can't skip this for rx=0,ry=0, those
104 ShapeSetLine (tag, shape, rx,ry);
109 // write a spline-to command into the swf
110 void splineto(TAG*tag, plotxy control,plotxy end)
112 int cx = ((int)(control.x*20)-swflastx);
113 int cy = ((int)(control.y*20)-swflasty);
116 int ex = ((int)(end.x*20)-swflastx);
117 int ey = ((int)(end.y*20)-swflasty);
120 ShapeSetCurve(tag, shape, cx,cy,ex,ey);
123 /* write a line, given two points and the transformation
125 void line(TAG*tag, plotxy p0, plotxy p1, struct swfmatrix*m)
133 /* write a cubic (!) spline. This involves calling the approximate()
134 function out of spline.cc to convert it to a quadratic spline. */
135 void spline(TAG*tag,plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
138 struct qspline q[16];
146 num = approximate(p0,p1,p2,p3,q);
148 moveto(tag,q[t].start);
149 splineto(tag,q[t].control, q[t].end);
153 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
154 (representing characters) */
155 void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m)
157 if(tag->id != ST_DEFINEFONT &&
158 tag->id != ST_DEFINESHAPE &&
159 tag->id != ST_DEFINESHAPE2 &&
160 tag->id != ST_DEFINESHAPE3)
162 logf("<error> internal error: drawpath needs a shape tag, not %d\n",tag->id);
168 double lastx=0,lasty=0;
169 double firstx=0,firsty=0;
174 x += (outline->dest.x/(float)0xffff);
175 y += (outline->dest.y/(float)0xffff);
176 if(outline->type == T1_PATHTYPE_MOVE)
178 if(((int)(lastx*20) != (int)(firstx*20) ||
179 (int)(lasty*20) != (int)(firsty*20)) &&
188 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
189 line(tag, p0, p1, m);
195 else if(outline->type == T1_PATHTYPE_LINE)
203 if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
206 else if(outline->type == T1_PATHTYPE_BEZIER)
212 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
215 p1.x=o2->C.x/(float)0xffff+lastx;
216 p1.y=o2->C.y/(float)0xffff+lasty;
217 p2.x=o2->B.x/(float)0xffff+lastx;
218 p2.y=o2->B.y/(float)0xffff+lasty;
221 if(log) printf("spline: %f,%f -> %f,%f\n",p3.x,p3.y,p0.x,p0.y);
222 spline(tag,p0,p1,p2,p3,m);
225 logf("<error> drawpath: unknown outline type:%d\n", outline->type);
229 outline = outline->link;
231 if(((int)(lastx*20) != (int)(firstx*20) ||
232 (int)(lasty*20) != (int)(firsty*20)) &&
241 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
242 line(tag, p0, p1, m);
246 int colorcompare(RGBA*a,RGBA*b)
258 static const int CHARDATAMAX = 1024;
266 } chardata[CHARDATAMAX];
269 void putcharacters(TAG*tag)
274 color.r = chardata[0].color.r^255;
283 int charadvance[128];
286 int glyphbits=1; //TODO: can this be zero?
289 if(tag->id != ST_DEFINETEXT &&
290 tag->id != ST_DEFINETEXT2) {
291 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
295 logf("<warning> putcharacters called with zero characters");
298 for(pass = 0; pass < 2; pass++)
308 advancebits++; // add sign bit
309 SetU8(tag, glyphbits);
310 SetU8(tag, advancebits);
313 for(t=0;t<=chardatapos;t++)
315 if(lastfontid != chardata[t].fontid ||
316 lastx!=chardata[t].x ||
317 lasty!=chardata[t].y ||
318 !colorcompare(&color, &chardata[t].color) ||
320 lastsize != chardata[t].size ||
323 if(charstorepos && pass==0)
326 for(s=0;s<charstorepos;s++)
328 while(charids[s]>=(1<<glyphbits))
330 while(charadvance[s]>=(1<<advancebits))
334 if(charstorepos && pass==1)
337 SetBits(tag, 0, 1); // GLYPH Record
338 SetBits(tag, charstorepos, 7); // number of glyphs
340 for(s=0;s<charstorepos;s++)
342 SetBits(tag, charids[s], glyphbits);
343 SetBits(tag, charadvance[s], advancebits);
348 if(pass == 1 && t<chardatapos)
354 if(lastx != chardata[t].x ||
355 lasty != chardata[t].y)
360 if(!colorcompare(&color, &chardata[t].color))
362 color = chardata[t].color;
365 font.id = chardata[t].fontid;
366 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
370 TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
373 lastfontid = chardata[t].fontid;
374 lastx = chardata[t].x;
375 lasty = chardata[t].y;
376 lastsize = chardata[t].size;
383 int nextt = t==chardatapos-1?t:t+1;
384 int rel = chardata[nextt].x-chardata[t].x;
385 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
387 lastx=chardata[nextt].x;
393 charids[charstorepos] = chardata[t].charid;
394 charadvance[charstorepos] = advance;
401 void putcharacter(struct swfoutput*obj, int fontid, int charid,
402 int x,int y, int size)
404 if(chardatapos == CHARDATAMAX)
409 chardata[chardatapos].fontid = fontid;
410 chardata[chardatapos].charid = charid;
411 chardata[chardatapos].x = x;
412 chardata[chardatapos].y = y;
413 chardata[chardatapos].color = obj->fillrgb;
414 chardata[chardatapos].size = size;
419 /* process a character. */
420 void drawchar(struct swfoutput*obj, SWFFont*font, char*character, swfmatrix*m)
423 if(m->m12!=0 || m->m21!=0)
428 if(usefonts && ! drawonlyshapes)
430 int charid = font->getSWFCharID(character);
435 putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
436 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
440 T1_OUTLINE*outline = font->getOutline(character);
441 char* charname = character;
444 logf("Didn't find %s in current charset (%s)",
445 character,font->getName());
461 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
466 drawpath(tag, outline, &m2);
471 /* draw a curved polygon. */
472 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline,
480 if(lastwasfill && !fill)
482 ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
485 if(!lastwasfill && fill)
487 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
491 drawpath(tag, outline,m);
494 /* SWFFont: copy all t1 font outlines to a local
496 SWFFont::SWFFont(char*name, int id, char*filename)
498 if(!T1_GetFontName(id))
501 this->name = strdup(T1_GetFontFileName(id));
502 this->fontid = strdup(name);
505 char**a= T1_GetAllCharNames(id);
506 int t=0, outlinepos=0;
514 logf("<verbose> Font %s(%d): Storing %d outlines.\n", name, id, t);
516 outline = (T1_OUTLINE**)malloc(t*sizeof(T1_OUTLINE*));
517 charname = (char**)malloc(t*sizeof(char*));
518 used = (char*)malloc(t*sizeof(char));
519 char2swfcharid = (U16*)malloc(t*2);
520 swfcharid2char = (U16*)malloc(t*2);
523 memset(used,0,t*sizeof(char));
525 this->swfid = ++currentswfid;
539 int ret = T1_ReencodeFont(id, map);
543 int ret = T1_ReencodeFont(id, map);
545 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
551 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
552 this->charname[outlinepos] = strdup(T1_GetCharName(id, s));
560 /* free all tables, write out definefont tags */
564 int*ptr = (int*)malloc(swfcharpos*sizeof(int));
566 for(t=0;t<charnum;t++)
567 if(used[t]) usednum++;
569 if(usednum && !drawonlyshapes)
571 logf("<verbose> Font %s has %d used characters",fontid, usednum);
572 TAG*ftag = InsertTag(swf.FirstTag,ST_DEFINEFONT);
573 SetU16(ftag, this->swfid);
574 int initpos = GetDataSize(ftag);
580 for(t=0;t<swfcharpos;t++)
582 ptr[t] = GetDataSize(ftag);
583 SetU16(ftag, 0x1234);
585 for(t=0;t<swfcharpos;t++)
587 *(U16*)&ftag->data[ptr[t]] = GetDataSize(ftag)-initpos;
590 SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
594 ShapeSetStyle(ftag,&s,0,1,0);
598 drawpath(ftag, outline[swfcharid2char[t]],&m);
607 for(t=0;t<charnum;t++)
611 free(swfcharid2char);
612 free(char2swfcharid);
615 T1_OUTLINE*SWFFont::getOutline(char*name)
618 for(t=0;t<this->charnum;t++) {
619 if(!strcmp(this->charname[t],name)) {
622 swfcharid2char[swfcharpos] = t;
623 char2swfcharid[t] = swfcharpos;
633 int SWFFont::getSWFCharID(char*name)
636 for(t=0;t<this->charnum;t++) {
637 if(!strcmp(this->charname[t],name)) {
640 swfcharid2char[swfcharpos] = t;
641 char2swfcharid[t] = swfcharpos++;
644 return char2swfcharid[t];
647 logf("<warning> Didn't find character '%s' in font '%s'", name, this->name);
651 char*SWFFont::getName()
662 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
663 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
665 fontlist_t*last=0,*iterator;
666 if(obj->font && !strcmp(obj->font->fontid,fontid))
671 if(!strcmp(iterator->font->fontid,fontid))
674 iterator = iterator->next;
678 obj->font = iterator->font;
683 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,fontid);
686 SWFFont*font = new SWFFont(fontid, t1id, filename);
687 iterator = new fontlist_t;
688 iterator->font = font;
692 last->next = iterator;
698 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
700 fontlist_t *iterator = fontlist;
702 if(!strcmp(iterator->font->fontid,fontid))
704 iterator = iterator->next;
709 /* set's the matrix which is to be applied to characters drawn by
710 swfoutput_drawchar() */
711 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
712 double m21,double m22)
714 if(obj->fontm11 == m11 &&
715 obj->fontm12 == m12 &&
716 obj->fontm21 == m21 &&
727 /* draws a character at x,y. */
728 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character)
731 m.m11 = obj->fontm11;
732 m.m12 = obj->fontm12;
733 m.m21 = obj->fontm21;
734 m.m22 = obj->fontm22;
737 drawchar(obj, obj->font, character, &m);
740 /* initialize the swf writer */
741 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
746 memset(obj, 0, sizeof(struct swfoutput));
747 filename = _filename;
751 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
755 memset(&swf,0x00,sizeof(SWF));
758 swf.FrameRate = 0x0040; // 1 frame per 4 seconds
759 swf.MovieSize.xmax = 20*sizex;
760 swf.MovieSize.ymax = 20*sizey;
762 swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
769 tag = InsertTag(tag, ST_PROTECT);
774 void swfoutput_setprotected() //write PROTECT tag
779 void startshape(struct swfoutput*obj)
787 tag = InsertTag(tag,ST_DEFINESHAPE);
790 linestyleid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
791 rgb.r = obj->fillrgb.r;
792 rgb.g = obj->fillrgb.g;
793 rgb.b = obj->fillrgb.b;
794 fillstyleid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
796 shapeid = ++currentswfid;
797 SetU16(tag,shapeid); // ID
806 SetShapeStyles(tag,shape);
807 ShapeCountBits(shape,NULL,NULL);
808 SetShapeBits(tag,shape);
810 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
815 void starttext(struct swfoutput*obj)
821 tag = InsertTag(tag,ST_DEFINETEXT);
822 textid = ++currentswfid;
848 tag = InsertTag(tag,ST_PLACEOBJECT2);
849 ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
859 tag = InsertTag(tag,ST_PLACEOBJECT2);
860 ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
864 void endpage(struct swfoutput*obj)
871 swfoutput_endclip(obj);
872 tag = InsertTag(tag,ST_SHOWFRAME);
875 void swfoutput_newpage(struct swfoutput*obj)
879 for(depth--;depth>=startdepth;depth--) {
880 tag = InsertTag(tag,ST_REMOVEOBJECT2);
888 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
889 up, complete the swf, and write it out. */
890 void swfoutput_destroy(struct swfoutput* obj)
893 fontlist_t *tmp,*iterator = fontlist;
895 delete iterator->font;
898 iterator = iterator->next;
906 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
911 logf("<fatal> Could not create \"%s\". ", filename);
915 tag = InsertTag(tag,ST_END);
917 if FAILED(WriteSWF(fi,&swf))
918 logf("<error> WriteSWF() failed.\n");
921 logf("<notice> SWF written\n");
924 void swfoutput_setdrawmode(swfoutput* obj, int mode)
927 if(mode == DRAWMODE_FILL)
929 else if(mode == DRAWMODE_EOFILL)
931 else if(mode == DRAWMODE_STROKE)
933 else if(mode == DRAWMODE_CLIP)
935 else if(mode == DRAWMODE_EOCLIP)
939 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
941 if(obj->fillrgb.r == r &&
942 obj->fillrgb.g == g &&
943 obj->fillrgb.b == b &&
944 obj->fillrgb.a == a) return;
954 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
956 if(obj->strokergb.r == r &&
957 obj->strokergb.g == g &&
958 obj->strokergb.b == b &&
959 obj->strokergb.a == a) return;
963 obj->strokergb.r = r;
964 obj->strokergb.g = g;
965 obj->strokergb.b = b;
966 obj->strokergb.a = a;
969 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
971 if(obj->linewidth == (u16)(linewidth*20))
976 obj->linewidth = (u16)(linewidth*20);
980 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
989 logf("<warning> Too many clip levels.");
994 int olddrawmode = drawmode;
995 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
996 swfoutput_drawpath(obj, outline, m);
998 swfoutput_setdrawmode(obj, olddrawmode);
1000 tag = InsertTag(tag,ST_PLACEOBJECT2);
1001 cliptags[clippos] = tag;
1002 clipshapes[clippos] = shapeid;
1003 clipdepths[clippos] = depth++;
1008 void swfoutput_endclip(swfoutput*obj)
1016 logf("<error> Invalid end of clipping region");
1020 PlaceObject(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1023 void swfoutput_drawimagefile(struct swfoutput*, char*filename, int sizex,int sizey,
1024 double x1,double y1,
1025 double x2,double y2,
1026 double x3,double y3,
1027 double x4,double y4)
1039 struct plotxy p1,p2,p3,p4;
1041 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1042 if(x2>xmax) xmax=x2;
1043 if(y2>ymax) ymax=y2;
1044 if(x2<xmin) xmin=x2;
1045 if(y2<ymin) ymin=y2;
1046 if(x3>xmax) xmax=x3;
1047 if(y3>ymax) ymax=y3;
1048 if(x3<xmin) xmin=x3;
1049 if(y3<ymin) ymin=y3;
1050 if(x4>xmax) xmax=x4;
1051 if(y4>ymax) ymax=y4;
1052 if(x4<xmin) xmin=x4;
1053 if(y4<ymin) ymin=y4;
1064 m.sx = (int)(65536*20*(x4-x1))/sizex;
1065 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1066 m.r0 = (int)(65536*20*(x1-x2))/sizey;
1067 m.sy = -(int)(65536*20*(y1-y2))/sizey;
1069 m.tx = (int)(x1*20);
1070 m.ty = (int)(y1*20);
1072 bitid = ++currentswfid;
1075 tag = InsertTag(tag,ST_DEFINEBITSJPEG2);
1077 SetJPEGBits(tag, filename, jpegquality);
1080 myshapeid = ++currentswfid;
1081 tag = InsertTag(tag,ST_DEFINESHAPE);
1083 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1084 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1085 fsid = ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1086 SetU16(tag, myshapeid);
1087 r.xmin = (int)(xmin*20);
1088 r.ymin = (int)(ymin*20);
1089 r.xmax = (int)(xmax*20);
1090 r.ymax = (int)(ymax*20);
1092 SetShapeStyles(tag,shape);
1093 ShapeCountBits(shape,NULL,NULL);
1094 SetShapeBits(tag,shape);
1095 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1096 swflastx = swflasty = 0;
1103 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1104 ShapeSetLine (tag, shape, (int)(x1*20);
1105 ShapeSetLine (tag, shape, x*20,0);
1106 ShapeSetLine (tag, shape, 0,-y*20);
1107 ShapeSetLine (tag, shape, -x*20,0);*/
1111 tag = InsertTag(tag,ST_PLACEOBJECT2);
1112 ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);