ufont_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Host-side ufont smoke test: parse the bundled Arimo subset, measure and
  2. * render a specimen to /tmp/opencode/ufont-test.ppm. */
  3. #include "ufont.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. static unsigned char *read_file(const char *path, unsigned int *len)
  8. {
  9. FILE *f = fopen(path, "rb");
  10. long n;
  11. unsigned char *p;
  12. if (!f)
  13. return 0;
  14. fseek(f, 0, SEEK_END);
  15. n = ftell(f);
  16. fseek(f, 0, SEEK_SET);
  17. if (n <= 0 || n > (long)UFONT_MAX_FILE) {
  18. fclose(f);
  19. return 0;
  20. }
  21. p = (unsigned char *)malloc((size_t)n);
  22. if (!p || fread(p, 1, (size_t)n, f) != (size_t)n) {
  23. free(p);
  24. fclose(f);
  25. return 0;
  26. }
  27. fclose(f);
  28. *len = (unsigned int)n;
  29. return p;
  30. }
  31. static int draw_text(unsigned short *fb, int w, int h, int x, int baseline,
  32. const struct ufont_face *face, int px, const char *s)
  33. {
  34. int pen = 0;
  35. unsigned short color = 0xFFFF;
  36. while (*s) {
  37. const struct ufont_glyph *g =
  38. ufont_glyph(face, px, (unsigned char)*s);
  39. if (g) {
  40. ufont_blit_rgb565(fb, w, h, x + (pen >> 6), baseline,
  41. g, color, 0, 0, w, h);
  42. pen += g->advance;
  43. }
  44. s++;
  45. }
  46. return (pen + 32) >> 6;
  47. }
  48. static int draw_utf8(unsigned short *fb, int w, int h, int x, int baseline,
  49. const struct ufont_face *face, int px, const char *s)
  50. {
  51. int i = 0, pen = 0, n = (int)strlen(s);
  52. unsigned short color = 0xFFFF;
  53. while (i < n) {
  54. unsigned int cp = ufont_utf8_next(s, &i, n);
  55. const struct ufont_glyph *g = ufont_glyph(face, px, cp);
  56. if (g) {
  57. ufont_blit_rgb565(fb, w, h, x + (pen >> 6), baseline,
  58. g, color, 0, 0, w, h);
  59. pen += g->advance;
  60. }
  61. }
  62. return (pen + 32) >> 6;
  63. }
  64. int main(void)
  65. {
  66. unsigned char *data;
  67. unsigned int len = 0;
  68. struct ufont_face *face;
  69. struct ufont_face *eface;
  70. struct ufont_metrics m;
  71. int sizes[] = { 12, 16, 24, 32 };
  72. unsigned int si;
  73. unsigned short *fb;
  74. int W = 720, H = 440;
  75. FILE *out;
  76. int i;
  77. data = read_file("fonts/sans.ttf", &len);
  78. if (!data) {
  79. fprintf(stderr, "cannot read fonts/sans.ttf\n");
  80. return 1;
  81. }
  82. /* A truncated sfnt must be rejected without touching the arena. */
  83. if (ufont_face_open_mem(data, 64) != 0) {
  84. fprintf(stderr, "truncated font was accepted\n");
  85. return 1;
  86. }
  87. face = ufont_face_open_mem(data, len);
  88. if (!face) {
  89. fprintf(stderr, "cannot parse font\n");
  90. return 1;
  91. }
  92. free(data);
  93. data = 0;
  94. {
  95. unsigned char *edata = read_file("fonts/emoji.ttf", &len);
  96. struct ufont_face *ef;
  97. if (!edata) {
  98. fprintf(stderr, "cannot read fonts/emoji.ttf\n");
  99. return 1;
  100. }
  101. eface = ufont_face_open_mem(edata, len);
  102. free(edata);
  103. if (!eface) {
  104. fprintf(stderr, "cannot parse emoji font\n");
  105. return 1;
  106. }
  107. if (!ufont_has_glyph(eface, 0x1F349)) {
  108. fprintf(stderr, "emoji font lacks U+1F349\n");
  109. return 1;
  110. }
  111. ef = eface;
  112. printf("emoji U+1F349 width 16=%d\n",
  113. ufont_text_width(ef, 16, "\xF0\x9F\x8D\x89", -1, 1));
  114. printf("emoji cmap12 U+1F349 advance 16=%d, U+1F600 advance=%d\n",
  115. ufont_advance_cp(ef, 16, 0x1F349),
  116. ufont_advance_cp(ef, 16, 0x1F600));
  117. }
  118. {
  119. /* WOFF 1.0: the same Arimo subset wrapped by tools/mkwoff.py
  120. * must decode to a face that measures exactly like the TTF. */
  121. unsigned char *wdata = read_file("testdata/fonts/sans.woff",
  122. &len);
  123. unsigned char *sfnt;
  124. unsigned int slen = 0;
  125. struct ufont_face *wf;
  126. const char *t = "Hello, world! \xC4\xE9";
  127. if (!wdata || !ufont_is_woff(wdata, len) ||
  128. ufont_is_woff2(wdata, len)) {
  129. fprintf(stderr, "cannot read the WOFF fixture\n");
  130. return 1;
  131. }
  132. /* Corrupted length field and truncated input are refused. */
  133. wdata[11] ^= 1;
  134. if (ufont_woff_decode(wdata, len, &slen) != 0) {
  135. fprintf(stderr, "bad WOFF length was accepted\n");
  136. return 1;
  137. }
  138. wdata[11] ^= 1;
  139. if (ufont_woff_decode(wdata, len - 100, &slen) != 0) {
  140. fprintf(stderr, "truncated WOFF was accepted\n");
  141. return 1;
  142. }
  143. sfnt = ufont_woff_decode(wdata, len, &slen);
  144. free(wdata);
  145. wf = sfnt ? ufont_face_open_mem(sfnt, slen) : 0;
  146. free(sfnt);
  147. if (!wf) {
  148. fprintf(stderr, "cannot decode the WOFF fixture\n");
  149. return 1;
  150. }
  151. if (ufont_text_width(wf, 16, t, -1, 0) !=
  152. ufont_text_width(face, 16, t, -1, 0) ||
  153. ufont_text_width(wf, 31, t, -1, 0) !=
  154. ufont_text_width(face, 31, t, -1, 0)) {
  155. fprintf(stderr, "WOFF face measures differently\n");
  156. return 1;
  157. }
  158. printf("woff: %u-byte sfnt, widths match the TTF\n", slen);
  159. }
  160. ufont_metrics(face, 16, &m);
  161. printf("16px: ascent=%d descent=%d gap=%d line=%d\n",
  162. m.ascent, m.descent, m.line_gap, m.line_height);
  163. printf("width 'Hello, world!' 16=%d 24=%d 32=%d\n",
  164. ufont_text_width(face, 16, "Hello, world!", -1, 0),
  165. ufont_text_width(face, 24, "Hello, world!", -1, 0),
  166. ufont_text_width(face, 32, "Hello, world!", -1, 0));
  167. printf("width composite 'Äé' 16=%d\n",
  168. ufont_text_width(face, 16, "\xC3\x84\xC3\xA9", -1, 1));
  169. fb = (unsigned short *)calloc((size_t)W * H, sizeof(unsigned short));
  170. if (!fb)
  171. return 1;
  172. for (si = 0; si < sizeof(sizes) / sizeof(sizes[0]); si++) {
  173. int px = sizes[si];
  174. int y = 20 + (int)si * 100;
  175. ufont_metrics(face, px, &m);
  176. draw_text(fb, W, H, 8, y, face, px,
  177. "The quick brown fox jumps over 1234567890");
  178. draw_text(fb, W, H, 8, y + 34, face, px,
  179. "\xC4rger \xE9 \xFC \xDF \xA9 \xAE \xB0 \xB1");
  180. if (si == 1)
  181. draw_utf8(fb, W, H, 8, y + 68, eface, px,
  182. "\xF0\x9F\x8D\x89 \xE2\x9A\xA1 \xE2\x9D\xA4");
  183. }
  184. out = fopen("/tmp/opencode/ufont-test.ppm", "wb");
  185. if (!out)
  186. return 1;
  187. fprintf(out, "P6\n%d %d\n255\n", W, H);
  188. for (i = 0; i < W * H; i++) {
  189. unsigned short c = fb[i];
  190. unsigned char rgb[3];
  191. rgb[0] = (unsigned char)(((c >> 11) & 0x1F) * 255 / 31);
  192. rgb[1] = (unsigned char)(((c >> 5) & 0x3F) * 255 / 63);
  193. rgb[2] = (unsigned char)((c & 0x1F) * 255 / 31);
  194. fwrite(rgb, 1, 3, out);
  195. }
  196. fclose(out);
  197. printf("wrote /tmp/opencode/ufont-test.ppm\n");
  198. ufont_reset();
  199. free(fb);
  200. free(data);
  201. return 0;
  202. }