mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Some fonts support multiple scripts, for example, some fonts for Korean supports not only "Kore" but also "Jamo". To select fonts based on their multiple languages, this CL introduces the following changes: - Compares all languages of the font family and use the maximum score for font selection. - Even if each language of the font family doesn't support the requested language, the font get score of 2 if the requested font is covered by all of the languages of the font family. For example, the font for "ko-Hang,ko-Hani" gets score of 2 for the requested language "ko-Kore". Bug: 26687969 Change-Id: I7f13b51464c9b01982bb573251d77052b9ddbd70
416 lines
15 KiB
C++
416 lines
15 KiB
C++
/*
|
|
* 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/FontFamily.h>
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include "FontLanguageListCache.h"
|
|
#include "ICUTestBase.h"
|
|
#include "MinikinFontForTest.h"
|
|
#include "MinikinInternal.h"
|
|
|
|
namespace android {
|
|
|
|
typedef ICUTestBase FontLanguagesTest;
|
|
typedef ICUTestBase FontLanguageTest;
|
|
|
|
static const FontLanguages& createFontLanguages(const std::string& input) {
|
|
AutoMutex _l(gMinikinLock);
|
|
uint32_t langId = FontLanguageListCache::getId(input);
|
|
return FontLanguageListCache::getById(langId);
|
|
}
|
|
|
|
static FontLanguage createFontLanguage(const std::string& input) {
|
|
AutoMutex _l(gMinikinLock);
|
|
uint32_t langId = FontLanguageListCache::getId(input);
|
|
return FontLanguageListCache::getById(langId)[0];
|
|
}
|
|
|
|
TEST_F(FontLanguageTest, basicTests) {
|
|
FontLanguage defaultLang;
|
|
FontLanguage emptyLang("", 0);
|
|
FontLanguage english = createFontLanguage("en");
|
|
FontLanguage french = createFontLanguage("fr");
|
|
FontLanguage und = createFontLanguage("und");
|
|
FontLanguage undZsye = createFontLanguage("und-Zsye");
|
|
|
|
EXPECT_EQ(english, english);
|
|
EXPECT_EQ(french, french);
|
|
|
|
EXPECT_TRUE(defaultLang != defaultLang);
|
|
EXPECT_TRUE(emptyLang != emptyLang);
|
|
EXPECT_TRUE(defaultLang != emptyLang);
|
|
EXPECT_TRUE(defaultLang != und);
|
|
EXPECT_TRUE(emptyLang != und);
|
|
EXPECT_TRUE(english != defaultLang);
|
|
EXPECT_TRUE(english != emptyLang);
|
|
EXPECT_TRUE(english != french);
|
|
EXPECT_TRUE(english != undZsye);
|
|
EXPECT_TRUE(und != undZsye);
|
|
EXPECT_TRUE(english != und);
|
|
|
|
EXPECT_TRUE(defaultLang.isUnsupported());
|
|
EXPECT_TRUE(emptyLang.isUnsupported());
|
|
|
|
EXPECT_FALSE(english.isUnsupported());
|
|
EXPECT_FALSE(french.isUnsupported());
|
|
EXPECT_FALSE(und.isUnsupported());
|
|
EXPECT_FALSE(undZsye.isUnsupported());
|
|
}
|
|
|
|
TEST_F(FontLanguageTest, getStringTest) {
|
|
EXPECT_EQ("en-Latn", createFontLanguage("en").getString());
|
|
EXPECT_EQ("en-Latn", createFontLanguage("en-Latn").getString());
|
|
|
|
// Capitalized language code or lowercased script should be normalized.
|
|
EXPECT_EQ("en-Latn", createFontLanguage("EN-LATN").getString());
|
|
EXPECT_EQ("en-Latn", createFontLanguage("EN-latn").getString());
|
|
EXPECT_EQ("en-Latn", createFontLanguage("en-latn").getString());
|
|
|
|
// Invalid script should be kept.
|
|
EXPECT_EQ("en-Xyzt", createFontLanguage("en-xyzt").getString());
|
|
|
|
EXPECT_EQ("en-Latn", createFontLanguage("en-Latn-US").getString());
|
|
EXPECT_EQ("ja-Jpan", createFontLanguage("ja").getString());
|
|
EXPECT_EQ("und", createFontLanguage("und").getString());
|
|
EXPECT_EQ("und", createFontLanguage("UND").getString());
|
|
EXPECT_EQ("und", createFontLanguage("Und").getString());
|
|
EXPECT_EQ("und-Zsye", createFontLanguage("und-Zsye").getString());
|
|
EXPECT_EQ("und-Zsye", createFontLanguage("Und-ZSYE").getString());
|
|
EXPECT_EQ("und-Zsye", createFontLanguage("Und-zsye").getString());
|
|
|
|
EXPECT_EQ("de-Latn", createFontLanguage("de-1901").getString());
|
|
|
|
// This is not a necessary desired behavior, just known behavior.
|
|
EXPECT_EQ("en-Latn", createFontLanguage("und-Abcdefgh").getString());
|
|
}
|
|
|
|
TEST_F(FontLanguageTest, ScriptEqualTest) {
|
|
EXPECT_TRUE(createFontLanguage("en").isEqualScript(createFontLanguage("en")));
|
|
EXPECT_TRUE(createFontLanguage("en-Latn").isEqualScript(createFontLanguage("en")));
|
|
EXPECT_TRUE(createFontLanguage("jp-Latn").isEqualScript(createFontLanguage("en-Latn")));
|
|
EXPECT_TRUE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Jpan")));
|
|
|
|
EXPECT_FALSE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Hira")));
|
|
EXPECT_FALSE(createFontLanguage("en-Jpan").isEqualScript(createFontLanguage("en-Hani")));
|
|
}
|
|
|
|
TEST_F(FontLanguageTest, ScriptMatchTest) {
|
|
const bool SUPPORTED = true;
|
|
const bool NOT_SUPPORTED = false;
|
|
|
|
struct TestCase {
|
|
const std::string baseScript;
|
|
const std::string requestedScript;
|
|
bool isSupported;
|
|
} testCases[] = {
|
|
// Same scripts
|
|
{ "en-Latn", "Latn", SUPPORTED },
|
|
{ "ja-Jpan", "Jpan", SUPPORTED },
|
|
{ "ja-Hira", "Hira", SUPPORTED },
|
|
{ "ja-Kana", "Kana", SUPPORTED },
|
|
{ "ja-Hrkt", "Hrkt", SUPPORTED },
|
|
{ "zh-Hans", "Hans", SUPPORTED },
|
|
{ "zh-Hant", "Hant", SUPPORTED },
|
|
{ "zh-Hani", "Hani", SUPPORTED },
|
|
{ "ko-Kore", "Kore", SUPPORTED },
|
|
{ "ko-Hang", "Hang", SUPPORTED },
|
|
{ "zh-Hanb", "Hanb", SUPPORTED },
|
|
|
|
// Japanese supports Hiragana, Katakanara, etc.
|
|
{ "ja-Jpan", "Hira", SUPPORTED },
|
|
{ "ja-Jpan", "Kana", SUPPORTED },
|
|
{ "ja-Jpan", "Hrkt", SUPPORTED },
|
|
{ "ja-Hrkt", "Hira", SUPPORTED },
|
|
{ "ja-Hrkt", "Kana", SUPPORTED },
|
|
|
|
// Chinese supports Han.
|
|
{ "zh-Hans", "Hani", SUPPORTED },
|
|
{ "zh-Hant", "Hani", SUPPORTED },
|
|
{ "zh-Hanb", "Hani", SUPPORTED },
|
|
|
|
// Hanb supports Bopomofo.
|
|
{ "zh-Hanb", "Bopo", SUPPORTED },
|
|
|
|
// Korean supports Hangul.
|
|
{ "ko-Kore", "Hang", SUPPORTED },
|
|
|
|
// Different scripts
|
|
{ "ja-Jpan", "Latn", NOT_SUPPORTED },
|
|
{ "en-Latn", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Jpan", "Hant", NOT_SUPPORTED },
|
|
{ "zh-Hant", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Jpan", "Hans", NOT_SUPPORTED },
|
|
{ "zh-Hans", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Jpan", "Kore", NOT_SUPPORTED },
|
|
{ "ko-Kore", "Jpan", NOT_SUPPORTED },
|
|
{ "zh-Hans", "Hant", NOT_SUPPORTED },
|
|
{ "zh-Hant", "Hans", NOT_SUPPORTED },
|
|
{ "zh-Hans", "Kore", NOT_SUPPORTED },
|
|
{ "ko-Kore", "Hans", NOT_SUPPORTED },
|
|
{ "zh-Hant", "Kore", NOT_SUPPORTED },
|
|
{ "ko-Kore", "Hant", NOT_SUPPORTED },
|
|
|
|
// Hiragana doesn't support Japanese, etc.
|
|
{ "ja-Hira", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Kana", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Hrkt", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Hani", "Jpan", NOT_SUPPORTED },
|
|
{ "ja-Hira", "Hrkt", NOT_SUPPORTED },
|
|
{ "ja-Kana", "Hrkt", NOT_SUPPORTED },
|
|
{ "ja-Hani", "Hrkt", NOT_SUPPORTED },
|
|
{ "ja-Hani", "Hira", NOT_SUPPORTED },
|
|
{ "ja-Hani", "Kana", NOT_SUPPORTED },
|
|
|
|
// Kanji doesn't support Chinese, etc.
|
|
{ "zh-Hani", "Hant", NOT_SUPPORTED },
|
|
{ "zh-Hani", "Hans", NOT_SUPPORTED },
|
|
{ "zh-Hani", "Hanb", NOT_SUPPORTED },
|
|
|
|
// Hangul doesn't support Korean, etc.
|
|
{ "ko-Hang", "Kore", NOT_SUPPORTED },
|
|
{ "ko-Hani", "Kore", NOT_SUPPORTED },
|
|
{ "ko-Hani", "Hang", NOT_SUPPORTED },
|
|
{ "ko-Hang", "Hani", NOT_SUPPORTED },
|
|
|
|
// Han with botomofo doesn't support simplified Chinese, etc.
|
|
{ "zh-Hanb", "Hant", NOT_SUPPORTED },
|
|
{ "zh-Hanb", "Hans", NOT_SUPPORTED },
|
|
{ "zh-Hanb", "Jpan", NOT_SUPPORTED },
|
|
{ "zh-Hanb", "Kore", NOT_SUPPORTED },
|
|
};
|
|
|
|
for (auto testCase : testCases) {
|
|
hb_script_t script = hb_script_from_iso15924_tag(
|
|
HB_TAG(testCase.requestedScript[0], testCase.requestedScript[1],
|
|
testCase.requestedScript[2], testCase.requestedScript[3]));
|
|
if (testCase.isSupported) {
|
|
EXPECT_TRUE(
|
|
createFontLanguage(testCase.baseScript).supportsHbScript(script))
|
|
<< testCase.baseScript << " should support " << testCase.requestedScript;
|
|
} else {
|
|
EXPECT_FALSE(
|
|
createFontLanguage(testCase.baseScript).supportsHbScript(script))
|
|
<< testCase.baseScript << " shouldn't support " << testCase.requestedScript;
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_F(FontLanguagesTest, basicTests) {
|
|
FontLanguages emptyLangs;
|
|
EXPECT_EQ(0u, emptyLangs.size());
|
|
|
|
FontLanguage english = createFontLanguage("en");
|
|
const FontLanguages& singletonLangs = createFontLanguages("en");
|
|
EXPECT_EQ(1u, singletonLangs.size());
|
|
EXPECT_EQ(english, singletonLangs[0]);
|
|
|
|
FontLanguage french = createFontLanguage("fr");
|
|
const FontLanguages& twoLangs = createFontLanguages("en,fr");
|
|
EXPECT_EQ(2u, twoLangs.size());
|
|
EXPECT_EQ(english, twoLangs[0]);
|
|
EXPECT_EQ(french, twoLangs[1]);
|
|
}
|
|
|
|
TEST_F(FontLanguagesTest, unsupportedLanguageTests) {
|
|
const FontLanguages& oneUnsupported = createFontLanguages("abcd-example");
|
|
EXPECT_TRUE(oneUnsupported.empty());
|
|
|
|
const FontLanguages& twoUnsupporteds = createFontLanguages("abcd-example,abcd-example");
|
|
EXPECT_TRUE(twoUnsupporteds.empty());
|
|
|
|
FontLanguage english = createFontLanguage("en");
|
|
const FontLanguages& firstUnsupported = createFontLanguages("abcd-example,en");
|
|
EXPECT_EQ(1u, firstUnsupported.size());
|
|
EXPECT_EQ(english, firstUnsupported[0]);
|
|
|
|
const FontLanguages& lastUnsupported = createFontLanguages("en,abcd-example");
|
|
EXPECT_EQ(1u, lastUnsupported.size());
|
|
EXPECT_EQ(english, lastUnsupported[0]);
|
|
}
|
|
|
|
TEST_F(FontLanguagesTest, repeatedLanguageTests) {
|
|
FontLanguage english = createFontLanguage("en");
|
|
FontLanguage french = createFontLanguage("fr");
|
|
FontLanguage englishInLatn = createFontLanguage("en-Latn");
|
|
ASSERT_TRUE(english == englishInLatn);
|
|
|
|
const FontLanguages& langs = createFontLanguages("en,en-Latn");
|
|
EXPECT_EQ(1u, langs.size());
|
|
EXPECT_EQ(english, langs[0]);
|
|
|
|
// Country codes are ignored.
|
|
const FontLanguages& fr = createFontLanguages("fr,fr-CA,fr-FR");
|
|
EXPECT_EQ(1u, fr.size());
|
|
EXPECT_EQ(french, fr[0]);
|
|
|
|
// The order should be kept.
|
|
const FontLanguages& langs2 = createFontLanguages("en,fr,en-Latn");
|
|
EXPECT_EQ(2u, langs2.size());
|
|
EXPECT_EQ(english, langs2[0]);
|
|
EXPECT_EQ(french, langs2[1]);
|
|
}
|
|
|
|
TEST_F(FontLanguagesTest, undEmojiTests) {
|
|
FontLanguage emoji = createFontLanguage("und-Zsye");
|
|
EXPECT_TRUE(emoji.hasEmojiFlag());
|
|
|
|
FontLanguage und = createFontLanguage("und");
|
|
EXPECT_FALSE(und.hasEmojiFlag());
|
|
EXPECT_FALSE(emoji == und);
|
|
|
|
FontLanguage undExample = createFontLanguage("und-example");
|
|
EXPECT_FALSE(undExample.hasEmojiFlag());
|
|
EXPECT_FALSE(emoji == undExample);
|
|
}
|
|
|
|
TEST_F(FontLanguagesTest, registerLanguageListTest) {
|
|
EXPECT_EQ(0UL, FontStyle::registerLanguageList(""));
|
|
EXPECT_NE(0UL, FontStyle::registerLanguageList("en"));
|
|
EXPECT_NE(0UL, FontStyle::registerLanguageList("jp"));
|
|
EXPECT_NE(0UL, FontStyle::registerLanguageList("en,zh-Hans"));
|
|
|
|
EXPECT_EQ(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("en"));
|
|
EXPECT_NE(FontStyle::registerLanguageList("en"), FontStyle::registerLanguageList("jp"));
|
|
|
|
EXPECT_EQ(FontStyle::registerLanguageList("en,zh-Hans"),
|
|
FontStyle::registerLanguageList("en,zh-Hans"));
|
|
EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
|
|
FontStyle::registerLanguageList("zh-Hans,en"));
|
|
EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
|
|
FontStyle::registerLanguageList("jp"));
|
|
EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
|
|
FontStyle::registerLanguageList("en"));
|
|
EXPECT_NE(FontStyle::registerLanguageList("en,zh-Hans"),
|
|
FontStyle::registerLanguageList("en,zh-Hant"));
|
|
}
|
|
|
|
// 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+FE00 (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)
|
|
const char kVsTestFont[] = kTestFontDir "VarioationSelectorTest-Regular.ttf";
|
|
|
|
class FontFamilyTest : public ICUTestBase {
|
|
public:
|
|
virtual void SetUp() override {
|
|
ICUTestBase::SetUp();
|
|
if (access(kVsTestFont, R_OK) != 0) {
|
|
FAIL() << "Unable to read " << kVsTestFont << ". "
|
|
<< "Please prepare the test data directory. "
|
|
<< "For more details, please see how_to_run.txt.";
|
|
}
|
|
}
|
|
};
|
|
|
|
// Asserts that the font family has glyphs for and only for specified codepoint
|
|
// and variationSelector pairs.
|
|
void expectVSGlyphs(FontFamily* family, uint32_t codepoint, const std::set<uint32_t>& vs) {
|
|
for (uint32_t i = 0xFE00; i <= 0xE01EF; ++i) {
|
|
// Move to variation selectors supplements after variation selectors.
|
|
if (i == 0xFF00) {
|
|
i = 0xE0100;
|
|
}
|
|
if (vs.find(i) == vs.end()) {
|
|
EXPECT_FALSE(family->hasVariationSelector(codepoint, i))
|
|
<< "Glyph for U+" << std::hex << codepoint << " U+" << i;
|
|
} else {
|
|
EXPECT_TRUE(family->hasVariationSelector(codepoint, i))
|
|
<< "Glyph for U+" << std::hex << codepoint << " U+" << i;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
TEST_F(FontFamilyTest, hasVariationSelectorTest) {
|
|
MinikinFontForTest minikinFont(kVsTestFont);
|
|
FontFamily family;
|
|
family.addFont(&minikinFont);
|
|
|
|
AutoMutex _l(gMinikinLock);
|
|
|
|
const uint32_t kVS1 = 0xFE00;
|
|
const uint32_t kVS2 = 0xFE01;
|
|
const uint32_t kVS3 = 0xFE02;
|
|
const uint32_t kVS17 = 0xE0100;
|
|
const uint32_t kVS18 = 0xE0101;
|
|
const uint32_t kVS19 = 0xE0102;
|
|
const uint32_t kVS20 = 0xE0103;
|
|
|
|
const uint32_t kSupportedChar1 = 0x82A6;
|
|
EXPECT_TRUE(family.getCoverage()->get(kSupportedChar1));
|
|
expectVSGlyphs(&family, kSupportedChar1, std::set<uint32_t>({kVS1, kVS17, kVS18, kVS19}));
|
|
|
|
const uint32_t kSupportedChar2 = 0x845B;
|
|
EXPECT_TRUE(family.getCoverage()->get(kSupportedChar2));
|
|
expectVSGlyphs(&family, kSupportedChar2, std::set<uint32_t>({kVS2, kVS18, kVS19, kVS20}));
|
|
|
|
const uint32_t kNoVsSupportedChar = 0x537F;
|
|
EXPECT_TRUE(family.getCoverage()->get(kNoVsSupportedChar));
|
|
expectVSGlyphs(&family, kNoVsSupportedChar, std::set<uint32_t>());
|
|
|
|
const uint32_t kVsOnlySupportedChar = 0x717D;
|
|
EXPECT_FALSE(family.getCoverage()->get(kVsOnlySupportedChar));
|
|
expectVSGlyphs(&family, kVsOnlySupportedChar, std::set<uint32_t>({kVS3, kVS19, kVS20}));
|
|
|
|
const uint32_t kNotSupportedChar = 0x845C;
|
|
EXPECT_FALSE(family.getCoverage()->get(kNotSupportedChar));
|
|
expectVSGlyphs(&family, kNotSupportedChar, std::set<uint32_t>());
|
|
}
|
|
|
|
TEST_F(FontFamilyTest, hasVSTableTest) {
|
|
struct TestCase {
|
|
const std::string fontPath;
|
|
bool hasVSTable;
|
|
} testCases[] = {
|
|
{ kTestFontDir "Ja.ttf", true },
|
|
{ kTestFontDir "ZhHant.ttf", true },
|
|
{ kTestFontDir "ZhHans.ttf", true },
|
|
{ kTestFontDir "Italic.ttf", false },
|
|
{ kTestFontDir "Bold.ttf", false },
|
|
{ kTestFontDir "BoldItalic.ttf", false },
|
|
};
|
|
|
|
for (auto testCase : testCases) {
|
|
SCOPED_TRACE(testCase.hasVSTable ?
|
|
"Font " + testCase.fontPath + " should have a variation sequence table." :
|
|
"Font " + testCase.fontPath + " shouldn't have a variation sequence table.");
|
|
|
|
MinikinFontForTest minikinFont(testCase.fontPath);
|
|
FontFamily family;
|
|
family.addFont(&minikinFont);
|
|
family.getCoverage();
|
|
|
|
EXPECT_EQ(testCase.hasVSTable, family.hasVSTable());
|
|
}
|
|
}
|
|
|
|
} // namespace android
|