+#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*poly = gfxpoly_from_file(filename, 1.0);//0.01);
+
+ if(argn!=2)
+ free(filename);
+
+ double zoom = 1.0;
+ intbbox_t bbox = intbbox_from_polygon(poly, zoom);
+
+ if(!gfxpoly_check(poly)) {
+ printf("bad polygon\n");
+ continue;
+ }
+
+ gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
+ unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule);
+ unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd);
+ 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);
+ gfxpoly_destroy(poly);
+ gfxpoly_destroy(poly2);
+ if(argn==2)
+ break;
+ }
+ closedir(_dir);
+}
+
+#include "../gfxdevice.h"
+#include "../pdf/pdf.h"
+
+void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
+{
+ gfxpoly_t*poly = gfxpoly_from_gfxline(line, 0.05);
+ printf("%d segments\n", gfxpoly_size(poly));
+
+ if(!gfxpoly_check(poly)) {
+ gfxpoly_destroy(poly);
+ printf("bad polygon\n");
+ return;
+ }
+
+ windrule_t*rule = &windrule_evenodd;
+
+ double zoom = 1.0;
+ intbbox_t bbox = intbbox_from_polygon(poly, zoom);
+ unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule);
+ if(!bitmap_ok(&bbox, bitmap1)) {
+ printf("bad polygon or error in renderer\n");
+ return;
+ }
+ gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
+ unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd);
+ 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(poly);
+ 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[])
+{
+ gfxsource_t*driver = gfxsource_pdf_create();
+ char*dir = "pdfs";
+ DIR*_dir = opendir(dir);
+ if(!_dir) return;
+ struct dirent*file;
+ while(1) {
+ file = readdir(_dir);
+ if (!file)
+ break;
+ if(!strstr(file->d_name, ".pdf"))
+ continue;
+ char* filename = allocprintf("%s/%s", dir, file->d_name);
+
+ gfxdocument_t*doc = driver->open(driver, filename);
+ gfxdevice_t*out = &extract_polygons;
+ int t;
+ for(t=1;t<=doc->num_pages;t++) {
+ printf("%s (page %d)\n", filename, t);
+ gfxpage_t* page = doc->getpage(doc, t);
+ page->render(page, out);
+ page->destroy(page);
+ }
+ doc->destroy(doc);
+ free(filename);
+ }
+ closedir(_dir);
+ driver->destroy(driver);
+}
+
+int main(int argn, char*argv[])