refactor Font

This commit is contained in:
MihailRis
2024-11-11 20:30:10 +03:00
parent a17314d5e0
commit 79693580e0
3 changed files with 15 additions and 37 deletions
+9 -31
View File
@@ -40,45 +40,23 @@ int Font::calcWidth(const std::wstring& text, size_t length) {
} }
int Font::calcWidth(const std::wstring& text, size_t offset, size_t length) { int Font::calcWidth(const std::wstring& text, size_t offset, size_t length) {
return std::min(text.length()-offset, length) * 8; return std::min(text.length()-offset, length) * glyphInterval;
} }
void Font::draw(Batch2D* batch, std::wstring text, int x, int y) { static inline void drawGlyph(
draw(batch, std::move(text), x, y, FontStyle::none); Batch2D* batch, int x, int y, uint c, int glyphSize
) {
batch->sprite(x, y, glyphSize, glyphSize, 16, c, batch->getColor());
} }
static inline void drawGlyph(Batch2D* batch, int x, int y, uint c, FontStyle style) { void Font::draw(Batch2D* batch, std::wstring_view text, int x, int y) {
switch (style){
case FontStyle::none:
break;
case FontStyle::shadow:
batch->sprite(x+1, y+1, GLYPH_SIZE, GLYPH_SIZE, 16, c, SHADOW_TINT);
break;
case FontStyle::outline:
for (int oy = -1; oy <= 1; oy++){
for (int ox = -1; ox <= 1; ox++){
if (ox || oy) {
batch->sprite(x+ox, y+oy, GLYPH_SIZE, GLYPH_SIZE, 16, c, SHADOW_TINT);
}
}
}
break;
}
batch->sprite(x, y, GLYPH_SIZE, GLYPH_SIZE, 16, c, batch->getColor());
}
void Font::draw(Batch2D* batch, const std::wstring& text, int x, int y, FontStyle style) {
draw(batch, std::wstring_view(text.c_str(), text.length()), x, y, style);
}
void Font::draw(Batch2D* batch, std::wstring_view text, int x, int y, FontStyle style) {
uint page = 0; uint page = 0;
uint next = MAX_CODEPAGES; uint next = MAX_CODEPAGES;
int init_x = x; int init_x = x;
do { do {
for (uint c : text){ for (uint c : text){
if (!isPrintableChar(c)) { if (!isPrintableChar(c)) {
x += 8; x += glyphInterval;
continue; continue;
} }
uint charpage = c >> 8; uint charpage = c >> 8;
@@ -91,12 +69,12 @@ void Font::draw(Batch2D* batch, std::wstring_view text, int x, int y, FontStyle
texture = pages[0].get(); texture = pages[0].get();
} }
batch->texture(texture); batch->texture(texture);
drawGlyph(batch, x, y, c, style); drawGlyph(batch, x, y, c, lineHeight);
} }
else if (charpage > page && charpage < next){ else if (charpage > page && charpage < next){
next = charpage; next = charpage;
} }
x += 8;//getGlyphWidth(c); x += glyphInterval;
} }
page = next; page = next;
next = MAX_CODEPAGES; next = MAX_CODEPAGES;
+4 -4
View File
@@ -17,8 +17,9 @@ enum class FontStyle {
class Font { class Font {
int lineHeight; int lineHeight;
int yoffset; int yoffset;
public: int glyphInterval = 8;
std::vector<std::unique_ptr<Texture>> pages; std::vector<std::unique_ptr<Texture>> pages;
public:
Font(std::vector<std::unique_ptr<Texture>> pages, int lineHeight, int yoffset); Font(std::vector<std::unique_ptr<Texture>> pages, int lineHeight, int yoffset);
~Font(); ~Font();
@@ -41,7 +42,6 @@ public:
/// @brief Check if character is visible (non-whitespace) /// @brief Check if character is visible (non-whitespace)
/// @param codepoint character unicode codepoint /// @param codepoint character unicode codepoint
bool isPrintableChar(uint codepoint) const; bool isPrintableChar(uint codepoint) const;
void draw(Batch2D* batch, std::wstring text, int x, int y);
void draw(Batch2D* batch, const std::wstring& text, int x, int y, FontStyle style); void draw(Batch2D* batch, std::wstring_view text, int x, int y);
void draw(Batch2D* batch, std::wstring_view text, int x, int y, FontStyle style);
}; };
+2 -2
View File
@@ -201,10 +201,10 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
if (i < cache.lines.size()-1) { if (i < cache.lines.size()-1) {
view = std::wstring_view(text.c_str()+offset, cache.lines.at(i+1).offset-offset); view = std::wstring_view(text.c_str()+offset, cache.lines.at(i+1).offset-offset);
} }
font->draw(batch, view, pos.x, pos.y + i * totalLineHeight, FontStyle::none); font->draw(batch, view, pos.x, pos.y + i * totalLineHeight);
} }
} else { } else {
font->draw(batch, text, pos.x, pos.y, FontStyle::none); font->draw(batch, text, pos.x, pos.y);
} }
} }