Font improvements (#4884)

* Font improvements

* Revert internal Inter font to v3.12 with original variants only

* Use translatable strings for internal Inter font variant labels
This commit is contained in:
tgex0
2024-12-05 02:08:14 +13:00
committed by GitHub
parent 3558b495e1
commit cb54dc22b5
4 changed files with 42212 additions and 40134 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -68,10 +68,10 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
.toList()
}
val uiRegular = ResourceFont(context, R.font.inter_regular, "Inter")
val uiMedium = ResourceFont(context, R.font.inter_medium, "Inter Medium")
val uiText = ResourceFont(context, R.font.inter_regular, "Inter")
val uiTextMedium = ResourceFont(context, R.font.inter_medium, "Inter Medium")
val uiRegular = ResourceFont(context, R.font.inter_regular, "Inter v3 " + context.getString(R.string.font_weight_regular))
val uiMedium = ResourceFont(context, R.font.inter_medium, "Inter v3 " + context.getString(R.string.font_weight_medium))
val uiText = ResourceFont(context, R.font.inter_regular, "Inter v3 " + context.getString(R.string.font_weight_regular))
val uiTextMedium = ResourceFont(context, R.font.inter_medium, "Inter v3 " + context.getString(R.string.font_weight_medium))
suspend fun getTypeface(font: Font): Typeface? {
return loadFontAsync(font).await()?.typeface
@@ -320,6 +320,11 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
override val fullDisplayName = name
override val composeFontFamily = FontFamily(ComposeFont("$name.ttf", assets))
override fun saveToJson(obj: JSONObject) {
super.saveToJson(obj)
obj.put(KEY_FAMILY_NAME, name)
}
override fun equals(other: Any?): Boolean {
return other is AssetFont && name == other.name
}
@@ -327,11 +332,22 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
override fun hashCode(): Int {
return hashCode
}
companion object {
@Keep
@JvmStatic
fun fromJson(context: Context, obj: JSONObject): Font {
val assets = context.assets
val name = obj.getString(KEY_FAMILY_NAME)
return AssetFont(assets, name)
}
}
}
class ResourceFont(
context: Context,
resId: Int,
private val resId: Int,
private val name: String,
) : TypefaceFont(ResourcesCompat.getFont(context, resId)) {
@@ -340,6 +356,12 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
override val fullDisplayName = name
override val composeFontFamily = FontFamily(ComposeFont(resId))
override fun saveToJson(obj: JSONObject) {
super.saveToJson(obj)
obj.put(KEY_RESOURCE_ID, resId)
obj.put(KEY_FAMILY_NAME, name)
}
override fun equals(other: Any?): Boolean {
return other is ResourceFont && name == other.name
}
@@ -347,6 +369,17 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
override fun hashCode(): Int {
return hashCode
}
companion object {
@Keep
@JvmStatic
fun fromJson(context: Context, obj: JSONObject): Font {
val resId = obj.getInt(KEY_RESOURCE_ID)
val name = obj.getString(KEY_FAMILY_NAME)
return ResourceFont(context, resId, name)
}
}
}
class GoogleFont(
@@ -490,6 +523,7 @@ class FontCache private constructor(private val context: Context) : SafeCloseabl
private const val KEY_VARIANT = "variant"
private const val KEY_VARIANTS = "variants"
private const val KEY_FONT_NAME = "font"
private const val KEY_RESOURCE_ID = "resourceId"
private val weightNameMap: Map<String, Int> = mapOf(
Pair("100", R.string.font_weight_thin),

View File

@@ -44,8 +44,7 @@ class GoogleFontsListing private constructor(private val context: Context) : Saf
private suspend fun getAdditionalFonts(): List<String> {
val prefs = PreferenceManager2.getInstance(context)
val userFontsString = prefs.additionalFonts.get().first()
val userFonts = if (userFontsString.isEmpty()) emptyList() else userFontsString.split(",")
return listOf("Inter") + userFonts
return if (userFontsString.isEmpty()) emptyList() else userFontsString.split(",")
}
private suspend fun parseFontListing(json: JSONObject): List<GoogleFontInfo> {

View File

@@ -74,6 +74,12 @@ fun FontSelection(
list.add(FontCache.Family(FontCache.SystemFont("sans-serif")))
list.add(FontCache.Family(FontCache.SystemFont("sans-serif-medium")))
list.add(FontCache.Family(FontCache.SystemFont("sans-serif-condensed")))
val interVariants = HashMap<String, FontCache.Font>()
interVariants["regular"] = FontCache.ResourceFont(context, R.font.inter_regular, "Inter v3 " + context.getString(R.string.font_weight_regular))
interVariants["500"] = FontCache.ResourceFont(context, R.font.inter_medium, "Inter v3 " + context.getString(R.string.font_weight_medium))
interVariants["600"] = FontCache.ResourceFont(context, R.font.inter_semi_bold, "Inter v3 " + context.getString(R.string.font_weight_semi_bold))
interVariants["700"] = FontCache.ResourceFont(context, R.font.inter_bold, "Inter v3 " + context.getString(R.string.font_weight_bold))
list.add(FontCache.Family("Inter v3", interVariants))
GoogleFontsListing.INSTANCE.get(context).getFonts().mapTo(list) { font ->
val variantsMap = HashMap<String, FontCache.Font>()
val variants = font.variants.toTypedArray()
@@ -260,6 +266,13 @@ private val VariantButtonContentPadding = PaddingValues(
bottom = 8.dp,
)
private fun removeFamilyPrefix(
familyName: CharSequence,
fontName: CharSequence,
): String {
return fontName.removePrefix(familyName).trim().toString()
}
@Composable
private fun VariantDropdown(
adapter: PreferenceAdapter<FontCache.Font>,
@@ -291,7 +304,7 @@ private fun VariantDropdown(
AndroidText(
modifier = Modifier.wrapContentWidth(),
update = {
it.text = selectedFont.displayName
it.text = removeFamilyPrefix(family.displayName, selectedFont.displayName)
it.setFont(selectedFont)
},
)
@@ -312,7 +325,7 @@ private fun VariantDropdown(
},
text = {
Text(
text = font.displayName,
text = removeFamilyPrefix(family.displayName, font.displayName),
fontFamily = font.composeFontFamily,
)
},