|
| 1 | +package com.halilibo.richtext.ui.string |
| 2 | + |
| 3 | +import androidx.compose.foundation.Image |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.padding |
| 6 | +import androidx.compose.foundation.layout.size |
| 7 | +import androidx.compose.foundation.text.appendInlineContent |
| 8 | +import androidx.compose.ui.Modifier |
| 9 | +import androidx.compose.ui.graphics.ColorFilter |
| 10 | +import androidx.compose.ui.text.AnnotatedString |
| 11 | +import androidx.compose.ui.text.LinkAnnotation |
| 12 | +import androidx.compose.ui.unit.Dp |
| 13 | +import androidx.compose.ui.unit.DpSize |
| 14 | +import androidx.compose.ui.unit.IntSize |
| 15 | +import androidx.compose.ui.unit.dp |
| 16 | +import kotlin.math.roundToInt |
| 17 | + |
| 18 | +internal data class DecoratedTextResult( |
| 19 | + val annotatedString: AnnotatedString, |
| 20 | + val inlineContents: Map<String, InlineContent>, |
| 21 | + val decoratedLinkRanges: List<DecoratedLinkRange>, |
| 22 | +) |
| 23 | + |
| 24 | +internal fun ResolvedLinkDecorationRange.hasInlineContent(): Boolean { |
| 25 | + val inlineContent = inlineContent ?: return false |
| 26 | + return inlineContent.leading != null || inlineContent.trailing != null |
| 27 | +} |
| 28 | + |
| 29 | +internal fun decorateAnnotatedStringWithLinkIcons( |
| 30 | + annotated: AnnotatedString, |
| 31 | + baseInlineContents: Map<String, InlineContent>, |
| 32 | + linkDecorations: List<ResolvedLinkDecorationRange>, |
| 33 | +): DecoratedTextResult { |
| 34 | + if (linkDecorations.isEmpty()) { |
| 35 | + return DecoratedTextResult( |
| 36 | + annotatedString = annotated, |
| 37 | + inlineContents = baseInlineContents, |
| 38 | + decoratedLinkRanges = emptyList(), |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + val builder = AnnotatedString.Builder() |
| 43 | + val inlineContents = LinkedHashMap<String, InlineContent>(baseInlineContents.size) |
| 44 | + inlineContents.putAll(baseInlineContents) |
| 45 | + val decoratedLinkRanges = mutableListOf<DecoratedLinkRange>() |
| 46 | + val sortedDecorations = linkDecorations.sortedBy { it.start } |
| 47 | + var cursor = 0 |
| 48 | + |
| 49 | + sortedDecorations.forEachIndexed { index, decoration -> |
| 50 | + val start = decoration.start |
| 51 | + val end = decoration.end |
| 52 | + if (cursor < start) { |
| 53 | + builder.append(annotated, cursor, start) |
| 54 | + } |
| 55 | + if (start >= end) { |
| 56 | + cursor = maxOf(cursor, end) |
| 57 | + return@forEachIndexed |
| 58 | + } |
| 59 | + |
| 60 | + val inlineContent = decoration.inlineContent |
| 61 | + val linkAnnotation = annotated.getLinkAnnotations(start, end) |
| 62 | + .firstOrNull() |
| 63 | + ?.item as? LinkAnnotation.Url |
| 64 | + if (inlineContent != null) { |
| 65 | + inlineContent.leading?.let { spec -> |
| 66 | + val id = "link_inline_${index}_leading" |
| 67 | + builder.appendInlineContent(id, REPLACEMENT_CHAR) |
| 68 | + inlineContents[id] = buildInlineContent( |
| 69 | + spec = spec, |
| 70 | + context = LinkContext(decoration.destination, decoration.text), |
| 71 | + spacing = inlineContent.spacing, |
| 72 | + isLeading = true, |
| 73 | + ) |
| 74 | + if (inlineContent.includeInHitTarget && linkAnnotation != null) { |
| 75 | + val iconStart = builder.length - 1 |
| 76 | + builder.addLink(linkAnnotation, iconStart, builder.length) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + val textStart = builder.length |
| 82 | + builder.append(annotated, start, end) |
| 83 | + val textEnd = builder.length |
| 84 | + |
| 85 | + if (inlineContent != null) { |
| 86 | + inlineContent.trailing?.let { spec -> |
| 87 | + val id = "link_inline_${index}_trailing" |
| 88 | + builder.appendInlineContent(id, REPLACEMENT_CHAR) |
| 89 | + inlineContents[id] = buildInlineContent( |
| 90 | + spec = spec, |
| 91 | + context = LinkContext(decoration.destination, decoration.text), |
| 92 | + spacing = inlineContent.spacing, |
| 93 | + isLeading = false, |
| 94 | + ) |
| 95 | + if (inlineContent.includeInHitTarget && linkAnnotation != null) { |
| 96 | + val iconStart = builder.length - 1 |
| 97 | + builder.addLink(linkAnnotation, iconStart, builder.length) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (decoration.underlineStyle !is UnderlineStyle.Solid) { |
| 103 | + decoratedLinkRanges += DecoratedLinkRange( |
| 104 | + start = textStart, |
| 105 | + end = textEnd, |
| 106 | + destination = decoration.destination, |
| 107 | + underlineStyle = decoration.underlineStyle, |
| 108 | + linkStyleOverride = decoration.linkStyleOverride, |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + cursor = end |
| 113 | + } |
| 114 | + |
| 115 | + if (cursor < annotated.length) { |
| 116 | + builder.append(annotated, cursor, annotated.length) |
| 117 | + } |
| 118 | + |
| 119 | + return DecoratedTextResult( |
| 120 | + annotatedString = builder.toAnnotatedString(), |
| 121 | + inlineContents = inlineContents, |
| 122 | + decoratedLinkRanges = decoratedLinkRanges, |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +private fun buildInlineContent( |
| 127 | + spec: InlineIconSpec, |
| 128 | + context: LinkContext, |
| 129 | + spacing: Dp, |
| 130 | + isLeading: Boolean, |
| 131 | +): InlineContent { |
| 132 | + val spacingWidth = if (spacing.value > 0f) spacing else 0.dp |
| 133 | + val placeholderSize = if (spacingWidth.value > 0f) { |
| 134 | + DpSize( |
| 135 | + width = spec.size.width + spacingWidth, |
| 136 | + height = spec.size.height, |
| 137 | + ) |
| 138 | + } else { |
| 139 | + spec.size |
| 140 | + } |
| 141 | + |
| 142 | + val paddingModifier = when { |
| 143 | + spacingWidth.value <= 0f -> Modifier |
| 144 | + isLeading -> Modifier.padding(end = spacingWidth) |
| 145 | + else -> Modifier.padding(start = spacingWidth) |
| 146 | + } |
| 147 | + |
| 148 | + val contentModifier = paddingModifier |
| 149 | + .then(Modifier.size(spec.size)) |
| 150 | + |
| 151 | + return InlineContent( |
| 152 | + initialSize = { |
| 153 | + IntSize( |
| 154 | + placeholderSize.width.toPx().roundToInt(), |
| 155 | + placeholderSize.height.toPx().roundToInt(), |
| 156 | + ) |
| 157 | + }, |
| 158 | + placeholderVerticalAlign = spec.placeholderVerticalAlign, |
| 159 | + ) { |
| 160 | + when (spec) { |
| 161 | + is InlineIconSpec.Painter -> { |
| 162 | + Image( |
| 163 | + painter = spec.painter, |
| 164 | + contentDescription = spec.contentDescription, |
| 165 | + colorFilter = spec.tint?.let { ColorFilter.tint(it) }, |
| 166 | + modifier = contentModifier, |
| 167 | + ) |
| 168 | + } |
| 169 | + is InlineIconSpec.Composable -> { |
| 170 | + Box(modifier = contentModifier) { |
| 171 | + spec.content.Render(context) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments