FontCollectionTest.cpp 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <minikin/FontCollection.h>
20
#include "FontTestUtils.h"
21 22 23
#include "MinikinFontForTest.h"
#include "MinikinInternal.h"

S
Seigo Nonaka 已提交
24
namespace minikin {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

// The test font has following glyphs.
// U+82A6
// U+82A6 U+FE00 (VS1)
// U+82A6 U+E0100 (VS17)
// U+82A6 U+E0101 (VS18)
// U+82A6 U+E0102 (VS19)
// U+845B
// U+845B U+FE01 (VS2)
// U+845B U+E0101 (VS18)
// U+845B U+E0102 (VS19)
// U+845B U+E0103 (VS20)
// U+537F
// U+717D U+FE02 (VS3)
// U+717D U+E0102 (VS19)
// U+717D U+E0103 (VS20)
41
const char kVsTestFont[] = kTestFontDir "/VarioationSelectorTest-Regular.ttf";
42

S
Seigo Nonaka 已提交
43
void expectVSGlyphs(const FontCollection* fc, uint32_t codepoint, const std::set<uint32_t>& vsSet) {
44 45 46 47 48 49
    for (uint32_t vs = 0xFE00; vs <= 0xE01EF; ++vs) {
        // Move to variation selectors supplements after variation selectors.
        if (vs == 0xFF00) {
            vs = 0xE0100;
        }
        if (vsSet.find(vs) == vsSet.end()) {
S
Seigo Nonaka 已提交
50
            EXPECT_FALSE(fc->hasVariationSelector(codepoint, vs))
51 52
                << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
        } else {
S
Seigo Nonaka 已提交
53
            EXPECT_TRUE(fc->hasVariationSelector(codepoint, vs))
54 55 56 57 58 59
                << "Glyph for U+" << std::hex << codepoint << " U+" << vs;
        }
    }
}

TEST(FontCollectionTest, hasVariationSelectorTest) {
S
Seigo Nonaka 已提交
60
  MinikinAutoUnref<FontFamily> family(new FontFamily());
61
  MinikinAutoUnref<MinikinFont> font(new MinikinFontForTest(kVsTestFont));
S
Seigo Nonaka 已提交
62 63 64
  family->addFont(font.get());
  std::vector<FontFamily*> families({family.get()});
  MinikinAutoUnref<FontCollection> fc(new FontCollection(families));
65

S
Seigo Nonaka 已提交
66 67
  EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0));
  expectVSGlyphs(fc.get(), 0x82A6, std::set<uint32_t>({0xFE00, 0xE0100, 0xE0101, 0xE0102}));
68

S
Seigo Nonaka 已提交
69 70
  EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0));
  expectVSGlyphs(fc.get(), 0x845B, std::set<uint32_t>({0xFE01, 0xE0101, 0xE0102, 0xE0103}));
71

S
Seigo Nonaka 已提交
72 73
  EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0));
  expectVSGlyphs(fc.get(), 0x537F, std::set<uint32_t>({}));
74

S
Seigo Nonaka 已提交
75 76
  EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0));
  expectVSGlyphs(fc.get(), 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
const char kEmojiXmlFile[] = kTestFontDir "emoji.xml";

TEST(FontCollectionTest, hasVariationSelectorTest_emoji) {
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));

    // Both text/color font have cmap format 14 subtable entry for VS15/VS16 respectively.
    EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0E));
    EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0F));

    // The text font has cmap format 14 subtable entry for VS15 but the color font doesn't have for
    // VS16
    EXPECT_TRUE(collection->hasVariationSelector(0x2626, 0xFE0E));
    EXPECT_FALSE(collection->hasVariationSelector(0x2626, 0xFE0F));

    // The color font has cmap format 14 subtable entry for VS16 but the text font doesn't have for
    // VS15.
    EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0E));
    EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0F));

    // Neither text/color font have cmap format 14 subtable entry for VS15/VS16.
    EXPECT_TRUE(collection->hasVariationSelector(0x262E, 0xFE0E));
    EXPECT_FALSE(collection->hasVariationSelector(0x262E, 0xFE0F));

    // Text font doesn't have U+262F U+FE0E or even its base code point U+262F.
    EXPECT_FALSE(collection->hasVariationSelector(0x262F, 0xFE0E));

    // VS15/VS16 is only for emoji, should return false for not an emoji code point.
    EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0E));
    EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0F));
108 109 110 111 112 113 114 115 116 117 118 119 120 121

}

TEST(FontCollectionTest, newEmojiTest) {
    MinikinAutoUnref<FontCollection> collection(getFontCollection(kTestFontDir, kEmojiXmlFile));

    // U+2695, U+2640, U+2642 are not in emoji catrgory in Unicode 9 but they are now in emoji
    // category. Should return true even if U+FE0E was appended.
    // These three emojis are only avalilable in TextEmoji.ttf but U+2695 is excluded here since it
    // is used in other tests.
    EXPECT_TRUE(collection->hasVariationSelector(0x2640, 0xFE0E));
    EXPECT_FALSE(collection->hasVariationSelector(0x2640, 0xFE0F));
    EXPECT_TRUE(collection->hasVariationSelector(0x2642, 0xFE0E));
    EXPECT_FALSE(collection->hasVariationSelector(0x2642, 0xFE0F));
122 123
}

S
Seigo Nonaka 已提交
124
}  // namespace minikin