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
63
64
65
|
#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 x, dx, scale;
uchar *bitmap;
font·Info *info;
mmap·Reader fontfile;
int off, y, ascent, descent, baseln;
int adv, lsb, r0[2], r1[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, mem·sys, nil);
if (!info)
panicf("failed to load info");
bitmap = calloc(W*H, sizeof(*bitmap));
scale = font·scaleheightto(info, L);
font·vmetrics(info, &ascent, &descent, &baseln);
ascent *= scale;
x = 0.;
for (i = 0; i < strlen(phrase); i++) {
dx = x - (float)floor(x);
font·code_hmetrics(info, phrase[i], &adv, &lsb);
font·code_bitmapbox_subpixel(info, phrase[i], scale, scale, dx, 0, r0, r0+1, r1, r1+1);
y = ascent + r0[1];
off = (int)floor(x) + (lsb * scale) + y * W;
font·code_fillbitmap_subpixel(info, bitmap+off, r1[0]-r0[0], r1[1]-r0[1], W, scale, scale, dx, 0, phrase[i]);
x += scale * adv;
if (phrase[i+1])
x += scale * font·code_kernadvance(info, phrase[i], phrase[i+1]);
}
stbi_write_png("out.png", W, H, 1, bitmap, W);
font·free(info);
free(bitmap);
end:
mmap·close(fontfile);
return err;
}
|