don't draw shapes with alpha=0
[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     dev->internal = init_internal_struct();
881
882     dev->startpage = swf_startframe;
883     dev->endpage = swf_endframe;
884     dev->finish = swf_finish;
885     dev->fillbitmap = swf_fillbitmap;
886     dev->setparameter = swf_setparameter;
887     dev->stroke = swf_stroke;
888     dev->startclip = swf_startclip;
889     dev->endclip = swf_endclip;
890     dev->fill = swf_fill;
891     dev->fillbitmap = swf_fillbitmap;
892     dev->fillgradient = swf_fillgradient;
893     dev->addfont = swf_addfont;
894     dev->drawchar = swf_drawchar;
895     dev->drawlink = swf_drawlink;
896
897     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
898     i->dev = dev;
899
900     SRECT r;
901     RGBA rgb;
902
903     msg("<verbose> initializing swf output\n", i->max_x,i->max_y);
904
905     i->swffont = 0;
906    
907     i->swf = (SWF*)rfx_calloc(sizeof(SWF));
908     i->swf->fileVersion    = i->config_flashversion;
909     i->swf->frameRate      = 0x0040; // 1 frame per 4 seconds
910     i->swf->movieSize.xmin = 0;
911     i->swf->movieSize.ymin = 0;
912     i->swf->movieSize.xmax = 0;
913     i->swf->movieSize.ymax = 0;
914     
915     i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
916     i->tag = i->swf->firstTag;
917     rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
918     swf_SetRGB(i->tag,&rgb);
919
920     i->startdepth = i->depth = 0;
921     
922     if(i->config_protect)
923       i->tag = swf_InsertTag(i->tag, ST_PROTECT);
924 }
925
926 static void startshape(gfxdevice_t*dev)
927 {
928     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
929     SRECT r;
930
931     if(i->shapeid>=0)
932         return;
933
934     if(i->textid>=0)
935         endtext(dev);
936
937     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
938
939     swf_ShapeNew(&i->shape);
940     i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&i->strokergb);
941     i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&i->fillrgb);
942     if(i->mark) {
943         RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
944         swf_ShapeAddSolidFillStyle(i->shape,&markcol);
945     }
946
947     i->shapeid = getNewID(dev);
948     
949     msg("<debug> Using shape id %d", i->shapeid);
950
951     swf_SetU16(i->tag,i->shapeid);  // ID
952
953     i->bboxrectpos = i->tag->len;
954     /* changed later */
955     r.xmin = 0;
956     r.ymin = 0;
957     r.xmax = 20*i->max_x;
958     r.ymax = 20*i->max_y;
959     swf_SetRect(i->tag,&r);
960    
961     memset(&i->bboxrect, 0, sizeof(i->bboxrect));
962
963     swf_SetShapeStyles(i->tag,i->shape);
964     swf_ShapeCountBits(i->shape,NULL,NULL);
965     swf_SetShapeBits(i->tag,i->shape);
966
967     /* TODO: do we really need this? */
968     //swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,i->linestyleid,0,0);
969     //swf_ShapeSetAll(i->tag,i->shape,/*x*/UNDEFINED_COORD,/*y*/UNDEFINED_COORD,i->linestyleid,0,0);
970     i->swflastx=i->swflasty=UNDEFINED_COORD;
971     i->lastwasfill = -1;
972     i->shapeisempty = 1;
973 }
974
975 static void starttext(gfxdevice_t*dev)
976 {
977     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
978     if(i->shapeid>=0)
979         endshape(dev);
980       
981     i->textid = getNewID(dev);
982
983     i->swflastx=i->swflasty=0;
984 }
985             
986
987 /* TODO: move to ../lib/rfxswf */
988 void changeRect(gfxdevice_t*dev, TAG*tag, int pos, SRECT*newrect)
989 {
990     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
991     /* determine length of old rect */
992     tag->pos = pos;
993     tag->readBit = 0;
994     SRECT old;
995     swf_GetRect(tag, &old);
996     swf_ResetReadBits(tag);
997     int pos_end = tag->pos;
998
999     int len = tag->len - pos_end;
1000     U8*data = (U8*)malloc(len);
1001     memcpy(data, &tag->data[pos_end], len);
1002     tag->writeBit = 0;
1003     tag->len = pos;
1004     swf_SetRect(tag, newrect);
1005     swf_SetBlock(tag, data, len);
1006     free(data);
1007     tag->pos = tag->readBit = 0;
1008 }
1009
1010 void cancelshape(gfxdevice_t*dev)
1011 {
1012     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1013     /* delete old shape tag */
1014     TAG*todel = i->tag;
1015     i->tag = i->tag->prev;
1016     swf_DeleteTag(todel);
1017     if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
1018     i->shapeid = -1;
1019     i->bboxrectpos = -1;
1020
1021 //    i->currentswfid--; // this was an *exceptionally* bad idea
1022 }
1023
1024 void fixAreas(gfxdevice_t*dev)
1025 {
1026     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1027     if(!i->shapeisempty && i->fill &&
1028        (i->bboxrect.xmin == i->bboxrect.xmax ||
1029         i->bboxrect.ymin == i->bboxrect.ymax) &&
1030         i->config_minlinewidth >= 0.001
1031        ) {
1032         msg("<debug> Shape has size 0: width=%.2f height=%.2f",
1033                 (i->bboxrect.xmax-i->bboxrect.xmin)/20.0,
1034                 (i->bboxrect.ymax-i->bboxrect.ymin)/20.0
1035                 );
1036     
1037         SRECT r = i->bboxrect;
1038         
1039         if(r.xmin == r.xmax && r.ymin == r.ymax) {
1040             /* this thing comes down to a single dot- nothing to fix here */
1041             return;
1042         }
1043
1044         cancelshape(dev);
1045
1046         RGBA save_col = i->strokergb;
1047         int  save_width = i->linewidth;
1048
1049         i->strokergb = i->fillrgb;
1050         i->linewidth = (int)(i->config_minlinewidth*20);
1051         if(i->linewidth==0) i->linewidth = 1;
1052         
1053         startshape(dev);
1054         stopFill(dev);
1055
1056         moveto(dev, i->tag, r.xmin/20.0,r.ymin/20.0);
1057         lineto(dev, i->tag, r.xmax/20.0,r.ymax/20.0);
1058
1059         i->strokergb = save_col;
1060         i->linewidth = save_width;
1061     }
1062     
1063 }
1064
1065 static void endshape_noput(gfxdevice_t*dev)
1066 {
1067     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1068     if(i->shapeid<0) 
1069         return;
1070     //changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1071     i->shapeid = -1;
1072     if(i->shape) {
1073         swf_ShapeFree(i->shape);
1074         i->shape=0;
1075     }
1076     i->fill=0;
1077     i->shapeposx=0;
1078     i->shapeposy=0;
1079 }
1080
1081 static void endshape(gfxdevice_t*dev)
1082 {
1083     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1084     if(i->shapeid<0) 
1085         return;
1086
1087     fixAreas(dev);
1088         
1089     if(i->shapeisempty ||
1090        /*bbox empty?*/
1091        (i->bboxrect.xmin == i->bboxrect.xmax && 
1092         i->bboxrect.ymin == i->bboxrect.ymax))
1093     {
1094         // delete the shape again, we didn't do anything
1095         msg("<debug> cancelling shape: bbox is (%f,%f,%f,%f)",
1096                 i->bboxrect.xmin /20.0,
1097                 i->bboxrect.ymin /20.0,
1098                 i->bboxrect.xmax /20.0,
1099                 i->bboxrect.ymax /20.0
1100                 );
1101         cancelshape(dev);
1102         return;
1103     }
1104     
1105     swf_ShapeSetEnd(i->tag);
1106
1107     changeRect(dev, i->tag, i->bboxrectpos, &i->bboxrect);
1108
1109     msg("<trace> Placing shape id %d", i->shapeid);
1110
1111     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1112     MATRIX m = i->page_matrix;
1113     m.tx += i->shapeposx;
1114     m.ty += i->shapeposy;
1115     swf_ObjectPlace(i->tag,i->shapeid,getNewDepth(dev),&m,NULL,NULL);
1116
1117     swf_ShapeFree(i->shape);
1118     i->shape = 0;
1119     i->shapeid = -1;
1120     i->bboxrectpos = -1;
1121
1122     i->fill=0;
1123     i->shapeposx=0;
1124     i->shapeposy=0;
1125 }
1126
1127 void wipeSWF(SWF*swf)
1128 {
1129     TAG*tag = swf->firstTag;
1130     while(tag) {
1131         TAG*next = tag->next;
1132         if(tag->id != ST_SETBACKGROUNDCOLOR &&
1133            tag->id != ST_END &&
1134            tag->id != ST_DOACTION &&
1135            tag->id != ST_SHOWFRAME) {
1136             swf_DeleteTag(tag);
1137         }
1138         tag = next;
1139     }
1140 }
1141
1142
1143 void swfoutput_finalize(gfxdevice_t*dev)
1144 {
1145     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1146
1147     if(i->tag && i->tag->id == ST_END)
1148         return; //already done
1149
1150     if(i->config_bboxvars) {
1151         TAG* tag = swf_InsertTag(i->swf->firstTag, ST_DOACTION);
1152         ActionTAG*a = 0;
1153         a = action_PushString(a, "xmin");
1154         a = action_PushFloat(a, i->swf->movieSize.xmin / 20.0);
1155         a = action_SetVariable(a);
1156         a = action_PushString(a, "ymin");
1157         a = action_PushFloat(a, i->swf->movieSize.ymin / 20.0);
1158         a = action_SetVariable(a);
1159         a = action_PushString(a, "xmax");
1160         a = action_PushFloat(a, i->swf->movieSize.xmax / 20.0);
1161         a = action_SetVariable(a);
1162         a = action_PushString(a, "ymax");
1163         a = action_PushFloat(a, i->swf->movieSize.ymax / 20.0);
1164         a = action_SetVariable(a);
1165         a = action_PushString(a, "width");
1166         a = action_PushFloat(a, (i->swf->movieSize.xmax - i->swf->movieSize.xmin) / 20.0);
1167         a = action_SetVariable(a);
1168         a = action_PushString(a, "height");
1169         a = action_PushFloat(a, (i->swf->movieSize.ymax - i->swf->movieSize.ymin) / 20.0);
1170         a = action_SetVariable(a);
1171         a = action_End(a);
1172         swf_ActionSet(tag, a);
1173         swf_ActionFree(a);
1174     }
1175
1176     if(i->frameno == i->lastframeno) // fix: add missing pagefeed
1177         dev->endpage(dev);
1178
1179     if(i->mark) {
1180         free(i->mark);i->mark = 0;
1181     }
1182
1183     endpage(dev);
1184     fontlist_t *tmp,*iterator = i->fontlist;
1185     while(iterator) {
1186         TAG*mtag = i->swf->firstTag;
1187         if(iterator->swffont) {
1188             if(!i->config_storeallcharacters) {
1189                 msg("<debug> Reducing font %s", iterator->swffont->name);
1190                 swf_FontReduce(iterator->swffont);
1191             }
1192             int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
1193             if(i->config_storeallcharacters || used) {
1194                 mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
1195                 swf_FontSetDefine2(mtag, iterator->swffont);
1196             }
1197         }
1198
1199         iterator = iterator->next;
1200     }
1201     i->tag = swf_InsertTag(i->tag,ST_END);
1202     TAG* tag = i->tag->prev;
1203
1204     /* remove the removeobject2 tags between the last ST_SHOWFRAME
1205        and the ST_END- they confuse the flash player  */
1206     while(tag->id == ST_REMOVEOBJECT2) {
1207         TAG* prev = tag->prev;
1208         swf_DeleteTag(tag);
1209         tag = prev;
1210     }
1211     
1212     if(i->overflow) {
1213         wipeSWF(i->swf);
1214     }
1215     if(i->config_enablezlib || i->config_flashversion>=6) {
1216         i->swf->compressed = 1;
1217     }
1218 }
1219
1220 int swfresult_save(gfxresult_t*gfx, char*filename)
1221 {
1222     SWF*swf = (SWF*)gfx->internal;
1223     int fi;
1224     if(filename)
1225      fi = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_WRONLY, 0777);
1226     else
1227      fi = 1; // stdout
1228     
1229     if(fi<=0) {
1230         msg("<fatal> Could not create \"%s\". ", FIXNULL(filename));
1231         return -1;
1232     }
1233     
1234     if(swf->compressed) {
1235         if FAILED(swf_WriteSWC(fi,swf)) 
1236             msg("<error> WriteSWC() failed.\n");
1237     } else {
1238         if FAILED(swf_WriteSWF(fi,swf)) 
1239             msg("<error> WriteSWF() failed.\n");
1240     }
1241
1242     if(filename)
1243      close(fi);
1244     return 0;
1245 }
1246 void* swfresult_get(gfxresult_t*gfx, char*name)
1247 {
1248     SWF*swf = (SWF*)gfx->internal;
1249     if(!strcmp(name, "swf")) {
1250         return (void*)swf_CopySWF(swf);
1251     } else if(!strcmp(name, "xmin")) {
1252         return (void*)(swf->movieSize.xmin/20);
1253     } else if(!strcmp(name, "ymin")) {
1254         return (void*)(swf->movieSize.ymin/20);
1255     } else if(!strcmp(name, "xmax")) {
1256         return (void*)(swf->movieSize.xmax/20);
1257     } else if(!strcmp(name, "ymax")) {
1258         return (void*)(swf->movieSize.ymax/20);
1259     } else if(!strcmp(name, "width")) {
1260         return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
1261     } else if(!strcmp(name, "height")) {
1262         return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
1263     }
1264     return 0;
1265 }
1266 void swfresult_destroy(gfxresult_t*gfx)
1267 {
1268     if(gfx->internal) {
1269         swf_FreeTags((SWF*)gfx->internal);
1270         free(gfx->internal);
1271         gfx->internal = 0;
1272     }
1273     memset(gfx, 0, sizeof(gfxresult_t));
1274     free(gfx);
1275 }
1276
1277 static void swfoutput_destroy(gfxdevice_t* dev);
1278
1279 gfxresult_t* swf_finish(gfxdevice_t* dev)
1280 {
1281     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1282     gfxresult_t*result;
1283
1284     if(i->config_linktarget) {
1285         free(i->config_linktarget);
1286         i->config_linktarget = 0;
1287     }
1288
1289     swfoutput_finalize(dev);
1290     SWF* swf = i->swf;i->swf = 0;
1291     swfoutput_destroy(dev);
1292
1293     result = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
1294     result->internal = swf;
1295     result->save = swfresult_save;
1296     result->write = 0;
1297     result->get = swfresult_get;
1298     result->destroy = swfresult_destroy;
1299     return result;
1300 }
1301
1302 /* Perform cleaning up */
1303 static void swfoutput_destroy(gfxdevice_t* dev) 
1304 {
1305     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1306     if(!i) {
1307         /* not initialized yet- nothing to destroy */
1308         return;
1309     }
1310
1311     fontlist_t *tmp,*iterator = i->fontlist;
1312     while(iterator) {
1313         if(iterator->swffont) {
1314             swf_FontFree(iterator->swffont);iterator->swffont=0;
1315         }
1316         tmp = iterator;
1317         iterator = iterator->next;
1318         free(tmp);
1319     }
1320     if(i->swf) {swf_FreeTags(i->swf);free(i->swf);i->swf = 0;}
1321
1322     free(i);i=0;
1323     memset(dev, 0, sizeof(gfxdevice_t));
1324 }
1325
1326 static void swfoutput_setfillcolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1327 {
1328     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1329     if(i->fillrgb.r == r &&
1330        i->fillrgb.g == g &&
1331        i->fillrgb.b == b &&
1332        i->fillrgb.a == a) return;
1333     if(i->shapeid>=0)
1334      endshape(dev);
1335
1336     i->fillrgb.r = r;
1337     i->fillrgb.g = g;
1338     i->fillrgb.b = b;
1339     i->fillrgb.a = a;
1340 }
1341
1342 static void swfoutput_setstrokecolor(gfxdevice_t* dev, U8 r, U8 g, U8 b, U8 a)
1343 {
1344     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1345     if(i->strokergb.r == r &&
1346        i->strokergb.g == g &&
1347        i->strokergb.b == b &&
1348        i->strokergb.a == a) return;
1349
1350     if(i->shapeid>=0)
1351      endshape(dev);
1352     i->strokergb.r = r;
1353     i->strokergb.g = g;
1354     i->strokergb.b = b;
1355     i->strokergb.a = a;
1356 }
1357
1358 //#define ROUND_UP 19
1359 //#define ROUND_UP 10
1360
1361 static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
1362 {
1363     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1364     if(i->linewidth == (U16)(_linewidth*20+19.0/20.0))
1365         return;
1366     if(i->shapeid>=0)
1367         endshape(dev);
1368     i->linewidth = (U16)(_linewidth*20+19.0/20.0);
1369 }
1370
1371
1372 static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
1373 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
1374 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
1375 static void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points);
1376
1377 void swfoutput_drawlink(gfxdevice_t*dev, char*url, gfxline_t*points)
1378 {
1379     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1380     dev->drawlink(dev, points, url);
1381 }
1382
1383 void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, char*url)
1384 {
1385     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1386
1387     if(!strncmp("http://pdf2swf:", url, 15)) {
1388         char*tmp = strdup(url);
1389         int l = strlen(tmp);
1390         if(tmp[l-1] == '/')
1391            tmp[l-1] = 0;
1392         swfoutput_namedlink(dev, tmp+15, points);
1393         free(tmp);
1394         return;
1395     } else if(!strncmp("page", url, 4)) {
1396         int t, nodigit=0;
1397         for(t=4;url[t];t++)
1398             if(url[t]<'0' || url[t]>'9')
1399                 nodigit = 1;
1400         if(!nodigit) {
1401             int page = atoi(&url[4]);
1402             if(page<0) page = 0;
1403             swfoutput_linktopage(dev, page, points);
1404         }
1405     } else {
1406         swfoutput_linktourl(dev, url, points);
1407     }
1408 }
1409 void swfoutput_linktourl(gfxdevice_t*dev, char*url, gfxline_t*points)
1410 {
1411     ActionTAG* actions = 0;
1412     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1413     if(i->shapeid>=0)
1414         endshape(dev);
1415     if(i->textid>=0)
1416         endtext(dev);
1417     
1418     if(i->config_externallinkfunction) {
1419         actions = action_PushString(actions, url); //parameter
1420         actions = action_PushInt(actions, 1); //number of parameters (1)
1421         actions = action_PushString(actions, i->config_externallinkfunction); //function name
1422         actions = action_CallFunction(actions);
1423     } else if(!i->config_linktarget) {
1424         if(!i->config_opennewwindow)
1425           actions = action_GetUrl(actions, url, "_parent");
1426         else
1427           actions = action_GetUrl(actions, url, "_this");
1428     } else {
1429         actions = action_GetUrl(actions, url, i->config_linktarget);
1430     }
1431     actions = action_End(actions);
1432     
1433     drawlink(dev, actions, 0, points,0);
1434 }
1435 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
1436 {
1437     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1438     ActionTAG* actions = 0;
1439
1440     if(i->shapeid>=0)
1441         endshape(dev);
1442     if(i->textid>=0)
1443         endtext(dev);
1444   
1445     if(!i->config_internallinkfunction) {
1446         actions = action_GotoFrame(actions, page-1);
1447         actions = action_End(actions);
1448     } else {
1449         actions = action_PushInt(actions, page); //parameter
1450         actions = action_PushInt(actions, 1); //number of parameters (1)
1451         actions = action_PushString(actions, i->config_internallinkfunction); //function name
1452         actions = action_CallFunction(actions);
1453         actions = action_End(actions);
1454     }
1455
1456     drawlink(dev, actions, 0, points,0);
1457 }
1458
1459 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
1460    of the viewer objects, like subtitles, index elements etc.
1461 */
1462 void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
1463 {
1464     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1465     ActionTAG *actions1,*actions2;
1466     char*tmp = strdup(name);
1467     char mouseover = 1;
1468
1469     if(i->shapeid>=0)
1470         endshape(dev);
1471     if(i->textid>=0)
1472         endtext(dev);
1473
1474     if(!strncmp(tmp, "call:", 5))
1475     {
1476         char*x = strchr(&tmp[5], ':');
1477         if(!x) {
1478             actions1 = action_PushInt(0, 0); //number of parameters (0)
1479             actions1 = action_PushString(actions1, &tmp[5]); //function name
1480             actions1 = action_CallFunction(actions1);
1481             actions1 = action_End(actions1);
1482         } else {
1483             *x = 0;
1484             actions1 = action_PushString(0, x+1); //parameter
1485             actions1 = action_PushInt(actions1, 1); //number of parameters (1)
1486             actions1 = action_PushString(actions1, &tmp[5]); //function name
1487             actions1 = action_CallFunction(actions1);
1488             actions1 = action_End(actions1);
1489         }
1490         actions2 = action_End(0);
1491         mouseover = 0;
1492     }
1493     else
1494     {
1495         actions1 = action_PushString(0, "/:subtitle");
1496         actions1 = action_PushString(actions1, name);
1497         actions1 = action_SetVariable(actions1);
1498         actions1 = action_End(actions1);
1499
1500         actions2 = action_PushString(0, "/:subtitle");
1501         actions2 = action_PushString(actions2, "");
1502         actions2 = action_SetVariable(actions2);
1503         actions2 = action_End(actions2);
1504     }
1505
1506     drawlink(dev, actions1, actions2, points,mouseover);
1507
1508     swf_ActionFree(actions1);
1509     swf_ActionFree(actions2);
1510     free(tmp);
1511 }
1512
1513 static void drawgfxline(gfxdevice_t*dev, gfxline_t*line)
1514 {
1515     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1516     gfxcoord_t lastx=0,lasty=0,px=0,py=0;
1517     char lastwasmoveto;
1518     int lines= 0, splines=0;
1519     while(1) {
1520         if(!line)
1521             break;
1522         /* check whether the next segment is zero */
1523         if(line->type == gfx_moveTo) {
1524             moveto(dev, i->tag, line->x, line->y);
1525             px = lastx = line->x;
1526             py = lasty = line->y;
1527             lastwasmoveto = 1;
1528         } if(line->type == gfx_lineTo) {
1529             lineto(dev, i->tag, line->x, line->y);
1530             px = line->x;
1531             py = line->y;
1532             lastwasmoveto = 0;
1533             lines++;
1534         } else if(line->type == gfx_splineTo) {
1535             plotxy_t s,p;
1536             s.x = line->sx;p.x = line->x;
1537             s.y = line->sy;p.y = line->y;
1538             splineto(dev, i->tag, s, p);
1539             px = line->x;
1540             py = line->y;
1541             lastwasmoveto = 0;
1542             splines++;
1543         }
1544         line = line->next;
1545     }
1546     msg("<trace> drawgfxline, %d lines, %d splines", lines, splines);
1547 }
1548
1549
1550 static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
1551 {
1552     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1553     RGBA rgb;
1554     SRECT r;
1555     int lsid=0;
1556     int fsid;
1557     plotxy_t p1,p2,p3,p4;
1558     int myshapeid;
1559     int myshapeid2;
1560     double posx = 0;
1561     double posy = 0;
1562     int t;
1563     int buttonid = getNewID(dev);
1564     gfxbbox_t bbox = gfxline_getbbox(points);
1565
1566     /* shape */
1567     myshapeid = getNewID(dev);
1568     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1569     swf_ShapeNew(&i->shape);
1570     rgb.r = rgb.b = rgb.a = rgb.g = 0; 
1571     fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1572     swf_SetU16(i->tag, myshapeid);
1573     r.xmin = (int)(bbox.xmin*20);
1574     r.ymin = (int)(bbox.ymin*20);
1575     r.xmax = (int)(bbox.xmax*20);
1576     r.ymax = (int)(bbox.ymax*20);
1577     swf_SetRect(i->tag,&r);
1578     swf_SetShapeStyles(i->tag,i->shape);
1579     swf_ShapeCountBits(i->shape,NULL,NULL);
1580     swf_SetShapeBits(i->tag,i->shape);
1581     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1582     i->swflastx = i->swflasty = 0;
1583     drawgfxline(dev, points);
1584     swf_ShapeSetEnd(i->tag);
1585
1586     /* shape2 */
1587     myshapeid2 = getNewID(dev);
1588     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
1589     swf_ShapeNew(&i->shape);
1590     
1591     rgb = i->config_linkcolor;
1592
1593     fsid = swf_ShapeAddSolidFillStyle(i->shape,&rgb);
1594     swf_SetU16(i->tag, myshapeid2);
1595     r.xmin = (int)(bbox.xmin*20);
1596     r.ymin = (int)(bbox.ymin*20);
1597     r.xmax = (int)(bbox.xmax*20);
1598     r.ymax = (int)(bbox.ymax*20);
1599     swf_SetRect(i->tag,&r);
1600     swf_SetShapeStyles(i->tag,i->shape);
1601     swf_ShapeCountBits(i->shape,NULL,NULL);
1602     swf_SetShapeBits(i->tag,i->shape);
1603     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
1604     i->swflastx = i->swflasty = 0;
1605     drawgfxline(dev, points);
1606     swf_ShapeSetEnd(i->tag);
1607
1608     if(!mouseover)
1609     {
1610         i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON);
1611         swf_SetU16(i->tag,buttonid); //id
1612         swf_ButtonSetFlags(i->tag, 0); //menu=no
1613         swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1614         swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1615         swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1616         swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1617         swf_SetU8(i->tag,0);
1618         swf_ActionSet(i->tag,actions1);
1619         swf_SetU8(i->tag,0);
1620     }
1621     else
1622     {
1623         i->tag = swf_InsertTag(i->tag,ST_DEFINEBUTTON2);
1624         swf_SetU16(i->tag,buttonid); //id
1625         swf_ButtonSetFlags(i->tag, 0); //menu=no
1626         swf_ButtonSetRecord(i->tag,0x01,myshapeid,i->depth,0,0);
1627         swf_ButtonSetRecord(i->tag,0x02,myshapeid2,i->depth,0,0);
1628         swf_ButtonSetRecord(i->tag,0x04,myshapeid2,i->depth,0,0);
1629         swf_ButtonSetRecord(i->tag,0x08,myshapeid,i->depth,0,0);
1630         swf_SetU8(i->tag,0); // end of button records
1631         swf_ButtonSetCondition(i->tag, BC_IDLE_OVERUP);
1632         swf_ActionSet(i->tag,actions1);
1633         if(actions2) {
1634             swf_ButtonSetCondition(i->tag, BC_OVERUP_IDLE);
1635             swf_ActionSet(i->tag,actions2);
1636             swf_SetU8(i->tag,0);
1637             swf_ButtonPostProcess(i->tag, 2);
1638         } else {
1639             swf_SetU8(i->tag,0);
1640             swf_ButtonPostProcess(i->tag, 1);
1641         }
1642     }
1643     
1644     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1645
1646     if(posx!=0 || posy!=0) {
1647         SPOINT p;
1648         p.x = (int)(posx*20);
1649         p.y = (int)(posy*20);
1650         p = swf_TurnPoint(p, &i->page_matrix);
1651         MATRIX m;
1652         m = i->page_matrix;
1653         m.tx = p.x;
1654         m.ty = p.y;
1655         swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,0);
1656     } else {
1657         swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,0);
1658     }
1659 }
1660
1661       
1662 ///////////
1663 /*
1664 for(t=0;t<picpos;t++)
1665       {
1666           if(pic_xids[t] == xid &&
1667              pic_yids[t] == yid) {
1668               width = pic_width[t];
1669               height = pic_height[t];
1670               found = t;break;
1671           }
1672       }
1673           pic_ids[picpos] = swfoutput_drawimagelosslessN(&output, pic, pal, width, height, x1,y1,x2,y2,x3,y3,x4,y4, numpalette);
1674           pic_xids[picpos] = xid;
1675           pic_yids[picpos] = yid;
1676           pic_width[picpos] = width;
1677           pic_height[picpos] = height;
1678           if(picpos<1024)
1679               picpos++;
1680             pic[width*y+x] = buf[0];
1681             xid+=x*buf[0]+1;
1682             yid+=y*buf[0]*3+1;
1683       
1684             xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1685       yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1686       
1687       int xid = 0;
1688       int yid = 0;
1689           xid += x*r+x*b*3+x*g*7+x*a*11;
1690           yid += y*r*3+y*b*17+y*g*19+y*a*11;
1691       int t,found = -1;
1692       for(t=0;t<picpos;t++)
1693       {
1694           if(pic_xids[t] == xid &&
1695              pic_yids[t] == yid) {
1696               found = t;break;
1697           }
1698       }
1699       if(found<0) {
1700 */
1701 ///////////
1702
1703
1704 int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
1705 {
1706     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1707
1708     msg("<trace> swfdevice: %s=%s", name, value);
1709     if(!strcmp(name, "jpegsubpixels")) {
1710         i->config_jpegsubpixels = atof(value);
1711     } else if(!strcmp(name, "ppmsubpixels")) {
1712         i->config_ppmsubpixels = atof(value);
1713     } else if(!strcmp(name, "subpixels")) {
1714         i->config_ppmsubpixels = i->config_jpegsubpixels = atof(value);
1715     } else if(!strcmp(name, "drawonlyshapes")) {
1716         i->config_drawonlyshapes = atoi(value);
1717     } else if(!strcmp(name, "ignoredraworder")) {
1718         i->config_ignoredraworder = atoi(value);
1719     } else if(!strcmp(name, "mark")) {
1720         if(!value || !value[0]) {
1721             if(i->mark) free(i->mark);
1722             i->mark = 0;
1723         } else {
1724             int t;
1725             i->mark = strdup("...");
1726             for(t=0;t<3;t++) if(value[t]) i->mark[t] = value[t];
1727         }
1728     } else if(!strcmp(name, "filloverlap")) {
1729         i->config_filloverlap = atoi(value);
1730     } else if(!strcmp(name, "linksopennewwindow")) {
1731         i->config_opennewwindow = atoi(value);
1732     } else if(!strcmp(name, "opennewwindow")) {
1733         i->config_opennewwindow = atoi(value);
1734     } else if(!strcmp(name, "storeallcharacters")) {
1735         i->config_storeallcharacters = atoi(value);
1736     } else if(!strcmp(name, "enablezlib")) {
1737         i->config_enablezlib = atoi(value);
1738     } else if(!strcmp(name, "bboxvars")) {
1739         i->config_bboxvars = atoi(value);
1740     } else if(!strcmp(name, "internallinkfunction")) {
1741         i->config_internallinkfunction = strdup(value);
1742     } else if(!strcmp(name, "externallinkfunction")) {
1743         i->config_externallinkfunction = strdup(value);
1744     } else if(!strcmp(name, "disable_polygon_conversion")) {
1745         i->config_disable_polygon_conversion = atoi(value);
1746     } else if(!strcmp(name, "insertstop")) {
1747         i->config_insertstoptag = atoi(value);
1748     } else if(!strcmp(name, "protect")) {
1749         i->config_protect = atoi(value);
1750         if(i->config_protect && i->tag) {
1751             i->tag = swf_InsertTag(i->tag, ST_PROTECT);
1752         }
1753     } else if(!strcmp(name, "faketags")) {
1754         i->config_generate_fake_tags = atoi(value);
1755     } else if(!strcmp(name, "flashversion")) {
1756         i->config_flashversion = atoi(value);
1757         if(i->swf) {
1758             i->swf->fileVersion = i->config_flashversion;
1759         }
1760     } else if(!strcmp(name, "minlinewidth")) {
1761         i->config_minlinewidth = atof(value);
1762     } else if(!strcmp(name, "caplinewidth")) {
1763         i->config_caplinewidth = atof(value);
1764     } else if(!strcmp(name, "linktarget")) {
1765         i->config_linktarget = strdup(value);
1766     } else if(!strcmp(name, "dumpfonts")) {
1767         i->config_dumpfonts = atoi(value);
1768     } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
1769         i->jpeg = 1;
1770     } else if(!strcmp(name, "jpegquality")) {
1771         int val = atoi(value);
1772         if(val<0) val=0;
1773         if(val>100) val=100;
1774         i->config_jpegquality = val;
1775     } else if(!strcmp(name, "splinequality")) {
1776         int v = atoi(value);
1777         v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
1778         if(v<1) v = 1;
1779         i->config_splinemaxerror = v;
1780     } else if(!strcmp(name, "fontquality")) {
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_fontsplinemaxerror = v;
1785     } else if(!strcmp(name, "linkcolor")) {
1786         if(strlen(value)!=8) {
1787             fprintf(stderr, "Unknown format for option 'linkcolor'. (%s <-> RRGGBBAA)\n", value);
1788             return 1;
1789         }
1790 #       define NIBBLE(s) (((s)>='0' && (s)<='9')?((s)-'0'):((s)&0x0f)+9)
1791         i->config_linkcolor.r = NIBBLE(value[0])<<4 | NIBBLE(value[1]);
1792         i->config_linkcolor.g = NIBBLE(value[2])<<4 | NIBBLE(value[3]);
1793         i->config_linkcolor.b = NIBBLE(value[4])<<4 | NIBBLE(value[5]);
1794         i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
1795         printf("%02x%02x%02x%02x\n", 
1796             i->config_linkcolor.r,
1797             i->config_linkcolor.g,
1798             i->config_linkcolor.b,
1799             i->config_linkcolor.a);
1800
1801     } else {
1802         fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
1803         return 0;
1804     }
1805     return 1;
1806 }
1807
1808 // --------------------------------------------------------------------
1809
1810 static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
1811 {
1812     CXFORM cx;
1813     swf_GetCXForm(0, &cx, 1);
1814     if(!c)
1815         return cx;
1816     if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
1817        c->gr!=0 || c->gb!=0 || c->ga!=0 ||
1818        c->br!=0 || c->bg!=0 || c->ba!=0 ||
1819        c->ar!=0 || c->ag!=0 || c->ab!=0)
1820         msg("<warning> CXForm not SWF-compatible");
1821
1822     cx.a0 = (S16)(c->aa*256);
1823     cx.r0 = (S16)(c->rr*256);
1824     cx.g0 = (S16)(c->gg*256);
1825     cx.b0 = (S16)(c->bb*256);
1826     cx.a1 = c->t.a;
1827     cx.r1 = c->t.r;
1828     cx.g1 = c->t.g;
1829     cx.b1 = c->t.b;
1830     return cx;
1831 }
1832
1833 /* TODO */
1834 static int imageInCache(gfxdevice_t*dev, void*data, int width, int height)
1835 {
1836     return -1;
1837 }
1838 static void addImageToCache(gfxdevice_t*dev, void*data, int width, int height)
1839 {
1840 }
1841     
1842 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
1843 {
1844     gfxdevice_t*dev = i->dev;
1845     RGBA*newpic = 0;
1846     RGBA*mem = (RGBA*)img->data;
1847     
1848     int sizex = img->width;
1849     int sizey = img->height;
1850     int is_jpeg = i->jpeg;
1851     i->jpeg = 0;
1852
1853     int newsizex=sizex, newsizey=sizey;
1854
1855     /// {
1856     if(is_jpeg && i->config_jpegsubpixels) {
1857         newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
1858         newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
1859     } else if(!is_jpeg && i->config_ppmsubpixels) {
1860         newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
1861         newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
1862     }
1863     /// }
1864
1865     if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
1866         return -1;
1867
1868     /* TODO: cache images */
1869     
1870     if(newsizex<sizex || newsizey<sizey) {
1871         msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
1872         newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
1873         *newwidth = sizex = newsizex;
1874         *newheight  = sizey = newsizey;
1875         mem = newpic;
1876     } else {
1877         *newwidth = newsizex = sizex;
1878         *newheight = newsizey  = sizey;
1879     }
1880
1881     int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
1882     int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
1883     
1884     msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
1885             sizex, sizey, 
1886             has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"", 
1887             is_jpeg?"jpeg-":"",
1888             newsizex, newsizey,
1889             targetwidth, targetheight,
1890             /*newsizex, newsizey,*/
1891             num_colors>256?">":"", num_colors>256?256:num_colors);
1892
1893     /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
1894     swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
1895     int t;
1896     for(t=0;t<num_colors;t++) {
1897         printf("%02x%02x%02x%02x ",
1898                 pal[t].r, pal[t].g, pal[t].b, pal[t].a);
1899         if((t&7)==7)
1900             printf("\n");
1901     }
1902     printf("\n");*/
1903
1904     int bitid = -1;
1905     int cacheid = imageInCache(dev, mem, sizex, sizey);
1906
1907     if(cacheid<=0) {
1908         bitid = getNewID(dev);
1909         i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
1910         addImageToCache(dev, mem, sizex, sizey);
1911     } else {
1912         bitid = cacheid;
1913     }
1914
1915     if(newpic)
1916         free(newpic);
1917     return bitid;
1918 }
1919
1920 static SRECT gfxline_getSWFbbox(gfxline_t*line)
1921 {
1922     gfxbbox_t bbox = gfxline_getbbox(line);
1923     SRECT r;
1924     r.xmin = (int)(bbox.xmin*20);
1925     r.ymin = (int)(bbox.ymin*20);
1926     r.xmax = (int)(bbox.xmax*20);
1927     r.ymax = (int)(bbox.ymax*20);
1928     return r;
1929 }
1930
1931 int line_is_empty(gfxline_t*line)
1932 {
1933     while(line) {
1934         if(line->type != gfx_moveTo)
1935             return 0;
1936         line = line->next;
1937     }
1938     return 1;
1939 }
1940
1941 static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
1942 {
1943     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1944     
1945     if(line_is_empty(line))
1946         return;
1947
1948     endshape(dev);
1949     endtext(dev);
1950
1951     int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
1952     int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
1953
1954     int newwidth=0,newheight=0;
1955     int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
1956     if(bitid<0)
1957         return;
1958     double fx = (double)img->width / (double)newwidth;
1959     double fy = (double)img->height / (double)newheight;
1960
1961     MATRIX m;
1962     float m00,m10,tx;
1963     float m01,m11,ty;
1964     m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
1965     m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
1966     m.tx = (int)(matrix->tx*20);
1967     m.ty = (int)(matrix->ty*20);
1968   
1969     /* shape */
1970     int myshapeid = getNewID(dev);
1971     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
1972     SHAPE*shape;
1973     swf_ShapeNew(&shape);
1974     int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
1975     swf_SetU16(i->tag, myshapeid);
1976     SRECT r = gfxline_getSWFbbox(line);
1977     swf_SetRect(i->tag,&r);
1978     swf_SetShapeStyles(i->tag,shape);
1979     swf_ShapeCountBits(shape,NULL,NULL);
1980     swf_SetShapeBits(i->tag,shape);
1981     swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
1982     i->swflastx = i->swflasty = UNDEFINED_COORD;
1983     drawgfxline(dev, line);
1984     swf_ShapeSetEnd(i->tag);
1985     swf_ShapeFree(shape);
1986
1987     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
1988     CXFORM cxform2 = gfxcxform_to_cxform(cxform);
1989     swf_ObjectPlace(i->tag,myshapeid,getNewDepth(dev),&i->page_matrix,&cxform2,NULL);
1990 }
1991
1992 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
1993 {
1994     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
1995
1996     endtext(dev);
1997     endshape(dev);
1998
1999     if(i->clippos >= 127)
2000     {
2001         msg("<warning> Too many clip levels.");
2002         i->clippos --;
2003     } 
2004
2005     int myshapeid = getNewID(dev);
2006     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
2007     RGBA col;
2008     memset(&col, 0, sizeof(RGBA));
2009     SHAPE*shape;
2010     swf_ShapeNew(&shape);
2011     int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
2012     if(i->mark) {
2013         RGBA markcol = {0,i->mark[0],i->mark[1],i->mark[2]};
2014         swf_ShapeAddSolidFillStyle(shape,&markcol);
2015     }
2016     swf_SetU16(i->tag,myshapeid);
2017     SRECT r = gfxline_getSWFbbox(line);
2018     swf_SetRect(i->tag,&r);
2019     swf_SetShapeStyles(i->tag,shape);
2020     swf_ShapeCountBits(shape,NULL,NULL);
2021     swf_SetShapeBits(i->tag,shape);
2022     swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
2023     i->swflastx = i->swflasty = UNDEFINED_COORD;
2024     drawgfxline(dev, line);
2025     swf_ShapeSetEnd(i->tag);
2026     swf_ShapeFree(shape);
2027
2028     /* TODO: remember the bbox, and check all shapes against it */
2029     
2030     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
2031     i->cliptags[i->clippos] = i->tag;
2032     i->clipshapes[i->clippos] = myshapeid;
2033     i->clipdepths[i->clippos] = getNewDepth(dev);
2034     i->clippos++;
2035 }
2036
2037 static void swf_endclip(gfxdevice_t*dev)
2038 {
2039     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2040     if(i->textid>=0)
2041         endtext(dev);
2042     if(i->shapeid>=0)
2043         endshape(dev);
2044
2045     if(!i->clippos) {
2046         msg("<error> Invalid end of clipping region");
2047         return;
2048     }
2049     i->clippos--;
2050     /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
2051             / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
2052     i->depth ++;*/
2053     swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
2054 }
2055 static int gfxline_type(gfxline_t*line)
2056 {
2057     int tmplines=0;
2058     int tmpsplines=0;
2059     int lines=0;
2060     int splines=0;
2061     int haszerosegments=0;
2062     int length=0;
2063     while(line) {
2064         if(line->type == gfx_moveTo) {
2065             tmplines=0;
2066             tmpsplines=0;
2067         } else if(line->type == gfx_lineTo) {
2068             tmplines++;
2069             if(tmplines>lines)
2070                 lines=tmplines;
2071         } else if(line->type == gfx_splineTo) {
2072             tmpsplines++;
2073             if(tmpsplines>lines)
2074                 splines=tmpsplines;
2075         }
2076         length++;
2077         line = line->next;
2078     }
2079     if(length>400)
2080         return 5;
2081     if(lines==0 && splines==0) return 0;
2082     else if(lines==1 && splines==0) return 1;
2083     else if(lines==0 && splines==1) return 2;
2084     else if(splines==0) return 3;
2085     else return 4;
2086 }
2087
2088 static int gfxline_has_dots(gfxline_t*line)
2089 {
2090     int tmplines=0;
2091     double x=0,y=0;
2092     double dist = 0;
2093     int isline = 0;
2094     int short_gap = 0;
2095     while(line) {
2096         if(line->type == gfx_moveTo) {
2097             /* test the length of the preceding line, and assume it is a dot if
2098                it's length is less than 1.0. But *only* if there's a noticable 
2099                gap between the previous line and the next moveTo. (I've come
2100                across a PDF where thousands of "dots" were stringed together,
2101                forming a line) */
2102             int last_short_gap = short_gap;
2103             if((fabs(line->x - x) + fabs(line->y - y)) < 1.0) {
2104                 short_gap = 1;
2105             } else {
2106                 short_gap = 0;
2107             }
2108             if(isline && dist < 1 && !short_gap && !last_short_gap) {
2109                 return 1;
2110             }
2111             dist = 0;
2112             isline = 0;
2113         } else if(line->type == gfx_lineTo) {
2114             dist += fabs(line->x - x) + fabs(line->y - y);
2115             isline = 1;
2116         } else if(line->type == gfx_splineTo) {
2117             dist += fabs(line->sx - x) + fabs(line->sy - y) + 
2118                     fabs(line->x - line->sx) + fabs(line->y - line->sy);
2119             isline = 1;
2120         }
2121         x = line->x;
2122         y = line->y;
2123         line = line->next;
2124     }
2125     if(isline && dist < 1 && !short_gap) {
2126         return 1;
2127     }
2128     return 0;
2129 }
2130
2131 static int gfxline_fix_short_edges(gfxline_t*line)
2132 {
2133     double x,y;
2134     while(line) {
2135         if(line->type == gfx_lineTo) {
2136             if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
2137                 line->x += 0.01;
2138             }
2139         } else if(line->type == gfx_splineTo) {
2140             if(fabs(line->sx - x) + fabs(line->sy - y) + 
2141                fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
2142                 line->x += 0.01;
2143             }
2144         }
2145         x = line->x;
2146         y = line->y;
2147         line = line->next;
2148     }
2149     return 0;
2150 }
2151
2152 static char is_inside_page(gfxdevice_t*dev, gfxcoord_t x, gfxcoord_t y)
2153 {
2154     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2155     if(x<i->min_x || x>i->max_x) return 0;
2156     if(y<i->min_y || y>i->max_y) return 0;
2157     return 1;
2158 }
2159
2160 gfxline_t* gfxline_move(gfxline_t*line, double x, double y)
2161 {
2162     gfxline_t*l = line = gfxline_clone(line);
2163
2164     while(l) {
2165         l->x += x;
2166         l->y += y;
2167         l->sx += x;
2168         l->sy += y;
2169         l = l->next;
2170     }
2171     return line;
2172 }
2173
2174 //#define NORMALIZE_POLYGON_POSITIONS
2175
2176 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)
2177 {
2178     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2179     if(line_is_empty(line))
2180         return;
2181     int type = gfxline_type(line);
2182     int has_dots = gfxline_has_dots(line);
2183     gfxbbox_t r = gfxline_getbbox(line);
2184     int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2185
2186     /* TODO: * split line into segments, and perform this check for all segments */
2187
2188     if(i->config_disable_polygon_conversion || type>=5 ||
2189        (!has_dots &&
2190         (width <= i->config_caplinewidth 
2191         || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
2192         || (cap_style == gfx_capRound && type<=2)))) {} else 
2193     {
2194         /* convert line to polygon */
2195         msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
2196         if(has_dots)
2197             gfxline_fix_short_edges(line);
2198         /* we need to convert the line into a polygon */
2199         ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
2200         gfxline_t*gfxline = SVPtogfxline(svp);
2201         dev->fill(dev, gfxline, color);
2202         free(gfxline);
2203         art_svp_free(svp);
2204         return;
2205     }
2206
2207     msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
2208     endtext(dev);
2209
2210 #ifdef NORMALIZE_POLYGON_POSITIONS
2211     endshape(dev);
2212     double startx = 0, starty = 0;
2213     if(line && line->type == gfx_moveTo) {
2214         startx = line->x;
2215         starty = line->y;
2216     }
2217     line = gfxline_move(line, -startx, -starty);
2218     i->shapeposx = (int)(startx*20);
2219     i->shapeposy = (int)(starty*20);
2220 #endif
2221
2222     swfoutput_setstrokecolor(dev, color->r, color->g, color->b, color->a);
2223     swfoutput_setlinewidth(dev, width);
2224     startshape(dev);
2225     stopFill(dev);
2226     drawgfxline(dev, line);
2227
2228 #ifdef NORMALIZE_POLYGON_POSITIONS
2229     free(line); //account for _move
2230 #endif
2231
2232 }
2233 static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
2234 {
2235     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2236     if(line_is_empty(line))
2237         return;
2238     if(!color->a)
2239         return;
2240     gfxbbox_t r = gfxline_getbbox(line);
2241     int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
2242
2243     endtext(dev);
2244     if(!i->config_ignoredraworder)
2245         endshape(dev);
2246
2247 #ifdef NORMALIZE_POLYGON_POSITIONS
2248     endshape(dev);
2249     double startx = 0, starty = 0;
2250     if(line && line->type == gfx_moveTo) {
2251         startx = line->x;
2252         starty = line->y;
2253     }
2254     line = gfxline_move(line, -startx, -starty);
2255     i->shapeposx = (int)(startx*20);
2256     i->shapeposy = (int)(starty*20);
2257 #endif
2258
2259     swfoutput_setfillcolor(dev, color->r, color->g, color->b, color->a);
2260     startshape(dev);
2261     startFill(dev);
2262     i->fill=1;
2263     drawgfxline(dev, line);
2264     msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
2265
2266 #ifdef NORMALIZE_POLYGON_POSITIONS
2267     free(line); //account for _move
2268 #endif
2269 }
2270 static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
2271 {
2272     if(line_is_empty(line))
2273         return;
2274     msg("<error> Gradient filling not implemented yet");
2275 }
2276
2277 static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, char* id)
2278 {
2279     SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
2280     int t;
2281     SRECT bounds = {0,0,0,0};
2282     swffont->id = -1;
2283     swffont->version = 2;
2284     swffont->name = (U8*)strdup(id);
2285     swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
2286     swffont->layout->ascent = 0; /* ? */
2287     swffont->layout->descent = 0;
2288     swffont->layout->leading = 0;
2289     swffont->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*font->num_glyphs);
2290     swffont->encoding = FONT_ENCODING_UNICODE;
2291     swffont->numchars = font->num_glyphs;
2292     swffont->maxascii = font->max_unicode;
2293     swffont->ascii2glyph = (int*)rfx_calloc(sizeof(int)*swffont->maxascii);
2294     swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
2295     swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
2296     swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
2297     for(t=0;t<font->max_unicode;t++) {
2298         swffont->ascii2glyph[t] = font->unicode2glyph[t];
2299     }
2300     for(t=0;t<font->num_glyphs;t++) {
2301         drawer_t draw;
2302         gfxline_t*line;
2303         int advance = 0;
2304         swffont->glyph2ascii[t] = font->glyphs[t].unicode;
2305         if(font->glyphs[t].name) {
2306             swffont->glyphnames[t] = strdup(font->glyphs[t].name);
2307         } else {
2308             swffont->glyphnames[t] = 0;
2309         }
2310         advance = (int)(font->glyphs[t].advance);
2311
2312         swf_Shape01DrawerInit(&draw, 0);
2313         line = font->glyphs[t].line;
2314         while(line) {
2315             FPOINT c,to;
2316             c.x = line->sx; c.y = line->sy;
2317             to.x = line->x; to.y = line->y;
2318             if(line->type == gfx_moveTo) {
2319                 draw.moveTo(&draw, &to);
2320             } else if(line->type == gfx_lineTo) {
2321                 draw.lineTo(&draw, &to);
2322             } else if(line->type == gfx_splineTo) {
2323                 draw.splineTo(&draw, &c, &to);
2324             }
2325             line = line->next;
2326         }
2327         draw.finish(&draw);
2328         swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
2329         swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
2330
2331         if(swffont->layout->bounds[t].xmax && swffont->layout->bounds[t].xmax*2 < advance) {
2332             printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", swffont->layout->bounds[t].xmax, advance, font->glyphs[t].advance);
2333             advance = swffont->layout->bounds[t].xmax;
2334         }
2335             
2336         if(advance<32768) {
2337             swffont->glyph[t].advance = advance;
2338         } else {
2339             swffont->glyph[t].advance = 32767;
2340         }
2341
2342         draw.dealloc(&draw);
2343
2344         swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
2345     }
2346     if(bounds.ymin < 0 && bounds.ymax > 0) {
2347         swffont->layout->ascent = -bounds.ymin;
2348         swffont->layout->descent = bounds.ymax;
2349         swffont->layout->leading = bounds.ymax - bounds.ymin;
2350     } else {
2351         swffont->layout->ascent = (bounds.ymax - bounds.ymin)/2;
2352         swffont->layout->descent = (bounds.ymax - bounds.ymin)/2;
2353         swffont->layout->leading = bounds.ymax - bounds.ymin;
2354     }
2355
2356     return swffont;
2357 }
2358
2359 static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
2360 {
2361     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2362
2363     if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,font->id))
2364         return; // the requested font is the current font
2365     
2366     fontlist_t*last=0,*l = i->fontlist;
2367     while(l) {
2368         last = l;
2369         if(!strcmp((char*)l->swffont->name, font->id)) {
2370             return; // we already know this font
2371         }
2372         l = l->next;
2373     }
2374     l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
2375     l->swffont = gfxfont_to_swffont(font, font->id);
2376     l->next = 0;
2377     if(last) {
2378         last->next = l;
2379     } else {
2380         i->fontlist = l;
2381     }
2382     swf_FontSetID(l->swffont, getNewID(i->dev));
2383
2384     if(getScreenLogLevel() >= LOGLEVEL_DEBUG)  {
2385         int iii;
2386         // print font information
2387         msg("<debug> Font %s",font->id);
2388         msg("<debug> |   ID: %d", l->swffont->id);
2389         msg("<debug> |   Version: %d", l->swffont->version);
2390         msg("<debug> |   Name: %s", l->swffont->name);
2391         msg("<debug> |   Numchars: %d", l->swffont->numchars);
2392         msg("<debug> |   Maxascii: %d", l->swffont->maxascii);
2393         msg("<debug> |   Style: %d", l->swffont->style);
2394         msg("<debug> |   Encoding: %d", l->swffont->encoding);
2395         for(iii=0; iii<l->swffont->numchars;iii++) {
2396             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, 
2397                     l->swffont->layout->bounds[iii].xmin/20.0,
2398                     l->swffont->layout->bounds[iii].ymin/20.0,
2399                     l->swffont->layout->bounds[iii].xmax/20.0,
2400                     l->swffont->layout->bounds[iii].ymax/20.0
2401                     );
2402             int t;
2403             for(t=0;t<l->swffont->maxascii;t++) {
2404                 if(l->swffont->ascii2glyph[t] == iii)
2405                     msg("<debug> | - maps to %d",t);
2406             }
2407         }
2408     }
2409 }
2410
2411 static void swf_switchfont(gfxdevice_t*dev, char*fontid)
2412 {
2413     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2414
2415     if(i->swffont && i->swffont->name && !strcmp((char*)i->swffont->name,fontid))
2416         return; // the requested font is the current font
2417     
2418     fontlist_t*l = i->fontlist;
2419     while(l) {
2420         if(!strcmp((char*)l->swffont->name, fontid)) {
2421             i->swffont = l->swffont;
2422             return; //done!
2423         }
2424         l = l->next;
2425     }
2426     msg("<error> Unknown font id: %s", fontid);
2427     return;
2428 }
2429
2430 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
2431 {
2432     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
2433         
2434     if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
2435     {
2436         /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
2437                  with multiple fonts */
2438         endtext(dev);
2439
2440         swf_switchfont(dev, font->id); // set the current font
2441     }
2442     swfoutput_setfontmatrix(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11);
2443    
2444     swfmatrix_t m;
2445     m.m11 = i->fontm11;
2446     m.m12 = i->fontm12;
2447     m.m21 = i->fontm21;
2448     m.m22 = i->fontm22;
2449     m.m31 = matrix->tx;
2450     m.m32 = matrix->ty;
2451   
2452 /*    printf("%f %f\n", m.m31,  m.m32);
2453     {
2454     static int xpos = 40;
2455     static int ypos = 200;
2456     m.m31 = xpos;
2457     m.m32 = ypos;
2458     xpos += 10;
2459     }*/
2460
2461
2462     drawchar(dev, i->swffont, glyph, &m, color);
2463 }