Home | History | Annotate | Download | only in unittest
      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 "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[] = "VariationSelectorTest-Regular.ttf";
     42 
     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 
     59 TEST(FontCollectionTest, hasVariationSelectorTest) {
     60     auto fc = buildFontCollection(kVsTestFont);
     61 
     62     EXPECT_FALSE(fc->hasVariationSelector(0x82A6, 0));
     63     expectVSGlyphs(fc.get(), 0x82A6,
     64                    std::set<uint32_t>({0xFE00, 0xFE0E, 0xE0100, 0xE0101, 0xE0102}));
     65 
     66     EXPECT_FALSE(fc->hasVariationSelector(0x845B, 0));
     67     expectVSGlyphs(fc.get(), 0x845B,
     68                    std::set<uint32_t>({0xFE01, 0xFE0E, 0xE0101, 0xE0102, 0xE0103}));
     69 
     70     EXPECT_FALSE(fc->hasVariationSelector(0x537F, 0));
     71     expectVSGlyphs(fc.get(), 0x537F, std::set<uint32_t>({0xFE0E}));
     72 
     73     EXPECT_FALSE(fc->hasVariationSelector(0x717D, 0));
     74     expectVSGlyphs(fc.get(), 0x717D, std::set<uint32_t>({0xFE02, 0xE0102, 0xE0103}));
     75 }
     76 
     77 const char kEmojiXmlFile[] = "emoji.xml";
     78 
     79 TEST(FontCollectionTest, hasVariationSelectorTest_emoji) {
     80     auto collection = buildFontCollectionFromXml(kEmojiXmlFile);
     81 
     82     // Both text/color font have cmap format 14 subtable entry for VS15/VS16 respectively.
     83     EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0E));
     84     EXPECT_TRUE(collection->hasVariationSelector(0x2623, 0xFE0F));
     85 
     86     // The text font has cmap format 14 subtable entry for VS15 but the color font doesn't have for
     87     // VS16
     88     EXPECT_TRUE(collection->hasVariationSelector(0x2626, 0xFE0E));
     89     EXPECT_FALSE(collection->hasVariationSelector(0x2626, 0xFE0F));
     90 
     91     // The color font has cmap format 14 subtable entry for VS16 but the text font doesn't have for
     92     // VS15.
     93     EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0E));
     94     EXPECT_TRUE(collection->hasVariationSelector(0x262A, 0xFE0F));
     95 
     96     // Neither text/color font have cmap format 14 subtable entry for VS15/VS16.
     97     EXPECT_TRUE(collection->hasVariationSelector(0x262E, 0xFE0E));
     98     EXPECT_FALSE(collection->hasVariationSelector(0x262E, 0xFE0F));
     99 
    100     // Text font doesn't support U+1F3FD. Only the color emoji fonts has. So VS15 is not supported.
    101     EXPECT_FALSE(collection->hasVariationSelector(0x1F3FD, 0xFE0E));
    102 
    103     // Text font doesn't have U+262F U+FE0E or even its base code point U+262F.
    104     EXPECT_FALSE(collection->hasVariationSelector(0x262F, 0xFE0E));
    105 
    106     // None of the fonts support U+2229.
    107     EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0E));
    108     EXPECT_FALSE(collection->hasVariationSelector(0x2229, 0xFE0F));
    109 }
    110 
    111 TEST(FontCollectionTest, newEmojiTest) {
    112     auto collection = buildFontCollectionFromXml(kEmojiXmlFile);
    113 
    114     // U+2695, U+2640, U+2642 are not in emoji catrgory in Unicode 9 but they are now in emoji
    115     // category. Should return true even if U+FE0E was appended.
    116     // These three emojis are only avalilable in TextEmoji.ttf but U+2695 is excluded here since it
    117     // is used in other tests.
    118     EXPECT_TRUE(collection->hasVariationSelector(0x2640, 0xFE0E));
    119     EXPECT_FALSE(collection->hasVariationSelector(0x2640, 0xFE0F));
    120     EXPECT_TRUE(collection->hasVariationSelector(0x2642, 0xFE0E));
    121     EXPECT_FALSE(collection->hasVariationSelector(0x2642, 0xFE0F));
    122 }
    123 
    124 TEST(FontCollectionTest, createWithVariations) {
    125     // This font has 'wdth' and 'wght' axes.
    126     const char kMultiAxisFont[] = "MultiAxis.ttf";
    127     const char kNoAxisFont[] = "Regular.ttf";
    128 
    129     std::shared_ptr<FontCollection> multiAxisFc = buildFontCollection(kMultiAxisFont);
    130     std::shared_ptr<FontCollection> noAxisFc = buildFontCollection(kNoAxisFont);
    131 
    132     {
    133         // Do not ceate new instance if none of variations are specified.
    134         EXPECT_EQ(nullptr,
    135                   multiAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
    136         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(std::vector<FontVariation>()));
    137     }
    138     {
    139         // New instance should be used for supported variation.
    140         std::vector<FontVariation> variations = {{MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f}};
    141         std::shared_ptr<FontCollection> newFc(
    142                 multiAxisFc->createCollectionWithVariation(variations));
    143         EXPECT_NE(nullptr, newFc.get());
    144         EXPECT_NE(multiAxisFc.get(), newFc.get());
    145 
    146         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
    147     }
    148     {
    149         // New instance should be used for supported variation (multiple variations case).
    150         std::vector<FontVariation> variations = {{MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f},
    151                                                  {MinikinFont::MakeTag('w', 'g', 'h', 't'), 1.0f}};
    152         std::shared_ptr<FontCollection> newFc(
    153                 multiAxisFc->createCollectionWithVariation(variations));
    154         EXPECT_NE(nullptr, newFc.get());
    155         EXPECT_NE(multiAxisFc.get(), newFc.get());
    156 
    157         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
    158     }
    159     {
    160         // Do not ceate new instance if none of variations are supported.
    161         std::vector<FontVariation> variations = {{MinikinFont::MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f}};
    162         EXPECT_EQ(nullptr, multiAxisFc->createCollectionWithVariation(variations));
    163         EXPECT_EQ(nullptr, noAxisFc->createCollectionWithVariation(variations));
    164     }
    165     {
    166         // At least one axis is supported, should create new instance.
    167         std::vector<FontVariation> variations = {{MinikinFont::MakeTag('w', 'd', 't', 'h'), 1.0f},
    168                                                  {MinikinFont::MakeTag('Z', 'Z', 'Z', 'Z'), 1.0f}};
    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 
    178 }  // namespace minikin
    179