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