Skip to content

Commit

Permalink
Refactor font handling code
Browse files Browse the repository at this point in the history
 * Add cache for loaded fonts
 * Replace `const std::string &` with `std::string_view` in print
 * Remove static load method
 * Reformat with clang-format
 * Return bool from celestiacore::set*Font
  • Loading branch information
375gnu committed Jan 20, 2022
1 parent 3c9334e commit f969b37
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 278 deletions.
50 changes: 23 additions & 27 deletions src/celestia/celestiacore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4053,14 +4053,10 @@ bool CelestiaCore::initRenderer()

if (font == nullptr)
cout << _("Error loading font; text will not be visible.\n");
else
font->buildTexture();

if (!config->titleFont.empty())
titleFont = LoadFontHelper(renderer, config->titleFont);
if (titleFont != nullptr)
titleFont->buildTexture();
else
if (titleFont == nullptr)
titleFont = font;

// Set up the overlay
Expand All @@ -4074,15 +4070,7 @@ bool CelestiaCore::initRenderer()
else
{
auto labelFont = LoadFontHelper(renderer, config->labelFont);
if (labelFont == nullptr)
{
renderer->setFont(Renderer::FontNormal, font);
}
else
{
labelFont->buildTexture();
renderer->setFont(Renderer::FontNormal, labelFont);
}
renderer->setFont(Renderer::FontNormal, labelFont == nullptr ? font : labelFont);
}

renderer->setFont(Renderer::FontLarge, titleFont);
Expand Down Expand Up @@ -4286,26 +4274,34 @@ CelestiaCore::TextDisplayHandler* CelestiaCore::getTextDisplayHandler() const
return textDisplayHandler;
}

void CelestiaCore::setFont(const fs::path& fontPath, int collectionIndex, int fontSize)
bool CelestiaCore::setFont(const fs::path& fontPath, int collectionIndex, int fontSize)
{
font = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize);
if (font != nullptr)
font->buildTexture();
if (auto f = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize); f != nullptr)
{
font = f;
return true;
}
return false;
}

void CelestiaCore::setTitleFont(const fs::path& fontPath, int collectionIndex, int fontSize)
bool CelestiaCore::setTitleFont(const fs::path& fontPath, int collectionIndex, int fontSize)
{
titleFont = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize);
if (titleFont != nullptr)
titleFont->buildTexture();
if (auto f = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize); f != nullptr)
{
titleFont = f;
return true;
}
return false;
}

void CelestiaCore::setRendererFont(const fs::path& fontPath, int collectionIndex, int fontSize, Renderer::FontStyle fontStyle)
bool CelestiaCore::setRendererFont(const fs::path& fontPath, int collectionIndex, int fontSize, Renderer::FontStyle fontStyle)
{
auto f = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize);
if (f != nullptr)
f->buildTexture();
renderer->setFont(fontStyle, f);
if (auto f = LoadTextureFont(renderer, fontPath, collectionIndex, fontSize); f != nullptr)
{
renderer->setFont(fontStyle, f);
return true;
}
return false;
}

void CelestiaCore::clearFonts()
Expand Down
6 changes: 3 additions & 3 deletions src/celestia/celestiacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ class CelestiaCore // : public Watchable<CelestiaCore>
void setTextDisplayHandler(TextDisplayHandler*);
TextDisplayHandler* getTextDisplayHandler() const;

void setFont(const fs::path& fontPath, int collectionIndex, int fontSize);
void setTitleFont(const fs::path& fontPath, int collectionIndex, int fontSize);
void setRendererFont(const fs::path& fontPath, int collectionIndex, int fontSize, Renderer::FontStyle fontStyle);
bool setFont(const fs::path& fontPath, int collectionIndex, int fontSize);
bool setTitleFont(const fs::path& fontPath, int collectionIndex, int fontSize);
bool setRendererFont(const fs::path& fontPath, int collectionIndex, int fontSize, Renderer::FontStyle fontStyle);
void clearFonts();

void toggleReferenceMark(const std::string& refMark, Selection sel = Selection());
Expand Down
1 change: 0 additions & 1 deletion src/celscript/lua/celx_celestia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,6 @@ static int celestia_loadfont(lua_State* l)
CelestiaCore* appCore = getAppCore(l, AllErrors);
auto font = LoadTextureFont(appCore->getRenderer(), s);
if (font == nullptr) return 0;
font->buildTexture();
return celx.pushClass(font);
}

Expand Down
Loading

0 comments on commit f969b37

Please sign in to comment.