1 /*
<lambda>null2  * 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 package androidx.compose.ui.text.style
17 
18 import androidx.compose.runtime.Immutable
19 import androidx.compose.runtime.Stable
20 import androidx.compose.ui.util.fastFold
21 import androidx.compose.ui.util.fastJoinToString
22 
23 /** Defines a horizontal line to be drawn on the text. */
24 @Immutable
25 class TextDecoration internal constructor(val mask: Int) {
26 
27     companion object {
28         @Stable val None: TextDecoration = TextDecoration(0x0)
29 
30         /**
31          * Draws a horizontal line below the text.
32          *
33          * @sample androidx.compose.ui.text.samples.TextDecorationUnderlineSample
34          */
35         @Stable val Underline: TextDecoration = TextDecoration(0x1)
36 
37         /**
38          * Draws a horizontal line over the text.
39          *
40          * @sample androidx.compose.ui.text.samples.TextDecorationLineThroughSample
41          */
42         @Stable val LineThrough: TextDecoration = TextDecoration(0x2)
43 
44         /**
45          * Creates a decoration that includes all the given decorations.
46          *
47          * @sample androidx.compose.ui.text.samples.TextDecorationCombinedSample
48          * @param decorations The decorations to be added
49          */
50         fun combine(decorations: List<TextDecoration>): TextDecoration {
51             val mask = decorations.fastFold(0) { acc, decoration -> acc or decoration.mask }
52             return TextDecoration(mask)
53         }
54     }
55 
56     /**
57      * Creates a decoration that includes both of the TextDecorations.
58      *
59      * @sample androidx.compose.ui.text.samples.TextDecorationCombinedSample
60      */
61     operator fun plus(decoration: TextDecoration): TextDecoration {
62         return TextDecoration(this.mask or decoration.mask)
63     }
64 
65     /**
66      * Check whether this [TextDecoration] contains the given decoration.
67      *
68      * @param other The [TextDecoration] to be checked.
69      */
70     operator fun contains(other: TextDecoration): Boolean {
71         return (mask or other.mask) == mask
72     }
73 
74     override fun toString(): String {
75         if (mask == 0) {
76             return "TextDecoration.None"
77         }
78 
79         val values: MutableList<String> = mutableListOf()
80         if ((mask and Underline.mask) != 0) {
81             values.add("Underline")
82         }
83         if ((mask and LineThrough.mask) != 0) {
84             values.add("LineThrough")
85         }
86         if ((values.size == 1)) {
87             return "TextDecoration.${values[0]}"
88         }
89         return "TextDecoration[${values.fastJoinToString(separator = ", ")}]"
90     }
91 
92     override fun equals(other: Any?): Boolean {
93         if (this === other) return true
94         if (other !is TextDecoration) return false
95         if (mask != other.mask) return false
96         return true
97     }
98 
99     override fun hashCode(): Int {
100         return mask
101     }
102 }
103