6 #include "../gfxtools.h"
9 #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*2);
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];
38 line[n*2-i-1].type = gfx_lineTo;
39 line[n*2-i-1].x = line[i].x;
40 line[n*2-i-1].y = line[i].y;
41 line[n*2-i-1].next = &line[n*2-i];
44 line[n-1].x = line[0].x;
45 line[n-1].y = line[0].y;
49 gfxline_t* mkchessboard()
58 //char do_centerpiece=1;
62 char do_centerpiece=0;
67 r = crc32_add_byte(r, x);r = crc32_add_byte(r, y);
71 box = gfxline_makerectangle(x*spacing,y*spacing,(x+1)*spacing,(y+1)*spacing);
73 box = gfxline_makerectangle((x+1)*spacing,y*spacing,x*spacing,(y+1)*spacing);
75 b = gfxline_append(b, box);
80 for(t=0;t<num_caros;t++) {
81 r = crc32_add_byte(r, t);
82 int x=(r%10-5)*spacing;
83 int y=((r>>4)%10-5)*spacing;
84 int sizex = ((r>>8)%4)*spacing;
88 gfxline_t*l = malloc(sizeof(gfxline_t)*5);
89 l[0].type = gfx_moveTo;l[0].next = &l[1];
90 l[1].type = gfx_lineTo;l[1].next = &l[2];
91 l[2].type = gfx_lineTo;l[2].next = &l[3];
92 l[3].type = gfx_lineTo;l[3].next = &l[4];
93 l[4].type = gfx_lineTo;l[4].next = 0;
104 gfxline_append(b, l);
108 gfxline_t*l = gfxline_makerectangle(-9*spacing,-10,9*spacing,10);
110 memset(&matrix, 0, sizeof(gfxmatrix_t));
112 matrix.m00=cos(ua);matrix.m10=sin(ua);
113 matrix.m01=-sin(ua);matrix.m11=cos(ua);
114 gfxline_transform(l, &matrix);
115 gfxline_append(b, l);
122 gfxline_t* b = mkchessboard();
125 memset(&m, 0, sizeof(gfxmatrix_t));
127 m.m00 = cos(t*M_PI/180.0);
128 m.m01 = sin(t*M_PI/180.0);
129 m.m10 = -sin(t*M_PI/180.0);
130 m.m11 = cos(t*M_PI/180.0);
133 gfxline_transform(b, &m);
135 gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
136 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd);
137 gfxpoly_destroy(poly2);
138 gfxpoly_destroy(poly);
143 gfxline_t*box1 = gfxline_makerectangle(50,50,150,150);
144 // put box2 and box3 on top of each other *snicker*
145 gfxline_t*box2 = gfxline_makerectangle(100,100,200,200);
146 gfxline_t*box3 = gfxline_makerectangle(100,100,200,200);
147 gfxline_t*star = mkstar(50,50, 150,150);
149 b = gfxline_append(b, box1);
150 b = gfxline_append(b, box2);
151 b = gfxline_append(b, box3);
152 //b = gfxline_append(b, star);
155 memset(&matrix, 0, sizeof(gfxmatrix_t));
157 matrix.m00=cos(ua);matrix.m10=sin(ua);
158 matrix.m01=-sin(ua);matrix.m11=cos(ua);
160 //gfxline_transform(b, &matrix);
162 gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
169 gfxpoly_t*poly2 = gfxpoly_process(poly, &windrule_evenodd);
170 gfxpoly_destroy(poly2);
171 gfxpoly_destroy(poly);
174 int test_square(int width, int height, int num, double gridsize, char bitmaptest)
177 gfxline_t* line = malloc(sizeof(gfxline_t)*num);
179 line[t].type = t?gfx_lineTo:gfx_moveTo;
180 line[t].x = (lrand48()%width);
181 line[t].y = (lrand48()%height);
182 line[t].next = &line[t+1];
184 line[num-1].x = line[0].x;
185 line[num-1].y = line[0].y;
186 line[num-1].next = 0;
188 gfxpoly_t*poly = gfxpoly_from_gfxline(line, gridsize);
191 windrule_t*rule = &windrule_circular;
192 gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
194 intbbox_t bbox = intbbox_new(0, 0, width, height);
195 unsigned char*bitmap1 = render_polygon(poly, &bbox, 1.0, rule);
196 unsigned char*bitmap2 = render_polygon(poly2, &bbox, 1.0, &windrule_evenodd);
197 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
198 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
199 assert(!"bitmaps don't match");
202 gfxpoly_destroy(poly2);
203 gfxpoly_destroy(poly);
208 test_square(400,400, 3, 0.05, 1);
212 test_square(400,400, 50, 0.05, 1);
213 test_square(200,3, 1000, 1.0, 0);
214 test_square(3,200, 1000, 1.0, 0);
215 test_square(10,10, 200, 1.0, 0);
219 #include "../rfxswf.h"
227 //gfxline_t*line = mkrandomshape(RANGE, N);
228 //windrule_t*rule = &windrule_circular;
229 gfxline_t*line = mkchessboard();
230 windrule_t*rule = &windrule_evenodd;
233 memset(&m, 0, sizeof(m));
236 memset(&swf, 0, sizeof(SWF));
237 swf.movieSize.xmax = RANGE*20*1.41;
238 swf.movieSize.ymax = RANGE*20*1.41;
240 swf.frameRate = 25*0x100;
241 TAG * tag = swf.firstTag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
248 m.m00 = cos(t*M_PI/180.0);
249 m.m01 = sin(t*M_PI/180.0);
250 m.m10 = -sin(t*M_PI/180.0);
251 m.m11 = cos(t*M_PI/180.0);
256 gfxline_t*l = gfxline_clone(line);
257 gfxline_transform(l, &m);
259 gfxpoly_t*poly = gfxpoly_from_gfxline(l, 0.05);
260 gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
262 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
266 rgb.r = rgb.g = 0x00; rgb.b = 0xff;
268 int fs = swf_ShapeAddSolidFillStyle(s,&rgb);
269 int ls = swf_ShapeAddLineStyle(s,20,&rgb);
271 swf_SetRect(tag,&swf.movieSize);
272 swf_SetShapeHeader(tag,s);
276 swf_ShapeSetAll(tag,s,0,0,0,fs,0);
277 edge_t*e = poly2->edges;
281 swf_ShapeSetMove(tag, s, e->a.y, e->a.x);
282 swf_ShapeSetLine(tag, s, e->b.y - e->a.y, e->b.x - e->a.x);
284 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
285 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
290 swf_ShapeSetAll(tag,s,0,0,ls,0,0);
291 edge_t*e = poly2->edges;
293 swf_ShapeSetMove(tag, s, e->a.x, e->a.y);
294 swf_ShapeSetLine(tag, s, e->b.x - e->a.x, e->b.y - e->a.y);
296 swf_ShapeSetCircle(tag, s, e->a.x, e->a.y, 5*20, 5*20);
297 swf_ShapeSetCircle(tag, s, e->b.x, e->b.y, 5*20, 5*20);
302 swf_ShapeSetEnd(tag);
305 gfxpoly_destroy(poly);
306 gfxpoly_destroy(poly2);
311 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
314 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
315 swf_ObjectPlace(tag,t+1,t+1,NULL,NULL,NULL);
317 tag = swf_InsertTag(tag, ST_SHOWFRAME);
319 tag = swf_InsertTag(tag, ST_END);
321 swf_SaveSWF(&swf, "test.swf");
328 DIR*_dir = opendir(dir);
332 file = readdir(_dir);
335 if(!strstr(file->d_name, ".ps"))
338 char* filename = allocprintf("%s/%s", dir, file->d_name);
339 windrule_t*rule = &windrule_evenodd;
340 gfxpoly_t*poly = gfxpoly_from_file(filename, 0.01);
344 intbbox_t bbox = intbbox_from_polygon(poly, zoom);
346 if(!gfxpoly_check(poly)) {
347 printf("bad polygon\n");
351 gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
352 unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule);
353 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd);
354 if(!bitmap_ok(&bbox, bitmap1) || !bitmap_ok(&bbox, bitmap2)) {
355 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
356 assert(!"error in bitmaps");
358 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
359 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
360 assert(!"bitmaps don't match");
362 gfxpoly_destroy(poly2);
366 #include "../gfxdevice.h"
367 #include "../pdf/pdf.h"
369 void extract_polygons_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
371 gfxpoly_t*poly = gfxpoly_from_gfxline(line, 0.05);
372 printf("%d segments\n", gfxpoly_size(poly));
374 if(!gfxpoly_check(poly)) {
375 gfxpoly_destroy(poly);
376 printf("bad polygon\n");
380 windrule_t*rule = &windrule_evenodd;
381 gfxpoly_t*poly2 = gfxpoly_process(poly, rule);
384 intbbox_t bbox = intbbox_from_polygon(poly, zoom);
385 unsigned char*bitmap1 = render_polygon(poly, &bbox, zoom, rule);
386 unsigned char*bitmap2 = render_polygon(poly2, &bbox, zoom, &windrule_evenodd);
387 if(!bitmap_ok(&bbox, bitmap1)) {
388 printf("bad polygon or error in renderer\n");
391 if(!bitmap_ok(&bbox, bitmap2)) {
392 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
393 assert(!"error in bitmap");
395 if(!compare_bitmaps(&bbox, bitmap1, bitmap2)) {
396 save_two_bitmaps(&bbox, bitmap1, bitmap2, "error.png");
397 assert(!"bitmaps don't match");
400 gfxpoly_destroy(poly);
401 gfxpoly_destroy(poly2);
403 int extract_polygons_setparameter(gfxdevice_t*dev, const char*key, const char*value) {
406 void extract_polygons_startclip(gfxdevice_t*dev, gfxline_t*line)
408 extract_polygons_fill(dev, line, 0);
410 void extract_polygons_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*imgcoord2devcoord, gfxcxform_t*cxform)
412 extract_polygons_fill(dev, line, 0);
414 void extract_polygons_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*gradcoord2devcoord)
416 extract_polygons_fill(dev, line, 0);
418 void extract_polygons_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
420 extract_polygons_fill(dev, line, 0);
422 void extract_polygons_addfont(gfxdevice_t*dev, gfxfont_t*font)
425 for(t=0;t<font->num_glyphs;t++) {
426 //extract_polygons_fill(dev, font->glyphs[t].line, 0);
429 void extract_polygons_endclip(gfxdevice_t*dev)
432 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)
435 void extract_polygons_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
439 gfxdevice_t extract_polygons =
441 name: "extract polygons",
442 setparameter:extract_polygons_setparameter,
443 startclip: extract_polygons_startclip,
444 endclip: extract_polygons_endclip,
445 stroke: extract_polygons_stroke,
446 fill: extract_polygons_fill,
447 fillbitmap: extract_polygons_fillbitmap,
448 fillgradient: extract_polygons_fillgradient,
449 addfont: extract_polygons_addfont,
450 drawchar: extract_polygons_drawchar,
451 drawlink: extract_polygons_drawlink,
462 DIR*_dir = opendir(dir);
466 file = readdir(_dir);
469 if(!strstr(file->d_name, ".pdf"))
471 char* filename = allocprintf("%s/%s", dir, file->d_name);
473 gfxsource_t*driver = gfxsource_pdf_create();
474 gfxdocument_t*doc = driver->open(driver, filename);
475 gfxdevice_t*out = &extract_polygons;
477 for(t=1;t<=doc->num_pages;t++) {
478 printf("%s (page %d)\n", filename, t);
479 gfxpage_t* page = doc->getpage(doc, t);
480 page->render(page, out);