don't print a warning on each unknown parameter
[swftools.git] / lib / devices / swf.c
1 /* gfxdevice_swf.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2001,2002,2003,2004,2005 Matthias Kramm <kramm@quiss.org> 
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 <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "../../config.h"
25 #include <fcntl.h>
26 #include <unistd.h>
27 #ifdef HAVE_ASSERT_H
28 #include <assert.h>
29 #else
30 #define assert(a)
31 #endif
32 #include <math.h>
33 #include "../log.h"
34 #include "../rfxswf.h"
35 #include "../gfxdevice.h"
36 #include "../gfxtools.h"
37 #include "../art/libart.h"
38 #include "artsutils.c"
39
40 #define CHARDATAMAX 8192
41 #define CHARMIDX 0
42 #define CHARMIDY 0
43
44 typedef struct _chardata {
45     int charid;
46     int fontid; /* TODO: use a SWFFONT instead */
47     int x;
48     int y;
49     int size;
50     RGBA color;
51 } chardata_t;
52
53 typedef struct _fontlist
54 {
55     SWFFONT *swffont;
56     struct _fontlist*next;
57 } fontlist_t;
58
59 typedef long int twip;
60
61 typedef struct _swfmatrix {
62     double m11,m12,m21,m22,m31,m32;
63 } swfmatrix_t;
64
65 typedef struct _swfoutput_internal
66 {
67     gfxdevice_t*dev; // the gfxdevice object where this internal struct resides
68
69     double config_dumpfonts;
70     double config_ppmsubpixels;
71     double config_jpegsubpixels;
72     int config_opennewwindow;
73     int config_ignoredraworder;
74     int config_drawonlyshapes;
75     int config_jpegquality;
76     int config_storeallcharacters;
77     int config_generate_fake_tags;
78     int config_enablezlib;
79     int config_insertstoptag;
80     int config_flashversion;
81     int config_splinemaxerror;
82     int config_fontsplinemaxerror;
83     int config_filloverlap;
84     int config_protect;
85     int config_bboxvars;
86     int config_disable_polygon_conversion;
87     RGBA config_linkcolor;
88     float config_minlinewidth;
89     double config_caplinewidth;
90     char* config_linktarget;
91     char*config_internallinkfunction;
92     char*config_externallinkfunction;
93
94     SWF* swf;
95
96     fontlist_t* fontlist;
97
98     char storefont;
99
100     MATRIX page_matrix;
101
102     TAG *tag;
103     int currentswfid;
104     int depth;
105     int startdepth;
106     int linewidth;
107     
108     SHAPE* shape;
109     int shapeid;
110     int textid;
111     
112     int fillstyleid;
113     int linestyleid;
114     int swflastx;
115     int swflasty;
116     int lastwasfill;
117     int shapeisempty;
118     char fill;
119     int min_x,max_x;
120     int min_y,max_y;
121     TAG* cliptags[128];
122     int clipshapes[128];
123     U32 clipdepths[128];
124     int clippos;
125
126     /* image cache */
127     int pic_xids[1024];
128     int pic_yids[1024];
129     int pic_ids[1024];
130     int pic_width[1024];
131     int pic_height[1024];
132     int picpos;
133
134     int frameno;
135     int lastframeno;
136     
137     char fillstylechanged;
138
139     int jpeg; //next image type
140     
141     int bboxrectpos;
142     SRECT bboxrect;
143
144     chardata_t chardata[CHARDATAMAX];
145     int chardatapos;
146     int firstpage;
147     char pagefinished;
148
149     char overflow;
150
151     MATRIX fontmatrix;
152     double fontm11,fontm12,fontm21,fontm22;
153     SWFFONT *swffont;
154     RGBA strokergb;
155     RGBA fillrgb;
156     int drawmode;
157
158     int shapeposx;
159     int shapeposy;
160
161     char* mark;
162
163 } swfoutput_internal;
164     
165 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
166 static int  swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
167 static void swf_drawstroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit);
168 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line);
169 static void swf_endclip(gfxdevice_t*dev);
170 static void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit);
171 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
172 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
173 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
174 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix);
175 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
176 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action);
177 static void swf_startframe(gfxdevice_t*dev, int width, int height);
178 static void swf_endframe(gfxdevice_t*dev);
179 static gfxresult_t* swf_finish(gfxdevice_t*driver);
180
181 static swfoutput_internal* init_internal_struct()
182 {
183     swfoutput_internal*i = (swfoutput_internal*)malloc(sizeof(swfoutput_internal));
184     memset(i, 0, sizeof(swfoutput_internal));
185
186     i->storefont = 0;
187     i->currentswfid = 0;
188     i->depth = 0;
189     i->overflow = 0;
190     i->startdepth = 0;
191     i->linewidth = 0;
192     i->shapeid = -1;
193     i->textid = -1;
194     i->frameno = 0;
195     i->lastframeno = 0;
196
197     i->mark = 0;
198
199     i->fillstyleid;
200     i->linestyleid;
201     i->swflastx=0;
202     i->swflasty=0;
203     i->lastwasfill = 0;
204     i->shapeisempty = 1;
205     i->fill = 0;
206     i->clippos = 0;
207
208     i->fillstylechanged = 0;
209
210     i->bboxrectpos = -1;
211     i->chardatapos = 0;
212     i->firstpage = 1;
213     i->pagefinished = 1;
214
215     i->config_dumpfonts=0;
216     i->config_ppmsubpixels=0;
217     i->config_jpegsubpixels=0;
218     i->config_opennewwindow=1;
219     i->config_ignoredraworder=0;
220     i->config_drawonlyshapes=0;
221     i->config_jpegquality=85;
222     i->config_storeallcharacters=0;
223     i->config_enablezlib=0;
224     i->config_generate_fake_tags=0;
225     i->config_insertstoptag=0;
226     i->config_flashversion=6;
227     i->config_splinemaxerror=1;
228     i->config_fontsplinemaxerror=1;
229     i->config_filloverlap=0;
230     i->config_protect=0;
231     i->config_bboxvars=0;
232     i->config_minlinewidth=0.05;
233     i->config_caplinewidth=1;
234     i->config_linktarget=0;
235     i->config_internallinkfunction=0;
236     i->config_externallinkfunction=0;
237
238     i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
239     i->config_linkcolor.a = 0x40;
240
241     return i;
242 };
243
244 static int id_error = 0;
245
246 static U16 getNewID(gfxdevice_t* dev)
247 {
248     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
249     if(i->currentswfid == 65535) {
250         if(!id_error) {
251             msg("<error> ID Table overflow");
252             msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
253         }
254         id_error=1;
255         i->overflow = 1;
256         exit(1);
257     }
258     return ++i->currentswfid;
259 }
260 static U16 getNewDepth(gfxdevice_t* dev)
261 {
262     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
263     if(i->depth == 65535) {
264         if(!id_error) {
265             msg("<error> Depth Table overflow");
266             msg("<error> This file is too complex to render- SWF only supports 65536 shapes at once");
267         }
268         id_error=1;
269         i->overflow = 1;
270         exit(1);
271     }
272     return ++i->depth;
273 }
274
275 static void startshape(gfxdevice_t* dev);
276 static void starttext(gfxdevice_t* dev);
277 static void endshape(gfxdevice_t* dev);
278 static void endtext(gfxdevice_t* dev);
279
280 typedef struct _plotxy
281 {
282     double x,y;
283 } plotxy_t;
284
285 // write a move-to command into the swf
286 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
287 {
288     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
289     int rx = (int)(p0.x*20);
290     int ry = (int)(p0.y*20);
291     if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
292       swf_ShapeSetMove (tag, i->shape, rx,ry);
293       i->fillstylechanged = 0;
294       i->swflastx=rx;
295       i->swflasty=ry;
296       return 1;
297     }
298     return 0;
299 }
300 static int moveto(gfxdevice_t*dev, TAG*tag, double  x, double y)
301 {
302     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
303     plotxy_t p;
304     p.x = x;
305     p.y = y;
306     return movetoxy(dev, tag, p);
307 }
308 static void addPointToBBox(gfxdevice_t*dev, int px, int py) 
309 {
310     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
311
312     SPOINT p;
313     p.x = px;
314     p.y = py;
315     if(i->fill) {
316         swf_ExpandRect(&i->bboxrect, p);
317     } else {
318         swf_ExpandRect3(&i->bboxrect, p, i->linewidth*3/2);
319     }
320 }
321
322 /*static void plot(gfxdevice_t*dev, int x, int y, TAG*tag)
323 {
324     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
325     int width = i->linewidth/4;
326     if(width > 5)
327         width = 5;
328     ////square
329     //swf_ShapeSetLine(tag, i->shape,-width,-width);
330     //swf_ShapeSetLine(tag, i->shape,width*2,0);
331     //swf_ShapeSetLine(tag, i->shape,0,width*2);
332     //swf_ShapeSetLine(tag, i->shape,-width*2,0);
333     //swf_ShapeSetLine(tag, i->shape,0,-width*2);
334     //swf_ShapeSetLine(tag, i->shape,width,width);
335    
336     // diamond
337     swf_ShapeSetLine(tag, i->shape,-width,0);
338     swf_ShapeSetLine(tag, i->shape,width,-width);
339     swf_ShapeSetLine(tag, i->shape,width,width);
340     swf_ShapeSetLine(tag, i->shape,-width,width);
341     swf_ShapeSetLine(tag, i->shape,-width,-width);
342     swf_ShapeSetLine(tag, i->shape,width,0);
343
344     addPointToBBox(dev, x-width ,y-width);
345     addPointToBBox(dev, x+width ,y+width);
346 }*/
347
348 // write a line-to command into the swf
349 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
350 {
351     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
352     int px = (int)(p0.x*20);
353     int py = (int)(p0.y*20);
354     int rx = (px-i->swflastx);
355     int ry = (py-i->swflasty);
356     if(rx|ry) {
357         swf_ShapeSetLine (tag, i->shape, rx,ry);
358         addPointToBBox(dev, i->swflastx,i->swflasty);
359         addPointToBBox(dev, px,py);
360     }/* else if(!i->fill) {
361        // treat lines of length 0 as plots, making them
362        // at least 1 twip wide so Flash will display them
363         plot(dev, i->swflastx, i->swflasty, tag);
364     }*/
365
366     i->shapeisempty = 0;
367     i->swflastx+=rx;
368     i->swflasty+=ry;
369 }
370 static void lineto(gfxdevice_t*dev, TAG*tag, double x, double y)
371 {
372     plotxy_t p;
373     p.x = x;
374     p.y = y;
375     linetoxy(dev,tag, p);
376 }
377
378 // write a spline-to command into the swf
379 static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
380 {
381     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
382     int lastlastx = i->swflastx;
383     int lastlasty = i->swflasty;
384
385     int cx = ((int)(control.x*20)-i->swflastx);
386     int cy = ((int)(control.y*20)-i->swflasty);
387     i->swflastx += cx;
388     i->swflasty += cy;
389     int ex = ((int)(end.x*20)-i->swflastx);
390     int ey = ((int)(end.y*20)-i->swflasty);
391     i->swflastx += ex;
392     i->swflasty += ey;
393     
394     if((cx || cy) && (ex || ey)) {
395         swf_ShapeSetCurve(tag, i->shape, cx,cy,ex,ey);
396         addPointToBBox(dev, lastlastx   ,lastlasty   );
397         addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
398         addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
399     } else if(cx || cy || ex || ey) {
400         swf_ShapeSetLine(tag, i->shape, cx+ex,cy+ey);
401         addPointToBBox(dev, lastlastx   ,lastlasty   );
402         addPointToBBox(dev, lastlastx+cx,lastlasty+cy);
403         addPointToBBox(dev, lastlastx+cx+ex,lastlasty+cy+ey);
404     }
405
406     i->shapeisempty = 0;
407 }
408
409 /* write a line, given two points and the transformation
410    matrix. */
411 /*static void line(gfxdevice_t*dev, TAG*tag, plotxy_t p0, plotxy_t p1)
412 {
413     moveto(dev, tag, p0);
414     lineto(dev, tag, p1);
415 }*/
416
417 void resetdrawer(gfxdevice_t*dev)
418 {
419     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
420     i->swflastx = 0;
421     i->swflasty = 0;
422 }
423
424 static void stopFill(gfxdevice_t*dev)
425 {
426     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
427     if(i->lastwasfill!=0)
428     {
429         swf_ShapeSetStyle(i->tag,i->shape,i->linestyleid,0x8000,0);
430         i->fillstylechanged = 1;
431         i->lastwasfill = 0;
432     }
433 }
434 static void startFill(gfxdevice_t*dev)
435 {
436     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
437     if(i->lastwasfill!=1)
438     {
439         swf_ShapeSetStyle(i->tag,i->shape,0x8000,i->fillstyleid,0);
440         i->fillstylechanged = 1;
441         i->lastwasfill = 1;
442     }
443 }
444
445 static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
446 {
447
448     if(a->r!=b->r ||
449        a->g!=b->g ||
450        a->b!=b->b ||
451        a->a!=b->a) {
452         return 0;
453     }
454     return 1;
455 }
456
457 static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
458 {
459     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
460     SRECT r;
461     char debug = 0;
462     memset(&r, 0, sizeof(r));
463
464     int t;
465     if(debug) printf("\n");
466     for(t=0;t<i->chardatapos;t++)
467     {
468         if(i->chardata[t].fontid != font->id) {
469             msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
470             exit(1);
471         }
472         SRECT b = font->layout->bounds[i->chardata[t].charid];
473         b.xmin *= i->chardata[t].size;
474         b.ymin *= i->chardata[t].size;
475         b.xmax *= i->chardata[t].size;
476         b.ymax *= i->chardata[t].size;
477
478         /* divide by 1024, rounding xmax/ymax up */
479         b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
480
481         b.xmin += i->chardata[t].x;
482         b.ymin += i->chardata[t].y;
483         b.xmax += i->chardata[t].x;
484         b.ymax += i->chardata[t].y;
485
486         /* until we solve the INTERNAL_SCALING problem (see below)
487            make sure the bounding box is big enough */
488         b.xmin -= 20;
489         b.ymin -= 20;
490         b.xmax += 20;
491         b.ymax += 20;
492
493         b = swf_TurnRect(b, m);
494
495         if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
496                 font->layout->bounds[i->chardata[t].charid].xmin/20.0,
497                 font->layout->bounds[i->chardata[t].charid].ymin/20.0,
498                 font->layout->bounds[i->chardata[t].charid].xmax/20.0,
499                 font->layout->bounds[i->chardata[t].charid].ymax/20.0,
500                 b.xmin/20.0,
501                 b.ymin/20.0,
502                 b.xmax/20.0,
503                 b.ymax/20.0,
504                 i->chardata[t].fontid,
505                 font->id,
506                 i->chardata[t].charid
507                 );
508         swf_ExpandRect2(&r, &b);
509     }
510     if(debug) printf("-----> (%f,%f,%f,%f)\n",
511             r.xmin/20.0,
512             r.ymin/20.0,
513             r.xmax/20.0,
514             r.ymax/20.0);
515     return r;
516 }
517
518 static void putcharacters(gfxdevice_t*dev, TAG*tag)
519 {
520     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
521     int t;
522     SWFFONT font;
523     RGBA color;
524     color.r = i->chardata[0].color.r^255;
525     color.g = 0;
526     color.b = 0;
527     color.a = 0;
528     int lastfontid;
529     int lastx;
530     int lasty;
531     int lastsize;
532     int charids[128];
533     int charadvance[128];
534     int charstorepos;
535     int pass;
536     int glyphbits=1; //TODO: can this be zero?
537     int advancebits=1;
538
539     if(tag->id != ST_DEFINETEXT &&
540         tag->id != ST_DEFINETEXT2) {
541         msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
542         exit(1);
543     }
544     if(!i->chardatapos) {
545         msg("<warning> putcharacters called with zero characters");
546     }
547
548     for(pass = 0; pass < 2; pass++)
549     {
550         charstorepos = 0;
551         lastfontid = -1;
552         lastx = CHARMIDX;
553         lasty = CHARMIDY;
554         lastsize = -1;
555
556         if(pass==1)
557         {
558             advancebits++; // add sign bit
559             swf_SetU8(tag, glyphbits);
560             swf_SetU8(tag, advancebits);
561         }
562
563         for(t=0;t<=i->chardatapos;t++)
564         {
565             if(lastfontid != i->chardata[t].fontid || 
566                     lastx!=i->chardata[t].x ||
567                     lasty!=i->chardata[t].y ||
568                     !colorcompare(dev,&color, &i->chardata[t].color) ||
569                     charstorepos==127 ||
570                     lastsize != i->chardata[t].size ||
571                     t == i->chardatapos)
572             {
573                 if(charstorepos && pass==0)
574                 {
575                     int s;
576                     for(s=0;s<charstorepos;s++)
577                     {
578                         while(charids[s]>=(1<<glyphbits))
579                             glyphbits++;
580                         while(charadvance[s]>=(1<<advancebits))
581                             advancebits++;
582                     }
583                 }
584                 if(charstorepos && pass==1)
585                 {
586                     tag->writeBit = 0; // Q&D
587                     swf_SetBits(tag, 0, 1); // GLYPH Record
588                     swf_SetBits(tag, charstorepos, 7); // number of glyphs
589                     int s;
590                     for(s=0;s<charstorepos;s++)
591                     {
592                         swf_SetBits(tag, charids[s], glyphbits);
593                         swf_SetBits(tag, charadvance[s], advancebits);
594                     }
595                 }
596                 charstorepos = 0;
597
598                 if(pass == 1 && t<i->chardatapos)
599                 {
600                     RGBA*newcolor=0;
601                     SWFFONT*newfont=0;
602                     int newx = 0;
603                     int newy = 0;
604                     if(lastx != i->chardata[t].x ||
605                        lasty != i->chardata[t].y)
606                     {
607                         newx = i->chardata[t].x;
608                         newy = i->chardata[t].y;
609                         if(newx == 0)
610                             newx = SET_TO_ZERO;
611                         if(newy == 0)
612                             newy = SET_TO_ZERO;
613                     }
614                     if(!colorcompare(dev,&color, &i->chardata[t].color)) 
615                     {
616                         color = i->chardata[t].color;
617                         newcolor = &color;
618                     }
619                     font.id = i->chardata[t].fontid;
620                     if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
621                         newfont = &font;
622
623                     tag->writeBit = 0; // Q&D
624                     swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
625                 }
626
627                 lastfontid = i->chardata[t].fontid;
628                 lastx = i->chardata[t].x;
629                 lasty = i->chardata[t].y;
630                 lastsize = i->chardata[t].size;
631             }
632
633             if(t==i->chardatapos)
634                     break;
635
636             int advance;
637             int nextt = t==i->chardatapos-1?t:t+1;
638             int rel = i->chardata[nextt].x-i->chardata[t].x;
639             if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
640                advance = rel;
641                lastx=i->chardata[nextt].x;
642             }
643             else {
644                advance = 0;
645                lastx=i->chardata[t].x;
646             }
647             charids[charstorepos] = i->chardata[t].charid;
648             charadvance[charstorepos] = advance;
649             charstorepos ++;
650         }
651     }
652     i->chardatapos = 0;
653 }
654
655 static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
656 {
657     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
658     if(i->chardatapos == CHARDATAMAX)
659     {
660         msg("<warning> Character buffer too small. SWF will be slightly bigger");
661         endtext(dev);
662         starttext(dev);
663     }
664     i->chardata[i->chardatapos].fontid = fontid;
665     i->chardata[i->chardatapos].charid = charid;
666     i->chardata[i->chardatapos].x = x;
667     i->chardata[i->chardatapos].y = y;
668     i->chardata[i->chardatapos].color = color;
669     i->chardata[i->chardatapos].size = size;
670     i->chardatapos++;
671 }
672
673 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
674    So if we set this value to high, the char coordinates will overflow.
675    If we set it to low, however, the char positions will be inaccurate */
676 #define FONT_INTERNAL_SIZE 4
677
678 /* process a character. */
679 static int drawchar(gfxdevice_t*dev, SWFFONT *swffont, int charid, swfmatrix_t*m, gfxcolor_t*col)
680 {
681     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
682     if(!swffont) {
683         msg("<warning> Font is NULL");
684         return 0;
685     }
686
687     if(charid<0 || charid>=swffont->numchars) {
688         msg("<warning> No character %d in font %s ", charid, FIXNULL((char*)swffont->name));
689         return 0;
690     }
691     /*if(swffont->glyph[charid].shape->bitlen <= 16) {
692         msg("<warning> Glyph %d in current charset (%s, %d characters) is empty", 
693                 charid, FIXNULL((char*)swffont->name), swffont->numchars);
694         return 1;
695     }*/
696
697     if(i->shapeid>=0)
698         endshape(dev);
699     if(i->textid<0)
700         starttext(dev);
701
702     float x = m->m31;
703     float y = m->m32;
704     float det = ((m->m11*m->m22)-(m->m21*m->m12));
705     if(fabs(det) < 0.0005) { 
706         /* x direction equals y direction- the text is invisible */
707         return 1;
708     }
709     det = 20*FONT_INTERNAL_SIZE / det;
710
711     SPOINT p;
712     p.x = (SCOORD)((  x * m->m22 - y * m->m12)*det);
713     p.y = (SCOORD)((- x * m->m21 + y * m->m11)*det);
714
715     RGBA rgba = *(RGBA*)col;
716     putcharacter(dev, swffont->id, charid,p.x,p.y,FONT_INTERNAL_SIZE, rgba);
717     swf_FontUseGlyph(swffont, charid);
718     return 1;
719 }
720
721 static void endtext(gfxdevice_t*dev)
722 {
723     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
724     if(i->textid<0)
725         return;
726
727     i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT);
728     swf_SetU16(i->tag, i->textid);
729
730     SRECT r;
731     r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
732     
733     swf_SetRect(i->tag,&r);
734
735     swf_SetMatrix(i->tag,&i->fontmatrix);
736
737     msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
738
739     putcharacters(dev, i->tag);
740     swf_SetU8(i->tag,0);
741     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
742
743     swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
744     i->textid = -1;
745 }
746
747 /* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
748 static void swfoutput_setfontmatrix(gfxdevice_t*dev,double m11,double m21,
749                                                   double m12,double m22)
750 {
751     m11 *= 1024;
752     m12 *= 1024;
753     m21 *= 1024;
754     m22 *= 1024;
755     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
756     if(i->fontm11 == m11 &&
757        i->fontm12 == m12 &&
758        i->fontm21 == m21 &&
759        i->fontm22 == m22)
760         return;
761    if(i->textid>=0)
762         endtext(dev);
763     i->fontm11 = m11;
764     i->fontm12 = m12;
765     i->fontm21 = m21;
766     i->fontm22 = m22;
767     
768     MATRIX m;
769     m.sx = (U32)(((i->fontm11)*65536)/FONT_INTERNAL_SIZE); m.r1 = (U32)(((i->fontm12)*65536)/FONT_INTERNAL_SIZE);
770     m.r0 = (U32)(((i->fontm21)*65536)/FONT_INTERNAL_SIZE); m.sy = (U32)(((i->fontm22)*65536)/FONT_INTERNAL_SIZE); 
771     m.tx = 0;
772     m.ty = 0;
773     i->fontmatrix = m;
774 }
775
776 static void endpage(gfxdevice_t*dev)
777 {
778     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
779     if(i->shapeid>=0)
780       endshape(dev);
781     if(i->textid>=0)
782       endtext(dev);
783     while(i->clippos)
784         dev->endclip(dev);
785     i->pagefinished = 1;
786 }
787
788 void swf_startframe(gfxdevice_t*dev, int width, int height)
789 {
790     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
791     if(!i->firstpage && !i->pagefinished)
792         endpage(dev);
793     msg("<verbose> Starting new SWF page of size %dx%d", width, height);
794
795     swf_GetMatrix(0, &i->page_matrix);
796     i->page_matrix.tx = 0;
797     i->page_matrix.ty = 0;
798     i->min_x = 0;
799     i->min_y = 0;
800     i->max_x = width;
801     i->max_y = height;
802
803     /* set clipping/background rectangle */
804     /* TODO: this should all be done in SWFOutputDev */
805     //setBackground(dev, x1, y1, x2, y2);
806
807     /* increase SWF's bounding box */
808     SRECT r;
809     r.xmin = 0;
810     r.ymin = 0;
811     r.xmax = width*20;
812     r.ymax = height*20;
813     swf_ExpandRect2(&i->swf->movieSize, &r);
814
815     i->lastframeno = i->frameno;
816     i->firstpage = 0;
817     i->pagefinished = 0;
818 }
819
820 void swf_endframe(gfxdevice_t*dev)
821 {
822     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
823     
824     if(!i->pagefinished)
825         endpage(dev);
826
827     if(i->config_insertstoptag) {
828         ActionTAG*atag=0;
829         atag = action_Stop(atag);
830         atag = action_End(atag);
831         i->tag = swf_InsertTag(i->tag,ST_DOACTION);
832         swf_ActionSet(i->tag,atag);
833     }
834     i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
835     i->frameno ++;
836     
837     for(i->depth;i->depth>i->startdepth;i->depth--) {
838         i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
839         swf_SetU16(i->tag,i->depth);
840     }
841     i->depth = i->startdepth;
842 }
843
844 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
845 {
846     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
847     RGBA rgb;
848     rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
849     SRECT r;
850     SHAPE* s;
851     int ls1=0,fs1=0;
852     int shapeid = getNewID(dev);
853     r.xmin = x1;
854     r.ymin = y1;
855     r.xmax = x2;
856     r.ymax = y2;
857     i->tag = swf_InsertTag(i->tag, ST_DEFINESHAPE);
858     swf_ShapeNew(&s);
859     fs1 = swf_ShapeAddSolidFillStyle(s, &rgb);
860     swf_SetU16(i->tag,shapeid);
861     swf_SetRect(i->tag,&r);
862     swf_SetShapeHeader(i->tag,s);
863     swf_ShapeSetAll(i->tag,s,x1,y1,ls1,fs1,0);
864     swf_ShapeSetLine(i->tag,s,(x2-x1),0);
865     swf_ShapeSetLine(i->tag,s,0,(y2-y1));
866     swf_ShapeSetLine(i->tag,s,(x1-x2),0);
867     swf_ShapeSetLine(i->tag,s,0,(y1-y2));
868     swf_ShapeSetEnd(i->tag);
869     swf_ShapeFree(s);
870     i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
871     swf_ObjectPlace(i->tag,shapeid,getNewDepth(dev),0,0,0);
872     i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
873     swf_ObjectPlaceClip(i->tag,shapeid,getNewDepth(dev),0,0,0,65535);
874 }
875
876 /* initialize the swf writer */
877 void gfxdevice_swf_init(gfxdevice_t* dev)
878 {
879     memset(dev, 0, sizeof(gfxdevice_t));
880     
881     dev->name = "swf";
882
883     dev->internal = init_internal_struct();
884
885     dev->startpage = swf_startframe;
886     dev->endpage = swf_endframe;
887     dev->finish = swf_finish;
888     dev->fillbitmap = swf_fillbitmap;
889     dev->setparameter = swf_setparameter;
890     dev->stroke = swf_stroke;
891     dev->startclip = swf_startclip;
892     dev->endclip = swf_endclip;
893     dev->fill = swf_fill;
894     dev->fillbitmap = swf_fillbitmap;
895     dev->fillgradient = swf_fillgradient;
896     dev->addfont = swf_addfont;
897     dev->drawchar = swf_drawchar;
898     dev->drawlink = swf_drawlink;
899
900     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
901     i->dev = dev;
902
903     SRECT r;
904     RGBA rgb;
905
906     msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
907
908     i->swffont = 0;
909    
910     i->swf = (SWF*)rfx_calloc(sizeof(SWF));
911     i->swf->fileVersion    = i->config_flashversion;
912     i->swf->frameRate      = 0x0040; // 1 frame per 4 seconds
913     i->swf->movieSize.xmin = 0;
914     i->swf->movieSize.ymin = 0;
915     i->swf->movieSize.xmax = 0;
916     i->swf->movieSize.ymax = 0;
917     
918     i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
919     i->tag = i->swf->firstTag;
920     rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
921     swf_SetRGB(i->tag,&rgb);
922
923     i->startdepth = i->depth = 0;
924     
925     if(i->config_protect)
926       i->tag = swf_InsertTag(i->tag, ST_PROTECT);
927 }
928
929 static void startshape(gfxdevice_t*dev)
930 {
931     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
932     SRECT r;
933
934     if(i->shapeid>=0)
935         return;
936
937     if(i->textid>=0)
938         endtext(dev);
939
940     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
941
942     swf_ShapeNew(&i->shape);
943     i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
944     i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
945     if(i->mark) {
946         RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
947         swf_ShapeAddSolidFillStyle(i->shape,&markcol);
948     }
949
950     i->shapeid = getNewID(dev);
951     
952     msg("<debug> Using shape id %d", i->shapeid);
953
954     swf_SetU16(i->tag,i->shapeid);  // ID
955
956     i->bboxrectpos = i->tag->len;
957     /* changed later */
958     r.xmin = 0;
959     r.ymin = 0;
960     r.xmax = 20*i->max_x;
961     r.ymax = 20*i->max_y;
962     swf_SetRect(i->tag,&r);
963    
964     memset(&i->bboxrect, 0, sizeof(i->bboxrect));
965
966     swf_SetShapeStyles(i->tag,i->shape);
967     swf_ShapeCountBits(i->shape,NULL,NULL);
968     swf_SetShapeBits(i->tag,i->shape);
969
970     /* TODO: do we really need this? */
971     //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
972     //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
973     i->swflastx=i->swflasty=UNDEFINED_COORD;
974     i->lastwasfill = -1;
975     i->shapeisempty = 1;
976 }
977
978 static void starttext(gfxdevice_t*dev)
979 {
980     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
981     if(i->shapeid>=0)
982         endshape(dev);
983       
984     i->textid = getNewID(dev);
985
986     i->swflastx=i->swflasty=0;
987 }
988             
989
990 /* TODO: move to ../lib/rfxswf */
991 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
992 {
993     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
994     /* determine length of old rect */
995     tag->pos = pos;
996     tag->readBit = 0;
997     SRECT old;
998     swf_GetRect(tag, &old);
999     swf_ResetReadBits(tag);
1000     int pos_end = tag->pos;
1001
1002     int len = tag->len - pos_end;
1003     U8*data = (U8*)malloc(len);
1004     memcpy(data, &tag->data[pos_end], len);
1005     tag->writeBit = 0;
1006     tag->len = pos;
1007     swf_SetRect(tag, newrect);
1008     swf_SetBlock(tag, data, len);
1009     free(data);
1010     tag->pos = tag->readBit = 0;
1011 }
1012
1013 void cancelshape(gfxdevice_t*dev)
1014 {
1015     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1016     /* delete old shape tag */
1017     TAG*todel = i->tag;
1018     i->tag = i->tag->prev;
1019     swf_DeleteTag(todel);
1020     if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1021     i->shapeid = -1;
1022     i->bboxrectpos = -1;
1023
1024 //    i->currentswfid--; // this was an *exceptionally* bad idea
1025 }
1026
1027 void fixAreas(gfxdevice_t*dev)
1028 {
1029     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1030     if(!i->shapeisempty && i->fill &&
1031        (i->bboxrect.xmin == i->bboxrect.xmax ||
1032         i->bboxrect.ymin == i->bboxrect.ymax) &&
1033         i->config_minlinewidth >= 0.001
1034        ) {
1035         msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1036                 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1037                 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1038                 );
1039     
1040         SRECT r = i->bboxrect;
1041         
1042         if(r.xmin == r.xmax && r.ymin == r.ymax) {
1043             /* this thing comes down to a single dot- nothing to fix here */
1044             return;
1045         }
1046
1047         cancelshape(dev);
1048
1049         RGBA save_col = i->strokergb;
1050         int  save_width = i->linewidth;
1051
1052         i->strokergb = i->fillrgb;
1053         i->linewidth = (int)(i->config_minlinewidth*20);
1054         if(i->linewidth==0) i->linewidth = 1;
1055         
1056         startshape(dev);
1057         stopFill(dev);
1058
1059         moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1060         lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1061
1062         i->strokergb = save_col;
1063         i->linewidth = save_width;
1064     }
1065     
1066 }
1067
1068 static void endshape_noput(gfxdevice_t*dev)
1069 {
1070     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1071     if(i->shapeid<0) 
1072         return;
1073     //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1074     i->shapeid = -1;
1075     if(i->shape) {
1076         swf_ShapeFree(i->shape);
1077         i->shape=0;
1078     }
1079     i->fill=0;
1080     i->shapeposx=0;
1081     i->shapeposy=0;
1082 }
1083
1084 static void endshape(gfxdevice_t*dev)
1085 {
1086     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1087     if(i->shapeid<0) 
1088         return;
1089
1090     fixAreas(dev);
1091         
1092     if(i->shapeisempty ||
1093        /*bbox empty?*/
1094        (i->bboxrect.xmin == i->bboxrect.xmax && 
1095         i->bboxrect.ymin == i->bboxrect.ymax))
1096     {
1097         // delete the shape again, we didn't do anything
1098         msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1099                 i->bboxrect.xmin /20.0,
1100                 i->bboxrect.ymin /20.0,
1101                 i->bboxrect.xmax /20.0,
1102                 i->bboxrect.ymax /20.0
1103                 );
1104         cancelshape(dev);
1105         return;
1106     }
1107     
1108     swf_ShapeSetEnd(i->tag);
1109
1110     changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1111
1112     msg("<trace> Placing shape id %d", i->shapeid);
1113
1114     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1115     MATRIX m = i->page_matrix;
1116     m.tx += i->shapeposx;
1117     m.ty += i->shapeposy;
1118     swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1119
1120     swf_ShapeFree(i->shape);
1121     i->shape = 0;
1122     i->shapeid = -1;
1123     i->bboxrectpos = -1;
1124
1125     i->fill=0;
1126     i->shapeposx=0;
1127     i->shapeposy=0;
1128 }
1129
1130 void wipeSWF(SWF*swf)
1131 {
1132     TAG*tag = swf->firstTag;
1133     while(tag) {
1134         TAG*next = tag->next;
1135         if(tag->id != ST_SETBACKGROUNDCOLOR &&
1136            tag->id != ST_END &&
1137            tag->id != ST_DOACTION &&
1138            tag->id != ST_SHOWFRAME) {
1139             swf_DeleteTag(tag);
1140         }
1141         tag = next;
1142     }
1143 }
1144
1145
1146 void swfoutput_finalize(gfxdevice_t*dev)
1147 {
1148     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1149
1150     if(i->tag && i->tag->id == ST_END)
1151         return; //already done
1152
1153     if(i->config_bboxvars) {
1154         TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1155         ActionTAG*a = 0;
1156         a = action_PushString(a, "xmin");
1157         a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1158         a = action_SetVariable(a);
1159         a = action_PushString(a, "ymin");
1160         a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1161         a = action_SetVariable(a);
1162         a = action_PushString(a, "xmax");
1163         a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1164         a = action_SetVariable(a);
1165         a = action_PushString(a, "ymax");
1166         a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1167         a = action_SetVariable(a);
1168         a = action_PushString(a, "width");
1169         a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1170         a = action_SetVariable(a);
1171         a = action_PushString(a, "height");
1172         a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1173         a = action_SetVariable(a);
1174         a = action_End(a);
1175         swf_ActionSet(tag, a);
1176         swf_ActionFree(a);
1177     }
1178
1179     if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1180         dev->endpage(dev);
1181
1182     if(i->mark) {
1183         free(i->mark);i->mark = 0;
1184     }
1185
1186     endpage(dev);
1187     fontlist_t *tmp,*iterator = i->fontlist;
1188     while(iterator) {
1189         TAG*mtag = i->swf->firstTag;
1190         if(iterator->swffont) {
1191             if(!i->config_storeallcharacters) {
1192                 msg("<debug> Reducing font %s", iterator->swffont->name);
1193                 swf_FontReduce(iterator->swffont);
1194             }
1195             int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1196             if(i->config_storeallcharacters || used) {
1197                 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1198                 swf_FontSetDefine2(mtag, iterator->swffont);
1199             }
1200         }
1201
1202         iterator = iterator->next;
1203     }
1204     i->tag = swf_InsertTag(i->tag,ST_END);
1205     TAG* tag = i->tag->prev;
1206
1207     /* remove the removeobject2 tags between the last ST_SHOWFRAME
1208        and the ST_END- they confuse the flash player  */
1209     while(tag->id == ST_REMOVEOBJECT2) {
1210         TAG* prev = tag->prev;
1211         swf_DeleteTag(tag);
1212         tag = prev;
1213     }
1214     
1215     if(i->overflow) {
1216         wipeSWF(i->swf);
1217     }
1218     if(i->config_enablezlib || i->config_flashversion>=6) {
1219         i->swf->compressed = 1;
1220     }
1221
1222     swf_Optimize(i->swf);
1223 }
1224
1225 int swfresult_save(gfxresult_t*gfx, char*filename)
1226 {
1227     SWF*swf = (SWF*)gfx->internal;
1228     int fi;
1229     if(filename)
1230      fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1231     else
1232      fi = 1; // stdout
1233     
1234     if(fi<=0) {
1235         msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1236         return -1;
1237     }
1238     
1239     if(swf->compressed) {
1240         if FAILED(swf_WriteSWC(fi,swf)) 
1241             msg("<error> WriteSWC() failed.\n");
1242     } else {
1243         if FAILED(swf_WriteSWF(fi,swf)) 
1244             msg("<error> WriteSWF() failed.\n");
1245     }
1246
1247     if(filename)
1248      close(fi);
1249     return 0;
1250 }
1251 void* swfresult_get(gfxresult_t*gfx, char*name)
1252 {
1253     SWF*swf = (SWF*)gfx->internal;
1254     if(!strcmp(name, "swf")) {
1255         return (void*)swf_CopySWF(swf);
1256     } else if(!strcmp(name, "xmin")) {
1257         return (void*)(swf->movieSize.xmin/20);
1258     } else if(!strcmp(name, "ymin")) {
1259         return (void*)(swf->movieSize.ymin/20);
1260     } else if(!strcmp(name, "xmax")) {
1261         return (void*)(swf->movieSize.xmax/20);
1262     } else if(!strcmp(name, "ymax")) {
1263         return (void*)(swf->movieSize.ymax/20);
1264     } else if(!strcmp(name, "width")) {
1265         return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1266     } else if(!strcmp(name, "height")) {
1267         return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1268     }
1269     return 0;
1270 }
1271 void swfresult_destroy(gfxresult_t*gfx)
1272 {
1273     if(gfx->internal) {
1274         swf_FreeTags((SWF*)gfx->internal);
1275         free(gfx->internal);
1276         gfx->internal = 0;
1277     }
1278     memset(gfx, 0, sizeof(gfxresult_t));
1279     free(gfx);
1280 }
1281
1282 static void swfoutput_destroy(gfxdevice_t* dev);
1283
1284 gfxresult_t* swf_finish(gfxdevice_t* dev)
1285 {
1286     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1287     gfxresult_t*result;
1288
1289     if(i->config_linktarget) {
1290         free(i->config_linktarget);
1291         i->config_linktarget = 0;
1292     }
1293
1294     swfoutput_finalize(dev);
1295     SWF* swf = i->swf;i->swf = 0;
1296     swfoutput_destroy(dev);
1297
1298     result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1299     result->internal = swf;
1300     result->save = swfresult_save;
1301     result->write = 0;
1302     result->get = swfresult_get;
1303     result->destroy = swfresult_destroy;
1304     return result;
1305 }
1306
1307 /* Perform cleaning up */
1308 static void swfoutput_destroy(gfxdevice_t* dev) 
1309 {
1310     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1311     if(!i) {
1312         /* not initialized yet- nothing to destroy */
1313         return;
1314     }
1315
1316     fontlist_t *tmp,*iterator = i->fontlist;
1317     while(iterator) {
1318         if(iterator->swffont) {
1319             swf_FontFree(iterator->swffont);iterator->swffont=0;
1320         }
1321         tmp = iterator;
1322         iterator = iterator->next;
1323         free(tmp);
1324     }
1325     if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1326
1327     free(i);i=0;
1328     memset(dev, 0, sizeof(gfxdevice_t));
1329 }
1330
1331 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1332 {
1333     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1334     if(i->fillrgb.r == r &&
1335        i->fillrgb.g == g &&
1336        i->fillrgb.b == b &&
1337        i->fillrgb.a == a) return;
1338     if(i->shapeid>=0)
1339      endshape(dev);
1340
1341     i->fillrgb.r = r;
1342     i->fillrgb.g = g;
1343     i->fillrgb.b = b;
1344     i->fillrgb.a = a;
1345 }
1346
1347 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1348 {
1349     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1350     if(i->strokergb.r == r &&
1351        i->strokergb.g == g &&
1352        i->strokergb.b == b &&
1353        i->strokergb.a == a) return;
1354
1355     if(i->shapeid>=0)
1356      endshape(dev);
1357     i->strokergb.r = r;
1358     i->strokergb.g = g;
1359     i->strokergb.b = b;
1360     i->strokergb.a = a;
1361 }
1362
1363 //#define ROUND_UP 19
1364 //#define ROUND_UP 10
1365
1366 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1367 {
1368     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1369     if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1370         return;
1371     if(i->shapeid>=0)
1372         endshape(dev);
1373     i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1374 }
1375
1376
1377 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1378 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1379 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1380 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1381
1382 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1383 {
1384     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1385     dev->drawlink(dev, points, url);
1386 }
1387
1388 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1389 {
1390     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1391
1392     if(!strncmp("http://pdf2swf:", url, 15)) {
1393         char*tmp = strdup(url);
1394         int l = strlen(tmp);
1395         if(tmp[l-1] == '/')
1396            tmp[l-1] = 0;
1397         swfoutput_namedlink(dev, tmp+15, points);
1398         free(tmp);
1399         return;
1400     } else if(!strncmp("page", url, 4)) {
1401         int t, nodigit=0;
1402         for(t=4;url[t];t++)
1403             if(url[t]<'0' || url[t]>'9')
1404                 nodigit = 1;
1405         if(!nodigit) {
1406             int page = atoi(&url[4]);
1407             if(page<0) page = 0;
1408             swfoutput_linktopage(dev, page, points);
1409         }
1410     } else {
1411         swfoutput_linktourl(dev, url, points);
1412     }
1413 }
1414 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1415 {
1416     ActionTAG* actions = 0;
1417     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1418     if(i->shapeid>=0)
1419         endshape(dev);
1420     if(i->textid>=0)
1421         endtext(dev);
1422     
1423     if(i->config_externallinkfunction) {
1424         actions = action_PushString(actions, url); //parameter
1425         actions = action_PushInt(actions, 1); //number of parameters (1)
1426         actions = action_PushString(actions, i->config_externallinkfunction); //function name
1427         actions = action_CallFunction(actions);
1428     } else if(!i->config_linktarget) {
1429         if(!i->config_opennewwindow)
1430           actions = action_GetUrl(actions, url, "_parent");
1431         else
1432           actions = action_GetUrl(actions, url, "_this");
1433     } else {
1434         actions = action_GetUrl(actions, url, i->config_linktarget);
1435     }
1436     actions = action_End(actions);
1437     
1438     drawlink(dev, actions, 0, points,0);
1439 }
1440 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1441 {
1442     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1443     ActionTAG* actions = 0;
1444
1445     if(i->shapeid>=0)
1446         endshape(dev);
1447     if(i->textid>=0)
1448         endtext(dev);
1449   
1450     if(!i->config_internallinkfunction) {
1451         actions = action_GotoFrame(actions, page-1);
1452         actions = action_End(actions);
1453     } else {
1454         actions = action_PushInt(actions, page); //parameter
1455         actions = action_PushInt(actions, 1); //number of parameters (1)
1456         actions = action_PushString(actions, i->config_internallinkfunction); //function name
1457         actions = action_CallFunction(actions);
1458         actions = action_End(actions);
1459     }
1460
1461     drawlink(dev, actions, 0, points,0);
1462 }
1463
1464 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1465    of the viewer objects, like subtitles, index elements etc.
1466 */
1467 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1468 {
1469     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1470     ActionTAG *actions1,*actions2;
1471     char*tmp = strdup(name);
1472     char mouseover = 1;
1473
1474     if(i->shapeid>=0)
1475         endshape(dev);
1476     if(i->textid>=0)
1477         endtext(dev);
1478
1479     if(!strncmp(tmp, "call:", 5))
1480     {
1481         char*x = strchr(&tmp[5], ':');
1482         if(!x) {
1483             actions1 = action_PushInt(0, 0); //number of parameters (0)
1484             actions1 = action_PushString(actions1, &tmp[5]); //function name
1485             actions1 = action_CallFunction(actions1);
1486             actions1 = action_End(actions1);
1487         } else {
1488             *x = 0;
1489             actions1 = action_PushString(0, x+1); //parameter
1490             actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1491             actions1 = action_PushString(actions1, &tmp[5]); //function name
1492             actions1 = action_CallFunction(actions1);
1493             actions1 = action_End(actions1);
1494         }
1495         actions2 = action_End(0);
1496         mouseover = 0;
1497     }
1498     else
1499     {
1500         actions1 = action_PushString(0, "/:subtitle");
1501         actions1 = action_PushString(actions1, name);
1502         actions1 = action_SetVariable(actions1);
1503         actions1 = action_End(actions1);
1504
1505         actions2 = action_PushString(0, "/:subtitle");
1506         actions2 = action_PushString(actions2, "");
1507         actions2 = action_SetVariable(actions2);
1508         actions2 = action_End(actions2);
1509     }
1510
1511     drawlink(dev, actions1, actions2, points,mouseover);
1512
1513     swf_ActionFree(actions1);
1514     swf_ActionFree(actions2);
1515     free(tmp);
1516 }
1517
1518 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1519 {
1520     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1521     gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1522     char lastwasmoveto;
1523     int lines= 0, splines=0;
1524     while(1) {
1525         if(!line)
1526             break;
1527         /* check whether the next segment is zero */
1528         if(line->type == gfx_moveTo) {
1529             moveto(dev, i->tag, line->x, line->y);
1530             px = lastx = line->x;
1531             py = lasty = line->y;
1532             lastwasmoveto = 1;
1533         } if(line->type == gfx_lineTo) {
1534             lineto(dev, i->tag, line->x, line->y);
1535             px = line->x;
1536             py = line->y;
1537             lastwasmoveto = 0;
1538             lines++;
1539         } else if(line->type == gfx_splineTo) {
1540             plotxy_t s,p;
1541             s.x = line->sx;p.x = line->x;
1542             s.y = line->sy;p.y = line->y;
1543             splineto(dev, i->tag, s, p);
1544             px = line->x;
1545             py = line->y;
1546             lastwasmoveto = 0;
1547             splines++;
1548         }
1549         line = line->next;
1550     }
1551     msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1552 }
1553
1554
1555 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1556 {
1557     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1558     RGBA rgb;
1559     SRECT r;
1560     int lsid=0;
1561     int fsid;
1562     plotxy_t p1,p2,p3,p4;
1563     int myshapeid;
1564     int myshapeid2;
1565     double posx = 0;
1566     double posy = 0;
1567     int t;
1568     int buttonid = getNewID(dev);
1569     gfxbbox_t bbox = gfxline_getbbox(points);
1570
1571     /* shape */
1572     myshapeid = getNewID(dev);
1573     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1574     swf_ShapeNew(&i->shape);
1575     rgb.r = rgb.b = rgb.a = rgb.g = 0; 
1576     fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1577     swf_SetU16(i->tag, myshapeid);
1578     r.xmin = (int)(bbox.xmin*20);
1579     r.ymin = (int)(bbox.ymin*20);
1580     r.xmax = (int)(bbox.xmax*20);
1581     r.ymax = (int)(bbox.ymax*20);
1582     swf_SetRect(i->tag,&r);
1583     swf_SetShapeStyles(i->tag,i->shape);
1584     swf_ShapeCountBits(i->shape,NULL,NULL);
1585     swf_SetShapeBits(i->tag,i->shape);
1586     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1587     i->swflastx = i->swflasty = 0;
1588     drawgfxline(dev, points);
1589     swf_ShapeSetEnd(i->tag);
1590
1591     /* shape2 */
1592     myshapeid2 = getNewID(dev);
1593     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1594     swf_ShapeNew(&i->shape);
1595     
1596     rgb = i->config_linkcolor;
1597
1598     fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1599     swf_SetU16(i->tag, myshapeid2);
1600     r.xmin = (int)(bbox.xmin*20);
1601     r.ymin = (int)(bbox.ymin*20);
1602     r.xmax = (int)(bbox.xmax*20);
1603     r.ymax = (int)(bbox.ymax*20);
1604     swf_SetRect(i->tag,&r);
1605     swf_SetShapeStyles(i->tag,i->shape);
1606     swf_ShapeCountBits(i->shape,NULL,NULL);
1607     swf_SetShapeBits(i->tag,i->shape);
1608     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1609     i->swflastx = i->swflasty = 0;
1610     drawgfxline(dev, points);
1611     swf_ShapeSetEnd(i->tag);
1612
1613     if(!mouseover)
1614     {
1615         i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1616         swf_SetU16(i->tag,buttonid); //id
1617         swf_ButtonSetFlags(i->tag, 0); //menu=no
1618         swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1619         swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1620         swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1621         swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1622         swf_SetU8(i->tag,0);
1623         swf_ActionSet(i->tag,actions1);
1624         swf_SetU8(i->tag,0);
1625     }
1626     else
1627     {
1628         i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1629         swf_SetU16(i->tag,buttonid); //id
1630         swf_ButtonSetFlags(i->tag, 0); //menu=no
1631         swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1632         swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1633         swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1634         swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1635         swf_SetU8(i->tag,0); // end of button records
1636         swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1637         swf_ActionSet(i->tag,actions1);
1638         if(actions2) {
1639             swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1640             swf_ActionSet(i->tag,actions2);
1641             swf_SetU8(i->tag,0);
1642             swf_ButtonPostProcess(i->tag, 2);
1643         } else {
1644             swf_SetU8(i->tag,0);
1645             swf_ButtonPostProcess(i->tag, 1);
1646         }
1647     }
1648     
1649     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1650
1651     if(posx!=0 || posy!=0) {
1652         SPOINT p;
1653         p.x = (int)(posx*20);
1654         p.y = (int)(posy*20);
1655         p = swf_TurnPoint(p, &i->page_matrix);
1656         MATRIX m;
1657         m = i->page_matrix;
1658         m.tx = p.x;
1659         m.ty = p.y;
1660         swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1661     } else {
1662         swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1663     }
1664 }
1665
1666       
1667 ///////////
1668 /*
1669 for(t=0;t<picpos;t++)
1670       {
1671           if(pic_xids[t] == xid &&
1672              pic_yids[t] == yid) {
1673               width = pic_width[t];
1674               height = pic_height[t];
1675               found = t;break;
1676           }
1677       }
1678           pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1679           pic_xids[picpos] = xid;
1680           pic_yids[picpos] = yid;
1681           pic_width[picpos] = width;
1682           pic_height[picpos] = height;
1683           if(picpos<1024)
1684               picpos++;
1685             pic[width*y+x] = buf[0];
1686             xid+=x*buf[0]+1;
1687             yid+=y*buf[0]*3+1;
1688       
1689             xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1690       yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1691       
1692       int xid = 0;
1693       int yid = 0;
1694           xid += x*r+x*b*3+x*g*7+x*a*11;
1695           yid += y*r*3+y*b*17+y*g*19+y*a*11;
1696       int t,found = -1;
1697       for(t=0;t<picpos;t++)
1698       {
1699           if(pic_xids[t] == xid &&
1700              pic_yids[t] == yid) {
1701               found = t;break;
1702           }
1703       }
1704       if(found<0) {
1705 */
1706 ///////////
1707
1708
1709 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1710 {
1711     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1712
1713     msg("<trace> swfdevice: %s=%s", name, value);
1714     if(!strcmp(name, "jpegsubpixels")) {
1715         i->config_jpegsubpixels = atof(value);
1716     } else if(!strcmp(name, "ppmsubpixels")) {
1717         i->config_ppmsubpixels = atof(value);
1718     } else if(!strcmp(name, "subpixels")) {
1719         i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1720     } else if(!strcmp(name, "drawonlyshapes")) {
1721         i->config_drawonlyshapes = atoi(value);
1722     } else if(!strcmp(name, "ignoredraworder")) {
1723         i->config_ignoredraworder = atoi(value);
1724     } else if(!strcmp(name, "mark")) {
1725         if(!value || !value[0]) {
1726             if(i->mark) free(i->mark);
1727             i->mark = 0;
1728         } else {
1729             int t;
1730             i->mark = strdup("...");
1731             for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1732         }
1733     } else if(!strcmp(name, "filloverlap")) {
1734         i->config_filloverlap = atoi(value);
1735     } else if(!strcmp(name, "linksopennewwindow")) {
1736         i->config_opennewwindow = atoi(value);
1737     } else if(!strcmp(name, "opennewwindow")) {
1738         i->config_opennewwindow = atoi(value);
1739     } else if(!strcmp(name, "storeallcharacters")) {
1740         i->config_storeallcharacters = atoi(value);
1741     } else if(!strcmp(name, "enablezlib")) {
1742         i->config_enablezlib = atoi(value);
1743     } else if(!strcmp(name, "bboxvars")) {
1744         i->config_bboxvars = atoi(value);
1745     } else if(!strcmp(name, "internallinkfunction")) {
1746         i->config_internallinkfunction = strdup(value);
1747     } else if(!strcmp(name, "externallinkfunction")) {
1748         i->config_externallinkfunction = strdup(value);
1749     } else if(!strcmp(name, "disable_polygon_conversion")) {
1750         i->config_disable_polygon_conversion = atoi(value);
1751     } else if(!strcmp(name, "insertstop")) {
1752         i->config_insertstoptag = atoi(value);
1753     } else if(!strcmp(name, "protect")) {
1754         i->config_protect = atoi(value);
1755         if(i->config_protect && i->tag) {
1756             i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1757         }
1758     } else if(!strcmp(name, "faketags")) {
1759         i->config_generate_fake_tags = atoi(value);
1760     } else if(!strcmp(name, "flashversion")) {
1761         i->config_flashversion = atoi(value);
1762         if(i->swf) {
1763             i->swf->fileVersion = i->config_flashversion;
1764         }
1765     } else if(!strcmp(name, "minlinewidth")) {
1766         i->config_minlinewidth = atof(value);
1767     } else if(!strcmp(name, "caplinewidth")) {
1768         i->config_caplinewidth = atof(value);
1769     } else if(!strcmp(name, "linktarget")) {
1770         i->config_linktarget = strdup(value);
1771     } else if(!strcmp(name, "dumpfonts")) {
1772         i->config_dumpfonts = atoi(value);
1773     } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1774         i->jpeg = 1;
1775     } else if(!strcmp(name, "jpegquality")) {
1776         int val = atoi(value);
1777         if(val<0) val=0;
1778         if(val>100) val=100;
1779         i->config_jpegquality = val;
1780     } else if(!strcmp(name, "splinequality")) {
1781         int v = atoi(value);
1782         v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1783         if(v<1) v = 1;
1784         i->config_splinemaxerror = v;
1785     } else if(!strcmp(name, "fontquality")) {
1786         int v = atoi(value);
1787         v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1788         if(v<1) v = 1;
1789         i->config_fontsplinemaxerror = v;
1790     } else if(!strcmp(name, "linkcolor")) {
1791         if(strlen(value)!=8) {
1792             fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1793             return 1;
1794         }
1795 #       define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1796         i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1797         i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1798         i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1799         i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1800         printf("%02x%02x%02x%02x\n", 
1801             i->config_linkcolor.r,
1802             i->config_linkcolor.g,
1803             i->config_linkcolor.b,
1804             i->config_linkcolor.a);
1805
1806     } else {
1807         return 0;
1808     }
1809     return 1;
1810 }
1811
1812 // --------------------------------------------------------------------
1813
1814 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1815 {
1816     CXFORM cx;
1817     swf_GetCXForm(0, &cx, 1);
1818     if(!c)
1819         return cx;
1820     if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1821        c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1822        c->br!=0 || c->bg!=0 || c->ba!=0 ||
1823        c->ar!=0 || c->ag!=0 || c->ab!=0)
1824         msg("<warning> CXForm not SWF-compatible");
1825
1826     cx.a0 = (S16)(c->aa*256);
1827     cx.r0 = (S16)(c->rr*256);
1828     cx.g0 = (S16)(c->gg*256);
1829     cx.b0 = (S16)(c->bb*256);
1830     cx.a1 = c->t.a;
1831     cx.r1 = c->t.r;
1832     cx.g1 = c->t.g;
1833     cx.b1 = c->t.b;
1834     return cx;
1835 }
1836
1837 /* TODO */
1838 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1839 {
1840     return -1;
1841 }
1842 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1843 {
1844 }
1845     
1846 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1847 {
1848     gfxdevice_t*dev = i->dev;
1849     RGBA*newpic = 0;
1850     RGBA*mem = (RGBA*)img->data;
1851     
1852     int sizex = img->width;
1853     int sizey = img->height;
1854     int is_jpeg = i->jpeg;
1855     i->jpeg = 0;
1856
1857     int newsizex=sizex, newsizey=sizey;
1858
1859     /// {
1860     if(is_jpeg && i->config_jpegsubpixels) {
1861         newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1862         newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1863     } else if(!is_jpeg && i->config_ppmsubpixels) {
1864         newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1865         newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1866     }
1867     /// }
1868
1869     if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1870         return -1;
1871
1872     /* TODO: cache images */
1873     
1874     if(newsizex<sizex || newsizey<sizey) {
1875         msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1876         newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1877         *newwidth = sizex = newsizex;
1878         *newheight  = sizey = newsizey;
1879         mem = newpic;
1880     } else {
1881         *newwidth = newsizex = sizex;
1882         *newheight = newsizey  = sizey;
1883     }
1884
1885     int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1886     int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1887     
1888     msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
1889             sizex, sizey, 
1890             has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"", 
1891             is_jpeg?"jpeg-":"",
1892             newsizex, newsizey,
1893             targetwidth, targetheight,
1894             /*newsizex, newsizey,*/
1895             num_colors>256?">":"", num_colors>256?256:num_colors);
1896
1897     /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1898     swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1899     int t;
1900     for(t=0;t<num_colors;t++) {
1901         printf("%02x%02x%02x%02x ",
1902                 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1903         if((t&7)==7)
1904             printf("\n");
1905     }
1906     printf("\n");*/
1907
1908     int bitid = -1;
1909     int cacheid = imageInCache(dev, mem, sizex, sizey);
1910
1911     if(cacheid<=0) {
1912         bitid = getNewID(dev);
1913         i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1914         addImageToCache(dev, mem, sizex, sizey);
1915     } else {
1916         bitid = cacheid;
1917     }
1918
1919     if(newpic)
1920         free(newpic);
1921     return bitid;
1922 }
1923
1924 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1925 {
1926     gfxbbox_t bbox = gfxline_getbbox(line);
1927     SRECT r;
1928     r.xmin = (int)(bbox.xmin*20);
1929     r.ymin = (int)(bbox.ymin*20);
1930     r.xmax = (int)(bbox.xmax*20);
1931     r.ymax = (int)(bbox.ymax*20);
1932     return r;
1933 }
1934
1935 int line_is_empty(gfxline_t*line)
1936 {
1937     while(line) {
1938         if(line->type != gfx_moveTo)
1939             return 0;
1940         line = line->next;
1941     }
1942     return 1;
1943 }
1944
1945 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1946 {
1947     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1948     
1949     if(line_is_empty(line))
1950         return;
1951
1952     endshape(dev);
1953     endtext(dev);
1954
1955     int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1956     int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1957
1958     int newwidth=0,newheight=0;
1959     int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1960     if(bitid<0)
1961         return;
1962     double fx = (double)img->width / (double)newwidth;
1963     double fy = (double)img->height / (double)newheight;
1964
1965     MATRIX m;
1966     float m00,m10,tx;
1967     float m01,m11,ty;
1968     m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1969     m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1970     m.tx = (int)(matrix->tx*20);
1971     m.ty = (int)(matrix->ty*20);
1972   
1973     /* shape */
1974     int myshapeid = getNewID(dev);
1975     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1976     SHAPE*shape;
1977     swf_ShapeNew(&shape);
1978     int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1979     swf_SetU16(i->tag, myshapeid);
1980     SRECT r = gfxline_getSWFbbox(line);
1981     swf_SetRect(i->tag,&r);
1982     swf_SetShapeStyles(i->tag,shape);
1983     swf_ShapeCountBits(shape,NULL,NULL);
1984     swf_SetShapeBits(i->tag,shape);
1985     swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1986     i->swflastx = i->swflasty = UNDEFINED_COORD;
1987     drawgfxline(dev, line);
1988     swf_ShapeSetEnd(i->tag);
1989     swf_ShapeFree(shape);
1990
1991     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1992     CXFORM cxform2 = gfxcxform_to_cxform(cxform);
1993     swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
1994 }
1995
1996 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
1997 {
1998     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1999
2000     endtext(dev);
2001     endshape(dev);
2002
2003     if(i->clippos >= 127)
2004     {
2005         msg("<warning> Too many clip levels.");
2006         i->clippos --;
2007     } 
2008
2009     int myshapeid = getNewID(dev);
2010     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2011     RGBA col;
2012     memset(&col, 0, sizeof(RGBA));
2013     col.a = 255;
2014     SHAPE*shape;
2015     swf_ShapeNew(&shape);
2016     int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2017     if(i->mark) {
2018         RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2019         swf_ShapeAddSolidFillStyle(shape,&markcol);
2020     }
2021     swf_SetU16(i->tag,myshapeid);
2022     SRECT r = gfxline_getSWFbbox(line);
2023     swf_SetRect(i->tag,&r);
2024     swf_SetShapeStyles(i->tag,shape);
2025     swf_ShapeCountBits(shape,NULL,NULL);
2026     swf_SetShapeBits(i->tag,shape);
2027     swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2028     i->swflastx = i->swflasty = UNDEFINED_COORD;
2029     drawgfxline(dev, line);
2030     swf_ShapeSetEnd(i->tag);
2031     swf_ShapeFree(shape);
2032
2033     /* TODO: remember the bbox, and check all shapes against it */
2034     
2035     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2036     i->cliptags[i->clippos] = i->tag;
2037     i->clipshapes[i->clippos] = myshapeid;
2038     i->clipdepths[i->clippos] = getNewDepth(dev);
2039     i->clippos++;
2040 }
2041
2042 static void swf_endclip(gfxdevice_t*dev)
2043 {
2044     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2045     if(i->textid>=0)
2046         endtext(dev);
2047     if(i->shapeid>=0)
2048         endshape(dev);
2049
2050     if(!i->clippos) {
2051         msg("<error> Invalid end of clipping region");
2052         return;
2053     }
2054     i->clippos--;
2055     /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2056             / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2057     i->depth ++;*/
2058     swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2059 }
2060 static int gfxline_type(gfxline_t*line)
2061 {
2062     int tmplines=0;
2063     int tmpsplines=0;
2064     int lines=0;
2065     int splines=0;
2066     int haszerosegments=0;
2067     int length=0;
2068     while(line) {
2069         if(line->type == gfx_moveTo) {
2070             tmplines=0;
2071             tmpsplines=0;
2072         } else if(line->type == gfx_lineTo) {
2073             tmplines++;
2074             if(tmplines>lines)
2075                 lines=tmplines;
2076         } else if(line->type == gfx_splineTo) {
2077             tmpsplines++;
2078             if(tmpsplines>lines)
2079                 splines=tmpsplines;
2080         }
2081         length++;
2082         line = line->next;
2083     }
2084     if(length>400)
2085         return 5;
2086     if(lines==0 && splines==0) return 0;
2087     else if(lines==1 && splines==0) return 1;
2088     else if(lines==0 && splines==1) return 2;
2089     else if(splines==0) return 3;
2090     else return 4;
2091 }
2092
2093 static int gfxline_has_dots(gfxline_t*line)
2094 {
2095     int tmplines=0;
2096     double x=0,y=0;
2097     double dist = 0;
2098     int isline = 0;
2099     int short_gap = 0;
2100     while(line) {
2101         if(line->type == gfx_moveTo) {
2102             /* test the length of the preceding line, and assume it is a dot if
2103                it's length is less than 1.0. But *only* if there's a noticable 
2104                gap between the previous line and the next moveTo. (I've come
2105                across a PDF where thousands of "dots" were stringed together,
2106                forming a line) */
2107             int last_short_gap = short_gap;
2108             if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2109                 short_gap = 1;
2110             } else {
2111                 short_gap = 0;
2112             }
2113             if(isline && dist < 1 && !short_gap && !last_short_gap) {
2114                 return 1;
2115             }
2116             dist = 0;
2117             isline = 0;
2118         } else if(line->type == gfx_lineTo) {
2119             dist += fabs(line->x - x) + fabs(line->y - y);
2120             isline = 1;
2121         } else if(line->type == gfx_splineTo) {
2122             dist += fabs(line->sx - x) + fabs(line->sy - y) + 
2123                     fabs(line->x - line->sx) + fabs(line->y - line->sy);
2124             isline = 1;
2125         }
2126         x = line->x;
2127         y = line->y;
2128         line = line->next;
2129     }
2130     if(isline && dist < 1 && !short_gap) {
2131         return 1;
2132     }
2133     return 0;
2134 }
2135
2136 static int gfxline_fix_short_edges(gfxline_t*line)
2137 {
2138     double x,y;
2139     while(line) {
2140         if(line->type == gfx_lineTo) {
2141             if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2142                 line->x += 0.01;
2143             }
2144         } else if(line->type == gfx_splineTo) {
2145             if(fabs(line->sx - x) + fabs(line->sy - y) + 
2146                fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2147                 line->x += 0.01;
2148             }
2149         }
2150         x = line->x;
2151         y = line->y;
2152         line = line->next;
2153     }
2154     return 0;
2155 }
2156
2157 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2158 {
2159     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2160     if(x<i->min_x || x>i->max_x) return 0;
2161     if(y<i->min_y || y>i->max_y) return 0;
2162     return 1;
2163 }
2164
2165 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2166 {
2167     gfxline_t*l = line = gfxline_clone(line);
2168
2169     while(l) {
2170         l->x += x;
2171         l->y += y;
2172         l->sx += x;
2173         l->sy += y;
2174         l = l->next;
2175     }
2176     return line;
2177 }
2178
2179 //#define NORMALIZE_POLYGON_POSITIONS
2180
2181 static void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
2182 {
2183     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2184     if(line_is_empty(line))
2185         return;
2186     int type = gfxline_type(line);
2187     int has_dots = gfxline_has_dots(line);
2188     gfxbbox_t r = gfxline_getbbox(line);
2189     int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2190
2191     /* TODO: * split line into segments, and perform this check for all segments */
2192
2193     if(i->config_disable_polygon_conversion || type>=5 ||
2194        (!has_dots &&
2195         (width <= i->config_caplinewidth 
2196         || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2197         || (cap_style == gfx_capRound && type<=2)))) {} else 
2198     {
2199         /* convert line to polygon */
2200         msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2201         if(has_dots)
2202             gfxline_fix_short_edges(line);
2203         /* we need to convert the line into a polygon */
2204         ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2205         gfxline_t*gfxline = SVPtogfxline(svp);
2206         dev->fill(dev, gfxline, color);
2207         free(gfxline);
2208         art_svp_free(svp);
2209         return;
2210     }
2211
2212     msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2213     endtext(dev);
2214
2215 #ifdef NORMALIZE_POLYGON_POSITIONS
2216     endshape(dev);
2217     double startx = 0, starty = 0;
2218     if(line && line->type == gfx_moveTo) {
2219         startx = line->x;
2220         starty = line->y;
2221     }
2222     line = gfxline_move(line, -startx, -starty);
2223     i->shapeposx = (int)(startx*20);
2224     i->shapeposy = (int)(starty*20);
2225 #endif
2226
2227     swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2228     swfoutput_setlinewidth(dev, width);
2229     startshape(dev);
2230     stopFill(dev);
2231     drawgfxline(dev, line);
2232
2233 #ifdef NORMALIZE_POLYGON_POSITIONS
2234     free(line); //account for _move
2235 #endif
2236
2237 }
2238 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2239 {
2240     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2241     if(line_is_empty(line))
2242         return;
2243     if(!color->a)
2244         return;
2245     gfxbbox_t r = gfxline_getbbox(line);
2246     int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2247
2248     endtext(dev);
2249     if(!i->config_ignoredraworder)
2250         endshape(dev);
2251
2252 #ifdef NORMALIZE_POLYGON_POSITIONS
2253     endshape(dev);
2254     double startx = 0, starty = 0;
2255     if(line && line->type == gfx_moveTo) {
2256         startx = line->x;
2257         starty = line->y;
2258     }
2259     line = gfxline_move(line, -startx, -starty);
2260     i->shapeposx = (int)(startx*20);
2261     i->shapeposy = (int)(starty*20);
2262 #endif
2263
2264     swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2265     startshape(dev);
2266     startFill(dev);
2267     i->fill=1;
2268     drawgfxline(dev, line);
2269     msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2270
2271 #ifdef NORMALIZE_POLYGON_POSITIONS
2272     free(line); //account for _move
2273 #endif
2274 }
2275 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2276 {
2277     if(line_is_empty(line))
2278         return;
2279     msg("<error> Gradient filling not implemented yet");
2280 }
2281
2282 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2283 {
2284     SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2285     int t;
2286     SRECT bounds = {0,0,0,0};
2287     swffont->id = -1;
2288     swffont->version = 2;
2289     swffont->name = (U8*)strdup(id);
2290     swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2291     swffont->layout->ascent = 0; /* ? */
2292     swffont->layout->descent = 0;
2293     swffont->layout->leading = 0;
2294     swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2295     swffont->encoding = FONT_ENCODING_UNICODE;
2296     swffont->numchars = font->num_glyphs;
2297     swffont->maxascii = font->max_unicode;
2298     swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2299     swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2300     swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2301     swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2302     for(t=0;t<font->max_unicode;t++) {
2303         swffont->ascii2glyph[t] = font->unicode2glyph[t];
2304     }
2305     for(t=0;t<font->num_glyphs;t++) {
2306         drawer_t draw;
2307         gfxline_t*line;
2308         int advance = 0;
2309         swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2310         if(font->glyphs[t].name) {
2311             swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2312         } else {
2313             swffont->glyphnames[t] = 0;
2314         }
2315         advance = (int)(font->glyphs[t].advance);
2316
2317         swf_Shape01DrawerInit(&draw, 0);
2318         line = font->glyphs[t].line;
2319         while(line) {
2320             FPOINT c,to;
2321             c.x = line->sx; c.y = line->sy;
2322             to.x = line->x; to.y = line->y;
2323             if(line->type == gfx_moveTo) {
2324                 draw.moveTo(&draw, &to);
2325             } else if(line->type == gfx_lineTo) {
2326                 draw.lineTo(&draw, &to);
2327             } else if(line->type == gfx_splineTo) {
2328                 draw.splineTo(&draw, &c, &to);
2329             }
2330             line = line->next;
2331         }
2332         draw.finish(&draw);
2333         swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2334         swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2335
2336         if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2337             printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2338             advance = swffont->layout->bounds[t].xmax;
2339         }
2340             
2341         if(advance<32768) {
2342             swffont->glyph[t].advance = advance;
2343         } else {
2344             swffont->glyph[t].advance = 32767;
2345         }
2346
2347         draw.dealloc(&draw);
2348
2349         swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2350     }
2351     if(bounds.ymin < 0 && bounds.ymax > 0) {
2352         swffont->layout->ascent = -bounds.ymin;
2353         swffont->layout->descent = bounds.ymax;
2354         swffont->layout->leading = bounds.ymax - bounds.ymin;
2355     } else {
2356         swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2357         swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2358         swffont->layout->leading = bounds.ymax - bounds.ymin;
2359     }
2360
2361     return swffont;
2362 }
2363
2364 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2365 {
2366     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2367
2368     if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2369         return; // the requested font is the current font
2370     
2371     fontlist_t*last=0,*l = i->fontlist;
2372     while(l) {
2373         last = l;
2374         if(!strcmp((char*)l->swffont->name, font->id)) {
2375             return; // we already know this font
2376         }
2377         l = l->next;
2378     }
2379     l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2380     l->swffont = gfxfont_to_swffont(font, font->id);
2381     l->next = 0;
2382     if(last) {
2383         last->next = l;
2384     } else {
2385         i->fontlist = l;
2386     }
2387     swf_FontSetID(l->swffont, getNewID(i->dev));
2388
2389     if(getScreenLogLevel() >= LOGLEVEL_DEBUG)  {
2390         int iii;
2391         // print font information
2392         msg("<debug> Font %s",font->id);
2393         msg("<debug> |   ID: %d", l->swffont->id);
2394         msg("<debug> |   Version: %d", l->swffont->version);
2395         msg("<debug> |   Name: %s", l->swffont->name);
2396         msg("<debug> |   Numchars: %d", l->swffont->numchars);
2397         msg("<debug> |   Maxascii: %d", l->swffont->maxascii);
2398         msg("<debug> |   Style: %d", l->swffont->style);
2399         msg("<debug> |   Encoding: %d", l->swffont->encoding);
2400         for(iii=0; iii<l->swffont->numchars;iii++) {
2401             msg("<debug> |   Glyph %d) name=%s, unicode=%d size=%d bbox=(%.2f,%.2f,%.2f,%.2f)\n", iii, l->swffont->glyphnames?l->swffont->glyphnames[iii]:"<nonames>", l->swffont->glyph2ascii[iii], l->swffont->glyph[iii].shape->bitlen, 
2402                     l->swffont->layout->bounds[iii].xmin/20.0,
2403                     l->swffont->layout->bounds[iii].ymin/20.0,
2404                     l->swffont->layout->bounds[iii].xmax/20.0,
2405                     l->swffont->layout->bounds[iii].ymax/20.0
2406                     );
2407             int t;
2408             for(t=0;t<l->swffont->maxascii;t++) {
2409                 if(l->swffont->ascii2glyph[t] == iii)
2410                     msg("<debug> | - maps to %d",t);
2411             }
2412         }
2413     }
2414 }
2415
2416 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2417 {
2418     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2419
2420     if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2421         return; // the requested font is the current font
2422     
2423     fontlist_t*l = i->fontlist;
2424     while(l) {
2425         if(!strcmp((char*)l->swffont->name, fontid)) {
2426             i->swffont = l->swffont;
2427             return; //done!
2428         }
2429         l = l->next;
2430     }
2431     msg("<error> Unknown font id: %s", fontid);
2432     return;
2433 }
2434
2435 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2436 {
2437     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2438         
2439     if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2440     {
2441         /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2442                  with multiple fonts */
2443         endtext(dev);
2444
2445         swf_switchfont(dev, font->id); // set the current font
2446     }
2447     swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2448    
2449     swfmatrix_t m;
2450     m.m11 = i->fontm11;
2451     m.m12 = i->fontm12;
2452     m.m21 = i->fontm21;
2453     m.m22 = i->fontm22;
2454     m.m31 = matrix->tx;
2455     m.m32 = matrix->ty;
2456   
2457 /*    printf("%f %f\n", m.m31,  m.m32);
2458     {
2459     static int xpos = 40;
2460     static int ypos = 200;
2461     m.m31 = xpos;
2462     m.m32 = ypos;
2463     xpos += 10;
2464     }*/
2465
2466
2467     drawchar(dev, i->swffont, glyph, &m, color);
2468 }