fixed a few bugs in remove_font_transforms filter
[swftools.git] / lib / devices / swf.c
index 9f1c224..b090c7b 100644 (file)
 #include "../rfxswf.h"
 #include "../gfxdevice.h"
 #include "../gfxtools.h"
-#include "../art/libart.h"
 #include "swf.h"
 #include "../gfxpoly.h"
+#include "../gfximage.h"
 
-#define CHARDATAMAX 8192
+#define CHARDATAMAX 1024
 #define CHARMIDX 0
 #define CHARMIDY 0
 
-typedef struct _chardata {
+typedef struct _charatposition {
     int charid;
-    int fontid; /* TODO: use a SWFFONT instead */
+    SWFFONT*font;
     int x;
     int y;
     int size;
     RGBA color;
-} chardata_t;
+} charatposition_t;
+
+typedef struct _chararray {
+    charatposition_t chr[CHARDATAMAX+1];
+    int pos;
+    struct _chararray *next;
+} chararray_t;
+
+typedef struct _charbuffer {
+    MATRIX matrix;
+    chararray_t*array;
+    chararray_t*last;
+    struct _charbuffer *next;
+} charbuffer_t;
 
 typedef struct _fontlist
 {
@@ -76,15 +89,22 @@ typedef struct _swfoutput_internal
     double config_dumpfonts;
     double config_ppmsubpixels;
     double config_jpegsubpixels;
+    char hasbuttons;
+    int config_invisibletexttofront;
+    int config_dots;
     int config_simpleviewer;
     int config_opennewwindow;
     int config_ignoredraworder;
     int config_drawonlyshapes;
+    int config_frameresets;
+    int config_linknameurl;
     int config_jpegquality;
     int config_storeallcharacters;
     int config_enablezlib;
     int config_insertstoptag;
+    int config_showimages;
     int config_watermark;
+    int config_noclips;
     int config_flashversion;
     int config_reordertags;
     int config_showclipshapes;
@@ -95,6 +115,8 @@ typedef struct _swfoutput_internal
     int config_bboxvars;
     int config_disable_polygon_conversion;
     int config_normalize_polygon_positions;
+    int config_alignfonts;
+    char config_disablelinks;
     RGBA config_linkcolor;
     float config_minlinewidth;
     double config_caplinewidth;
@@ -114,13 +136,14 @@ typedef struct _swfoutput_internal
 
     TAG *tag;
     int currentswfid;
+    int startids;
     int depth;
     int startdepth;
     int linewidth;
     
     SHAPE* shape;
     int shapeid;
-    int textid;
+    int textmode;
 
     int watermarks;
     
@@ -139,12 +162,14 @@ typedef struct _swfoutput_internal
     int clippos;
 
     /* image cache */
+    /*
     int pic_xids[1024];
     int pic_yids[1024];
     int pic_ids[1024];
     int pic_width[1024];
     int pic_height[1024];
     int picpos;
+    */
 
     int frameno;
     int lastframeno;
@@ -158,8 +183,9 @@ typedef struct _swfoutput_internal
 
     SRECT pagebbox;
 
-    chardata_t chardata[CHARDATAMAX];
-    int chardatapos;
+    charbuffer_t* chardata;
+    charbuffer_t* topchardata; //chars supposed to be above everything else
+
     int firstpage;
     char pagefinished;
 
@@ -179,6 +205,8 @@ typedef struct _swfoutput_internal
     char* mark;
 
 } swfoutput_internal;
+
+static const int NO_FONT3=0;
     
 static void swf_fillbitmap(gfxdevice_t*driver, gfxline_t*line, gfximage_t*img, gfxmatrix_t*move, gfxcxform_t*cxform);
 static int  swf_setparameter(gfxdevice_t*driver, const char*key, const char*value);
@@ -194,6 +222,10 @@ static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font);
 static void swf_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action);
 static void swf_startframe(gfxdevice_t*dev, int width, int height);
 static void swf_endframe(gfxdevice_t*dev);
+static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
+static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
+static void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points);
+
 static gfxresult_t* swf_finish(gfxdevice_t*driver);
 
 static swfoutput_internal* init_internal_struct()
@@ -208,7 +240,7 @@ static swfoutput_internal* init_internal_struct()
     i->startdepth = 0;
     i->linewidth = 0;
     i->shapeid = -1;
-    i->textid = -1;
+    i->textmode = 0;
     i->frameno = 0;
     i->lastframeno = 0;
 
@@ -226,10 +258,11 @@ static swfoutput_internal* init_internal_struct()
     i->fillstylechanged = 0;
 
     i->bboxrectpos = -1;
-    i->chardatapos = 0;
+    i->chardata = 0;
     i->firstpage = 1;
     i->pagefinished = 1;
 
+    i->config_disablelinks=0;
     i->config_dumpfonts=0;
     i->config_ppmsubpixels=0;
     i->config_jpegsubpixels=0;
@@ -238,6 +271,7 @@ static swfoutput_internal* init_internal_struct()
     i->config_drawonlyshapes=0;
     i->config_jpegquality=85;
     i->config_storeallcharacters=0;
+    i->config_dots=1;
     i->config_enablezlib=0;
     i->config_insertstoptag=0;
     i->config_flashversion=6;
@@ -254,6 +288,7 @@ static swfoutput_internal* init_internal_struct()
     i->config_internallinkfunction=0;
     i->config_externallinkfunction=0;
     i->config_reordertags=1;
+    i->config_linknameurl=0;
 
     i->config_linkcolor.r = i->config_linkcolor.g = i->config_linkcolor.b = 255;
     i->config_linkcolor.a = 0x40;
@@ -302,12 +337,38 @@ typedef struct _plotxy
     double x,y;
 } plotxy_t;
 
+static inline int twipsnap(double f)
+{
+    /* if(f < -0x40000000/20.0) {
+       fprintf(stderr, "Warning: Coordinate underflow (%f)\n", f);
+       f = -0x40000000/20.0;
+    } else if(f>0x3fffffff/20.0) {
+       fprintf(stderr, "Warning: Coordinate overflow (%f)\n", f);
+       f = 0x3fffffff/20.0;
+    }*/
+
+    /* clamp coordinates to a rectangle with the property that we
+       can represent a line from the upper left corner to the upper
+       right corner using no more than 64 strokes */
+    const double min = -(1<<(18+4))/20.0;
+    const double max = ((1<<(18+4))-1)/20.0;
+    if(f < min) {
+       fprintf(stderr, "Warning: Coordinate underflow (%f)\n", f);
+       f = min;
+    } else if(f>max) {
+       fprintf(stderr, "Warning: Coordinate overflow (%f)\n", f);
+       f = max;
+    }
+
+    return (int)(f*20);
+}
+
 // write a move-to command into the swf
 static int movetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    int rx = (int)(p0.x*20);
