5 #include "../gfxtools.h"
8 #include "renderpoly.h"
10 gfxline_t*mkstar(int x1, int y1, int x2, int y2)
12 gfxline_t*l=0,*line = 0;
14 for(x=x1;x<=x2;x+=50) {
15 l = rfx_calloc(sizeof(gfxline_t));
18 line = gfxline_append(line, l);
20 l = rfx_calloc(sizeof(gfxline_t));
22 l->x = x2-x;l->y = y2;
23 line = gfxline_append(line, l);
28 gfxline_t* mkrandomshape(int range, int n)
31 gfxline_t* line = malloc(sizeof(gfxline_t)*n);
33 line[i].type = i?gfx_lineTo:gfx_moveTo;
34 line[i].x = lrand48()%range - range/2;
35 line[i].y = lrand48()%range - range/2;
36 line[i].next = &line[i+1];
38 line[n-1].x = line[0].x;
39 line[n-1].y = line[0].y;
44 gfxline_t* mkchessboard()
53 char do_centerpiece=1;
57 //char do_centerpiece=0;
62 r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
66 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
68 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
70 b = gfxline_append(b, box);
75 for(t=0;t<num_caros;t++) {
76 r = crc32_add_byte(r, t);
77 int x=(r%10-5)*spacing;
78 int y=((r>>4)%10-5)*spacing;
79 int sizex = ((r>>8)%4)*spacing;
83 gfxline_t*l = malloc(sizeof(gfxline_t)*5);
84 l[0].type = gfx_moveTo;l[0].next = &l[1];
85 l[1].type = gfx_lineTo;l[1].next = &l[2];
86 l[2].type = gfx_lineTo;l[2].next = &l[3];
87 l[3].type = gfx_lineTo;l[3].next = &l[4];
88 l[4].type = gfx_lineTo;l[4].next = 0;
103 gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
105 memset(&matrix, 0, sizeof(gfxmatrix_t));
107 matrix.m00=cos(ua);matrix.m10=sin(ua);
108 matrix.m01=-sin(ua);matrix.m11=cos(ua);
109 gfxline_transform(l, &matrix);
110 gfxline_append(b, l);
112 gfxline_append(b, gfxline_makecircle(100,100,100,100));
117 gfxline_t* make_circles(int n)
123 c = crc32_add_byte(c, t);
125 c = crc32_add_byte(c, t);
127 c = crc32_add_byte(c, t^0x55);
129 b = gfxline_append(b, gfxline_makecircle(x,y,r,r));
130 //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
135 static windcontext_t onepolygon = {1};
139 //gfxline_t* b = mkchessboard();
140 //gfxline_t* b = mkrandomshape(100,7);
141 gfxline_t* b = make_circles(30);
144 memset(&m, 0, sizeof(gfxmatrix_t));
148 m.m00 = cos(t*M_PI/180.0);
149 m.m01 = sin(t*M_PI/180.0);
150 m.m10 = -sin(t*M_PI/180.0);
151 m.m11 = cos(t*M_PI/180.0);
154 gfxline_t*l = gfxline_clone(b);
155 gfxline_transform(l, &m);
156 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
158 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
159 gfxcompactpoly_destroy(poly);
160 gfxpoly_destroy(poly2);
166 int test1(int argn, char*argv[])
168 gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
169 gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
170 gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
171 gfxline_t*star = mkstar(50,50, 150,150);
173 b = gfxline_append(b, box1);
174 b = gfxline_append(b, box2);
175 b = gfxline_append(b, box3);
178 memset(&matrix, 0, sizeof(gfxmatrix_t));
180 matrix.m00=cos(ua);matrix.m10=sin(ua);
181 matrix.m01=-sin(ua);matrix.m11=cos(ua);
183 //gfxline_transform(b, &matrix);
185 gfxline_dump(b, stderr, "");
187 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
193 gfxcompactpoly_dump(poly);
194 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
195 gfxcompactpoly_destroy(poly);
196 gfxpoly_destroy(poly2);
199 static void test_conversion(gfxline_t*line, double gridsize)
202 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
203 gfxpoly_t*poly1 = gfxpoly_from_gfxline(line, gridsize);
204 gfxpoly_t*poly2 = gfxpoly_from_gfxcompactpoly(poly);
205 assert(gfxpoly_check(poly1));
206 assert(gfxpoly_check(poly2));
207 assert(gfxcompactpoly_check(poly));
208 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
209 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, &windrule_evenodd, &onepolygon);
210 assert(bitmap_ok(&bbox, bitmap1));
211 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
212 assert(bitmap_ok(&bbox, bitmap2));
213 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
214 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
215 assert(!"bitmaps don't match");
219 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
222 gfxline_t* line = malloc(sizeof(gfxline_t)*num);
224 line[t].type = t?gfx_lineTo:gfx_moveTo;
225 line[t].x = (lrand48()%width);
226 line[t].y = (lrand48()%height);
227 line[t].next = &line[t+1];
229 line[num-1].x = line[0].x;
230 line[num-1].y = line[0].y;
231 line[num-1].next = 0;
233 test_conversion(line, gridsize);
235 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
237 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
239 windrule_t*rule = &windrule_circular;
240 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
242 intbbox_t bbox = intbbox_new(0, 0, width, height);
243 unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
244 assert(bitmap_ok(&bbox, bitmap1));
245 unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
246 assert(bitmap_ok(&bbox, bitmap2));
247 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
248 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
249 assert(!"bitmaps don't match");
252 gfxpoly_destroy(poly1);
253 gfxpoly_destroy(poly2);
254 gfxcompactpoly_destroy(poly);
257 int test2(int argn, char*argv[])
259 test_square(400,400, 3, 0.05, 1);
263 fprintf(stderr, "%d\n", t);
264 test_square(400,400, 50, 0.05, 1);
265 test_square(200,3, 1000, 1.0, 0);
266 test_square(3,200, 1000, 1.0, 0);
267 test_square(10,10, 200, 1.0, 0);
271 #include "../rfxswf.h"
272 void test3(int argn, char*argv[])
279 //gfxline_t*line = mkrandomshape(RANGE, N);
280 //windrule_t*rule = &windrule_circular;
281 //gfxline_t*line = mkchessboard();
282 gfxline_t*line = make_circles(30);
283 windrule_t*rule = &windrule_evenodd;
284 //windrule_t*rule = &windrule_circular;
287 memset(&m, 0, sizeof(m));
290 memset(&swf, 0, sizeof(SWF));
291 swf.movieSize.xmax = RANGE*20*1.41;
292 swf.movieSize.ymax = RANGE*20*1.41;
294 swf.frameRate = 25*0x100;
295 TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
302 fprintf(stderr, "%d\n", t);
303 m.m00 = cos(t*M_PI/180.0);
304 m.m01 = sin(t*M_PI/180.0);
305 m.m10 = -sin(t*M_PI/180.0);
306 m.m11 = cos(t*M_PI/180.0);
310 gfxline_t*l = gfxline_clone(line);
311 gfxline_transform(l, &m);
313 test_conversion(l, 0.05);
315 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(l, 0.05);
316 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
318 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
322 rgb.r = rgb.g = 0x00; rgb.b = 0xff;
324 int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
325 int ls = swf_ShapeAddLineStyle(s,20,&rgb);
327 swf_SetRect(tag,&swf.movieSize);
328 swf_SetShapeHeader(tag,s);
332 swf_ShapeSetAll(tag,s,0,0,0,fs,0);
333 edge_t*e = poly2->edges;
337 swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
338 swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
340 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
341 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
346 swf_ShapeSetAll(tag,s,0,0,ls,0,0);
347 edge_t*e = poly2->edges;
349 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
350 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
352 swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
353 swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
358 swf_ShapeSetEnd(tag);
361 gfxcompactpoly_destroy(poly);
362 gfxpoly_destroy(poly2);
367 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
370 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
371 swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
373 tag = swf_InsertTag(tag, ST_SHOWFRAME);
375 tag = swf_InsertTag(tag, ST_END);
377 swf_SaveSWF(&swf, "test.swf");
380 void rotate90(gfxpoly_t*poly)
382 edge_t*e = poly->edges;
395 void test4(int argn, char*argv[])
398 DIR*_dir = opendir(dir);
402 file = readdir(_dir);
405 if(!strstr(file->d_name, ".ps"))
411 filename = allocprintf("%s/%s", dir, file->d_name);
415 windrule_t*rule = &windrule_evenodd;
416 gfxcompactpoly_t*poly = gfxcompactpoly_from_file(filename, 1.0);//0.01);
423 if(!gfxcompactpoly_check(poly)) {
424 printf("bad polygon\n");
428 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
429 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
432 for(pass=0;pass<2;pass++) {
433 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
434 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
435 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
436 if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
437 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
438 assert(!"error in bitmaps");
440 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
441 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
442 assert(!"bitmaps don't match");
447 // second pass renders the 90° rotated version
452 gfxpoly_destroy(poly1);
453 gfxpoly_destroy(poly2);
454 gfxcompactpoly_destroy(poly);
461 #include "../gfxdevice.h"
462 #include "../pdf/pdf.h"
464 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
466 //gfxcompactpoly_t*c = gfxcompactpoly_from_gfxline(line, 0.05);
467 //gfxcompactpoly_free(c);
469 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, 0.05);
471 //gfxline_dump(line, stderr, "");
472 //gfxcompactpoly_dump(poly);
474 if(gfxcompactpoly_size(poly)>100000) {
475 fprintf(stderr, "%d segments (skipping)\n", gfxcompactpoly_size(poly));
478 //fprintf(stderr, "%d segments\n", gfxpoly_size(poly));
481 if(!gfxcompactpoly_check(poly)) {
482 gfxcompactpoly_destroy(poly);
483 fprintf(stderr, "bad polygon\n");
487 windrule_t*rule = &windrule_evenodd;
489 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
492 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
493 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
494 if(!bitmap_ok(&bbox, bitmap1)) {
495 fprintf(stderr, "bad polygon or error in renderer\n");
498 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
499 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
500 if(!bitmap_ok(&bbox, bitmap2)) {
501 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
502 assert(!"error in bitmap");
504 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
505 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
506 assert(!"bitmaps don't match");
511 gfxpoly_destroy(poly1);
512 gfxpoly_destroy(poly2);
513 gfxcompactpoly_destroy(poly);
515 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
518 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
520 extract_polygons_fill(dev, line, 0);
522 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
524 extract_polygons_fill(dev, line, 0);
526 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
528 extract_polygons_fill(dev, line, 0);
530 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
532 extract_polygons_fill(dev, line, 0);
534 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
537 for(t=0;t<font->num_glyphs;t++) {
538 //extract_polygons_fill(dev, font->glyphs[t].line, 0);
541 void extract_polygons_endclip(gfxdevice_t*dev)
544 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)
547 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
551 gfxdevice_t extract_polygons =
553 name: "extract polygons",
554 setparameter:extract_polygons_setparameter,
555 startclip: extract_polygons_startclip,
556 endclip: extract_polygons_endclip,
557 stroke: extract_polygons_stroke,
558 fill: extract_polygons_fill,
559 fillbitmap: extract_polygons_fillbitmap,
560 fillgradient: extract_polygons_fillgradient,
561 addfont: extract_polygons_addfont,
562 drawchar: extract_polygons_drawchar,
563 drawlink: extract_polygons_drawlink,
571 void test5(int argn, char*argv[])
573 gfxsource_t*driver = gfxsource_pdf_create();
575 DIR*_dir = opendir(dir);
579 file = readdir(_dir);
582 if(!strstr(file->d_name, ".pdf"))
584 char* filename = allocprintf("%s/%s", dir, file->d_name);
589 gfxdocument_t*doc = driver->open(driver, filename);
590 gfxdevice_t*out = &extract_polygons;
592 for(t=1;t<=doc->num_pages;t++) {
593 fprintf(stderr, "%s (page %d)\n", filename, t);
594 gfxpage_t* page = doc->getpage(doc, t);
595 page->render(page, out);
604 driver->destroy(driver);
607 int main(int argn, char*argv[])
609 test_speed(argn, argv);