mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-04 15:27:50 +07:00
Modding: allow mods to supply custom fonts (#8715)
* Modding: allow mods to supply custom fonts * Cleanup * Code cleanup --------- Co-authored-by: vegeta1k95 <vfylfhby>
This commit is contained in:
@ -15,7 +15,6 @@ import com.unciv.logic.files.SETTINGS_FILE_NAME
|
||||
import com.unciv.logic.files.UncivFiles
|
||||
import com.unciv.models.metadata.ScreenSize
|
||||
import com.unciv.models.metadata.WindowState
|
||||
import com.unciv.ui.components.Fonts
|
||||
import com.unciv.utils.Log
|
||||
import com.unciv.utils.debug
|
||||
import java.awt.GraphicsEnvironment
|
||||
@ -73,7 +72,7 @@ internal object DesktopLauncher {
|
||||
val platformSpecificHelper = PlatformSpecificHelpersDesktop(config)
|
||||
val desktopParameters = UncivGameParameters(
|
||||
cancelDiscordEvent = { discordTimer?.cancel() },
|
||||
fontImplementation = NativeFontDesktop((Fonts.ORIGINAL_FONT_SIZE * settings.fontSizeMultiplier).toInt(), settings.fontFamily),
|
||||
fontImplementation = FontDesktop(),
|
||||
customFileLocationHelper = CustomFileLocationHelperDesktop(),
|
||||
crashReportSysInfo = CrashReportSysInfoDesktop(),
|
||||
platformSpecificHelper = platformSpecificHelper,
|
||||
|
61
desktop/src/com/unciv/app/desktop/NativeFontDesktop.kt → desktop/src/com/unciv/app/desktop/FontDesktop.kt
Executable file → Normal file
61
desktop/src/com/unciv/app/desktop/NativeFontDesktop.kt → desktop/src/com/unciv/app/desktop/FontDesktop.kt
Executable file → Normal file
@ -1,35 +1,66 @@
|
||||
package com.unciv.app.desktop
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.graphics.Pixmap
|
||||
import com.unciv.ui.components.FontFamilyData
|
||||
import com.unciv.ui.components.NativeFontImplementation
|
||||
import com.unciv.ui.components.FontImplementation
|
||||
import com.unciv.ui.components.Fonts
|
||||
import java.awt.*
|
||||
import java.awt.image.BufferedImage
|
||||
import java.util.*
|
||||
|
||||
class NativeFontDesktop(private val size: Int, private val fontFamily: String) :
|
||||
NativeFontImplementation {
|
||||
private val font by lazy {
|
||||
Font(fontFamily, Font.PLAIN, size)
|
||||
|
||||
class FontDesktop : FontImplementation {
|
||||
|
||||
private lateinit var font: Font
|
||||
private lateinit var metric: FontMetrics
|
||||
|
||||
override fun setFontFamily(fontFamilyData: FontFamilyData, size: Int) {
|
||||
|
||||
// Mod font
|
||||
if (fontFamilyData.filePath != null)
|
||||
{
|
||||
this.font = createFontFromFile(fontFamilyData.filePath!!, size)
|
||||
}
|
||||
// System font
|
||||
else
|
||||
{
|
||||
this.font = Font(fontFamilyData.invariantName, Font.PLAIN, size)
|
||||
}
|
||||
|
||||
val bufferedImage = BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR)
|
||||
val graphics = bufferedImage.createGraphics()
|
||||
this.metric = graphics.getFontMetrics(font)
|
||||
graphics.dispose()
|
||||
}
|
||||
private val metric by lazy {
|
||||
val bi = BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR)
|
||||
val g = bi.createGraphics()
|
||||
g.font = font
|
||||
val fontMetrics = g.fontMetrics
|
||||
g.dispose()
|
||||
fontMetrics
|
||||
|
||||
private fun createFontFromFile(path: String, size: Int): Font {
|
||||
var font: Font
|
||||
try
|
||||
{
|
||||
// Try to create and register new font
|
||||
val fontFile = Gdx.files.local(path).file()
|
||||
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
||||
font = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(size.toFloat())
|
||||
ge.registerFont(font)
|
||||
}
|
||||
catch (e: Exception)
|
||||
{
|
||||
// Fallback to default, if failed.
|
||||
font = Font(Fonts.DEFAULT_FONT_FAMILY, Font.PLAIN, size)
|
||||
}
|
||||
return font
|
||||
}
|
||||
|
||||
override fun getFontSize(): Int {
|
||||
return size
|
||||
return font.size
|
||||
}
|
||||
|
||||
override fun getCharPixmap(char: Char): Pixmap {
|
||||
var width = metric.charWidth(char)
|
||||
var height = metric.ascent + metric.descent
|
||||
if (width == 0) {
|
||||
height = size
|
||||
height = font.size
|
||||
width = height
|
||||
}
|
||||
val bi = BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR)
|
||||
@ -50,7 +81,7 @@ class NativeFontDesktop(private val size: Int, private val fontFamily: String) :
|
||||
return pixmap
|
||||
}
|
||||
|
||||
override fun getAvailableFontFamilies(): Sequence<FontFamilyData> {
|
||||
override fun getSystemFonts(): Sequence<FontFamilyData> {
|
||||
val cjkLanguage = " CJK " +System.getProperty("user.language").uppercase()
|
||||
return GraphicsEnvironment.getLocalGraphicsEnvironment().allFonts.asSequence()
|
||||
.filter { " CJK " !in it.fontName || cjkLanguage in it.fontName }
|
Reference in New Issue
Block a user