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 <minikin/FontCollection.h>
20 #include "FontTestUtils.h"
21 #include "MinikinFontForTest.h"
22 #include "MinikinInternal.h"
23 
24 namespace minikin {
25 
26 // The test font has following glyphs.
27 // U+82A6
28 // U+82A6 U+FE00 (VS1)
29 // U+82A6 U+E0100 (VS17)
30 // U+82A6 U+E0101 (VS18)
31 // U+82A6 U+E0102 (VS19)
32 // U+845B
33 // U+845B U+FE01 (VS2)
34 // U+845B U+E0101 (VS18)
35 // U+845B U+E0102 (VS19)
36 // U+845B U+E0103 (VS20)
37 // U+537F
38 // U+717D U+FE02 (VS3)
39 // U+717D U+E0102 (VS19)
40 // U+717D U+E0103 (VS20)
41 const char kVsTestFont[] = kTestFontDir "/VariationSelectorTest-Regular.ttf";
42 
expectVSGlyphs(const FontCollection * fc,uint32_t codepoint,const std::set<uint32_t> & vsSet)43 void expectVSGlyphs(const FontCollection* fc, uint32_t codepoint, const std::set<uint32_t>& vsSet) {
44     for (uint32_t vs = 0xFE00; vs <= 0xE01EF; ++vs) {
45         // Move to variation selectors supplements after variation selectors.
46         if (vs == 0xFF00) {
47             vs = 0xE0100;
48         }
49         if (vsSet.find(vs) == vsSet.end()) {
50             EXPECT_FALSE(fc->hasVariationSelector(codepoint, vs))
51                 << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
52         } else {
53             EXPECT_TRUE(fc->hasVariationSelector(codepoint, vs))
54                 << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
55         }
56     }
57 }
58 
TEST(FontCollectionTest,hasVariationSelectorTest)59 TEST(FontCollectionTest, hasVariationSelectorTest) {
60   std::shared_ptr<MinikinFont> font(new MinikinFontForTest(kVsTestFont));
61   std::shared_ptr<FontFamily> family(new FontFamily(
62           std::vector<Font>({ Font(font, FontStyle()) })));
63   std::vector<std::shared_ptr<FontFamily>> families({ family });
64   std::shared_ptr<FontCollection> fc(new FontCollection(families));
65 
66   EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0));
67   expectVSGlyphs(fc.get(), 0x82A6, std::set<uint32_t>({0xFE00, 0xFE0E, 0xE0100, 0xE0101, 0xE0102}));
68 
69   EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0));
70   expectVSGlyphs(fc.get(), 0x845B, std::set<uint32_t>({0xFE01, 0xFE0E, 0xE0101, 0xE0102, 0xE0103}));
71 
72   EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0));
73   expectVSGlyphs(fc.get(), 0x537F, std::set<uint32_t>({0xFE0E}));
74 
75   EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0));
76   expectVSGlyphs(fc.get(), 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
77 }
78 
79 const char kEmojiXmlFile[] = kTestFontDir "emoji.xml";
80 
TEST(FontCollectionTest,hasVariationSelectorTest_emoji)81 TEST(FontCollectionTest, hasVariationSelectorTest_emoji) {
82     std::shared_ptr<FontCollection> collection(getFontCollection(kTestFontDir, 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 }
113 
TEST(FontCollectionTest,newEmojiTest)114 TEST(FontCollectionTest, newEmojiTest) {
115     std::shared_ptr<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));
116 
117     // U+2695, U+2640, U+2642 are not in emoji catrgory in Unicode 9 but they are now in emoji
118     // category. Should return true even if U+FE0E was appended.
119     // These three emojis are only avalilable in TextEmoji.ttf but U+2695 is excluded here since it
120     // is used in other tests.
121     EXPECT_TRUE(collection->hasVariationSelector(0x2640, 0xFE0E));
122     EXPECT_FALSE(collection->hasVariationSelector(0x2640, 0xFE0F));
123     EXPECT_TRUE(collection->hasVariationSelector(0x2642, 0xFE0E));
124     EXPECT_FALSE(collection->hasVariationSelector(0x2642, 0xFE0F));
125 }
126 
TEST(FontCollectionTest,createWithVariations)127 TEST(FontCollectionTest, createWithVariations) {
128     // This font has 'wdth' and 'wght' axes.
129     const char kMultiAxisFont[] = kTestFontDir "/MultiAxis.ttf";
130     const char kNoAxisFont[] = kTestFontDir "/Regular.ttf";
131 
132     std::shared_ptr<MinikinFont> multiAxisFont(new MinikinFontForTest(kMultiAxisFont));
133     std::shared_ptr<FontFamily> multiAxisFamily(new FontFamily(
134             std::vector<Font>({ Font(multiAxisFont, FontStyle()) })));
135     std::vector<std::shared_ptr<FontFamily>> multiAxisFamilies({multiAxisFamily});
136     std::shared_ptr<FontCollection> multiAxisFc(new FontCollection(multiAxisFamilies));
137 
138     std::shared_ptr<MinikinFont> noAxisFont(new MinikinFontForTest(kNoAxisFont));
139     std::shared_ptr<FontFamily> noAxisFamily(new FontFamily(
140             std::vector<Font>({ Font(noAxisFont, FontStyle()) })));
141     std::vector<std::shared_ptr<FontFamily>> noAxisFamilies({noAxisFamily});
142     std::shared_ptr<FontCollection> noAxisFc(new FontCollection(noAxisFamilies));
143 
144     {
145         // Do not ceate new instance if none of variations are specified.
146         EXPECT_EQ(nullptr,
147                 multiAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
148         EXPECT_EQ(nullptr,
149                 noAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
150     }
151     {
152         // New instance should be used for supported variation.
153         std::vector<FontVariation> variations = {
154                 { MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f }
155         };
156         std::shared_ptr<FontCollection> newFc(
157                 multiAxisFc->createCollectionWithVariation(variations));
158         EXPECT_NE(nullptr, newFc.get());
159         EXPECT_NE(multiAxisFc.get(), newFc.get());
160 
161         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
162     }
163     {
164         // New instance should be used for supported variation (multiple variations case).
165         std::vector<FontVariation> variations = {
166                 { MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f },
167                 { MinikinFont::MakeTag('w', 'g', 'h', 't'), 1.0f }
168         };
169         std::shared_ptr<FontCollection> newFc(
170                 multiAxisFc->createCollectionWithVariation(variations));
171         EXPECT_NE(nullptr, newFc.get());
172         EXPECT_NE(multiAxisFc.get(), newFc.get());
173 
174         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
175     }
176     {
177         // Do not ceate new instance if none of variations are supported.
178         std::vector<FontVariation> variations = {
179                 { MinikinFont::MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f }
180         };
181         EXPECT_EQ(nullptr, multiAxisFc->createCollectionWithVariation(variations));
182         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
183     }
184     {
185         // At least one axis is supported, should create new instance.
186         std::vector<FontVariation> variations = {
187                 { MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f },
188                 { MinikinFont::MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f }
189         };
190         std::shared_ptr<FontCollection> newFc(
191                 multiAxisFc->createCollectionWithVariation(variations));
192         EXPECT_NE(nullptr, newFc.get());
193         EXPECT_NE(multiAxisFc.get(), newFc.get());
194 
195         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
196     }
197 }
198 
199 }  // namespace minikin
200