Skip to content

Instantly share code, notes, and snippets.

@albertelwin
Created August 8, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertelwin/13cd7e3b6c75a108f1605683aadbcfb6 to your computer and use it in GitHub Desktop.
Save albertelwin/13cd7e3b6c75a108f1605683aadbcfb6 to your computer and use it in GitHub Desktop.
stbtt_fontinfo ttf_info;
MemoryPtr ttf_file = read_file_to_memory(file_name);
ASSERT(ttf_file.ptr);
stbtt_InitFont(&ttf_info, ttf_file.ptr, stbtt_GetFontOffsetForIndex(ttf_file.ptr, 0));
f32 scale_factor = stbtt_ScaleForPixelHeight(&ttf_info, pixel_height);
i32 ascent, descent, line_gap;
stbtt_GetFontVMetrics(&ttf_info, &ascent, &descent, &line_gap);
Font * font = &font_asset->font;
font->glyphs = ALLOC_ARRAY(FontGlyph, FONT_GLYPH_COUNT);
font->ascent = ascent * scale_factor;
font->descent = descent * scale_factor;
font->whitespace_advance = whitespace_advance * scale_factor;
for(char code_point = FONT_FIRST_CHAR; code_point < FONT_ONE_PAST_LAST_CHAR; code_point++) {
FontGlyph * glyph = font->glyphs + get_font_glyph_index((char)code_point);
i32 bitmap_width, bitmap_height;
i32 x_offset, y_offset;
u8 * bitmap_data = stbtt_GetCodepointBitmap(&ttf_info, 0, scale_factor, code_point, &bitmap_width, &bitmap_height, &x_offset, &y_offset);
ASSERT(bitmap_data);
//NOTE: When you render a glyph add this offset to it's position to align it correctly
math::vec2 offset = math::vec2(bitmap_width * 0.5 + x_offset, -(bitmap_height * 0.5f + y_offset));
i32 advance, left_side_bearing;
stbtt_GetCodepointHMetrics(&ttf_info, code_point, &advance, &left_side_bearing);
glyph->advance = advance * scale_factor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment