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.style
18
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.runtime.Stable
21 import androidx.compose.ui.text.lerpTextUnitInheritable
22 import androidx.compose.ui.unit.TextUnit
23 import androidx.compose.ui.unit.sp
24
25 /**
26 * Specify the indentation of a paragraph.
27 *
28 * @param firstLine the amount of indentation applied to the first line.
29 * @param restLine the amount of indentation applied to every line except the first line.
30 */
31 @Immutable
32 class TextIndent(val firstLine: TextUnit = 0.sp, val restLine: TextUnit = 0.sp) {
33 companion object {
34 /** Constant fot no text indent. */
35 @Stable val None = TextIndent()
36 }
37
copynull38 fun copy(firstLine: TextUnit = this.firstLine, restLine: TextUnit = this.restLine): TextIndent {
39 return TextIndent(firstLine, restLine)
40 }
41
equalsnull42 override fun equals(other: Any?): Boolean {
43 if (this === other) return true
44 if (other !is TextIndent) return false
45 if (firstLine != other.firstLine) return false
46 if (restLine != other.restLine) return false
47 return true
48 }
49
hashCodenull50 override fun hashCode(): Int {
51 var result = firstLine.hashCode()
52 result = 31 * result + restLine.hashCode()
53 return result
54 }
55
toStringnull56 override fun toString(): String {
57 return "TextIndent(firstLine=$firstLine, restLine=$restLine)"
58 }
59 }
60
61 /**
62 * Linearly interpolate between two [TextIndent]s.
63 *
64 * The [fraction] argument represents position on the timeline, with 0.0 meaning that the
65 * interpolation has not started, returning [start] (or something equivalent to [start]), 1.0
66 * meaning that the interpolation has finished, returning [stop] (or something equivalent to
67 * [stop]), and values in between meaning that the interpolation is at the relevant point on the
68 * timeline between [start] and [stop]. The interpolation can be extrapolated beyond 0.0 and 1.0, so
69 * negative values and values greater than 1.0 are valid.
70 */
lerpnull71 fun lerp(start: TextIndent, stop: TextIndent, fraction: Float): TextIndent {
72 return TextIndent(
73 lerpTextUnitInheritable(start.firstLine, stop.firstLine, fraction),
74 lerpTextUnitInheritable(start.restLine, stop.restLine, fraction)
75 )
76 }
77