/* Host test for svg_raster.c: coverage of basic shapes, fill rules, path * commands, transforms and strokes. `./svg_test out.ppm` also writes a * specimen image. */ #include #include #include #include "svg_raster.h" #define W 160 #define H 120 static unsigned short px[W * H]; static unsigned char al[W * H]; static struct svgr_layer L = { px, al, W, H }; static unsigned char ptbuf[8192 * 9]; static struct svgr R; static int fails; static void clear(void) { memset(px, 0, sizeof(px)); memset(al, 0, sizeof(al)); } static const double ident[6] = { 1, 0, 0, 1, 0, 0 }; static void check(const char *what, int cond) { if (!cond) { printf("FAIL %s\n", what); fails++; } } static unsigned int sum_alpha(void) { unsigned int i, s = 0; for (i = 0; i < W * H; i++) s += al[i]; return s; } static void path_fill(const char *d, const double m[6], int evenodd) { svgr_reset(&R, m); svgr_path(&R, d); svgr_fill(&R, &L, 0xFFFF, 255, evenodd); } int main(int argc, char **argv) { double m[6]; unsigned int s; svgr_init(&R, ptbuf, 8192); /* axis-aligned rect: exact coverage */ clear(); path_fill("M10 10 H30 V20 H10 Z", ident, 0); check("rect inside", al[15 * W + 15] == 255); check("rect outside", al[5 * W + 5] == 0 && al[15 * W + 30] == 0); s = sum_alpha(); check("rect area", s > 200 * 250 && s <= 200 * 255); /* half-pixel edge antialiasing */ clear(); path_fill("M10.5 10 L20 10 L20 20 L10.5 20 Z", ident, 0); check("half pixel", al[15 * W + 10] > 100 && al[15 * W + 10] < 160); /* relative commands, implicit lineto after m, and h/v */ clear(); path_fill("m10 10 20 0 0 10 h-20z", ident, 0); check("relative", al[15 * W + 15] == 255 && al[15 * W + 31] == 0); /* nonzero vs even-odd: two nested squares, same direction */ clear(); path_fill("M0 0H40V40H0Z M10 10H30V30H10Z", ident, 0); check("nonzero hole filled", al[20 * W + 20] == 255); clear(); path_fill("M0 0H40V40H0Z M10 10H30V30H10Z", ident, 1); check("evenodd hole", al[20 * W + 20] == 0 && al[5 * W + 5] == 255); /* circle via arcs: area ~ pi r^2 */ clear(); svgr_reset(&R, ident); svgr_ellipse(&R, 50, 50, 20, 20); svgr_fill(&R, &L, 0xFFFF, 255, 0); s = sum_alpha() / 255; check("circle area", s > 1220 && s < 1290); check("circle centre", al[50 * W + 50] == 255); /* cubic + smooth cubic + quadratic + arc in one path */ clear(); path_fill("M10 60 C10 20 50 20 50 60 S90 100 90 60 Q120 20 150 60 " "A20 20 0 0 1 110 60 Z", ident, 0); check("curves drew", sum_alpha() > 255 * 500); /* transform: scale(2) translate(5,5) of a 10x10 square */ clear(); memcpy(m, ident, sizeof(m)); svgr_transform(m, "scale(2) translate(5 5)"); path_fill("M0 0h10v10h-10z", m, 0); check("transform", al[15 * W + 15] == 255 && al[31 * W + 31] == 0 && al[9 * W + 9] == 0); memcpy(m, ident, sizeof(m)); svgr_transform(m, "rotate(90 20 20)"); check("rotate about point", (int)(m[4] + 0.5) == 40 && (int)(m[5] + 0.5) == 0); /* number grammar: ".5.5", exponents, negative separators */ { const char *p = ".5.5-1e1"; int ok; double a = svgr_num(&p, &ok), b = svgr_num(&p, &ok), c = svgr_num(&p, &ok); check("numbers", a == 0.5 && b == 0.5 && c == -10 && ok); } /* stroke: a 4px horizontal line */ clear(); svgr_reset(&R, ident); svgr_move(&R, 10, 50); svgr_line(&R, 60, 50); svgr_stroke(&R, &L, 4, 0xFFFF, 255); check("stroke body", al[49 * W + 30] == 255 && al[51 * W + 30] == 255); check("stroke edge", al[45 * W + 30] == 0); /* the tagesschau burger (cubic caps, s commands, implicit repeats) */ clear(); memcpy(m, ident, sizeof(m)); m[0] = m[3] = 3; path_fill("M1.5,9.02h32c0.83,0,1.5-0.67,1.5-1.5s-0.67-1.5-1.5-1.5h-32" "C0.67,6.02,0,6.69,0,7.52S0.67,9.02,1.5,9.02z", m, 0); check("burger bar", al[22 * W + 50] == 255 && al[30 * W + 50] == 0); if (argc > 1) { FILE *f = fopen(argv[1], "wb"); int i; clear(); memcpy(m, ident, sizeof(m)); m[0] = m[3] = 0.22; m[4] = 2; m[5] = 20; path_fill("M101.9,71.9c0,11-7.6,22.3-19,22.3c-6.5,0-10.7-3.8-10.7-9.7" "c0-6.3,5.4-13.8,29.8-13.8v1.2H101.9z M114.9,102.9c-0.1-6.1-0.3-13.5-0.3-20.2" "V54.8c0-14-5.8-23.4-26-23.4c-9.3,0-17.8,2.3-24.5,5.1l1.3,10.9" "c6.1-3.7,14.5-5.8,21.2-5.8c11.6,0,15.4,5.2,15.4,15v5.2" "c-26.5,0-42.2,9.7-42.2,25.1c0,11.1,7.9,17.4,19.5,17.4" "s20.5-6.6,23.8-15.2c-0.4,4-0.4,9-0.4,14h12.2V102.9z", m, 0); svgr_reset(&R, ident); svgr_ellipse(&R, 110, 60, 30, 20); svgr_fill(&R, &L, 0x001F, 200, 0); svgr_reset(&R, ident); svgr_move(&R, 60, 100); svgr_line(&R, 150, 80); svgr_line(&R, 150, 110); svgr_stroke(&R, &L, 3, 0xF800, 255); fprintf(f, "P6\n%d %d\n255\n", W, H); for (i = 0; i < W * H; i++) { unsigned int a = al[i], c = px[i]; unsigned int r = ((c >> 11) & 31) * 255 / 31, g = ((c >> 5) & 63) * 255 / 63, b = (c & 31) * 255 / 31; fputc((int)(r * a / 255), f); fputc((int)(g * a / 255), f); fputc((int)(b * a / 255), f); } fclose(f); } printf("%s (%d failures)\n", fails ? "FAIL" : "ok", fails); return fails ? 1 : 0; }