From 21aa8543ac59cc96e1f102ae321e56c82731caca Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 23 Jun 2014 14:43:38 -0400 Subject: [PATCH] Font: add operator[](), get_baseline_offset() --- src/gui/Font.cc | 6 ++++-- src/gui/Font.h | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/gui/Font.cc b/src/gui/Font.cc index 9664c12..b3d2ccd 100644 --- a/src/gui/Font.cc +++ b/src/gui/Font.cc @@ -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; diff --git a/src/gui/Font.h b/src/gui/Font.h index 433bfa8..e8a6b6a 100644 --- a/src/gui/Font.h +++ b/src/gui/Font.h @@ -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 m_glyphs; };