summaryrefslogtreecommitdiff
path: root/graphics/java/android
diff options
context:
space:
mode:
author Haoyu Zhang <haoyuchang@google.com> 2023-02-22 11:20:09 -0800
committer Haoyu Zhang <haoyuchang@google.com> 2023-03-04 05:55:34 -0800
commit043d23a8a8e8fe214df0d10c4341c4ad55e7b2aa (patch)
tree40b3c458c29cadeed4852729a0f20a4790f6df1f /graphics/java/android
parentbaf281de2be4e8118edf3766d39cb654860e41f1 (diff)
Optimize GraphemeClusterSegmentFinder performance
This CL utilized minikin:isGraphemeBreak to compute all grapheme break indices when creating GraphemeClusterSegmentFinder. This makes all methods (such as #nextStartBoundary) of GraphemeClusterSegmentFinder O(1) instead of O(n). Bug: 271004887 Test: atest TextViewHandwritingGestureTest Change-Id: I61c1f2c45123a584456b4860b72cd4fdc84c5f1c
Diffstat (limited to 'graphics/java/android')
-rw-r--r--graphics/java/android/graphics/text/GraphemeBreak.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/graphics/java/android/graphics/text/GraphemeBreak.java b/graphics/java/android/graphics/text/GraphemeBreak.java
new file mode 100644
index 000000000000..f82b2fd659cc
--- /dev/null
+++ b/graphics/java/android/graphics/text/GraphemeBreak.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics.text;
+
+/** @hide */
+public class GraphemeBreak {
+ private GraphemeBreak() { }
+
+ /**
+ * Util method that checks if the offsets in given range are grapheme break.
+ *
+ * @param advances the advances of characters in the given text. It contains the font
+ * information used by the algorithm to determine the grapheme break. It's useful
+ * when some character is missing in the font. For example, if the smile emoji
+ * "0xD83D 0xDE0A" is not found in the font and is displayed as 2 characters.
+ * We can't treat it as a single grapheme cluster.
+ * @param text the text to be processed.
+ * @param start the start offset of the queried range, inclusive.
+ * @param end the end offset of the queried range, exclusive.
+ * @param isGraphemeBreak the array to receive the result. The i-th element of the
+ * array will be set to true if the offset (start + i) is a grapheme
+ * break; otherwise, it will be set to false.
+ */
+ public static void isGraphemeBreak(float[] advances, char[] text, int start, int end,
+ boolean[] isGraphemeBreak) {
+ if (start > end) {
+ throw new IllegalArgumentException("start is greater than end: start = " + start
+ + " end = " + end);
+ }
+ if (advances.length < end) {
+ throw new IllegalArgumentException("the length of advances is less than end"
+ + "advances.length = " + advances.length
+ + " end = " + end);
+ }
+ if (isGraphemeBreak.length < end - start) {
+ throw new IndexOutOfBoundsException("isGraphemeBreak doesn't have enough space to "
+ + "receive the result, isGraphemeBreak.length: " + isGraphemeBreak.length
+ + " needed space: " + (end - start));
+ }
+ nIsGraphemeBreak(advances, text, start, end, isGraphemeBreak);
+ }
+
+ private static native void nIsGraphemeBreak(float[] advances, char[] text, int start, int end,
+ boolean[] isGraphemeBreak);
+}