added stub for image cache
[swftools.git] / pdf2swf / swfoutput.cc
index 0af7041..0488bf3 100644 (file)
@@ -61,7 +61,7 @@ struct fontlist_t
 
 double config_ppmsubpixels=0;
 double config_jpegsubpixels=0;
-int config_opennewwindow=0;
+int config_opennewwindow=1;
 int config_ignoredraworder=0;
 int config_drawonlyshapes=0;
 int config_jpegquality=85;
@@ -151,7 +151,7 @@ void swf_endclip(gfxdevice_t*dev);
 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);
 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color);
 void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform);
-void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
+void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix);
 
 static swfoutput_internal* init_internal_struct()
 {
@@ -160,8 +160,8 @@ static swfoutput_internal* init_internal_struct()
 
     i->storefont = 0;
     i->currentswfid = 0;
-    i->depth = 1;
-    i->startdepth = 1;
+    i->depth = 0;
+    i->startdepth = 0;
     i->linewidth = 0;
     i->shapeid = -1;
     i->textid = -1;
@@ -197,6 +197,19 @@ static swfoutput_internal* init_internal_struct()
     return i;
 };
 
+static int id_error = 0;
+
+static U16 getNewID(struct swfoutput* obj)
+{
+    swfoutput_internal*i = (swfoutput_internal*)obj->internal;
+    if(i->currentswfid == 65535) {
+       if(!id_error)
+           msg("<error> ID Table overflow");
+       id_error=1;
+    }
+    return ++i->currentswfid;
+}
+
 static void startshape(struct swfoutput* obj);
 static void starttext(struct swfoutput* obj);
 static void endshape(struct swfoutput* obj);
@@ -227,7 +240,7 @@ static int moveto(struct swfoutput*obj, TAG*tag, plotxy p0)
     }
     return 0;
 }
-static int moveto(struct swfoutput*obj, TAG*tag, float x, float y)
+static int moveto(struct swfoutput*obj, TAG*tag, double  x, double y)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
     plotxy p;
@@ -249,22 +262,34 @@ static void addPointToBBox(struct swfoutput*obj, int px, int py)
     }
 }
 
-static void plot(struct swfoutput*obj, int x, int y, TAG*tag)
+/*static void plot(struct swfoutput*obj, int x, int y, TAG*tag)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
-    int width = 3;
-    swf_ShapeSetLine(tag, i->shape,-width,-width);
-    swf_ShapeSetLine(tag, i->shape,width*2,0);
-    swf_ShapeSetLine(tag, i->shape,0,width*2);
-    swf_ShapeSetLine(tag, i->shape,-width*2,0);
-    swf_ShapeSetLine(tag, i->shape,0,-width*2);
+    int width = i->linewidth/4;
+    if(width > 5)
+       width = 5;
+    ////square
+    //swf_ShapeSetLine(tag, i->shape,-width,-width);
+    //swf_ShapeSetLine(tag, i->shape,width*2,0);
+    //swf_ShapeSetLine(tag, i->shape,0,width*2);
+    //swf_ShapeSetLine(tag, i->shape,-width*2,0);
+    //swf_ShapeSetLine(tag, i->shape,0,-width*2);
+    //swf_ShapeSetLine(tag, i->shape,width,width);
+   
+    // diamond
+    swf_ShapeSetLine(tag, i->shape,-width,0);
+    swf_ShapeSetLine(tag, i->shape,width,-width);
     swf_ShapeSetLine(tag, i->shape,width,width);
-    addPointToBBox(obj, x   ,y   );
-    addPointToBBox(obj, x+1 ,y+1 );
-}
+    swf_ShapeSetLine(tag, i->shape,-width,width);
+    swf_ShapeSetLine(tag, i->shape,-width,-width);
+    swf_ShapeSetLine(tag, i->shape,width,0);
+
+    addPointToBBox(obj, x-width ,y-width);
+    addPointToBBox(obj, x+width ,y+width);
+}*/
 
 // write a line-to command into the swf
-static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0, char issolesegment)
+static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
     int px = (int)(p0.x*20);
@@ -275,26 +300,26 @@ static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0, char issolesegment)
        swf_ShapeSetLine (tag, i->shape, rx,ry);
        addPointToBBox(obj, i->swflastx,i->swflasty);
        addPointToBBox(obj, px,py);