-    int ry = (int)(p0.y*20);
+    int rx = twipsnap(p0.x);
+    int ry = twipsnap(p0.y);
     if(rx!=i->swflastx || ry!=i->swflasty || i->fillstylechanged) {
       swf_ShapeSetMove (tag, i->shape, rx,ry);
       i->fillstylechanged = 0;
@@ -369,18 +430,24 @@ static void addPointToBBox(gfxdevice_t*dev, int px, int py)
 static void linetoxy(gfxdevice_t*dev, TAG*tag, plotxy_t p0)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    int px = (int)(p0.x*20);
-    int py = (int)(p0.y*20);
+    int px = twipsnap(p0.x);
+    int py = twipsnap(p0.y);
     int rx = (px-i->swflastx);
     int ry = (py-i->swflasty);
     if(rx|ry) {
        swf_ShapeSetLine (tag, i->shape, rx,ry);
        addPointToBBox(dev, i->swflastx,i->swflasty);
        addPointToBBox(dev, px,py);
-    }/* else if(!i->fill) {
+    } /* this is a nice idea, but doesn't work with current flash
+         players (the pixel will be invisible if they're not
+         precisely on a pixel boundary) 
+         Besides, we should only do this if this lineto itself
+         is again followed by a "move".
+         else if(!i->fill && i->config_dots) {
        // treat lines of length 0 as plots, making them
        // at least 1 twip wide so Flash will display them
-       plot(dev, i->swflastx, i->swflasty, tag);
+       //plot(dev, i->swflastx, i->swflasty, tag);
+       swf_ShapeSetLine (tag, i->shape, rx+1,ry);
     }*/
 
     i->shapeisempty = 0;
@@ -402,12 +469,12 @@ static void splineto(gfxdevice_t*dev, TAG*tag, plotxy_t control,plotxy_t end)
     int lastlastx = i->swflastx;
     int lastlasty = i->swflasty;
 
-    int cx = ((int)(control.x*20)-i->swflastx);
-    int cy = ((int)(control.y*20)-i->swflasty);
+    int cx = (twipsnap(control.x)-i->swflastx);
+    int cy = (twipsnap(control.y)-i->swflasty);
     i->swflastx += cx;
     i->swflasty += cy;
-    int ex = ((int)(end.x*20)-i->swflastx);
-    int ey = ((int)(end.y*20)-i->swflasty);
+    int ex = (twipsnap(end.x)-i->swflastx);
+    int ey = (twipsnap(end.y)-i->swflasty);
     i->swflastx += ex;
     i->swflasty += ey;
     
@@ -462,7 +529,7 @@ static void startFill(gfxdevice_t*dev)
     }
 }
 
