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