fix: 使用TextPaint副本避免修改共享paint影响绘制线程
This commit is contained in:
1 file changed
+4
-8
@@ -1365,7 +1365,7 @@ class TextChapterLayout(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 对有自定义字体的高亮字符重新测量宽度,确保排版和绘制使用同一套字体度量。
|
* 对有自定义字体的高亮字符重新测量宽度,确保排版和绘制使用同一套字体度量。
|
||||||
* 不使用 getTextWidthsCompat 避免对子串错误添加 letterSpacing。
|
* 使用 textPaint 副本测量,避免修改共享 paint 的 typeface 影响绘制线程。
|
||||||
*/
|
*/
|
||||||
private fun remeasureWithHighlightFonts(
|
private fun remeasureWithHighlightFonts(
|
||||||
text: String,
|
text: String,
|
||||||
@@ -1373,8 +1373,7 @@ class TextChapterLayout(
|
|||||||
textPaint: TextPaint,
|
textPaint: TextPaint,
|
||||||
widthsArray: FloatArray
|
widthsArray: FloatArray
|
||||||
) {
|
) {
|
||||||
val originalTypeface = textPaint.typeface
|
val measurePaint = TextPaint(textPaint)
|
||||||
try {
|
|
||||||
var i = 0
|
var i = 0
|
||||||
while (i < text.length) {
|
while (i < text.length) {
|
||||||
val fontPath = charStyles[i]?.fontPath.orEmpty()
|
val fontPath = charStyles[i]?.fontPath.orEmpty()
|
||||||
@@ -1385,15 +1384,12 @@ class TextChapterLayout(
|
|||||||
while (i < text.length && charStyles[i]?.fontPath.orEmpty() == fontPath) i++
|
while (i < text.length && charStyles[i]?.fontPath.orEmpty() == fontPath) i++
|
||||||
val segEnd = i
|
val segEnd = i
|
||||||
val typeface = TextColumn.getTypeface(fontPath) ?: continue
|
val typeface = TextColumn.getTypeface(fontPath) ?: continue
|
||||||
textPaint.typeface = typeface
|
measurePaint.typeface = typeface
|
||||||
val segLen = segEnd - segStart
|
val segLen = segEnd - segStart
|
||||||
val segWidths = FloatArray(segLen)
|
val segWidths = FloatArray(segLen)
|
||||||
textPaint.getTextWidths(text, segStart, segEnd, segWidths)
|
measurePaint.getTextWidths(text, segStart, segEnd, segWidths)
|
||||||
segWidths.copyInto(widthsArray, segStart)
|
segWidths.copyInto(widthsArray, segStart)
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
textPaint.typeface = originalTypeface
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user