/* Host-side ufont smoke test: parse the bundled Arimo subset, measure and * render a specimen to /tmp/opencode/ufont-test.ppm. */ #include "ufont.h" #include #include #include static unsigned char *read_file(const char *path, unsigned int *len) { FILE *f = fopen(path, "rb"); long n; unsigned char *p; if (!f) return 0; fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET); if (n <= 0 || n > (long)UFONT_MAX_FILE) { fclose(f); return 0; } p = (unsigned char *)malloc((size_t)n); if (!p || fread(p, 1, (size_t)n, f) != (size_t)n) { free(p); fclose(f); return 0; } fclose(f); *len = (unsigned int)n; return p; } static int draw_text(unsigned short *fb, int w, int h, int x, int baseline, const struct ufont_face *face, int px, const char *s) { int pen = 0; unsigned short color = 0xFFFF; while (*s) { const struct ufont_glyph *g = ufont_glyph(face, px, (unsigned char)*s); if (g) { ufont_blit_rgb565(fb, w, h, x + (pen >> 6), baseline, g, color, 0, 0, w, h); pen += g->advance; } s++; } return (pen + 32) >> 6; } static int draw_utf8(unsigned short *fb, int w, int h, int x, int baseline, const struct ufont_face *face, int px, const char *s) { int i = 0, pen = 0, n = (int)strlen(s); unsigned short color = 0xFFFF; while (i < n) { unsigned int cp = ufont_utf8_next(s, &i, n); const struct ufont_glyph *g = ufont_glyph(face, px, cp); if (g) { ufont_blit_rgb565(fb, w, h, x + (pen >> 6), baseline, g, color, 0, 0, w, h); pen += g->advance; } } return (pen + 32) >> 6; } int main(void) { unsigned char *data; unsigned int len = 0; struct ufont_face *face; struct ufont_face *eface; struct ufont_metrics m; int sizes[] = { 12, 16, 24, 32 }; unsigned int si; unsigned short *fb; int W = 720, H = 440; FILE *out; int i; data = read_file("fonts/sans.ttf", &len); if (!data) { fprintf(stderr, "cannot read fonts/sans.ttf\n"); return 1; } /* A truncated sfnt must be rejected without touching the arena. */ if (ufont_face_open_mem(data, 64) != 0) { fprintf(stderr, "truncated font was accepted\n"); return 1; } face = ufont_face_open_mem(data, len); if (!face) { fprintf(stderr, "cannot parse font\n"); return 1; } free(data); data = 0; { unsigned char *edata = read_file("fonts/emoji.ttf", &len); struct ufont_face *ef; if (!edata) { fprintf(stderr, "cannot read fonts/emoji.ttf\n"); return 1; } eface = ufont_face_open_mem(edata, len); free(edata); if (!eface) { fprintf(stderr, "cannot parse emoji font\n"); return 1; } if (!ufont_has_glyph(eface, 0x1F349)) { fprintf(stderr, "emoji font lacks U+1F349\n"); return 1; } ef = eface; printf("emoji U+1F349 width 16=%d\n", ufont_text_width(ef, 16, "\xF0\x9F\x8D\x89", -1, 1)); printf("emoji cmap12 U+1F349 advance 16=%d, U+1F600 advance=%d\n", ufont_advance_cp(ef, 16, 0x1F349), ufont_advance_cp(ef, 16, 0x1F600)); } { /* WOFF 1.0: the same Arimo subset wrapped by tools/mkwoff.py * must decode to a face that measures exactly like the TTF. */ unsigned char *wdata = read_file("testdata/fonts/sans.woff", &len); unsigned char *sfnt; unsigned int slen = 0; struct ufont_face *wf; const char *t = "Hello, world! \xC4\xE9"; if (!wdata || !ufont_is_woff(wdata, len) || ufont_is_woff2(wdata, len)) { fprintf(stderr, "cannot read the WOFF fixture\n"); return 1; } /* Corrupted length field and truncated input are refused. */ wdata[11] ^= 1; if (ufont_woff_decode(wdata, len, &slen) != 0) { fprintf(stderr, "bad WOFF length was accepted\n"); return 1; } wdata[11] ^= 1; if (ufont_woff_decode(wdata, len - 100, &slen) != 0) { fprintf(stderr, "truncated WOFF was accepted\n"); return 1; } sfnt = ufont_woff_decode(wdata, len, &slen); free(wdata); wf = sfnt ? ufont_face_open_mem(sfnt, slen) : 0; free(sfnt); if (!wf) { fprintf(stderr, "cannot decode the WOFF fixture\n"); return 1; } if (ufont_text_width(wf, 16, t, -1, 0) != ufont_text_width(face, 16, t, -1, 0) || ufont_text_width(wf, 31, t, -1, 0) != ufont_text_width(face, 31, t, -1, 0)) { fprintf(stderr, "WOFF face measures differently\n"); return 1; } printf("woff: %u-byte sfnt, widths match the TTF\n", slen); } ufont_metrics(face, 16, &m); printf("16px: ascent=%d descent=%d gap=%d line=%d\n", m.ascent, m.descent, m.line_gap, m.line_height); printf("width 'Hello, world!' 16=%d 24=%d 32=%d\n", ufont_text_width(face, 16, "Hello, world!", -1, 0), ufont_text_width(face, 24, "Hello, world!", -1, 0), ufont_text_width(face, 32, "Hello, world!", -1, 0)); printf("width composite 'Äé' 16=%d\n", ufont_text_width(face, 16, "\xC3\x84\xC3\xA9", -1, 1)); fb = (unsigned short *)calloc((size_t)W * H, sizeof(unsigned short)); if (!fb) return 1; for (si = 0; si < sizeof(sizes) / sizeof(sizes[0]); si++) { int px = sizes[si]; int y = 20 + (int)si * 100; ufont_metrics(face, px, &m); draw_text(fb, W, H, 8, y, face, px, "The quick brown fox jumps over 1234567890"); draw_text(fb, W, H, 8, y + 34, face, px, "\xC4rger \xE9 \xFC \xDF \xA9 \xAE \xB0 \xB1"); if (si == 1) draw_utf8(fb, W, H, 8, y + 68, eface, px, "\xF0\x9F\x8D\x89 \xE2\x9A\xA1 \xE2\x9D\xA4"); } out = fopen("/tmp/opencode/ufont-test.ppm", "wb"); if (!out) return 1; fprintf(out, "P6\n%d %d\n255\n", W, H); for (i = 0; i < W * H; i++) { unsigned short c = fb[i]; unsigned char rgb[3]; rgb[0] = (unsigned char)(((c >> 11) & 0x1F) * 255 / 31); rgb[1] = (unsigned char)(((c >> 5) & 0x3F) * 255 / 63); rgb[2] = (unsigned char)((c & 0x1F) * 255 / 31); fwrite(rgb, 1, 3, out); } fclose(out); printf("wrote /tmp/opencode/ufont-test.ppm\n"); ufont_reset(); free(fb); free(data); return 0; }