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()
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 = gfxline_makecircle(100,100,100,100);
144 memset(&m, 0, sizeof(gfxmatrix_t));
147 m.m00 = cos(t*M_PI/180.0);
148 m.m01 = sin(t*M_PI/180.0);
149 m.m10 = -sin(t*M_PI/180.0);
150 m.m11 = cos(t*M_PI/180.0);
153 gfxline_transform(b, &m);
154 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
156 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
157 gfxcompactpoly_destroy(poly);
158 gfxpoly_destroy(poly2);
163 int test1(int argn, char*argv[])
165 gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
166 gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
167 gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
168 gfxline_t*star = mkstar(50,50, 150,150);
170 b = gfxline_append(b, box1);
171 b = gfxline_append(b, box2);
172 b = gfxline_append(b, box3);
175 memset(&matrix, 0, sizeof(gfxmatrix_t));
177 matrix.m00=cos(ua);matrix.m10=sin(ua);
178 matrix.m01=-sin(ua);matrix.m11=cos(ua);
180 //gfxline_transform(b, &matrix);
182 gfxline_dump(b, stderr, "");
184 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(b, 0.05);
190 gfxcompactpoly_dump(poly);
191 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
192 gfxcompactpoly_destroy(poly);
193 gfxpoly_destroy(poly2);
196 static void test_conversion(gfxline_t*line, double gridsize)
199 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
200 gfxpoly_t*poly1 = gfxpoly_from_gfxline(line, gridsize);
201 gfxpoly_t*poly2 = gfxpoly_from_gfxcompactpoly(poly);
202 assert(gfxpoly_check(poly1));
203 assert(gfxpoly_check(poly2));
204 assert(gfxcompactpoly_check(poly));
205 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
206 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, &windrule_evenodd, &onepolygon);
207 assert(bitmap_ok(&bbox, bitmap1));
208 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
209 assert(bitmap_ok(&bbox, bitmap2));
210 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
211 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
212 assert(!"bitmaps don't match");
216 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
219 gfxline_t* line = malloc(sizeof(gfxline_t)*num);
221 line[t].type = t?gfx_lineTo:gfx_moveTo;
222 line[t].x = (lrand48()%width);
223 line[t].y = (lrand48()%height);
224 line[t].next = &line[t+1];
226 line[num-1].x = line[0].x;
227 line[num-1].y = line[0].y;
228 line[num-1].next = 0;
230 test_conversion(line, gridsize);
232 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, gridsize);
234 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
236 windrule_t*rule = &windrule_circular;
237 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
239 intbbox_t bbox = intbbox_new(0, 0, width, height);
240 unsigned char*bitmap1 = render_polygon(poly1, &bbox, 1.0, rule, &onepolygon);
241 assert(bitmap_ok(&bbox, bitmap1));
242 unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
243 assert(bitmap_ok(&bbox, bitmap2));
244 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
245 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
246 assert(!"bitmaps don't match");
249 gfxpoly_destroy(poly1);
250 gfxpoly_destroy(poly2);
251 gfxcompactpoly_destroy(poly);
254 int test2(int argn, char*argv[])
256 test_square(400,400, 3, 0.05, 1);
260 fprintf(stderr, "%d\n", t);
261 test_square(400,400, 50, 0.05, 1);
262 test_square(200,3, 1000, 1.0, 0);
263 test_square(3,200, 1000, 1.0, 0);
264 test_square(10,10, 200, 1.0, 0);
268 #include "../rfxswf.h"
269 void test3(int argn, char*argv[])
276 //gfxline_t*line = mkrandomshape(RANGE, N);
277 //windrule_t*rule = &windrule_circular;
278 //gfxline_t*line = mkchessboard();
279 gfxline_t*line = make_circles();
280 windrule_t*rule = &windrule_evenodd;
281 //windrule_t*rule = &windrule_circular;
284 memset(&m, 0, sizeof(m));
287 memset(&swf, 0, sizeof(SWF));
288 swf.movieSize.xmax = RANGE*20*1.41;
289 swf.movieSize.ymax = RANGE*20*1.41;
291 swf.frameRate = 25*0x100;
292 TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
299 fprintf(stderr, "%d\n", t);
300 m.m00 = cos(t*M_PI/180.0);
301 m.m01 = sin(t*M_PI/180.0);
302 m.m10 = -sin(t*M_PI/180.0);
303 m.m11 = cos(t*M_PI/180.0);
307 gfxline_t*l = gfxline_clone(line);
308 gfxline_transform(l, &m);
310 test_conversion(l, 0.05);
312 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(l, 0.05);
313 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
315 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
319 rgb.r = rgb.g = 0x00; rgb.b = 0xff;
321 int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
322 int ls = swf_ShapeAddLineStyle(s,20,&rgb);
324 swf_SetRect(tag,&swf.movieSize);
325 swf_SetShapeHeader(tag,s);
329 swf_ShapeSetAll(tag,s,0,0,0,fs,0);
330 edge_t*e = poly2->edges;
334 swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
335 swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
337 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
338 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
343 swf_ShapeSetAll(tag,s,0,0,ls,0,0);
344 edge_t*e = poly2->edges;
346 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
347 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
349 swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
350 swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
355 swf_ShapeSetEnd(tag);
358 gfxcompactpoly_destroy(poly);
359 gfxpoly_destroy(poly2);
364 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
367 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
368 swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
370 tag = swf_InsertTag(tag, ST_SHOWFRAME);
372 tag = swf_InsertTag(tag, ST_END);
374 swf_SaveSWF(&swf, "test.swf");
377 void rotate90(gfxpoly_t*poly)
379 edge_t*e = poly->edges;
392 void test4(int argn, char*argv[])
395 DIR*_dir = opendir(dir);
399 file = readdir(_dir);
402 if(!strstr(file->d_name, ".ps"))
408 filename = allocprintf("%s/%s", dir, file->d_name);
412 windrule_t*rule = &windrule_evenodd;
413 gfxcompactpoly_t*poly = gfxcompactpoly_from_file(filename, 1.0);//0.01);
420 if(!gfxcompactpoly_check(poly)) {
421 printf("bad polygon\n");
425 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
426 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
429 for(pass=0;pass<2;pass++) {
430 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
431 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
432 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
433 if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
434 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
435 assert(!"error in bitmaps");
437 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
438 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
439 assert(!"bitmaps don't match");
444 // second pass renders the 90° rotated version
449 gfxpoly_destroy(poly1);
450 gfxpoly_destroy(poly2);
451 gfxcompactpoly_destroy(poly);
458 #include "../gfxdevice.h"
459 #include "../pdf/pdf.h"
461 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
463 //gfxcompactpoly_t*c = gfxcompactpoly_from_gfxline(line, 0.05);
464 //gfxcompactpoly_free(c);
466 gfxcompactpoly_t*poly = gfxcompactpoly_from_gfxline(line, 0.05);
468 //gfxline_dump(line, stderr, "");
469 //gfxcompactpoly_dump(poly);
471 if(gfxcompactpoly_size(poly)>100000) {
472 fprintf(stderr, "%d segments (skipping)\n", gfxcompactpoly_size(poly));
475 //fprintf(stderr, "%d segments\n", gfxpoly_size(poly));
478 if(!gfxcompactpoly_check(poly)) {
479 gfxcompactpoly_destroy(poly);
480 fprintf(stderr, "bad polygon\n");
484 windrule_t*rule = &windrule_evenodd;
486 gfxpoly_t*poly1 = gfxpoly_from_gfxcompactpoly(poly);
489 intbbox_t bbox = intbbox_from_polygon(poly1, zoom);
490 unsigned char*bitmap1 = render_polygon(poly1, &bbox, zoom, rule, &onepolygon);
491 if(!bitmap_ok(&bbox, bitmap1)) {
492 fprintf(stderr, "bad polygon or error in renderer\n");
495 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
496 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
497 if(!bitmap_ok(&bbox, bitmap2)) {
498 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
499 assert(!"error in bitmap");
501 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
502 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
503 assert(!"bitmaps don't match");
508 gfxpoly_destroy(poly1);
509 gfxpoly_destroy(poly2);
510 gfxcompactpoly_destroy(poly);
512 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
515 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
517 extract_polygons_fill(dev, line, 0);
519 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
521 extract_polygons_fill(dev, line, 0);
523 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
525 extract_polygons_fill(dev, line, 0);
527 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
529 extract_polygons_fill(dev, line, 0);
531 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
534 for(t=0;t<font->num_glyphs;t++) {
535 //extract_polygons_fill(dev, font->glyphs[t].line, 0);
538 void extract_polygons_endclip(gfxdevice_t*dev)
541 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)
544 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
548 gfxdevice_t extract_polygons =
550 name: "extract polygons",
551 setparameter:extract_polygons_setparameter,
552 startclip: extract_polygons_startclip,
553 endclip: extract_polygons_endclip,
554 stroke: extract_polygons_stroke,
555 fill: extract_polygons_fill,
556 fillbitmap: extract_polygons_fillbitmap,
557 fillgradient: extract_polygons_fillgradient,
558 addfont: extract_polygons_addfont,
559 drawchar: extract_polygons_drawchar,
560 drawlink: extract_polygons_drawlink,
568 void test5(int argn, char*argv[])
570 gfxsource_t*driver = gfxsource_pdf_create();
572 DIR*_dir = opendir(dir);
576 file = readdir(_dir);
579 if(!strstr(file->d_name, ".pdf"))
581 char* filename = allocprintf("%s/%s", dir, file->d_name);
586 gfxdocument_t*doc = driver->open(driver, filename);
587 gfxdevice_t*out = &extract_polygons;
589 for(t=1;t<=doc->num_pages;t++) {
590 fprintf(stderr, "%s (page %d)\n", filename, t);
591 gfxpage_t* page = doc->getpage(doc, t);
592 page->render(page, out);
601 driver->destroy(driver);
604 int main(int argn, char*argv[])