5 #include "../gfxtools.h"
8 #include "renderpoly.h"
11 gfxline_t*mkstar(int x1, int y1, int x2, int y2)
13 gfxline_t*l=0,*line = 0;
15 for(x=x1;x<=x2;x+=50) {
16 l = rfx_calloc(sizeof(gfxline_t));
19 line = gfxline_append(line, l);
21 l = rfx_calloc(sizeof(gfxline_t));
23 l->x = x2-x;l->y = y2;
24 line = gfxline_append(line, l);
29 gfxline_t* mkrandomshape(int range, int n)
32 gfxline_t* line = malloc(sizeof(gfxline_t)*n);
34 line[i].type = i?gfx_lineTo:gfx_moveTo;
35 line[i].x = lrand48()%range - range/2;
36 line[i].y = lrand48()%range - range/2;
37 line[i].next = &line[i+1];
39 line[n-1].x = line[0].x;
40 line[n-1].y = line[0].y;
45 gfxline_t* mkchessboard()
54 char do_centerpiece=1;
58 //char do_centerpiece=0;
63 r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
67 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
69 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
71 b = gfxline_append(b, box);
76 for(t=0;t<num_caros;t++) {
77 r = crc32_add_byte(r, t);
78 int x=(r%10-5)*spacing;
79 int y=((r>>4)%10-5)*spacing;
80 int sizex = ((r>>8)%4)*spacing;
84 gfxline_t*l = malloc(sizeof(gfxline_t)*5);
85 l[0].type = gfx_moveTo;l[0].next = &l[1];
86 l[1].type = gfx_lineTo;l[1].next = &l[2];
87 l[2].type = gfx_lineTo;l[2].next = &l[3];
88 l[3].type = gfx_lineTo;l[3].next = &l[4];
89 l[4].type = gfx_lineTo;l[4].next = 0;
100 gfxline_append(b, l);
104 gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
106 memset(&matrix, 0, sizeof(gfxmatrix_t));
108 matrix.m00=cos(ua);matrix.m10=sin(ua);
109 matrix.m01=-sin(ua);matrix.m11=cos(ua);
110 gfxline_transform(l, &matrix);
111 gfxline_append(b, l);
113 gfxline_append(b, gfxline_makecircle(100,100,100,100));
118 gfxline_t* make_circles(int n)
124 c = crc32_add_byte(c, t);
126 c = crc32_add_byte(c, t);
128 c = crc32_add_byte(c, t^0x55);
130 gfxline_t*c = gfxline_makecircle(x,y,r,r);
131 b = gfxline_append(b, c);
132 //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100));
137 static windcontext_t onepolygon = {1};
138 static windcontext_t twopolygons = {2};
142 //gfxline_t* b = mkchessboard();
143 //gfxline_t* b = mkrandomshape(100,7);
144 gfxline_t* b = make_circles(30);
147 memset(&m, 0, sizeof(gfxmatrix_t));
151 m.m00 = cos(t*M_PI/180.0);
152 m.m01 = sin(t*M_PI/180.0);
153 m.m10 = -sin(t*M_PI/180.0);
154 m.m11 = cos(t*M_PI/180.0);
157 gfxline_t*l = gfxline_clone(b);
158 gfxline_transform(l, &m);
159 gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
161 gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon);
162 gfxpoly_destroy(poly);
163 gfxpoly_destroy(poly2);
169 int test0(int argn, char*argv[])
171 gfxline_t*box1 = gfxline_makerectangle(-100,-100,100,100);
172 gfxline_t*box2 = gfxline_makerectangle(-100,-100,100,100);
173 gfxline_t*box3 = gfxline_makerectangle(-100,-100,100,100);
174 //gfxline_append(box2, box3);
176 gfxpoly_check(gfxpoly_from_stroke(box1, 2.0, gfx_capRound, gfx_joinRound, 0, 0.05));
179 memset(&matrix, 0, sizeof(gfxmatrix_t));
181 matrix.m00=cos(ua);matrix.m10=sin(ua);
182 matrix.m01=-sin(ua);matrix.m11=cos(ua);
183 gfxline_transform(box1, &matrix);
186 //b = gfxline_append(b, box1);
187 //b = gfxline_append(b, box2);
188 //gfxline_dump(b, stderr, "");
190 gfxpoly_t*poly1 = gfxpoly_from_fill(box1, 0.05);
191 gfxpoly_t*poly2 = gfxpoly_from_fill(box2, 0.05);
196 //gfxpoly_t*poly3 = gfxpoly_process(poly1, poly2, &windrule_intersect, &twopolygons);
197 gfxpoly_t*poly3 = gfxpoly_process(poly1, poly2, &windrule_intersect, &twopolygons);
200 gfxline_t*line = gfxline_from_gfxpoly(poly3);
202 gfxline_dump(line, stdout, "");
204 gfxpoly_destroy(poly1);
205 gfxpoly_destroy(poly2);
206 gfxpoly_destroy(poly3);
210 int test1(int argn, char*argv[])
212 gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
213 gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
214 gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
215 gfxline_t*star = mkstar(50,50, 150,150);
217 b = gfxline_append(b, box1);
218 b = gfxline_append(b, box2);
219 b = gfxline_append(b, box3);
222 memset(&matrix, 0, sizeof(gfxmatrix_t));
224 matrix.m00=cos(ua);matrix.m10=sin(ua);
225 matrix.m01=-sin(ua);matrix.m11=cos(ua);
227 //gfxline_transform(b, &matrix);
229 gfxline_dump(b, stderr, "");
230 gfxpoly_t*poly = gfxpoly_from_fill(b, 0.05);
238 gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_evenodd, &onepolygon);
239 gfxpoly_destroy(poly);
240 gfxpoly_destroy(poly2);
243 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
246 gfxline_t* line = malloc(sizeof(gfxline_t)*num);
248 line[t].type = t?gfx_lineTo:gfx_moveTo;
249 line[t].x = (lrand48()%width);
250 line[t].y = (lrand48()%height);
251 line[t].next = &line[t+1];
253 line[num-1].x = line[0].x;
254 line[num-1].y = line[0].y;
255 line[num-1].next = 0;
257 gfxpoly_t*poly1 = gfxpoly_from_fill(line, gridsize);
260 windrule_t*rule = &windrule_circular;
261 gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
263 intbbox_t bbox = intbbox_new(0, 0, width, height);
264 unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
265 assert(bitmap_ok(&bbox, bitmap1));
266 unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
267 assert(bitmap_ok(&bbox, bitmap2));
268 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
269 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
270 assert(!"bitmaps don't match");
273 gfxpoly_destroy(poly1);
274 gfxpoly_destroy(poly2);
277 int test2(int argn, char*argv[])
279 test_square(400,400, 3, 0.05, 1);
283 fprintf(stderr, "%d\n", t);
284 test_square(400,400, 50, 0.05, 1);
285 test_square(200,3, 1000, 1.0, 0);
286 test_square(3,200, 1000, 1.0, 0);
287 test_square(10,10, 200, 1.0, 0);
291 #include "../rfxswf.h"
292 void test3(int argn, char*argv[])
299 //gfxline_t*line = mkrandomshape(RANGE, N);
300 //windrule_t*rule = &windrule_circular;
301 //gfxline_t*line = mkchessboard();
302 gfxline_t*line = make_circles(30);
303 windrule_t*rule = &windrule_evenodd;
304 //windrule_t*rule = &windrule_circular;
307 memset(&m, 0, sizeof(m));
310 memset(&swf, 0, sizeof(SWF));
311 swf.movieSize.xmax = RANGE*20*1.41;
312 swf.movieSize.ymax = RANGE*20*1.41;
314 swf.frameRate = 25*0x100;
315 TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
322 fprintf(stderr, "%d\n", t);
323 m.m00 = cos(t*M_PI/180.0);
324 m.m01 = sin(t*M_PI/180.0);
325 m.m10 = -sin(t*M_PI/180.0);
326 m.m11 = cos(t*M_PI/180.0);
330 gfxline_t*l = gfxline_clone(line);
331 gfxline_transform(l, &m);
333 gfxpoly_t*poly1 = gfxpoly_from_fill(l, 0.05);
335 gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
337 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
341 rgb.r = rgb.g = 0x00; rgb.b = 0xff;
343 int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
344 int ls = swf_ShapeAddLineStyle(s,20,&rgb);
346 swf_SetRect(tag,&swf.movieSize);
347 swf_SetShapeHeader(tag,s);
351 swf_ShapeSetAll(tag,s,UNDEFINED_COORD,UNDEFINED_COORD,0,fs,0);
354 gfxpolystroke_t*stroke = poly2->strokes;
355 for(;stroke;stroke=stroke->next) {
356 for(j=0;j<stroke->num_points-1;j++) {
357 point_t a = stroke->points[j];
358 point_t b = stroke->points[j+1];
361 swf_ShapeSetMove(tag, s, a.y, a.x);
362 swf_ShapeSetLine(tag, s, b.y - a.y, b.x - a.x);
364 swf_ShapeSetMove(tag, s, a.x, a.y);
365 swf_ShapeSetLine(tag, s, b.x - a.x, b.y - a.y);
370 swf_ShapeSetAll(tag,s,0,0,ls,0,0);
371 edge_t*e = poly2->edges;
373 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
374 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
376 swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
377 swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
382 swf_ShapeSetEnd(tag);
385 gfxpoly_destroy(poly1);
386 gfxpoly_destroy(poly2);
391 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
394 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
395 swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
397 tag = swf_InsertTag(tag, ST_SHOWFRAME);
399 tag = swf_InsertTag(tag, ST_END);
401 swf_SaveSWF(&swf, "test.swf");
404 void rotate90(gfxpoly_t*poly)
407 gfxpolystroke_t*stroke = poly->strokes;
408 for(;stroke;stroke=stroke->next) {
409 for(j=0;j<stroke->num_points;j++) {
410 point_t a = stroke->points[j];
411 stroke->points[j].x = a.y;
412 stroke->points[j].y = a.x;
418 void test4(int argn, char*argv[])
421 DIR*_dir = opendir(dir);
425 file = readdir(_dir);
428 if(!strstr(file->d_name, ".ps"))
434 filename = allocprintf("%s/%s", dir, file->d_name);
438 windrule_t*rule = &windrule_evenodd;
439 gfxpoly_t*poly1 = gfxpoly_from_file(filename, 1.0);//0.01);
446 if(!gfxpoly_check(poly1)) {
447 printf("bad polygon\n");
451 gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
454 for(pass=0;pass<2;pass++) {
455 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
456 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
457 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
458 if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
459 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
460 assert(!"error in bitmaps");
462 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
463 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
464 assert(!"bitmaps don't match");
469 // second pass renders the 90° rotated version
474 gfxpoly_destroy(poly1);
475 gfxpoly_destroy(poly2);
482 #include "../gfxdevice.h"
483 #include "../pdf/pdf.h"
485 static int max_segments = 0;
486 static int max_any_segments = 0;
487 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
489 //gfxpoly_t*c = gfxpoly_from_fill(line, 0.05);
492 //gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
493 gfxpoly_t*poly1 = gfxpoly_from_fill(line, 0.05);
495 //gfxline_dump(line, stderr, "");
496 //gfxpoly_dump(poly);
498 int size = gfxpoly_size(poly1);
500 //rectangles are boring.
501 gfxpoly_destroy(poly1);
505 max_any_segments = size > max_any_segments? size : max_any_segments;
507 fprintf(stderr, "%d segments (skipping)\n", size);
510 max_segments = size > max_segments? size : max_segments;
511 fprintf(stderr, "%d segments (max so far: %d/%d)\n", size, max_segments, max_any_segments);
514 if(!gfxpoly_check(poly1)) {
515 gfxpoly_destroy(poly1);
516 fprintf(stderr, "bad polygon\n");
520 windrule_t*rule = &windrule_evenodd;
523 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
524 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
525 if(!bitmap_ok(&bbox, bitmap1)) {
526 fprintf(stderr, "bad polygon or error in renderer\n");
529 gfxpoly_t*poly2 = gfxpoly_process(poly1, 0, rule, &onepolygon);
530 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
531 if(!bitmap_ok(&bbox, bitmap2)) {
532 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
533 assert(!"error in bitmap");
535 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
536 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
537 assert(!"bitmaps don't match");
542 gfxpoly_destroy(poly1);
543 gfxpoly_destroy(poly2);
545 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
548 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
550 extract_polygons_fill(dev, line, 0);
552 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
554 extract_polygons_fill(dev, line, 0);
556 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
558 extract_polygons_fill(dev, line, 0);
560 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
562 extract_polygons_fill(dev, line, 0);
564 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
567 for(t=0;t<font->num_glyphs;t++) {
568 //extract_polygons_fill(dev, font->glyphs[t].line, 0);
571 void extract_polygons_endclip(gfxdevice_t*dev)
574 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)
577 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
581 gfxdevice_t extract_polygons =
583 name: "extract polygons",
584 setparameter:extract_polygons_setparameter,
585 startclip: extract_polygons_startclip,
586 endclip: extract_polygons_endclip,
587 stroke: extract_polygons_stroke,
588 fill: extract_polygons_fill,
589 fillbitmap: extract_polygons_fillbitmap,
590 fillgradient: extract_polygons_fillgradient,
591 addfont: extract_polygons_addfont,
592 drawchar: extract_polygons_drawchar,
593 drawlink: extract_polygons_drawlink,
602 void test5(int argn, char*argv[])
604 gfxsource_t*driver = gfxsource_pdf_create();
606 DIR*_dir = opendir(dir);
610 file = readdir(_dir);
613 if(!strstr(file->d_name, ".pdf"))
615 char* filename = allocprintf("%s/%s", dir, file->d_name);
620 gfxdocument_t*doc = driver->open(driver, filename);
621 gfxdevice_t*out = &extract_polygons;
623 for(t=1;t<=doc->num_pages;t++) {
624 fprintf(stderr, "%s (page %d)\n", filename, t);
625 gfxpage_t* page = doc->getpage(doc, t);
626 page->render(page, out);
635 driver->destroy(driver);
639 int main(int argn, char*argv[])