Merge df1f29c8bc12408ab59f75198c664abb75991159 into 068b74e7d8ab64dd42c249633ec8c2f9abc4913e

This commit is contained in:
ketal 2026-05-31 21:30:47 +08:00 committed by GitHub
commit d9502d8d0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 17 deletions

View File

@ -41,6 +41,7 @@ class AutoScaleTextView @JvmOverloads constructor(
}
var scaleMode = Mode.None
var enableBaselineAdjust = false
private lateinit var text: String
@ -92,26 +93,31 @@ class AutoScaleTextView @JvmOverloads constructor(
private fun measureTextBounds(): Rect {
if (needsMeasureText) {
val paint = paint
paint.getFontMetrics(fontMetrics)
val codePointCount = Character.codePointCount(text, 0, text.length)
if (codePointCount == 1) {
// use actual text bounds when there is only one "character",
// eg. full-width punctuation
paint.getTextBounds(text, 0, text.length, textBounds)
} else {
textBounds.set(
/* left = */ 0,
/* top = */ floor(fontMetrics.top).toInt(),
/* right = */ ceil(paint.measureText(text)).toInt(),
/* bottom = */ ceil(fontMetrics.bottom).toInt()
)
}
measureTextBounds(text, textBounds)
needsMeasureText = false
}
return textBounds
}
private fun measureTextBounds(text: String, textBounds: Rect): Rect {
val paint = paint
paint.getFontMetrics(fontMetrics)
val codePointCount = Character.codePointCount(text, 0, text.length)
if (codePointCount == 1) {
// use actual text bounds when there is only one "character",
// eg. full-width punctuation
paint.getTextBounds(text, 0, text.length, textBounds)
} else {
textBounds.set(
/* left = */ 0,
/* top = */ floor(fontMetrics.top).toInt(),
/* right = */ ceil(paint.measureText(text)).toInt(),
/* bottom = */ ceil(fontMetrics.bottom).toInt()
)
}
return textBounds
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
if (needsCalculateTransform || changed) {
calculateTransform(right - left, bottom - top)
@ -154,8 +160,15 @@ class AutoScaleTextView @JvmOverloads constructor(
textScaleY = 1.0f
translateX = if (shouldAlignLeft) leftAlignOffset else centerAlignOffset
}
val fontHeight = (fontMetrics.bottom - fontMetrics.top) * textScaleY
val fontOffsetY = fontMetrics.top * textScaleY
val offsetBounds = Rect()
val refChar = if (text.isNotEmpty() && text[0].isLowerCase()) "o" else "O"
measureTextBounds(refChar, offsetBounds)
val scaleY = textScaleY
val (fontHeight, fontOffsetY) = if (enableBaselineAdjust) {
offsetBounds.height() * scaleY to offsetBounds.top * scaleY
} else {
(fontMetrics.bottom - fontMetrics.top) * scaleY to fontMetrics.top * scaleY
}
translateY = (contentHeight.toFloat() - fontHeight) / 2.0f - fontOffsetY + paddingTop
}

View File

@ -267,6 +267,7 @@ open class TextKeyView(ctx: Context, theme: Theme, def: KeyDef.Appearance.Text)
Variant.Accent -> theme.accentKeyTextColor
}
)
enableBaselineAdjust = ThemeManager.prefs.punctuationPosition.getValue() != PunctuationPosition.Bottom
}
init {