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 #include "minikin/Measurement.h"
18
19 #include <gtest/gtest.h>
20
21 #include "UnicodeUtils.h"
22
23 namespace minikin {
24
getAdvance(const float * advances,const char * src)25 float getAdvance(const float* advances, const char* src) {
26 const size_t BUF_SIZE = 256;
27 uint16_t buf[BUF_SIZE];
28 size_t offset;
29 size_t size;
30 ParseUnicode(buf, BUF_SIZE, src, &size, &offset);
31 return getRunAdvance(advances, buf, 0, size, offset);
32 }
33
distributeAdvances(float * advances,const char * src,int count)34 void distributeAdvances(float* advances, const char* src, int count) {
35 const size_t BUF_SIZE = 256;
36 uint16_t buf[BUF_SIZE];
37 size_t offset;
38 size_t size;
39 ParseUnicode(buf, BUF_SIZE, src, &size, &offset);
40 distributeAdvances(advances, buf, offset, count);
41 }
42
43 // Latin fi
TEST(Measurement,getRunAdvance_fi)44 TEST(Measurement, getRunAdvance_fi) {
45 const float unligated[] = {30.0, 20.0};
46 EXPECT_EQ(0.0, getAdvance(unligated, "| 'f' 'i'"));
47 EXPECT_EQ(30.0, getAdvance(unligated, "'f' | 'i'"));
48 EXPECT_EQ(50.0, getAdvance(unligated, "'f' 'i' |"));
49
50 const float ligated[] = {40.0, 0.0};
51 EXPECT_EQ(0.0, getAdvance(ligated, "| 'f' 'i'"));
52 EXPECT_EQ(20.0, getAdvance(ligated, "'f' | 'i'"));
53 EXPECT_EQ(40.0, getAdvance(ligated, "'f' 'i' |"));
54 }
55
TEST(Measurement,getRunAdvance_control_characters)56 TEST(Measurement, getRunAdvance_control_characters) {
57 const float unligated[] = {30.0, 20.0, 0.0, 0.0};
58 EXPECT_EQ(0.0, getAdvance(unligated, "| 'f' 'i' U+2066 U+202C"));
59 EXPECT_EQ(30.0, getAdvance(unligated, "'f' | 'i' U+2066 U+202C"));
60 EXPECT_EQ(50.0, getAdvance(unligated, "'f' 'i' | U+2066 U+202C"));
61 EXPECT_EQ(50.0, getAdvance(unligated, "'f' 'i' U+2066 | U+202C"));
62 EXPECT_EQ(50.0, getAdvance(unligated, "'f' 'i' U+2066 U+202C |"));
63
64 const float liagated[] = {40.0, 0.0, 0.0, 0.0};
65 EXPECT_EQ(0.0, getAdvance(liagated, "| 'f' 'i' U+2066 U+202C"));
66 EXPECT_EQ(20.0, getAdvance(liagated, "'f' | 'i' U+2066 U+202C"));
67 EXPECT_EQ(40.0, getAdvance(liagated, "'f' 'i' | U+2066 U+202C"));
68 EXPECT_EQ(40.0, getAdvance(liagated, "'f' 'i' U+2066 | U+202C"));
69 EXPECT_EQ(40.0, getAdvance(liagated, "'f' 'i' U+2066 U+202C |"));
70 }
71
72 // Devanagari ka+virama+ka
TEST(Measurement,getRunAdvance_kka)73 TEST(Measurement, getRunAdvance_kka) {
74 const float unligated[] = {30.0, 0.0, 30.0};
75 EXPECT_EQ(0.0, getAdvance(unligated, "| U+0915 U+094D U+0915"));
76 EXPECT_EQ(30.0, getAdvance(unligated, "U+0915 | U+094D U+0915"));
77 EXPECT_EQ(30.0, getAdvance(unligated, "U+0915 U+094D | U+0915"));
78 EXPECT_EQ(60.0, getAdvance(unligated, "U+0915 U+094D U+0915 |"));
79
80 const float ligated[] = {30.0, 0.0, 0.0};
81 EXPECT_EQ(0.0, getAdvance(ligated, "| U+0915 U+094D U+0915"));
82 EXPECT_EQ(30.0, getAdvance(ligated, "U+0915 | U+094D U+0915"));
83 EXPECT_EQ(30.0, getAdvance(ligated, "U+0915 U+094D | U+0915"));
84 EXPECT_EQ(30.0, getAdvance(ligated, "U+0915 U+094D U+0915 |"));
85 }
86
TEST(Measurement,distributeAdvances_fi)87 TEST(Measurement, distributeAdvances_fi) {
88 float ligated[] = {20.0, 0.0};
89 distributeAdvances(ligated, "| 'f' 'i' ", 2);
90 EXPECT_EQ(ligated[0], 10.0);
91 EXPECT_EQ(ligated[1], 10.0);
92 }
93
TEST(Measurement,distributeAdvances_non_zero_start)94 TEST(Measurement, distributeAdvances_non_zero_start) {
95 // Note that advance[i] corresponding to (i + start)-th character.
96 float ligated[] = {20.0, 0.0};
97 distributeAdvances(ligated, "'a' 'b' | 'f' 'i' ", 2);
98 EXPECT_EQ(ligated[0], 10.0);
99 EXPECT_EQ(ligated[1], 10.0);
100 }
101
TEST(Measurement,distributeAdvances_non_zero_start_with_control_characters)102 TEST(Measurement, distributeAdvances_non_zero_start_with_control_characters) {
103 // Note that advance[i] corresponding to (i + start)-th character.
104 float ligated[] = {20.0, 0.0, 0.0, 0.0};
105 distributeAdvances(ligated, "'a' U+2066 | 'f' 'i' U+2066 U+202C", 4);
106 EXPECT_EQ(ligated[0], 10.0);
107 EXPECT_EQ(ligated[1], 10.0);
108 EXPECT_EQ(ligated[2], 0.0);
109 EXPECT_EQ(ligated[3], 0.0);
110 }
111
TEST(Measurement,distributeAdvances_with_count)112 TEST(Measurement, distributeAdvances_with_count) {
113 // Note that advance[i] corresponding to (i + start)-th character.
114 float ligated[] = {20.0, 0.0, 30.0, 0.0};
115 distributeAdvances(ligated, "'a' 'b' | 'f' 'i' 'f' 'i' ", 2);
116 EXPECT_EQ(ligated[0], 10.0);
117 EXPECT_EQ(ligated[1], 10.0);
118 // Count is 2, so it won't change the rest of the array.
119 EXPECT_EQ(ligated[2], 30.0);
120 EXPECT_EQ(ligated[3], 0.0);
121 }
122
TEST(Measurement,distributeAdvances_control_characters)123 TEST(Measurement, distributeAdvances_control_characters) {
124 float ligated[] = {20.0, 0.0, 0.0, 0.0};
125 distributeAdvances(ligated, "| 'f' 'i' U+2066 U+202C", 4);
126 EXPECT_EQ(ligated[0], 10.0);
127 EXPECT_EQ(ligated[1], 10.0);
128 EXPECT_EQ(ligated[2], 0.0);
129 EXPECT_EQ(ligated[3], 0.0);
130 }
131
TEST(Measurement,distributeAdvances_surrogate)132 TEST(Measurement, distributeAdvances_surrogate) {
133 float advances[] = {20.0, 0.0, 0.0, 0.0};
134 distributeAdvances(advances, "| U+D83D U+DE00 U+2066 U+202C", 4);
135 EXPECT_EQ(advances[0], 20.0);
136 EXPECT_EQ(advances[1], 0.0);
137 EXPECT_EQ(advances[2], 0.0);
138 EXPECT_EQ(advances[3], 0.0);
139 }
140
TEST(Measurement,distributeAdvances_surrogate_in_ligature)141 TEST(Measurement, distributeAdvances_surrogate_in_ligature) {
142 // If a ligature contains surrogates, advances is assigned to the first
143 // character in surrogate.
144 float ligated[] = {40.0, 0.0, 0.0, 0.0};
145 distributeAdvances(ligated, "| U+D83D U+DE00 U+D83D U+DE01", 4);
146 EXPECT_EQ(ligated[0], 20.0);
147 EXPECT_EQ(ligated[1], 0.0);
148 EXPECT_EQ(ligated[2], 20.0);
149 EXPECT_EQ(ligated[3], 0.0);
150 }
151
152 } // namespace minikin
153