-static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
+static inline int colorcompare(RGBA*a,RGBA*b)
 {
 
     if(a->r!=b->r ||
@@ -474,58 +541,57 @@ static inline int colorcompare(gfxdevice_t*dev, RGBA*a,RGBA*b)
     return 1;
 }
 
-static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
+static SRECT getcharacterbbox(chararray_t*chardata, MATRIX* m, int flashversion)
 {
-    swfoutput_internal*i = (swfoutput_internal*)dev->internal;
     SRECT r;
     char debug = 0;
     memset(&r, 0, sizeof(r));
 
     int t;
     if(debug) printf("\n");
-    for(t=0;t<i->chardatapos;t++)
-    {
-       if(i->chardata[t].fontid != font->id) {
-           msg("<error> Internal error: fontid %d != fontid %d", i->chardata[t].fontid, font->id);
-           exit(1);
+
+    double div = 1.0 / 1024.0;
+    if(flashversion>=8 && !NO_FONT3) {
+       div = 1.0 / 20480.0;
+    }
+
+    while(chardata) {
+       for(t=0;t<chardata->pos;t++) {
+           charatposition_t*chr = &chardata->chr[t];
+           SRECT b = chr->font->layout->bounds[chr->charid];
+           b.xmin = floor((b.xmin*(double)chr->size) *div);
+           b.ymin = floor((b.ymin*(double)chr->size) *div);
+           b.xmax = ceil((b.xmax*(double)chr->size)  *div);
+           b.ymax = ceil((b.ymax*(double)chr->size)  *div);
+
+           b.xmin += chr->x;
+           b.ymin += chr->y;
+           b.xmax += chr->x;
+           b.ymax += chr->y;
+
+           /* until we solve the INTERNAL_SCALING problem (see below)
+              make sure the bounding box is big enough */
+           b.xmin -= 20;
+           b.ymin -= 20;
+           b.xmax += 20;
+           b.ymax += 20;
+
+           b = swf_TurnRect(b, m);
+
+           if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d, char %d]\n",
+                   chr->font->layout->bounds[chr->charid].xmin/20.0,
+                   chr->font->layout->bounds[chr->charid].ymin/20.0,
+                   chr->font->layout->bounds[chr->charid].xmax/20.0,
+                   chr->font->layout->bounds[chr->charid].ymax/20.0,
+                   b.xmin/20.0,
+                   b.ymin/20.0,
+                   b.xmax/20.0,
+                   b.ymax/20.0,
+                   chr->font->id,
+                   chr->charid);
+           swf_ExpandRect2(&r, &b);
        }
-       SRECT b = font->layout->bounds[i->chardata[t].charid];
-       b.xmin *= i->chardata[t].size;
-       b.ymin *= i->chardata[t].size;
-       b.xmax *= i->chardata[t].size;
-       b.ymax *= i->chardata[t].size;
-
-       /* divide by 1024, rounding xmax/ymax up */
-       b.xmax += 1023; b.ymax += 1023; b.xmin /= 1024; b.ymin /= 1024; b.xmax /= 1024; b.ymax /= 1024;
-
-       b.xmin += i->chardata[t].x;
-       b.ymin += i->chardata[t].y;
-       b.xmax += i->chardata[t].x;
-       b.ymax += i->chardata[t].y;
-
-       /* until we solve the INTERNAL_SCALING problem (see below)
-          make sure the bounding box is big enough */
-       b.xmin -= 20;
-       b.ymin -= 20;
-       b.xmax += 20;
-       b.ymax += 20;
-
-       b = swf_TurnRect(b, m);
-
-       if(debug) printf("(%f,%f,%f,%f) -> (%f,%f,%f,%f) [font %d/%d, char %d]\n",
-               font->layout->bounds[i->chardata[t].charid].xmin/20.0,
-               font->layout->bounds[i->chardata[t].charid].ymin/20.0,
-               font->layout->bounds[i->chardata[t].charid].xmax/20.0,
-               font->layout->bounds[i->chardata[t].charid].ymax/20.0,
-               b.xmin/20.0,
-               b.ymin/20.0,
-               b.xmax/20.0,
-               b.ymax/20.0,
-               i->chardata[t].fontid,
-               font->id,
-               i->chardata[t].charid
-               );
-       swf_ExpandRect2(&r, &b);
+       chardata = chardata->next;
     }
     if(debug) printf("-----> (%f,%f,%f,%f)\n",
            r.xmin/20.0,
@@ -535,20 +601,31 @@ static SRECT getcharacterbbox(gfxdevice_t*dev, SWFFONT*font, MATRIX* m)
     return r;
 }
 
-static void putcharacters(gfxdevice_t*dev, TAG*tag)
+static chararray_t*chararray_reverse(chararray_t*buf)
+{
+    chararray_t*prev = 0;
+    while(buf) {
+       chararray_t*next = buf->next;
+       buf->next = prev;
+       prev = buf;
+       buf = next;
+    }
+    return prev;
+}
+
+static void chararray_writetotag(chararray_t*_chardata, TAG*tag)
 {
-    swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    int t;
     SWFFONT font;
     RGBA color;
-    color.r = i->chardata[0].color.r^255;
+    color.r = _chardata?_chardata->chr[0].color.r^255:0;
     color.g = 0;
     color.b = 0;
     color.a = 0;
-    int lastfontid;
+    SWFFONT*lastfont;
     int lastx;
     int lasty;
     int lastsize;
+    int lastchar;
     int charids[128];
     int charadvance[128];
     int charstorepos;
@@ -558,20 +635,21 @@ static void putcharacters(gfxdevice_t*dev, TAG*tag)
 
     if(tag->id != ST_DEFINETEXT &&
         tag->id != ST_DEFINETEXT2) {
-        msg("<error> internal error: putcharacters needs an text tag, not %d\n",tag->id);
+        msg("<error> internal error: charbuffer_put needs an text tag, not %d\n",tag->id);
         exit(1);
     }
-    if(!i->chardatapos) {
-        msg("<warning> putcharacters called with zero characters");
+    if(!_chardata) {
+        msg("<warning> charbuffer_put called with zero characters");
     }
 
     for(pass = 0; pass < 2; pass++)
     {
         charstorepos = 0;
-        lastfontid = -1;
+        lastfont = 0;
         lastx = CHARMIDX;
         lasty = CHARMIDY;
         lastsize = -1;
+       lastchar = -1;
 
         if(pass==1)
         {
@@ -580,114 +658,157 @@ static void putcharacters(gfxdevice_t*dev, TAG*tag)
             swf_SetU8(tag, advancebits);
         }
 
-        for(t=0;t<=i->chardatapos;t++)
-        {
-            if(lastfontid != i->chardata[t].fontid || 
-                    lastx!=i->chardata[t].x ||
-                    lasty!=i->chardata[t].y ||
-                    !colorcompare(dev,&color, &i->chardata[t].color) ||
-                    charstorepos==127 ||
-                    lastsize != i->chardata[t].size ||
-                    t == i->chardatapos)
-            {
-                if(charstorepos && pass==0)
-                {
-                    int s;
-                    for(s=0;s<charstorepos;s++)
-                    {
-                        while(charids[s]>=(1<<glyphbits))
-                            glyphbits++;
-                        while(charadvance[s]>=(1<<advancebits))
-                            advancebits++;
-                    }
-                }
-                if(charstorepos && pass==1)
-                {
-                    tag->writeBit = 0; // Q&D
-                    swf_SetBits(tag, 0, 1); // GLYPH Record
-                    swf_SetBits(tag, charstorepos, 7); // number of glyphs
-                    int s;
-                    for(s=0;s<charstorepos;s++)
-                    {
-                        swf_SetBits(tag, charids[s], glyphbits);
-                        swf_SetBits(tag, charadvance[s], advancebits);
-                    }
-                }
-                charstorepos = 0;
-
-                if(pass == 1 && t<i->chardatapos)
-                {
-                    RGBA*newcolor=0;
-                    SWFFONT*newfont=0;
-                    int newx = 0;
-                    int newy = 0;
-                    if(lastx != i->chardata[t].x ||
-                       lasty != i->chardata[t].y)
-                    {
-                        newx = i->chardata[t].x;
-                        newy = i->chardata[t].y;
-                       if(newx == 0)
-                           newx = SET_TO_ZERO;
-                       if(newy == 0)
-                           newy = SET_TO_ZERO;
-                    }
-                    if(!colorcompare(dev,&color, &i->chardata[t].color)) 
-                    {
-                        color = i->chardata[t].color;
-                        newcolor = &color;
-                    }
-                    font.id = i->chardata[t].fontid;
-                    if(lastfontid != i->chardata[t].fontid || lastsize != i->chardata[t].size)
-                        newfont = &font;
-
-                    tag->writeBit = 0; // Q&D
-                    swf_TextSetInfoRecord(tag, newfont, i->chardata[t].size, newcolor, newx,newy);
-                }
-
-                lastfontid = i->chardata[t].fontid;
-                lastx = i->chardata[t].x;
-                lasty = i->chardata[t].y;
-                lastsize = i->chardata[t].size;
-            }
-
-            if(t==i->chardatapos)
-                    break;
-
-            int advance;
-            int nextt = t==i->chardatapos-1?t:t+1;
-            int rel = i->chardata[nextt].x-i->chardata[t].x;
-            if(rel>=0 && (rel<(1<<(advancebits-1)) || pass==0)) {
-               advance = rel;
-               lastx=i->chardata[nextt].x;
-            }
-            else {
-               advance = 0;
-               lastx=i->chardata[t].x;
-            }
-            charids[charstorepos] = i->chardata[t].charid;
-            charadvance[charstorepos] = advance;
-            charstorepos ++;
-        }
+       chararray_t*chardata = _chardata;
+
+       while(chardata) {
+           int t;
+           
+           assert(!chardata->next || chardata->pos == CHARDATAMAX);
+           assert(chardata->pos);
+
+           int to = chardata->next?chardata->pos-1:chardata->pos;
+
+           for(t=0;t<=to;t++)
+           {
+               char islast = t==chardata->pos;
+
+               charatposition_t*chr = &chardata->chr[t];
+
+               if(lastfont != chr->font || 
+                       lastx!=chr->x ||
+                       lasty!=chr->y ||
+                       !colorcompare(&color, &chardata->chr[t].color) ||
+                       charstorepos==127 ||
+                       lastsize != chardata->chr[t].size ||
+                       islast)
+               {
+                   if(charstorepos && pass==0)
+                   {
+                       int s;
+                       for(s=0;s<charstorepos;s++)
+                       {
+                           while(charids[s]>=(1<<glyphbits))
+                               glyphbits++;
+                           while(charadvance[s]>=(1<<advancebits))
+                               advancebits++;
+                       }
+                   }
+                   if(charstorepos && pass==1)
+                   {
+                       tag->writeBit = 0; // Q&D
+                       swf_SetBits(tag, 0, 1); // GLYPH Record
+                       swf_SetBits(tag, charstorepos, 7); // number of glyphs
+                       int s;
+                       for(s=0;s<charstorepos;s++)
+                       {
+                           swf_SetBits(tag, charids[s], glyphbits);
+                           swf_SetBits(tag, charadvance[s], advancebits);
+                       }
+                   }
+                   charstorepos = 0;
+
+                   if(pass == 1 && !islast)
+                   {
+                       RGBA*newcolor=0;
+                       SWFFONT*newfont=0;
+                       int newx = 0;
+                       int newy = 0;
+                       if(lastx != chr->x ||
+                          lasty != chr->y)
+                       {
+                           newx = chr->x;
+                           newy = chr->y;
+                           if(newx == 0)
+                               newx = SET_TO_ZERO;
+                           if(newy == 0)
+                               newy = SET_TO_ZERO;
+                       }
+                       if(!colorcompare(&color, &chr->color)) 
+                       {
+                           color = chr->color;
+                           newcolor = &color;
+                       }
+                       font.id = chr->font->id;
+                       if(lastfont != chr->font || lastsize != chr->size)
+                           newfont = &font;
+
+                       tag->writeBit = 0; // Q&D
+                       swf_TextSetInfoRecord(tag, newfont, chr->size, newcolor, newx, newy);
+                   }
+
+                   lastfont = chr->font;
+                   lastx = chr->x;
+                   lasty = chr->y;
+                   lastsize = chr->size;
+               }
+
+               if(islast)
+                       break;
+
+               int nextx = chr->x;
+               if(t<chardata->pos-1) nextx = chardata->chr[t+1].x;
+               if(t==chardata->pos-1 && chardata->next) nextx = chardata->next->chr[0].x;
+               int dx = nextx-chr->x;
+               
+               int advance;
+               if(dx>=0 && (dx<(1<<(advancebits-1)) || pass==0)) {
+                  advance = dx;
+                  lastx=nextx;
+               } else {
+                  advance = 0;
+                  lastx=chr->x;
+               }
+
+               charids[charstorepos] = chr->charid;
+               charadvance[charstorepos] = advance;
+               lastchar = chr->charid;
+               charstorepos ++;
+           }
+           chardata = chardata->next;
+       }
     }
-    i->chardatapos = 0;
 }
 
-static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, int size, RGBA color)
+static void chararray_destroy(chararray_t*chr)
 {
-    swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    if(i->chardatapos == CHARDATAMAX)
-    {
-       msg("<warning> Character buffer too small. SWF will be slightly bigger");
-        endtext(dev);
-        starttext(dev);
+    while(chr) {
+       chararray_t*next = chr->next;
+       chr->next = 0;
+       free(chr);
+       chr = next;
+    }
+}
+
+static inline int matrix_diff(MATRIX*m1, MATRIX*m2)
+{
+    return memcmp(m1,m2,sizeof(MATRIX));
+}
+static charbuffer_t*charbuffer_append(charbuffer_t*buf, SWFFONT*font, int charid, int x,int y, int size, RGBA color, MATRIX*m)
+{
+    if(!buf || matrix_diff(&buf->matrix,m)) {
+       charbuffer_t*n = rfx_calloc(sizeof(charbuffer_t));
+       n->matrix = *m;
+       n->next = buf;
+       buf = n;
+    }
+    if(!buf->last || buf->last->pos == CHARDATAMAX) {
+       chararray_t*n = rfx_calloc(sizeof(chararray_t));
+       if(!buf->array) {
+           buf->array = buf->last = n;
+       } else {
+           buf->last->next = n;
+           buf->last = n;
+       }
     }
-    i->chardata[i->chardatapos].fontid = fontid;
-    i->chardata[i->chardatapos].charid = charid;
-    i->chardata[i->chardatapos].x = x;
-    i->chardata[i->chardatapos].y = y;
-    i->chardata[i->chardatapos].color = color;
-    i->chardata[i->chardatapos].size = size;
-    i->chardatapos++;
+    chararray_t*a = buf->last;
+    a->chr[a->pos].font = font;
+    a->chr[a->pos].charid = charid;
+    a->chr[a->pos].x = x;
+    a->chr[a->pos].y = y;
+    a->chr[a->pos].color = color;
+    a->chr[a->pos].size = size;
+    a->pos++;
+    return buf;
 }
 
 /* Notice: we can only put chars in the range -1639,1638 (-32768/20,32768/20).
@@ -695,30 +816,27 @@ static void putcharacter(gfxdevice_t*dev, int fontid, int charid, int x,int y, i
    If we set it to low, however, the char positions will be inaccurate */
 #define GLYPH_SCALE 1
 
-static void endtext(gfxdevice_t*dev)
+static void chararray_writetodev(gfxdevice_t*dev, chararray_t*array, MATRIX*matrix, char invisible)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    if(i->textid<0)
-        return;
-
+   
+    int textid = getNewID(dev);
     i->tag = swf_InsertTag(i->tag,ST_DEFINETEXT2);
-    swf_SetU16(i->tag, i->textid);
-
+    swf_SetU16(i->tag, textid);
     SRECT r;
-    r = getcharacterbbox(dev, i->swffont, &i->fontmatrix);
+    r = getcharacterbbox(array, matrix, i->config_flashversion);
     r = swf_ClipRect(i->pagebbox, r);
     swf_SetRect(i->tag,&r);
+    swf_SetMatrix(i->tag, matrix);
+    msg("<trace> Placing text as ID %d", textid);
+    chararray_writetotag(array, i->tag);
+    i->chardata = 0;
 
-    swf_SetMatrix(i->tag,&i->fontmatrix);
-
-    msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
-
-    putcharacters(dev, i->tag);
     swf_SetU8(i->tag,0);
 
     if(i->swf->fileVersion >= 8) {
        i->tag = swf_InsertTag(i->tag, ST_CSMTEXTSETTINGS);
-       swf_SetU16(i->tag, i->textid);
+       swf_SetU16(i->tag, textid);
 
        //swf_SetU8(i->tag, /*subpixel grid*/(2<<3)|/*flashtype*/0x40);
        swf_SetU8(i->tag, /*grid*/(1<<3)|/*flashtype*/0x40);
@@ -726,51 +844,37 @@ static void endtext(gfxdevice_t*dev)
 
        swf_SetU32(i->tag, 0);//thickness
        swf_SetU32(i->tag, 0);//sharpness
+       //swf_SetU32(i->tag, 0x20000);//thickness
+       //swf_SetU32(i->tag, 0x800000);//sharpness
        swf_SetU8(i->tag, 0);//reserved
     }
-    i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
-    
-    swf_ObjectPlace(i->tag,i->textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
-    i->textid = -1;
+    if(invisible && i->config_flashversion>=8) {
+       i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT3);
+       swf_ObjectPlaceBlend(i->tag,textid,getNewDepth(dev),&i->page_matrix,NULL,NULL,BLENDMODE_MULTIPLY);
+    } else {
+       i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
+       swf_ObjectPlace(i->tag,textid,getNewDepth(dev),&i->page_matrix,NULL,NULL);
+    }
 }
 
-/* set's the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
-static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22,double x, double y, char force)
+static void charbuffer_writetodevandfree(gfxdevice_t*dev, charbuffer_t*buf, char invisible)
+{
+    while(buf) {
+       charbuffer_t*next = buf->next;buf->next = 0;
+       chararray_writetodev(dev, buf->array, &buf->matrix, invisible);
+       chararray_destroy(buf->array);
+       free(buf);
+       buf = next;
+    }
+}
+
+static void endtext(gfxdevice_t*dev)
 {
-    m11 *= 1024;
-    m12 *= 1024;
-    m21 *= 1024;
-    m22 *= 1024;
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    if(i->lastfontm11 == m11 &&
-       i->lastfontm12 == m12 &&
-       i->lastfontm21 == m21 &&
-       i->lastfontm22 == m22 && !force)
+    if(!i->textmode)
         return;
-   if(i->textid>=0)
-       endtext(dev);
-    
-    i->lastfontm11 = m11;
-    i->lastfontm12 = m12;
-    i->lastfontm21 = m21;
-    i->lastfontm22 = m22;
-
-    double xsize = sqrt(m11*m11 + m12*m12);
-    double ysize = sqrt(m21*m21 + m22*m22);
-    i->current_font_size = (xsize>ysize?xsize:ysize)*1;
-    if(i->current_font_size < 1)
-       i->current_font_size = 1;
-    double ifs = 1.0 / (i->current_font_size*GLYPH_SCALE);
-
-    MATRIX m;
-    m.sx = (S32)((m11*ifs)*65536); m.r1 = (S32)((m21*ifs)*65536);
-    m.r0 = (S32)((m12*ifs)*65536); m.sy = (S32)((m22*ifs)*65536); 
-    /* this is the position of the first char to set a new fontmatrix-
-       we hope that it's close enough to all other characters using the
-       font, so we use its position as origin for the matrix */
-    m.tx = x*20;
-    m.ty = y*20;
-    i->fontmatrix = m;
+    charbuffer_writetodevandfree(dev, i->chardata, 0);i->chardata = 0;
+    i->textmode = 0;
 }
 
 static int watermark2_width=47;
@@ -851,11 +955,16 @@ static void endpage(gfxdevice_t*dev)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
     if(i->pagefinished)
-      return;
+       return;
+    
     if(i->shapeid>=0)
-      endshape(dev);
-    if(i->textid>=0)
-      endtext(dev);
+       endshape(dev);
+    if(i->textmode)
+       endtext(dev);
+    if(i->topchardata) {
+       charbuffer_writetodevandfree(dev, i->topchardata, 1);
+       i->topchardata=0;
+    }
     
     while(i->clippos)
         dev->endclip(dev);
@@ -985,6 +1094,7 @@ void swf_startframe(gfxdevice_t*dev, int width, int height)
     i->lastframeno = i->frameno;
     i->firstpage = 0;
     i->pagefinished = 0;
+    i->chardata = 0;
 }
 
 void swf_endframe(gfxdevice_t*dev)
