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