X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=pdf2swf%2Fswfoutput.cc;h=6a891563761e0bf05cfe2123aa5de00d7ba937f4;hb=c3524ecd520460c80abf53cea7c6ab53b9679270;hp=d73e3dcd485dbc240e15545efb235a3f9b58ed1b;hpb=67fc212dc42bb9100097582e481521863ae0eab3;p=swftools.git diff --git a/pdf2swf/swfoutput.cc b/pdf2swf/swfoutput.cc index d73e3dc..6a89156 100644 --- a/pdf2swf/swfoutput.cc +++ b/pdf2swf/swfoutput.cc @@ -38,7 +38,7 @@ extern "C" { #include "../lib/gfxdevice.h" #include "../lib/gfxtools.h" } -#include "../lib/libart/libart.h" +#include "../lib/art/libart.h" #define CHARDATAMAX 8192 #define CHARMIDX 0 @@ -249,6 +249,32 @@ static void addPointToBBox(struct swfoutput*obj, int px, int py) } } +/*static void plot(struct swfoutput*obj, int x, int y, TAG*tag) +{ + swfoutput_internal*i = (swfoutput_internal*)obj->internal; + 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); + 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) { @@ -257,12 +283,15 @@ static void lineto(struct swfoutput*obj, TAG*tag, plotxy p0) int py = (int)(p0.y*20); int rx = (px-i->swflastx); int ry = (py-i->swflasty); - /* we can't skip this for rx=0,ry=0, those - are plots */ - swf_ShapeSetLine (tag, i->shape, rx,ry); - - addPointToBBox(obj, i->swflastx,i->swflasty); - addPointToBBox(obj, px,py); + if(rx|ry) { + swf_ShapeSetLine (tag, i->shape, rx,ry); + addPointToBBox(obj, i->swflastx,i->swflasty); + addPointToBBox(obj, px,py); + }/* 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; @@ -297,17 +326,20 @@ static void splineto(struct swfoutput*obj, TAG*tag, plotxy control,plotxy end) addPointToBBox(obj, lastlastx ,lastlasty ); addPointToBBox(obj, lastlastx+cx,lastlasty+cy); addPointToBBox(obj, lastlastx+cx+ex,lastlasty+cy+ey); - } + }/* else if(!i->fill) { + // treat splines of length 0 as plots + plot(obj, lastlastx, lastlasty, tag); + }*/ i->shapeisempty = 0; } /* write a line, given two points and the transformation matrix. */ -static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1) +/*static void line(struct swfoutput*obj, TAG*tag, plotxy p0, plotxy p1) { moveto(obj, tag, p0); lineto(obj, tag, p1); -} +}*/ void resetdrawer(struct swfoutput*obj) { @@ -1124,6 +1156,9 @@ static void startshape(struct swfoutput*obj) i->fillstyleid = swf_ShapeAddSolidFillStyle(i->shape,&obj->fillrgb); i->shapeid = ++i->currentswfid; + + msg(" Using shape id %d", i->shapeid); + swf_SetU16(i->tag,i->shapeid); // ID i->bboxrectpos = i->tag->len; @@ -1262,6 +1297,12 @@ static void endshape(swfoutput*obj) i->bboxrect.ymin == i->bboxrect.ymax)) { // delete the shape again, we didn't do anything + msg(" cancelling 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 + ); cancelshape(obj); return; } @@ -1270,6 +1311,8 @@ static void endshape(swfoutput*obj) changeRect(obj, i->tag, i->bboxrectpos, &i->bboxrect); + msg(" 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); @@ -1278,6 +1321,22 @@ static void endshape(swfoutput*obj) i->shapeid = -1; i->bboxrectpos = -1; i->fill=0; + + /*int debug = 1; + if(debug) { + char text[80]; + sprintf(text, "id%d", i->shapeid); + swfcoord points[4]; + points[0].x = i->bboxrect.xmin; + points[0].y = i->bboxrect.ymin; + points[1].x = i->bboxrect.xmax; + points[1].y = i->bboxrect.ymin; + points[2].x = i->bboxrect.xmax; + points[2].y = i->bboxrect.ymax; + points[3].x = i->bboxrect.xmin; + points[3].y = i->bboxrect.ymax; + swfoutput_namedlink(obj,text,points); + }*/ } void swfoutput_finalize(struct swfoutput*obj) @@ -1713,28 +1772,32 @@ static void drawgfxline(struct swfoutput*obj, gfxline_t*line) { swfoutput_internal*i = (swfoutput_internal*)obj->internal; gfxcoord_t lastx=0,lasty=0,px=0,py=0; - int needsfix = 0; + char lastwasmoveto; while(1) { if(!line) break; + /* check whether the next segment is zero */ if(line->type == gfx_moveTo) { + msg(" ======== moveTo %.2f %.2f", line->x, line->y); moveto(obj, i->tag, line->x, line->y); px = lastx = line->x; py = lasty = line->y; - needsfix = 0; + lastwasmoveto = 1; } if(line->type == gfx_lineTo) { + msg(" ======== lineTo %.2f %.2f", line->x, line->y); lineto(obj, i->tag, line->x, line->y); px = line->x; py = line->y; - needsfix = 1; + lastwasmoveto = 0; } else if(line->type == gfx_splineTo) { + msg(" ======== splineTo %.2f %.2f", line->x, line->y); 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); px = line->x; py = line->y; - needsfix = 1; + lastwasmoveto = 0; } line = line->next; } @@ -2015,8 +2078,6 @@ gfxline_t* SVPtogfxline(ArtSVP*svp) } } - - static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight) { RGBA*newpic = 0; @@ -2025,8 +2086,6 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int 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; @@ -2042,11 +2101,23 @@ static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int } /// } - *newwidth = newsizex; - *newheight = newsizey; - + if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0) + return -1; /* TODO: cache images */ + *newwidth = sizex; + *newheight = sizey; + + if(newsizex Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors", sizex, sizey, @@ -2057,39 +2128,8 @@ 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(newsizex!=sizex || newsizey!=sizey) { - newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey); - sizex = newsizex; - sizey = newsizey; - mem = newpic; - } + i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, config_jpegquality); - 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); @@ -2120,6 +2160,8 @@ void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t 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; @@ -2227,6 +2269,7 @@ int gfxline_type(gfxline_t*line) int tmpsplines=0; int lines=0; int splines=0; + int haszerosegments=0; while(line) { if(line->type == gfx_moveTo) { tmplines=0; @@ -2249,16 +2292,73 @@ 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) { 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)) { + if(!has_dots && + (width <= config_caplinewidth + || (cap_style == gfx_capRound && joint_style == gfx_joinRound) + || (cap_style == gfx_capRound && type<=2))) { + msg(" 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); @@ -2266,6 +2366,9 @@ void swf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*co stopFill(obj); drawgfxline(obj, line); } else { + msg(" 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); @@ -2284,6 +2387,7 @@ void swf_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color) startFill(obj); i->fill=1; drawgfxline(obj, line); + msg(" end of swf_fill (shapeid=%d)", i->shapeid); } void swf_fillgradient(gfxdevice_t*dev, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix) {