@@ -1009,6 +1119,14 @@ void swf_endframe(gfxdevice_t*dev)
         swf_SetU16(i->tag,i->depth);
     }
     i->depth = i->startdepth;
+
+    if(i->config_frameresets) {
+       for(i->currentswfid;i->currentswfid>i->startids;i->currentswfid--) {
+           i->tag = swf_InsertTag(i->tag,ST_FREECHARACTER);
+           swf_SetU16(i->tag,i->currentswfid);
+       }
+       i->currentswfid = i->startids;
+    }
 }
 
 static void setBackground(gfxdevice_t*dev, int x1, int y1, int x2, int y2)
@@ -1081,15 +1199,17 @@ void gfxdevice_swf_init(gfxdevice_t* dev)
     i->swf->movieSize.ymin = 0;
     i->swf->movieSize.xmax = 0;
     i->swf->movieSize.ymax = 0;
+    i->swf->fileAttributes = 9; // as3, local-with-network
     
     i->swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
     i->tag = i->swf->firstTag;
     RGBA rgb;
     rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
-    rgb.r = 0;
+    //rgb.r = 0;
     swf_SetRGB(i->tag,&rgb);
 
     i->startdepth = i->depth = 0;
+    i->startids = i->currentswfid = 0;
 }
 
 static void startshape(gfxdevice_t*dev)
@@ -1145,7 +1265,7 @@ static void starttext(gfxdevice_t*dev)
     if(i->config_watermark) {
        insert_watermark(dev, 0);
     }
-    i->textid = getNewID(dev);
+    i->textmode = 1;
     i->swflastx=i->swflasty=0;
 }
            
