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*2);
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];
37 line[n*2-i-1].type = gfx_lineTo;
38 line[n*2-i-1].x = line[i].x;
39 line[n*2-i-1].y = line[i].y;
40 line[n*2-i-1].next = &line[n*2-i];
43 line[n-1].x = line[0].x;
44 line[n-1].y = line[0].y;
48 gfxline_t* mkchessboard()
57 char do_centerpiece=1;
61 //char do_centerpiece=0;
66 r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
70 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
72 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
74 b = gfxline_append(b, box);
79 for(t=0;t<num_caros;t++) {
80 r = crc32_add_byte(r, t);
81 int x=(r%10-5)*spacing;
82 int y=((r>>4)%10-5)*spacing;
83 int sizex = ((r>>8)%4)*spacing;
87 gfxline_t*l = malloc(sizeof(gfxline_t)*5);
88 l[0].type = gfx_moveTo;l[0].next = &l[1];
89 l[1].type = gfx_lineTo;l[1].next = &l[2];
90 l[2].type = gfx_lineTo;l[2].next = &l[3];
91 l[3].type = gfx_lineTo;l[3].next = &l[4];
92 l[4].type = gfx_lineTo;l[4].next = 0;
103 gfxline_append(b, l);
107 gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
109 memset(&matrix, 0, sizeof(gfxmatrix_t));
111 matrix.m00=cos(ua);matrix.m10=sin(ua);
112 matrix.m01=-sin(ua);matrix.m11=cos(ua);
113 gfxline_transform(l, &matrix);
114 gfxline_append(b, l);
119 static windcontext_t onepolygon = {1};
123 gfxline_t* b = mkchessboard();
126 memset(&m, 0, sizeof(gfxmatrix_t));
129 m.m00 = cos(t*M_PI/180.0);
130 m.m01 = sin(t*M_PI/180.0);
131 m.m10 = -sin(t*M_PI/180.0);
132 m.m11 = cos(t*M_PI/180.0);
135 gfxline_transform(b, &m);
137 gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
138 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
139 gfxpoly_destroy(poly2);
140 gfxpoly_destroy(poly);
144 int test1(int argn, char*argv[])
146 gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
147 // put box2 and box3 on top of each other *snicker*
148 gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
149 gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
150 gfxline_t*star = mkstar(50,50, 150,150);
152 b = gfxline_append(b, box1);
153 b = gfxline_append(b, box2);
154 b = gfxline_append(b, box3);
155 //b = gfxline_append(b, star);
158 memset(&matrix, 0, sizeof(gfxmatrix_t));
160 matrix.m00=cos(ua);matrix.m10=sin(ua);
161 matrix.m01=-sin(ua);matrix.m11=cos(ua);
163 //gfxline_transform(b, &matrix);
165 gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
172 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd, &onepolygon);
173 gfxpoly_destroy(poly2);
174 gfxpoly_destroy(poly);
177 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
180 gfxline_t* line = malloc(sizeof(gfxline_t)*num);
182 line[t].type = t?gfx_lineTo:gfx_moveTo;
183 line[t].x = (lrand48()%width);
184 line[t].y = (lrand48()%height);
185 line[t].next = &line[t+1];
187 line[num-1].x = line[0].x;
188 line[num-1].y = line[0].y;
189 line[num-1].next = 0;
191 gfxpoly_t*poly = gfxpoly_from_gfxline(line, gridsize);
194 windrule_t*rule = &windrule_circular;
195 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
197 intbbox_t bbox = intbbox_new(0, 0, width, height);
198 unsigned char*bitmap1 = render_polygon(poly, &bbox, 1.0, rule, &onepolygon);
199 unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd, &onepolygon);
200 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
201 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
202 assert(!"bitmaps don't match");
205 gfxpoly_destroy(poly2);
206 gfxpoly_destroy(poly);
209 int test2(int argn, char*argv[])
211 test_square(400,400, 3, 0.05, 1);
215 test_square(400,400, 50, 0.05, 1);
216 test_square(200,3, 1000, 1.0, 0);
217 test_square(3,200, 1000, 1.0, 0);
218 test_square(10,10, 200, 1.0, 0);
222 #include "../rfxswf.h"
223 void test3(int argn, char*argv[])
230 //gfxline_t*line = mkrandomshape(RANGE, N);
231 //windrule_t*rule = &windrule_circular;
232 gfxline_t*line = mkchessboard();
233 //windrule_t*rule = &windrule_evenodd;
234 windrule_t*rule = &windrule_circular;
237 memset(&m, 0, sizeof(m));
240 memset(&swf, 0, sizeof(SWF));
241 swf.movieSize.xmax = RANGE*20*1.41;
242 swf.movieSize.ymax = RANGE*20*1.41;
244 swf.frameRate = 25*0x100;
245 TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
252 m.m00 = cos(t*M_PI/180.0);
253 m.m01 = sin(t*M_PI/180.0);
254 m.m10 = -sin(t*M_PI/180.0);
255 m.m11 = cos(t*M_PI/180.0);
259 gfxline_t*l = gfxline_clone(line);
260 gfxline_transform(l, &m);
262 gfxpoly_t*poly = gfxpoly_from_gfxline(l, 0.05);
263 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
265 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
269 rgb.r = rgb.g = 0x00; rgb.b = 0xff;
271 int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
272 int ls = swf_ShapeAddLineStyle(s,20,&rgb);
274 swf_SetRect(tag,&swf.movieSize);
275 swf_SetShapeHeader(tag,s);
279 swf_ShapeSetAll(tag,s,0,0,0,fs,0);
280 edge_t*e = poly2->edges;
284 swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
285 swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
287 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
288 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
293 swf_ShapeSetAll(tag,s,0,0,ls,0,0);
294 edge_t*e = poly2->edges;
296 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
297 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
299 swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
300 swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
305 swf_ShapeSetEnd(tag);
308 gfxpoly_destroy(poly);
309 gfxpoly_destroy(poly2);
314 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
317 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
318 swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
320 tag = swf_InsertTag(tag, ST_SHOWFRAME);
322 tag = swf_InsertTag(tag, ST_END);
324 swf_SaveSWF(&swf, "test.swf");
327 void rotate90(gfxpoly_t*poly)
329 edge_t*e = poly->edges;
342 void test4(int argn, char*argv[])
345 DIR*_dir = opendir(dir);
349 file = readdir(_dir);
352 if(!strstr(file->d_name, ".ps"))
358 filename = allocprintf("%s/%s", dir, file->d_name);
362 windrule_t*rule = &windrule_evenodd;
363 gfxpoly_t*poly = gfxpoly_from_file(filename, 1.0);//0.01);
370 if(!gfxpoly_check(poly)) {
371 printf("bad polygon\n");
375 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
378 for(pass=0;pass<2;pass++) {
379 intbbox_t bbox = intbbox_from_polygon(poly, zoom);
380 unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule, &onepolygon);
381 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
382 if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
383 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
384 assert(!"error in bitmaps");
386 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
387 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
388 assert(!"bitmaps don't match");
393 // second pass renders the 90° rotated version
398 gfxpoly_destroy(poly);
399 gfxpoly_destroy(poly2);
406 #include "../gfxdevice.h"
407 #include "../pdf/pdf.h"
409 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
411 gfxpoly_t*poly = gfxpoly_from_gfxline(line, 0.05);
412 if(gfxpoly_size(poly)>100000) {
413 printf("%d segments (skipping)\n", gfxpoly_size(poly));
416 printf("%d segments\n", gfxpoly_size(poly));
419 if(!gfxpoly_check(poly)) {
420 gfxpoly_destroy(poly);
421 printf("bad polygon\n");
425 windrule_t*rule = &windrule_evenodd;
428 intbbox_t bbox = intbbox_from_polygon(poly, zoom);
429 unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule, &onepolygon);
430 if(!bitmap_ok(&bbox, bitmap1)) {
431 printf("bad polygon or error in renderer\n");
434 gfxpoly_t*poly2 = gfxpoly_process(poly, rule, &onepolygon);
435 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd, &onepolygon);
436 if(!bitmap_ok(&bbox, bitmap2)) {
437 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
438 assert(!"error in bitmap");
440 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
441 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
442 assert(!"bitmaps don't match");
447 gfxpoly_destroy(poly);
448 gfxpoly_destroy(poly2);
450 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
453 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
455 extract_polygons_fill(dev, line, 0);
457 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
459 extract_polygons_fill(dev, line, 0);
461 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
463 extract_polygons_fill(dev, line, 0);
465 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
467 extract_polygons_fill(dev, line, 0);
469 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
472 for(t=0;t<font->num_glyphs;t++) {
473 //extract_polygons_fill(dev, font->glyphs[t].line, 0);
476 void extract_polygons_endclip(gfxdevice_t*dev)
479 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)
482 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
486 gfxdevice_t extract_polygons =
488 name: "extract polygons",
489 setparameter:extract_polygons_setparameter,
490 startclip: extract_polygons_startclip,
491 endclip: extract_polygons_endclip,
492 stroke: extract_polygons_stroke,
493 fill: extract_polygons_fill,
494 fillbitmap: extract_polygons_fillbitmap,
495 fillgradient: extract_polygons_fillgradient,
496 addfont: extract_polygons_addfont,
497 drawchar: extract_polygons_drawchar,
498 drawlink: extract_polygons_drawlink,
506 void test5(int argn, char*argv[])
508 gfxsource_t*driver = gfxsource_pdf_create();
510 DIR*_dir = opendir(dir);
514 file = readdir(_dir);
517 if(!strstr(file->d_name, ".pdf"))
519 char* filename = allocprintf("%s/%s", dir, file->d_name);
521 gfxdocument_t*doc = driver->open(driver, filename);
522 gfxdevice_t*out = &extract_polygons;
524 for(t=1;t<=doc->num_pages;t++) {
525 printf("%s (page %d)\n", filename, t);
526 gfxpage_t* page = doc->getpage(doc, t);
527 page->render(page, out);
534 driver->destroy(driver);
537 int main(int argn, char*argv[])