X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fdevices%2Fpolyops.c;h=de035339a5dbd6cbb6f4abe160ac5f2ee194eb40;hb=7fb4a4ac393f19a0b8a8998a2f1deac88c97eda0;hp=e1935898bb14f07e2010eef26e57493123ec802e;hpb=525771a0aad2e2ca0b352465623e9ab5d1a4daf0;p=swftools.git diff --git a/lib/devices/polyops.c b/lib/devices/polyops.c index e193589..de03533 100644 --- a/lib/devices/polyops.c +++ b/lib/devices/polyops.c @@ -45,6 +45,9 @@ typedef struct _internal { gfxdevice_t*out; clip_t*clip; gfxpoly_t*polyunion; + + int good_polygons; + int bad_polygons; } internal_t; static int verbose = 0; @@ -57,7 +60,7 @@ static void dbg(char*format, ...) int l; va_list arglist; va_start(arglist, format); - vsprintf(buf, format, arglist); + vsnprintf(buf, sizeof(buf)-1, format, arglist); va_end(arglist); l = strlen(buf); while(l && buf[l-1]=='\n') { @@ -89,7 +92,12 @@ void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line) internal_t*i = (internal_t*)dev->internal; gfxpoly_t* oldclip = i->clip?i->clip->poly:0; - gfxpoly_t* poly = gfxpoly_fillToPoly(line); + gfxpoly_t* poly = gfxpoly_from_fill(line, DEFAULT_GRID); + if(poly) + i->good_polygons++; + else + i->bad_polygons++; + gfxpoly_t* currentclip = 0; int type = 0; @@ -97,13 +105,17 @@ void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line) a gfxline into a gfxpoly- for polygons which are too complex or just degenerate, this might fail. So handle all the cases where polygon conversion or intersection - might go awry */ + might go awry + UPDATE: this is not needed anymore. The new gfxpoly + implementation is stable enough so it always returns + a valid result. Still, it's good practice. + */ if(!poly && !oldclip) { i->out->startclip(i->out,line); currentclip = 0; type = 1; } else if(!poly && oldclip) { - gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip); + gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip); i->out->startclip(i->out,oldclipline); i->out->startclip(i->out,line); currentclip = 0; @@ -111,12 +123,14 @@ void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line) } else if(poly && oldclip) { gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip); if(intersection) { + i->good_polygons++; // this case is what usually happens - gfxpoly_free(poly);poly=0; + gfxpoly_destroy(poly);poly=0; currentclip = intersection; type = 0; } else { - gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip); + i->bad_polygons++; + gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip); i->out->startclip(i->out, oldclipline); currentclip = poly; type = 1; @@ -146,7 +160,7 @@ void polyops_endclip(struct _gfxdevice*dev) clip_t*old = i->clip; i->clip = i->clip->next; if(old->poly) { - gfxpoly_free(old->poly);old->poly = 0; + gfxpoly_destroy(old->poly);old->poly = 0; } int t; for(t=0;topenclips;t++) @@ -162,7 +176,7 @@ static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly) gfxpoly_t*old = i->polyunion; gfxpoly_t*newpoly = gfxpoly_union(poly,i->polyunion); i->polyunion = newpoly; - gfxpoly_free(old); + gfxpoly_destroy(old); } } @@ -173,15 +187,21 @@ static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok) gfxpoly_t*old = poly; if(poly) { poly = gfxpoly_intersect(poly, i->clip->poly); - gfxpoly_free(old); + gfxpoly_destroy(old); } } + + if(poly) + i->good_polygons++; + else + i->bad_polygons++; + addtounion(dev, poly); gfxline_t*gfxline = 0; if(poly) { // this is the case where everything went right - gfxline_t*line = gfxpoly_to_gfxline(poly); - gfxpoly_free(poly); + gfxline_t*line = gfxline_from_gfxpoly(poly); + gfxpoly_destroy(poly); *ok = 1; return line; } else { @@ -189,10 +209,10 @@ static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok) /* convert current clipping from a polygon to an actual "startclip" written to the output */ assert(i->clip->openclips <= 1); - gfxline_t*clipline = gfxpoly_to_gfxline(i->clip->poly); + gfxline_t*clipline = gfxline_from_gfxpoly(i->clip->poly); i->out->startclip(i->out, clipline); gfxline_free(clipline); - gfxpoly_free(i->clip->poly);i->clip->poly = 0; + gfxpoly_destroy(i->clip->poly);i->clip->poly = 0; i->clip->openclips++; return 0; } else { @@ -206,7 +226,7 @@ void polyops_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfx dbg("polyops_stroke"); internal_t*i = (internal_t*)dev->internal; - gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit); + gfxpoly_t* poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID); char ok = 0; gfxline_t*line2 = handle_poly(dev, poly, &ok); @@ -224,7 +244,7 @@ void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color) dbg("polyops_fill"); internal_t*i = (internal_t*)dev->internal; - gfxpoly_t*poly = gfxpoly_fillToPoly(line); + gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID); char ok = 0; gfxline_t*line2 = handle_poly(dev, poly, &ok); @@ -241,7 +261,7 @@ void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, g dbg("polyops_fillbitmap"); internal_t*i = (internal_t*)dev->internal; - gfxpoly_t*poly = gfxpoly_fillToPoly(line); + gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID); char ok = 0; gfxline_t*line2 = handle_poly(dev, poly, &ok); @@ -258,7 +278,7 @@ void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*g dbg("polyops_fillgradient"); internal_t*i = (internal_t*)dev->internal; - gfxpoly_t*poly = gfxpoly_fillToPoly(line); + gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID); char ok = 0; gfxline_t*line2 = handle_poly(dev, poly, &ok); @@ -288,7 +308,10 @@ void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcol if(i->clip && i->clip->poly) { gfxbbox_t bbox = gfxline_getbbox(glyph); - gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax); + gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax, DEFAULT_GRID); + gfxline_t*dummybox2 = gfxline_from_gfxpoly(dummybox); + bbox = gfxline_getbbox(dummybox2); + gfxline_free(dummybox2); char ok=0; gfxline_t*gfxline = handle_poly(dev, dummybox, &ok); @@ -296,12 +319,15 @@ void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcol gfxbbox_t bbox2 = gfxline_getbbox(gfxline); double w = bbox2.xmax - bbox2.xmin; double h = bbox2.ymax - bbox2.ymin; - if(w < 0.001 || h < 0.001) /* character was clipped completely */ { - } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 || - fabs((bbox.ymax - bbox.ymin) - h) > 0.05) { + if(fabs((bbox.xmax - bbox.xmin) - w) > DEFAULT_GRID*2 || + fabs((bbox.ymax - bbox.ymin) - h) > DEFAULT_GRID*2) { /* notable change in character size: character was clipped TODO: how to deal with diagonal cuts? */ + msg(" Character %d was clipped: (%f,%f,%f,%f) -> (%f,%f,%f,%f)", + glyphnr, + bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax, + bbox2.xmin,bbox2.ymin,bbox2.xmax,bbox2.ymax); polyops_fill(dev, glyph, color); } else { if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix); @@ -309,6 +335,7 @@ void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcol } else { if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix); } + gfxline_free(gfxline); } else { if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix); } @@ -336,10 +363,16 @@ gfxresult_t* polyops_finish(struct _gfxdevice*dev) internal_t*i = (internal_t*)dev->internal; if(i->polyunion) { - gfxpoly_free(i->polyunion);i->polyunion=0; + gfxpoly_destroy(i->polyunion);i->polyunion=0; + } else { + if(i->bad_polygons) { + msg(" --flatten success rate: %.1f%% (%d failed polygons)", i->good_polygons*100.0 / (i->good_polygons + i->bad_polygons), i->bad_polygons); + } } - if(i->out) { - return i->out->finish(i->out); + gfxdevice_t*out = i->out; + free(i);memset(dev, 0, sizeof(gfxdevice_t)); + if(out) { + return out->finish(out); } else { return 0; } @@ -348,7 +381,7 @@ gfxresult_t* polyops_finish(struct _gfxdevice*dev) gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev) { internal_t*i = (internal_t*)dev->internal; - return gfxpoly_to_gfxline(i->polyunion); + return gfxline_from_gfxpoly(i->polyunion); } void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out) @@ -404,6 +437,7 @@ void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out) dev->finish = polyops_finish; i->out = out; - i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0); + /* create empty polygon */ + i->polyunion = gfxpoly_from_stroke(0, 0, gfx_capButt, gfx_joinMiter, 0, DEFAULT_GRID); }