Custom desktop font (#6377)

* Custom desktop font

* Add `getDesktopAllFonts` to setting custom desktop font.

* Custom font.
`desktopFontFamily` change to `fontFamily`.
Add GameSettings.getSettingsForPlatformLaunchers().

* Add `Custom font` setting UI.

* Add `Custom font` on Android.

* `Default Font` use translations.

* format

* remove open fun.

Co-authored-by: Yair Morgenstern <yairm210@hotmail.com>
This commit is contained in:
Tang
2022-03-21 14:12:16 -05:00
committed by GitHub
parent 130fd653a4
commit c1737b6183
10 changed files with 153 additions and 26 deletions

View File

@ -562,6 +562,8 @@ Enable portrait orientation = Hochkant-Orientierung zulassen
Generate translation files = Erstelle Übersetzungsdateien
Translation files are generated successfully. = Die Übersetzungsdateien wurden erfolgreich erstellt.
Please note that translations are a community-based work in progress and are INCOMPLETE! The percentage shown is how much of the language is translated in-game. If you want to help translating the game into your language, click here. = Bitte beachte, daß die Übersetzungen eine andauernde Leistung einer Gemeinschaft von Freiwilligen sind und damit oft unvollständig. Die angezeigte Prozentzahl bedeutet den Anteil übersetzter Texte im gesamten Spiel. Wenn Du helfen willst, die Übersetzungen zu verbessern - dies ist ein Link zur Anleitung.
Font family = Schriftart
You need to restart the game for this change to take effect. = Diese Änderung wird erst beim nächsten Start des Spiels wirksam.
# Notifications

View File

@ -567,6 +567,9 @@ Enable portrait orientation = 启用竖屏
Generate translation files = 生成翻译文件
Translation files are generated successfully. = 翻译文件生成成功。
Please note that translations are a community-based work in progress and are INCOMPLETE! The percentage shown is how much of the language is translated in-game. If you want to help translating the game into your language, click here. = 请注意,翻译是一项基于社区的正在进行的工作,并且是【不完整的】!显示的百分比是语言在游戏中的翻译量。如果您想帮助将游戏翻译成您的语言,请单击此处。
Font family = 字体
Default Font = 默认字体
You need to restart the game for this change to take effect. = 您需要重新启动游戏才能使此更改生效。
# Notifications

View File

@ -567,6 +567,9 @@ Enable portrait orientation =
Generate translation files =
Translation files are generated successfully. =
Please note that translations are a community-based work in progress and are INCOMPLETE! The percentage shown is how much of the language is translated in-game. If you want to help translating the game into your language, click here. =
Font family =
Default Font =
You need to restart the game for this change to take effect. =
# Notifications

View File

@ -11,6 +11,7 @@ import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.unciv.UncivGame
import com.unciv.UncivGameParameters
import com.unciv.logic.GameSaver
import com.unciv.models.metadata.GameSettings
import com.unciv.ui.utils.Fonts
import java.io.File
@ -34,12 +35,15 @@ open class AndroidLauncher : AndroidApplication() {
val config = AndroidApplicationConfiguration().apply {
useImmersiveMode = true;
}
val fontFamily = GameSettings.getSettingsForPlatformLaunchers(filesDir.path).fontFamily
val androidParameters = UncivGameParameters(
version = BuildConfig.VERSION_NAME,
crashReportSysInfo = CrashReportSysInfoAndroid,
fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt()),
customSaveLocationHelper = customSaveLocationHelper,
limitOrientationsHelper = limitOrientationsHelper
version = BuildConfig.VERSION_NAME,
crashReportSysInfo = CrashReportSysInfoAndroid,
fontImplementation = NativeFontAndroid(Fonts.ORIGINAL_FONT_SIZE.toInt(), fontFamily),
customSaveLocationHelper = customSaveLocationHelper,
limitOrientationsHelper = limitOrientationsHelper
)
game = UncivGame(androidParameters)

View File

@ -3,21 +3,44 @@ package com.unciv.app
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.fonts.Font
import android.graphics.fonts.FontFamily
import android.graphics.fonts.SystemFonts
import android.os.Build
import com.badlogic.gdx.graphics.Pixmap
import com.unciv.ui.utils.FontData
import com.unciv.ui.utils.NativeFontImplementation
/**
* Created by tian on 2016/10/2.
*/
class NativeFontAndroid(val size: Int) : NativeFontImplementation {
class NativeFontAndroid(private val size: Int, private val fontFamily: String) :
NativeFontImplementation {
private val paint = Paint().apply {
typeface = if (fontFamily.isNotBlank() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val font = fontList.firstOrNull { it.file?.nameWithoutExtension == fontFamily }
if (font != null) {
Typeface.CustomFallbackBuilder(FontFamily.Builder(font).build())
.setSystemFallback(fontFamily).build()
} else Typeface.create(fontFamily, Typeface.NORMAL)
} else Typeface.create(fontFamily, Typeface.NORMAL)
isAntiAlias = true
textSize = size.toFloat()
strokeWidth = 0f
setARGB(255, 255, 255, 255)
}
private val fontList: List<Font>
get() = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) emptyList()
else SystemFonts.getAvailableFonts().toList()
override fun getFontSize(): Int {
return size
}
override fun getCharPixmap(char: Char): Pixmap {
val paint = Paint()
paint.isAntiAlias = true
paint.textSize = size.toFloat()
val metric = paint.fontMetrics
var width = paint.measureText(char.toString()).toInt()
var height = (metric.descent - metric.ascent).toInt()
@ -27,8 +50,6 @@ class NativeFontAndroid(val size: Int) : NativeFontImplementation {
}
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
paint.strokeWidth = 0f
paint.setARGB(255, 255, 255, 255)
canvas.drawText(char.toString(), 0f, -metric.ascent, paint)
val pixmap = Pixmap(width, height, Pixmap.Format.RGBA8888)
val data = IntArray(width * height)
@ -42,4 +63,14 @@ class NativeFontAndroid(val size: Int) : NativeFontImplementation {
bitmap.recycle()
return pixmap
}
override fun getAvailableFont(): Collection<FontData> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
SystemFonts.getAvailableFonts().mapNotNull {
it.file?.nameWithoutExtension
}.map { FontData(it) }.toSet()
} else {
listOf(FontData("sans-serif"), FontData("serif"), FontData("mono"))
}
}
}