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