Skip to content

Commit

Permalink
Text: Also apply style to ExFont
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Mar 8, 2023
1 parent bbdcef8 commit 1ea24c6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,21 +766,27 @@ void Font::SetFallbackFont(FontRef fallback_font) {
this->fallback_font = fallback_font;
}

bool Font::IsStyleApplied() const {
return style_applied;
}

Font::Style Font::GetCurrentStyle() const {
return current_style;
}

Font::StyleScopeGuard Font::ApplyStyle(Style new_style) {
vApplyStyle(new_style);
current_style = new_style;
style_applied = true;

return lcf::ScopeGuard<std::function<void()>>([&]() {
vApplyStyle(original_style);
current_style = original_style;
style_applied = false;
});
}

ExFont::ExFont() : Font("exfont", 12, false, false) {
ExFont::ExFont() : Font("exfont", HEIGHT, false, false) {
}

FontRef Font::exfont = std::make_shared<ExFont>();
Expand Down
6 changes: 6 additions & 0 deletions src/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class Font {

using StyleScopeGuard = lcf::ScopeGuard<std::function<void()>>;

/**
* @return Whether a custom style is currently active
*/
bool IsStyleApplied() const;

/**
* Returns the current font style used for rendering.
*
Expand Down Expand Up @@ -234,6 +239,7 @@ class Font {
Font(StringView name, int size, bool bold, bool italic);

std::string name;
bool style_applied = false;
Style original_style;
Style current_style;
FontRef fallback_font;
Expand Down
24 changes: 21 additions & 3 deletions src/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@

Point Text::Draw(Bitmap& dest, int x, int y, const Font& font, const Bitmap& system, int color, char32_t glyph, bool is_exfont) {
if (is_exfont) {
return Font::exfont->Render(dest, x, y, system, color, glyph);
if (!font.IsStyleApplied()) {
return Font::exfont->Render(dest, x, y, system, color, glyph);
} else {
auto style = font.GetCurrentStyle();
auto style_guard = Font::exfont->ApplyStyle(style);
return Font::exfont->Render(dest, x, y, system, color, glyph);
}
} else {
return font.Render(dest, x, y, system, color, glyph);
}
}

Point Text::Draw(Bitmap& dest, int x, int y, const Font& font, Color color, char32_t glyph, bool is_exfont) {
if (is_exfont) {
return Font::exfont->Render(dest, x, y, color, glyph);
if (!font.IsStyleApplied()) {
return Font::exfont->Render(dest, x, y, color, glyph);
} else {
auto style = font.GetCurrentStyle();
auto style_guard = Font::exfont->ApplyStyle(style);
return Font::exfont->Render(dest, x, y, color, glyph);
}
} else {
return font.Render(dest, x, y, color, glyph);
}
Expand Down Expand Up @@ -242,7 +254,13 @@ Rect Text::GetSize(const Font& font, StringView text) {

Rect Text::GetSize(const Font& font, char32_t glyph, bool is_exfont) {
if (is_exfont) {
return Font::exfont->GetSize(glyph);
if (!font.IsStyleApplied()) {
return Font::exfont->GetSize(glyph);
} else {
auto style = font.GetCurrentStyle();
auto style_guard = Font::exfont->ApplyStyle(style);
return Font::exfont->GetSize(glyph);
}
} else {
return font.GetSize(glyph);
}
Expand Down

0 comments on commit 1ea24c6

Please sign in to comment.