From efaa0c9a6c2c1166df3e88f735063ac165cbf7ed Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 1 Sep 2019 17:12:47 +0300 Subject: [PATCH] Performance improvements for NativeFont - separated chinese and other charsForFonts, since the chinese have hundreds of chars and it makes the font loading time really long --- core/src/com/unciv/ui/utils/Fonts.kt | 39 +++++++++++-------- .../optionstable/WorldScreenOptionsTable.kt | 9 ++++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/core/src/com/unciv/ui/utils/Fonts.kt b/core/src/com/unciv/ui/utils/Fonts.kt index 2eafa2b555..59eac4b026 100644 --- a/core/src/com/unciv/ui/utils/Fonts.kt +++ b/core/src/com/unciv/ui/utils/Fonts.kt @@ -11,10 +11,10 @@ import com.unciv.models.gamebasics.tr import com.unciv.ui.worldscreen.optionstable.PopupTable import core.java.nativefont.NativeFont import core.java.nativefont.NativeFontPaint -import java.io.FileOutputStream import java.io.FileInputStream +import java.io.FileOutputStream import java.net.URL -import java.security.* +import java.security.MessageDigest class Fonts { companion object { @@ -52,7 +52,8 @@ class Fonts { return true return false } - fun getCharsForFont(): String { + + fun getCharsForFont(withChinese:Boolean): String { val defaultText = "ABCČĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽaäàâăbcčćçdđeéfghiîjklmnoöpqrsșštțuüvwxyzž" + "АБВГҐДЂЕЁЄЖЗЅИІЇЙЈКЛЉМНЊОПРСТЋУЎФХЦЧЏШЩЪЫЬЭЮЯабвгґдђеёєжзѕиіїйјклљмнњопрстћуўфхцчџшщъыьэюя" + "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάßΆέΈέΉίϊΐΊόΌύΰϋΎΫΏ" + @@ -61,19 +62,21 @@ class Fonts { val charSet = HashSet() charSet.addAll(defaultText.asIterable()) - if(Gdx.files.internal("jsons/BasicHelp/BasicHelp_Simplified_Chinese.json").exists()) - charSet.addAll(Gdx.files.internal("jsons/BasicHelp/BasicHelp_Simplified_Chinese.json").readString().asIterable()) - if (Gdx.files.internal("jsons/Nations_Simplified_Chinese.json").exists()) - charSet.addAll(Gdx.files.internal("jsons/Nations_Simplified_Chinese.json").readString().asIterable()) - if (Gdx.files.internal("jsons/Tutorials/Tutorials_Simplified_Chinese.json").exists()) - charSet.addAll(Gdx.files.internal("jsons/Tutorials/Tutorials_Simplified_Chinese.json").readString().asIterable()) + if(withChinese) { + if (Gdx.files.internal("jsons/BasicHelp/BasicHelp_Simplified_Chinese.json").exists()) + charSet.addAll(Gdx.files.internal("jsons/BasicHelp/BasicHelp_Simplified_Chinese.json").readString().asIterable()) + if (Gdx.files.internal("jsons/Nations_Simplified_Chinese.json").exists()) + charSet.addAll(Gdx.files.internal("jsons/Nations_Simplified_Chinese.json").readString().asIterable()) + if (Gdx.files.internal("jsons/Tutorials/Tutorials_Simplified_Chinese.json").exists()) + charSet.addAll(Gdx.files.internal("jsons/Tutorials/Tutorials_Simplified_Chinese.json").readString().asIterable()) - for (entry in GameBasics.Translations.entries) { - for (lang in entry.value) { - if (lang.key.contains("Chinese")) charSet.addAll(lang.value.asIterable()) + for (entry in GameBasics.Translations.entries) { + for (lang in entry.value) { + if (lang.key.contains("Chinese")) charSet.addAll(lang.value.asIterable()) + } } } - return charSet.joinToString() + return charSet.joinToString("") } fun getFont(size: Int): BitmapFont { if(UnCivGame.Current.settings.fontSet=="WenQuanYiMicroHei"){ @@ -97,17 +100,19 @@ class Fonts { parameter.size = size parameter.minFilter = Texture.TextureFilter.Linear parameter.magFilter = Texture.TextureFilter.Linear - parameter.characters = getCharsForFont() + parameter.characters = getCharsForFont(true) val font = generator.generateFont(parameter) fontCache[keyForFont] = font return font } } val fontForLanguage ="Nativefont" - val keyForFont = "$fontForLanguage $size" - if (fontCache.containsKey(keyForFont))return fontCache[keyForFont]!! + val withChinese = UnCivGame.Current.settings.language.contains("Chinese") + val keyForFont = if(!withChinese) "$fontForLanguage $size" else "$fontForLanguage $size withChinese" // different cache for chinese + if (fontCache.containsKey(keyForFont)) return fontCache[keyForFont]!! val font=NativeFont(NativeFontPaint(size)) - font.appendText(getCharsForFont()) + val charsForFont = getCharsForFont(withChinese) + font.appendText(charsForFont) fontCache[keyForFont] = font return font } diff --git a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt index 0617f08cd2..8eca1f3f6a 100644 --- a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt +++ b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt @@ -3,7 +3,10 @@ package com.unciv.ui.worldscreen.optionstable import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Actor -import com.badlogic.gdx.scenes.scene2d.ui.* +import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane +import com.badlogic.gdx.scenes.scene2d.ui.SelectBox +import com.badlogic.gdx.scenes.scene2d.ui.Slider +import com.badlogic.gdx.scenes.scene2d.ui.TextField import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener import com.badlogic.gdx.utils.Array import com.unciv.UnCivGame @@ -280,13 +283,15 @@ class WorldScreenOptionsTable(val worldScreen:WorldScreen) : PopupTable(worldScr GameBasics.Translations.filter { !it.value.containsKey(currentLanguage) }.forEach { missingTextArray.add(it.key) } missingTextSelectBox.items = missingTextArray missingTextSelectBox.selected = "Untranslated texts" - innerTable.add(missingTextSelectBox).pad(10f).width(UnCivGame.Current.worldScreen.stage.width / 2).colspan(2).row() + innerTable.add(missingTextSelectBox).pad(10f) + .width(screen.stage.width / 2).colspan(2).row() } } fun selectLanguage(){ UnCivGame.Current.settings.language = selectedLanguage UnCivGame.Current.settings.save() + CameraStageBaseScreen.resetFonts() // to load chinese characters if necessary UnCivGame.Current.worldScreen = WorldScreen(worldScreen.viewingCiv) UnCivGame.Current.setWorldScreen() WorldScreenOptionsTable(UnCivGame.Current.worldScreen)