Skip to content

Commit

Permalink
fix(core/ui): fonts
Browse files Browse the repository at this point in the history
Signed-off-by: rhunk <[email protected]>
  • Loading branch information
rhunk committed Dec 19, 2024
1 parent d71b996 commit 61a20e7
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions core/src/main/kotlin/me/rhunk/snapenhance/core/ui/UserInterface.kt
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -47,14 +48,36 @@ class UserInterface(
fun init() {
ResourcesCompat::class.java.hook("getFont", HookStage.BEFORE) { param ->
val id = param.arg<Int>(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<Typeface>()
fontMap[typeface.weight] = typeface
lateinit var unhook: () -> Unit

unhook = Resources::class.java.hook("getValue", HookStage.AFTER) { param ->
val typedValue = param.argNullable<TypedValue>(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() } }
}
}
}

0 comments on commit 61a20e7

Please sign in to comment.