• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
18 
19 #include "ScriptUtils.h"
20 #include "UnicodeUtils.h"
21 
22 namespace minikin {
23 namespace {
24 
25 struct Result {
Resultminikin::__anon433396a90111::Result26     Result(int start, int end, hb_script_t script) : start(start), end(end), script(script) {}
27     int start;
28     int end;
29     hb_script_t script;
30 };
31 
operator ==(const Result & l,const Result & r)32 bool operator==(const Result& l, const Result& r) {
33     return l.start == r.start && l.end == r.end && l.script == r.script;
34 }
35 
operator <<(std::ostream & os,const Result & r)36 std::ostream& operator<<(std::ostream& os, const Result& r) {
37     char buf[5] = {};
38     buf[0] = static_cast<char>((r.script >> 24) & 0xFF);
39     buf[1] = static_cast<char>((r.script >> 16) & 0xFF);
40     buf[2] = static_cast<char>((r.script >> 8) & 0xFF);
41     buf[3] = static_cast<char>((r.script) & 0xFF);
42     return os << "(" << r.start << "," << r.end << "): " << buf;
43 }
44 
splitByScript(const std::vector<uint16_t> & text,uint32_t start,uint32_t end)45 std::vector<Result> splitByScript(const std::vector<uint16_t>& text, uint32_t start, uint32_t end) {
46     std::vector<Result> result;
47     for (const auto [range, script] : ScriptText(text, start, end)) {
48         result.emplace_back(range.getStart(), range.getEnd(), script);
49     }
50     return result;
51 }
52 
splitByScript(const std::string & text,uint32_t start,uint32_t end)53 std::vector<Result> splitByScript(const std::string& text, uint32_t start, uint32_t end) {
54     std::vector<uint16_t> utf16 = utf8ToUtf16(text);
55     return splitByScript(utf16, start, end);
56 }
57 
TEST(ScriptUtilsTest,Latin)58 TEST(ScriptUtilsTest, Latin) {
59     auto result = splitByScript("abcde", 0, 5);
60     ASSERT_EQ(1u, result.size());
61     EXPECT_EQ(Result(0, 5, HB_SCRIPT_LATIN), result[0]);
62 
63     result = splitByScript("abcde", 0, 3);
64     ASSERT_EQ(1u, result.size());
65     EXPECT_EQ(Result(0, 3, HB_SCRIPT_LATIN), result[0]);
66 
67     result = splitByScript("abcde", 2, 5);
68     ASSERT_EQ(1u, result.size());
69     EXPECT_EQ(Result(2, 5, HB_SCRIPT_LATIN), result[0]);
70 
71     result = splitByScript("abcde", 2, 3);
72     ASSERT_EQ(1u, result.size());
73     EXPECT_EQ(Result(2, 3, HB_SCRIPT_LATIN), result[0]);
74 }
75 
TEST(ScriptUtilsTest,Arabic)76 TEST(ScriptUtilsTest, Arabic) {
77     auto result = splitByScript("\u0645\u0631\u062D\u0628\u064B\u0627", 0, 6);
78     ASSERT_EQ(1u, result.size());
79     EXPECT_EQ(Result(0, 6, HB_SCRIPT_ARABIC), result[0]);
80 
81     result = splitByScript("\u0645\u0631\u062D\u0628\u064B\u0627", 0, 3);
82     ASSERT_EQ(1u, result.size());
83     EXPECT_EQ(Result(0, 3, HB_SCRIPT_ARABIC), result[0]);
84 
85     result = splitByScript("\u0645\u0631\u062D\u0628\u064B\u0627", 2, 5);
86     ASSERT_EQ(1u, result.size());
87     EXPECT_EQ(Result(2, 5, HB_SCRIPT_ARABIC), result[0]);
88 
89     result = splitByScript("\u0645\u0631\u062D\u0628\u064B\u0627", 2, 3);
90     ASSERT_EQ(1u, result.size());
91     EXPECT_EQ(Result(2, 3, HB_SCRIPT_ARABIC), result[0]);
92 }
93 
TEST(ScriptUtilsTest,Common)94 TEST(ScriptUtilsTest, Common) {
95     auto result = splitByScript("     ", 0, 5);
96     ASSERT_EQ(1u, result.size());
97     EXPECT_EQ(Result(0, 5, HB_SCRIPT_COMMON), result[0]);
98 
99     result = splitByScript("     ", 0, 3);
100     ASSERT_EQ(1u, result.size());
101     EXPECT_EQ(Result(0, 3, HB_SCRIPT_COMMON), result[0]);
102 
103     result = splitByScript("     ", 2, 5);
104     ASSERT_EQ(1u, result.size());
105     EXPECT_EQ(Result(2, 5, HB_SCRIPT_COMMON), result[0]);
106 
107     result = splitByScript("     ", 2, 3);
108     ASSERT_EQ(1u, result.size());
109     EXPECT_EQ(Result(2, 3, HB_SCRIPT_COMMON), result[0]);
110 }
111 
TEST(ScriptUtilsTest,InheritOrCommon)112 TEST(ScriptUtilsTest, InheritOrCommon) {
113     // Parens are inherit which is inherit from the previous script. If there is no character
114     // before, use the next non-inherit type of script.
115     auto result = splitByScript("(abc)", 0, 5);
116     ASSERT_EQ(1u, result.size());
117     EXPECT_EQ(Result(0, 5, HB_SCRIPT_LATIN), result[0]);
118 
119     result = splitByScript("[(b)]", 0, 5);
120     ASSERT_EQ(1u, result.size());
121     EXPECT_EQ(Result(0, 5, HB_SCRIPT_LATIN), result[0]);
122 
123     result = splitByScript("[(b)]", 0, 2);
124     ASSERT_EQ(1u, result.size());
125     EXPECT_EQ(Result(0, 2, HB_SCRIPT_COMMON), result[0]);
126 }
127 
TEST(ScriptUtilsTest,MultiScript_InheritOrCommon)128 TEST(ScriptUtilsTest, MultiScript_InheritOrCommon) {
129     auto result = splitByScript("a(\u0645)e", 0, 5);
130     EXPECT_EQ(Result(0, 2, HB_SCRIPT_LATIN), result[0]);
131     EXPECT_EQ(Result(2, 4, HB_SCRIPT_ARABIC), result[1]);
132     EXPECT_EQ(Result(4, 5, HB_SCRIPT_LATIN), result[2]);
133 }
134 
TEST(ScriptUtilsTest,MultiScript_NoInheritOrCommon)135 TEST(ScriptUtilsTest, MultiScript_NoInheritOrCommon) {
136     auto result = splitByScript("a\u0645b\u0631c", 0, 5);
137     EXPECT_EQ(Result(0, 1, HB_SCRIPT_LATIN), result[0]);
138     EXPECT_EQ(Result(1, 2, HB_SCRIPT_ARABIC), result[1]);
139     EXPECT_EQ(Result(2, 3, HB_SCRIPT_LATIN), result[2]);
140     EXPECT_EQ(Result(3, 4, HB_SCRIPT_ARABIC), result[3]);
141     EXPECT_EQ(Result(4, 5, HB_SCRIPT_LATIN), result[4]);
142 }
143 
TEST(ScriptUtilsTest,SurrogatePair)144 TEST(ScriptUtilsTest, SurrogatePair) {
145     auto result = splitByScript(std::vector<uint16_t>({0xD83C, 0xDFF3, 0xD83C, 0xDFF3}), 0, 4);
146     ASSERT_EQ(1u, result.size());
147     EXPECT_EQ(Result(0, 4, HB_SCRIPT_COMMON), result[0]);
148 
149     result = splitByScript(std::vector<uint16_t>({0xD83C, 0xDFF3, 0xD83C, 0xDFF3}), 0, 3);
150     ASSERT_EQ(1u, result.size());
151     EXPECT_EQ(Result(0, 3, HB_SCRIPT_COMMON), result[0]);
152 
153     result = splitByScript(std::vector<uint16_t>({0xD83C, 0xDFF3, 0xD83C, 0xDFF3}), 1, 4);
154     ASSERT_EQ(1u, result.size());
155     EXPECT_EQ(Result(1, 4, HB_SCRIPT_COMMON), result[0]);
156 
157     result = splitByScript(std::vector<uint16_t>({0xD83C, 0xDFF3, 0xD83C, 0xDFF3}), 1, 3);
158     ASSERT_EQ(1u, result.size());
159     EXPECT_EQ(Result(1, 3, HB_SCRIPT_COMMON), result[0]);
160 }
161 }  // namespace
162 }  // namespace minikin
163