Font: add operator[](), get_baseline_offset()

This commit is contained in:
Josh Holtrop 2014-06-23 14:43:38 -04:00
parent da73f62845
commit 21aa8543ac
2 changed files with 15 additions and 2 deletions

View File

@ -29,15 +29,17 @@ namespace jes
FT_Set_Pixel_Sizes(m_face, 0, size);
m_line_height = round_up_26_6(m_face->size->metrics.height);
GlyphRef x_glyph = load_glyph('x');
if (x_glyph.is_null())
{
std::cerr << "Could not load 'x' glyph from font" << fname << std::endl;
return false;
}
m_glyphs['x'] = x_glyph;
m_advance = x_glyph->get_advance();
m_line_height = round_up_26_6(m_face->size->metrics.height);
m_baseline_offset = round_up_26_6((m_face->size->metrics.height - (m_face->size->metrics.ascender - m_face->size->metrics.descender)) / 2 - m_face->size->metrics.descender);
m_loaded = true;
return true;

View File

@ -44,12 +44,23 @@ namespace jes
bool load(FT_Library ft, const char * fname, size_t size);
int get_line_height() { return m_line_height; }
int get_advance() { return m_advance; }
int get_baseline_offset() { return m_baseline_offset; }
GlyphRef operator[](FT_ULong char_code)
{
auto it = m_glyphs.find(char_code);
if (it != m_glyphs.end())
return it->second;
GlyphRef g = load_glyph(char_code);
m_glyphs[char_code] = g;
return g;
}
protected:
GlyphRef load_glyph(FT_ULong char_code);
FT_Face m_face;
int m_line_height;
int m_advance;
int m_baseline_offset;
bool m_loaded;
std::unordered_map<FT_ULong, GlyphRef> m_glyphs;
};