• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "minikin/FontCollection.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "FontTestUtils.h"
22 #include "FreeTypeMinikinFontForTest.h"
23 #include "MinikinInternal.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 = {{MinikinFont::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 = {{MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f},
153                                                  {MinikinFont::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 = {{MinikinFont::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 = {{MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f},
170                                                  {MinikinFont::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 
writeToBuffer(const std::vector<std::shared_ptr<FontCollection>> & collections)180 std::vector<uint8_t> writeToBuffer(
181         const std::vector<std::shared_ptr<FontCollection>>& collections) {
182     BufferWriter fakeWriter(nullptr);
183     FontCollection::writeVector<writeFreeTypeMinikinFontForTest>(&fakeWriter, collections);
184     std::vector<uint8_t> buffer(fakeWriter.size());
185     BufferWriter writer(buffer.data());
186     FontCollection::writeVector<writeFreeTypeMinikinFontForTest>(&writer, collections);
187     return buffer;
188 }
189 
TEST(FontCollectionTest,bufferTest)190 TEST(FontCollectionTest, bufferTest) {
191     {
192         std::vector<std::shared_ptr<FontCollection>> original({buildFontCollection(kVsTestFont)});
193         std::vector<uint8_t> buffer = writeToBuffer(original);
194         BufferReader reader(buffer.data());
195         auto copied = FontCollection::readVector<readFreeTypeMinikinFontForTest>(&reader);
196         EXPECT_EQ(1u, copied.size());
197         expectVSGlyphsForVsTestFont(copied[0].get());
198         EXPECT_EQ(original[0]->getSupportedTags(), copied[0]->getSupportedTags());
199         // Id will be different.
200         EXPECT_NE(original[0]->getId(), copied[0]->getId());
201         std::vector<uint8_t> newBuffer = writeToBuffer(copied);
202         EXPECT_EQ(buffer, newBuffer);
203     }
204     {
205         // Test that FontFamily instances are shared.
206         std::vector<std::shared_ptr<FontFamily>> families = {buildFontFamily(kVsTestFont)};
207         auto fc1 = std::make_shared<FontCollection>(families);
208         auto fc2 = std::make_shared<FontCollection>(families);
209         std::vector<std::shared_ptr<FontCollection>> original({fc1, fc2});
210         std::vector<uint8_t> buffer = writeToBuffer(original);
211         BufferReader reader(buffer.data());
212         auto copied = FontCollection::readVector<readFreeTypeMinikinFontForTest>(&reader);
213         EXPECT_EQ(2u, copied.size());
214         EXPECT_EQ(copied[0]->mFamilies[0], copied[1]->mFamilies[0]);
215         std::vector<uint8_t> newBuffer = writeToBuffer(copied);
216         EXPECT_EQ(buffer, newBuffer);
217     }
218     {
219         // Test axes.
220         // This font has 'wdth' and 'wght' axes.
221         const char kMultiAxisFont[] = "MultiAxis.ttf";
222         std::vector<std::shared_ptr<FontCollection>> original(
223                 {buildFontCollection(kMultiAxisFont)});
224         std::vector<uint8_t> buffer = writeToBuffer(original);
225         BufferReader reader(buffer.data());
226         auto copied = FontCollection::readVector<readFreeTypeMinikinFontForTest>(&reader);
227         EXPECT_EQ(1u, copied.size());
228         EXPECT_EQ(1u,
229                   copied[0]->getSupportedTags().count(MinikinFont::MakeTag('w', 'd', 't', 'h')));
230         EXPECT_EQ(1u,
231                   copied[0]->getSupportedTags().count(MinikinFont::MakeTag('w', 'g', 'h', 't')));
232         std::vector<uint8_t> newBuffer = writeToBuffer(copied);
233         EXPECT_EQ(buffer, newBuffer);
234     }
235 }
236 
TEST(FontCollectionTest,FamilyMatchResultBuilderTest)237 TEST(FontCollectionTest, FamilyMatchResultBuilderTest) {
238     using Builder = FontCollection::FamilyMatchResult::Builder;
239     EXPECT_TRUE(Builder().empty());
240     EXPECT_EQ(0u, Builder().size());
241     EXPECT_EQ(1u, Builder().add(5).size());
242     EXPECT_EQ(2u, Builder().add(5).add(4).size());
243 
244     // Reset
245     EXPECT_TRUE(Builder().add(5).reset().empty());
246     EXPECT_EQ(0u, Builder().add(5).reset().size());
247 }
248 
TEST(FontCollectionTest,FamilyMatchResultTest)249 TEST(FontCollectionTest, FamilyMatchResultTest) {
250     using Builder = FontCollection::FamilyMatchResult::Builder;
251 
252     auto r = Builder().build();
253     EXPECT_EQ(0u, r.size());
254     EXPECT_TRUE(r.empty());
255 
256     r = Builder().add(1).build();
257     EXPECT_EQ(1u, r.size());
258     EXPECT_FALSE(r.empty());
259     EXPECT_EQ(1u, r[0]);
260 
261     r = Builder().add(1).add(2).build();
262     EXPECT_EQ(2u, r.size());
263     EXPECT_FALSE(r.empty());
264     EXPECT_EQ(1u, r[0]);
265     EXPECT_EQ(2u, r[1]);
266 }
267 
TEST(FontCollectionTest,FamilyMatchResultTest_BuilderHoldeFirst7)268 TEST(FontCollectionTest, FamilyMatchResultTest_BuilderHoldeFirst7) {
269     auto b = FontCollection::FamilyMatchResult::Builder();
270     for (uint8_t i = 0; i < 128; ++i) {
271         b.add(i);
272     }
273     auto r = b.build();
274     EXPECT_EQ(7u, r.size());
275     EXPECT_FALSE(r.empty());
276     EXPECT_EQ(0u, r[0]);
277     EXPECT_EQ(1u, r[1]);
278     EXPECT_EQ(2u, r[2]);
279     EXPECT_EQ(3u, r[3]);
280     EXPECT_EQ(4u, r[4]);
281     EXPECT_EQ(5u, r[5]);
282     EXPECT_EQ(6u, r[6]);
283 }
284 
TEST(FontCollectionTest,FamilyMatchResultTest_iterator)285 TEST(FontCollectionTest, FamilyMatchResultTest_iterator) {
286     auto b = FontCollection::FamilyMatchResult::Builder();
287     for (uint8_t i = 0; i < 7; ++i) {
288         b.add(i);
289     }
290     auto r = b.build();
291     EXPECT_EQ(7u, r.size());
292     EXPECT_FALSE(r.empty());
293     int i = 0;
294     for (auto v : r) {
295         EXPECT_EQ(i, v);
296         i++;
297     }
298 }
299 
TEST(FontCollectionTest,FamilyMatchResultTest_intersect)300 TEST(FontCollectionTest, FamilyMatchResultTest_intersect) {
301     using Builder = FontCollection::FamilyMatchResult::Builder;
302 
303     EXPECT_EQ(Builder().add(1).add(2).add(3).build(),
304               FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
305                                                            Builder().add(1).add(2).add(3).build()));
306 
307     EXPECT_EQ(Builder().build(),
308               FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
309                                                            Builder().build()));
310 
311     EXPECT_EQ(Builder().build(),
312               FontCollection::FamilyMatchResult::intersect(Builder().add(2).add(4).add(6).build(),
313                                                            Builder().add(1).add(3).add(5).build()));
314 
315     EXPECT_EQ(Builder().add(1).add(3).build(),
316               FontCollection::FamilyMatchResult::intersect(Builder().add(1).add(2).add(3).build(),
317                                                            Builder().add(1).add(3).add(5).build()));
318 }
319 
320 }  // namespace minikin
321