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