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

Application: General i18n workaround for shared font loading #76

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions library/include/borealis/frame_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class FontStash
{
public:
int regular = 0;
int standard = 0;
int schinese = 0;
int extSchinese = 0;
int tchinese = 0;
int korean = 0;

int material = 0;
Expand Down
8 changes: 8 additions & 0 deletions library/include/borealis/i18n.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ void loadTranslations();
*/
std::string getCurrentLocale();

#ifdef __SWITCH__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put it here? Technically it's i18n related but it doesn't belong to the i18n system

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it depends on the Switch implementation, and i18n is supposed to be generic (and eventually will)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this id only make sense for libnx i think
removed #ifdef

/**
* Returns the current system locale id
* NOT the one that's currently used in the app!
*/
int swGetCurrentLocaleID();
#endif

inline namespace literals
{
/**
Expand Down
119 changes: 111 additions & 8 deletions library/lib/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,126 @@ bool Application::init(std::string title, Style* style, LibraryViewsThemeVariant
#ifdef __SWITCH__
{
PlFontData font;
int locale = i18n::swGetCurrentLocaleID();

// Standard font
Result rc = plGetSharedFontByType(&font, PlSharedFontType_Standard);
if (R_SUCCEEDED(rc))
{
Logger::info("Using Switch shared font");
Application::fontStash.regular = Application::loadFontFromMemory("regular", font.address, font.size, false);
Logger::info("Adding Switch shared standard font");
Application::fontStash.standard = Application::loadFontFromMemory("regular", font.address, font.size, false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that be standard as well in the loadFontFromMemory call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

}

// Korean font
rc = plGetSharedFontByType(&font, PlSharedFontType_KO);
if (R_SUCCEEDED(rc))
// Load other fonts on demand
bool isFullFallback = false;
AppletType at = appletGetAppletType();
if (at == AppletType_Application || at == AppletType_SystemApplication) // title takeover
{
isFullFallback = true;
Logger::warning("Non applet mode, font full fallback is enabled!");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a warning?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to INFO

}

if (locale == 6 || locale == 15 || isFullFallback)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of magic values here, could you define them somewhere instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

substituted with enforcing SetLanguage enum

{
// S.Chinese font
rc = plGetSharedFontByType(&font, PlSharedFontType_ChineseSimplified);
if (R_SUCCEEDED(rc))
{
Logger::info("Adding Switch shared S.Chinese font");
Application::fontStash.schinese = Application::loadFontFromMemory("schinese", font.address, font.size, false);
}
// Ext S.Chinese font
rc = plGetSharedFontByType(&font, PlSharedFontType_ExtChineseSimplified);
if (R_SUCCEEDED(rc))
{
Logger::info("Adding Switch shared S.Chinese extended font");
Application::fontStash.extSchinese = Application::loadFontFromMemory("extSchinese", font.address, font.size, false);
}
}
if (locale == 11 || locale == 16 || isFullFallback)
{
Logger::info("Adding Switch shared Korean font");
Application::fontStash.korean = Application::loadFontFromMemory("korean", font.address, font.size, false);
nvgAddFallbackFontId(Application::vg, Application::fontStash.regular, Application::fontStash.korean);
// T.Chinese font
rc = plGetSharedFontByType(&font, PlSharedFontType_ChineseTraditional);
if (R_SUCCEEDED(rc))
{
Logger::info("Adding Switch shared T.Chinese font");
Application::fontStash.tchinese = Application::loadFontFromMemory("tchinese", font.address, font.size, false);
}
}
if (locale == 7 || isFullFallback)
{
// Korean font
rc = plGetSharedFontByType(&font, PlSharedFontType_KO);
if (R_SUCCEEDED(rc))
{
Logger::info("Adding Switch shared Korean font");
Application::fontStash.korean = Application::loadFontFromMemory("korean", font.address, font.size, false);
}
}

// Sequentially fallback to other fonts and decide regular font, also on demand
switch (locale)
{
case 6 :
case 15 :
if (isFullFallback)
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.korean);
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.standard);
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.tchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.extSchinese);
}
else
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.standard);
nvgAddFallbackFontId(Application::vg, Application::fontStash.schinese, Application::fontStash.extSchinese);
}
Logger::info("Using Switch shared S.Chinese font as regular");
Application::fontStash.regular = Application::fontStash.schinese;
break;
case 11 :
case 16 :
if (isFullFallback)
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.tchinese, Application::fontStash.korean);
nvgAddFallbackFontId(Application::vg, Application::fontStash.tchinese, Application::fontStash.standard);
nvgAddFallbackFontId(Application::vg, Application::fontStash.tchinese, Application::fontStash.extSchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.tchinese, Application::fontStash.schinese);
}
else
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.tchinese, Application::fontStash.standard);
}
Logger::info("Using Switch shared T.Chinese font as regular");
Application::fontStash.regular = Application::fontStash.tchinese;
break;
case 7 :
if (isFullFallback)
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.korean, Application::fontStash.standard);
nvgAddFallbackFontId(Application::vg, Application::fontStash.korean, Application::fontStash.tchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.korean, Application::fontStash.extSchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.korean, Application::fontStash.schinese);
}
else
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.korean, Application::fontStash.standard);
}
Logger::info("Using Switch shared Korean font as regular");
Application::fontStash.regular = Application::fontStash.korean;
break;
default:
if (isFullFallback)
{
nvgAddFallbackFontId(Application::vg, Application::fontStash.standard, Application::fontStash.korean);
nvgAddFallbackFontId(Application::vg, Application::fontStash.standard, Application::fontStash.tchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.standard, Application::fontStash.extSchinese);
nvgAddFallbackFontId(Application::vg, Application::fontStash.standard, Application::fontStash.schinese);
}
Logger::info("Using Switch shared standard font as regular");
Application::fontStash.regular = Application::fontStash.standard;
break;
}

// Extented font
rc = plGetSharedFontByType(&font, PlSharedFontType_NintendoExt);
Expand Down
28 changes: 28 additions & 0 deletions library/lib/i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ std::string getCurrentLocale()
return DEFAULT_LOCALE;
}

#ifdef __SWITCH__
int swGetCurrentLocaleID()
{
u64 languageCode = 0;
SetLanguage setlanguage = SetLanguage_ENUS;

Result res = setGetSystemLanguage(&languageCode);

if (R_SUCCEEDED(res))
{
if (R_SUCCEEDED(res))
{
res = setMakeLanguage(languageCode, &setlanguage);
return (int)setlanguage;
}
else
{
brls::Logger::error("Unable to convert system language ID (error 0x{0:x}), using the default one: {1}", res, DEFAULT_LOCALE);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use {0:#x} instead of 0x{0x:}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
else
{
brls::Logger::error("Unable to get system language (error 0x{0:x}), using the default one: {1}", res, DEFAULT_LOCALE);
}
return 1; // SetLanguage_ENUS
}
#endif

void loadTranslations()
{
loadLocale(DEFAULT_LOCALE, &defaultLocale);
Expand Down