mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
Guest fonts: give the runtime Android's fonts for Chinese, Japanese, Korean and other scripts (#118, #206)
This commit is contained in:
1 parent
b3ec65f41b
commit
1addb5c848
5 files changed
+235
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import android.content.Context
|
||||
import com.droiddeck.launcher.core.AppLanguage
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Fonts for every script the app's languages (and Steam's) are written in.
|
||||
*
|
||||
* The runtime ships DejaVu and Adwaita only, which have no Chinese, Japanese, Korean or Thai, so
|
||||
* Steam drew those as boxes (#118, #206). Android already carries Noto for all of them in
|
||||
* /system/fonts; the guest gets that folder read-only at [GUEST_DIR], where fontconfig finds it
|
||||
* with the rest of /usr/share/fonts. Nothing is downloaded and the runtime is not changed.
|
||||
*
|
||||
* One CJK font holds Chinese, Japanese and Korean under one set of characters, and a character
|
||||
* they share is drawn the way the chosen region writes it. [preferenceConf] puts that region's
|
||||
* variant first, after DejaVu, which stays the font for Latin text.
|
||||
*/
|
||||
object GuestFonts {
|
||||
const val HOST_DIR = "/system/fonts"
|
||||
const val GUEST_DIR = "/usr/share/fonts/android"
|
||||
/** Rewritten at every session start, so it follows the language picked in Setup. */
|
||||
const val CONF = "etc/fonts/conf.d/65-droiddeck-language.conf"
|
||||
|
||||
/** The bind spec for Android's fonts, or null on a device without the folder. */
|
||||
@JvmStatic
|
||||
fun bindSpec(): String? = File(HOST_DIR).takeIf { it.isDirectory }?.let { "${it.path}:$GUEST_DIR" }
|
||||
|
||||
/** Writes the guest's font preference for the app's current language; best effort. */
|
||||
@JvmStatic
|
||||
fun prepare(context: Context, root: File) {
|
||||
val conf = File(root, CONF)
|
||||
val text = preferenceConf(AppLanguage.effective(context))
|
||||
try {
|
||||
if (conf.isFile && conf.readText() == text) return
|
||||
conf.parentFile?.mkdirs()
|
||||
conf.writeText(text)
|
||||
} catch (e: Exception) {
|
||||
// fontconfig still finds every font; shared characters just take its own default shape.
|
||||
}
|
||||
}
|
||||
|
||||
/** The CJK variant whose shapes match how [locale]'s readers write shared characters. */
|
||||
fun cjkRegion(locale: Locale): String {
|
||||
val script = locale.script
|
||||
return when (locale.language) {
|
||||
"ja" -> "JP"
|
||||
"ko" -> "KR"
|
||||
"zh" -> when {
|
||||
script == "Hans" -> "SC"
|
||||
locale.country == "HK" || locale.country == "MO" -> "HK"
|
||||
script == "Hant" || locale.country == "TW" -> "TC"
|
||||
else -> "SC"
|
||||
}
|
||||
else -> "SC"
|
||||
}
|
||||
}
|
||||
|
||||
fun preferenceConf(locale: Locale): String {
|
||||
val region = cjkRegion(locale)
|
||||
fun alias(generic: String, latin: String, cjk: String) =
|
||||
" <alias binding=\"same\">\n <family>$generic</family>\n <prefer>\n" +
|
||||
" <family>$latin</family>\n <family>$cjk $region</family>\n" +
|
||||
" </prefer>\n </alias>\n"
|
||||
return "<?xml version=\"1.0\"?>\n<!DOCTYPE fontconfig SYSTEM \"urn:fontconfig:fonts.dtd\">\n" +
|
||||
"<!-- Written by DroidDeck at session start for its language (${locale.toLanguageTag()}). -->\n" +
|
||||
"<fontconfig>\n" +
|
||||
alias("sans-serif", "DejaVu Sans", "Noto Sans CJK") +
|
||||
alias("serif", "DejaVu Serif", "Noto Serif CJK") +
|
||||
alias("monospace", "DejaVu Sans Mono", "Noto Sans Mono CJK") +
|
||||
"</fontconfig>\n"
|
||||
}
|
||||
}
|
||||
@@ -270,6 +270,13 @@ public final class LinuxRuntime {
|
||||
if (extraBinds != null) {
|
||||
for (String spec : extraBinds) bind(cmd, spec);
|
||||
}
|
||||
// Android's fonts, for the scripts the runtime's own fonts lack (GuestFonts). Last, so a
|
||||
// fast path that keeps only its first binds drops these before a game library.
|
||||
String fonts = GuestFonts.bindSpec();
|
||||
if (fonts != null) {
|
||||
bind(cmd, fonts);
|
||||
GuestFonts.prepare(context, root);
|
||||
}
|
||||
List<String> specs = new ArrayList<>();
|
||||
for (int i = 0; i + 1 < cmd.size(); i += 2) specs.add(cmd.get(i + 1));
|
||||
return specs;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Steam's name for a language: the API language codes the client and the Steamworks API use
|
||||
* ("english", "schinese", "koreana"...), for every language Steam's interface comes in.
|
||||
*/
|
||||
object SteamLanguage {
|
||||
const val DEFAULT = "english"
|
||||
|
||||
/** Every language the Steam client's interface is offered in. */
|
||||
val all: Set<String> = setOf(
|
||||
"arabic", "bulgarian", "schinese", "tchinese", "czech", "danish", "dutch", "english",
|
||||
"finnish", "french", "german", "greek", "hungarian", "indonesian", "italian", "japanese",
|
||||
"koreana", "norwegian", "polish", "portuguese", "brazilian", "romanian", "russian",
|
||||
"spanish", "latam", "swedish", "thai", "turkish", "ukrainian", "vietnamese",
|
||||
)
|
||||
|
||||
/** Spanish-speaking regions Steam serves with Latin American Spanish rather than Spain's. */
|
||||
private val latinAmerica = setOf(
|
||||
"419", "AR", "BO", "CL", "CO", "CR", "CU", "DO", "EC", "GT", "HN", "MX", "NI", "PA", "PE",
|
||||
"PR", "PY", "SV", "US", "UY", "VE",
|
||||
)
|
||||
|
||||
/**
|
||||
* Steam's language for [locale]. [region] stands in for the locale's own when it has none: the
|
||||
* app's Spanish carries no region, and a player in Mexico should get Latin American Spanish.
|
||||
*/
|
||||
fun forLocale(locale: Locale, region: String? = null): String {
|
||||
val country = locale.country.ifEmpty { region.orEmpty() }
|
||||
return when (locale.language) {
|
||||
"ar" -> "arabic"
|
||||
"bg" -> "bulgarian"
|
||||
"cs" -> "czech"
|
||||
"da" -> "danish"
|
||||
"nl" -> "dutch"
|
||||
"en" -> "english"
|
||||
"fi" -> "finnish"
|
||||
"fr" -> "french"
|
||||
"de" -> "german"
|
||||
"el" -> "greek"
|
||||
"hu" -> "hungarian"
|
||||
"id", "in" -> "indonesian"
|
||||
"it" -> "italian"
|
||||
"ja" -> "japanese"
|
||||
"ko" -> "koreana"
|
||||
"nb", "no", "nn" -> "norwegian"
|
||||
"pl" -> "polish"
|
||||
"pt" -> if (country == "BR") "brazilian" else "portuguese"
|
||||
"ro" -> "romanian"
|
||||
"ru" -> "russian"
|
||||
"es" -> if (country in latinAmerica) "latam" else "spanish"
|
||||
"sv" -> "swedish"
|
||||
"th" -> "thai"
|
||||
"tr" -> "turkish"
|
||||
"uk" -> "ukrainian"
|
||||
"vi" -> "vietnamese"
|
||||
"zh" -> when {
|
||||
locale.script == "Hant" -> "tchinese"
|
||||
locale.script == "Hans" -> "schinese"
|
||||
country == "TW" || country == "HK" || country == "MO" -> "tchinese"
|
||||
else -> "schinese"
|
||||
}
|
||||
else -> DEFAULT
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import java.util.Locale
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class GuestFontsTest {
|
||||
private fun region(tag: String) = GuestFonts.cjkRegion(Locale.forLanguageTag(tag))
|
||||
|
||||
@Test fun sharedCharactersTakeTheReadersRegionalShapes() {
|
||||
assertEquals("JP", region("ja"))
|
||||
assertEquals("KR", region("ko"))
|
||||
assertEquals("SC", region("zh-CN"))
|
||||
assertEquals("TC", region("zh-TW"))
|
||||
assertEquals("HK", region("zh-HK"))
|
||||
assertEquals("HK", region("zh-MO"))
|
||||
assertEquals("SC", region("zh-Hans-HK"))
|
||||
assertEquals("TC", region("zh-Hant"))
|
||||
assertEquals("SC", region("en"))
|
||||
}
|
||||
|
||||
@Test fun latinStaysOnDejaVuWithTheRegionsCjkNext() {
|
||||
val conf = GuestFonts.preferenceConf(Locale.JAPANESE)
|
||||
val sans = conf.substringAfter("<family>sans-serif</family>").substringBefore("</alias>")
|
||||
assertTrue(sans, sans.indexOf("DejaVu Sans") in 0 until sans.indexOf("Noto Sans CJK JP"))
|
||||
assertTrue(conf.contains("<family>Noto Serif CJK JP</family>"))
|
||||
assertTrue(conf.contains("<family>Noto Sans Mono CJK JP</family>"))
|
||||
assertTrue(conf.startsWith("<?xml"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import java.util.Locale
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class SteamLanguageTest {
|
||||
private fun steam(tag: String, region: String? = null) = SteamLanguage.forLocale(Locale.forLanguageTag(tag), region)
|
||||
|
||||
@Test fun theAppsLanguagesMapToSteams() {
|
||||
assertEquals("english", steam("en"))
|
||||
assertEquals("spanish", steam("es"))
|
||||
assertEquals("japanese", steam("ja"))
|
||||
assertEquals("koreana", steam("ko"))
|
||||
assertEquals("schinese", steam("zh-CN"))
|
||||
assertEquals("tchinese", steam("zh-TW"))
|
||||
assertEquals("tchinese", steam("zh-HK"))
|
||||
}
|
||||
|
||||
@Test fun chineseFollowsTheScriptBeforeTheRegion() {
|
||||
assertEquals("tchinese", steam("zh-Hant-CN"))
|
||||
assertEquals("schinese", steam("zh-Hans-HK"))
|
||||
assertEquals("schinese", steam("zh"))
|
||||
assertEquals("schinese", steam("zh-SG"))
|
||||
assertEquals("tchinese", steam("zh-MO"))
|
||||
}
|
||||
|
||||
@Test fun spanishInTheAmericasIsLatinAmerican() {
|
||||
assertEquals("latam", steam("es-MX"))
|
||||
assertEquals("latam", steam("es-419"))
|
||||
assertEquals("spanish", steam("es-ES"))
|
||||
// The app's Spanish has no region; the system's says where the player is.
|
||||
assertEquals("latam", steam("es", region = "AR"))
|
||||
assertEquals("spanish", steam("es", region = "ES"))
|
||||
}
|
||||
|
||||
@Test fun systemLanguagesTheAppDoesNotShipStillReachSteam() {
|
||||
assertEquals("german", steam("de-DE"))
|
||||
assertEquals("brazilian", steam("pt-BR"))
|
||||
assertEquals("portuguese", steam("pt-PT"))
|
||||
assertEquals("norwegian", steam("nb-NO"))
|
||||
assertEquals("indonesian", steam("in-ID"))
|
||||
assertEquals("ukrainian", steam("uk-UA"))
|
||||
}
|
||||
|
||||
@Test fun anythingElseIsEnglish() {
|
||||
assertEquals("english", steam("sw-KE"))
|
||||
assertEquals("english", steam("und"))
|
||||
}
|
||||
|
||||
@Test fun everyAnswerIsALanguageSteamHas() {
|
||||
Locale.getAvailableLocales().forEach { assertTrue(it.toLanguageTag(), SteamLanguage.forLocale(it) in SteamLanguage.all) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user