xref: /aosp_15_r20/frameworks/minikin/libs/minikin/ScriptUtils.cpp (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
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 "ScriptUtils.h"
18 
19 #include <unicode/ubidi.h>
20 #include <unicode/uscript.h>
21 #include <unicode/utf16.h>
22 #include <unicode/utypes.h>
23 
24 #include <algorithm>
25 
26 #include "MinikinInternal.h"
27 #include "minikin/Emoji.h"
28 
29 namespace minikin {
30 
decodeUtf16(U16StringPiece text,Range range,uint32_t pos)31 static hb_codepoint_t decodeUtf16(U16StringPiece text, Range range, uint32_t pos) {
32     uint32_t result;
33     U16_NEXT(text.data(), pos, range.getEnd(), result);
34     if (U_IS_SURROGATE(result)) {  // isolated surrogate
35         result = CHAR_REPLACEMENT_CHARACTER;
36     }
37     return static_cast<hb_codepoint_t>(result);
38 }
39 
getICUScript(uint32_t cp)40 static UScriptCode getICUScript(uint32_t cp) {
41     UErrorCode status = U_ZERO_ERROR;
42     UScriptCode scriptCode = uscript_getScript(cp, &status);
43     if (U_FAILURE(status)) [[unlikely]] {
44         return USCRIPT_INVALID_CODE;
45     }
46     return scriptCode;
47 }
48 
getHbScript(uint32_t cp)49 static hb_script_t getHbScript(uint32_t cp) {
50     hb_unicode_funcs_t* unicode_func = hb_unicode_funcs_get_default();
51     return hb_unicode_script(unicode_func, cp);
52 }
53 
54 // static
getScriptRun(U16StringPiece text,Range range,uint32_t pos)55 std::pair<uint32_t, hb_script_t> ScriptText::getScriptRun(U16StringPiece text, Range range,
56                                                           uint32_t pos) {
57     if (!range.contains(pos)) {
58         return std::make_pair(range.getEnd(), HB_SCRIPT_UNKNOWN);
59     }
60 
61     uint32_t cp = decodeUtf16(text, range, pos);
62     UScriptCode current_script = getICUScript(cp);
63     hb_script_t current_hb_script = getHbScript(cp);
64     uint32_t i;
65     for (i = pos + U16_LENGTH(cp); i < range.getEnd(); i += U16_LENGTH(cp)) {
66         cp = decodeUtf16(text, range, i);
67         UScriptCode next_script = getICUScript(cp);
68         if (current_script != next_script) {
69             if (current_script == USCRIPT_INHERITED || current_script == USCRIPT_COMMON) {
70                 current_script = next_script;
71                 current_hb_script = getHbScript(cp);
72             } else if (next_script == USCRIPT_INHERITED || next_script == USCRIPT_COMMON) {
73                 continue;
74             } else {
75                 break;
76             }
77         }
78     }
79     if (current_script == USCRIPT_INHERITED) {
80         return std::make_pair(i, HB_SCRIPT_COMMON);
81     } else {
82         return std::make_pair(i, current_hb_script);
83     }
84 }
85 
86 }  // namespace minikin
87