mirror of
https://github.com/fcitx5-android/fcitx5-android.git
synced 2026-08-02 04:44:35 +08:00
Merge df1f29c8bc12408ab59f75198c664abb75991159 into 068b74e7d8ab64dd42c249633ec8c2f9abc4913e
This commit is contained in:
commit
d9502d8d0e
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user