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
24namespace android {
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)
41const char kVsTestFont[] = kTestFontDir "/VarioationSelectorTest-Regular.ttf";
42
43void 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
59TEST(FontCollectionTest, hasVariationSelectorTest) {
60  FontFamily* family = new FontFamily();
61  family->addFont(new MinikinFontForTest(kVsTestFont));
62  std::vector<FontFamily*> families({family});
63  FontCollection fc(families);
64  family->Unref();
65
66  EXPECT_FALSE(fc.hasVariationSelector(0x82A6, 0));
67  expectVSGlyphs(fc, 0x82A6, std::set<uint32_t>({0xFE00, 0xE0100, 0xE0101, 0xE0102}));
68
69  EXPECT_FALSE(fc.hasVariationSelector(0x845B, 0));
70  expectVSGlyphs(fc, 0x845B, std::set<uint32_t>({0xFE01, 0xE0101, 0xE0102, 0xE0103}));
71
72  EXPECT_FALSE(fc.hasVariationSelector(0x537F, 0));
73  expectVSGlyphs(fc, 0x537F, std::set<uint32_t>({}));
74
75  EXPECT_FALSE(fc.hasVariationSelector(0x717D, 0));
76  expectVSGlyphs(fc, 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
77}
78
79const char kEmojiXmlFile[] = kTestFontDir "emoji.xml";
80
81TEST(FontCollectionTest, hasVariationSelectorTest_emoji) {
82    MinikinAutoUnref<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 have U+262F U+FE0E or even its base code point U+262F.
103    EXPECT_FALSE(collection->hasVariationSelector(0x262F, 0xFE0E));
104
105    // VS15/VS16 is only for emoji, should return false for not an emoji code point.
106    EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0E));
107    EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0F));
108}
109
110}  // namespace android
111