aboutsummaryrefslogtreecommitdiff
path: root/src/common/Font.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/Font.cpp')
-rw-r--r--src/common/Font.cpp40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/common/Font.cpp b/src/common/Font.cpp
index 82f6ad8..8f058ca 100644
--- a/src/common/Font.cpp
+++ b/src/common/Font.cpp
@@ -12,30 +12,24 @@ is_ch_ascii(char32_t ch)
return result;
}
-Font::~Font()
-{
- delete[] m_file_content;
-}
-
-bool
-Font::Init(const char* path, int font_size)
+Font::Font(const char *path, int font_size)
{
if (!ReadFile(path)) {
- return false;
+ return;
}
- // set font info
+ // init m_file_content, m_font_info
if (!stbtt_InitFont(&m_font_info, (unsigned char*)m_file_content, 0))
{
std::cout << "stbtt_InitFont failed.\n";
delete[] m_file_content;
m_file_content = nullptr;
- return false;
+ return;
}
- // set font settings (scale + vmetrics)
+ // init font settings
float scale = stbtt_ScaleForPixelHeight(&m_font_info, (float)font_size);
int baseline, ascent, descent, line_gap;
stbtt_GetFontVMetrics(&m_font_info, &ascent, &descent, &line_gap);
@@ -44,8 +38,6 @@ Font::Init(const char* path, int font_size)
descent = int(scale * (float)descent);
line_gap = int(scale * (float)line_gap);
-
- // init members
m_font_scale = scale;
m_font_baseline = baseline;
m_font_yadvance = ascent - descent + line_gap;
@@ -53,12 +45,14 @@ Font::Init(const char* path, int font_size)
// load glyphs
for (char c = first_ascii_ch; c <= last_ascii_ch; ++c) {
- LoadGlyph(m_glyphs[c-first_ascii_ch], static_cast<char32_t>(c));
+ InitGlyph(m_glyphs[c-first_ascii_ch], static_cast<char32_t>(c));
}
memset((void*)&m_fail_glyph, 0, sizeof(m_fail_glyph));
+}
-
- return true;
+Font::~Font()
+{
+ delete[] m_file_content;
}
bool
@@ -83,7 +77,7 @@ Font::ReadFile(const char* path)
}
void
-Font::LoadGlyph(Glyph& glyph, char32_t c)
+Font::InitGlyph(Glyph& glyph, char32_t c)
{
int bbx0, bby0, bbx1, bby1;
stbtt_GetCodepointBitmapBox(&m_font_info, (int)c, m_font_scale, m_font_scale, &bbx0, &bby0, &bbx1, &bby1);
@@ -130,8 +124,10 @@ Font::LoadGlyph(Glyph& glyph, char32_t c)
AlphaBitmap&
Font::GetAlphaBitmap(char32_t c)
{
- if (is_ch_ascii(c)) {
- return m_glyphs[c - first_ascii_ch].bitmap;
+ if (m_file_content) {
+ if (is_ch_ascii(c)) {
+ return m_glyphs[c - first_ascii_ch].bitmap;
+ }
}
return m_fail_glyph.bitmap;
@@ -140,8 +136,10 @@ Font::GetAlphaBitmap(char32_t c)
Glyph&
Font::GetGlyph(char32_t c)
{
- if (is_ch_ascii(c)) {
- return m_glyphs[c - first_ascii_ch];
+ if (m_file_content) {
+ if (is_ch_ascii(c)) {
+ return m_glyphs[c - first_ascii_ch];
+ }
}
return m_fail_glyph;