mirror of
https://github.com/BetterAndroid/PanguText.git
synced 2026-02-04 04:16:48 +08:00
refactor: migrate to BetterAndroid new usage
This commit is contained in:
@@ -32,6 +32,7 @@ import android.text.style.CharacterStyle
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.Px
|
||||
import com.highcapable.kavaref.extension.classOf
|
||||
import com.highcapable.pangutext.android.PanguText.PH
|
||||
import com.highcapable.pangutext.android.core.PanguMarginSpan
|
||||
import com.highcapable.pangutext.android.core.PanguPatterns
|
||||
import com.highcapable.pangutext.android.extension.injectPanguText
|
||||
@@ -155,7 +156,7 @@ object PanguText {
|
||||
|
||||
// Find the [PanguMarginSpan.Placeholder] subscript in [builder] and use [PanguMarginSpan] to set it to [original].
|
||||
val builderSpans = builder.getSpans(0, builder.length, classOf<PanguMarginSpan.Placeholder>())
|
||||
val spannable = if (this !is Spannable) SpannableString(this) else this
|
||||
val spannable = this as? Spannable ?: SpannableString(this)
|
||||
|
||||
// Add new [PanguMarginSpan].
|
||||
builderSpans.forEach {
|
||||
|
||||
@@ -39,7 +39,7 @@ import kotlin.math.round
|
||||
* Pangu span with margin.
|
||||
* @param margin the margin size (px).
|
||||
*/
|
||||
internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
|
||||
internal class PanguMarginSpan(@field:Px val margin: Int) : ReplacementSpan() {
|
||||
|
||||
internal companion object {
|
||||
|
||||
@@ -71,25 +71,25 @@ internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
|
||||
|
||||
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
|
||||
if (text is Spanned) text.getSpans<Any>(start, end).forEach { span ->
|
||||
when {
|
||||
span is BackgroundColorSpan -> {
|
||||
when (span) {
|
||||
is BackgroundColorSpan -> {
|
||||
// Get background color.
|
||||
val color = span.backgroundColor
|
||||
val originalColor = paint.color
|
||||
|
||||
|
||||
// Save the current [paint] color.
|
||||
paint.color = color
|
||||
|
||||
|
||||
// Get the width of the text.
|
||||
val textWidth = paint.measureText(text, start, end)
|
||||
|
||||
|
||||
// Draw background rectangle.
|
||||
canvas.drawRect(x, top.toFloat(), x + textWidth + margin, bottom.toFloat(), paint)
|
||||
|
||||
|
||||
// Restore original color.
|
||||
paint.color = originalColor
|
||||
}
|
||||
span is CharacterStyle && paint is TextPaint -> span.updateDrawState(paint)
|
||||
is CharacterStyle if paint is TextPaint -> span.updateDrawState(paint)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
*
|
||||
* This file is created by fankes on 2025/1/20.
|
||||
*/
|
||||
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable")
|
||||
@file:Suppress("RegExpRedundantEscape", "RegExpSimplifiable", "CanConvertToMultiDollarString", "CanUnescapeDollarLiteral")
|
||||
|
||||
package com.highcapable.pangutext.android.core
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import com.highcapable.betterandroid.system.extension.tool.AndroidVersion
|
||||
import com.highcapable.betterandroid.system.extension.utils.AndroidVersion
|
||||
import com.highcapable.pangutext.android.PanguText
|
||||
import com.highcapable.pangutext.android.PanguTextConfig
|
||||
import com.highcapable.pangutext.android.core.TextViewDelegate.Companion.delegate
|
||||
|
||||
@@ -94,6 +94,8 @@ fun TextView.injectRealTimePanguText(injectHint: Boolean = true, config: PanguTe
|
||||
val self = this@injectRealTimePanguText
|
||||
if (self.hint != currentHint)
|
||||
self.setHintWithPangu(self.hint, config)
|
||||
|
||||
@Suppress("AssignedValueIsNeverRead")
|
||||
currentHint = self.hint
|
||||
}
|
||||
|
||||
|
||||
@@ -44,10 +44,10 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
|
||||
|
||||
val matcher = regex.toPattern().matcher(this)
|
||||
val excludeMatchers = excludePatterns.map { it.toPattern().matcher(this) }
|
||||
val excludeIndexs = mutableSetOf<Pair<Int, Int>>()
|
||||
val excludeIndexes = mutableSetOf<Pair<Int, Int>>()
|
||||
|
||||
excludeMatchers.forEach {
|
||||
while (it.find()) excludeIndexs.add(it.start() to it.end())
|
||||
while (it.find()) excludeIndexes.add(it.start() to it.end())
|
||||
}
|
||||
|
||||
var offset = 0
|
||||
@@ -59,7 +59,7 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
|
||||
|
||||
// Skip the replacement if the matched range is excluded.
|
||||
// The character range offset is adjusted by 1 to avoid the exclusion of the matched range.
|
||||
if (excludeIndexs.any { it.first <= start + 1 && it.second >= end - 1 }) continue
|
||||
if (excludeIndexes.any { it.first <= start + 1 && it.second >= end - 1 }) continue
|
||||
|
||||
// Perform the replacement.
|
||||
val replacementText = matcher.buildReplacementText(replacement)
|
||||
|
||||
@@ -49,7 +49,7 @@ import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
|
||||
internal object PanguWidget {
|
||||
|
||||
/** The text regex split symbol. */
|
||||
private const val TEXT_REGEX_SPLITE_SYMBOL = "|@|"
|
||||
private const val TEXT_REGEX_SPLIT_SYMBOL = "|@|"
|
||||
|
||||
/**
|
||||
* Process the widget by the given name.
|
||||
@@ -118,7 +118,7 @@ internal object PanguWidget {
|
||||
val cjkSpacingRatio = getFloatOrNull(R.styleable.PanguTextHelper_panguText_cjkSpacingRatio)
|
||||
|
||||
val excludePatterns = getStringOrNull(R.styleable.PanguTextHelper_panguText_excludePatterns)
|
||||
?.split(TEXT_REGEX_SPLITE_SYMBOL)?.mapNotNull { regex ->
|
||||
?.split(TEXT_REGEX_SPLIT_SYMBOL)?.mapNotNull { regex ->
|
||||
runCatching { regex.toRegex() }.onFailure { th ->
|
||||
Log.e(PangutextAndroidProperties.PROJECT_NAME, "Invalid exclude pattern of $instance: $regex", th)
|
||||
}.getOrNull()
|
||||
|
||||
Reference in New Issue
Block a user