-    } else if(issolesegment && !i->fill) {
-       /* treat lines of length 0 as plots, making them
-         at least 1 twip wide so Flash will display them */
+    }/* else if(!i->fill) {
+       // treat lines of length 0 as plots, making them
+       // at least 1 twip wide so Flash will display them
        plot(obj, i->swflastx, i->swflasty, tag);
-    }
+    }*/
 
     i->shapeisempty = 0;
     i->swflastx+=rx;
     i->swflasty+=ry;
 }
-static void lineto(struct swfoutput*obj, TAG*tag, double x, double y, char issolesegment)
+static void lineto(struct swfoutput*obj, TAG*tag, double x, double y)
 {
     plotxy p;
     p.x = x;
     p.y = y;
-    lineto(obj,tag, p, issolesegment);
+    lineto(obj,tag, p);
 }
 
 // write a spline-to command into the swf
-static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end, char issolesegment)
+static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end)
 {
     swfoutput_internal*i = (swfoutput_internal*)obj->internal;
     int lastlastx = i->swflastx;
@@ -314,10 +339,10 @@ static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end, c
        addPointToBBox(obj, lastlastx   ,lastlasty   );
        addPointToBBox(obj, lastlastx+cx,lastlasty+cy);
        addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey);
-    } else if(issolesegment && !i->fill) {
-       /* treat splines of length 0 as plots */
+    }/* else if(!i->fill) {
+       // treat splines of length 0 as plots
        plot(obj, lastlastx, lastlasty, tag);
-    }
+    }*/
     i->shapeisempty = 0;
 }
 
@@ -326,7 +351,7 @@ static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end, c
 /*static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1)
 {
     moveto(obj, tag, p0);
-    lineto(obj, tag, p1, 0);
+    lineto(obj, tag, p1);
 }*/
 
 void resetdrawer(struct swfoutput*obj)
@@ -689,6 +714,12 @@ static int drawchar(struct swfoutput*obj, SWFFONT *swffont, char*character, int
                FIXNULL(character),charnr, u, FIXNULL((char*)swffont->name), swffont->numchars);
        return 0;
     }
+    /*if(swffont->glyph[charid].shape->bitlen <= 16) {
+       msg("<warning> Character '%s' (c=%d,u=%d), glyph %d in current charset (%s, %d characters) is empty", 
+               FIXNULL(character),charnr, u, charid, FIXNULL((char*)swffont->name), swffont->numchars);
+       return 0;
+    }*/
+
 
     if(i->shapeid>=0)
        endshape(obj);
@@ -762,6 +793,8 @@ static void endtext(swfoutput*obj)
     swf_GetMatrix(0, &m); /* set unit matrix- the real matrix is in the placeobject */
     swf_SetMatrix(i->tag,&m);
 
+    msg("<trace> Placing text (%d characters) as ID %d", i->chardatapos, i->textid);
+
     putcharacters(obj, i->tag);
     swf_SetU8(i->tag,0);
     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
@@ -769,7 +802,7 @@ static void endtext(swfoutput*obj)
     MATRIX m2;
     swf_MatrixJoin(&m2,&obj->fontmatrix, &i->page_matrix);
 
-    swf_ObjectPlace(i->tag,i->textid,/*depth*/i->depth++,&m2,NULL,NULL);
+    swf_ObjectPlace(i->tag,i->textid,/*depth*/++i->depth,&m2,NULL,NULL);
     i->textid = -1;
 }
 
@@ -881,14 +914,14 @@ void swfoutput_setfont(struct swfoutput*obj, char*fontid, char*filename)
         }
     }
 
