Initial revision
[swftools.git] / pdf2swf / swfoutput.cc
1 /* swfoutput.cc
2    Implements generation of swf files using the rfxswf lib. The routines
3    in this file are called from pdf2swf.
4
5    This file is part of swftools.
6
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.
11
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.
16
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 */
20
21 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include "swfoutput.h"
25 #include "spline.h"
26 extern "C" {
27 #include "../lib/log.h"
28 #include "../lib/rfxswf.h"
29 }
30
31 typedef unsigned char u8;
32 typedef unsigned short int u16;
33 typedef unsigned long int u32;
34
35 static int fi;
36 static int flag_protected;
37 static SWF swf;
38 static TAG *tag;
39 static int shapeid = -1;
40 static int shapecount = 0;
41 static SHAPE* shape;
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;
48 static int sizex;
49 static int sizey;
50 static char fill = 0;
51 static int depth = 1;
52 static int startdepth = 1;
53 TAG* cliptags[128];
54 int clipshapes[128];
55 u32 clipdepths[128];
56 int clippos = 0;
57
58 void startshape(struct swfoutput* obj);
59
60 // matrix multiplication. changes p0
61 void transform (plotxy*p0,struct swfmatrix*m)
62 {
63     double x,y;
64     x = m->m11*p0->x+m->m12*p0->y;
65     y = m->m21*p0->x+m->m22*p0->y;
66     p0->x = x + m->m13;
67     p0->y = y + m->m23;
68 }
69
70 // write a move-to command into the swf
71 void moveto(plotxy p0)
72 {
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);
77     }
78     swflastx=rx;
79     swflasty=ry;
80 }
81
82 // write a line-to command into the swf
83 void lineto(plotxy p0)
84 {
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
88        are plots */
89     ShapeSetLine (tag, shape, rx,ry);
90     swflastx+=rx;
91     swflasty+=ry;
92 }
93
94 // write a spline-to command into the swf
95 void splineto(plotxy control,plotxy end)
96 {
97     int cx = ((int)(control.x*20)-swflastx);
98     int cy = ((int)(control.y*20)-swflasty);
99     swflastx += cx;
100     swflasty += cy;
101     int ex = ((int)(end.x*20)-swflastx);
102     int ey = ((int)(end.y*20)-swflasty);
103     swflastx += ex;
104     swflasty += ey;
105     ShapeSetCurve(tag, shape, cx,cy,ex,ey);
106 }
107
108 /* write a line, given two points and the transformation
109    matrix. */
110 void line(plotxy p0, plotxy p1, struct swfmatrix*m)
111 {
112     transform(&p0,m);
113     transform(&p1,m);
114     moveto(p0);
115     lineto(p1);
116 }
117
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)
121 {
122     double d;
123     struct qspline q[16];
124     int num;
125     int t;
126     transform(&p0,m);
127     transform(&p1,m);
128     transform(&p2,m);
129     transform(&p3,m);
130
131     num = approximate(p0,p1,p2,p3,q);
132     for(t=0;t<num;t++) {
133         moveto(q[t].start);
134         splineto(q[t].control, q[t].end);
135     }
136 }
137
138 /* Adds an outline to a font. Applies only the 2x2 component of the transformation matrix. 
139  */
140 void addtofont(T1_OUTLINE*outline, struct swfmatrix*m, char*namehint)
141 {
142 }
143
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)
147 {
148     double x=0,y=0;
149     double lastx=0,lasty=0;
150
151     while (outline)
152     {
153         logf("<debug> Pathtype:%s",outline->type == T1_PATHTYPE_MOVE?"MOVE":
154                                                     (outline->type == T1_PATHTYPE_LINE?"LINE"
155                                                                                       :"BEZIER"));
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)
161         {
162         }
163         else if(outline->type == T1_PATHTYPE_LINE) 
164         {
165             plotxy p0;
166             plotxy p1;
167             p0.x=lastx;
168             p0.y=lasty;
169             p1.x=x;
170             p1.y=y;
171             line(p0,p1,m);
172         }
173         else if(outline->type == T1_PATHTYPE_BEZIER)
174         {
175             plotxy p0;
176             plotxy p1;
177             plotxy p2;
178             plotxy p3;
179             T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
180             p0.x=x; 
181             p0.y=y;
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;
186             p3.x=lastx;
187             p3.y=lasty;
188             spline(p0,p1,p2,p3,m);
189         } 
190         else {
191          logf("<error> drawpath: unknown outline type:%d\n", outline->type);
192         }
193         lastx=x;
194         lasty=y;
195         outline = outline->link;
196     }
197 }
198
199 /* process a character. */
200 void drawchar(struct swfoutput*obj, int t1fontindex, char character, swfmatrix*m)
201 {
202
203     /* <T1 stuff> */
204     T1_OUTLINE*outline;
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] == '.') 
212     {
213      /* for newline, we don't print an error. FIXME: We shouldn't get newlines here
214         in the first place
215       */
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);
220      }
221      return;
222     }
223     swfmatrix m2=*m;    
224     m2.m11/=100;
225     m2.m21/=100;
226     m2.m12/=100;
227     m2.m22/=100;
228     outline =  T1_GetCharOutline( t1fontindex, character, 100.0, 0);
229
230     /** </T1 stuff> **/
231
232     if(shapeid<0)
233         startshape(obj);
234
235     if(!lastwasfill)
236      ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
237     lastwasfill = 1;
238
239     drawpath(outline, &m2, charname);
240 }
241
242 /* draw a curved polygon. */
243 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, struct swfmatrix*m)
244 {
245     if(shapeid<0)
246         startshape(output);
247
248     if(lastwasfill && !fill)
249     {
250      ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
251      lastwasfill = 0;
252     }
253     if(!lastwasfill && fill)
254     {
255      ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
256      lastwasfill = 1;
257     }
258
259     drawpath(outline,m, 0); 
260 }
261
262
263 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
264 int swfoutput_setfont(struct swfoutput*obj, int t1id)
265 {
266     obj->t1font = t1id;
267 }
268
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)
273 {
274     obj->fontm11 = m11;
275     obj->fontm12 = m12;
276     obj->fontm21 = m21;
277     obj->fontm22 = m22;
278 }
279
280 /* draws a character at x,y. */
281 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char character) 
282 {
283     swfmatrix m;
284     m.m11 = obj->fontm11;
285     m.m12 = obj->fontm12;
286     m.m21 = obj->fontm21;
287     m.m22 = obj->fontm22;
288     m.m13 = x;
289     m.m23 = y;
290     drawchar(obj, obj->t1font, character, &m);
291 }
292
293 /* initialize the swf writer */
294 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey) 
295 {
296   GLYPH *glyph;
297   RGBA rgb;
298   SRECT r;
299   memset(obj, 0, sizeof(struct swfoutput));
300   filename = _filename;
301   sizex = _sizex;
302   sizey = _sizey;
303
304   logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
305
306   obj->t1font = 0;
307   
308   memset(&swf,0x00,sizeof(SWF));
309
310   swf.FileVersion    = 4;
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;
315   
316   swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
317   tag = swf.FirstTag;
318   rgb.r = 0xff;
319   rgb.g = 0xff;
320   rgb.b = 0xff;
321   SetRGB(tag,&rgb);
322   if(flag_protected)
323     tag = InsertTag(tag, ST_PROTECT);
324   depth = 1;
325   startdepth = depth;
326 }
327
328 void swfoutput_setprotected() //write PROTECT tag
329 {
330   flag_protected = 1;
331 }
332
333 void startshape(struct swfoutput*obj)
334 {
335   RGBA rgb;
336   SRECT r;
337   tag = InsertTag(tag,ST_DEFINESHAPE);
338
339   NewShape(&shape);
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);
345
346   shapeid = ++shapecount;
347   SetU16(tag,shapeid);  // ID
348
349   r.xmin = 0;
350   r.ymin = 0;
351   r.xmax = 20*sizex;
352   r.ymax = 20*sizey;
353   
354   SetRect(tag,&r);
355
356   SetShapeStyles(tag,shape);
357   ShapeCountBits(shape,NULL,NULL);
358   SetShapeBits(tag,shape);
359
360   ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
361   swflastx=swflasty=0;
362   lastwasfill = 0;
363 }
364
365 void endshape()
366 {
367     if(shapeid<0) 
368         return;
369     ShapeSetEnd(tag);
370     tag = InsertTag(tag,ST_PLACEOBJECT2);
371     ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
372     shapeid = -1;
373 }
374
375 void endpage(struct swfoutput*obj)
376 {
377     if(shapeid>=0)
378       endshape();
379     while(clippos)
380         swfoutput_endclip(obj);
381     tag = InsertTag(tag,ST_SHOWFRAME);
382 }
383
384 void swfoutput_newpage(struct swfoutput*obj)
385 {
386     endpage(obj);
387
388     for(depth--;depth>=startdepth;depth--) {
389         tag = InsertTag(tag,ST_REMOVEOBJECT2);
390         SetU16(tag,depth);
391     }
392
393     depth = 1;
394     startdepth = depth;
395 }
396
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) 
400 {
401     endpage(obj);
402
403     T1_CloseLib();
404     if(!filename) 
405         return;
406     if(filename)
407      fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
408     else
409      fi = 1; // stdout
410     
411     if(fi<=0) {
412      logf("<fatal> Could not create \"%s\". ", filename);
413      exit(1);
414     }
415  
416     tag = InsertTag(tag,ST_END);
417
418     if FAILED(WriteSWF(fi,&swf)) 
419      logf("<error> WriteSWF() failed.\n");
420     if(filename)
421      close(fi);
422     printf("SWF written\n");
423 }
424
425 void swfoutput_setdrawmode(swfoutput* obj, int mode)
426 {
427     if(mode == DRAWMODE_FILL)
428      fill = 1;
429     else if(mode == DRAWMODE_EOFILL)
430      fill = 1;
431     else if(mode == DRAWMODE_STROKE)
432      fill = 0;
433     else if(mode == DRAWMODE_CLIP)
434      fill = 1;
435     else if(mode == DRAWMODE_EOCLIP)
436      fill = 1;
437 }
438
439 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
440 {
441     if(shape>=0)
442      endshape();
443     obj->fillrgb.r = r;
444     obj->fillrgb.g = g;
445     obj->fillrgb.b = b;
446     obj->fillrgb.a = a;
447 }
448
449 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
450 {
451     if(shape>=0)
452      endshape();
453     obj->strokergb.r = r;
454     obj->strokergb.g = g;
455     obj->strokergb.b = b;
456     obj->strokergb.a = a;
457 }
458
459 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
460 {
461     if(shape>=0)
462      endshape();
463     obj->linewidth = (u16)(linewidth*20);
464 }
465
466
467 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
468 {
469     if(shape>=0)
470      endshape();
471
472     if(clippos >= 127)
473     {
474         logf("<warning> Too many clip levels.");
475         clippos --;
476     } 
477
478     startshape(obj);
479
480     swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
481     swfoutput_drawpath(obj, outline, m);
482
483     ShapeSetEnd(tag);
484     tag = InsertTag(tag,ST_PLACEOBJECT2);
485     cliptags[clippos] = tag;
486     clipshapes[clippos] = shapeid;
487     clipdepths[clippos] = depth++;
488     clippos++;
489     shapeid = -1;
490 }
491
492 void swfoutput_endclip(swfoutput*obj)
493 {
494     if(shape>=0)
495      endshape();
496
497     if(!clippos) {
498         logf("<error> Invalid end of clipping region");
499         return;
500     }
501     clippos--;
502     PlaceObject(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
503 }
504
505 void swfoutput_drawimagefile(struct swfoutput*, char*filename, int sizex,int sizey, 
506         double x1,double y1,
507         double x2,double y2,
508         double x3,double y3,
509         double x4,double y4)
510 {
511     RGBA rgb;
512     SRECT r;
513     int lsid=0;
514     int fsid;
515     int bitid;
516     struct plotxy p1,p2,p3,p4;
517     int myshapeid;
518     double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
519     if(x2>xmax) xmax=x2;
520     if(y2>ymax) ymax=y2;
521     if(x2<xmin) xmin=x2;
522     if(y2<ymin) ymin=y2;
523     if(x3>xmax) xmax=x3;
524     if(y3>ymax) ymax=y3;
525     if(x3<xmin) xmin=x3;
526     if(y3<ymin) ymin=y3;
527     if(x4>xmax) xmax=x4;
528     if(y4>ymax) ymax=y4;
529     if(x4<xmin) xmin=x4;
530     if(y4<ymin) ymin=y4;
531     p1.x=x1;
532     p1.y=y1;
533     p2.x=x2;
534     p2.y=y2;
535     p3.x=x3;
536     p3.y=y3;
537     p4.x=x4;
538     p4.y=y4;
539     
540     MATRIX m;
541     m.sx = (int)(65536*20*(x4-x1))/sizex;
542     m.r1 = -(int)(65536*20*(y4-y1))/sizex;
543     m.r0 = (int)(65536*20*(x1-x2))/sizey;
544     m.sy = -(int)(65536*20*(y1-y2))/sizey;
545
546     m.tx = (int)(x1*20);
547     m.ty = (int)(y1*20);
548     
549     if(shape>=0)
550      endshape();
551
552     bitid = ++shapecount;
553   
554     /* bitmap */
555     tag = InsertTag(tag,ST_DEFINEBITSJPEG2);
556     SetU16(tag, bitid);
557     SetJPEGBits(tag, filename, 85);
558
559     /* shape */
560     myshapeid = ++shapecount;
561     tag = InsertTag(tag,ST_DEFINESHAPE);
562     NewShape(&shape);
563     //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
564     //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
565     fsid = ShapeAddBitmapFillStyle(shape,&m,bitid,0);
566     SetU16(tag, myshapeid);
567     r.xmin = (int)(xmin*20);
568     r.ymin = (int)(ymin*20);
569     r.xmax = (int)(xmax*20);
570     r.ymax = (int)(ymax*20);
571     SetRect(tag,&r);
572     SetShapeStyles(tag,shape);
573     ShapeCountBits(shape,NULL,NULL);
574     SetShapeBits(tag,shape);
575     ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
576     swflastx = swflasty = 0;
577     moveto(p1);
578     lineto(p2);
579     lineto(p3);
580     lineto(p4);
581     lineto(p1);
582     /*
583     ShapeMoveTo  (tag, shape, (int)(x1*20),(int)(y1*20));
584     ShapeSetLine (tag, shape, (int)(x1*20);
585     ShapeSetLine (tag, shape, x*20,0);
586     ShapeSetLine (tag, shape, 0,-y*20);
587     ShapeSetLine (tag, shape, -x*20,0);*/
588     ShapeSetEnd(tag);
589
590     /* instance */
591     tag = InsertTag(tag,ST_PLACEOBJECT2);
592     ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
593 }
594