@@ -1179,7 +1299,7 @@ void cancelshape(gfxdevice_t*dev)
     /* delete old shape tag */
     TAG*todel = i->tag;
     i->tag = i->tag->prev;
-    swf_DeleteTag(todel);
+    swf_DeleteTag(0, todel);
     if(i->shape) {swf_ShapeFree(i->shape);i->shape=0;}
     i->shapeid = -1;
     i->bboxrectpos = -1;
@@ -1304,7 +1424,7 @@ void wipeSWF(SWF*swf)
           tag->id != ST_END &&
           tag->id != ST_DOACTION &&
           tag->id != ST_SHOWFRAME) {
-           swf_DeleteTag(tag);
+           swf_DeleteTag(swf, tag);
        }
        tag = next;
     }
@@ -1352,6 +1472,8 @@ void swfoutput_finalize(gfxdevice_t*dev)
 
     endpage(dev);
     fontlist_t *iterator = i->fontlist;
+    char use_font3 = i->config_flashversion>=8 && !NO_FONT3;
+
     while(iterator) {
        TAG*mtag = i->swf->firstTag;
        if(iterator->swffont) {
@@ -1361,22 +1483,31 @@ void swfoutput_finalize(gfxdevice_t*dev)
            }
            int used = iterator->swffont->use && iterator->swffont->use->used_glyphs;
            if(used) {
-               mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
-               swf_FontSetDefine2(mtag, iterator->swffont);
+               if(!use_font3) {
+                   mtag = swf_InsertTag(mtag, ST_DEFINEFONT2);
+                   swf_FontSetDefine2(mtag, iterator->swffont);
+               } else {
+                   mtag = swf_InsertTag(mtag, ST_DEFINEFONT3);
+                   swf_FontSetDefine2(mtag, iterator->swffont);
+               }
            }
        }
 
         iterator = iterator->next;
     }
-       
+
     i->tag = swf_InsertTag(i->tag,ST_END);
     TAG* tag = i->tag->prev;
+   
+    if(use_font3 && i->config_storeallcharacters && i->config_alignfonts) {
+       swf_FontPostprocess(i->swf); // generate alignment information
+    }
 
     /* remove the removeobject2 tags between the last ST_SHOWFRAME
        and the ST_END- they confuse the flash player  */
     while(tag->id == ST_REMOVEOBJECT2) {
         TAG* prev = tag->prev;
-        swf_DeleteTag(tag);
+        swf_DeleteTag(i->swf, tag);
         tag = prev;
     }
     
@@ -1387,9 +1518,11 @@ void swfoutput_finalize(gfxdevice_t*dev)
        i->swf->compressed = 1;
     }
 
-    /* Initialize AVM2 if it is a Flash9 file */
-    if(i->config_flashversion>=9 && i->config_insertstoptag) {
-       AVM2_InsertStops(i->swf);
+    /* Add AVM2 actionscript */
+    if(i->config_flashversion>=9 && 
+            (i->config_insertstoptag || i->hasbuttons) && !i->config_linknameurl) {
+        swf_AddButtonLinks(i->swf, i->config_insertstoptag, 
+                i->config_internallinkfunction||i->config_externallinkfunction);
     }
 //    if(i->config_reordertags)
 //     swf_Optimize(i->swf);
@@ -1422,17 +1555,17 @@ void* swfresult_get(gfxresult_t*gfx, const char*name)
     if(!strcmp(name, "swf")) {
        return (void*)swf_CopySWF(swf);
     } else if(!strcmp(name, "xmin")) {
-       return (void*)(swf->movieSize.xmin/20);
+       return (void*)(ptroff_t)(swf->movieSize.xmin/20);
     } else if(!strcmp(name, "ymin")) {
-       return (void*)(swf->movieSize.ymin/20);
+       return (void*)(ptroff_t)(swf->movieSize.ymin/20);
     } else if(!strcmp(name, "xmax")) {
-       return (void*)(swf->movieSize.xmax/20);
+       return (void*)(ptroff_t)(swf->movieSize.xmax/20);
     } else if(!strcmp(name, "ymax")) {
-       return (void*)(swf->movieSize.ymax/20);
+       return (void*)(ptroff_t)(swf->movieSize.ymax/20);
     } else if(!strcmp(name, "width")) {
-       return (void*)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
+       return (void*)(ptroff_t)((swf->movieSize.xmax - swf->movieSize.xmin)/20);
     } else if(!strcmp(name, "height")) {
-       return (void*)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
+       return (void*)(ptroff_t)((swf->movieSize.ymax - swf->movieSize.ymin)/20);
     }
     return 0;
 }
@@ -1526,7 +1659,7 @@ static void swfoutput_setlinewidth(gfxdevice_t*dev, double _linewidth)
 }
 
 
-static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover);
+static void drawlink(gfxdevice_t*dev, ActionTAG*,ActionTAG*, gfxline_t*points, char mouseover, char*type, const char*url);
 static void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points);
 static void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points);
 static void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points);
@@ -1541,6 +1674,9 @@ void swf_drawlink(gfxdevice_t*dev, gfxline_t*points, const char*url)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
 
+    if(i->config_disablelinks)
+        return;
+
     if(!strncmp("http://pdf2swf:", url, 15)) {
        char*tmp = strdup(url);
        int l = strlen(tmp);
@@ -1569,10 +1705,12 @@ void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points)
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
     if(i->shapeid>=0)
        endshape(dev);
-    if(i->textid>=0)
+    if(i->textmode)
        endtext(dev);
+
+    /* TODO: escape special characters in url */
     
-    if(i->config_externallinkfunction) {
+    if(i->config_externallinkfunction && i->config_flashversion<=8) {
        actions = action_PushString(actions, url); //parameter
        actions = action_PushInt(actions, 1); //number of parameters (1)
        actions = action_PushString(actions, i->config_externallinkfunction); //function name
@@ -1587,7 +1725,9 @@ void swfoutput_linktourl(gfxdevice_t*dev, const char*url, gfxline_t*points)
     }
     actions = action_End(actions);
    
-    drawlink(dev, actions, 0, points, 0);
+    drawlink(dev, actions, 0, points, 0, "url", url);
+    
+    swf_ActionFree(actions);
 }
 void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
 {
@@ -1596,10 +1736,10 @@ void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
 
     if(i->shapeid>=0)
        endshape(dev);
-    if(i->textid>=0)
+    if(i->textmode)
        endtext(dev);
   
-    if(!i->config_internallinkfunction) {
+    if(!i->config_internallinkfunction || i->config_flashversion>=9) {
        actions = action_GotoFrame(actions, page-1);
        actions = action_End(actions);
     } else {
@@ -1610,7 +1750,12 @@ void swfoutput_linktopage(gfxdevice_t*dev, int page, gfxline_t*points)
        actions = action_End(actions);
     }
 
-    drawlink(dev, actions, 0, points, 0);
+    char name[80];
+    sprintf(name, "page%d", page);
+
+    drawlink(dev, actions, 0, points, 0, "page", name);
+    
+    swf_ActionFree(actions);
 }
 
 /* Named Links (a.k.a. Acrobatmenu) are used to implement various gadgets
@@ -1625,9 +1770,10 @@ void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
 
     if(i->shapeid>=0)
        endshape(dev);
-    if(i->textid>=0)
+    if(i->textmode)
        endtext(dev);
 
+    char*type = 0;
     if(!strncmp(tmp, "call:", 5))
     {
        char*x = strchr(&tmp[5], ':');
@@ -1646,6 +1792,7 @@ void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
        }
        actions2 = action_End(0);
        mouseover = 0;
+        type = "call";
     }
     else
     {
@@ -1658,9 +1805,10 @@ void swfoutput_namedlink(gfxdevice_t*dev, char*name, gfxline_t*points)
        actions2 = action_PushString(actions2, "");
        actions2 = action_SetVariable(actions2);
        actions2 = action_End(actions2);
+        type = "subtitle";
     }
 
-    drawlink(dev, actions1, actions2, points, mouseover);
+    drawlink(dev, actions1, actions2, points, mouseover, type, name);
 
     swf_ActionFree(actions1);
     swf_ActionFree(actions2);
@@ -1707,7 +1855,7 @@ static void drawgfxline(gfxdevice_t*dev, gfxline_t*line, int fill)
 }
 
 
-static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover)
+static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gfxline_t*points, char mouseover, char*type, const char*url)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
     RGBA rgb;
@@ -1720,6 +1868,13 @@ static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gf
     double posy = 0;
     int buttonid = getNewID(dev);
     gfxbbox_t bbox = gfxline_getbbox(points);
+    
+    if(i->config_linknameurl) {
+        actions1 = 0;
+        actions2 = 0;
+    }
+    
+    i->hasbuttons = 1;
 
     /* shape */
     myshapeid = getNewID(dev);
@@ -1741,6 +1896,7 @@ static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gf
     i->swflastx = i->swflasty = 0;
     drawgfxline(dev, points, 1);
     swf_ShapeSetEnd(i->tag);
+    swf_ShapeFree(i->shape);
 
     /* shape2 */
     myshapeid2 = getNewID(dev);
@@ -1764,6 +1920,7 @@ static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gf
     i->swflastx = i->swflasty = 0;
     drawgfxline(dev, points, 1);
     swf_ShapeSetEnd(i->tag);
+    swf_ShapeFree(i->shape);
 
     if(!mouseover)
     {
@@ -1800,8 +1957,18 @@ static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gf
             swf_ButtonPostProcess(i->tag, 1);
        }
     }
