diff --git a/app/src/main/java/helium314/keyboard/latin/common/ComposedData.java b/app/src/main/java/helium314/keyboard/latin/common/ComposedData.java deleted file mode 100644 index 7e42f380a..000000000 --- a/app/src/main/java/helium314/keyboard/latin/common/ComposedData.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * modified - * SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only - */ - -package helium314.keyboard.latin.common; - -import androidx.annotation.NonNull; - -/** - * An immutable class that encapsulates a snapshot of word composition data. - */ -public class ComposedData { - @NonNull - public final InputPointers mInputPointers; - public final boolean mIsBatchMode; - @NonNull - public final String mTypedWord; - - public ComposedData(@NonNull final InputPointers inputPointers, final boolean isBatchMode, - @NonNull final String typedWord) { - mInputPointers = inputPointers; - mIsBatchMode = isBatchMode; - mTypedWord = typedWord; - } - - /** - * Copy the code points in the typed word to a destination array of ints. - * - * If the array is too small to hold the code points in the typed word, nothing is copied and - * -1 is returned. - * - * @param destination the array of ints. - * @return the number of copied code points. - */ - public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( - @NonNull final int[] destination) { - // lastIndex is exclusive - final int lastIndex = mTypedWord.length() - - StringUtils.getTrailingSingleQuotesCount(mTypedWord); - if (lastIndex <= 0) { - // The string is empty or contains only single quotes. - return 0; - } - - // The following function counts the number of code points in the text range which begins - // at index 0 and extends to the character at lastIndex. - final int codePointSize = Character.codePointCount(mTypedWord, 0, lastIndex); - if (codePointSize > destination.length) { - return -1; - } - return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWord, 0, - lastIndex, true /* downCase */); - } -} diff --git a/app/src/main/java/helium314/keyboard/latin/common/ComposedData.kt b/app/src/main/java/helium314/keyboard/latin/common/ComposedData.kt new file mode 100644 index 000000000..159d1b1c7 --- /dev/null +++ b/app/src/main/java/helium314/keyboard/latin/common/ComposedData.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * modified + * SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only + */ +package helium314.keyboard.latin.common + +import helium314.keyboard.latin.WordComposer + +/** An immutable class that encapsulates a snapshot of word composition data. */ +class ComposedData( + @JvmField val mInputPointers: InputPointers, + @JvmField val mIsBatchMode: Boolean, + @JvmField val mTypedWord: String +) { + /** + * Copy the code points in the typed word to a destination array of ints. + * + * If the array is too small to hold the code points in the typed word, nothing is copied and + * -1 is returned. + * + * @param destination the array of ints. + * @return the number of copied code points. + */ + fun copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( + destination: IntArray + ): Int { + // lastIndex is exclusive + val lastIndex = (mTypedWord.length - StringUtils.getTrailingSingleQuotesCount(mTypedWord)) + if (lastIndex <= 0) { + return 0 // The string is empty or contains only single quotes. + } + + // The following function counts the number of code points in the text range which begins + // at index 0 and extends to the character at lastIndex. + val codePointSize = Character.codePointCount(mTypedWord, 0, lastIndex) + if (codePointSize > destination.size) { + return -1 + } + return StringUtils.copyCodePointsAndReturnCodePointCount( + destination, mTypedWord, 0, lastIndex, true + ) + } + + companion object { + fun createForWord(word: String): ComposedData { + val codePoints = StringUtils.toCodePointArray(word) + val coordinates = CoordinateUtils.newCoordinateArray(codePoints.size) + for (i in codePoints.indices) { + CoordinateUtils.setXYInArray(coordinates, i, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE) + } + return WordComposer().apply { setComposingWord(codePoints, coordinates) }.composedDataSnapshot + } + } +}