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