* new function SWFFont::getWidth(char*)
[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 #define standardEncodingSize 335
31 extern char *standardEncodingNames[standardEncodingSize];
32
33 int opennewwindow=0;
34 int ignoredraworder=0;
35 int drawonlyshapes=0;
36 int jpegquality=85;
37 int storeallcharacters=0;
38 static int flag_protected = 0;
39
40 typedef unsigned char u8;
41 typedef unsigned short int u16;
42 typedef unsigned long int u32;
43
44 static int fi;
45 static char* filename = 0;
46 static SWF swf;
47 static TAG *tag;
48 static int currentswfid = 0;
49 static int depth = 1;
50 static int startdepth = 1;
51
52 static SHAPE* shape;
53 static int shapeid = -1;
54 static int textid = -1;
55
56 static int drawmode = -1;
57 static char storefont = 0;
58 static int fillstyleid;
59 static int linestyleid;
60 static int swflastx=0;
61 static int swflasty=0;
62 static int lastwasfill = 0;
63 static char fill = 0;
64 static int sizex;
65 static int sizey;
66 TAG* cliptags[128];
67 int clipshapes[128];
68 u32 clipdepths[128];
69 int clippos = 0;
70
71 int CHARMIDX = 0;
72 int CHARMIDY = 0;
73
74 void startshape(struct swfoutput* obj);
75 void starttext(struct swfoutput* obj);
76 void endshape();
77 void endtext();
78
79 // matrix multiplication. changes p0
80 void transform (plotxy*p0,struct swfmatrix*m)
81 {
82     double x,y;
83     x = m->m11*p0->x+m->m12*p0->y;
84     y = m->m21*p0->x+m->m22*p0->y;
85     p0->x = x + m->m13;
86     p0->y = y + m->m23;
87 }
88
89 // write a move-to command into the swf
90 void moveto(TAG*tag, plotxy p0)
91 {
92     int rx = (int)(p0.x*20);
93     int ry = (int)(p0.y*20);
94     if(rx!=swflastx || ry!=swflasty) {
95       swf_ShapeSetMove (tag, shape, rx,ry);
96     }
97     swflastx=rx;
98     swflasty=ry;
99 }
100
101 // write a line-to command into the swf
102 void lineto(TAG*tag, plotxy p0)
103 {
104     int rx = ((int)(p0.x*20)-swflastx);
105     int ry = ((int)(p0.y*20)-swflasty);
106     /* we can't skip this for rx=0,ry=0, those
107        are plots */
108     swf_ShapeSetLine (tag, shape, rx,ry);
109     swflastx+=rx;
110     swflasty+=ry;
111 }
112
113 // write a spline-to command into the swf
114 void splineto(TAG*tag, plotxy control,plotxy end)
115 {
116     int cx = ((int)(control.x*20)-swflastx);
117     int cy = ((int)(control.y*20)-swflasty);
118     swflastx += cx;
119     swflasty += cy;
120     int ex = ((int)(end.x*20)-swflastx);
121     int ey = ((int)(end.y*20)-swflasty);
122     swflastx += ex;
123     swflasty += ey;
124     swf_ShapeSetCurve(tag, shape, cx,cy,ex,ey);
125 }
126
127 /* write a line, given two points and the transformation
128    matrix. */
129 void line(TAG*tag, plotxy p0, plotxy p1, struct swfmatrix*m)
130 {
131     transform(&p0,m);
132     transform(&p1,m);
133     moveto(tag, p0);
134     lineto(tag, p1);
135 }
136
137 /* write a cubic (!) spline. This involves calling the approximate()
138    function out of spline.cc to convert it to a quadratic spline.  */
139 void spline(TAG*tag,plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
140 {
141     double d;
142     struct qspline q[16];
143     int num;
144     int t;
145     transform(&p0,m);
146     transform(&p1,m);
147     transform(&p2,m);
148     transform(&p3,m);
149
150     num = approximate(p0,p1,p2,p3,q);
151     for(t=0;t<num;t++) {
152         moveto(tag,q[t].start);
153         splineto(tag,q[t].control, q[t].end);
154     }
155 }
156
157 void resetdrawer()
158 {
159     swflastx = 0;
160     swflasty = 0;
161 }
162
163 /* draw a T1 outline. These are generated by pdf2swf and by t1lib.
164    (representing characters) */
165 void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log)
166 {
167     if(tag->id != ST_DEFINEFONT &&
168         tag->id != ST_DEFINESHAPE &&
169         tag->id != ST_DEFINESHAPE2 &&
170         tag->id != ST_DEFINESHAPE3)
171     {
172         logf("<error> internal error: drawpath needs a shape tag, not %d\n",tag->id);
173         exit(1);
174     }
175     double x=0,y=0;
176     double lastx=0,lasty=0;
177     double firstx=0,firsty=0;
178     int init=1;
179
180     while (outline)
181     {
182         x += (outline->dest.x/(float)0xffff);
183         y += (outline->dest.y/(float)0xffff);
184         if(outline->type == T1_PATHTYPE_MOVE)
185         {
186             if(((int)(lastx*20) != (int)(firstx*20) ||
187                 (int)(lasty*20) != (int)(firsty*20)) &&
188                      fill && !init)
189             {
190                 plotxy p0;
191                 plotxy p1;
192                 p0.x=lastx;
193                 p0.y=lasty;
194                 p1.x=firstx;
195                 p1.y=firsty;
196                 if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
197                 line(tag, p0, p1, m);
198             }
199             firstx=x;
200             firsty=y;
201             init = 0;
202         }
203         else if(outline->type == T1_PATHTYPE_LINE) 
204         {
205             plotxy p0;
206             plotxy p1;
207             p0.x=lastx;
208             p0.y=lasty;
209             p1.x=x;
210             p1.y=y;
211             if(log) printf("line: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
212             line(tag, p0,p1,m);
213         }
214         else if(outline->type == T1_PATHTYPE_BEZIER)
215         {
216             plotxy p0;
217             plotxy p1;
218             plotxy p2;
219             plotxy p3;
220             T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
221             p0.x=x; 
222             p0.y=y;
223             p1.x=o2->C.x/(float)0xffff+lastx;
224             p1.y=o2->C.y/(float)0xffff+lasty;
225             p2.x=o2->B.x/(float)0xffff+lastx;
226             p2.y=o2->B.y/(float)0xffff+lasty;
227             p3.x=lastx;
228             p3.y=lasty;
229             if(log) printf("spline: %f,%f -> %f,%f\n",p3.x,p3.y,p0.x,p0.y);
230             spline(tag,p0,p1,p2,p3,m);
231         } 
232         else {
233          logf("<error> drawpath: unknown outline type:%d\n", outline->type);
234         }
235         lastx=x;
236         lasty=y;
237         outline = outline->link;
238     }
239     if(((int)(lastx*20) != (int)(firstx*20) ||
240         (int)(lasty*20) != (int)(firsty*20)) &&
241              fill)
242     {
243         plotxy p0;
244         plotxy p1;
245         p0.x=lastx;
246         p0.y=lasty;
247         p1.x=firstx;
248         p1.y=firsty;
249         if(log) printf("fix: %f,%f -> %f,%f\n",p0.x,p0.y,p1.x,p1.y);
250         line(tag, p0, p1, m);
251     }
252 }
253
254 int colorcompare(RGBA*a,RGBA*b)
255 {
256
257     if(a->r!=b->r ||
258        a->g!=b->g ||
259        a->b!=b->b ||
260        a->a!=b->a) {
261         return 0;
262     }
263     return 1;
264 }
265
266 static const int CHARDATAMAX = 1024;
267 struct chardata {
268     int charid;
269     int fontid;
270     int x;
271     int y;
272     int size;
273     RGBA color;
274 } chardata[CHARDATAMAX];
275 int chardatapos = 0;
276
277 void putcharacters(TAG*tag)
278 {
279     int t;
280     SWFFONT font;
281     RGBA color;
282     color.r = chardata[0].color.r^255;
283     color.g = 0;
284     color.b = 0;
285     color.a = 0;
286     int lastfontid;
287     int lastx;
288     int lasty;
289     int lastsize;
290     int charids[128];
291     int charadvance[128];
292     int charstorepos;
293     int pass;
294     int glyphbits=1; //TODO: can this be zero?
295     int advancebits=1;
296
297     if(tag->id != ST_DEFINETEXT &&
298         tag->id != ST_DEFINETEXT2) {
299         logf("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
300         exit(1);
301     }
302     if(!chardatapos) {
303         logf("<warning> putcharacters called with zero characters");
304     }
305
306     for(pass = 0; pass < 2; pass++)
307     {
308         charstorepos = 0;
309         lastfontid = -1;
310         lastx = CHARMIDX;
311         lasty = CHARMIDY;
312         lastsize = -1;
313
314         if(pass==1)
315         {
316             advancebits++; // add sign bit
317             swf_SetU8(tag, glyphbits);
318             swf_SetU8(tag, advancebits);
319         }
320
321         for(t=0;t<=chardatapos;t++)
322         {
323             if(lastfontid != chardata[t].fontid || 
324                     lastx!=chardata[t].x ||
325                     lasty!=chardata[t].y ||
326                     !colorcompare(&color, &chardata[t].color) ||
327                     charstorepos==127 ||
328                     lastsize != chardata[t].size ||
329                     t == chardatapos)
330             {
331                 if(charstorepos && pass==0)
332                 {
333                     int s;
334                     for(s=0;s<charstorepos;s++)
335                     {
336                         while(charids[s]>=(1<<glyphbits))
337                             glyphbits++;
338                         while(charadvance[s]>=(1<<advancebits))
339                             advancebits++;
340                     }
341                 }
342                 if(charstorepos && pass==1)
343                 {
344                     tag->writeBit = 0; // Q&D
345                     swf_SetBits(tag, 0, 1); // GLYPH Record
346                     swf_SetBits(tag, charstorepos, 7); // number of glyphs
347                     int s;
348                     for(s=0;s<charstorepos;s++)
349                     {
350                         swf_SetBits(tag, charids[s], glyphbits);
351                         swf_SetBits(tag, charadvance[s], advancebits);
352                     }
353                 }
354                 charstorepos = 0;
355
356                 if(pass == 1 && t<chardatapos)
357                 {
358                     RGBA*newcolor=0;
359                     SWFFONT*newfont=0;
360                     int newx = 0;
361                     int newy = 0;
362                     if(lastx != chardata[t].x ||
363                        lasty != chardata[t].y)
364                     {
365                         newx=chardata[t].x;
366                         newy=chardata[t].y;
367                     }
368                     if(!colorcompare(&color, &chardata[t].color)) 
369                     {
370                         color = chardata[t].color;
371                         newcolor = &color;
372                     }
373                     font.id = chardata[t].fontid;
374                     if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size)
375                         newfont = &font;
376
377                     tag->writeBit = 0; // Q&D
378                     swf_TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy);
379                 }
380
381                 lastfontid = chardata[t].fontid;
382                 lastx = chardata[t].x;
383                 lasty = chardata[t].y;
384                 lastsize = chardata[t].size;
385             }
386
387             if(t==chardatapos)
388                     break;
389
390             int advance;
391             int nextt = t==chardatapos-1?t:t+1;
392             int rel = chardata[nextt].x-chardata[t].x;
393             if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
394                advance = rel;
395                lastx=chardata[nextt].x;
396             }
397             else {
398                advance = 0;
399                lastx=chardata[t].x;
400             }
401             charids[charstorepos] = chardata[t].charid;
402             charadvance[charstorepos] = advance;
403             charstorepos ++;
404         }
405     }
406     chardatapos = 0;
407 }
408
409 void putcharacter(struct swfoutput*obj, int fontid, int charid, 
410                     int x,int y, int size)
411 {
412     if(chardatapos == CHARDATAMAX)
413     {
414         endtext();
415         starttext(obj);
416     }
417     chardata[chardatapos].fontid = fontid;
418     chardata[chardatapos].charid = charid;
419     chardata[chardatapos].x = x;
420     chardata[chardatapos].y = y;
421     chardata[chardatapos].color = obj->fillrgb;
422     chardata[chardatapos].size = size;
423     chardatapos++;
424 }
425
426
427 /* process a character. */
428 void drawchar(struct swfoutput*obj, SWFFont*font, char*character, int charnr, swfmatrix*m)
429 {
430     int usefonts=1;
431     if(m->m12!=0 || m->m21!=0)
432         usefonts=0;
433     if(m->m11 != m->m22)
434         usefonts=0;
435
436     if(usefonts && ! drawonlyshapes)
437     {
438         int charid = font->getSWFCharID(character, charnr);
439         if(shapeid>=0)
440             endshape();
441         if(textid<0)
442             starttext(obj);
443         putcharacter(obj, font->swfid, charid,(int)(m->m13*20),(int)(m->m23*20),
444                 (int)(m->m11*20/2+0.5)); //where does the /2 come from?
445     }
446     else
447     {
448         T1_OUTLINE*outline = font->getOutline(character);
449         char* charname = character;
450
451         if(!outline) {
452          logf("<warning> Didn't find %s in current charset (%s)", 
453                  character,font->getName());
454          return;
455         }
456         
457         swfmatrix m2=*m;    
458         m2.m11/=100;
459         m2.m21/=100;
460         m2.m12/=100;
461         m2.m22/=100;
462
463         if(textid>=0)
464             endtext();
465         if(shapeid<0)
466             startshape(obj);
467
468         if(!lastwasfill)
469          swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
470         lastwasfill = 1;
471
472         int lf = fill;
473         fill = 1;
474         drawpath(tag, outline, &m2, 0);
475         fill = lf;
476     }
477 }
478
479 /* draw a curved polygon. */
480 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, 
481                             struct swfmatrix*m)
482 {
483     if(textid>=0)
484         endtext();
485     if(shapeid<0)
486         startshape(output);
487
488     if(lastwasfill && !fill)
489     {
490      swf_ShapeSetStyle(tag,shape,linestyleid,0x8000,0);
491      lastwasfill = 0;
492     }
493     if(!lastwasfill && fill)
494     {
495      swf_ShapeSetStyle(tag,shape,0x8000,fillstyleid,0);
496      lastwasfill = 1;
497     }
498
499     drawpath(tag, outline,m, 0); 
500 }
501
502 /* SWFFont: copy all t1 font outlines to a local 
503    array. */
504 SWFFont::SWFFont(char*name, int id, char*filename)
505 {
506     if(!T1_GetFontName(id))
507         T1_LoadFont(id);
508
509     this->name = strdup(T1_GetFontFileName(id));
510     this->fontid = strdup(name);
511     this->t1id = id;
512     
513     char**a= T1_GetAllCharNames(id);
514     int t, outlinepos=0;
515     char*map[256];
516
517     t=0;
518     while(a[t])
519         t++;
520     this->charnum = t;
521
522     if(!charnum) 
523         return;
524     logf("<verbose> Font %s(%d): Storing %d outlines.\n", name, id, charnum);
525
526     this->standardtablesize = 256;
527     if(this->charnum < this->standardtablesize)
528         this->standardtablesize = this->charnum;
529     this->standardtable = (char**)malloc(standardtablesize*sizeof(char*));
530
531     for(t = 0; t < this->standardtablesize; t++) {
532         char*name = T1_GetCharName(id,t);
533         if(!name)
534             name = "";
535         standardtable[t] = strdup(name);
536     }
537     
538     outline = (T1_OUTLINE**)malloc(charnum*sizeof(T1_OUTLINE*));
539     charname = (char**)malloc(charnum*sizeof(char*));
540     width = (int*)malloc(charnum*sizeof(int));
541     memset(width, 0, charnum*sizeof(int));
542     memset(charname, 0, charnum*sizeof(char*));
543     used = (char*)malloc(charnum*sizeof(char));
544     char2swfcharid = (U16*)malloc(charnum*2);
545     swfcharid2char = (U16*)malloc(charnum*2);
546     swfcharpos = 0;
547
548     memset(used,0,charnum*sizeof(char));
549
550     this->swfid = ++currentswfid;
551     
552     t=0;
553     while(*a)
554     {
555         map[t] = *a;
556         a++;
557         t++;
558         if(t==256 || !*a) {
559             int s;
560             for(s=t;s<256;s++)
561                 map[s] = ".notdef";
562
563             int ret = T1_ReencodeFont(id, map);
564             if(ret) {
565              T1_DeleteFont(id);
566              T1_LoadFont(id);
567              int ret = T1_ReencodeFont(id, map);
568              if(ret)
569                fprintf(stderr,"Can't reencode font: (%s) ret:%d\n",filename, ret);
570             }
571
572             // parsecharacters
573             for(s=0;s<t;s++)
574             {
575                 char* name = T1_GetCharName(id, s);
576                 if(!name) name = "";
577                 this->outline[outlinepos] = T1_CopyOutline(T1_GetCharOutline(id, s, 100.0, 0));
578                 this->width[outlinepos] = T1_GetCharWidth(id, s);
579                 this->charname[outlinepos] = strdup(name);
580                 outlinepos++;
581             }
582             t=0;
583         }
584     }
585 }
586
587 /* free all tables, write out definefont tags */
588 SWFFont::~SWFFont()
589 {
590     int t,usednum=0;
591     int*ptr; 
592
593     if(storeallcharacters)
594     {
595         int t;
596         for(t=0;t<this->charnum;t++) 
597         {
598             if(this->charname[t])
599               getSWFCharID(this->charname[t], -1);
600         }
601     }
602     
603     ptr = (int*)malloc(swfcharpos*sizeof(int));
604
605     for(t=0;t<charnum;t++)
606         if(used[t]) usednum++;
607
608     if(usednum && !drawonlyshapes)
609     {
610         logf("<verbose> Font %s has %d used characters",fontid, usednum);
611         TAG*ftag = swf_InsertTag(swf.firstTag,ST_DEFINEFONT);
612         swf_SetU16(ftag, this->swfid);
613         int initpos = swf_GetTagLen(ftag);
614         swfmatrix m;
615         m.m11 = m.m22 = 1;
616         m.m21 = m.m12 = 0;
617         m.m13 = CHARMIDX;
618         m.m23 = CHARMIDY;
619
620         for(t=0;t<swfcharpos;t++) 
621         {
622             ptr[t] = swf_GetTagLen(ftag);
623             swf_SetU16(ftag, 0x1234);
624         }
625         for(t=0;t<swfcharpos;t++)
626         {
627             *(U16*)&ftag->data[ptr[t]] = swf_GetTagLen(ftag)-initpos;
628             swflastx=0;
629             swflasty=0;
630             swf_SetU8(ftag,0x10); //0 fill bits, 0 linestyle bits
631             SHAPE s;
632             s.bits.fill = 1;
633             s.bits.line = 0;
634             swf_ShapeSetStyle(ftag,&s,0,1,0);
635             int lastfill = fill;
636             fill = 1;
637             storefont = 1;
638             drawpath(ftag, outline[swfcharid2char[t]],&m, 0);
639             storefont = 0;
640             fill = lastfill;
641             swf_ShapeSetEnd(ftag);
642         }
643         ftag = swf_InsertTag(ftag,ST_DEFINEFONTINFO);
644         swf_SetU16(ftag, this->swfid);
645         if(this->fontid) {
646             swf_SetU8(ftag, strlen(this->fontid));
647             swf_SetBlock(ftag, (U8*)this->fontid, strlen(this->fontid));
648         } else {
649             swf_SetU8(ftag, 0);
650         }
651         swf_SetU8(ftag, 0); //flags
652         for(t=0;t<swfcharpos;t++)
653         {
654             int s;
655             char * name = this->charname[this->swfcharid2char[t]];
656             for(s=0;s<256;s++) {
657                 if(standardEncodingNames[s] && 
658                         !strcmp(name,standardEncodingNames[s]))
659                     break;
660             }
661             swf_SetU8(ftag, (U8)s);
662         }
663     }
664
665     free(ptr);
666     free(outline);
667     for(t=0;t<charnum;t++)
668         free(charname[t]);
669     for(t=0;t<standardtablesize;t++)
670         if(standardtable[t]) {
671             free(standardtable[t]);
672         }
673     free(standardtable);
674     free(charname);
675     free(width);
676     free(used);
677     free(swfcharid2char);
678     free(char2swfcharid);
679 }
680
681 T1_OUTLINE*SWFFont::getOutline(char*name)
682 {
683     int t;
684     for(t=0;t<this->charnum;t++) {
685         if(!strcmp(this->charname[t],name)) {
686             if(!used[t])
687             {
688                 swfcharid2char[swfcharpos] = t;
689                 char2swfcharid[t] = swfcharpos;
690                 swfcharpos++;
691                 used[t] = 1;
692             }
693             return outline[t];
694         }
695     }
696     return 0;
697 }
698
699 int SWFFont::getWidth(char*name)
700 {
701     int t;
702     for(t=0;t<this->charnum;t++) {
703         if(!strcmp(this->charname[t],name)) {
704             return this->width[t];
705         }
706     }
707     return 0;
708 }
709
710 int SWFFont::getSWFCharID(char*name, int charnr)
711 {
712     int t;
713     for(t=0;t<this->charnum;t++) {
714         if(!strcmp(this->charname[t],name)) {
715             if(!used[t])
716             {
717                 swfcharid2char[swfcharpos] = t;
718                 char2swfcharid[t] = swfcharpos++;
719                 used[t] = 1;
720             }
721             return char2swfcharid[t];
722         }
723     }
724     if(this->standardtable && charnr>=0 && charnr < this->standardtablesize) {
725         return getSWFCharID(this->standardtable[charnr], -1);
726     }
727     logf("<warning> Didn't find character '%s' in font '%s'", name, this->name);
728     return 0;
729 }
730
731 char*SWFFont::getName()
732 {
733     return this->name;
734 }
735
736 struct fontlist_t 
737 {
738     SWFFont * font;
739     fontlist_t*next;
740 } *fontlist = 0;
741
742 /* set's the t1 font index of the font to use for swfoutput_drawchar(). */
743 void swfoutput_setfont(struct swfoutput*obj, char*fontid, int t1id, char*filename)
744 {
745     fontlist_t*last=0,*iterator;
746     if(obj->font && !strcmp(obj->font->fontid,fontid))
747         return;
748
749     iterator = fontlist;
750     while(iterator) {
751         if(!strcmp(iterator->font->fontid,fontid))
752             break;
753         last = iterator;
754         iterator = iterator->next;
755     }
756     if(iterator) 
757     {
758         obj->font = iterator->font;
759         return ;
760     }
761
762     if(t1id<0) {
763         logf("<error> internal error: t1id:%d, fontid:%s\n", t1id,fontid);
764     }
765     
766     SWFFont*font = new SWFFont(fontid, t1id, filename);
767     iterator = new fontlist_t;
768     iterator->font = font;
769     iterator->next = 0;
770
771     if(last) 
772         last->next = iterator;
773     else 
774         fontlist = iterator;
775     obj->font = font;
776 }
777
778 int swfoutput_queryfont(struct swfoutput*obj, char*fontid)
779 {
780     fontlist_t *iterator = fontlist;
781     while(iterator) {
782         if(!strcmp(iterator->font->fontid,fontid))
783             return 1;
784         iterator = iterator->next;
785     }
786     return 0;
787 }
788
789 /* set's the matrix which is to be applied to characters drawn by
790    swfoutput_drawchar() */
791 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
792                                                   double m21,double m22)
793 {
794     if(obj->fontm11 == m11 &&
795        obj->fontm12 == m12 &&
796        obj->fontm21 == m21 &&
797        obj->fontm22 == m22)
798         return;
799 //    if(textid>=0)
800 //      endtext();
801     obj->fontm11 = m11;
802     obj->fontm12 = m12;
803     obj->fontm21 = m21;
804     obj->fontm22 = m22;
805 }
806
807 /* draws a character at x,y. */
808 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char*character, int charnr) 
809 {
810     swfmatrix m;
811     m.m11 = obj->fontm11;
812     m.m12 = obj->fontm12;
813     m.m21 = obj->fontm21;
814     m.m22 = obj->fontm22;
815     m.m13 = x;
816     m.m23 = y;
817     drawchar(obj, obj->font, character, charnr, &m);
818 }
819
820 /* initialize the swf writer */
821 void swfoutput_init(struct swfoutput* obj, char*_filename, int _sizex, int _sizey) 
822 {
823   GLYPH *glyph;
824   RGBA rgb;
825   SRECT r;
826   memset(obj, 0, sizeof(struct swfoutput));
827   filename = _filename;
828   sizex = _sizex;
829   sizey = _sizey;
830
831   logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
832
833   obj->font = 0;
834   
835   memset(&swf,0x00,sizeof(SWF));
836
837   swf.fileVersion    = 4;
838   swf.frameRate      = 0x0040; // 1 frame per 4 seconds
839   swf.movieSize.xmax = 20*sizex;
840   swf.movieSize.ymax = 20*sizey;
841   
842   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
843   tag = swf.firstTag;
844   rgb.r = 0xff;
845   rgb.g = 0xff;
846   rgb.b = 0xff;
847   swf_SetRGB(tag,&rgb);
848   if(flag_protected)  // good practice! /r
849     tag = swf_InsertTag(tag, ST_PROTECT);
850   depth = 1;
851   startdepth = depth;
852 }
853
854 void swfoutput_setprotected() //write PROTECT tag
855 {
856   flag_protected = 1;
857 }
858
859 void startshape(struct swfoutput*obj)
860 {
861   RGBA rgb;
862   SRECT r;
863
864   if(textid>=0)
865       endtext();
866
867   tag = swf_InsertTag(tag,ST_DEFINESHAPE);
868
869   swf_ShapeNew(&shape);
870   linestyleid = swf_ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
871   rgb.r = obj->fillrgb.r;
872   rgb.g = obj->fillrgb.g;
873   rgb.b = obj->fillrgb.b;
874   fillstyleid = swf_ShapeAddSolidFillStyle(shape,&obj->fillrgb);
875
876   shapeid = ++currentswfid;
877   swf_SetU16(tag,shapeid);  // ID
878
879   r.xmin = 0;
880   r.ymin = 0;
881   r.xmax = 20*sizex;
882   r.ymax = 20*sizey;
883   
884   swf_SetRect(tag,&r);
885
886   swf_SetShapeStyles(tag,shape);
887   swf_ShapeCountBits(shape,NULL,NULL);
888   swf_SetShapeBits(tag,shape);
889
890   swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,linestyleid,0,0);
891   swflastx=swflasty=0;
892   lastwasfill = 0;
893 }
894
895 void starttext(struct swfoutput*obj)
896 {
897   SRECT r;
898   MATRIX m;
899   if(shapeid>=0)
900       endshape();
901   tag = swf_InsertTag(tag,ST_DEFINETEXT);
902   textid = ++currentswfid;
903   swf_SetU16(tag, textid);
904
905   r.xmin = 0;
906   r.ymin = 0;
907   r.xmax = 20*sizex;
908   r.ymax = 20*sizey;
909   
910   swf_SetRect(tag,&r);
911
912   m.sx = 65536;
913   m.sy = 65536;
914   m.r0 = 0;
915   m.r1 = 0;
916   m.tx = 0;
917   m.ty = 0;
918  
919   swf_SetMatrix(tag,&m);
920   swflastx=swflasty=0;
921 }
922
923 void endshape()
924 {
925     if(shapeid<0) 
926         return;
927     swf_ShapeSetEnd(tag);
928     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
929     swf_ObjectPlace(tag,shapeid,/*depth*/depth++,NULL,NULL,NULL);
930     shapeid = -1;
931 }
932
933 void endtext()
934 {
935     if(textid<0)
936         return;
937     putcharacters(tag);
938     swf_SetU8(tag,0);
939     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
940     swf_ObjectPlace(tag,textid,/*depth*/depth++,NULL,NULL,NULL);
941     textid = -1;
942 }
943
944 void endpage(struct swfoutput*obj)
945 {
946     if(shapeid>=0)
947       endshape();
948     if(textid>=0)
949       endtext();
950     while(clippos)
951         swfoutput_endclip(obj);
952     tag = swf_InsertTag(tag,ST_SHOWFRAME);
953 }
954
955 void swfoutput_newpage(struct swfoutput*obj)
956 {
957     endpage(obj);
958
959     for(depth--;depth>=startdepth;depth--) {
960         tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
961         swf_SetU16(tag,depth);
962     }
963
964     depth = 1;
965     startdepth = depth;
966 }
967
968 /* "destroy" like in (oo-terminology) "destructor". Perform cleaning
969    up, complete the swf, and write it out. */
970 void swfoutput_destroy(struct swfoutput* obj) 
971 {
972     endpage(obj);
973     fontlist_t *tmp,*iterator = fontlist;
974     while(iterator) {
975         delete iterator->font;
976         iterator->font = 0;
977         tmp = iterator;
978         iterator = iterator->next;
979         delete tmp;
980     }
981
982     T1_CloseLib();
983     if(!filename) 
984         return;
985     if(filename)
986      fi = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0777);
987     else
988      fi = 1; // stdout
989     
990     if(fi<=0) {
991      logf("<fatal> Could not create \"%s\". ", filename);
992      exit(1);
993     }
994  
995     tag = swf_InsertTag(tag,ST_END);
996
997     if FAILED(swf_WriteSWF(fi,&swf)) 
998      logf("<error> WriteSWF() failed.\n");
999     if(filename)
1000      close(fi);
1001     logf("<notice> SWF written\n");
1002 }
1003
1004 void swfoutput_setdrawmode(swfoutput* obj, int mode)
1005 {
1006     drawmode = mode;
1007     if(mode == DRAWMODE_FILL)
1008      fill = 1;
1009     else if(mode == DRAWMODE_EOFILL)
1010      fill = 1;
1011     else if(mode == DRAWMODE_STROKE)
1012      fill = 0;
1013     else if(mode == DRAWMODE_CLIP)
1014      fill = 1;
1015     else if(mode == DRAWMODE_EOCLIP)
1016      fill = 1;
1017 }
1018
1019 void swfoutput_setfillcolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1020 {
1021     if(obj->fillrgb.r == r &&
1022        obj->fillrgb.g == g &&
1023        obj->fillrgb.b == b &&
1024        obj->fillrgb.a == a) return;
1025
1026     if(shapeid>=0)
1027      endshape();
1028     obj->fillrgb.r = r;
1029     obj->fillrgb.g = g;
1030     obj->fillrgb.b = b;
1031     obj->fillrgb.a = a;
1032 }
1033
1034 void swfoutput_setstrokecolor(swfoutput* obj, u8 r, u8 g, u8 b, u8 a)
1035 {
1036     if(obj->strokergb.r == r &&
1037        obj->strokergb.g == g &&
1038        obj->strokergb.b == b &&
1039        obj->strokergb.a == a) return;
1040
1041     if(shapeid>=0)
1042      endshape();
1043     obj->strokergb.r = r;
1044     obj->strokergb.g = g;
1045     obj->strokergb.b = b;
1046     obj->strokergb.a = a;
1047 }
1048
1049 void swfoutput_setlinewidth(struct swfoutput*obj, double linewidth)
1050 {
1051     if(obj->linewidth == (u16)(linewidth*20))
1052         return;
1053
1054     if(shapeid>=0)
1055      endshape();
1056     obj->linewidth = (u16)(linewidth*20);
1057 }
1058
1059
1060 void swfoutput_startclip(swfoutput*obj, T1_OUTLINE*outline, struct swfmatrix*m)
1061 {
1062     if(textid>=0)
1063      endtext();
1064     if(shapeid>=0)
1065      endshape();
1066
1067     if(clippos >= 127)
1068     {
1069         logf("<warning> Too many clip levels.");
1070         clippos --;
1071     } 
1072     
1073     startshape(obj);
1074     int olddrawmode = drawmode;
1075     swfoutput_setdrawmode(obj, DRAWMODE_CLIP);
1076     swfoutput_drawpath(obj, outline, m);
1077     swf_ShapeSetEnd(tag);
1078     swfoutput_setdrawmode(obj, olddrawmode);
1079
1080     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1081     cliptags[clippos] = tag;
1082     clipshapes[clippos] = shapeid;
1083     clipdepths[clippos] = depth++;
1084     clippos++;
1085     shapeid = -1;
1086 }
1087
1088 void swfoutput_endclip(swfoutput*obj)
1089 {
1090     if(textid>=0)
1091      endtext();
1092     if(shapeid>=0)
1093      endshape();
1094
1095     if(!clippos) {
1096         logf("<error> Invalid end of clipping region");
1097         return;
1098     }
1099     clippos--;
1100     swf_ObjectPlaceClip(cliptags[clippos],clipshapes[clippos],clipdepths[clippos],NULL,NULL,NULL,depth++);
1101 }
1102
1103 void drawlink(struct swfoutput*obj, ActionTAG*, swfcoord*points);
1104
1105 void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
1106 {
1107     ActionTAG* actions;
1108     
1109     if(shapeid>=0)
1110      endshape();
1111     if(textid>=0)
1112      endtext();
1113     
1114     actions = swf_ActionStart();
1115     if(opennewwindow)
1116       action_GetUrl(url, "_parent");
1117     else
1118       action_GetUrl(url, "_this");
1119       action_End();
1120     swf_ActionEnd();
1121     
1122     drawlink(obj, actions, points);
1123 }
1124 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1125 {
1126     ActionTAG* actions;
1127
1128     if(shapeid>=0)
1129      endshape();
1130     if(textid>=0)
1131      endtext();
1132    
1133     actions = swf_ActionStart();
1134       action_GotoFrame(page);
1135       action_End();
1136     swf_ActionEnd();
1137
1138     drawlink(obj, actions, points);
1139 }
1140
1141 void drawlink(struct swfoutput*obj, ActionTAG*actions, swfcoord*points)
1142 {
1143     RGBA rgb;
1144     SRECT r;
1145     int lsid=0;
1146     int fsid;
1147     struct plotxy p1,p2,p3,p4;
1148     int myshapeid;
1149     int myshapeid2;
1150     double xmin,ymin;
1151     double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1152     int t;
1153     int buttonid = ++currentswfid;
1154     for(t=1;t<4;t++)
1155     {
1156         if(points[t].x>xmax) xmax=points[t].x;
1157         if(points[t].y>ymax) ymax=points[t].y;
1158         if(points[t].x<xmin) xmin=points[t].x;
1159         if(points[t].y<ymin) ymin=points[t].y;
1160     }
1161     p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y; 
1162     p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1163     
1164     /* shape */
1165     myshapeid = ++currentswfid;
1166     tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1167     swf_ShapeNew(&shape);
1168     rgb.r = rgb.b = rgb.a = rgb.g = 0; 
1169     fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1170     swf_SetU16(tag, myshapeid);
1171     r.xmin = (int)(xmin*20);
1172     r.ymin = (int)(ymin*20);
1173     r.xmax = (int)(xmax*20);
1174     r.ymax = (int)(ymax*20);
1175     swf_SetRect(tag,&r);
1176     swf_SetShapeStyles(tag,shape);
1177     swf_ShapeCountBits(shape,NULL,NULL);
1178     swf_SetShapeBits(tag,shape);
1179     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1180     swflastx = swflasty = 0;
1181     moveto(tag, p1);
1182     lineto(tag, p2);
1183     lineto(tag, p3);
1184     lineto(tag, p4);
1185     lineto(tag, p1);
1186     swf_ShapeSetEnd(tag);
1187
1188     /* shape2 */
1189     myshapeid2 = ++currentswfid;
1190     tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1191     swf_ShapeNew(&shape);
1192     rgb.r = rgb.b = rgb.a = rgb.g = 255;
1193     rgb.a = 40;
1194     fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1195     swf_SetU16(tag, myshapeid2);
1196     r.xmin = (int)(xmin*20);
1197     r.ymin = (int)(ymin*20);
1198     r.xmax = (int)(xmax*20);
1199     r.ymax = (int)(ymax*20);
1200     swf_SetRect(tag,&r);
1201     swf_SetShapeStyles(tag,shape);
1202     swf_ShapeCountBits(shape,NULL,NULL);
1203     swf_SetShapeBits(tag,shape);
1204     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1205     swflastx = swflasty = 0;
1206     moveto(tag, p1);
1207     lineto(tag, p2);
1208     lineto(tag, p3);
1209     lineto(tag, p4);
1210     lineto(tag, p1);
1211     swf_ShapeSetEnd(tag);
1212
1213     tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
1214     swf_SetU16(tag,buttonid); //id
1215     swf_ButtonSetFlags(tag, 0); //menu=no
1216     swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1217     swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1218     swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1219     swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1220     swf_SetU8(tag,0);
1221     swf_ActionSet(tag,actions);
1222     swf_SetU8(tag,0);
1223     
1224     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1225     swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
1226 }
1227
1228 void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey, 
1229         double x1,double y1,
1230         double x2,double y2,
1231         double x3,double y3,
1232         double x4,double y4)
1233 {
1234     RGBA rgb;
1235     SRECT r;
1236     int lsid=0;
1237     int fsid;
1238     struct plotxy p1,p2,p3,p4;
1239     int myshapeid;
1240     double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1241     if(x2>xmax) xmax=x2;
1242     if(y2>ymax) ymax=y2;
1243     if(x2<xmin) xmin=x2;
1244     if(y2<ymin) ymin=y2;
1245     if(x3>xmax) xmax=x3;
1246     if(y3>ymax) ymax=y3;
1247     if(x3<xmin) xmin=x3;
1248     if(y3<ymin) ymin=y3;
1249     if(x4>xmax) xmax=x4;
1250     if(y4>ymax) ymax=y4;
1251     if(x4<xmin) xmin=x4;
1252     if(y4<ymin) ymin=y4;
1253     p1.x=x1;
1254     p1.y=y1;
1255     p2.x=x2;
1256     p2.y=y2;
1257     p3.x=x3;
1258     p3.y=y3;
1259     p4.x=x4;
1260     p4.y=y4;
1261     
1262     MATRIX m;
1263     m.sx = (int)(65536*20*(x4-x1))/sizex;
1264     m.r1 = -(int)(65536*20*(y4-y1))/sizex;
1265     m.r0 = (int)(65536*20*(x1-x2))/sizey;
1266     m.sy = -(int)(65536*20*(y1-y2))/sizey;
1267
1268     m.tx = (int)(x1*20);
1269     m.ty = (int)(y1*20);
1270   
1271     /* shape */
1272     myshapeid = ++currentswfid;
1273     tag = swf_InsertTag(tag,ST_DEFINESHAPE);
1274     swf_ShapeNew(&shape);
1275     //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1276     //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1277     fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,0);
1278     swf_SetU16(tag, myshapeid);
1279     r.xmin = (int)(xmin*20);
1280     r.ymin = (int)(ymin*20);
1281     r.xmax = (int)(xmax*20);
1282     r.ymax = (int)(ymax*20);
1283     swf_SetRect(tag,&r);
1284     swf_SetShapeStyles(tag,shape);
1285     swf_ShapeCountBits(shape,NULL,NULL);
1286     swf_SetShapeBits(tag,shape);
1287     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1288     swflastx = swflasty = 0;
1289     moveto(tag, p1);
1290     lineto(tag, p2);
1291     lineto(tag, p3);
1292     lineto(tag, p4);
1293     lineto(tag, p1);
1294     /*
1295     ShapeMoveTo  (tag, shape, (int)(x1*20),(int)(y1*20));
1296     ShapeSetLine (tag, shape, (int)(x1*20);
1297     ShapeSetLine (tag, shape, x*20,0);
1298     ShapeSetLine (tag, shape, 0,-y*20);
1299     ShapeSetLine (tag, shape, -x*20,0);*/
1300     swf_ShapeSetEnd(tag);
1301
1302     /* instance */
1303     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1304     swf_ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
1305 }
1306
1307 int swfoutput_drawimagejpeg(struct swfoutput*obj, char*filename, int sizex,int sizey, 
1308         double x1,double y1,
1309         double x2,double y2,
1310         double x3,double y3,
1311         double x4,double y4)
1312 {
1313     TAG*oldtag;
1314     if(shapeid>=0)
1315      endshape();
1316     if(textid>=0)
1317      endtext();
1318
1319     int bitid = ++currentswfid;
1320     oldtag = tag;
1321     tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1322     swf_SetU16(tag, bitid);
1323     if(swf_SetJPEGBits(tag, filename, jpegquality)<0) {
1324         swf_DeleteTag(tag);
1325         tag = oldtag;
1326         return -1;
1327     }
1328
1329     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1330     return bitid;
1331 }
1332
1333 int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey, 
1334         double x1,double y1,
1335         double x2,double y2,
1336         double x3,double y3,
1337         double x4,double y4)
1338 {
1339     TAG*oldtag;
1340     if(shapeid>=0)
1341      endshape();
1342     if(textid>=0)
1343      endtext();
1344
1345     int bitid = ++currentswfid;
1346     oldtag = tag;
1347     tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS);
1348     swf_SetU16(tag, bitid);
1349     if(swf_SetLosslessBits(tag,sizex,sizey,mem, BMF_32BIT)<0) {
1350         swf_DeleteTag(tag);
1351         tag = oldtag;
1352         return -1;
1353     }
1354     
1355     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1356     return bitid;
1357 }
1358
1359 int swfoutput_drawimagelossless256(struct swfoutput*obj, U8*mem, RGBA*pal, int sizex,int sizey, 
1360         double x1,double y1,
1361         double x2,double y2,
1362         double x3,double y3,
1363         double x4,double y4)
1364 {
1365     TAG*oldtag;
1366     if(shapeid>=0)
1367      endshape();
1368     if(textid>=0)
1369      endtext();
1370
1371     int bitid = ++currentswfid;
1372     oldtag = tag;
1373     tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS2);
1374     swf_SetU16(tag, bitid);
1375     if(swf_SetLosslessBitsIndexed(tag,sizex,sizey,mem, pal, 256)<0) {
1376         swf_DeleteTag(tag);
1377         tag = oldtag;
1378         return -1;
1379     }
1380   
1381     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1382     return bitid;
1383 }
1384
1385 void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey, 
1386         double x1,double y1,
1387         double x2,double y2,
1388         double x3,double y3,
1389         double x4,double y4)
1390 {
1391     if(id<0) return;
1392     if(shapeid>=0)
1393      endshape();
1394     if(textid>=0)
1395      endtext();
1396
1397     drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1398 }
1399