1 /*
2  * Copyright 2024 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.foundation.text.input.internal
18 
19 import androidx.compose.foundation.text.appendCodePointX
20 import androidx.compose.foundation.text.input.TextFieldCharSequence
21 import androidx.compose.runtime.Stable
22 
23 /**
24  * Visual transformation interface for input fields.
25  *
26  * This interface is responsible for 1-to-1 mapping of every codepoint in input state to another
27  * codepoint before text is rendered. Visual transformation is useful when the underlying source of
28  * input needs to remain but rendered content should look different, e.g. password obscuring.
29  */
30 @Stable
31 internal fun interface CodepointTransformation {
32 
33     /**
34      * Transforms a single [codepoint] located at [codepointIndex] to another codepoint.
35      *
36      * A codepoint is an integer that always maps to a single character. Every codepoint in Unicode
37      * is comprised of 16 bits, 2 bytes.
38      */
39     // TODO: add more codepoint explanation or doc referral
transformnull40     fun transform(codepointIndex: Int, codepoint: Int): Int
41 
42     companion object
43 }
44 
45 /**
46  * Creates a masking [CodepointTransformation] that maps all codepoints to a specific [character].
47  */
48 @Stable
49 internal fun CodepointTransformation.Companion.mask(character: Char): CodepointTransformation =
50     MaskCodepointTransformation(character)
51 
52 private data class MaskCodepointTransformation(val character: Char) : CodepointTransformation {
53     override fun transform(codepointIndex: Int, codepoint: Int): Int {
54         return character.code
55     }
56 }
57 
58 /**
59  * [CodepointTransformation] that converts all line breaks (\n) into white space(U+0020) and
60  * carriage returns(\r) to zero-width no-break space (U+FEFF). This transformation forces any
61  * content to appear as single line.
62  */
63 internal object SingleLineCodepointTransformation : CodepointTransformation {
64 
65     private const val LINE_FEED = '\n'.code
66     private const val CARRIAGE_RETURN = '\r'.code
67 
68     private const val WHITESPACE = ' '.code
69     private const val ZERO_WIDTH_SPACE = '\uFEFF'.code
70 
transformnull71     override fun transform(codepointIndex: Int, codepoint: Int): Int {
72         if (codepoint == LINE_FEED) return WHITESPACE
73         if (codepoint == CARRIAGE_RETURN) return ZERO_WIDTH_SPACE
74         return codepoint
75     }
76 
toStringnull77     override fun toString(): String {
78         return "SingleLineCodepointTransformation"
79     }
80 }
81 
toVisualTextnull82 internal fun TextFieldCharSequence.toVisualText(
83     codepointTransformation: CodepointTransformation,
84     offsetMappingCalculator: OffsetMappingCalculator
85 ): CharSequence {
86     val text = this
87     var changed = false
88     val newText = buildString {
89         var charOffset = 0
90         var codePointOffset = 0
91         while (charOffset < text.length) {
92             val codePoint = text.codePointAt(charOffset)
93             val newCodePoint = codepointTransformation.transform(codePointOffset, codePoint)
94             val charCount = charCount(codePoint)
95             if (newCodePoint != codePoint) {
96                 changed = true
97                 val newCharCount = charCount(newCodePoint)
98                 offsetMappingCalculator.recordEditOperation(
99                     sourceStart = length,
100                     sourceEnd = length + charCount,
101                     newLength = newCharCount
102                 )
103             }
104             appendCodePointX(newCodePoint)
105 
106             charOffset += charCount
107             codePointOffset += 1
108         }
109     }
110 
111     // Return the same instance if nothing changed, which signals to the caller that nothing changed
112     // and allows the new string to be GC'd earlier.
113     return if (changed) newText else this
114 }
115