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