Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font clean up #620

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/include/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ class CFont : public gcn::Font
void setIsNormal(bool value) { is_normal = value; }

void Load();
void Reload() const;

CGraphic *GetFontColorGraphic(const CFontColor &fontColor) const;
CGraphic *GetGraphic() const;

template<bool CLIP>
unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const;
Expand Down
46 changes: 4 additions & 42 deletions src/video/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,12 @@ static FontMap Fonts; /// Font mappings
using FontColorMap = std::map<std::string, std::unique_ptr<CFontColor>, std::less<>>;
static FontColorMap FontColors; /// Map of ident to font color.

static CFontColor *FontColor; /// Current font color

static const CFontColor *LastTextColor; /// Last text color
static CFontColor *DefaultTextColor; /// Default text color
static CFontColor *ReverseTextColor; /// Reverse text color
static std::string DefaultNormalColorIndex; /// Default normal color index
static std::string DefaultReverseColorIndex; /// Default reverse color index

/**
** Font color graphics
** Usage: FontColorGraphics[CFont *font][CFontColor *color]
*/
using FontColorGraphicMap = std::map<const CFontColor *, CGraphic *>;
static std::map<const CFont *, FontColorGraphicMap> FontColorGraphics;

// FIXME: remove these
static CFont *SmallFont; /// Small font used in stats
static CFont *GameFont; /// Normal font used in game
Expand Down Expand Up @@ -201,7 +192,7 @@ void SetDefaultTextColors(const std::string &normal, const std::string &reverse)
{
DefaultNormalColorIndex = normal;
DefaultReverseColorIndex = reverse;
LastTextColor = DefaultTextColor = FontColor = CFontColor::Get(normal);
LastTextColor = DefaultTextColor = CFontColor::Get(normal);
ReverseTextColor = CFontColor::Get(reverse);
}

Expand Down Expand Up @@ -593,7 +584,7 @@ unsigned int CFont::DrawChar(CGraphic &g, int utf8, int x, int y, const CFontCol
return w + 1;
}

CGraphic *CFont::GetFontColorGraphic(const CFontColor &fontColor) const
CGraphic *CFont::GetGraphic() const
{
return this->G;
}
Expand Down Expand Up @@ -625,7 +616,7 @@ int CLabel::DoDrawText(int x, int y, std::string_view text, const CFontColor *fc
const CFontColor *backup = fc;
bool isColor = false;
font->DynamicLoad();
CGraphic *g = font->GetFontColorGraphic(*FontColor);
CGraphic *g = font->GetGraphic();

while (int utf8 = CodepageIndexFromUTF8(text.data(), text.size(), pos, subpos)) {
bool tab = false;
Expand All @@ -643,26 +634,21 @@ int CLabel::DoDrawText(int x, int y, std::string_view text, const CFontColor *fc
++pos;
continue;
case '!':
if (fc != reverse) {
fc = reverse;
g = font->GetFontColorGraphic(*fc);
}
fc = reverse;
++pos;
continue;
case '<':
LastTextColor = fc;
if (fc != reverse) {
isColor = true;
fc = reverse;
g = font->GetFontColorGraphic(*fc);
}
++pos;
continue;
case '>':
if (fc != LastTextColor) {
std::swap(fc, LastTextColor);
isColor = false;
g = font->GetFontColorGraphic(*fc);
}
++pos;
continue;
Expand All @@ -680,7 +666,6 @@ int CLabel::DoDrawText(int x, int y, std::string_view text, const CFontColor *fc
if (fc_tmp) {
isColor = true;
fc = fc_tmp;
g = font->GetFontColorGraphic(*fc);
}
continue;
}
Expand All @@ -696,7 +681,6 @@ int CLabel::DoDrawText(int x, int y, std::string_view text, const CFontColor *fc

if (isColor == false && fc != backup) {
fc = backup;
g = font->GetFontColorGraphic(*fc);
}
}
return widths;
Expand Down Expand Up @@ -937,28 +921,6 @@ void LoadFonts()
GameFont = CFont::Get("game");
}

void CFont::Reload() const
{
if (this->G) {
FontColorGraphicMap &fontColorGraphicMap = FontColorGraphics[this];
for (auto &[key, g] : fontColorGraphicMap) {
delete g;
}
fontColorGraphicMap.clear();
}
}


/**
** Reload fonts
*/
void ReloadFonts()
{
for (auto &[key, font] : Fonts) {
font->Reload();
}
}

/**
** Create a new font
**
Expand Down
Loading