From 61a20e76fa29f483edb67d452c712599dde672f4 Mon Sep 17 00:00:00 2001 From: rhunk <101876869+rhunk@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:22:30 +0100 Subject: [PATCH] fix(core/ui): fonts Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com> --- .../snapenhance/core/ui/UserInterface.kt | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt index 2f424a71..7e19bd8d 100644 --- a/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt +++ b/core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt @@ -1,13 +1,14 @@ package me.rhunk.snapenhance.core.ui +import android.content.res.Resources import android.graphics.Typeface +import android.util.TypedValue import android.view.Gravity import android.widget.TextView import androidx.core.content.res.ResourcesCompat import me.rhunk.snapenhance.core.ModContext import me.rhunk.snapenhance.core.util.hook.HookStage import me.rhunk.snapenhance.core.util.hook.hook -import me.rhunk.snapenhance.core.util.hook.hookConstructor import me.rhunk.snapenhance.core.util.ktx.isDarkTheme class UserInterface( @@ -47,14 +48,36 @@ class UserInterface( fun init() { ResourcesCompat::class.java.hook("getFont", HookStage.BEFORE) { param -> val id = param.arg(1) - if (fontMap.containsKey(id)) { + if (id == avenirNextFontId) { + param.setResult(avenirNextTypeface) + } else if (fontMap.containsKey(id)) { param.setResult(fontMap[id]) } } - Typeface::class.java.hookConstructor(HookStage.AFTER) { param -> - val typeface = param.thisObject() - fontMap[typeface.weight] = typeface + lateinit var unhook: () -> Unit + + unhook = Resources::class.java.hook("getValue", HookStage.AFTER) { param -> + val typedValue = param.argNullable(1)?.takeIf { + it.resourceId != 0 && + it.type == TypedValue.TYPE_STRING && it.string?.endsWith(".ttf") == true + } ?: return@hook + + var offset = typedValue.resourceId.shr(8).shl(8) + + while (true) { + var font = try { + context.resources.getFont(++offset) + } catch (_: Throwable) { + break + } + + fontMap[font.weight] = font + } + + unhook() + }.let { + { it.forEach { it.unhook() } } } } } \ No newline at end of file