1 /*
2  * Copyright (C) 2023 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 #undef LOG_TAG
18 #define LOG_TAG "GraphemeBreaker"
19 
20 #include <minikin/GraphemeBreak.h>
21 #include <nativehelper/ScopedPrimitiveArray.h>
22 
23 #include "GraphicsJNI.h"
24 
25 namespace android {
26 
nIsGraphemeBreak(JNIEnv * env,jclass,jfloatArray advances,jcharArray text,jint start,jint end,jbooleanArray isGraphemeBreak)27 static void nIsGraphemeBreak(JNIEnv* env, jclass, jfloatArray advances, jcharArray text, jint start,
28                              jint end, jbooleanArray isGraphemeBreak) {
29     if (start > end || env->GetArrayLength(advances) < end ||
30         env->GetArrayLength(isGraphemeBreak) < end - start) {
31         doThrowAIOOBE(env);
32     }
33 
34     if (start == end) {
35         return;
36     }
37 
38     ScopedFloatArrayRO advancesArray(env, advances);
39     ScopedCharArrayRO textArray(env, text);
40     ScopedBooleanArrayRW isGraphemeBreakArray(env, isGraphemeBreak);
41 
42     size_t count = end - start;
43     for (size_t offset = 0; offset < count; ++offset) {
44         bool isBreak = minikin::GraphemeBreak::isGraphemeBreak(advancesArray.get(), textArray.get(),
45                                                                start, end, start + offset);
46         isGraphemeBreakArray[offset] = isBreak ? JNI_TRUE : JNI_FALSE;
47     }
48 }
49 
50 static const JNINativeMethod gMethods[] = {
51         {"nIsGraphemeBreak",
52          "("
53          "[F"  // advances
54          "[C"  // text
55          "I"   // start
56          "I"   // end
57          "[Z"  // isGraphemeBreak
58          ")V",
59          (void*)nIsGraphemeBreak},
60 };
61 
register_android_graphics_text_GraphemeBreak(JNIEnv * env)62 int register_android_graphics_text_GraphemeBreak(JNIEnv* env) {
63     return RegisterMethodsOrDie(env, "android/graphics/text/GraphemeBreak", gMethods,
64                                 NELEM(gMethods));
65 }
66 
67 }  // namespace android
68