@@ -364,18 +364,8 @@ Window::Window() {
364
364
365
365
// Load UI fonts
366
366
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)
377
368
bndSetFont (uiFont->handle );
378
- }
379
369
380
370
if (APP->scene ) {
381
371
widget::Widget::ContextCreateEvent e;
@@ -758,14 +748,39 @@ double Window::getFrameDurationRemaining() {
758
748
759
749
760
750
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 ;
764
780
765
781
// Load font
766
- std::shared_ptr<Font> font;
782
+ std::shared_ptr<Font> font = std::make_shared<Font>() ;
767
783
try {
768
- font = std::make_shared<Font>();
769
784
font->loadFile (filename, vg);
770
785
}
771
786
catch (Exception& e) {
@@ -778,9 +793,9 @@ std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
778
793
779
794
780
795
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 ;
784
799
785
800
// Load image
786
801
std::shared_ptr<Image> image;
0 commit comments