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