-    char name[80];
-    sprintf(name, "link%d", buttonid);
+
+    char buf[80];
+    char*buf2 = 0;
+    const char* name = 0;
+    if(i->config_linknameurl) {
+        buf2 = malloc(strlen(type)+strlen(url)+2);
+        sprintf(buf2, "%s:%s", type, url);
+        name = buf2;
+    } else {
+        name = buf;
+        sprintf(buf, "button%d", buttonid);
+    }
     
     msg("<trace> Placing link ID %d", buttonid);
     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
@@ -1815,10 +1982,13 @@ static void drawlink(gfxdevice_t*dev, ActionTAG*actions1, ActionTAG*actions2, gf
         m = i->page_matrix;
         m.tx = p.x;
         m.ty = p.y;
-       swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,name);
+       swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&m,0,(U8*)name);
     } else {
-       swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,name);
+       swf_ObjectPlace(i->tag, buttonid, getNewDepth(dev),&i->page_matrix,0,(U8*)name);
     }
+
+    if(buf2)
+       free(buf2);
 }
 
       
@@ -1900,6 +2070,10 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
        i->config_enablezlib = atoi(value);
     } else if(!strcmp(name, "bboxvars")) {
        i->config_bboxvars = atoi(value);
+    } else if(!strcmp(name, "dots")) {
+       i->config_dots = atoi(value);
+    } else if(!strcmp(name, "frameresets")) {
+       i->config_frameresets = atoi(value);
     } else if(!strcmp(name, "showclipshapes")) {
        i->config_showclipshapes = atoi(value);
     } else if(!strcmp(name, "reordertags")) {
@@ -1930,7 +2104,7 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
            i->swf->fileVersion = i->config_flashversion;
        }
     } else if(!strcmp(name, "framerate")) {
-       i->config_framerate = atoi(value);
+       i->config_framerate = atof(value);
        if(i->swf) {
            i->swf->frameRate = i->config_framerate*0x100;
        }
@@ -1940,10 +2114,20 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
        i->config_caplinewidth = atof(value);
     } else if(!strcmp(name, "linktarget")) {
        i->config_linktarget = strdup(value);
+    } else if(!strcmp(name, "invisibletexttofront")) {
+       i->config_invisibletexttofront = atoi(value);
+    } else if(!strcmp(name, "noclips")) {
+       i->config_noclips = atoi(value);
     } else if(!strcmp(name, "dumpfonts")) {
        i->config_dumpfonts = atoi(value);
     } else if(!strcmp(name, "animate")) {
        i->config_animate = atoi(value);
+    } else if(!strcmp(name, "linknameurl")) {
+       i->config_linknameurl = atoi(value);
+    } else if(!strcmp(name, "showimages")) {
+       i->config_showimages = atoi(value);
+    } else if(!strcmp(name, "disablelinks")) {
+       i->config_disablelinks = atoi(value);
     } else if(!strcmp(name, "simpleviewer")) {
        i->config_simpleviewer = atoi(value);
     } else if(!strcmp(name, "next_bitmap_is_jpeg")) {
@@ -1975,9 +2159,7 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
        i->config_linkcolor.a = NIBBLE(value[6])<<4 | NIBBLE(value[7]);
     } else if(!strcmp(name, "help")) {
        printf("\nSWF layer options:\n");
-       printf("jpegdpi=<dpi>               resolution adjustment for jpeg images\n");
         printf("jpegsubpixels=<pixels>      resolution adjustment for jpeg images (same as jpegdpi, but in pixels)\n");
-        printf("ppmdpi=<dpi>                resolution adjustment for lossless images\n");
         printf("ppmsubpixels=<pixels        resolution adjustment for  lossless images (same as ppmdpi, but in pixels)\n");
         printf("subpixels=<pixels>          shortcut for setting both jpegsubpixels and ppmsubpixels\n");
         printf("drawonlyshapes              convert everything to shapes (currently broken)\n");
@@ -1985,9 +2167,11 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
         printf("linksopennewwindow          make links open a new browser window\n");
         printf("linktarget                  target window name of new links\n");
         printf("linkcolor=<color)           color of links (format: RRGGBBAA)\n");
+        printf("linknameurl                Link buttons will be named like the URL they refer to (handy for iterating through links with actionscript)\n");
         printf("storeallcharacters          don't reduce the fonts to used characters in the output file\n");
-        printf("enablezlib                  switch on zlib compression (also done if flashversion>=7)\n");
+        printf("enablezlib                  switch on zlib compression (also done if flashversion>=6)\n");
         printf("bboxvars                    store the bounding box of the SWF file in actionscript variables\n");
+        printf("dots                        Take care to handle dots correctly\n");
         printf("reordertags=0/1             (default: 1) perform some tag optimizations\n");
         printf("internallinkfunction=<name> when the user clicks a internal link (to a different page) in the converted file, this actionscript function is called\n");
         printf("externallinkfunction=<name> when the user clicks an external link (e.g. http://www.foo.bar/) on the converted file, this actionscript function is called\n");
@@ -1996,9 +2180,13 @@ int swf_setparameter(gfxdevice_t*dev, const char*name, const char*value)
         printf("insertstop                  put an ActionScript \"STOP\" tag in every frame\n");
         printf("protect                     add a \"protect\" tag to the file, to prevent loading in the Flash editor\n");
         printf("flashversion=<version>      the SWF fileversion (6)\n");
+        printf("framerate=<fps>                    SWF framerate\n");
         printf("minlinewidth=<width>        convert horizontal/vertical boxes smaller than this width to lines (0.05) \n");
+        printf("simpleviewer                Add next/previous buttons to the SWF\n");
         printf("animate                     insert a showframe tag after each placeobject (animate draw order of PDF files)\n");
         printf("jpegquality=<quality>       set compression quality of jpeg images\n");
+       printf("splinequality=<value>       Set the quality of spline convertion to value (0-100, default: 100).\n");
+       printf("disablelinks                Disable links.\n");
     } else {
        return 0;
     }
