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.util.lerp
22 
23 /**
24  * Define a geometric transformation on text.
25  *
26  * @param scaleX The scale of the text on the horizontal direction. The default value is 1.0f, i.e
27  *   no scaling.
28  * @param skewX The shear of the text on the horizontal direction. A pixel at (x, y), where y is the
29  *   distance above baseline, will be transformed to (x + y * skewX, y). The default value is 0.0f
30  *   i.e. no skewing.
31  */
32 @Immutable
33 class TextGeometricTransform(val scaleX: Float = 1.0f, val skewX: Float = 0f) {
34     companion object {
35         @Stable internal val None = TextGeometricTransform(1.0f, 0.0f)
36     }
37 
copynull38     fun copy(scaleX: Float = this.scaleX, skewX: Float = this.skewX): TextGeometricTransform {
39         return TextGeometricTransform(scaleX, skewX)
40     }
41 
equalsnull42     override fun equals(other: Any?): Boolean {
43         if (this === other) return true
44         if (other !is TextGeometricTransform) return false
45         if (scaleX != other.scaleX) return false
46         if (skewX != other.skewX) return false
47         return true
48     }
49 
hashCodenull50     override fun hashCode(): Int {
51         var result = scaleX.hashCode()
52         result = 31 * result + skewX.hashCode()
53         return result
54     }
55 
toStringnull56     override fun toString(): String {
57         return "TextGeometricTransform(scaleX=$scaleX, skewX=$skewX)"
58     }
59 }
60 
lerpnull61 fun lerp(
62     start: TextGeometricTransform,
63     stop: TextGeometricTransform,
64     fraction: Float
65 ): TextGeometricTransform {
66     return TextGeometricTransform(
67         lerp(start.scaleX, stop.scaleX, fraction),
68         lerp(start.skewX, stop.skewX, fraction)
69     )
70 }
71