@@ -88,6 +88,7 @@ object PanguText {
8888 fun format (resources : Resources , @Px textSize : Float , text : CharSequence , config : PanguTextConfig = globalConfig): CharSequence {
8989 if (! config.isEnabled) return text
9090 if (text.isBlank()) return text
91+
9192 val formatted = format(text, PH , config)
9293 return text.applySpans(formatted, resources, textSize, config)
9394 }
@@ -110,9 +111,11 @@ object PanguText {
110111 @JvmStatic
111112 fun format (text : CharSequence , whiteSpace : Char = ' ', config : PanguTextConfig = globalConfig): CharSequence {
112113 if (! config.isEnabled) return text
114+
113115 // In any case, always perform a cleanup operation before accepting text.
114116 val processed = text.clearSpans()
115117 val patterns = config.excludePatterns.toTypedArray()
118+
116119 return if ((config.isProcessedSpanned || text !is Spanned ) && text.isNotBlank() && text.length > 1 )
117120 PanguPatterns .matchAndReplace(processed, whiteSpace, * patterns)
118121 else processed
@@ -136,27 +139,34 @@ object PanguText {
136139 whiteSpace : Char = PH
137140 ): CharSequence {
138141 val builder = SpannableStringBuilder (formatted)
142+
139143 formatted.forEachIndexed { index, c ->
140144 // Add spacing to the previous character.
141145 if (c == whiteSpace && index in 0 .. formatted.lastIndex) {
142146 val span = PanguMarginSpan .Placeholder ()
143147 builder.setSpan(span, index - 1 , index, Spanned .SPAN_INCLUSIVE_EXCLUSIVE )
144148 }
145149 }
150+
146151 // Delete the placeholder character.
147152 for (i in (builder.length - 1 ) downTo 0 ) {
148153 if (builder[i] == whiteSpace) builder.delete(i, i + 1 )
149154 }
155+
150156 // Find the [PanguMarginSpan.Placeholder] subscript in [builder] and use [PanguMarginSpan] to set it to [original].
151157 val builderSpans = builder.getSpans(0 , builder.length, classOf<PanguMarginSpan .Placeholder >())
152158 val spannable = if (this !is Spannable ) SpannableString (this ) else this
159+
153160 // Add new [PanguMarginSpan].
154161 builderSpans.forEach {
155162 val start = builder.getSpanStart(it)
156163 val end = builder.getSpanEnd(it)
164+
157165 val span = PanguMarginSpan .create(resources, textSize, config.cjkSpacingRatio)
158166 spannable.setSpan(span, start, end, Spanned .SPAN_INCLUSIVE_EXCLUSIVE )
159- }; builder.clear()
167+ }
168+
169+ builder.clear()
160170 return spannable
161171 }
162172
@@ -170,12 +180,16 @@ object PanguText {
170180 */
171181 private fun CharSequence.clearSpans (): CharSequence {
172182 if (this !is Spannable || isBlank() || ! hasSpan<PanguMarginSpan >()) return this
183+
173184 getSpans(0 , length, classOf<PanguMarginSpan >()).forEach { span ->
174185 val start = getSpanStart(span)
175- val end = getSpanEnd(span)
186+ val end = getSpanEnd(span)
187+
176188 // Clear the [PanguMarginSpan].
177189 if (start < length && end > 0 ) removeSpan(span)
178- }; return this
190+ }
191+
192+ return this
179193 }
180194
181195 /* *
@@ -186,6 +200,7 @@ object PanguText {
186200 private inline fun <reified T : CharacterStyle > CharSequence.hasSpan (): Boolean {
187201 val spannable = this as ? Spanned ? : return false
188202 val spans = spannable.getSpans(0 , spannable.length, classOf<T >())
203+
189204 return spans.isNotEmpty()
190205 }
191206}
0 commit comments