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;
35 typedef unsigned char u8;
36 typedef unsigned short int u16;
37 typedef unsigned long int u32;
41 static int flag_protected;
44 static int currentswfid = 0;
47 static int shapeid = -1;
48 static int textid = -1;
50 static int fillstyleid;
51 static int linestyleid;
52 static int swflastx=0;
53 static int swflasty=0;
54 static int lastwasfill = 0;
55 static char* filename = 0;
59 static char storefont = 0;
61 static int startdepth = 1;
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;
171 if(log) printf("shape-start %d\n", fill);
175 x += (outline->dest.x/(float)0xffff);
176 y += (outline->dest.y/(float)0xffff);
177 if(outline->type == T1_PATHTYPE_MOVE)
179 if(((int)(lastx*20) != (int)(firstx*20) ||
180 (int)(lasty*20) != (int)(firsty*20)) &&
189 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
190 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);
244 if(log) printf("shape-end\n");
247 int colorcompare(RGBA*a,RGBA*b)
259 static const int CHARDATAMAX = 1024;
267 } chardata[CHARDATAMAX];
271 void putcharacters(TAG*tag)
276 color.r = chardata[0].color.r^255;
285 int charadvance[128];
288 int glyphbits=1; //TODO: can this be zero?
291 if(tag->id != ST_DEFINETEXT &&
292 tag->id != ST_DEFINETEXT2) {
293 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
297 for(pass = 0; pass < 2; pass++)
308 SetU8(tag, glyphbits);
309 SetU8(tag, advancebits);
312 for(t=0;t<=chardatapos;t++)
314 if(lastfontid != chardata[t].fontid ||
315 lastx!=chardata[t].x ||
316 lasty!=chardata[t].y ||
317 !colorcompare(&color, &chardata[t].color) ||
319 lastsize != chardata[t].size ||
322 if(charstorepos && pass==0)
325 for(s=0;s<charstorepos;s++)
327 while(charids[s]>=(1<<glyphbits))
329 while(charadvance[s]>=(1<<advancebits))
333 if(charstorepos && pass==1)
336 SetBits(tag, 0, 1); // GLYPH Record
337 SetBits(tag, charstorepos, 7); // one glyph
339 for(s=0;s<charstorepos;s++)
341 SetBits(tag, charids[s], glyphbits);
342 SetBits(tag, charadvance[s], advancebits);
347 if(pass == 1 && t<chardatapos)
353 if(lastx != chardata[t].x ||
354 lasty != chardata[t].y)
359 if(!colorcompare(&color, &chardata[t].color))
361 color = chardata[t].color;
364 font.id = chardata[t].fontid;
365 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
369 TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
372 lastfontid = chardata[t].fontid;
373 lastx = chardata[t].x;
374 lasty = chardata[t].y;
375 lastsize = chardata[t].size;
382 int nextt = t==chardatapos-1?t:t+1;
383 int rel = chardata[nextt].x-chardata[t].x;
384 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
386 lastx=chardata[nextt].x;
392 charids[charstorepos] = chardata[t].charid;
393 charadvance[charstorepos] = advance;
400 void putcharacter(struct swfoutput*obj, int fontid, int charid, int x,int y, int size)
402 if(chardatapos == CHARDATAMAX)
407 chardata[chardatapos].fontid = fontid;
408 chardata[chardatapos].charid = charid;
409 chardata[chardatapos].x = x;
410 chardata[chardatapos].y = y;
411 chardata[chardatapos].color = obj->fillrgb;
412 chardata[chardatapos].size = size;
417 /* process a character. */
418 void drawchar(struct swfoutput*obj, SWFFont*font, char*character, swfmatrix*m)
421 if(m->m12!=0 || m->m21!=0)
426 if(usefonts && ! drawonlyshapes)
428 int charid = font->getSWFCharID(character);
433 putcharacter(obj, font->swfid, charid, (int)(m->m13*20),(int)(m->m23*20),
434 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
438 T1_OUTLINE*outline = font->getOutline(character);
439 char* charname = character;
442 logf("Didn't find %s in current charset (%s)", character,font->getName());
458 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
463 drawpath(tag, outline, &m2);
468 /* draw a curved polygon. */
469 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, struct swfmatrix*m)
476 if(lastwasfill && !fill)
478 ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
481 if(!lastwasfill && fill)
483 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
487 drawpath(tag, outline,m);
490 /* SWFFont: copy all t1 font outlines to a local
492 SWFFont::SWFFont(char*name, int id, char*filename)
494 if(!T1_GetFontName(id))
497 this->name = strdup(T1_GetFontFileName(id));
498 this->fontid = strdup(name);
501 char**a= T1_GetAllCharNames(id);
502 int t=0, outlinepos=0;
510 logf("<verbose> Font %s(%d): Storing %d outlines.\n", name, id, t);
512 outline = (T1_OUTLINE**)malloc(t*sizeof(T1_OUTLINE*));
513 charname = (char**)malloc(t*sizeof(char*));
514 used = (char*)malloc(t*sizeof(char));
515 char2swfcharid = (U16*)malloc(t*2);
516 swfcharid2char = (U16*)malloc(t*2);
519 memset(used,0,t*sizeof(char));
521 this->swfid = ++currentswfid;
535 int ret = T1_ReencodeFont(id, map);
539 int ret = T1_ReencodeFont(id, map);
541 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
547 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
548 this->charname[outlinepos] = strdup(T1_GetCharName(id, s));
556 /* free all tables, write out definefont tags */
560 int*ptr = (int*)malloc(swfcharpos*sizeof(int));
562 for(t=0;t<charnum;t++)
563 if(used[t]) usednum++;
565 if(usednum && !drawonlyshapes)
567 logf("<verbose> Font %s has %d used characters",fontid, usednum);
568 TAG*ftag = InsertTag(swf.FirstTag,ST_DEFINEFONT);
569 SetU16(ftag, this->swfid);
570 int initpos = GetDataSize(ftag);
576 for(t=0;t<swfcharpos;t++)
578 ptr[t] = GetDataSize(ftag);
579 SetU16(ftag, 0x1234);
581 for(t=0;t<swfcharpos;t++)
583 *(U16*)&ftag->data[ptr[t]] = GetDataSize(ftag)-initpos;
586 SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
590 ShapeSetStyle(ftag,&s,0,1,0);
594 drawpath(ftag, outline[swfcharid2char[t]],&m);
603 for(t=0;t<charnum;t++)
607 free(swfcharid2char);
608 free(char2swfcharid);
611 T1_OUTLINE*SWFFont::getOutline(char*name)
614 for(t=0;t<this->charnum;t++) {
615 if(!strcmp(this->charname[t],name)) {
619 swfcharid2char[swfcharpos] = t;
620 char2swfcharid[t] = swfcharpos;
630 int SWFFont::getSWFCharID(char*name)
633 for(t=0;t<this->charnum;t++) {
634 if(!strcmp(this->charname[t],name)) {
638 swfcharid2char[swfcharpos] = t;
639 char2swfcharid[t] = swfcharpos++;
642 return char2swfcharid[t];
648 char*SWFFont::getName()
659 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
660 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
662 fontlist_t*last=0,*iterator;
663 if(obj->font && !strcmp(obj->font->fontid,fontid))
668 if(!strcmp(iterator->font->fontid,fontid))
671 iterator = iterator->next;
675 obj->font = iterator->font;
680 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,fontid);
683 SWFFont*font = new SWFFont(fontid, t1id, filename);
684 iterator = new fontlist_t;
685 iterator->font = font;
689 last->next = iterator;
695 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
697 fontlist_t *iterator = fontlist;
699 if(!strcmp(iterator->font->fontid,fontid))
701 iterator = iterator->next;
706 /* set's the matrix which is to be applied to characters drawn by
707 swfoutput_drawchar() */
708 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
709 double m21,double m22)
711 if(obj->fontm11 == m11 &&
712 obj->fontm12 == m12 &&
713 obj->fontm21 == m21 &&
724 /* draws a character at x,y. */
725 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character)
728 m.m11 = obj->fontm11;
729 m.m12 = obj->fontm12;
730 m.m21 = obj->fontm21;
731 m.m22 = obj->fontm22;
734 drawchar(obj, obj->font, character, &m);
737 /* initialize the swf writer */
738 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
743 memset(obj, 0, sizeof(struct swfoutput));
744 filename = _filename;
748 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
752 memset(&swf,0x00,sizeof(SWF));
755 // swf.FrameRate = 0x1900;
756 swf.FrameRate = 0x0040; // 1 frame per 4 seconds
757 swf.MovieSize.xmax = 20*sizex;
758 swf.MovieSize.ymax = 20*sizey;
760 swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
767 tag = InsertTag(tag, ST_PROTECT);
772 void swfoutput_setprotected() //write PROTECT tag
777 void startshape(struct swfoutput*obj)
785 tag = InsertTag(tag,ST_DEFINESHAPE);
788 linestyleid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
789 rgb.r = obj->fillrgb.r;
790 rgb.g = obj->fillrgb.g;
791 rgb.b = obj->fillrgb.b;
792 fillstyleid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
794 shapeid = ++currentswfid;
795 SetU16(tag,shapeid); // ID
804 SetShapeStyles(tag,shape);
805 ShapeCountBits(shape,NULL,NULL);
806 SetShapeBits(tag,shape);
808 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
813 void starttext(struct swfoutput*obj)
819 tag = InsertTag(tag,ST_DEFINETEXT);
820 textid = ++currentswfid;
846 tag = InsertTag(tag,ST_PLACEOBJECT2);
847 ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
857 tag = InsertTag(tag,ST_PLACEOBJECT2);
858 ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
862 void endpage(struct swfoutput*obj)
869 swfoutput_endclip(obj);
870 tag = InsertTag(tag,ST_SHOWFRAME);
873 void swfoutput_newpage(struct swfoutput*obj)
877 for(depth--;depth>=startdepth;depth--) {
878 tag = InsertTag(tag,ST_REMOVEOBJECT2);
886 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
887 up, complete the swf, and write it out. */
888 void swfoutput_destroy(struct swfoutput* obj)
891 fontlist_t *tmp,*iterator = fontlist;
893 delete iterator->font;
896 iterator = iterator->next;
904 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
909 logf("<fatal> Could not create \"%s\". ", filename);
913 tag = InsertTag(tag,ST_END);
915 if FAILED(WriteSWF(fi,&swf))
916 logf("<error> WriteSWF() failed.\n");
919 logf("<notice> SWF written\n");
922 void swfoutput_setdrawmode(swfoutput* obj, int mode)
925 if(mode == DRAWMODE_FILL)
927 else if(mode == DRAWMODE_EOFILL)
929 else if(mode == DRAWMODE_STROKE)
931 else if(mode == DRAWMODE_CLIP)
933 else if(mode == DRAWMODE_EOCLIP)
937 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
939 if(obj->fillrgb.r == r &&
940 obj->fillrgb.g == g &&
941 obj->fillrgb.b == b &&
942 obj->fillrgb.a == a) return;
952 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
954 if(obj->strokergb.r == r &&
955 obj->strokergb.g == g &&
956 obj->strokergb.b == b &&
957 obj->strokergb.a == a) return;
961 obj->strokergb.r = r;
962 obj->strokergb.g = g;
963 obj->strokergb.b = b;
964 obj->strokergb.a = a;
967 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
969 if(obj->linewidth == (u16)(linewidth*20))
974 obj->linewidth = (u16)(linewidth*20);
978 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
987 logf("<warning> Too many clip levels.");
992 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
993 swfoutput_drawpath(obj, outline, m);
996 tag = InsertTag(tag,ST_PLACEOBJECT2);
997 cliptags[clippos] = tag;
998 clipshapes[clippos] = shapeid;
999 clipdepths[clippos] = depth++;
1004 void swfoutput_endclip(swfoutput*obj)
1012 logf("<error> Invalid end of clipping region");
1016 PlaceObject(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1019 void swfoutput_drawimagefile(struct swfoutput*, char*filename, int sizex,int sizey,
1020 double x1,double y1,
1021 double x2,double y2,
1022 double x3,double y3,
1023 double x4,double y4)
1035 struct plotxy p1,p2,p3,p4;
1037 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1038 if(x2>xmax) xmax=x2;
1039 if(y2>ymax) ymax=y2;
1040 if(x2<xmin) xmin=x2;
1041 if(y2<ymin) ymin=y2;
1042 if(x3>xmax) xmax=x3;
1043 if(y3>ymax) ymax=y3;
1044 if(x3<xmin) xmin=x3;
1045 if(y3<ymin) ymin=y3;
1046 if(x4>xmax) xmax=x4;
1047 if(y4>ymax) ymax=y4;
1048 if(x4<xmin) xmin=x4;
1049 if(y4<ymin) ymin=y4;
1060 m.sx = (int)(65536*20*(x4-x1))/sizex;
1061 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1062 m.r0 = (int)(65536*20*(x1-x2))/sizey;
1063 m.sy = -(int)(65536*20*(y1-y2))/sizey;
1065 m.tx = (int)(x1*20);
1066 m.ty = (int)(y1*20);
1068 bitid = ++currentswfid;
1071 tag = InsertTag(tag,ST_DEFINEBITSJPEG2);
1073 SetJPEGBits(tag, filename, jpegquality);
1076 myshapeid = ++currentswfid;
1077 tag = InsertTag(tag,ST_DEFINESHAPE);
1079 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1080 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1081 fsid = ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1082 SetU16(tag, myshapeid);
1083 r.xmin = (int)(xmin*20);
1084 r.ymin = (int)(ymin*20);
1085 r.xmax = (int)(xmax*20);
1086 r.ymax = (int)(ymax*20);
1088 SetShapeStyles(tag,shape);
1089 ShapeCountBits(shape,NULL,NULL);
1090 SetShapeBits(tag,shape);
1091 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1092 swflastx = swflasty = 0;
1099 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1100 ShapeSetLine (tag, shape, (int)(x1*20);
1101 ShapeSetLine (tag, shape, x*20,0);
1102 ShapeSetLine (tag, shape, 0,-y*20);
1103 ShapeSetLine (tag, shape, -x*20,0);*/
1107 tag = InsertTag(tag,ST_PLACEOBJECT2);
1108 ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);