Makefile fixes
[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     if(!strncmp("http://pdf2swf:", url, 15)) {
1170      char*tmp = strdup(url);
1171      int l = strlen(tmp);
1172      if(tmp[l-1] == '/')
1173         tmp[l-1] = 0;
1174      swfoutput_namedlink(obj, tmp+15, points);
1175      free(tmp);
1176      return;
1177     }
1178     
1179     if(shapeid>=0)
1180      endshape();
1181     if(textid>=0)
1182      endtext();
1183     
1184     if(opennewwindow)
1185       actions = action_GetUrl(0, url, "_parent");
1186     else
1187       actions = action_GetUrl(0, url, "_this");
1188     actions = action_End(actions);
1189     
1190     drawlink(obj, actions, 0, points,0);
1191 }
1192 void swfoutput_linktopage(struct swfoutput*obj, int page, swfcoord*points)
1193 {
1194     ActionTAG* actions;
1195
1196     if(shapeid>=0)
1197      endshape();
1198     if(textid>=0)
1199      endtext();
1200    
1201       actions = action_GotoFrame(0, page);
1202       actions = action_End(actions);
1203
1204     drawlink(obj, actions, 0, points,0);
1205 }
1206
1207 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1208    of the viewer objects, like subtitles, index elements etc.
1209 */
1210 void swfoutput_namedlink(struct swfoutput*obj, char*name, swfcoord*points)
1211 {
1212     ActionTAG *actions1,*actions2;
1213     char*tmp = strdup(name);
1214     char mouseover = 1;
1215
1216     if(shapeid>=0)
1217      endshape();
1218     if(textid>=0)
1219      endtext();
1220
1221     if(!strncmp(tmp, "call:", 5))
1222     {
1223         char*x = strchr(&tmp[5], ':');
1224         if(!x) {
1225             actions1 = action_PushInt(0, 0); //number of parameters (0)
1226             actions1 = action_PushString(actions1, &tmp[5]); //function name
1227             actions1 = action_CallFunction(actions1);
1228         } else {
1229             *x = 0;
1230             actions1 = action_PushString(0, x+1); //parameter
1231             actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1232             actions1 = action_PushString(actions1, &tmp[5]); //function name
1233             actions1 = action_CallFunction(actions1);
1234         }
1235         actions2 = action_End(0);
1236         mouseover = 0;
1237     }
1238     else
1239     {
1240         actions1 = action_PushString(0, "/:subtitle");
1241         actions1 = action_PushString(actions1, name);
1242         actions1 = action_SetVariable(actions1);
1243         actions1 = action_End(actions1);
1244
1245         actions2 = action_PushString(0, "/:subtitle");
1246         actions2 = action_PushString(actions2, "");
1247         actions2 = action_SetVariable(actions2);
1248         actions2 = action_End(actions2);
1249     }
1250
1251     drawlink(obj, actions1, actions2, points,mouseover);
1252
1253     swf_ActionFree(actions1);
1254     swf_ActionFree(actions2);
1255     free(tmp);
1256 }
1257
1258 static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions2, swfcoord*points, char mouseover)
1259 {
1260     RGBA rgb;
1261     SRECT r;
1262     int lsid=0;
1263     int fsid;
1264     struct plotxy p1,p2,p3,p4;
1265     int myshapeid;
1266     int myshapeid2;
1267     double xmin,ymin;
1268     double xmax=xmin=points[0].x,ymax=ymin=points[0].y;
1269     double posx = 0;
1270     double posy = 0;
1271     int t;
1272     int buttonid = ++currentswfid;
1273     for(t=1;t<4;t++)
1274     {
1275         if(points[t].x>xmax) xmax=points[t].x;
1276         if(points[t].y>ymax) ymax=points[t].y;
1277         if(points[t].x<xmin) xmin=points[t].x;
1278         if(points[t].y<ymin) ymin=points[t].y;
1279     }
1280    
1281     p1.x=points[0].x; p1.y=points[0].y; p2.x=points[1].x; p2.y=points[1].y; 
1282     p3.x=points[2].x; p3.y=points[2].y; p4.x=points[3].x; p4.y=points[3].y;
1283    
1284     /* the following code subtracts the upper left edge from all coordinates,
1285        and set's posx,posy so that ST_PLACEOBJECT is used with a matrix.
1286        Necessary for preprocessing with swfcombine. */
1287     posx = xmin; posy = ymin;
1288     p1.x-=posx;p2.x-=posx;p3.x-=posx;p4.x-=posx;
1289     p1.y-=posy;p2.y-=posy;p3.y-=posy;p4.y-=posy;
1290     xmin -= posx; ymin -= posy;
1291     xmax -= posx; ymax -= posy;
1292     
1293     /* shape */
1294     myshapeid = ++currentswfid;
1295     tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1296     swf_ShapeNew(&shape);
1297     rgb.r = rgb.b = rgb.a = rgb.g = 0; 
1298     fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1299     swf_SetU16(tag, myshapeid);
1300     r.xmin = (int)(xmin*20);
1301     r.ymin = (int)(ymin*20);
1302     r.xmax = (int)(xmax*20);
1303     r.ymax = (int)(ymax*20);
1304     swf_SetRect(tag,&r);
1305     swf_SetShapeStyles(tag,shape);
1306     swf_ShapeCountBits(shape,NULL,NULL);
1307     swf_SetShapeBits(tag,shape);
1308     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1309     swflastx = swflasty = 0;
1310     moveto(tag, p1);
1311     lineto(tag, p2);
1312     lineto(tag, p3);
1313     lineto(tag, p4);
1314     lineto(tag, p1);
1315     swf_ShapeSetEnd(tag);
1316
1317     /* shape2 */
1318     myshapeid2 = ++currentswfid;
1319     tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
1320     swf_ShapeNew(&shape);
1321     rgb.r = rgb.b = rgb.a = rgb.g = 255;
1322     rgb.a = 40;
1323     fsid = swf_ShapeAddSolidFillStyle(shape,&rgb);
1324     swf_SetU16(tag, myshapeid2);
1325     r.xmin = (int)(xmin*20);
1326     r.ymin = (int)(ymin*20);
1327     r.xmax = (int)(xmax*20);
1328     r.ymax = (int)(ymax*20);
1329     swf_SetRect(tag,&r);
1330     swf_SetShapeStyles(tag,shape);
1331     swf_ShapeCountBits(shape,NULL,NULL);
1332     swf_SetShapeBits(tag,shape);
1333     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,0,fsid,0);
1334     swflastx = swflasty = 0;
1335     moveto(tag, p1);
1336     lineto(tag, p2);
1337     lineto(tag, p3);
1338     lineto(tag, p4);
1339     lineto(tag, p1);
1340     swf_ShapeSetEnd(tag);
1341
1342     if(!mouseover)
1343     {
1344         tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
1345         swf_SetU16(tag,buttonid); //id
1346         swf_ButtonSetFlags(tag, 0); //menu=no
1347         swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1348         swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1349         swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1350         swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1351         swf_SetU8(tag,0);
1352         swf_ActionSet(tag,actions1);
1353         swf_SetU8(tag,0);
1354     }
1355     else
1356     {
1357         tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
1358         swf_SetU16(tag,buttonid); //id
1359         swf_ButtonSetFlags(tag, 0); //menu=no
1360         swf_ButtonSetRecord(tag,0x01,myshapeid,depth,0,0);
1361         swf_ButtonSetRecord(tag,0x02,myshapeid2,depth,0,0);
1362         swf_ButtonSetRecord(tag,0x04,myshapeid2,depth,0,0);
1363         swf_ButtonSetRecord(tag,0x08,myshapeid,depth,0,0);
1364         swf_SetU8(tag,0); // end of button records
1365         swf_ButtonSetCondition(tag, BC_IDLE_OVERUP);
1366         swf_ActionSet(tag,actions1);
1367         if(actions2) {
1368             swf_ButtonSetCondition(tag, BC_OVERUP_IDLE);
1369             swf_ActionSet(tag,actions2);
1370             swf_SetU8(tag,0);
1371             swf_ButtonPostProcess(tag, 2);
1372         } else {
1373             swf_SetU8(tag,0);
1374             swf_ButtonPostProcess(tag, 1);
1375         }
1376     }
1377     
1378     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1379
1380     if(posx!=0 || posy!=0) {
1381         MATRIX m;
1382         swf_GetMatrix(0,&m);
1383         m.tx = (int)(posx*20);
1384         m.ty = (int)(posy*20);
1385         swf_ObjectPlace(tag, buttonid, depth++,&m,0,0);
1386     }
1387     else {
1388         swf_ObjectPlace(tag, buttonid, depth++,0,0,0);
1389     }
1390 }
1391
1392 static void drawimage(struct swfoutput*obj, int bitid, int sizex,int sizey, 
1393         double x1,double y1,
1394         double x2,double y2,
1395         double x3,double y3,
1396         double x4,double y4)
1397 {
1398     RGBA rgb;
1399     SRECT r;
1400     int lsid=0;
1401     int fsid;
1402     struct plotxy p1,p2,p3,p4;
1403     int myshapeid;
1404     double xmax=x1,ymax=y1,xmin=x1,ymin=y1;
1405     if(x2>xmax) xmax=x2;
1406     if(y2>ymax) ymax=y2;
1407     if(x2<xmin) xmin=x2;
1408     if(y2<ymin) ymin=y2;
1409     if(x3>xmax) xmax=x3;
1410     if(y3>ymax) ymax=y3;
1411     if(x3<xmin) xmin=x3;
1412     if(y3<ymin) ymin=y3;
1413     if(x4>xmax) xmax=x4;
1414     if(y4>ymax) ymax=y4;
1415     if(x4<xmin) xmin=x4;
1416     if(y4<ymin) ymin=y4;
1417     p1.x=x1; p1.y=y1;
1418     p2.x=x2; p2.y=y2;
1419     p3.x=x3; p3.y=y3;
1420     p4.x=x4; p4.y=y4;
1421
1422     {p1.x = (int)(p1.x*20)/20.0;
1423      p1.y = (int)(p1.y*20)/20.0;
1424      p2.x = (int)(p2.x*20)/20.0;
1425      p2.y = (int)(p2.y*20)/20.0;
1426      p3.x = (int)(p3.x*20)/20.0;
1427      p3.y = (int)(p3.y*20)/20.0;
1428      p4.x = (int)(p4.x*20)/20.0;
1429      p4.y = (int)(p4.y*20)/20.0;}
1430     
1431     MATRIX m;
1432     m.sx = (int)(65536*20*(p4.x-p1.x)/sizex);
1433     m.r1 = -(int)(65536*20*(p4.y-p1.y)/sizex);
1434     m.r0 = (int)(65536*20*(p1.x-p2.x)/sizey);
1435     m.sy = -(int)(65536*20*(p1.y-p2.y)/sizey);
1436
1437     m.tx = (int)(p1.x*20);
1438     m.ty = (int)(p1.y*20);
1439   
1440     /* shape */
1441     myshapeid = ++currentswfid;
1442     tag = swf_InsertTag(tag,ST_DEFINESHAPE);
1443     swf_ShapeNew(&shape);
1444     //lsid = ShapeAddLineStyle(shape,obj->linewidth,&obj->strokergb);
1445     //fsid = ShapeAddSolidFillStyle(shape,&obj->fillrgb);
1446     fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1447     swf_SetU16(tag, myshapeid);
1448     r.xmin = (int)(xmin*20);
1449     r.ymin = (int)(ymin*20);
1450     r.xmax = (int)(xmax*20);
1451     r.ymax = (int)(ymax*20);
1452     swf_SetRect(tag,&r);
1453     swf_SetShapeStyles(tag,shape);
1454     swf_ShapeCountBits(shape,NULL,NULL);
1455     swf_SetShapeBits(tag,shape);
1456     swf_ShapeSetAll(tag,shape,/*x*/0,/*y*/0,lsid,fsid,0);
1457     swflastx = swflasty = 0;
1458     moveto(tag, p1);
1459     lineto(tag, p2);
1460     lineto(tag, p3);
1461     lineto(tag, p4);
1462     lineto(tag, p1);
1463     /*
1464     ShapeMoveTo  (tag, shape, (int)(x1*20),(int)(y1*20));
1465     ShapeSetLine (tag, shape, (int)(x1*20);
1466     ShapeSetLine (tag, shape, x*20,0);
1467     ShapeSetLine (tag, shape, 0,-y*20);
1468     ShapeSetLine (tag, shape, -x*20,0);*/
1469     swf_ShapeSetEnd(tag);
1470
1471     /* instance */
1472     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1473     swf_ObjectPlace(tag,myshapeid,/*depth*/depth++,NULL,NULL,NULL);
1474 }
1475
1476 int swfoutput_drawimagejpeg_old(struct swfoutput*obj, char*filename, int sizex,int sizey, 
1477         double x1,double y1,
1478         double x2,double y2,
1479         double x3,double y3,
1480         double x4,double y4)
1481 {
1482     TAG*oldtag;
1483     if(shapeid>=0)
1484      endshape();
1485     if(textid>=0)
1486      endtext();
1487
1488     int bitid = ++currentswfid;
1489     oldtag = tag;
1490     tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1491     swf_SetU16(tag, bitid);
1492     if(swf_SetJPEGBits(tag, filename, jpegquality)<0) {
1493         swf_DeleteTag(tag);
1494         tag = oldtag;
1495         return -1;
1496     }
1497
1498     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1499     return bitid;
1500 }
1501
1502 int swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey, 
1503         double x1,double y1,
1504         double x2,double y2,
1505         double x3,double y3,
1506         double x4,double y4)
1507 {
1508     TAG*oldtag;
1509     JPEGBITS*jpeg;
1510
1511     if(shapeid>=0)
1512      endshape();
1513     if(textid>=0)
1514      endtext();
1515
1516     int bitid = ++currentswfid;
1517     oldtag = tag;
1518     tag = swf_InsertTag(tag,ST_DEFINEBITSJPEG2);
1519     swf_SetU16(tag, bitid);
1520     swf_SetJPEGBits2(tag,sizex,sizey,mem,jpegquality);
1521     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1522     return bitid;
1523 }
1524
1525 int swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey, 
1526         double x1,double y1,
1527         double x2,double y2,
1528         double x3,double y3,
1529         double x4,double y4)
1530 {
1531     TAG*oldtag;
1532     if(shapeid>=0)
1533      endshape();
1534     if(textid>=0)
1535      endtext();
1536
1537     int bitid = ++currentswfid;
1538     oldtag = tag;
1539     tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS);
1540     swf_SetU16(tag, bitid);
1541     if(swf_SetLosslessBits(tag,sizex,sizey,mem, BMF_32BIT)<0) {
1542         swf_DeleteTag(tag);
1543         tag = oldtag;
1544         return -1;
1545     }
1546     
1547     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1548     return bitid;
1549 }
1550
1551 int swfoutput_drawimagelosslessN(struct swfoutput*obj, U8*mem, RGBA*pal, int sizex,int sizey, 
1552         double x1,double y1,
1553         double x2,double y2,
1554         double x3,double y3,
1555         double x4,double y4, int n)
1556 {
1557     TAG*oldtag;
1558     U8*mem2 = 0;
1559     if(shapeid>=0)
1560      endshape();
1561     if(textid>=0)
1562      endtext();
1563
1564     if(sizex&3)
1565     { 
1566         /* SWF expects scanlines to be 4 byte aligned */
1567         int x,y;
1568         U8*ptr;
1569         mem2 = (U8*)malloc(BYTES_PER_SCANLINE(sizex)*sizey);
1570         ptr = mem2;
1571         for(y=0;y<sizey;y++)
1572         {
1573             for(x=0;x<sizex;x++)
1574                 *ptr++ = mem[y*sizex+x];
1575             ptr+= BYTES_PER_SCANLINE(sizex)-sizex;
1576         }
1577         mem = mem2;
1578     }
1579
1580     int bitid = ++currentswfid;
1581     oldtag = tag;
1582     tag = swf_InsertTag(tag,ST_DEFINEBITSLOSSLESS2);
1583     swf_SetU16(tag, bitid);
1584     if(swf_SetLosslessBitsIndexed(tag,sizex,sizey,mem, pal, n)<0) {
1585         swf_DeleteTag(tag);
1586         tag = oldtag;
1587         return -1;
1588     }
1589     if(mem2)
1590         free(mem2);
1591   
1592     drawimage(obj, bitid, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1593     return bitid;
1594 }
1595
1596 void swfoutput_drawimageagain(struct swfoutput*obj, int id, int sizex,int sizey, 
1597         double x1,double y1,
1598         double x2,double y2,
1599         double x3,double y3,
1600         double x4,double y4)
1601 {
1602     if(id<0) return;
1603     if(shapeid>=0)
1604      endshape();
1605     if(textid>=0)
1606      endtext();
1607
1608     drawimage(obj, id, sizex, sizey, x1,y1,x2,y2,x3,y3,x4,y4);
1609 }
1610