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