2012-02-23 14:51:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "src/core/SkEndian.h"
|
|
|
|
#include "src/core/SkFontStream.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/Resources.h"
|
2012-02-23 14:51:10 +00:00
|
|
|
|
|
|
|
//#define DUMP_TABLES
|
2013-03-11 20:13:36 +00:00
|
|
|
//#define DUMP_TTC_TABLES
|
2012-02-23 14:51:10 +00:00
|
|
|
|
|
|
|
#define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
|
|
|
|
#define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
|
|
|
|
#define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p')
|
|
|
|
|
|
|
|
static const struct TagSize {
|
|
|
|
SkFontTableTag fTag;
|
|
|
|
size_t fSize;
|
|
|
|
} gKnownTableSizes[] = {
|
|
|
|
{ kFontTableTag_head, 54 },
|
|
|
|
{ kFontTableTag_hhea, 36 },
|
|
|
|
};
|
|
|
|
|
2013-03-21 15:20:00 +00:00
|
|
|
// Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
|
2013-07-15 19:42:57 +00:00
|
|
|
// (if that table is available).
|
2016-05-12 17:09:30 +00:00
|
|
|
static void test_unitsPerEm(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
|
2013-07-15 19:42:57 +00:00
|
|
|
int nativeUPEM = face->getUnitsPerEm();
|
2012-07-31 17:24:44 +00:00
|
|
|
|
2013-03-21 15:20:00 +00:00
|
|
|
int tableUPEM = -1;
|
2012-07-31 17:24:44 +00:00
|
|
|
size_t size = face->getTableSize(kFontTableTag_head);
|
|
|
|
if (size) {
|
|
|
|
// unitsPerEm is at offset 18 into the 'head' table.
|
2013-07-15 19:42:57 +00:00
|
|
|
uint16_t rawUPEM;
|
|
|
|
face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
|
|
|
|
tableUPEM = SkEndian_SwapBE16(rawUPEM);
|
2013-03-21 15:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tableUPEM >= 0) {
|
|
|
|
REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
|
2012-07-31 17:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 19:42:57 +00:00
|
|
|
// Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
|
|
|
|
// (if that table is available).
|
2016-05-12 17:09:30 +00:00
|
|
|
static void test_countGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
|
2013-07-15 19:42:57 +00:00
|
|
|
int nativeGlyphs = face->countGlyphs();
|
|
|
|
|
|
|
|
int tableGlyphs = -1;
|
|
|
|
size_t size = face->getTableSize(kFontTableTag_maxp);
|
|
|
|
if (size) {
|
|
|
|
// glyphs is at offset 4 into the 'maxp' table.
|
|
|
|
uint16_t rawGlyphs;
|
|
|
|
face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
|
|
|
|
tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tableGlyphs >= 0) {
|
|
|
|
REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:49:08 +00:00
|
|
|
|
2015-04-30 21:12:58 +00:00
|
|
|
static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream, int ttcIndex) {
|
2015-08-27 14:41:13 +00:00
|
|
|
int n = SkFontStream::GetTableTags(stream, ttcIndex, nullptr);
|
2013-03-11 20:13:36 +00:00
|
|
|
SkAutoTArray<SkFontTableTag> array(n);
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-03-11 20:13:36 +00:00
|
|
|
int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
|
|
|
|
REPORTER_ASSERT(reporter, n == n2);
|
2013-03-12 07:12:32 +00:00
|
|
|
|
2013-03-11 20:13:36 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
#ifdef DUMP_TTC_TABLES
|
|
|
|
SkString str;
|
|
|
|
SkFontTableTag t = array[i];
|
|
|
|
str.appendUnichar((t >> 24) & 0xFF);
|
|
|
|
str.appendUnichar((t >> 16) & 0xFF);
|
|
|
|
str.appendUnichar((t >> 8) & 0xFF);
|
|
|
|
str.appendUnichar((t >> 0) & 0xFF);
|
|
|
|
SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
|
|
|
|
#endif
|
|
|
|
size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
|
|
|
|
if (gKnownTableSizes[j].fTag == array[i]) {
|
|
|
|
REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
|
|
|
|
}
|
2013-03-12 07:12:32 +00:00
|
|
|
}
|
2013-03-11 20:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 21:12:58 +00:00
|
|
|
static void test_fontstream(skiatest::Reporter* reporter) {
|
2017-12-08 19:25:14 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("fonts/test.ttc"));
|
2015-04-30 21:12:58 +00:00
|
|
|
if (!stream) {
|
|
|
|
SkDebugf("Skipping FontHostTest::test_fontstream\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
int count = SkFontStream::CountTTCEntries(stream.get());
|
2013-03-11 20:13:36 +00:00
|
|
|
#ifdef DUMP_TTC_TABLES
|
|
|
|
SkDebugf("CountTTCEntries %d\n", count);
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2016-11-03 18:40:50 +00:00
|
|
|
test_fontstream(reporter, stream.get(), i);
|
2013-03-11 20:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 15:22:39 +00:00
|
|
|
// Exercise this rare cmap format (platform 3, encoding 0)
|
2015-06-05 20:38:12 +00:00
|
|
|
static void test_symbolfont(skiatest::Reporter* reporter) {
|
2018-12-13 15:22:39 +00:00
|
|
|
auto tf = MakeResourceAsTypeface("fonts/SpiderSymbol.ttf");
|
|
|
|
if (tf) {
|
|
|
|
SkUnichar c = 0xf021;
|
|
|
|
uint16_t g = SkFont(tf).unicharToGlyph(c);
|
|
|
|
REPORTER_ASSERT(reporter, g == 3);
|
|
|
|
} else {
|
|
|
|
// not all platforms support data fonts, so we just note that failure
|
2016-05-12 17:09:30 +00:00
|
|
|
SkDebugf("Skipping FontHostTest::test_symbolfont\n");
|
|
|
|
}
|
2015-06-05 20:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
static void test_tables(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((false)) { // avoid bit rot, suppress warning
|
2022-02-07 17:12:45 +00:00
|
|
|
SkTypefaceID typefaceID = face->uniqueID();
|
|
|
|
REPORTER_ASSERT(reporter, typefaceID);
|
2012-06-06 12:03:39 +00:00
|
|
|
}
|
2012-02-23 14:51:10 +00:00
|
|
|
|
2012-04-19 18:52:39 +00:00
|
|
|
int count = face->countTables();
|
2012-02-23 14:51:10 +00:00
|
|
|
|
|
|
|
SkAutoTMalloc<SkFontTableTag> storage(count);
|
|
|
|
SkFontTableTag* tags = storage.get();
|
2012-02-23 16:15:58 +00:00
|
|
|
|
2012-04-19 18:52:39 +00:00
|
|
|
int count2 = face->getTableTags(tags);
|
2012-02-23 16:15:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, count2 == count);
|
2012-02-23 14:51:10 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
2012-04-19 18:52:39 +00:00
|
|
|
size_t size = face->getTableSize(tags[i]);
|
2012-02-23 14:51:10 +00:00
|
|
|
REPORTER_ASSERT(reporter, size > 0);
|
|
|
|
|
|
|
|
#ifdef DUMP_TABLES
|
|
|
|
char name[5];
|
|
|
|
name[0] = (tags[i] >> 24) & 0xFF;
|
|
|
|
name[1] = (tags[i] >> 16) & 0xFF;
|
|
|
|
name[2] = (tags[i] >> 8) & 0xFF;
|
|
|
|
name[3] = (tags[i] >> 0) & 0xFF;
|
|
|
|
name[4] = 0;
|
|
|
|
SkDebugf("%s %d\n", name, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
|
|
|
|
if (gKnownTableSizes[j].fTag == tags[i]) {
|
|
|
|
REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-02-23 16:15:58 +00:00
|
|
|
// do we get the same size from GetTableData and GetTableSize
|
|
|
|
{
|
|
|
|
SkAutoMalloc data(size);
|
2012-04-19 18:52:39 +00:00
|
|
|
size_t size2 = face->getTableData(tags[i], 0, size, data.get());
|
2012-02-23 16:15:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, size2 == size);
|
2019-07-24 18:51:34 +00:00
|
|
|
sk_sp<SkData> data2 = face->copyTableData(tags[i]);
|
|
|
|
REPORTER_ASSERT(reporter, size == data2->size());
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(data.get(), data2->data(), size));
|
2012-02-23 16:15:58 +00:00
|
|
|
}
|
2012-02-23 14:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_tables(skiatest::Reporter* reporter) {
|
|
|
|
static const char* const gNames[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr, // default font
|
2014-09-24 18:04:41 +00:00
|
|
|
"Helvetica", "Arial",
|
|
|
|
"Times", "Times New Roman",
|
|
|
|
"Courier", "Courier New",
|
|
|
|
"Terminal", "MS Sans Serif",
|
|
|
|
"Hiragino Mincho ProN", "MS PGothic",
|
2012-02-23 14:51:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> face(SkTypeface::MakeFromName(gNames[i], SkFontStyle()));
|
2012-02-23 14:51:10 +00:00
|
|
|
if (face) {
|
|
|
|
#ifdef DUMP_TABLES
|
|
|
|
SkDebugf("%s\n", gNames[i]);
|
|
|
|
#endif
|
|
|
|
test_tables(reporter, face);
|
2012-07-31 17:24:44 +00:00
|
|
|
test_unitsPerEm(reporter, face);
|
2013-07-15 19:42:57 +00:00
|
|
|
test_countGlyphs(reporter, face);
|
2012-02-23 14:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:11:47 +00:00
|
|
|
/*
|
|
|
|
* Verifies that the advance values returned by generateAdvance and
|
|
|
|
* generateMetrics match.
|
|
|
|
*/
|
|
|
|
static void test_advances(skiatest::Reporter* reporter) {
|
|
|
|
static const char* const faces[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr, // default font
|
2012-03-23 18:11:47 +00:00
|
|
|
"Arial", "Times", "Times New Roman", "Helvetica", "Courier",
|
|
|
|
"Courier New", "Verdana", "monospace",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
2018-11-08 00:54:33 +00:00
|
|
|
SkFontHinting hinting;
|
2019-01-10 18:55:35 +00:00
|
|
|
bool linear;
|
|
|
|
bool subpixel;
|
2012-03-23 18:11:47 +00:00
|
|
|
} settings[] = {
|
2019-05-07 20:50:29 +00:00
|
|
|
{ SkFontHinting::kNone, false, false },
|
|
|
|
{ SkFontHinting::kNone, true, false },
|
|
|
|
{ SkFontHinting::kNone, false, true },
|
|
|
|
{ SkFontHinting::kSlight, false, false },
|
|
|
|
{ SkFontHinting::kSlight, true, false },
|
|
|
|
{ SkFontHinting::kSlight, false, true },
|
|
|
|
{ SkFontHinting::kNormal, false, false },
|
|
|
|
{ SkFontHinting::kNormal, true, false },
|
|
|
|
{ SkFontHinting::kNormal, false, true },
|
2012-03-23 18:11:47 +00:00
|
|
|
};
|
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
static const struct {
|
|
|
|
SkScalar fScaleX;
|
|
|
|
SkScalar fSkewX;
|
|
|
|
} gScaleRec[] = {
|
|
|
|
{ SK_Scalar1, 0 },
|
|
|
|
{ SK_Scalar1/2, 0 },
|
|
|
|
// these two exercise obliquing (skew)
|
|
|
|
{ SK_Scalar1, -SK_Scalar1/4 },
|
|
|
|
{ SK_Scalar1/2, -SK_Scalar1/4 },
|
|
|
|
};
|
|
|
|
|
2018-12-13 15:22:39 +00:00
|
|
|
SkFont font;
|
2019-05-07 19:38:46 +00:00
|
|
|
char const * const txt = "long.text.with.lots.of.dots.";
|
|
|
|
size_t textLen = strlen(txt);
|
2012-03-23 18:11:47 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
|
2018-12-13 15:22:39 +00:00
|
|
|
font.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle()));
|
2012-03-23 18:11:47 +00:00
|
|
|
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
|
2018-12-13 15:22:39 +00:00
|
|
|
font.setHinting(settings[j].hinting);
|
2019-01-10 18:55:35 +00:00
|
|
|
font.setLinearMetrics(settings[j].linear);
|
|
|
|
font.setSubpixel(settings[j].subpixel);
|
2012-03-23 18:11:47 +00:00
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
|
2018-12-13 15:22:39 +00:00
|
|
|
font.setScaleX(gScaleRec[k].fScaleX);
|
|
|
|
font.setSkewX(gScaleRec[k].fSkewX);
|
2012-03-23 18:11:47 +00:00
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
SkRect bounds;
|
2012-03-23 18:11:47 +00:00
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
// For no hinting and light hinting this should take the
|
|
|
|
// optimized generateAdvance path.
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar width1 = font.measureText(txt, textLen, SkTextEncoding::kUTF8);
|
2012-03-23 18:11:47 +00:00
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
// Requesting the bounds forces a generateMetrics call.
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar width2 = font.measureText(txt, textLen, SkTextEncoding::kUTF8, &bounds);
|
2012-03-23 18:11:47 +00:00
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
// SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
|
|
|
|
// faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, width1 == width2);
|
|
|
|
}
|
2012-03-23 18:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(FontHost, reporter) {
|
2012-02-23 14:51:10 +00:00
|
|
|
test_tables(reporter);
|
2013-03-11 20:13:36 +00:00
|
|
|
test_fontstream(reporter);
|
2012-03-23 18:11:47 +00:00
|
|
|
test_advances(reporter);
|
2015-06-05 20:38:12 +00:00
|
|
|
test_symbolfont(reporter);
|
2012-02-23 14:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// need tests for SkStrSearch
|