1 /*
2  * Copyright 2021 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.glance.text
18 
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.ui.unit.TextUnit
21 import androidx.glance.unit.ColorProvider
22 
23 /**
24  * Description of a text style for the [androidx.glance.text.Text] composable.
25  *
26  * @param color optionally specifies the color to use for the text, defaults to
27  *   [TextDefaults.defaultTextColor].
28  * @param fontSize optionally specifies the size to use for the text, defaults to system when null.
29  * @param fontWeight optionally specifies the weight to use for the text, defaults to system when
30  *   null.
31  * @param fontStyle optionally specifies style (such as italics) to use for the text, defaults to
32  *   system when null.
33  * @param textAlign optionally specifies the alignment to use for the text, defaults to start when.
34  *   null.
35  * @param textDecoration optionally specifies decorations (e.g. underline) to use for the text,
36  *   defaults to none when null
37  * @param fontFamily optionally specifies which font family to use for the text, defaults to system
38  *   when null.
39  */
40 @Immutable
41 class TextStyle(
42     val color: ColorProvider = TextDefaults.defaultTextColor,
43     val fontSize: TextUnit? = null,
44     val fontWeight: FontWeight? = null,
45     val fontStyle: FontStyle? = null,
46     val textAlign: TextAlign? = null,
47     val textDecoration: TextDecoration? = null,
48     val fontFamily: FontFamily? = null,
49 ) {
copynull50     fun copy(
51         color: ColorProvider = this.color,
52         fontSize: TextUnit? = this.fontSize,
53         fontWeight: FontWeight? = this.fontWeight,
54         fontStyle: FontStyle? = this.fontStyle,
55         textAlign: TextAlign? = this.textAlign,
56         textDecoration: TextDecoration? = this.textDecoration,
57         fontFamily: FontFamily? = this.fontFamily,
58     ) =
59         TextStyle(
60             color = color,
61             fontSize = fontSize,
62             fontWeight = fontWeight,
63             fontStyle = fontStyle,
64             textAlign = textAlign,
65             textDecoration = textDecoration,
66             fontFamily = fontFamily,
67         )
68 
69     override fun equals(other: Any?): Boolean {
70         if (this === other) return true
71         if (other !is TextStyle) return false
72         if (color != other.color) return false
73         if (fontSize != other.fontSize) return false
74         if (fontWeight != other.fontWeight) return false
75         if (fontStyle != other.fontStyle) return false
76         if (textDecoration != other.textDecoration) return false
77         if (textAlign != other.textAlign) return false
78         if (fontFamily != other.fontFamily) return false
79         return true
80     }
81 
hashCodenull82     override fun hashCode(): Int {
83         var result = color.hashCode()
84         result = 31 * result + fontSize.hashCode()
85         result = 31 * result + fontWeight.hashCode()
86         result = 31 * result + fontStyle.hashCode()
87         result = 31 * result + textDecoration.hashCode()
88         result = 31 * result + textAlign.hashCode()
89         result = 31 * result + fontFamily.hashCode()
90         return result
91     }
92 
toStringnull93     override fun toString() =
94         "TextStyle(color=$color, fontSize=$fontSize, fontWeight=$fontWeight, " +
95             "fontStyle=$fontStyle, textDecoration=$textDecoration, textAlign=$textAlign, " +
96             "fontFamily=$fontFamily)"
97 }
98