+void rotate90(gfxpoly_t*poly)
+{
+ int i,j;
+ gfxpolystroke_t*stroke = poly->strokes;
+ for(;stroke;stroke=stroke->next) {
+ for(j=0;j<stroke->num_points;j++) {
+ point_t a = stroke->points[j];
+ stroke->points[j].x = a.y;
+ stroke->points[j].y = a.x;
+ }
+ }
+}
+
+#include <dirent.h>
+void test4(int argn, char*argv[])
+{
+ char*dir = "ps";
+ DIR*_dir = opendir(dir);
+ if(!_dir) return;
+ struct dirent*file;
+ while(1) {
+ file = readdir(_dir);
+ if (!file)
+ break;
+ if(!strstr(file->d_name, ".ps"))
+ continue;
+
+ char* filename;
+
+ if(argn<2)
+ filename = allocprintf("%s/%s", dir, file->d_name);
+ else
+ filename = argv[1];
+
+ windrule_t*rule = &windrule_evenodd;
+ gfxpoly_t*poly1 = gfxpoly_from_file(filename, 1.0);//0.01);
+
+ if(argn!=2)
+ free(filename);
+
+ double zoom = 1.0;
+
+ if(!gfxpoly_check(poly1)) {
+ printf("bad polygon\n");
+ continue;
+ }
+
+ gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
+
+ int pass;
+ for(pass=0;pass<2;pass++) {
+ intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+ unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
+ unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
+ if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"error in bitmaps");
+ }
+ if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"bitmaps don't match");
+ }
+ free(bitmap1);
+ free(bitmap2);
+
+ // second pass renders the 90° rotated version
+ rotate90(poly1);
+ rotate90(poly2);
+ }
+
+ gfxpoly_destroy(poly1);
+ gfxpoly_destroy(poly2);
+ if(argn==2)
+ break;
+ }
+ closedir(_dir);
+}
+
+#include "../gfxdevice.h"
+#include "../pdf/pdf.h"
+
+static int max_segments = 0;
+static int max_any_segments = 0;
+void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
+{
+ //gfxpoly_t*c = gfxpoly_from_fill(line, 0.05);
+ //gfxpoly_free(c);
+
+ //gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
+ gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
+
+ //gfxline_dump(line, stderr, "");
+ //gfxpoly_dump(poly);
+
+ int size = gfxpoly_size(poly1);
+ if(size == 4) {
+ //rectangles are boring.
+ gfxpoly_destroy(poly1);
+ return;
+ }
+
+ max_any_segments = size > max_any_segments? size : max_any_segments;
+ if(size>100000) {
+ fprintf(stderr, "%d segments (skipping)\n", size);
+ return;
+ } else {
+ max_segments = size > max_segments? size : max_segments;
+ fprintf(stderr, "%d segments (max so far: %d/%d)\n", size, max_segments, max_any_segments);
+ }
+
+ if(!gfxpoly_check(poly1)) {
+ gfxpoly_destroy(poly1);
+ fprintf(stderr, "bad polygon\n");
+ return;
+ }
+
+ windrule_t*rule = &windrule_evenodd;
+
+ double zoom = 1.0;
+ intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
+ unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
+ if(!bitmap_ok(&bbox, bitmap1)) {
+ fprintf(stderr, "bad polygon or error in renderer\n");
+ return;
+ }
+ gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
+ unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
+ if(!bitmap_ok(&bbox, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"error in bitmap");
+ }
+ if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
+ save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
+ assert(!"bitmaps don't match");
+ }
+ free(bitmap1);
+ free(bitmap2);
+
+ gfxpoly_destroy(poly1);
+ gfxpoly_destroy(poly2);
+}
+int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
+ return 0;
+}
+void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
+{
+ extract_polygons_fill(dev, line, 0);
+}
+void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
+{
+ int t;
+ for(t=0;t<font->num_glyphs;t++) {
+ //extract_polygons_fill(dev, font->glyphs[t].line, 0);
+ }
+}
+void extract_polygons_endclip(gfxdevice_t*dev)
+{
+}
+void extract_polygons_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 extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
+{
+}
+
+gfxdevice_t extract_polygons =
+{
+name: "extract polygons",
+setparameter:extract_polygons_setparameter,
+startclip: extract_polygons_startclip,
+endclip: extract_polygons_endclip,
+stroke: extract_polygons_stroke,
+fill: extract_polygons_fill,
+fillbitmap: extract_polygons_fillbitmap,
+fillgradient: extract_polygons_fillgradient,
+addfont: extract_polygons_addfont,
+drawchar: extract_polygons_drawchar,
+drawlink: extract_polygons_drawlink,
+startpage: 0,
+endpage: 0,
+geterror: 0,
+finish: 0,
+internal: 0
+};
+
+void test5(int argn, char*argv[])