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