Browse Source

Automatically add Japanese, Chinese, and emoji fallback fonts in Window::loadFont(). Add Window::loadFontWithoutFallbacks().

tags/v2.6.1
Andrew Belt 1 month ago
parent
commit
0dc28e6d56
2 changed files with 37 additions and 19 deletions
  1. +3
    -0
      include/window/Window.hpp
  2. +34
    -19
      src/window/Window.cpp

+ 3
- 0
include/window/Window.hpp View File

@@ -105,8 +105,11 @@ struct Window {

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


+ 34
- 19
src/window/Window.cpp View File

@@ -364,18 +364,8 @@ Window::Window() {

// Load UI fonts
uiFont = loadFont(asset::system("res/fonts/DejaVuSans.ttf"));
if (uiFont) {
std::shared_ptr<Font> jpFont = loadFont(asset::system("res/fonts/NotoSansJP-Medium.otf"));
if (jpFont)
nvgAddFallbackFontId(vg, uiFont->handle, jpFont->handle);
std::shared_ptr<Font> scFont = loadFont(asset::system("res/fonts/NotoSansSC-Medium.otf"));
if (scFont)
nvgAddFallbackFontId(vg, uiFont->handle, scFont->handle);
std::shared_ptr<Font> emojiFont = loadFont(asset::system("res/fonts/NotoEmoji-Medium.ttf"));
if (emojiFont)
nvgAddFallbackFontId(vg, uiFont->handle, emojiFont->handle);
if (uiFont)
bndSetFont(uiFont->handle);
}

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


std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
const auto& pair = internal->fontCache.find(filename);
if (pair != internal->fontCache.end())
return pair->second;
// If font is already cached, no need to add fallback fonts again.
const auto& it = internal->fontCache.find(filename);
if (it != internal->fontCache.end())
return it->second;

// This redundantly searches the font cache, but it's not a performance issue because it only happens when font is first loaded.
std::shared_ptr<Font> font = loadFontWithoutFallbacks(filename);
if (!font)
return NULL;

std::shared_ptr<Font> jpFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoSansJP-Medium.otf"));
if (jpFont)
nvgAddFallbackFontId(vg, font->handle, jpFont->handle);
std::shared_ptr<Font> scFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoSansSC-Medium.otf"));
if (scFont)
nvgAddFallbackFontId(vg, font->handle, scFont->handle);
std::shared_ptr<Font> emojiFont = loadFontWithoutFallbacks(asset::system("res/fonts/NotoEmoji-Medium.ttf"));
if (emojiFont)
nvgAddFallbackFontId(vg, font->handle, emojiFont->handle);

return font;
}


std::shared_ptr<Font> Window::loadFontWithoutFallbacks(const std::string& filename) {
// Return cached font, even if null
const auto& it = internal->fontCache.find(filename);
if (it != internal->fontCache.end())
return it->second;

// Load font
std::shared_ptr<Font> font;
std::shared_ptr<Font> font = std::make_shared<Font>();
try {
font = std::make_shared<Font>();
font->loadFile(filename, vg);
}
catch (Exception& e) {
@@ -778,9 +793,9 @@ std::shared_ptr<Font> Window::loadFont(const std::string& filename) {


std::shared_ptr<Image> Window::loadImage(const std::string& filename) {
const auto& pair = internal->imageCache.find(filename);
if (pair != internal->imageCache.end())
return pair->second;
const auto& it = internal->imageCache.find(filename);
if (it != internal->imageCache.end())
return it->second;

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


Loading…
Cancel
Save