@@ -2073,7 +2261,9 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int
     
     if(newsizex<sizex || newsizey<sizey) {
        msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
-       newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
+       gfximage_t*ni = gfximage_rescale(img, newsizex, newsizey);
+       newpic = (RGBA*)ni->data;
+       free(ni);
        *newwidth = sizex = newsizex;
        *newheight  = sizey = newsizey;
        mem = newpic;
@@ -2110,6 +2300,7 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int
 
     if(cacheid<=0) {
        bitid = getNewID(dev);
+
        i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
        addImageToCache(dev, mem, sizex, sizey);
     } else {
@@ -2174,6 +2365,11 @@ static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxm
     SHAPE*shape;
     swf_ShapeNew(&shape);
     int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
+    int lsid = 0;
+    if(i->config_showimages) {
+       RGBA pink = {255,255,0,255};
+       lsid = swf_ShapeAddLineStyle(shape, 20, &pink);
+    }
     swf_SetU16(i->tag, myshapeid);
     SRECT r = gfxline_getSWFbbox(line);
     r = swf_ClipRect(i->pagebbox, r);
@@ -2181,7 +2377,7 @@ static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxm
     swf_SetShapeStyles(i->tag,shape);
     swf_ShapeCountBits(shape,NULL,NULL);
     swf_SetShapeBits(i->tag,shape);
-    swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,0,fsid,0);
+    swf_ShapeSetAll(i->tag,shape,UNDEFINED_COORD,UNDEFINED_COORD,lsid,fsid,0);
     i->swflastx = i->swflasty = UNDEFINED_COORD;
     drawgfxline(dev, line, 1);
     swf_ShapeSetEnd(i->tag);
@@ -2225,6 +2421,8 @@ static void drawoutline(gfxdevice_t*dev, gfxline_t*line)
 static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+    if(i->config_noclips)
+       return;
 
     endtext(dev);
     endshape(dev);
@@ -2291,7 +2489,9 @@ static void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
 static void swf_endclip(gfxdevice_t*dev)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
-    if(i->textid>=0)
+    if(i->config_noclips)
+       return;
+    if(i->textmode)
        endtext(dev);
     if(i->shapeid>=0)
        endshape(dev);
@@ -2439,22 +2639,24 @@ static void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcol
 
     /* TODO: * split line into segments, and perform this check for all segments */
 
-    if(i->config_disable_polygon_conversion || type>=5 ||
+    if(i->config_disable_polygon_conversion || /*type>=5 ||*/
        (!has_dots &&
         (width <= i->config_caplinewidth 
         || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
-        || (cap_style == gfx_capRound && type<=2)))) {} else 
+        || (cap_style == gfx_capRound && type<=2)))) 
     {
+        // ...
+    } else {
        /* convert line to polygon */
        msg("<trace> draw as polygon, type=%d dots=%d", type, has_dots);
        if(has_dots)
            gfxline_fix_short_edges(line);
        /* we need to convert the line into a polygon */
-       gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
-       gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
+       gfxpoly_t* poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
+       gfxline_t*gfxline = gfxline_from_gfxpoly(poly);
        dev->fill(dev, gfxline, color);
        gfxline_free(gfxline);
-       gfxpoly_free(poly);
+       gfxpoly_destroy(poly);
        return;
     }
 
@@ -2495,7 +2697,6 @@ static void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
     gfxbbox_t r = gfxline_getbbox(line);
     int is_outside_page = !is_inside_page(dev, r.xmin, r.ymin) || !is_inside_page(dev, r.xmax, r.ymax);
 
-    //if(i->chardatapos && i->chardata[i->chardatapos-1].color.a) {
     endtext(dev);
 
     if(!i->config_ignoredraworder)
@@ -2605,13 +2806,13 @@ static void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*grad
     swf_FreeGradient(swfgradient);free(swfgradient);
 }
 
-static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
+static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id, int version)
 {
     SWFFONT*swffont = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
     int t;
     SRECT bounds = {0,0,0,0};
     swffont->id = -1;
-    swffont->version = 2;
+    swffont->version = version;
     swffont->name = (U8*)strdup(id);
     swffont->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
     swffont->layout->ascent = 0;
@@ -2625,34 +2826,48 @@ static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
     swffont->glyph2ascii = (U16*)rfx_calloc(sizeof(U16)*swffont->numchars);
     swffont->glyph = (SWFGLYPH*)rfx_calloc(sizeof(SWFGLYPH)*swffont->numchars);
     swffont->glyphnames = (char**)rfx_calloc(sizeof(char*)*swffont->numchars);
-    for(t=0;t<font->max_unicode;t++) {
-       swffont->ascii2glyph[t] = font->unicode2glyph[t];
-    }
+
+    SRECT max = {0,0,0,0};
     for(t=0;t<font->num_glyphs;t++) {
        drawer_t draw;
        gfxline_t*line;
-       int advance = 0;
-       swffont->glyph2ascii[t] = font->glyphs[t].unicode;
-       if(swffont->glyph2ascii[t] == 0xffff || swffont->glyph2ascii[t] == 0x0000) {
+       double advance = 0;
+       int u = font->glyphs[t].unicode;
+       int s;
+       char twice=0;
+       for(s=0;s<font->num_glyphs;s++) {
+           if(swffont->glyph2ascii[s]==u) 
+               twice=1;
+       }
+       if(u >= 0xe000 || u == 0x0000 || twice) {
            /* flash 8 flashtype requires unique unicode IDs for each character.
               We use the Unicode private user area to assign characters, hoping that
               the font doesn't contain more than 2048 glyphs */
-           swffont->glyph2ascii[t] = 0xe000 + (t&0x1fff);
+           u = 0xe000 + (t&0x1fff);
        }
+       swffont->glyph2ascii[t] = u;
 
        if(font->glyphs[t].name) {
            swffont->glyphnames[t] = strdup(font->glyphs[t].name);
        } else {
            swffont->glyphnames[t] = 0;
        }
-       advance = (int)(font->glyphs[t].advance);
+       advance = font->glyphs[t].advance;
 
        swf_Shape01DrawerInit(&draw, 0);
        line = font->glyphs[t].line;
+
+       const double scale = GLYPH_SCALE;
        while(line) {
            FPOINT c,to;
-           c.x = line->sx * GLYPH_SCALE; c.y = line->sy * GLYPH_SCALE;
-           to.x = line->x * GLYPH_SCALE; to.y = line->y * GLYPH_SCALE;
+           c.x = line->sx * scale; c.y = -line->sy * scale;
+           //to.x = floor(line->x * scale); to.y = floor(-line->y * scale);
+           to.x = line->x * scale; to.y = -line->y * scale;
+
+           /*if(strstr(swffont->name, "BIRNU") && t==90) {
+               to.x += 1;
+           }*/
+
            if(line->type == gfx_moveTo) {
                draw.moveTo(&draw, &to);
            } else if(line->type == gfx_lineTo) {
@@ -2664,17 +2879,16 @@ static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
        }
        draw.finish(&draw);
        swffont->glyph[t].shape = swf_ShapeDrawerToShape(&draw);
-       swffont->layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
 
-       int xmax = swffont->layout->bounds[t].xmax / 20;
-       if(xmax>0 && xmax*2 < advance) {
-           printf("fix bad advance value: bbox=%d, advance=%d (%f)\n", xmax, advance, font->glyphs[t].advance);
-           advance = xmax;
-       }
+       SRECT bbox = swf_ShapeDrawerGetBBox(&draw);
+       swf_ExpandRect2(&max, &bbox);
+
+       swffont->layout->bounds[t] = bbox;
            
-       if(advance<32768/20) {
-           swffont->glyph[t].advance = advance*20;
+       if(advance<32768.0/20) {
+           swffont->glyph[t].advance = (int)(advance*20);
        } else {
+           //msg("<warning> Advance value overflow in glyph %d", t);
            swffont->glyph[t].advance = 32767;
        }
 
@@ -2683,6 +2897,25 @@ static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
        swf_ExpandRect2(&bounds, &swffont->layout->bounds[t]);
     }
 
+    for(t=0;t<font->num_glyphs;t++) {
+       SRECT bbox = swffont->layout->bounds[t];
+
+       /* if the glyph doesn't have a bounding box, use the
+          combined bounding box (necessary e.g. for space characters) */
+       if(!(bbox.xmin|bbox.ymin|bbox.xmax|bbox.ymax)) {
+           swffont->layout->bounds[t] = bbox = max;
+       }
+       
+       /* check that the advance value is reasonable, by comparing it
+          with the bounding box */
+       if(bbox.xmax>0 && (bbox.xmax*10 < swffont->glyph[t].advance || !swffont->glyph[t].advance)) {
+           if(swffont->glyph[t].advance)
+               msg("<warning> fix bad advance value for char %d: bbox=%.2f, advance=%.2f\n", t, bbox.xmax/20.0, swffont->glyph[t].advance/20.0);
+           swffont->glyph[t].advance = bbox.xmax;
+       }
+       //swffont->glyph[t].advance = bbox.xmax - bbox.xmin;
+    }
+
 
     /* Flash player will use the advance value from the char, and the ascent/descent values
        from the layout for text selection.
@@ -2691,14 +2924,19 @@ static SWFFONT* gfxfont_to_swffont(gfxfont_t*font, const char* id)
        The baseline is defined as the y-position zero 
      */
 
-    swffont->layout->ascent = -bounds.ymin;
-    if(swffont->layout->ascent < 0)
-        swffont->layout->ascent = 0;
-    swffont->layout->descent = bounds.ymax;
-    if(swffont->layout->descent < 0)
-        swffont->layout->descent = 0;
+    swffont->layout->ascent = bounds.ymin<0?-bounds.ymin:0;
+    swffont->layout->descent = bounds.ymax>0?bounds.ymax:0;
     swffont->layout->leading = bounds.ymax - bounds.ymin;
 
+    /* if the font has proper ascent/descent values (>0) and those define
+       greater line spacing that what we estimated from the bounding boxes,
+       use the font's parameters */
+    if(font->ascent*20 > swffont->layout->ascent)
+       swffont->layout->ascent = font->ascent*20;
+    if(font->descent*20 > swffont->layout->descent)
+       swffont->layout->descent = font->descent*20;
+
+    swf_FontSort(swffont);
     return swffont;
 }
 
@@ -2718,7 +2956,7 @@ static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
        l = l->next;
     }
     l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
-    l->swffont = gfxfont_to_swffont(font, font->id);
+    l->swffont = gfxfont_to_swffont(font, font->id, (i->config_flashversion>=8 && !NO_FONT3)?3:2);
     l->next = 0;
     if(last) {
        last->next = l;
@@ -2745,11 +2983,6 @@ static void swf_addfont(gfxdevice_t*dev, gfxfont_t*font)
                    l->swffont->layout->bounds[iii].xmax/20.0,
                    l->swffont->layout->bounds[iii].ymax/20.0
                    );
-           int t;
-           for(t=0;t<l->swffont->maxascii;t++) {
-               if(l->swffont->ascii2glyph[t] == iii)
-                   msg("<debug> | - maps to %d",t);
-           }
        }
     }
 }
