2013-04-02 14:12:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFontMgr.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "src/sfnt/SkOTTable_name.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2013-04-02 14:12:38 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-04-02 14:12:38 +00:00
|
|
|
template <size_t R, size_t D> struct Format0NameTable {
|
|
|
|
SkOTTableName header;
|
|
|
|
SkOTTableName::Record nameRecord[R];
|
|
|
|
char data[D];
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t R, size_t L, size_t D> struct Format1NameTable {
|
|
|
|
SkOTTableName header;
|
|
|
|
SkOTTableName::Record nameRecord[R];
|
|
|
|
struct {
|
|
|
|
SkOTTableName::Format1Ext header;
|
|
|
|
SkOTTableName::Format1Ext::LangTagRecord langTagRecord[L];
|
|
|
|
} format1ext;
|
|
|
|
char data[D];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Format0NameTable<1, 9> SimpleFormat0NameTable;
|
2018-08-20 17:45:57 +00:00
|
|
|
constexpr SimpleFormat0NameTable simpleFormat0NameTable = {
|
2013-04-02 14:12:38 +00:00
|
|
|
/*header*/ {
|
|
|
|
/*format*/ SkOTTableName::format_0,
|
|
|
|
/*count*/ SkTEndianSwap16<1>::value,
|
|
|
|
/*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat0NameTable, data)>::value,
|
|
|
|
},
|
|
|
|
/*nameRecord[]*/ {
|
|
|
|
/*Record*/ {
|
|
|
|
/*platformID*/ { SkOTTableName::Record::PlatformID::Windows },
|
|
|
|
/*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 },
|
|
|
|
/*languageID*/ { SkOTTableName::Record::LanguageID::Windows::English_UnitedStates },
|
|
|
|
/*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName },
|
|
|
|
/*length*/ SkTEndianSwap16<8>::value,
|
|
|
|
/*offset*/ SkTEndianSwap16<0>::value,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t",
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Format1NameTable<1, 1, 19> SimpleFormat1NameTable;
|
2018-08-20 17:45:57 +00:00
|
|
|
constexpr SimpleFormat1NameTable simpleFormat1NameTable = {
|
2013-04-02 14:12:38 +00:00
|
|
|
/*header*/ {
|
|
|
|
/*format*/ SkOTTableName::format_1,
|
|
|
|
/*count*/ SkTEndianSwap16<1>::value,
|
|
|
|
/*stringOffset*/ SkTEndianSwap16<offsetof(SimpleFormat1NameTable, data)>::value,
|
|
|
|
},
|
|
|
|
/*nameRecord[]*/ {
|
|
|
|
/*Record*/ {
|
|
|
|
/*platformID*/ { SkOTTableName::Record::PlatformID::Windows },
|
|
|
|
/*encodingID*/ { SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2 },
|
|
|
|
/*languageID*/ { SkTEndianSwap16<0x8000 + 0>::value },
|
|
|
|
/*nameID*/ { SkOTTableName::Record::NameID::Predefined::FontFamilyName },
|
|
|
|
/*length*/ SkTEndianSwap16<8>::value,
|
|
|
|
/*offset*/ SkTEndianSwap16<0>::value,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/*format1ext*/ {
|
|
|
|
/*header*/ {
|
|
|
|
/*langTagCount*/ SkTEndianSwap16<1>::value,
|
|
|
|
},
|
|
|
|
/*langTagRecord[]*/ {
|
|
|
|
/*LangTagRecord*/ {
|
|
|
|
/*length*/ SkTEndianSwap16<10>::value,
|
|
|
|
/*offset*/ SkTEndianSwap16<8>::value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/*data*/ "\x0" "T" "\x0" "e" "\x0" "s" "\x0" "t"
|
|
|
|
"\x0" "e" "\x0" "n" "\x0" "-" "\x0" "U" "\x0" "S",
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FontNamesTest {
|
2018-08-20 17:45:57 +00:00
|
|
|
const uint8_t* data;
|
|
|
|
size_t size;
|
2013-04-02 14:12:38 +00:00
|
|
|
SkOTTableName::Record::NameID nameID;
|
|
|
|
size_t nameCount;
|
|
|
|
struct {
|
|
|
|
const char* name;
|
|
|
|
const char* language;
|
|
|
|
} names[10];
|
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
} tests[] = {
|
2013-04-02 14:12:38 +00:00
|
|
|
{
|
2018-08-20 17:45:57 +00:00
|
|
|
reinterpret_cast<const uint8_t*>(&simpleFormat0NameTable),
|
|
|
|
sizeof(simpleFormat0NameTable),
|
2013-04-02 14:12:38 +00:00
|
|
|
{ SkOTTableName::Record::NameID::Predefined::FontFamilyName },
|
|
|
|
1,
|
|
|
|
{
|
|
|
|
{ "Test", "en-US" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-08-20 17:45:57 +00:00
|
|
|
reinterpret_cast<const uint8_t*>(&simpleFormat1NameTable),
|
|
|
|
sizeof(simpleFormat1NameTable),
|
2013-04-02 14:12:38 +00:00
|
|
|
{ SkOTTableName::Record::NameID::Predefined::FontFamilyName },
|
|
|
|
1,
|
|
|
|
{
|
|
|
|
{ "Test", "en-US" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-08-07 02:45:25 +00:00
|
|
|
static void test_synthetic(skiatest::Reporter* reporter, bool verbose) {
|
2018-08-20 17:45:57 +00:00
|
|
|
for (const auto& test : tests) {
|
|
|
|
SkOTTableName::Iterator iter(test.data, test.size, test.nameID.predefined.value);
|
2013-04-02 14:12:38 +00:00
|
|
|
SkOTTableName::Iterator::Record record;
|
|
|
|
size_t nameIndex = 0;
|
2018-08-20 17:45:57 +00:00
|
|
|
while (nameIndex < test.nameCount && iter.next(record)) {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
2018-08-20 17:45:57 +00:00
|
|
|
strcmp(test.names[nameIndex].name, record.name.c_str()) == 0,
|
2018-01-29 14:50:47 +00:00
|
|
|
"Name did not match.");
|
2013-04-02 14:12:38 +00:00
|
|
|
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
2018-08-20 17:45:57 +00:00
|
|
|
strcmp(test.names[nameIndex].language, record.language.c_str()) == 0,
|
2018-01-29 14:50:47 +00:00
|
|
|
"Language did not match.");
|
2013-04-02 14:12:38 +00:00
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
if (verbose) {
|
|
|
|
SkDebugf("%s <%s>\n", record.name.c_str(), record.language.c_str());
|
|
|
|
}
|
2013-04-02 14:12:38 +00:00
|
|
|
|
|
|
|
++nameIndex;
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
REPORTER_ASSERT(reporter, nameIndex == test.nameCount, "Fewer names than expected.");
|
2013-04-02 14:12:38 +00:00
|
|
|
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, !iter.next(record), "More names than expected.");
|
2013-04-02 14:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 02:45:25 +00:00
|
|
|
#define MAX_FAMILIES 1000
|
|
|
|
static void test_systemfonts(skiatest::Reporter* reporter, bool verbose) {
|
|
|
|
static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e');
|
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
|
2013-08-07 02:45:25 +00:00
|
|
|
int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
|
2013-08-07 02:45:25 +00:00
|
|
|
for (int j = 0; j < set->count(); ++j) {
|
|
|
|
SkString sname;
|
|
|
|
SkFontStyle fs;
|
|
|
|
set->getStyle(j, &fs, &sname);
|
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkTypeface> typeface(set->createTypeface(j));
|
2013-08-07 02:45:25 +00:00
|
|
|
|
|
|
|
SkString familyName;
|
|
|
|
typeface->getFamilyName(&familyName);
|
|
|
|
if (verbose) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("[%s]\n", familyName.c_str());
|
2013-08-07 02:45:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkTypeface::LocalizedStrings> familyNamesIter(
|
2013-08-07 17:09:22 +00:00
|
|
|
typeface->createFamilyNameIterator());
|
2013-08-07 02:45:25 +00:00
|
|
|
SkTypeface::LocalizedString familyNameLocalized;
|
|
|
|
while (familyNamesIter->next(&familyNameLocalized)) {
|
|
|
|
if (verbose) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("(%s) <%s>\n", familyNameLocalized.fString.c_str(),
|
2014-08-25 19:00:49 +00:00
|
|
|
familyNameLocalized.fLanguage.c_str());
|
2013-08-07 02:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nameTableSize = typeface->getTableSize(nameTag);
|
|
|
|
if (0 == nameTableSize) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkAutoTMalloc<uint8_t> nameTableData(nameTableSize);
|
|
|
|
size_t copied = typeface->getTableData(nameTag, 0, nameTableSize, nameTableData.get());
|
|
|
|
if (copied != nameTableSize) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkOTTableName::Iterator::Record record;
|
2018-08-20 17:45:57 +00:00
|
|
|
SkOTTableName::Iterator familyNameIter(nameTableData.get(), nameTableSize,
|
2013-08-07 02:45:25 +00:00
|
|
|
SkOTTableName::Record::NameID::Predefined::FontFamilyName);
|
|
|
|
while (familyNameIter.next(record)) {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(
|
|
|
|
reporter,
|
|
|
|
SkOTTableName::Record::NameID::Predefined::FontFamilyName == record.type,
|
|
|
|
"Requested family name, got something else.");
|
2013-08-07 02:45:25 +00:00
|
|
|
if (verbose) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("{%s} <%s>\n", record.name.c_str(), record.language.c_str());
|
2013-08-07 02:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
SkOTTableName::Iterator styleNameIter(nameTableData.get(), nameTableSize,
|
2013-08-07 02:45:25 +00:00
|
|
|
SkOTTableName::Record::NameID::Predefined::FontSubfamilyName);
|
|
|
|
while (styleNameIter.next(record)) {
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(
|
|
|
|
reporter,
|
|
|
|
SkOTTableName::Record::NameID::Predefined::FontSubfamilyName == record.type,
|
|
|
|
"Requested subfamily name, got something else.");
|
2013-08-07 02:45:25 +00:00
|
|
|
if (verbose) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("{{%s}} <%s>\n", record.name.c_str(), record.language.c_str());
|
2013-08-07 02:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("\n");
|
2013-08-07 02:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:45:57 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-03-21 16:31:36 +00:00
|
|
|
static DEFINE_bool(verboseFontNames, false, "verbose FontNames test.");
|
2013-08-07 02:45:25 +00:00
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(FontNames, reporter) {
|
2013-08-07 02:45:25 +00:00
|
|
|
test_synthetic(reporter, FLAGS_verboseFontNames);
|
|
|
|
test_systemfonts(reporter, FLAGS_verboseFontNames);
|
|
|
|
}
|