+void swfoutput_drawimagejpeg(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
+ double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
+{
+ swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_JPEG);
+}
+
+void swfoutput_drawimagelossless(struct swfoutput*obj, RGBA*mem, int sizex,int sizey,
+ double x1,double y1, double x2,double y2, double x3,double y3, double x4,double y4)
+{
+ swfoutput_drawimage(obj,mem,sizex,sizey,x1,y1,x2,y2,x3,y3,x4,y4, IMAGE_TYPE_LOSSLESS);
+}
+
+void swfoutput_setparameter(char*name, char*value)
+{
+ if(!strcmp(name, "jpegsubpixels")) {
+ config_jpegsubpixels = atof(value);
+ } else if(!strcmp(name, "ppmsubpixels")) {
+ config_ppmsubpixels = atof(value);
+ } else if(!strcmp(name, "drawonlyshapes")) {
+ config_drawonlyshapes = atoi(value);
+ } else if(!strcmp(name, "ignoredraworder")) {
+ config_ignoredraworder = atoi(value);
+ } else if(!strcmp(name, "filloverlap")) {
+ config_filloverlap = atoi(value);
+ } else if(!strcmp(name, "linksopennewwindow")) {
+ config_opennewwindow = atoi(value);
+ } else if(!strcmp(name, "opennewwindow")) {
+ config_opennewwindow = atoi(value);
+ } else if(!strcmp(name, "storeallcharacters")) {
+ config_storeallcharacters = atoi(value);
+ } else if(!strcmp(name, "enablezlib")) {
+ config_enablezlib = atoi(value);
+ } else if(!strcmp(name, "insertstop")) {
+ config_insertstoptag = atoi(value);
+ } else if(!strcmp(name, "protected")) {
+ config_protect = atoi(value);
+ } else if(!strcmp(name, "flashversion")) {
+ config_flashversion = atoi(value);
+ } else if(!strcmp(name, "minlinewidth")) {
+ config_minlinewidth = atof(value);
+ } else if(!strcmp(name, "caplinewidth")) {
+ config_caplinewidth = atof(value);
+ } else if(!strcmp(name, "jpegquality")) {
+ int val = atoi(value);
+ if(val<0) val=0;
+ if(val>100) val=100;
+ config_jpegquality = val;
+ } else if(!strcmp(name, "splinequality")) {
+ int v = atoi(value);
+ v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
+ if(v<1) v = 1;
+ config_splinemaxerror = v;
+ } else if(!strcmp(name, "fontquality")) {
+ int v = atoi(value);
+ v = 500-(v*5); // 100% = 0.25 pixel, 0% = 25 pixel
+ if(v<1) v = 1;
+ config_fontsplinemaxerror = v;
+ } else {
+ fprintf(stderr, "unknown parameter: %s (=%s)\n", name, value);
+ }
+}
+
+// --------------------------------------------------------------------
+
+static CXFORM gfxcxform_to_cxform(gfxcxform_t* c)
+{
+ CXFORM cx;
+ swf_GetCXForm(0, &cx, 1);
+ if(!c)
+ return cx;
+ if(c->rg!=0 || c->rb!=0 || c->ra!=0 ||
+ c->gr!=0 || c->gb!=0 || c->ga!=0 ||
+ c->br!=0 || c->bg!=0 || c->ba!=0 ||
+ c->ar!=0 || c->ag!=0 || c->ab!=0)
+ msg("<warning> CXForm not SWF-compatible");
+
+ cx.a0 = (S16)(c->aa*256);
+ cx.r0 = (S16)(c->rr*256);
+ cx.g0 = (S16)(c->gg*256);
+ cx.b0 = (S16)(c->bb*256);
+ cx.a1 = c->t.a;
+ cx.r1 = c->t.r;
+ cx.g1 = c->t.g;
+ cx.b1 = c->t.b;
+ return cx;
+}
+
+ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
+{
+ ArtVpath *vec = NULL;
+ ArtSVP *svp = NULL;
+ int pos=0,len=0;
+ gfxline_t*l2;
+ double x=0,y=0;
+
+ l2 = line;
+ while(l2) {
+ if(l2->type == gfx_moveTo) {
+ pos ++;
+ } if(l2->type == gfx_lineTo) {
+ pos ++;
+ } if(l2->type == gfx_splineTo) {
+ int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
+ if(!parts) parts = 1;
+ pos += parts + 1;
+ }
+ x = l2->x;
+ y = l2->y;
+ l2 = l2->next;
+ }
+ pos++;
+ len = pos;
+
+ vec = art_new (ArtVpath, len);
+
+ pos = 0;
+ l2 = line;
+ while(l2) {
+ if(l2->type == gfx_moveTo) {
+ vec[pos].code = ART_MOVETO;
+ vec[pos].x = l2->x;
+ vec[pos].y = l2->y;
+ pos++;
+ assert(pos<=len);
+ } else if(l2->type == gfx_lineTo) {
+ vec[pos].code = ART_LINETO;
+ vec[pos].x = l2->x;
+ vec[pos].y = l2->y;
+ pos++;
+ assert(pos<=len);
+ } else if(l2->type == gfx_splineTo) {
+ int i;
+ int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))/3);
+ if(!parts) parts = 1;
+ for(i=0;i<=parts;i++) {
+ double t = (double)i/(double)parts;
+ vec[pos].code = ART_LINETO;
+ vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
+ vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
+ pos++;
+ assert(pos<=len);
+ }
+ }
+ x = l2->x;
+ y = l2->y;
+ l2 = l2->next;
+ }
+ vec[pos].code = ART_END;
+
+ svp = art_svp_vpath_stroke (vec,
+ (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
+ ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
+ ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
+ (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
+ ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
+ ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
+ width, //line_width
+ miterLimit, //miter_limit
+ 0.05 //flatness
+ );
+ free(vec);
+ return svp;
+}
+
+gfxline_t* SVPtogfxline(ArtSVP*svp)
+{
+ int size = 0;
+ int t;
+ int pos = 0;
+ for(t=0;t<svp->n_segs;t++) {
+ size += svp->segs[t].n_points + 1;
+ }
+ gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
+
+ for(t=0;t<svp->n_segs;t++) {
+ ArtSVPSeg* seg = &svp->segs[t];
+ int p;
+ for(p=0;p<seg->n_points;p++) {
+ lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
+ ArtPoint* point = &seg->points[p];
+ lines[pos].x = point->x;
+ lines[pos].y = point->y;
+ lines[pos].next = &lines[pos+1];
+ pos++;
+ }
+ }
+ if(pos) {
+ lines[pos-1].next = 0;
+ return lines;
+ } else {
+ return 0;
+ }
+}
+
+
+
+static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
+{
+ RGBA*newpic = 0;
+ RGBA*mem = (RGBA*)img->data;
+ int bitid = ++i->currentswfid;
+
+ int sizex = img->width;
+ int sizey = img->height;
+ int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
+ int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
+ int is_jpeg = i->jpeg;
+ i->jpeg = 0;
+
+ int newsizex=sizex, newsizey=sizey;
+
+ /// {
+ if(is_jpeg && config_jpegsubpixels) {
+ newsizex = (int)(targetwidth*config_jpegsubpixels+0.5);
+ newsizey = (int)(targetheight*config_jpegsubpixels+0.5);
+ } else if(!is_jpeg && config_ppmsubpixels) {
+ newsizex = (int)(targetwidth*config_ppmsubpixels+0.5);
+ newsizey = (int)(targetheight*config_ppmsubpixels+0.5);
+ }
+ /// }
+
+ *newwidth = newsizex;
+ *newheight = newsizey;
+
+
+ /* TODO: cache images */
+
+ msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
+ sizex, sizey,
+ has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
+ is_jpeg?"jpeg-":"",
+ newsizex, newsizey,
+ targetwidth, targetheight,
+ /*newsizex, newsizey,*/
+ num_colors>256?">":"", num_colors>256?256:num_colors);
+
+ if(newsizex!=sizex || newsizey!=sizey) {
+ newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
+ sizex = newsizex;
+ sizey = newsizey;
+ mem = newpic;
+ }
+
+ 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);
+ }
+ } else {
+ if(num_colors<=256 || sizex<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);
+ }
+ }
+
+ if(newpic)
+ free(newpic);
+
+ return bitid;
+}
+
+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);
+ 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;
+ }
+ i->clippos--;
+ 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)
+{
+ if(!strcmp(key, "next_bitmap_is_jpeg")) {
+ ((swfoutput_internal*)dev->internal)->jpeg = 1;
+ return 1;
+ }
+ return 0;
+}
+
+int gfxline_type(gfxline_t*line)
+{
+ int tmplines=0;
+ int tmpsplines=0;
+ int lines=0;
+ int splines=0;
+ while(line) {
+ if(line->type == gfx_moveTo) {
+ tmplines=0;
+ tmpsplines=0;
+ } else if(line->type == gfx_lineTo) {
+ tmplines++;
+ if(tmplines>lines)
+ lines=tmplines;
+ } else if(line->type == gfx_splineTo) {
+ tmpsplines++;
+ if(tmpsplines>lines)
+ splines=tmpsplines;
+ }
+ line = line->next;
+ }
+ if(lines==0 && splines==0) return 0;
+ else if(lines==1 && splines==0) return 1;
+ else if(lines==0 && splines==1) return 2;
+ else if(splines==0) return 3;
+ else return 4;
+}
+
+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)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+ swfoutput*obj = i->obj;
+ int type = gfxline_type(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)) {
+ endtext(obj);
+ swfoutput_setstrokecolor(obj, color->r, color->g, color->b, color->a);
+ swfoutput_setlinewidth(obj, width);
+ startshape(obj);
+ stopFill(obj);
+ drawgfxline(obj, line);
+ } else {
+ /* 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);
+ }
+}
+void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+ swfoutput*obj = i->obj;
+ endtext(obj);
+ if(!config_ignoredraworder)
+ endshape(obj);
+ swfoutput_setfillcolor(obj, color->r, color->g, color->b, color->a);
+ startshape(obj);
+ startFill(obj);
+ i->fill=1;
+ drawgfxline(obj, line);
+}
+void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
+{
+ msg("<error> Gradient filling not implemented yet");
+}