1 /*
2  * Copyright (C) 2017 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 #ifndef LIBTEXTCLASSIFIER_TEXTCLASSIFIER_JNI_H_
18 #define LIBTEXTCLASSIFIER_TEXTCLASSIFIER_JNI_H_
19 
20 #include <jni.h>
21 #include <string>
22 
23 #include "types.h"
24 
25 // When we use a macro as an argument for a macro, an additional level of
26 // indirection is needed, if the macro argument is used with # or ##.
27 #define ADD_QUOTES_HELPER(TOKEN) #TOKEN
28 #define ADD_QUOTES(TOKEN) ADD_QUOTES_HELPER(TOKEN)
29 
30 #ifndef TC_PACKAGE_NAME
31 #define TC_PACKAGE_NAME android_view_textclassifier
32 #endif
33 
34 #ifndef TC_CLASS_NAME
35 #define TC_CLASS_NAME TextClassifierImplNative
36 #endif
37 #define TC_CLASS_NAME_STR ADD_QUOTES(TC_CLASS_NAME)
38 
39 #ifndef TC_PACKAGE_PATH
40 #define TC_PACKAGE_PATH "android/view/textclassifier/"
41 #endif
42 
43 #define JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name) \
44   Java_##package_name##_##class_name##_##method_name
45 
46 #define JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, \
47                              method_name)                           \
48   JNIEXPORT return_type JNICALL JNI_METHOD_NAME_INTERNAL(           \
49       package_name, class_name, method_name)
50 
51 // The indirection is needed to correctly expand the TC_PACKAGE_NAME macro.
52 // See the explanation near ADD_QUOTES macro.
53 #define JNI_METHOD2(return_type, package_name, class_name, method_name) \
54   JNI_METHOD_PRIMITIVE(return_type, package_name, class_name, method_name)
55 
56 #define JNI_METHOD(return_type, class_name, method_name) \
57   JNI_METHOD2(return_type, TC_PACKAGE_NAME, class_name, method_name)
58 
59 #define JNI_METHOD_NAME2(package_name, class_name, method_name) \
60   JNI_METHOD_NAME_INTERNAL(package_name, class_name, method_name)
61 
62 #define JNI_METHOD_NAME(class_name, method_name) \
63   JNI_METHOD_NAME2(TC_PACKAGE_NAME, class_name, method_name)
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 // SmartSelection.
70 JNI_METHOD(jlong, TC_CLASS_NAME, nativeNew)
71 (JNIEnv* env, jobject thiz, jint fd);
72 
73 JNI_METHOD(jlong, TC_CLASS_NAME, nativeNewFromPath)
74 (JNIEnv* env, jobject thiz, jstring path);
75 
76 JNI_METHOD(jlong, TC_CLASS_NAME, nativeNewFromAssetFileDescriptor)
77 (JNIEnv* env, jobject thiz, jobject afd, jlong offset, jlong size);
78 
79 JNI_METHOD(jintArray, TC_CLASS_NAME, nativeSuggestSelection)
80 (JNIEnv* env, jobject thiz, jlong ptr, jstring context, jint selection_begin,
81  jint selection_end, jobject options);
82 
83 JNI_METHOD(jobjectArray, TC_CLASS_NAME, nativeClassifyText)
84 (JNIEnv* env, jobject thiz, jlong ptr, jstring context, jint selection_begin,
85  jint selection_end, jobject options);
86 
87 JNI_METHOD(jobjectArray, TC_CLASS_NAME, nativeAnnotate)
88 (JNIEnv* env, jobject thiz, jlong ptr, jstring context, jobject options);
89 
90 JNI_METHOD(void, TC_CLASS_NAME, nativeClose)
91 (JNIEnv* env, jobject thiz, jlong ptr);
92 
93 // DEPRECATED. Use nativeGetLocales instead.
94 JNI_METHOD(jstring, TC_CLASS_NAME, nativeGetLanguage)
95 (JNIEnv* env, jobject clazz, jint fd);
96 
97 JNI_METHOD(jstring, TC_CLASS_NAME, nativeGetLocales)
98 (JNIEnv* env, jobject clazz, jint fd);
99 
100 JNI_METHOD(jstring, TC_CLASS_NAME, nativeGetLocalesFromAssetFileDescriptor)
101 (JNIEnv* env, jobject thiz, jobject afd, jlong offset, jlong size);
102 
103 JNI_METHOD(jint, TC_CLASS_NAME, nativeGetVersion)
104 (JNIEnv* env, jobject clazz, jint fd);
105 
106 JNI_METHOD(jint, TC_CLASS_NAME, nativeGetVersionFromAssetFileDescriptor)
107 (JNIEnv* env, jobject thiz, jobject afd, jlong offset, jlong size);
108 
109 JNI_METHOD(jstring, TC_CLASS_NAME, nativeGetName)
110 (JNIEnv* env, jobject clazz, jint fd);
111 
112 JNI_METHOD(jstring, TC_CLASS_NAME, nativeGetNameFromAssetFileDescriptor)
113 (JNIEnv* env, jobject thiz, jobject afd, jlong offset, jlong size);
114 
115 #ifdef __cplusplus
116 }
117 #endif
118 
119 namespace libtextclassifier2 {
120 
121 // Given a utf8 string and a span expressed in Java BMP (basic multilingual
122 // plane) codepoints, converts it to a span expressed in utf8 codepoints.
123 libtextclassifier2::CodepointSpan ConvertIndicesBMPToUTF8(
124     const std::string& utf8_str, libtextclassifier2::CodepointSpan bmp_indices);
125 
126 // Given a utf8 string and a span expressed in utf8 codepoints, converts it to a
127 // span expressed in Java BMP (basic multilingual plane) codepoints.
128 libtextclassifier2::CodepointSpan ConvertIndicesUTF8ToBMP(
129     const std::string& utf8_str,
130     libtextclassifier2::CodepointSpan utf8_indices);
131 
132 }  // namespace libtextclassifier2
133 
134 #endif  // LIBTEXTCLASSIFIER_TEXTCLASSIFIER_JNI_H_
135