aboutsummaryrefslogtreecommitdiff
path: root/sys/libfont/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libfont/test.c')
-rw-r--r--sys/libfont/test.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/sys/libfont/test.c b/sys/libfont/test.c
new file mode 100644
index 0000000..b92a56f
--- /dev/null
+++ b/sys/libfont/test.c
@@ -0,0 +1,62 @@
+#include <u.h>
+#include <libn.h>
+#include <libfont.h>
+
+#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;
+}