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.compose.ui.text.input 18 19 /** Provides bidirectional offset mapping between original and transformed text. */ 20 interface OffsetMapping { 21 /** 22 * Convert offset in original text into the offset in transformed text. 23 * 24 * This function must be a monotonically non-decreasing function. In other words, if a cursor 25 * advances in the original text, the cursor in the transformed text must advance or stay there. 26 * 27 * @param offset offset in original text. 28 * @return offset in transformed text 29 * @see VisualTransformation 30 */ originalToTransformednull31 fun originalToTransformed(offset: Int): Int 32 33 /** 34 * Convert offset in transformed text into the offset in original text. 35 * 36 * This function must be a monotonically non-decreasing function. In other words, if a cursor 37 * advances in the transformed text, the cusrsor in the original text must advance or stay 38 * there. 39 * 40 * @param offset offset in transformed text 41 * @return offset in original text 42 * @see VisualTransformation 43 */ 44 fun transformedToOriginal(offset: Int): Int 45 46 companion object { 47 /** The offset map used for identity mapping. */ 48 val Identity = 49 object : OffsetMapping { 50 override fun originalToTransformed(offset: Int): Int = offset 51 52 override fun transformedToOriginal(offset: Int): Int = offset 53 } 54 } 55 } 56