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 typedef unsigned char u8;
32 typedef unsigned short int u16;
33 typedef unsigned long int u32;
36 static int flag_protected;
39 static int shapeid = -1;
40 static int shapecount = 0;
42 static int fillstyleid;
43 static int linestyleid;
44 static int swflastx=0;
45 static int swflasty=0;
46 static int lastwasfill = 0;
47 static char* filename = 0;
52 static int startdepth = 1;
58 void startshape(struct swfoutput* obj);
60 // matrix multiplication. changes p0
61 void transform (plotxy*p0,struct swfmatrix*m)
64 x = m->m11*p0->x+m->m12*p0->y;
65 y = m->m21*p0->x+m->m22*p0->y;
70 // write a move-to command into the swf
71 void moveto(plotxy p0)
73 int rx = (int)(p0.x*20);
74 int ry = (int)(p0.y*20);
75 if(rx!=swflastx || ry!=swflasty) {
76 ShapeSetMove (tag, shape, rx,ry);
82 // write a line-to command into the swf
83 void lineto(plotxy p0)
85 int rx = ((int)(p0.x*20)-swflastx);
86 int ry = ((int)(p0.y*20)-swflasty);
87 /* we can't skip this for rx=0,ry=0, those
89 ShapeSetLine (tag, shape, rx,ry);
94 // write a spline-to command into the swf
95 void splineto(plotxy control,plotxy end)
97 int cx = ((int)(control.x*20)-swflastx);
98 int cy = ((int)(control.y*20)-swflasty);
101 int ex = ((int)(end.x*20)-swflastx);
102 int ey = ((int)(end.y*20)-swflasty);
105 ShapeSetCurve(tag, shape, cx,cy,ex,ey);
108 /* write a line, given two points and the transformation
110 void line(plotxy p0, plotxy p1, struct swfmatrix*m)
118 /* write a cubic (!) spline. This involves calling the approximate()
119 function out of spline.cc to convert it to a quadratic spline. */
120 void spline(plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
123 struct qspline q[16];
131 num = approximate(p0,p1,p2,p3,q);
134 splineto(q[t].control, q[t].end);
138 /* Adds an outline to a font. Applies only the 2x2 component of the transformation matrix.
140 void addtofont(T1_OUTLINE*outline, struct swfmatrix*m, char*namehint)
144 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
145 (representing characters) */
146 void drawpath(T1_OUTLINE*outline, struct swfmatrix*m, char*namehint)
149 double lastx=0,lasty=0;
153 logf("<debug> Pathtype:%s",outline->type == T1_PATHTYPE_MOVE?"MOVE":
154 (outline->type == T1_PATHTYPE_LINE?"LINE"
156 logf("<debug> relative coordinates: %08x,%08x", outline->dest.x, outline->dest.y);
157 x += (outline->dest.x/(float)0xffff);
158 y += (outline->dest.y/(float)0xffff);
159 logf("<debug> coordinates: %f,%f", x, y);
160 if(outline->type == T1_PATHTYPE_MOVE)
163 else if(outline->type == T1_PATHTYPE_LINE)
173 else if(outline->type == T1_PATHTYPE_BEZIER)
179 T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
182 p1.x=o2->C.x/(float)0xffff+lastx;
183 p1.y=o2->C.y/(float)0xffff+lasty;
184 p2.x=o2->B.x/(float)0xffff+lastx;
185 p2.y=o2->B.y/(float)0xffff+lasty;
188 spline(p0,p1,p2,p3,m);
191 logf("<error> drawpath: unknown outline type:%d\n", outline->type);
195 outline = outline->link;
199 /* process a character. */
200 void drawchar(struct swfoutput*obj, int t1fontindex, char character, swfmatrix*m)
205 int width = T1_GetCharWidth(t1fontindex, character);
206 BBox bbox = T1_GetCharBBox(t1fontindex, character);
207 char*charname= T1_GetCharName(t1fontindex, character);
208 logf("<debug> Font name is %s", T1_GetFontFileName(t1fontindex));
209 logf("<debug> char 0x%02x is named %s\n",character,charname);
210 logf("<debug> bbox: %d %d %d %d\n",bbox.llx,bbox.lly,bbox.urx,bbox.ury);
211 if(!charname || charname[0] == '.')
213 /* for newline, we don't print an error. FIXME: We shouldn't get newlines here
216 if(character != '\n') {
217 logf("<error> Char to set is not defined!");
218 logf("<error> - font file is %s\n", T1_GetFontFileName(t1fontindex));
219 logf("<error> - char 0x%02x is named %s\n",character,charname);
228 outline = T1_GetCharOutline( t1fontindex, character, 100.0, 0);
236 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
239 drawpath(outline, &m2, charname);
242 /* draw a curved polygon. */
243 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, struct swfmatrix*m)
248 if(lastwasfill && !fill)
250 ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
253 if(!lastwasfill && fill)
255 ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
259 drawpath(outline,m, 0);
263 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
264 int swfoutput_setfont(struct swfoutput*obj, int fontid, int t1id)
269 /* set's the matrix which is to be applied to characters drawn by
270 swfoutput_drawchar() */
271 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
272 double m21,double m22)
280 /* draws a character at x,y. */
281 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char character)
284 m.m11 = obj->fontm11;
285 m.m12 = obj->fontm12;
286 m.m21 = obj->fontm21;
287 m.m22 = obj->fontm22;
290 drawchar(obj, obj->t1font, character, &m);
293 /* initialize the swf writer */
294 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey)
299 memset(obj, 0, sizeof(struct swfoutput));
300 filename = _filename;
304 logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
308 memset(&swf,0x00,sizeof(SWF));
311 // swf.FrameRate = 0x1900;
312 swf.FrameRate = 0x0040; // 1 frame per 4 seconds
313 swf.MovieSize.xmax = 20*sizex;
314 swf.MovieSize.ymax = 20*sizey;
316 swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
323 tag = InsertTag(tag, ST_PROTECT);
328 void swfoutput_setprotected() //write PROTECT tag
333 void startshape(struct swfoutput*obj)
337 tag = InsertTag(tag,ST_DEFINESHAPE);
340 linestyleid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
341 rgb.r = obj->fillrgb.r;
342 rgb.g = obj->fillrgb.g;
343 rgb.b = obj->fillrgb.b;
344 fillstyleid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
346 shapeid = ++shapecount;
347 SetU16(tag,shapeid); // ID
356 SetShapeStyles(tag,shape);
357 ShapeCountBits(shape,NULL,NULL);
358 SetShapeBits(tag,shape);
360 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
370 tag = InsertTag(tag,ST_PLACEOBJECT2);
371 ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
375 void endpage(struct swfoutput*obj)
380 swfoutput_endclip(obj);
381 tag = InsertTag(tag,ST_SHOWFRAME);
384 void swfoutput_newpage(struct swfoutput*obj)
388 for(depth--;depth>=startdepth;depth--) {
389 tag = InsertTag(tag,ST_REMOVEOBJECT2);
397 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
398 up, complete the swf, and write it out. */
399 void swfoutput_destroy(struct swfoutput* obj)
407 fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
412 logf("<fatal> Could not create \"%s\". ", filename);
416 tag = InsertTag(tag,ST_END);
418 if FAILED(WriteSWF(fi,&swf))
419 logf("<error> WriteSWF() failed.\n");
422 printf("SWF written\n");
425 void swfoutput_setdrawmode(swfoutput* obj, int mode)
427 if(mode == DRAWMODE_FILL)
429 else if(mode == DRAWMODE_EOFILL)
431 else if(mode == DRAWMODE_STROKE)
433 else if(mode == DRAWMODE_CLIP)
435 else if(mode == DRAWMODE_EOCLIP)
439 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
441 if(obj->fillrgb.r == r &&
442 obj->fillrgb.g == g &&
443 obj->fillrgb.b == b &&
444 obj->fillrgb.a == a) return;
454 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
456 if(obj->strokergb.r == r &&
457 obj->strokergb.g == g &&
458 obj->strokergb.b == b &&
459 obj->strokergb.a == a) return;
463 obj->strokergb.r = r;
464 obj->strokergb.g = g;
465 obj->strokergb.b = b;
466 obj->strokergb.a = a;
469 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
471 if(obj->linewidth == (u16)(linewidth*20))
476 obj->linewidth = (u16)(linewidth*20);
480 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
487 logf("<warning> Too many clip levels.");
493 swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
494 swfoutput_drawpath(obj, outline, m);
497 tag = InsertTag(tag,ST_PLACEOBJECT2);
498 cliptags[clippos] = tag;
499 clipshapes[clippos] = shapeid;
500 clipdepths[clippos] = depth++;
505 void swfoutput_endclip(swfoutput*obj)
511 logf("<error> Invalid end of clipping region");
515 PlaceObject(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
518 void swfoutput_drawimagefile(struct swfoutput*, char*filename, int sizex,int sizey,
529 struct plotxy p1,p2,p3,p4;
531 double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
554 m.sx = (int)(65536*20*(x4-x1))/sizex;
555 m.r1 = -(int)(65536*20*(y4-y1))/sizex;
556 m.r0 = (int)(65536*20*(x1-x2))/sizey;
557 m.sy = -(int)(65536*20*(y1-y2))/sizey;
565 bitid = ++shapecount;
568 tag = InsertTag(tag,ST_DEFINEBITSJPEG2);
570 SetJPEGBits(tag, filename, 85);
573 myshapeid = ++shapecount;
574 tag = InsertTag(tag,ST_DEFINESHAPE);
576 //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
577 //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
578 fsid = ShapeAddBitmapFillStyle(shape,&m,bitid,0);
579 SetU16(tag, myshapeid);
580 r.xmin = (int)(xmin*20);
581 r.ymin = (int)(ymin*20);
582 r.xmax = (int)(xmax*20);
583 r.ymax = (int)(ymax*20);
585 SetShapeStyles(tag,shape);
586 ShapeCountBits(shape,NULL,NULL);
587 SetShapeBits(tag,shape);
588 ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
589 swflastx = swflasty = 0;
596 ShapeMoveTo (tag, shape, (int)(x1*20),(int)(y1*20));
597 ShapeSetLine (tag, shape, (int)(x1*20);
598 ShapeSetLine (tag, shape, x*20,0);
599 ShapeSetLine (tag, shape, 0,-y*20);
600 ShapeSetLine (tag, shape, -x*20,0);*/
604 tag = InsertTag(tag,ST_PLACEOBJECT2);
605 ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);