Skip to content

Commit 492637f

Browse files
feedback
1 parent c0a4a56 commit 492637f

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

android-sample/src/main/java/com/zachklipp/richtext/sample/Demo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import com.halilibo.richtext.ui.string.withFormat
7474
val dottedLinkDecorations = RichTextDecorations(
7575
linkDecorations = listOf(
7676
LinkDecoration(
77-
matcher = { destination -> destination.contains("dotted") },
77+
matcher = { destination, _ -> destination.contains("dotted") },
7878
underlineStyle = UnderlineStyle.Dotted(),
7979
linkStyleOverride = { base ->
8080
TextLinkStyles(

android-sample/src/main/java/com/zachklipp/richtext/sample/MarkdownSample.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ import com.halilibo.richtext.ui.string.UnderlineStyle
132132
RichTextDecorations(
133133
linkDecorations = listOf(
134134
LinkDecoration(
135-
matcher = { destination -> destination.contains("dotted") },
135+
matcher = { destination, _ -> destination.contains("dotted") },
136136
underlineStyle = UnderlineStyle.Dotted(),
137137
),
138138
LinkDecoration(
139-
matcher = { destination -> destination.contains("dashed") },
139+
matcher = { destination, _ -> destination.contains("dashed") },
140140
underlineStyle = UnderlineStyle.Dashed(),
141141
),
142142
),

richtext-ui/src/commonMain/kotlin/com/halilibo/richtext/ui/string/LinkDecorations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import androidx.compose.ui.text.TextLinkStyles
88
* Defines how specific links should be decorated based on their destination.
99
*/
1010
public data class LinkDecoration(
11-
val matcher: (String) -> Boolean,
11+
val matcher: (destination: String, text: String) -> Boolean,
1212
val underlineStyle: UnderlineStyle = UnderlineStyle.Solid,
1313
val linkStyleOverride: ((TextLinkStyles?) -> TextLinkStyles)? = null,
1414
)

richtext-ui/src/commonMain/kotlin/com/halilibo/richtext/ui/string/RichTextString.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ public class RichTextString internal constructor(
183183
// And apply their actual SpanStyles to the string.
184184
tags.forEach { range ->
185185
val format = Format.findTag(range.item, formatObjects) ?: return@forEach
186-
when (format) {
187-
is Format.Link -> {
188-
val decoration = decorations.findLinkDecoration(format.destination)
186+
if (format is Format.Link) {
187+
val linkText = taggedString.text.substring(range.start, range.end)
188+
val decoration = decorations.findLinkDecoration(format.destination, linkText)
189189
val linkStyle = decoration?.linkStyleOverride
190190
?.invoke(style.linkStyle)
191191
?: style.linkStyle
@@ -200,17 +200,15 @@ public class RichTextString internal constructor(
200200
linkInteractionListener = format.linkInteractionListener,
201201
)
202202
addLink(linkAnnotation, range.start, range.end)
203-
}
204-
else -> {
205-
format.getAnnotation(style, contentColor)
206-
?.let { annotation ->
207-
if (annotation is SpanStyle) {
208-
addStyle(annotation, range.start, range.end)
209-
} else if (annotation is LinkAnnotation.Url) {
210-
addLink(annotation, range.start, range.end)
211-
}
203+
} else {
204+
format.getAnnotation(style, contentColor)
205+
?.let { annotation ->
206+
if (annotation is SpanStyle) {
207+
addStyle(annotation, range.start, range.end)
208+
} else if (annotation is LinkAnnotation.Url) {
209+
addLink(annotation, range.start, range.end)
212210
}
213-
}
211+
}
214212
}
215213
}
216214
}
@@ -238,7 +236,8 @@ public class RichTextString internal constructor(
238236
val format = Format.findTag(range.item, formatObjects)
239237
as? Format.Link
240238
?: return@mapNotNull null
241-
val decoration = decorations.findLinkDecoration(format.destination)
239+
val linkText = taggedString.text.substring(range.start, range.end)
240+
val decoration = decorations.findLinkDecoration(format.destination, linkText)
242241
?: return@mapNotNull null
243242
if (decoration.underlineStyle is UnderlineStyle.Solid) return@mapNotNull null
244243
DecoratedLinkRange(
@@ -507,8 +506,10 @@ internal data class DecoratedLinkRange(
507506
val linkStyleOverride: ((TextLinkStyles?) -> TextLinkStyles)?,
508507
)
509508

510-
private fun RichTextDecorations.findLinkDecoration(destination: String): LinkDecoration? =
511-
linkDecorations.firstOrNull { it.matcher(destination) }
509+
private fun RichTextDecorations.findLinkDecoration(
510+
destination: String,
511+
text: String,
512+
): LinkDecoration? = linkDecorations.firstOrNull { it.matcher(destination, text) }
512513

513514
private fun TextLinkStyles?.withoutUnderline(): TextLinkStyles? {
514515
if (this == null) return null

richtext-ui/src/commonMain/kotlin/com/halilibo/richtext/ui/string/Text.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import androidx.compose.ui.graphics.ShaderBrush
2727
import androidx.compose.ui.graphics.StrokeCap
2828
import androidx.compose.ui.graphics.isSpecified
2929
import androidx.compose.ui.layout.layout
30-
import androidx.compose.ui.platform.LocalDensity
3130
import androidx.compose.ui.text.AnnotatedString
3231
import androidx.compose.ui.text.SpanStyle
3332
import androidx.compose.ui.text.TextLayoutResult
3433
import androidx.compose.ui.text.buildAnnotatedString
3534
import androidx.compose.ui.text.style.TextOverflow
3635
import androidx.compose.ui.unit.Constraints
3736
import androidx.compose.ui.unit.dp
37+
import androidx.compose.ui.util.fastForEach
3838
import com.halilibo.richtext.ui.RichTextScope
3939
import com.halilibo.richtext.ui.Text
4040
import com.halilibo.richtext.ui.currentContentColor
@@ -45,6 +45,7 @@ import com.halilibo.richtext.ui.util.segmentIntoPhrases
4545
import kotlinx.coroutines.delay
4646
import kotlinx.coroutines.launch
4747
import kotlin.math.pow
48+
import kotlin.math.roundToInt
4849
import kotlin.time.Duration.Companion.milliseconds
4950

5051
/**
@@ -67,7 +68,6 @@ public fun RichTextScope.Text(
6768
) {
6869
val style = currentRichTextStyle.stringStyle
6970
val contentColor = currentContentColor
70-
val density = LocalDensity.current
7171
val resolvedStyle = remember(style) {
7272
(style ?: RichTextStringStyle.Default).resolveDefaults()
7373
}
@@ -97,14 +97,13 @@ public fun RichTextScope.Text(
9797
Modifier.drawWithContent {
9898
drawContent()
9999
val layoutResult = textLayoutResult ?: return@drawWithContent
100-
underlineSpecs.forEach { spec ->
100+
underlineSpecs.fastForEach { spec ->
101101
drawUnderline(
102102
layoutResult = layoutResult,
103103
start = spec.range.start,
104104
end = spec.range.end,
105105
underlineStyle = spec.range.underlineStyle,
106106
color = spec.color,
107-
density = density,
108107
)
109108
}
110109
}
@@ -176,7 +175,6 @@ private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawUnderline(
176175
end: Int,
177176
underlineStyle: UnderlineStyle,
178177
color: Color,
179-
density: androidx.compose.ui.unit.Density,
180178
) {
181179
val textLength = layoutResult.layoutInput.text.text.length
182180
val clampedStart = start.coerceIn(0, textLength)
@@ -188,7 +186,7 @@ private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawUnderline(
188186
val pathEffect: PathEffect?
189187
val cap: StrokeCap
190188

191-
with(density) {
189+
with(this) {
192190
when (underlineStyle) {
193191
is UnderlineStyle.Solid -> {
194192
strokeWidthPx = 1.dp.toPx()
@@ -226,9 +224,9 @@ private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawUnderline(
226224

227225
val startBox = layoutResult.getBoundingBox(segmentStart)
228226
val endBox = layoutResult.getBoundingBox(segmentEnd - 1)
229-
val y = maxOf(startBox.bottom, endBox.bottom) + offsetPx
230-
val xStart = startBox.left
231-
val xEnd = endBox.right
227+
val y = (maxOf(startBox.bottom, endBox.bottom) + offsetPx).roundToInt().toFloat()
228+
val xStart = startBox.left.roundToInt().toFloat()
229+
val xEnd = endBox.right.roundToInt().toFloat()
232230

233231
drawLine(
234232
color = color,

0 commit comments

Comments
 (0)