1 /*
2  * Copyright (C) 2024 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 "ffi/IcuBridge.h"
18 
19 #include <unicode/uchar.h>
20 #include <unicode/uscript.h>
21 
22 namespace minikin {
23 
24 namespace rust {
25 // The following U_JT_ constants must be same to the ones defined in
26 // frameworks/minikin/rust/hyphenator.rs
27 // TODO: Remove this file once ICU4X API becomes available in Rust.
28 const uint8_t RUST_U_JT_NON_JOINING = 0;
29 const uint8_t RUST_U_JT_DUAL_JOINING = 1;
30 const uint8_t RUST_U_JT_RIGHT_JOINING = 2;
31 const uint8_t RUST_U_JT_LEFT_JOINING = 3;
32 const uint8_t RUST_U_JT_JOIN_CAUSING = 4;
33 const uint8_t RUST_U_JT_TRANSPARENT = 5;
34 
35 // The following USCRIPT_ constants must be same to the ones defined in
36 // frameworks/minikin/rust/hyphenator.rs
37 // TODO: Remove this file once ICU4X API becomes available in Rust.
38 const uint8_t RUST_USCRIPT_LATIN = 0;
39 const uint8_t RUST_USCRIPT_ARABIC = 1;
40 const uint8_t RUST_USCRIPT_KANNADA = 2;
41 const uint8_t RUST_USCRIPT_MALAYALAM = 3;
42 const uint8_t RUST_USCRIPT_TAMIL = 4;
43 const uint8_t RUST_USCRIPT_TELUGU = 5;
44 const uint8_t RUST_USCRIPT_ARMENIAN = 6;
45 const uint8_t RUST_USCRIPT_CANADIAN_ABORIGINAL = 7;
46 const uint8_t RUST_USCRIPT_INVALID_CODE = 8;
47 
getScript(uint32_t codePoint)48 uint8_t getScript(uint32_t codePoint) {
49     UErrorCode errorCode = U_ZERO_ERROR;
50     const UScriptCode script = uscript_getScript(static_cast<UChar32>(codePoint), &errorCode);
51     if (U_FAILURE(errorCode)) {
52         return RUST_USCRIPT_INVALID_CODE;
53     }
54     switch (script) {
55         case USCRIPT_LATIN:
56             return RUST_USCRIPT_LATIN;
57         case USCRIPT_ARABIC:
58             return RUST_USCRIPT_ARABIC;
59         case USCRIPT_KANNADA:
60             return RUST_USCRIPT_KANNADA;
61         case USCRIPT_MALAYALAM:
62             return RUST_USCRIPT_MALAYALAM;
63         case USCRIPT_TAMIL:
64             return RUST_USCRIPT_TAMIL;
65         case USCRIPT_TELUGU:
66             return RUST_USCRIPT_TELUGU;
67         case USCRIPT_ARMENIAN:
68             return RUST_USCRIPT_ARMENIAN;
69         case USCRIPT_CANADIAN_ABORIGINAL:
70             return RUST_USCRIPT_CANADIAN_ABORIGINAL;
71         default:
72             return RUST_USCRIPT_INVALID_CODE;
73     }
74 }
75 
getJoiningType(uint32_t codePoint)76 uint8_t getJoiningType(uint32_t codePoint) {
77     int32_t joiningType = u_getIntPropertyValue(codePoint, UCHAR_JOINING_TYPE);
78     switch (joiningType) {
79         case U_JT_NON_JOINING:
80             return RUST_U_JT_NON_JOINING;
81         case U_JT_DUAL_JOINING:
82             return RUST_U_JT_DUAL_JOINING;
83         case U_JT_RIGHT_JOINING:
84             return RUST_U_JT_RIGHT_JOINING;
85         case U_JT_LEFT_JOINING:
86             return RUST_U_JT_LEFT_JOINING;
87         case U_JT_JOIN_CAUSING:
88             return RUST_U_JT_JOIN_CAUSING;
89         case U_JT_TRANSPARENT:
90             return RUST_U_JT_TRANSPARENT;
91         default:
92             return RUST_U_JT_NON_JOINING;
93     }
94 }
95 
96 }  // namespace rust
97 }  // namespace minikin
98