-    swf_FontSetID(swffont, ++i->currentswfid);
+    swf_FontSetID(swffont, getNewID(obj));
     
     if(getScreenLogLevel() >= LOGLEVEL_DEBUG)  {
        // print font information
-       msg("<debug> Font %s (%s)",swffont->name, filename);
+       msg("<debug> Font %s (%s)",fontid, filename);
        msg("<debug> |   ID: %d", swffont->id);
        msg("<debug> |   Version: %d", swffont->version);
-       msg("<debug> |   Name: %s", fontid);
+       msg("<debug> |   Name: %s", swffont->name);
        msg("<debug> |   Numchars: %d", swffont->numchars);
        msg("<debug> |   Maxascii: %d", swffont->maxascii);
        msg("<debug> |   Style: %d", swffont->style);
@@ -1005,7 +1038,7 @@ void swfoutput_pagefeed(struct swfoutput*obj)
     i->tag = swf_InsertTag(i->tag,ST_SHOWFRAME);
     i->frameno ++;
     
-    for(i->depth--;i->depth>=i->startdepth;i->depth--) {
+    for(i->depth;i->depth>i->startdepth;i->depth--) {
         i->tag = swf_InsertTag(i->tag,ST_REMOVEOBJECT2);
         swf_SetU16(i->tag,i->depth);
     }
@@ -1020,7 +1053,7 @@ static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
     SRECT r;
     SHAPE* s;
     int ls1=0,fs1=0;
-    int shapeid = ++i->currentswfid;
+    int shapeid = getNewID(obj);
     r.xmin = x1;
     r.ymin = y1;
     r.xmax = x2;
@@ -1039,9 +1072,9 @@ static void setBackground(struct swfoutput*obj, int x1, int y1, int x2, int y2)
     swf_ShapeSetEnd(i->tag);
     swf_ShapeFree(s);
     i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
-    swf_ObjectPlace(i->tag,shapeid,i->depth++,0,0,0);
+    swf_ObjectPlace(i->tag,shapeid,++i->depth,0,0,0);
     i->tag = swf_InsertTag(i->tag, ST_PLACEOBJECT2);
-    swf_ObjectPlaceClip(i->tag,shapeid,i->depth++,0,0,0,65535);
+    swf_ObjectPlaceClip(i->tag,shapeid,++i->depth,0,0,0,65535);
     i->cliptag = i->tag;
 }
 
@@ -1058,7 +1091,7 @@ void swfoutput_newpage(struct swfoutput*obj, int pageNum, int movex, int movey,
     if(i->cliptag && i->frameno == i->lastframeno) {
         SWFPLACEOBJECT obj;
         swf_GetPlaceObject(i->cliptag, &obj);
-        obj.clipdepth = i->depth++;
+        obj.clipdepth = i->depth;
         swf_ResetTag(i->cliptag, i->cliptag->id);
         swf_SetPlaceObject(i->cliptag, &obj);
         swf_PlaceObjectFree(&obj);
@@ -1120,7 +1153,7 @@ void swfoutput_init(struct swfoutput* obj)
     rgb.a = rgb.r = rgb.g = rgb.b = 0xff;
     swf_SetRGB(i->tag,&rgb);
 
-    i->startdepth = i->depth = 3; /* leave room for clip and background rectangle */
+    i->startdepth = i->depth = 0;
     
     if(config_protect)
       i->tag = swf_InsertTag(i->tag, ST_PROTECT);
@@ -1143,7 +1176,7 @@ static void startshape(struct swfoutput*obj)
     i->linestyleid = swf_ShapeAddLineStyle(i->shape,i->linewidth,&obj->strokergb);
     i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb);
 
-    i->shapeid = ++i->currentswfid;
+    i->shapeid = getNewID(obj);
     
     msg("<debug> Using shape id %d", i->shapeid);
 
@@ -1176,7 +1209,7 @@ static void starttext(struct swfoutput*obj)
     if(i->shapeid>=0)
         endshape(obj);
       
-    i->textid = ++i->currentswfid;
+    i->textid = getNewID(obj);
 
     i->swflastx=i->swflasty=0;
 }
@@ -1249,7 +1282,7 @@ void fixAreas(swfoutput*obj)
        startshape(obj);
 
        moveto(obj, i->tag, r.xmin/20.0,r.ymin/20.0);
-       lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0, 0);
+       lineto(obj, i->tag, r.xmax/20.0,r.ymax/20.0);
 
        obj->strokergb = save_col;
        i->linewidth = save_width;
@@ -1302,7 +1335,7 @@ static void endshape(swfoutput*obj)
     msg("<trace> Placing shape id %d", i->shapeid);
 
     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
-    swf_ObjectPlace(i->tag,i->shapeid,/*depth*/i->depth++,&i->page_matrix,NULL,NULL);
+    swf_ObjectPlace(i->tag,i->shapeid,/*depth*/++i->depth,&i->page_matrix,NULL,NULL);
 
     swf_ShapeFree(i->shape);
     i->shape = 0;
@@ -1406,7 +1439,6 @@ int swfoutput_save(struct swfoutput* obj, char*filename)
 
     if(filename)
      close(fi);
-    msg("<notice> SWF written\n");
     return 1;
 }
 
@@ -1499,7 +1531,7 @@ void swfoutput_linktourl(struct swfoutput*obj, char*url, swfcoord*points)
     if(i->textid>=0)
        endtext(obj);
     
