+static SRECT gfxline_getSWFbbox(gfxline_t*line)
+{
+ gfxbbox_t bbox = gfxline_getbbox(line);
+ SRECT r;
+ r.xmin = (int)(bbox.xmin*20);
+ r.ymin = (int)(bbox.ymin*20);
+ r.xmax = (int)(bbox.xmax*20);
+ r.ymax = (int)(bbox.ymax*20);
+ return r;
+}
+
+void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+ swfoutput*obj = i->obj;
+
+ endshape(obj);
+ endtext(obj);
+
+ int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
+ int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
+
+ int newwidth=0,newheight=0;
+ int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
+ if(bitid<0)
+ return;
+ double fx = (double)img->width / (double)newwidth;
+ double fy = (double)img->height / (double)newheight;
+
+ MATRIX m;
+ float m00,m10,tx;
+ float m01,m11,ty;
+ m.sx = (int)(65536*20*matrix->m00*fx); m.r1 = (int)(65536*20*matrix->m10*fy);
+ m.r0 = (int)(65536*20*matrix->m01*fx); m.sy = (int)(65536*20*matrix->m11*fy);
+ m.tx = (int)(matrix->tx*20);
+ m.ty = (int)(matrix->ty*20);
+
+ /* shape */
+ int myshapeid = ++i->currentswfid;
+ i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
+ SHAPE*shape;
+ swf_ShapeNew(&shape);
+ int fsid = swf_ShapeAddBitmapFillStyle(shape,&m,bitid,1);
+ swf_SetU16(i->tag, myshapeid);
+ SRECT r = gfxline_getSWFbbox(line);
+ swf_SetRect(i->tag,&r);
+ 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);
+ i->swflastx = i->swflasty = UNDEFINED_COORD;
+ drawgfxline(obj, line);
+ swf_ShapeSetEnd(i->tag);
+ swf_ShapeFree(shape);
+
+ 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);
+}
+
+void swf_startclip(gfxdevice_t*dev, gfxline_t*line)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+ swfoutput*obj = i->obj;
+
+ endtext(obj);
+ endshape(obj);
+
+ if(i->clippos >= 127)
+ {
+ msg("<warning> Too many clip levels.");
+ i->clippos --;
+ }
+
+ int myshapeid = ++i->currentswfid;
+ i->tag = swf_InsertTag(i->tag,ST_DEFINESHAPE);
+ RGBA col;
+ memset(&col, 0, sizeof(RGBA));
+ SHAPE*shape;
+ swf_ShapeNew(&shape);
+ int fsid = swf_ShapeAddSolidFillStyle(shape,&col);
+ swf_SetU16(i->tag,myshapeid);
+ SRECT r = gfxline_getSWFbbox(line);
+ swf_SetRect(i->tag,&r);
+ 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);
+ i->swflastx = i->swflasty = UNDEFINED_COORD;
+ drawgfxline(obj, line);
+ swf_ShapeSetEnd(i->tag);
+ swf_ShapeFree(shape);
+
+ /* TODO: remember the bbox, and check all shapes against it */
+
+ 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->clippos++;
+}
+
+void swf_endclip(gfxdevice_t*dev)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+ swfoutput*obj = i->obj;
+ if(i->textid>=0)
+ endtext(obj);
+ if(i->shapeid>=0)
+ endshape(obj);
+
+ if(!i->clippos) {
+ msg("<error> Invalid end of clipping region");
+ return;