1 /*
<lambda>null2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.foundation.text
18
19 import androidx.compose.runtime.Composable
20 import androidx.compose.ui.layout.Layout
21 import androidx.compose.ui.text.AnnotatedString
22 import androidx.compose.ui.text.Placeholder
23 import androidx.compose.ui.util.fastForEach
24 import androidx.compose.ui.util.fastMap
25
26 internal typealias PlaceholderRange = AnnotatedString.Range<Placeholder>
27
28 internal typealias InlineContentRange = AnnotatedString.Range<@Composable (String) -> Unit>
29
30 /**
31 * Attempts to match AnnotatedString placeholders with passed [InlineTextContent]
32 *
33 * Matches will produce a entry in both returned lists.
34 *
35 * Non-matches will be ignored silently.
36 */
37 internal fun AnnotatedString.resolveInlineContent(
38 inlineContent: Map<String, InlineTextContent>?
39 ): Pair<List<PlaceholderRange>, List<InlineContentRange>> {
40 if (inlineContent.isNullOrEmpty()) {
41 return EmptyInlineContent
42 }
43 val inlineContentAnnotations = getStringAnnotations(INLINE_CONTENT_TAG, 0, text.length)
44
45 val placeholders = mutableListOf<AnnotatedString.Range<Placeholder>>()
46 val inlineComposables = mutableListOf<AnnotatedString.Range<@Composable (String) -> Unit>>()
47 inlineContentAnnotations.fastForEach { annotation ->
48 inlineContent[annotation.item]?.let { inlineTextContent ->
49 placeholders.add(
50 AnnotatedString.Range(
51 inlineTextContent.placeholder,
52 annotation.start,
53 annotation.end
54 )
55 )
56 inlineComposables.add(
57 AnnotatedString.Range(inlineTextContent.children, annotation.start, annotation.end)
58 )
59 }
60 }
61 return Pair(placeholders, inlineComposables)
62 }
63
hasInlineContentnull64 internal fun AnnotatedString.hasInlineContent(): Boolean =
65 hasStringAnnotations(INLINE_CONTENT_TAG, 0, text.length)
66
67 @Composable
68 internal fun InlineChildren(text: AnnotatedString, inlineContents: List<InlineContentRange>) {
69 inlineContents.fastForEach { (content, start, end) ->
70 Layout(content = { content(text.subSequence(start, end).text) }) { children, constrains ->
71 val placeables = children.fastMap { it.measure(constrains) }
72 layout(width = constrains.maxWidth, height = constrains.maxHeight) {
73 placeables.fastForEach { it.placeRelative(0, 0) }
74 }
75 }
76 }
77 }
78
79 private val EmptyInlineContent: Pair<List<PlaceholderRange>, List<InlineContentRange>> =
80 Pair(emptyList(), emptyList())
81