-    if(config_opennewwindow)
+    if(!config_opennewwindow)
       actions = action_GetUrl(0, url, "_parent");
     else
       actions = action_GetUrl(0, url, "_this");
@@ -1590,7 +1622,7 @@ static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions
     double posx = 0;
     double posy = 0;
     int t;
-    int buttonid = ++i->currentswfid;
+    int buttonid = getNewID(obj);
     for(t=1;t<4;t++)
     {
         if(points[t].x>xmax) xmax=points[t].x;
@@ -1612,7 +1644,7 @@ static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions
     xmax -= posx; ymax -= posy;
     
     /* shape */
-    myshapeid = ++i->currentswfid;
+    myshapeid = getNewID(obj);
     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
     swf_ShapeNew(&i->shape);
     rgb.r = rgb.b = rgb.a = rgb.g = 0; 
@@ -1629,14 +1661,14 @@ static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions
     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
     i->swflastx = i->swflasty = 0;
     moveto(obj, i->tag, p1);
-    lineto(obj, i->tag, p2, 0);
-    lineto(obj, i->tag, p3, 0);
-    lineto(obj, i->tag, p4, 0);
-    lineto(obj, i->tag, p1, 0);
+    lineto(obj, i->tag, p2);
+    lineto(obj, i->tag, p3);
+    lineto(obj, i->tag, p4);
+    lineto(obj, i->tag, p1);
     swf_ShapeSetEnd(i->tag);
 
     /* shape2 */
-    myshapeid2 = ++i->currentswfid;
+    myshapeid2 = getNewID(obj);
     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE3);
     swf_ShapeNew(&i->shape);
     rgb.r = rgb.b = rgb.a = rgb.g = 255;
@@ -1654,10 +1686,10 @@ static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions
     swf_ShapeSetAll(i->tag,i->shape,/*x*/0,/*y*/0,0,fsid,0);
     i->swflastx = i->swflasty = 0;
     moveto(obj, i->tag, p1);
-    lineto(obj, i->tag, p2, 0);
-    lineto(obj, i->tag, p3, 0);
-    lineto(obj, i->tag, p4, 0);
-    lineto(obj, i->tag, p1, 0);
+    lineto(obj, i->tag, p2);
+    lineto(obj, i->tag, p3);
+    lineto(obj, i->tag, p4);
+    lineto(obj, i->tag, p1);
     swf_ShapeSetEnd(i->tag);
 
     if(!mouseover)
@@ -1707,10 +1739,9 @@ static void drawlink(struct swfoutput*obj, ActionTAG*actions1, ActionTAG*actions
         m = i->page_matrix;
         m.tx = p.x;
         m.ty = p.y;
-       swf_ObjectPlace(i->tag, buttonid, i->depth++,&m,0,0);
-    }
-    else {
-       swf_ObjectPlace(i->tag, buttonid, i->depth++,&i->page_matrix,0,0);
+       swf_ObjectPlace(i->tag, buttonid, ++i->depth,&m,0,0);
+    } else {
+       swf_ObjectPlace(i->tag, buttonid, ++i->depth,&i->page_matrix,0,0);
     }
 }
 
@@ -1765,16 +1796,6 @@ static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
        if(!line)
            break;
        /* check whether the next segment is zero */
-       char issolesegment = 0;
-       if(lastwasmoveto)
-           if(!line->next ||
-              (line->next->type == gfx_moveTo) ||
-              (line->next->type == gfx_lineTo   && fabs(line->next->x - line->x) + fabs(line->next->y - line->y) < 0.01) ||
-              (line->next->type == gfx_splineTo && fabs(line->next->x - line->x) + fabs(line->next->y - line->y) + fabs(line->next->sx - line->x) + fabs(line->next->sy - line->y) < 0.01)
-              ) { 
-                  issolesegment = 1;
-                  msg("<trace> is sole segment!");
-           }
        if(line->type == gfx_moveTo) {
            msg("<trace> ======== moveTo %.2f %.2f", line->x, line->y);
            moveto(obj, i->tag, line->x, line->y);
@@ -1783,7 +1804,7 @@ static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
            lastwasmoveto = 1;
        } if(line->type == gfx_lineTo) {
            msg("<trace> ======== lineTo %.2f %.2f", line->x, line->y);
-           lineto(obj, i->tag, line->x, line->y, issolesegment);
+           lineto(obj, i->tag, line->x, line->y);
            px = line->x;
            py = line->y;
            lastwasmoveto = 0;
@@ -1792,7 +1813,7 @@ static void drawgfxline(struct swfoutput*obj, gfxline_t*line)
            plotxy s,p;
            s.x = line->sx;p.x = line->x;
            s.y = line->sy;p.y = line->y;
-           splineto(obj, i->tag, s, p, issolesegment);
+           splineto(obj, i->tag, s, p);
            px = line->x;
            py = line->y;
            lastwasmoveto = 0;
@@ -2076,13 +2097,20 @@ gfxline_t* SVPtogfxline(ArtSVP*svp)
     }
 }
 
