1 /*
2 * Copyright 2019 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.ui.text
18
19 import androidx.compose.ui.text.font.Font
20 import androidx.compose.ui.text.font.FontFamily
21 import androidx.compose.ui.text.font.createFontFamilyResolver
22 import androidx.compose.ui.text.platform.ActualParagraphIntrinsics
23 import androidx.compose.ui.unit.Density
24
25 /** Calculates and presents the intrinsic width and height of text. */
26 interface ParagraphIntrinsics {
27 /** The width for text if all soft wrap opportunities were taken. */
28 val minIntrinsicWidth: Float
29
30 /** Returns the smallest width beyond which increasing the width never decreases the height. */
31 val maxIntrinsicWidth: Float
32
33 /**
34 * Any [Paragraph] rendered using this [ParagraphIntrinsics] will be measured and drawn using
35 * stale resolved fonts.
36 *
37 * If this is false, this [ParagraphIntrinsics] is using the most current font resolution from
38 * [FontFamily.Resolver].
39 *
40 * If this is true, recreating this [ParagraphIntrinsics] will use new fonts from
41 * [FontFamily.Resolver] for both display and measurement. Recreating this [ParagraphIntrinsics]
42 * and displaying the resulting [Paragraph] causes user-visible reflow of the displayed text.
43 *
44 * Once true, this will never become false without recreating this [ParagraphIntrinsics].
45 *
46 * It is discouraged, but safe, to continue to use this object after this becomes true. The only
47 * impact of using this object after [hasStaleResolvedFonts] becomes true is stale resolutions
48 * of async fonts for measurement and display.
49 */
50 val hasStaleResolvedFonts: Boolean
51 get() = false
52 }
53
54 /**
55 * Factory method to create a [ParagraphIntrinsics].
56 *
57 * If the [style] does not contain any [androidx.compose.ui.text.style.TextDirection],
58 * [androidx.compose.ui.text.style.TextDirection.Content] is used as the default value.
59 *
60 * @see ParagraphIntrinsics
61 */
62 @Suppress("DEPRECATION")
63 @Deprecated(
64 "Font.ResourceLoader is deprecated, instead use FontFamily.Resolver",
65 ReplaceWith(
66 "ParagraphIntrinsics(text, style, spanStyles, placeholders, density, " +
67 "fontFamilyResolver"
68 )
69 )
ParagraphIntrinsicsnull70 fun ParagraphIntrinsics(
71 text: String,
72 style: TextStyle,
73 spanStyles: List<AnnotatedString.Range<SpanStyle>> = listOf(),
74 placeholders: List<AnnotatedString.Range<Placeholder>> = listOf(),
75 density: Density,
76 resourceLoader: Font.ResourceLoader
77 ): ParagraphIntrinsics =
78 ActualParagraphIntrinsics(
79 text = text,
80 style = style,
81 annotations = spanStyles,
82 placeholders = placeholders,
83 density = density,
84 fontFamilyResolver = createFontFamilyResolver(resourceLoader)
85 )
86
87 @Deprecated(
88 "Use an overload that takes `annotations` instead",
89 ReplaceWith(
90 "ParagraphIntrinsics(text, style, spanStyles, density, fontFamilyResolver, placeholders)"
91 )
92 )
93 fun ParagraphIntrinsics(
94 text: String,
95 style: TextStyle,
96 spanStyles: List<AnnotatedString.Range<SpanStyle>> = listOf(),
97 placeholders: List<AnnotatedString.Range<Placeholder>> = listOf(),
98 density: Density,
99 fontFamilyResolver: FontFamily.Resolver
100 ): ParagraphIntrinsics =
101 ActualParagraphIntrinsics(
102 text = text,
103 style = style,
104 annotations = spanStyles,
105 placeholders = placeholders,
106 density = density,
107 fontFamilyResolver = fontFamilyResolver
108 )
109
110 /**
111 * Factory method to create a [ParagraphIntrinsics].
112 *
113 * If the [style] does not contain any [androidx.compose.ui.text.style.TextDirection],
114 * [androidx.compose.ui.text.style.TextDirection.Content] is used as the default value.
115 *
116 * @see ParagraphIntrinsics
117 */
118 fun ParagraphIntrinsics(
119 text: String,
120 style: TextStyle,
121 annotations: List<AnnotatedString.Range<out AnnotatedString.Annotation>>,
122 density: Density,
123 fontFamilyResolver: FontFamily.Resolver,
124 placeholders: List<AnnotatedString.Range<Placeholder>> = listOf()
125 ): ParagraphIntrinsics =
126 ActualParagraphIntrinsics(
127 text = text,
128 style = style,
129 annotations = annotations,
130 placeholders = placeholders,
131 density = density,
132 fontFamilyResolver = fontFamilyResolver
133 )
134