@@ -2773,6 +3006,56 @@ static void swf_switchfont(gfxdevice_t*dev, const char*fontid)
     return;
 }
 
+/* sets the matrix which is to be applied to characters drawn by swfoutput_drawchar() */
+static void setfontscale(gfxdevice_t*dev,double m11,double m12, double m21,double m22,double x, double y, char force)
+{
+    m11 *= 1024;
+    m12 *= 1024;
+    m21 *= 1024;
+    m22 *= 1024;
+    swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+    if(i->lastfontm11 == m11 &&
+       i->lastfontm12 == m12 &&
+       i->lastfontm21 == m21 &&
+       i->lastfontm22 == m22 && !force)
+        return;
+   if(i->textmode)
+       endtext(dev);
+    
+    i->lastfontm11 = m11;
+    i->lastfontm12 = m12;
+    i->lastfontm21 = m21;
+    i->lastfontm22 = m22;
+
+    double xsize = sqrt(m11*m11 + m12*m12);
+    double ysize = sqrt(m21*m21 + m22*m22);
+
+    int extrazoom = 1;
+    if(i->config_flashversion>=8 && !NO_FONT3)
+       extrazoom = 20;
+
+    i->current_font_size = (xsize>ysize?xsize:ysize)*extrazoom;
+    if(i->current_font_size < 1)
+       i->current_font_size = 1;
+
+    MATRIX m;
+    swf_GetMatrix(0, &m);
+
+    if(m21 || m12 || fabs(m11+m22)>0.001) {
+       double ifs = (double)extrazoom/(i->current_font_size);
+       m.sx =  (S32)((m11*ifs)*65536); m.r1 = -(S32)((m21*ifs)*65536);
+       m.r0 =  (S32)((m12*ifs)*65536); m.sy = -(S32)((m22*ifs)*65536); 
+    }
+
+    /* this is the position of the first char to set a new fontmatrix-
+       we hope that it's close enough to all other characters using the
+       font, so we use its position as origin for the matrix */
+    m.tx = x*20;
+    m.ty = y*20;
+    i->fontmatrix = m;
+}
+
+
 static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
 {
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
@@ -2780,13 +3063,21 @@ static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*
        msg("<error> swf_drawchar called (glyph %d) without font", glyph);
        return;
     }
+
+    if(i->config_drawonlyshapes) {
+        gfxglyph_t*g = &font->glyphs[glyph];
+        gfxline_t*line2 = gfxline_clone(g->line);
+        gfxline_transform(line2, matrix);
+       dev->fill(dev, line2, color);
+        gfxline_free(line2);
+        return;
+    }
+
     if(!i->swffont || !i->swffont->name || strcmp((char*)i->swffont->name,font->id)) // not equal to current font
     {
-       /* TODO: remove the need for this (enhance getcharacterbbox so that it can cope
-                with multiple fonts */
-       endtext(dev);
        swf_switchfont(dev, font->id); // set the current font
     }
+
     if(!i->swffont) {
        msg("<warning> swf_drawchar: Font is NULL");
        return;
@@ -2795,6 +3086,7 @@ static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*
        msg("<warning> No character %d in font %s (%d chars)", glyph, FIXNULL((char*)i->swffont->name), i->swffont->numchars);
        return;
     }
+    glyph = i->swffont->glyph2glyph[glyph];
     
     setfontscale(dev, matrix->m00, matrix->m01, matrix->m10, matrix->m11, matrix->tx, matrix->ty, 0);
     
@@ -2802,7 +3094,7 @@ static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*
                 i->fontmatrix.r0/65536.0 * i->fontmatrix.r1/65536.0;
     if(fabs(det) < 0.0005) { 
        /* x direction equals y direction- the text is invisible */
-       msg("<verbose> Not drawing invisible character character %d (det=%f, m=[%f %f;%f %f]\n", glyph, 
+       msg("<verbose> Not drawing invisible character %d (det=%f, m=[%f %f;%f %f]\n", glyph, 
                det,
                i->fontmatrix.sx/65536.0, i->fontmatrix.r1/65536.0, 
                i->fontmatrix.r0/65536.0, i->fontmatrix.sy/65536.0);
@@ -2832,13 +3124,22 @@ static void swf_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*
     
     if(i->shapeid>=0)
        endshape(dev);
-    if(i->textid<0)
+    if(!i->textmode)
        starttext(dev);
 
     msg("<trace> Drawing char %d in font %d at %d,%d in color %02x%02x%02x%02x", 
            glyph, i->swffont->id, x, y, color->r, color->g, color->b, color->a);
 
-    putcharacter(dev, i->swffont->id, glyph, x, y, i->current_font_size, *(RGBA*)color);
-    swf_FontUseGlyph(i->swffont, glyph);
+    if(color->a == 0 && i->config_invisibletexttofront) {
+       RGBA color2 = *(RGBA*)color;
+       if(i->config_flashversion>=8) {
+           // use "multiply" blend mode
+           color2.a = color2.r = color2.g = color2.b = 255;
+       }
+       i->topchardata = charbuffer_append(i->topchardata, i->swffont, glyph, x, y, i->current_font_size, color2, &i->fontmatrix);
+    } else {
+       i->chardata = charbuffer_append(i->chardata, i->swffont, glyph, x, y, i->current_font_size, *(RGBA*)color, &i->fontmatrix);
+    }
+    swf_FontUseGlyph(i->swffont, glyph, i->current_font_size);
     return;
 }