#include #include #include #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #define W 512 #define H 128 #define L 64 static char *phrase = "the quick brown"; int main() { int i, err; float scale; uchar *bitmap; font·Info *info; mmap·Reader fontfile; int x, y, as, ds, lg, ax, lsb, off, kern, r[2], c0[2], c1[2]; err = 0; fontfile = mmap·open("/home/nolln/root/data/DejaVuSans.ttf"); if (!fontfile.len) { err = 1; goto end; } info = font·make(fontfile.ubuf, 0); if (!info) panicf("failed to load info"); bitmap = calloc(W*H, sizeof(*bitmap)); scale = font·scaleheightto(info, L); font·vmetrics(info, &as, &ds, &lg); as *= scale; ds *= scale; x = 0; for (i = 0; i < strlen(phrase); i++) { font·code_hmetrics(info, phrase[i], &ax, &lsb); font·code_bitmapbox(info, phrase[i], scale, scale, c0, c0+1, c1, c1+1); y = as + c0[1]; off = x + lsb * scale + y * W; font·code_fillbitmap(info, bitmap+off, c1[0]-c0[0], c1[1]-c0[1], W, scale, scale, phrase[i]); x += ax * scale; kern = font·code_kernadvance(info, phrase[i], phrase[i+1]); x += kern * scale; } stbi_write_png("out.png", W, H, 1, bitmap, W); font·free(info); free(bitmap); end: mmap·close(fontfile); return err; }