aboutsummaryrefslogtreecommitdiff
path: root/sys/libfont/test.c
blob: b92a56fedb7a61dfff127c4016abd4d77b6e9e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}