ufont_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. int main(void)
  49. {
  50. unsigned char *data;
  51. unsigned int len = 0;
  52. struct ufont_face *face;
  53. struct ufont_metrics m;
  54. int sizes[] = { 12, 16, 24, 32 };
  55. unsigned int si;
  56. unsigned short *fb;
  57. int W = 720, H = 440;
  58. FILE *out;
  59. int i;
  60. data = read_file("fonts/sans.ttf", &len);
  61. if (!data) {
  62. fprintf(stderr, "cannot read fonts/sans.ttf\n");
  63. return 1;
  64. }
  65. /* A truncated sfnt must be rejected without touching the arena. */
  66. if (ufont_face_open_mem(data, 64) != 0) {
  67. fprintf(stderr, "truncated font was accepted\n");
  68. return 1;
  69. }
  70. face = ufont_face_open_mem(data, len);
  71. if (!face) {
  72. fprintf(stderr, "cannot parse font\n");
  73. return 1;
  74. }
  75. ufont_metrics(face, 16, &m);
  76. printf("16px: ascent=%d descent=%d gap=%d line=%d\n",
  77. m.ascent, m.descent, m.line_gap, m.line_height);
  78. printf("width 'Hello, world!' 16=%d 24=%d 32=%d\n",
  79. ufont_text_width(face, 16, "Hello, world!", -1, 0),
  80. ufont_text_width(face, 24, "Hello, world!", -1, 0),
  81. ufont_text_width(face, 32, "Hello, world!", -1, 0));
  82. printf("width composite 'Äé' 16=%d\n",
  83. ufont_text_width(face, 16, "\xC3\x84\xC3\xA9", -1, 1));
  84. fb = (unsigned short *)calloc((size_t)W * H, sizeof(unsigned short));
  85. if (!fb)
  86. return 1;
  87. for (si = 0; si < sizeof(sizes) / sizeof(sizes[0]); si++) {
  88. int px = sizes[si];
  89. int y = 20 + (int)si * 100;
  90. ufont_metrics(face, px, &m);
  91. draw_text(fb, W, H, 8, y, face, px,
  92. "The quick brown fox jumps over 1234567890");
  93. draw_text(fb, W, H, 8, y + 34, face, px,
  94. "\xC4rger \xE9 \xFC \xDF \xA9 \xAE \xB0 \xB1");
  95. }
  96. out = fopen("/tmp/opencode/ufont-test.ppm", "wb");
  97. if (!out)
  98. return 1;
  99. fprintf(out, "P6\n%d %d\n255\n", W, H);
  100. for (i = 0; i < W * H; i++) {
  101. unsigned short c = fb[i];
  102. unsigned char rgb[3];
  103. rgb[0] = (unsigned char)(((c >> 11) & 0x1F) * 255 / 31);
  104. rgb[1] = (unsigned char)(((c >> 5) & 0x3F) * 255 / 63);
  105. rgb[2] = (unsigned char)((c & 0x1F) * 255 / 31);
  106. fwrite(rgb, 1, 3, out);
  107. }
  108. fclose(out);
  109. printf("wrote /tmp/opencode/ufont-test.ppm\n");
  110. ufont_reset();
  111. free(fb);
  112. free(data);
  113. return 0;
  114. }