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