-
-
+/* TODO */
+static int imageInCache(swfoutput*obj, void*data, int width, int height)
+{
+    return -1;
+}
+static void addImageToCache(swfoutput*obj, void*data, int width, int height)
+{
+}
+    
 static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
 {
+    swfoutput*obj = i->obj;
     RGBA*newpic = 0;
     RGBA*mem = (RGBA*)img->data;
-    int bitid = ++i->currentswfid;
     
     int sizex = img->width;
     int sizey = img->height;
@@ -2109,6 +2137,7 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int
     *newheight  = sizey;
     
     if(newsizex<sizex || newsizey<sizey) {
+       msg("<notice> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
        newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
        *newwidth = sizex = newsizex;
        *newheight  = sizey = newsizey;
@@ -2128,35 +2157,30 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int
            /*newsizex, newsizey,*/
            num_colors>256?">":"", num_colors>256?256:num_colors);
 
-    if(has_alpha) {
-       if(num_colors<=256 || sizex<8 || sizey<8) {
-           i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSLOSSLESS2);
-           swf_SetU16(i->tag, bitid);
-           swf_SetLosslessImage(i->tag,mem,sizex,sizey);
-       } else {
-           /*TODO: check what is smaller */
-           i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSJPEG3);
-           swf_SetU16(i->tag, bitid);
-           swf_SetJPEGBits3(i->tag,sizex,sizey,mem,config_jpegquality);
-           //swf_SetLosslessImage(i->tag,mem,sizex,sizey);
-       }
+    /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
+    swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
+    int t;
+    for(t=0;t<num_colors;t++) {
+       printf("%02x%02x%02x%02x ",
+               pal[t].r, pal[t].g, pal[t].b, pal[t].a);
+       if((t&7)==7)
+           printf("\n");
+    }
+    printf("\n");*/
+
+    int bitid = -1;
+    int cacheid = imageInCache(obj, mem, sizex, sizey);
+
+    if(cacheid<=0) {
+       bitid = getNewID(obj);
+       i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, config_jpegquality);
+       addImageToCache(obj, mem, sizex, sizey);
     } else {
-       if(num_colors<=256 || sizex<8 || sizey<8) {
-           i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSLOSSLESS);
-           swf_SetU16(i->tag, bitid);
-           swf_SetLosslessImage(i->tag,mem,sizex,sizey);
-       } else {
-           /*TODO: check what is smaller */
-           i->tag = swf_InsertTag(i->tag,ST_DEFINEBITSJPEG2);
-           swf_SetU16(i->tag, bitid);
-           swf_SetJPEGBits2(i->tag,sizex,sizey,mem,config_jpegquality);
-           //swf_SetLosslessImage(i->tag,mem,sizex,sizey);
-       }
+       bitid = cacheid;
     }
-    
+
     if(newpic)
        free(newpic);
-
     return bitid;
 }
 
@@ -2198,7 +2222,7 @@ void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t
     m.ty = (int)(matrix->ty*20);
   
     /* shape */
-    int myshapeid = ++i->currentswfid;
+    int myshapeid = getNewID(obj);
     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
     SHAPE*shape;
     swf_ShapeNew(&shape);
@@ -2217,7 +2241,7 @@ void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t
 
     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
     CXFORM cxform2 = gfxcxform_to_cxform(cxform);
-    swf_ObjectPlace(i->tag,myshapeid,/*depth*/i->depth++,&i->page_matrix,&cxform2,NULL);
+    swf_ObjectPlace(i->tag,myshapeid,/*depth*/++i->depth,&i->page_matrix,&cxform2,NULL);
 }
 
 void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
@@ -2234,7 +2258,7 @@ void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
         i->clippos --;
     } 
 
-    int myshapeid = ++i->currentswfid;
+    int myshapeid = getNewID(obj);
     i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
     RGBA col;
     memset(&col, 0, sizeof(RGBA));
