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;
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];
270 void putcharacters(TAG*tag)
275 color.r = chardata[0].color.r^255;
284 int charadvance[128];
287 int glyphbits=1; //TODO: can this be zero?
290 if(tag->id != ST_DEFINETEXT &&
291 tag->id != ST_DEFINETEXT2) {
292 logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
296 logf("<warning> putcharacters called with zero characters");
299 for(pass = 0; pass < 2; pass++)
309 advancebits++; // add sign bit
310 SetU8(tag, glyphbits);
311 SetU8(tag, advancebits);
314 for(t=0;t<=chardatapos;t++)
316 if(lastfontid != chardata[t].fontid ||
317 lastx!=chardata[t].x ||
318 lasty!=chardata[t].y ||
319 !colorcompare(&color, &chardata[t].color) ||
321 lastsize != chardata[t].size ||
324 if(charstorepos && pass==0)
327 for(s=0;s<charstorepos;s++)
329 while(charids[s]>=(1<<glyphbits))
331 while(charadvance[s]>=(1<<advancebits))
335 if(charstorepos && pass==1)
338 SetBits(tag, 0, 1); // GLYPH Record
339 SetBits(tag, charstorepos, 7); // number of glyphs
341 for(s=0;s<charstorepos;s++)
343 SetBits(tag, charids[s], glyphbits);
344 SetBits(tag, charadvance[s], advancebits);
349 if(pass == 1 && t<chardatapos)
355 if(lastx != chardata[t].x ||
356 lasty != chardata[t].y)
361 if(!colorcompare(&color, &chardata[t].color))
363 color = chardata[t].color;
366 font.id = chardata[t].fontid;
367 if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
371 TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
374 lastfontid = chardata[t].fontid;
375 lastx = chardata[t].x;
376 lasty = chardata[t].y;
377 lastsize = chardata[t].size;
384 int nextt = t==chardatapos-1?t:t+1;
385 int rel = chardata[nextt].x-chardata[t].x;
386 if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
388 lastx=chardata[nextt].x;
394 charids[charstorepos] = chardata[t].charid;
395 charadvance[charstorepos] = advance;
402 void putcharacter(struct swfoutput*obj, int fontid, int charid,
403 int x,int y, int size)
405 if(chardatapos == CHARDATAMAX)
410 chardata[chardatapos].fontid = fontid;
411 chardata[chardatapos].charid = charid;
412 chardata[chardatapos].x = x;
413 chardata[chardatapos].y = y;
414 chardata[chardatapos].color = obj->fillrgb;
415 chardata[chardatapos].size = size;
420 /* process a character. */
421 void drawchar(struct swfoutput*obj, SWFFont*font, char*character, swfmatrix*m)
424 if(m->m12!=0 || m->m21!=0)
429 if(usefonts && ! drawonlyshapes)
431 int charid = font->getSWFCharID(character);
436 putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
437 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
441 T1_OUTLINE*outline = font->getOutline(character);
442 char* charname = character;
445 logf("Didn't find %s in current charset (%s)",
446 character,font->getName());
462 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
467 drawpath(tag, outline, &m2);
472 /* draw a curved polygon. */
473 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline,
481 if(lastwasfill && !fill)
483 ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
486 if(!lastwasfill && fill)
488 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
492 drawpath(tag, outline,m);
495 /* SWFFont: copy all t1 font outlines to a local
497 SWFFont::SWFFont(char*name, int id, char*filename)
499 if(!T1_GetFontName(id))
502 this->name = strdup(T1_GetFontFileName(id));
503 this->fontid = strdup(name);
506 char**a= T1_GetAllCharNames(id);
507 int t=0, outlinepos=0;
515 logf("<verbose> Font %s(%d): Storing %d outlines.\n", name, id, t);
517 outline = (T1_OUTLINE**)malloc(t*sizeof(T1_OUTLINE*));
518 charname = (char**)malloc(t*sizeof(char*));
519 used = (char*)malloc(t*sizeof(char));
520 char2swfcharid = (U16*)malloc(t*2);
521 swfcharid2char = (U16*)malloc(t*2);
524 memset(used,0,t*sizeof(char));
526 this->swfid = ++currentswfid;
540 int ret = T1_ReencodeFont(id, map);
544 int ret = T1_ReencodeFont(id, map);
546 fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
552 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
553 this->charname[outlinepos] = strdup(T1_GetCharName(id, s));
561 /* free all tables, write out definefont tags */
565 int*ptr = (int*)malloc(swfcharpos*sizeof(int));
567 for(t=0;t<charnum;t++)
568 if(used[t]) usednum++;
570 if(usednum && !drawonlyshapes)
572 logf("<verbose> Font %s has %d used characters",fontid, usednum);
573 TAG*ftag = InsertTag(swf.FirstTag,ST_DEFINEFONT);
574 SetU16(ftag, this->swfid);
575 int initpos = GetDataSize(ftag);
581 for(t=0;t<swfcharpos;t++)
583 ptr[t] = GetDataSize(ftag);
584 SetU16(ftag, 0x1234);
586 for(t=0;t<swfcharpos;t++)
588 *(U16*)&ftag->data[ptr[t]] = GetDataSize(ftag)-initpos;
591 SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
595 ShapeSetStyle(ftag,&s,0,1,0);
599 drawpath(ftag, outline[swfcharid2char[t]],&m);
608 for(t=0;t<charnum;t++)
612 free(swfcharid2char);
613 free(char2swfcharid);
616 T1_OUTLINE*SWFFont::getOutline(char*name)
619 for(t=0;t<this->charnum;t++) {
620 if(!strcmp(this->charname[t],name)) {
623 swfcharid2char[swfcharpos] = t;
624 char2swfcharid[t] = swfcharpos;
634 int SWFFont::getSWFCharID(char*name)
637 for(t=0;t<this->charnum;t++) {
638 if(!strcmp(this->charname[t],name)) {
641 swfcharid2char[swfcharpos] = t;
642 char2swfcharid[t] = swfcharpos++;
645 return char2swfcharid[t];
648 logf("<warning> Didn't find character '%s' in font '%s'", name, this->name);
652 char*SWFFont::getName()
663 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
664 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
666 fontlist_t*last=0,*iterator;
667 if(obj->font && !strcmp(obj->font->fontid,fontid))
672 if(!strcmp(iterator->font->fontid,fontid))
675 iterator = iterator->next;
679 obj->font = iterator->font;
684 logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,fontid);
687 SWFFont*font = new SWFFont(fontid, t1id, filename);
688 iterator = new fontlist_t;
689 iterator->font = font;
693 last->next = iterator;
699 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
701 fontlist_t *iterator = fontlist;
703 if(!strcmp(iterator->font->fontid,fontid))
705 iterator = iterator->next;
710 /* set's the matrix which is to be applied to characters drawn by
711 swfoutput_drawchar() */
712 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
713 double m21,double m22)
715 if(obj->fontm11 == m11 &&
716 obj->fontm12 == m12 &&
717 obj->fontm21 == m21 &&
728 /* draws a character at x,y. */
729 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character)
732 m.m11 = obj->fontm11;
733 m.m12 = obj->fontm12;
734 m.m21 = obj->fontm21;
735 m.m22 = obj->fontm22;
738 drawchar(obj, obj->font, character, &m);
741 /* initialize the swf writer */
742 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
747 memset(obj, 0, sizeof(struct swfoutput));
748 filename = _filename;
752 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
756 memset(&swf,0x00,sizeof(SWF));
759 swf.FrameRate = 0x0040; // 1 frame per 4 seconds
760 swf.MovieSize.xmax = 20*sizex;
761 swf.MovieSize.ymax = 20*sizey;
763 swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
770 tag = InsertTag(tag, ST_PROTECT);
775 void swfoutput_setprotected() //write PROTECT tag
780 void startshape(struct swfoutput*obj)
788 tag = InsertTag(tag,ST_DEFINESHAPE);
791 linestyleid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
792 rgb.r = obj->fillrgb.r;
793 rgb.g = obj->fillrgb.g;
794 rgb.b = obj->fillrgb.b;
795 fillstyleid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
797 shapeid = ++currentswfid;
798 SetU16(tag,shapeid); // ID
807 SetShapeStyles(tag,shape);
808 ShapeCountBits(shape,NULL,NULL);
809 SetShapeBits(tag,shape);
811 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
816 void starttext(struct swfoutput*obj)
822 tag = InsertTag(tag,ST_DEFINETEXT);
823 textid = ++currentswfid;
849 tag = InsertTag(tag,ST_PLACEOBJECT2);
850 ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
860 tag = InsertTag(tag,ST_PLACEOBJECT2);
861 ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
865 void endpage(struct swfoutput*obj)
872 swfoutput_endclip(obj);
873 tag = InsertTag(tag,ST_SHOWFRAME);
876 void swfoutput_newpage(struct swfoutput*obj)
880 for(depth--;depth>=startdepth;depth--) {
881 tag = InsertTag(tag,ST_REMOVEOBJECT2);
889 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
890 up, complete the swf, and write it out. */
891 void swfoutput_destroy(struct swfoutput* obj)
894 fontlist_t *tmp,*iterator = fontlist;
896 delete iterator->font;
899 iterator = iterator->next;
907 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
912 logf("<fatal> Could not create \"%s\". ", filename);
916 tag = InsertTag(tag,ST_END);
918 if FAILED(WriteSWF(fi,&swf))
919 logf("<error> WriteSWF() failed.\n");
922 logf("<notice> SWF written\n");
925 void swfoutput_setdrawmode(swfoutput* obj, int mode)
928 if(mode == DRAWMODE_FILL)
930 else if(mode == DRAWMODE_EOFILL)
932 else if(mode == DRAWMODE_STROKE)
934 else if(mode == DRAWMODE_CLIP)
936 else if(mode == DRAWMODE_EOCLIP)
940 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
942 if(obj->fillrgb.r == r &&
943 obj->fillrgb.g == g &&
944 obj->fillrgb.b == b &&
945 obj->fillrgb.a == a) return;
955 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
957 if(obj->strokergb.r == r &&
958 obj->strokergb.g == g &&
959 obj->strokergb.b == b &&
960 obj->strokergb.a == a) return;
964 obj->strokergb.r = r;
965 obj->strokergb.g = g;
966 obj->strokergb.b = b;
967 obj->strokergb.a = a;
970 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
972 if(obj->linewidth == (u16)(linewidth*20))
977 obj->linewidth = (u16)(linewidth*20);
981 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
990 logf("<warning> Too many clip levels.");
995 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
996 swfoutput_drawpath(obj, outline, m);
999 tag = InsertTag(tag,ST_PLACEOBJECT2);
1000 cliptags[clippos] = tag;
1001 clipshapes[clippos] = shapeid;
1002 clipdepths[clippos] = depth++;
1007 void swfoutput_endclip(swfoutput*obj)
1015 logf("<error> Invalid end of clipping region");
1019 PlaceObject(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1022 void swfoutput_drawimagefile(struct swfoutput*, char*filename, int sizex,int sizey,
1023 double x1,double y1,
1024 double x2,double y2,
1025 double x3,double y3,
1026 double x4,double y4)
1038 struct plotxy p1,p2,p3,p4;
1040 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1041 if(x2>xmax) xmax=x2;
1042 if(y2>ymax) ymax=y2;
1043 if(x2<xmin) xmin=x2;
1044 if(y2<ymin) ymin=y2;
1045 if(x3>xmax) xmax=x3;
1046 if(y3>ymax) ymax=y3;
1047 if(x3<xmin) xmin=x3;
1048 if(y3<ymin) ymin=y3;
1049 if(x4>xmax) xmax=x4;
1050 if(y4>ymax) ymax=y4;
1051 if(x4<xmin) xmin=x4;
1052 if(y4<ymin) ymin=y4;
1063 m.sx = (int)(65536*20*(x4-x1))/sizex;
1064 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1065 m.r0 = (int)(65536*20*(x1-x2))/sizey;
1066 m.sy = -(int)(65536*20*(y1-y2))/sizey;
1068 m.tx = (int)(x1*20);
1069 m.ty = (int)(y1*20);
1071 bitid = ++currentswfid;
1074 tag = InsertTag(tag,ST_DEFINEBITSJPEG2);
1076 SetJPEGBits(tag, filename, jpegquality);
1079 myshapeid = ++currentswfid;
1080 tag = InsertTag(tag,ST_DEFINESHAPE);
1082 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1083 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1084 fsid = ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1085 SetU16(tag, myshapeid);
1086 r.xmin = (int)(xmin*20);
1087 r.ymin = (int)(ymin*20);
1088 r.xmax = (int)(xmax*20);
1089 r.ymax = (int)(ymax*20);
1091 SetShapeStyles(tag,shape);
1092 ShapeCountBits(shape,NULL,NULL);
1093 SetShapeBits(tag,shape);
1094 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1095 swflastx = swflasty = 0;
1102 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
1103 ShapeSetLine (tag, shape, (int)(x1*20);
1104 ShapeSetLine (tag, shape, x*20,0);
1105 ShapeSetLine (tag, shape, 0,-y*20);
1106 ShapeSetLine (tag, shape, -x*20,0);*/
1110 tag = InsertTag(tag,ST_PLACEOBJECT2);
1111 ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);