Skip to content

Commit 0dc28e6

Browse files
committed
Automatically add Japanese, Chinese, and emoji fallback fonts in Window::loadFont(). Add Window::loadFontWithoutFallbacks().
1 parent 02deb03 commit 0dc28e6

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

include/window/Window.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ struct Window {
105105

106106
/** Loads and caches a Font from a file path.
107107
Do not store this reference across screen frames, as the Window may have changed, invalidating the Font.
108+
Automatically adds fallback fonts such as Japanese, Chinese, and emoji.
108109
*/
109110
std::shared_ptr<Font> loadFont(const std::string& filename);
111+
/** Loads and caches a Font without adding fallback fonts. */
112+
std::shared_ptr<Font> loadFontWithoutFallbacks(const std::string& filename);
110113
/** Loads and caches an Image from a file path.
111114
Do not store this reference across screen frames, as the Window may have changed, invalidating the Image.
112115
*/

src/window/Window.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,8 @@ Window::Window() {
364364

365365
// Load UI fonts
366366
uiFont = loadFont(asset::system("res/fonts/DejaVuSans.ttf"));
367-
if (uiFont) {
368-
std::shared_ptr<Font> jpFont = loadFont(asset::system("res/fonts/NotoSansJP-Medium.otf"));
369-
if (jpFont)
370-
nvgAddFallbackFontId(vg, uiFont->handle, jpFont->handle);
371-
std::shared_ptr<Font> scFont = loadFont(asset::system("res/fonts/NotoSansSC-Medium.otf"));
372-
if (scFont)
373-
nvgAddFallbackFontId(vg, uiFont->handle, scFont->handle);
374-
std::shared_ptr<Font> emojiFont = loadFont(asset::system("res/fonts/NotoEmoji-Medium.ttf"));
375-
if (emojiFont)
376-
nvgAddFallbackFontId(vg, uiFont->handle, emojiFont->handle);
367+
if (uiFont)
377368
bndSetFont(uiFont->handle);
378-
}
379369

380370
if (APP->scene) {
381371
widget::Widget::ContextCreateEvent e;
@@ -758,14 +748,39 @@ double Window::getFrameDurationRemaining() {
758748

759749

760750
std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
761-
const auto& pair = internal->fontCache.find(filename);
762-
if (pair != internal->fontCache.end())
763-
return pair->second;
751+
// If font is already cached, no need to add fallback fonts again.
752+
const auto& it = internal->fontCache.find(filename);
753+
if (it != internal->fontCache.end())
754+
return it->second;
755+
756+
// This redundantly searches the font cache, but it's not a performance issue because it only happens when font is first loaded.
757+
std::shared_ptr<Font> font = loadFontWithoutFallbacks(filename);
758+
if (!font)
759+
return NULL;
760+
761+
std::shared_ptr<Font> jpFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoSansJP-Medium.otf"));
762+
if (jpFont)
763+
nvgAddFallbackFontId(vg, font->handle, jpFont->handle);
764+
std::shared_ptr<Font> scFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoSansSC-Medium.otf"));
765+
if (scFont)
766+
nvgAddFallbackFontId(vg, font->handle, scFont->handle);
767+
std::shared_ptr<Font> emojiFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoEmoji-Medium.ttf"));
768+
if (emojiFont)
769+
nvgAddFallbackFontId(vg, font->handle, emojiFont->handle);
770+
771+
return font;
772+
}
773+
774+
775+
std::shared_ptr<Font> Window::loadFontWithoutFallbacks(const std::string& filename) {
776+
// Return cached font, even if null
777+
const auto& it = internal->fontCache.find(filename);
778+
if (it != internal->fontCache.end())
779+
return it->second;
764780

765781
// Load font
766-
std::shared_ptr<Font> font;
782+
std::shared_ptr<Font> font = std::make_shared<Font>();
767783
try {
768-
font = std::make_shared<Font>();
769784
font->loadFile(filename, vg);
770785
}
771786
catch (Exception& e) {
@@ -778,9 +793,9 @@ std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
778793

779794

780795
std::shared_ptr<Image> Window::loadImage(const std::string& filename) {
781-
const auto& pair = internal->imageCache.find(filename);
782-
if (pair != internal->imageCache.end())
783-
return pair->second;
796+
const auto& it = internal->imageCache.find(filename);
797+
if (it != internal->imageCache.end())
798+
return it->second;
784799

785800
// Load image
786801
std::shared_ptr<Image> image;

0 commit comments

Comments
 (0)