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