1 /*
2 * Copyright (C) 2015 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 "FontTestUtils.h"
20 #include "FreeTypeMinikinFontForTest.h"
21 #include "MinikinInternal.h"
22 #include "minikin/Constants.h"
23 #include "minikin/FontCollection.h"
24
25 namespace minikin {
26
27 // The test font has following glyphs.
28 // U+82A6
29 // U+82A6 U+FE00 (VS1)
30 // U+82A6 U+E0100 (VS17)
31 // U+82A6 U+E0101 (VS18)
32 // U+82A6 U+E0102 (VS19)
33 // U+845B
34 // U+845B U+FE01 (VS2)
35 // U+845B U+E0101 (VS18)
36 // U+845B U+E0102 (VS19)
37 // U+845B U+E0103 (VS20)
38 // U+537F
39 // U+717D U+FE02 (VS3)
40 // U+717D U+E0102 (VS19)
41 // U+717D U+E0103 (VS20)
42 const char kVsTestFont[] = "VariationSelectorTest-Regular.ttf";
43
expectVSGlyphs(const FontCollection * fc,uint32_t codepoint,const std::set<uint32_t> & vsSet)44 void expectVSGlyphs(const FontCollection* fc, uint32_t codepoint, const std::set<uint32_t>& vsSet) {
45 for (uint32_t vs = 0xFE00; vs <= 0xE01EF; ++vs) {
46 // Move to variation selectors supplements after variation selectors.
47 if (vs == 0xFF00) {
48 vs = 0xE0100;
49 }
50 if (vsSet.find(vs) == vsSet.end()) {
51 EXPECT_FALSE(fc->hasVariationSelector(codepoint, vs))
52 << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
53 } else {
54 EXPECT_TRUE(fc->hasVariationSelector(codepoint, vs))
55 << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
56 }
57 }
58 }
59
expectVSGlyphsForVsTestFont(const FontCollection * fc)60 void expectVSGlyphsForVsTestFont(const FontCollection* fc) {
61 EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0));
62 expectVSGlyphs(fc, 0x82A6, std::set<uint32_t>({0xFE00, 0xFE0E, 0xE0100, 0xE0101, 0xE0102}));
63
64 EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0));
65 expectVSGlyphs(fc, 0x845B, std::set<uint32_t>({0xFE01, 0xFE0E, 0xE0101, 0xE0102, 0xE0103}));
66
67 EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0));
68 expectVSGlyphs(fc, 0x537F, std::set<uint32_t>({0xFE0E}));
69
70 EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0));
71 expectVSGlyphs(fc, 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
72 }
73
TEST(FontCollectionTest,hasVariationSelectorTest)74 TEST(FontCollectionTest, hasVariationSelectorTest) {
75 auto fc = buildFontCollection(kVsTestFont);
76 expectVSGlyphsForVsTestFont(fc.get());
77 }
78
79 const char kEmojiXmlFile[] = "emoji.xml";
80
TEST(FontCollectionTest,hasVariationSelectorTest_emoji)81 TEST(FontCollectionTest, hasVariationSelectorTest_emoji) {
82 auto collection = buildFontCollectionFromXml(kEmojiXmlFile);
83
84 // Both text/color font have cmap format 14 subtable entry for VS15/VS16 respectively.
85 EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0E));
86 EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0F));
87
88 // The text font has cmap format 14 subtable entry for VS15 but the color font doesn't have for
89 // VS16
90 EXPECT_TRUE(collection->hasVariationSelector(0x2626, 0xFE0E));
91 EXPECT_FALSE(collection->hasVariationSelector(0x2626, 0xFE0F));
92
93 // The color font has cmap format 14 subtable entry for VS16 but the text font doesn't have for
94 // VS15.
95 EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0E));
96 EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0F));
97
98 // Neither text/color font have cmap format 14 subtable entry for VS15/VS16.
99 EXPECT_TRUE(collection->hasVariationSelector(0x262E, 0xFE0E));
100 EXPECT_FALSE(collection->hasVariationSelector(0x262E, 0xFE0F));
101
102 // Text font doesn't support U+1F3FD. Only the color emoji fonts has. So VS15 is not supported.
103 EXPECT_FALSE(collection->hasVariationSelector(0x1F3FD, 0xFE0E));
104
105 // Text font doesn't have U+262F U+FE0E or even its base code point U+262F.
106 EXPECT_FALSE(collection->hasVariationSelector(0x262F, 0xFE0E));
107
108 // None of the fonts support U+2229.
109 EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0E));
110 EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0F));
111 }
112
TEST(FontCollectionTest,newEmojiTest)113 TEST(FontCollectionTest, newEmojiTest) {
114 auto collection = buildFontCollectionFromXml(kEmojiXmlFile);
115
116 // U+2695, U+2640, U+2642 are not in emoji catrgory in Unicode 9 but they are now in emoji
117 // category. Should return true even if U+FE0E was appended.
118 // These three emojis are only avalilable in TextEmoji.ttf but U+2695 is excluded here since it
119 // is used in other tests.
120 EXPECT_TRUE(collection->hasVariationSelector(0x2640, 0xFE0E));
121 EXPECT_FALSE(collection->hasVariationSelector(0x2640, 0xFE0F));
122 EXPECT_TRUE(collection->hasVariationSelector(0x2642, 0xFE0E));
123 EXPECT_FALSE(collection->hasVariationSelector(0x2642, 0xFE0F));
124 }
125
TEST(FontCollectionTest,createWithVariations)126 TEST(FontCollectionTest, createWithVariations) {
127 // This font has 'wdth' and 'wght' axes.
128 const char kMultiAxisFont[] = "MultiAxis.ttf";
129 const char kNoAxisFont[] = "Regular.ttf";
130
131 std::shared_ptr<FontCollection> multiAxisFc = buildFontCollection(kMultiAxisFont);
132 std::shared_ptr<FontCollection> noAxisFc = buildFontCollection(kNoAxisFont);
133
134 {
135 // Do not ceate new instance if none of variations are specified.
136 EXPECT_EQ(nullptr,
137 multiAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
138 EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
139 }
140 {
141 // New instance should be used for supported variation.
142 std::vector<FontVariation> variations = {{MakeTag('w', 'd', 't', 'h'), 1.0f}};
143 std::shared_ptr<FontCollection> newFc(
144 multiAxisFc->createCollectionWithVariation(variations));
145 EXPECT_NE(nullptr, newFc.get());
146 EXPECT_NE(multiAxisFc.get(), newFc.get());
147
148 EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
149 }
150 {
151 // New instance should be used for supported variation (multiple variations case).
152 std::vector<FontVariation> variations = {{MakeTag('w', 'd', 't', 'h'), 1.0f},
153 {MakeTag('w', 'g', 'h', 't'), 1.0f}};
154 std::shared_ptr<FontCollection> newFc(
155 multiAxisFc->createCollectionWithVariation(variations));
156 EXPECT_NE(nullptr, newFc.get());
157 EXPECT_NE(multiAxisFc.get(), newFc.get());
158
159 EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
160 }
161 {
162 // Do not ceate new instance if none of variations are supported.
163 std::vector<FontVariation> variations = {{MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f}};
164 EXPECT_EQ(nullptr, multiAxisFc->createCollectionWithVariation(variations));
165 EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
166 }
167 {
168 // At least one axis is supported, should create new instance.
169 std::vector<FontVariation> variations = {{MakeTag('w', 'd', 't', 'h'), 1.0f},
170 {MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f}};
171 std::shared_ptr<FontCollection> newFc(
172 multiAxisFc->createCollectionWithVariation(variations));
173 EXPECT_NE(nullptr, newFc.get());
174 EXPECT_NE(multiAxisFc.get(), newFc.get());
175
176 EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
177 }
178 }
179
TEST(FontCollectionTest,createCollectionWithFamilies)180 TEST(FontCollectionTest, createCollectionWithFamilies) {
181 auto fallback = buildFontCollectionFromXml(kEmojiXmlFile);
182 std::shared_ptr<FontFamily> family = buildFontFamily(kVsTestFont);
183 std::shared_ptr<FontCollection> created = fallback->createCollectionWithFamilies({family});
184 ASSERT_EQ(fallback->getFamilyCount() + 1, created->getFamilyCount());
185 EXPECT_EQ(family, created->getFamilyAt(0));
186 for (size_t i = 0; i < fallback->getFamilyCount(); i++) {
187 EXPECT_EQ(fallback->getFamilyAt(i), created->getFamilyAt(i + 1));
188 }
189 }
190
writeToBuffer(const std::vector<std::shared_ptr<FontCollection>> & collections)191 std::vector<uint8_t> writeToBuffer(
192 const std::vector<std::shared_ptr<FontCollection>>& collections) {
193 BufferWriter fakeWriter(nullptr);
194 FontCollection::writeVector(&fakeWriter, collections);
195 std::vector<uint8_t> buffer(fakeWriter.size());
196 BufferWriter writer(buffer.data());
197 FontCollection::writeVector(&writer, collections);
198 return buffer;
199 }
200
TEST(FontCollectionTest,bufferTest)201 TEST(FontCollectionTest, bufferTest) {
202 FreeTypeMinikinFontForTestFactory::init();
203 {
204 std::vector<std::shared_ptr<FontCollection>> original({buildFontCollection(kVsTestFont)});
205 std::vector<uint8_t> buffer = writeToBuffer(original);
206 BufferReader reader(buffer.data());
207 auto copied = FontCollection::readVector(&reader);
208 EXPECT_EQ(1u, copied.size());
209 expectVSGlyphsForVsTestFont(copied[0].get());
210 ASSERT_EQ(original[0]->getSupportedAxesCount(), copied[0]->getSupportedAxesCount());
211 for (size_t i = 0; i < original[0]->getSupportedAxesCount(); i++) {
212 EXPECT_EQ(original[0]->getSupportedAxisAt(i), copied[0]->getSupportedAxisAt(i));
213 }
214 // Id will be different.
215 EXPECT_NE(original[0]->getId(), copied[0]->getId());
216 std::vector<uint8_t> newBuffer = writeToBuffer(copied);
217 EXPECT_EQ(buffer, newBuffer);
218 }
219 {
220 // Test that FontFamily instances are shared.
221 std::vector<std::shared_ptr<FontFamily>> families = {buildFontFamily(kVsTestFont)};
222 auto fc1 = FontCollection::create(families);
223 auto fc2 = FontCollection::create(families);
224 std::vector<std::shared_ptr<FontCollection>> original({fc1, fc2});
225 std::vector<uint8_t> buffer = writeToBuffer(original);
226 BufferReader reader(buffer.data());
227 auto copied = FontCollection::readVector(&reader);
228 EXPECT_EQ(2u, copied.size());
229 EXPECT_EQ(copied[0]->getFamilyAt(0), copied[1]->getFamilyAt(0));
230 std::vector<uint8_t> newBuffer = writeToBuffer(copied);
231 EXPECT_EQ(buffer, newBuffer);
232 }
233 {
234 // Test axes.
235 // This font has 'wdth' and 'wght' axes.
236 const char kMultiAxisFont[] = "MultiAxis.ttf";
237 std::vector<std::shared_ptr<FontCollection>> original(
238 {buildFontCollection(kMultiAxisFont)});
239 std::vector<uint8_t> buffer = writeToBuffer(original);
240 BufferReader reader(buffer.data());
241 auto copied = FontCollection::readVector(&reader);
242 EXPECT_EQ(1u, copied.size());
243 ASSERT_EQ(2u, copied[0]->getSupportedAxesCount());
244 // mSupportedAxes must be sorted.
245 EXPECT_EQ(MakeTag('w', 'd', 't', 'h'), copied[0]->getSupportedAxisAt(0));
246 EXPECT_EQ(MakeTag('w', 'g', 'h', 't'), copied[0]->getSupportedAxisAt(1));
247 std::vector<uint8_t> newBuffer = writeToBuffer(copied);
248 EXPECT_EQ(buffer, newBuffer);
249 }
250 }
251
TEST(FontCollectionTest,FamilyMatchResultBuilderTest)252 TEST(FontCollectionTest, FamilyMatchResultBuilderTest) {
253 using Builder = FontCollection::FamilyMatchResult::Builder;
254 EXPECT_TRUE(Builder().empty());
255 EXPECT_EQ(0u, Builder().size());
256 EXPECT_EQ(1u, Builder().add(5).size());
257 EXPECT_EQ(2u, Builder().add(5).add(4).size());
258
259 // Reset
260 EXPECT_TRUE(Builder().add(5).reset().empty());
261 EXPECT_EQ(0u, Builder().add(5).reset().size());
262 }
263
TEST(FontCollectionTest,FamilyMatchResultTest)264 TEST(FontCollectionTest, FamilyMatchResultTest) {
265 using Builder = FontCollection::FamilyMatchResult::Builder;
266
267 auto r = Builder().build();
268 EXPECT_EQ(0u, r.size());
269 EXPECT_TRUE(r.empty());
270
271 r = Builder().add(1).build();
272 EXPECT_EQ(1u, r.size());
273 EXPECT_FALSE(r.empty());
274 EXPECT_EQ(1u, r[0]);
275
276 r = Builder().add(1).add(2).build();
277 EXPECT_EQ(2u, r.size());
278 EXPECT_FALSE(r.empty());
279 EXPECT_EQ(1u, r[0]);
280 EXPECT_EQ(2u, r[1]);
281 }
282
TEST(FontCollectionTest,FamilyMatchResultTest_BuilderHoldeFirst7)283 TEST(FontCollectionTest, FamilyMatchResultTest_BuilderHoldeFirst7) {
284 auto b = FontCollection::FamilyMatchResult::Builder();
285 for (uint8_t i = 0; i < 128; ++i) {
286 b.add(i);
287 }
288 auto r = b.build();
289 EXPECT_EQ(7u, r.size());
290 EXPECT_FALSE(r.empty());
291 EXPECT_EQ(0u, r[0]);
292 EXPECT_EQ(1u, r[1]);
293 EXPECT_EQ(2u, r[2]);
294 EXPECT_EQ(3u, r[3]);
295 EXPECT_EQ(4u, r[4]);
296 EXPECT_EQ(5u, r[5]);
297 EXPECT_EQ(6u, r[6]);
298 }
299
TEST(FontCollectionTest,FamilyMatchResultTest_iterator)300 TEST(FontCollectionTest, FamilyMatchResultTest_iterator) {
301 auto b = FontCollection::FamilyMatchResult::Builder();
302 for (uint8_t i = 0; i < 7; ++i) {
303 b.add(i);
304 }
305 auto r = b.build();
306 EXPECT_EQ(7u, r.size());
307 EXPECT_FALSE(r.empty());
308 int i = 0;
309 for (auto v : r) {
310 EXPECT_EQ(i, v);
311 i++;
312 }
313 }
314
TEST(FontCollectionTest,FamilyMatchResultTest_intersect)315 TEST(FontCollectionTest, FamilyMatchResultTest_intersect) {
316 using Builder = FontCollection::FamilyMatchResult::Builder;
317
318 EXPECT_EQ(Builder().add(1).add(2).add(3).build(),
319 FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
320 Builder().add(1).add(2).add(3).build()));
321
322 EXPECT_EQ(Builder().build(),
323 FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
324 Builder().build()));
325
326 EXPECT_EQ(Builder().build(),
327 FontCollection::FamilyMatchResult::intersect(Builder().add(2).add(4).add(6).build(),
328 Builder().add(1).add(3).add(5).build()));
329
330 EXPECT_EQ(Builder().add(1).add(3).build(),
331 FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
332 Builder().add(1).add(3).add(5).build()));
333 }
334
335 } // namespace minikin
336