• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.text.method;
18 
19 import android.annotation.IntDef;
20 
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 
24 /**
25  * The interface for the index mapping information of a transformed text returned by
26  * {@link TransformationMethod}. This class is mainly used to support the
27  * {@link TransformationMethod} that alters the text length.
28  * @hide
29  */
30 @android.ravenwood.annotation.RavenwoodKeepWholeClass
31 public interface OffsetMapping {
32     /**
33      * The mapping strategy for a character offset.
34      *
35      * @see #originalToTransformed(int, int)
36      * @see #transformedToOriginal(int, int)
37      */
38     int MAP_STRATEGY_CHARACTER = 0;
39 
40     /**
41      * The mapping strategy for a cursor position.
42      *
43      * @see #originalToTransformed(int, int)
44      * @see #transformedToOriginal(int, int)
45      */
46     int MAP_STRATEGY_CURSOR = 1;
47 
48     @IntDef(prefix = { "MAP_STRATEGY" }, value = {
49             MAP_STRATEGY_CHARACTER,
50             MAP_STRATEGY_CURSOR
51     })
52     @Retention(RetentionPolicy.SOURCE)
53     @interface MapStrategy {}
54 
55     /**
56      * Map an offset at original text to the offset at transformed text. <br/>
57      *
58      * This function must be a monotonically non-decreasing function. In other words, if the offset
59      * advances at the original text, the offset at the transformed text must advance or stay there.
60      * <br/>
61      *
62      * Depending on the mapping strategy, a same offset can be mapped differently. For example,
63      * <pre>
64      * Original:       ABCDE
65      * Transformed:    ABCXDE ('X' is introduced due to the transformation.)
66      * </pre>
67      * Let's check the offset 3, which is the offset of the character 'D'.
68      * If we want to map the character offset 3, it should be mapped to index 4.
69      * If we want to map the cursor offset 3 (the offset of the character before which the cursor is
70      * placed), it's unclear if the mapped cursor is before 'X' or after 'X'.
71      * This depends on how the transformed text reacts an insertion at offset 3 in the original
72      * text. Assume character 'V' is insert at offset 3 in the original text, and the original text
73      * become "ABCVDE". The transformed text can be:
74      * <pre>
75      * 1) "ABCVXDE"
76      * or
77      * 2) "ABCXVDE"
78      * </pre>
79      * In the first case, the offset 3 should be mapped to 3 (before 'X'). And in the second case,
80      * the offset should be mapped to 4 (after 'X').<br/>
81      *
82      * In some cases, a character offset at the original text doesn't have a proper corresponding
83      * offset at the transformed text. For example:
84      * <pre>
85      * Original:    ABCDE
86      * Transformed: ABDE ('C' is deleted due to the transformation.)
87      * </pre>
88      * The character offset 2 can be mapped either to offset 2 or 3, but neither is meaningful. For
89      * convenience, it MUST map to the next offset (offset 3 in this case), or the
90      * transformedText.length() if there is no valid character to map.
91      * This is mandatory when the map strategy is {@link #MAP_STRATEGY_CHARACTER}, but doesn't
92      * apply for other map strategies.
93      *
94      * @param offset the offset at the original text. It's normally equal to or less than the
95      *               originalText.length(). When {@link #MAP_STRATEGY_CHARACTER} is passed, it must
96      *               be less than originalText.length(). For convenience, it's also allowed to be
97      *               negative, which represents an invalid offset. When the given offset is
98      *               negative, this method should return it as it is.
99      * @param strategy the mapping strategy. Depending on its value, the same offset can be mapped
100      *                 differently.
101      * @return the mapped offset at the transformed text, must be equal to or less than the
102      * transformedText.length().
103      *
104      * @see #transformedToOriginal(int, int)
105      */
originalToTransformed(int offset, @MapStrategy int strategy)106     int originalToTransformed(int offset, @MapStrategy int strategy);
107 
108     /**
109      * Map an offset at transformed text to the offset at original text. This is the reverse method
110      * of {@link #originalToTransformed(int, int)}. <br/>
111      * This function must be a monotonically non-decreasing function. In other words, if the offset
112      * advances at the original text, the offset at the transformed text must advance or stay there.
113      * <br/>
114      * Similar to the {@link #originalToTransformed(int, int)} if a character offset doesn't have a
115      * corresponding offset at the transformed text, it MUST return the value as the previous
116      * offset. This is mandatory when the map strategy is {@link #MAP_STRATEGY_CHARACTER},
117      * but doesn't apply for other map strategies.
118      *
119      * @param offset the offset at the transformed text. It's normally equal to or less than the
120      *               transformedText.length(). When {@link #MAP_STRATEGY_CHARACTER} is passed, it
121      *               must be less than transformedText.length(). For convenience, it's also allowed
122      *               to be negative, which represents an invalid offset. When the given offset is
123      *               negative, this method should return it as it is.
124      * @param strategy the mapping strategy. Depending on its value, the same offset can be mapped
125      *                 differently.
126      * @return the mapped offset at the original text, must be equal to or less than the
127      * original.length().
128      *
129      * @see #originalToTransformed(int, int)
130      */
transformedToOriginal(int offset, @MapStrategy int strategy)131     int transformedToOriginal(int offset, @MapStrategy int strategy);
132 
133     /**
134      * Map a text update in the original text to an update the transformed text.
135      * This method used to determine how the transformed text is updated in response to an
136      * update in the original text. It is always called before the original text being changed.
137      *
138      * The main usage of this method is to update text layout incrementally. So it should report
139      * the range where text needs to be laid out again.
140      *
141      * @param textUpdate the {@link TextUpdate} object containing the text  update information on
142      *                  the original text. The transformed text update information will also be
143      *                   stored at this object.
144      */
originalToTransformed(TextUpdate textUpdate)145     void originalToTransformed(TextUpdate textUpdate);
146 
147     /**
148      * The class that stores the text update information that from index <code>where</code>,
149      * <code>after</code> characters will replace the old text that has length <code>before</code>.
150      */
151     class TextUpdate {
152         /** The start index of the text update range, inclusive */
153         public int where;
154         /** The length of the replaced old text. */
155         public int before;
156         /** The length of the new text that replaces the old text. */
157         public int after;
158 
159         /**
160          * Creates a {@link TextUpdate} object.
161          * @param where the start index of the text update range.
162          * @param before the length of the replaced old text.
163          * @param after the length of the new text that replaces the old text.
164          */
TextUpdate(int where, int before, int after)165         public TextUpdate(int where, int before, int after) {
166             this.where = where;
167             this.before = before;
168             this.after = after;
169         }
170     }
171 }
172