@@ -2258,7 +2282,7 @@ void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
     i->tag = swf_InsertTag(i->tag,ST_PLACEOBJECT2);
     i->cliptags[i->clippos] = i->tag;
     i->clipshapes[i->clippos] = myshapeid;
-    i->clipdepths[i->clippos] = i->depth++;
+    i->clipdepths[i->clippos] = ++i->depth;
     i->clippos++;
 }
 
@@ -2276,7 +2300,10 @@ void swf_endclip(gfxdevice_t*dev)
         return;
     }
     i->clippos--;
-    swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth++);
+    /*swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,
+           / * clip to depth: * / i->depth <= i->clipdepths[i->clippos]? i->depth : i->depth - 1);
+    i->depth ++;*/
+    swf_ObjectPlaceClip(i->cliptags[i->clippos],i->clipshapes[i->clippos],i->clipdepths[i->clippos],&i->page_matrix,NULL,NULL,i->depth);
 }
 int swf_setparameter(gfxdevice_t*dev, const char*key, const char*value)
 {
@@ -2316,6 +2343,58 @@ int gfxline_type(gfxline_t*line)
     else return 4;
 }
 
+int gfxline_has_dots(gfxline_t*line)
+{
+    int tmplines=0;
+    double x,y;
+    double dist = 0;
+    int isline = 0;
+    while(line) {
+       if(line->type == gfx_moveTo) {
+           if(isline && dist < 1) {
+               return 1;
+           }
+           dist = 0;
+           isline = 0;
+       } else if(line->type == gfx_lineTo) {
+           dist += fabs(line->x - x) + fabs(line->y - y);
+           isline = 1;
+       } else if(line->type == gfx_splineTo) {
+           dist += fabs(line->sx - x) + fabs(line->sy - y) + 
+                   fabs(line->x - line->sx) + fabs(line->y - line->sy);
+           isline = 1;
+       }
+       x = line->x;
+       y = line->y;
+       line = line->next;
+    }
+    if(isline && dist < 1) {
+       return 1;
+    }
+    return 0;
+}
+
+int gfxline_fix_short_edges(gfxline_t*line)
+{
+    double x,y;
+    while(line) {
+       if(line->type == gfx_lineTo) {
+           if(fabs(line->x - x) + fabs(line->y - y) < 0.01) {
+               line->x += 0.01;
+           }
+       } else if(line->type == gfx_splineTo) {
+           if(fabs(line->sx - x) + fabs(line->sy - y) + 
+              fabs(line->x - line->sx) + fabs(line->y - line->sy) < 0.01) {
+               line->x += 0.01;
+           }
+       }
+       x = line->x;
+       y = line->y;
+       line = line->next;
+    }
+    return 0;
+}
+
 int shapenr = 0;
 
 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)
@@ -2323,30 +2402,30 @@ void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*co
     swfoutput_internal*i = (swfoutput_internal*)dev->internal;
     swfoutput*obj = i->obj;
     int type = gfxline_type(line);
+    int has_dots = gfxline_has_dots(line);
 
     /* TODO: * split line into segments, and perform this check for all segments */
-    if(width <= config_caplinewidth 
-       || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
-       || (cap_style == gfx_capRound && type<=2)) {
-       msg("<trace> draw as stroke, type=%d", type);
+    if(!has_dots &&
+       (width <= config_caplinewidth 
+        || (cap_style == gfx_capRound && joint_style == gfx_joinRound)
+        || (cap_style == gfx_capRound && type<=2))) {
+       msg("<trace> draw as stroke, type=%d dots=%d", type, has_dots);
        endtext(obj);
        swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
        swfoutput_setlinewidth(obj, width);
        startshape(obj);
        stopFill(obj);
        drawgfxline(obj, line);
-       msg("<trace > shape bbox is (%f,%f,%f,%f)",
-               i->bboxrect.xmin /20.0,
-               i->bboxrect.ymin /20.0,
-               i->bboxrect.xmax /20.0,
-               i->bboxrect.ymax /20.0
-               );
     } else {
-       msg("<trace> draw as polygon, type=%d", type);
+       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 */
        ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
        gfxline_t*gfxline = SVPtogfxline(svp);
        dev->fill(dev, gfxline, color);
+       free(gfxline);
+       art_svp_free(svp);
     }
 }
 void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
@@ -2363,7 +2442,7 @@ void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
     drawgfxline(obj, line);
     msg("<trace> end of swf_fill (shapeid=%d)", i->shapeid);
 }
-void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
+void swf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
 {
     msg("<error> Gradient filling not implemented yet");
 }