diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-10-16 15:33:06 +0200 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-10-16 15:33:06 +0200 |
| commit | a873df7a66dc1831cee4eae2d998abed88246268 (patch) | |
| tree | c19cd079ce106e1431d64c34babf4ef59cf71723 /src/common | |
| parent | 9f2845b12135c32dde91e58afc1193d54333ec9f (diff) | |
renderer: introduce text rendering
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/Font.cpp | 40 | ||||
| -rw-r--r-- | src/common/Font.hpp | 6 |
2 files changed, 22 insertions, 24 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; diff --git a/src/common/Font.hpp b/src/common/Font.hpp index af4ddb5..876b03a 100644 --- a/src/common/Font.hpp +++ b/src/common/Font.hpp @@ -23,9 +23,9 @@ struct Glyph { class Font { public: + explicit Font(const char* path, int size); ~Font(); - bool Init(const char* path, int font_size); AlphaBitmap& GetAlphaBitmap(char32_t c); Glyph& GetGlyph(char32_t c); @@ -33,7 +33,7 @@ public: private: bool ReadFile(const char* path); - void LoadGlyph(Glyph& glyph, char32_t c); + void InitGlyph(Glyph& glyph, char32_t c); private: @@ -43,12 +43,12 @@ private: const char* m_file_content = nullptr; + stbtt_fontinfo m_font_info; float m_font_scale; int m_font_baseline; int m_font_yadvance; - stbtt_fontinfo m_font_info; Glyph m_glyphs[ascii_glyph_count]; Glyph m_fail_glyph; }; |
