mirror of
https://github.com/BetterAndroid/PanguText.git
synced 2026-02-04 12:17:24 +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 android.widget.TextView
|
||||||
import androidx.annotation.Px
|
import androidx.annotation.Px
|
||||||
import com.highcapable.kavaref.extension.classOf
|
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.PanguMarginSpan
|
||||||
import com.highcapable.pangutext.android.core.PanguPatterns
|
import com.highcapable.pangutext.android.core.PanguPatterns
|
||||||
import com.highcapable.pangutext.android.extension.injectPanguText
|
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].
|
// 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 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].
|
// Add new [PanguMarginSpan].
|
||||||
builderSpans.forEach {
|
builderSpans.forEach {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import kotlin.math.round
|
|||||||
* Pangu span with margin.
|
* Pangu span with margin.
|
||||||
* @param margin the margin size (px).
|
* @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 {
|
internal companion object {
|
||||||
|
|
||||||
@@ -71,8 +71,8 @@ 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) {
|
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 ->
|
if (text is Spanned) text.getSpans<Any>(start, end).forEach { span ->
|
||||||
when {
|
when (span) {
|
||||||
span is BackgroundColorSpan -> {
|
is BackgroundColorSpan -> {
|
||||||
// Get background color.
|
// Get background color.
|
||||||
val color = span.backgroundColor
|
val color = span.backgroundColor
|
||||||
val originalColor = paint.color
|
val originalColor = paint.color
|
||||||
@@ -89,7 +89,7 @@ internal class PanguMarginSpan(@param:Px val margin: Int) : ReplacementSpan() {
|
|||||||
// Restore original color.
|
// Restore original color.
|
||||||
paint.color = originalColor
|
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.
|
* 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
|
package com.highcapable.pangutext.android.core
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import android.text.Editable
|
|||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import android.widget.TextView
|
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.PanguText
|
||||||
import com.highcapable.pangutext.android.PanguTextConfig
|
import com.highcapable.pangutext.android.PanguTextConfig
|
||||||
import com.highcapable.pangutext.android.core.TextViewDelegate.Companion.delegate
|
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
|
val self = this@injectRealTimePanguText
|
||||||
if (self.hint != currentHint)
|
if (self.hint != currentHint)
|
||||||
self.setHintWithPangu(self.hint, config)
|
self.setHintWithPangu(self.hint, config)
|
||||||
|
|
||||||
|
@Suppress("AssignedValueIsNeverRead")
|
||||||
currentHint = self.hint
|
currentHint = self.hint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
|
|||||||
|
|
||||||
val matcher = regex.toPattern().matcher(this)
|
val matcher = regex.toPattern().matcher(this)
|
||||||
val excludeMatchers = excludePatterns.map { it.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 {
|
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
|
var offset = 0
|
||||||
@@ -59,7 +59,7 @@ internal fun CharSequence.replaceAndPreserveSpans(regex: Regex, replacement: Str
|
|||||||
|
|
||||||
// Skip the replacement if the matched range is excluded.
|
// 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.
|
// 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.
|
// Perform the replacement.
|
||||||
val replacementText = matcher.buildReplacementText(replacement)
|
val replacementText = matcher.buildReplacementText(replacement)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ import com.highcapable.pangutext.android.generated.PangutextAndroidProperties
|
|||||||
internal object PanguWidget {
|
internal object PanguWidget {
|
||||||
|
|
||||||
/** The text regex split symbol. */
|
/** 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.
|
* Process the widget by the given name.
|
||||||
@@ -118,7 +118,7 @@ internal object PanguWidget {
|
|||||||
val cjkSpacingRatio = getFloatOrNull(R.styleable.PanguTextHelper_panguText_cjkSpacingRatio)
|
val cjkSpacingRatio = getFloatOrNull(R.styleable.PanguTextHelper_panguText_cjkSpacingRatio)
|
||||||
|
|
||||||
val excludePatterns = getStringOrNull(R.styleable.PanguTextHelper_panguText_excludePatterns)
|
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 ->
|
runCatching { regex.toRegex() }.onFailure { th ->
|
||||||
Log.e(PangutextAndroidProperties.PROJECT_NAME, "Invalid exclude pattern of $instance: $regex", th)
|
Log.e(PangutextAndroidProperties.PROJECT_NAME, "Invalid exclude pattern of $instance: $regex", th)
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
|
|||||||
Reference in New Issue
Block a user