I used the stbtt_truetype.h to render a bitmapfont.
However, like stb_image.h the bitmaps loaded are upside down.
In stb_image there's this function stbi_set_flip_vertically_on_load(true); which inverts the bitmap automatically.
I didn't find a corresponding function for stb_truetype so i came up with this little algorithm, but is this a good way to go or
is there a better way?
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 | bool success = 0;
const u32 pxlw = 2048;
const u32 pxlh = 2048;
const u32 fontf_sz = pxlw*pxlh;
unsigned char font_file[fontf_sz] = {};
unsigned char pixels[fontf_sz] = {};
stbtt_packedchar packed_char[124] = {};
stbtt_pack_context pack_c = {};
size_t bytes_read = 0;
bytes_read = fread(font_file, 1, fontf_sz, fopen("C:/windows/fonts/arial.ttf", "rb"));
// init pixel_bitmap
success = stbtt_PackBegin(&pack_c, pixels, pxlw, pxlh, 0, 1, 0);
// set detail level of characters in font which requires the pixel_bitmap to be bigger
stbtt_PackSetOversampling(&pack_c, 8, 8);
// Set what characters in the pixel_map you want STBTT_POINT_SIZE makes the charcter size in pixels
stbtt_PackFontRange(&pack_c, font_file, 0, 32, 0, 124, packed_char);
for(i32 y = 0; y < pxlh / 2; y++)
{
for(i32 x = 0; x < pxlw; x++)
{
i32 temp = pixels[x + (y * pxlw)];
pixels[x + (y * pxlw)] = pixels[x + ((pxlh - 1) * pxlw) - (y * pxlw)];
pixels[x + ((pxlh - 1) * pxlw) - (y * pxlw)] = temp;
}
}
stbtt_PackEnd(&pack_c);
|