svg_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Host test for svg_raster.c: coverage of basic shapes, fill rules, path
  2. * commands, transforms and strokes. `./svg_test out.ppm` also writes a
  3. * specimen image. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "svg_raster.h"
  8. #define W 160
  9. #define H 120
  10. static unsigned short px[W * H];
  11. static unsigned char al[W * H];
  12. static struct svgr_layer L = { px, al, W, H };
  13. static unsigned char ptbuf[8192 * 9];
  14. static struct svgr R;
  15. static int fails;
  16. static void clear(void)
  17. {
  18. memset(px, 0, sizeof(px));
  19. memset(al, 0, sizeof(al));
  20. }
  21. static const double ident[6] = { 1, 0, 0, 1, 0, 0 };
  22. static void check(const char *what, int cond)
  23. {
  24. if (!cond) {
  25. printf("FAIL %s\n", what);
  26. fails++;
  27. }
  28. }
  29. static unsigned int sum_alpha(void)
  30. {
  31. unsigned int i, s = 0;
  32. for (i = 0; i < W * H; i++)
  33. s += al[i];
  34. return s;
  35. }
  36. static void path_fill(const char *d, const double m[6], int evenodd)
  37. {
  38. svgr_reset(&R, m);
  39. svgr_path(&R, d);
  40. svgr_fill(&R, &L, 0xFFFF, 255, evenodd);
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. double m[6];
  45. unsigned int s;
  46. svgr_init(&R, ptbuf, 8192);
  47. /* axis-aligned rect: exact coverage */
  48. clear();
  49. path_fill("M10 10 H30 V20 H10 Z", ident, 0);
  50. check("rect inside", al[15 * W + 15] == 255);
  51. check("rect outside", al[5 * W + 5] == 0 && al[15 * W + 30] == 0);
  52. s = sum_alpha();
  53. check("rect area", s > 200 * 250 && s <= 200 * 255);
  54. /* half-pixel edge antialiasing */
  55. clear();
  56. path_fill("M10.5 10 L20 10 L20 20 L10.5 20 Z", ident, 0);
  57. check("half pixel", al[15 * W + 10] > 100 && al[15 * W + 10] < 160);
  58. /* relative commands, implicit lineto after m, and h/v */
  59. clear();
  60. path_fill("m10 10 20 0 0 10 h-20z", ident, 0);
  61. check("relative", al[15 * W + 15] == 255 && al[15 * W + 31] == 0);
  62. /* nonzero vs even-odd: two nested squares, same direction */
  63. clear();
  64. path_fill("M0 0H40V40H0Z M10 10H30V30H10Z", ident, 0);
  65. check("nonzero hole filled", al[20 * W + 20] == 255);
  66. clear();
  67. path_fill("M0 0H40V40H0Z M10 10H30V30H10Z", ident, 1);
  68. check("evenodd hole", al[20 * W + 20] == 0 && al[5 * W + 5] == 255);
  69. /* circle via arcs: area ~ pi r^2 */
  70. clear();
  71. svgr_reset(&R, ident);
  72. svgr_ellipse(&R, 50, 50, 20, 20);
  73. svgr_fill(&R, &L, 0xFFFF, 255, 0);
  74. s = sum_alpha() / 255;
  75. check("circle area", s > 1220 && s < 1290);
  76. check("circle centre", al[50 * W + 50] == 255);
  77. /* cubic + smooth cubic + quadratic + arc in one path */
  78. clear();
  79. path_fill("M10 60 C10 20 50 20 50 60 S90 100 90 60 Q120 20 150 60 "
  80. "A20 20 0 0 1 110 60 Z", ident, 0);
  81. check("curves drew", sum_alpha() > 255 * 500);
  82. /* transform: scale(2) translate(5,5) of a 10x10 square */
  83. clear();
  84. memcpy(m, ident, sizeof(m));
  85. svgr_transform(m, "scale(2) translate(5 5)");
  86. path_fill("M0 0h10v10h-10z", m, 0);
  87. check("transform", al[15 * W + 15] == 255 && al[31 * W + 31] == 0 &&
  88. al[9 * W + 9] == 0);
  89. memcpy(m, ident, sizeof(m));
  90. svgr_transform(m, "rotate(90 20 20)");
  91. check("rotate about point", (int)(m[4] + 0.5) == 40 &&
  92. (int)(m[5] + 0.5) == 0);
  93. /* number grammar: ".5.5", exponents, negative separators */
  94. {
  95. const char *p = ".5.5-1e1";
  96. int ok;
  97. double a = svgr_num(&p, &ok), b = svgr_num(&p, &ok),
  98. c = svgr_num(&p, &ok);
  99. check("numbers", a == 0.5 && b == 0.5 && c == -10 && ok);
  100. }
  101. /* stroke: a 4px horizontal line */
  102. clear();
  103. svgr_reset(&R, ident);
  104. svgr_move(&R, 10, 50);
  105. svgr_line(&R, 60, 50);
  106. svgr_stroke(&R, &L, 4, 0xFFFF, 255);
  107. check("stroke body", al[49 * W + 30] == 255 && al[51 * W + 30] == 255);
  108. check("stroke edge", al[45 * W + 30] == 0);
  109. /* the tagesschau burger (cubic caps, s commands, implicit repeats) */
  110. clear();
  111. memcpy(m, ident, sizeof(m));
  112. m[0] = m[3] = 3;
  113. 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"
  114. "C0.67,6.02,0,6.69,0,7.52S0.67,9.02,1.5,9.02z", m, 0);
  115. check("burger bar", al[22 * W + 50] == 255 && al[30 * W + 50] == 0);
  116. if (argc > 1) {
  117. FILE *f = fopen(argv[1], "wb");
  118. int i;
  119. clear();
  120. memcpy(m, ident, sizeof(m));
  121. m[0] = m[3] = 0.22;
  122. m[4] = 2;
  123. m[5] = 20;
  124. 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"
  125. "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"
  126. "V54.8c0-14-5.8-23.4-26-23.4c-9.3,0-17.8,2.3-24.5,5.1l1.3,10.9"
  127. "c6.1-3.7,14.5-5.8,21.2-5.8c11.6,0,15.4,5.2,15.4,15v5.2"
  128. "c-26.5,0-42.2,9.7-42.2,25.1c0,11.1,7.9,17.4,19.5,17.4"
  129. "s20.5-6.6,23.8-15.2c-0.4,4-0.4,9-0.4,14h12.2V102.9z", m, 0);
  130. svgr_reset(&R, ident);
  131. svgr_ellipse(&R, 110, 60, 30, 20);
  132. svgr_fill(&R, &L, 0x001F, 200, 0);
  133. svgr_reset(&R, ident);
  134. svgr_move(&R, 60, 100);
  135. svgr_line(&R, 150, 80);
  136. svgr_line(&R, 150, 110);
  137. svgr_stroke(&R, &L, 3, 0xF800, 255);
  138. fprintf(f, "P6\n%d %d\n255\n", W, H);
  139. for (i = 0; i < W * H; i++) {
  140. unsigned int a = al[i], c = px[i];
  141. unsigned int r = ((c >> 11) & 31) * 255 / 31,
  142. g = ((c >> 5) & 63) * 255 / 63,
  143. b = (c & 31) * 255 / 31;
  144. fputc((int)(r * a / 255), f);
  145. fputc((int)(g * a / 255), f);
  146. fputc((int)(b * a / 255), f);
  147. }
  148. fclose(f);
  149. }
  150. printf("%s (%d failures)\n", fails ? "FAIL" : "ok", fails);
  151. return fails ? 1 : 0;
  152. }