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.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-06-18 21:32:48 +00:00
|
|
|
|
#include "Resources.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
|
#include "SkEndian.h"
|
2013-03-11 20:13:36 +00:00
|
|
|
|
#include "SkFontStream.h"
|
2013-10-24 21:39:35 +00:00
|
|
|
|
#include "SkOSFile.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
|
#include "SkPaint.h"
|
2013-03-11 20:13:36 +00:00
|
|
|
|
#include "SkStream.h"
|
2012-02-23 14:51:10 +00:00
|
|
|
|
#include "SkTypeface.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
|
#include "Test.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
|
|
|
|
|
2013-10-24 21:39:35 +00:00
|
|
|
|
// The following three are all the same code points in various encodings.
|
2014-09-24 18:04:41 +00:00
|
|
|
|
// a中Яיו𝄞a𠮟
|
|
|
|
|
static uint8_t utf8Chars[] = { 0x61, 0xE4,0xB8,0xAD, 0xD0,0xAF, 0xD7,0x99, 0xD7,0x95, 0xF0,0x9D,0x84,0x9E, 0x61, 0xF0,0xA0,0xAE,0x9F };
|
|
|
|
|
static uint16_t utf16Chars[] = { 0x0061, 0x4E2D, 0x042F, 0x05D9, 0x05D5, 0xD834,0xDD1E, 0x0061, 0xD842,0xDF9F };
|
|
|
|
|
static uint32_t utf32Chars[] = { 0x00000061, 0x00004E2D, 0x0000042F, 0x000005D9, 0x000005D5, 0x0001D11E, 0x00000061, 0x00020B9F };
|
2013-10-24 21:39:35 +00:00
|
|
|
|
|
|
|
|
|
struct CharsToGlyphs_TestData {
|
|
|
|
|
const void* chars;
|
|
|
|
|
int charCount;
|
|
|
|
|
size_t charsByteLength;
|
|
|
|
|
SkTypeface::Encoding typefaceEncoding;
|
|
|
|
|
const char* name;
|
|
|
|
|
} static charsToGlyphs_TestData[] = {
|
2014-09-24 18:04:41 +00:00
|
|
|
|
{ utf8Chars, 8, sizeof(utf8Chars), SkTypeface::kUTF8_Encoding, "Simple UTF-8" },
|
|
|
|
|
{ utf16Chars, 8, sizeof(utf16Chars), SkTypeface::kUTF16_Encoding, "Simple UTF-16" },
|
|
|
|
|
{ utf32Chars, 8, sizeof(utf32Chars), SkTypeface::kUTF32_Encoding, "Simple UTF-32" },
|
2013-10-24 21:39:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Test that SkPaint::textToGlyphs agrees with SkTypeface::charsToGlyphs.
|
2016-05-12 17:09:30 +00:00
|
|
|
|
static void test_charsToGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
|
2013-10-24 21:39:35 +00:00
|
|
|
|
uint16_t paintGlyphIds[256];
|
|
|
|
|
uint16_t faceGlyphIds[256];
|
|
|
|
|
|
|
|
|
|
for (size_t testIndex = 0; testIndex < SK_ARRAY_COUNT(charsToGlyphs_TestData); ++testIndex) {
|
|
|
|
|
CharsToGlyphs_TestData& test = charsToGlyphs_TestData[testIndex];
|
|
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
paint.setTypeface(face);
|
|
|
|
|
paint.setTextEncoding((SkPaint::TextEncoding)test.typefaceEncoding);
|
|
|
|
|
paint.textToGlyphs(test.chars, test.charsByteLength, paintGlyphIds);
|
|
|
|
|
|
|
|
|
|
face->charsToGlyphs(test.chars, test.typefaceEncoding, faceGlyphIds, test.charCount);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < test.charCount; ++i) {
|
|
|
|
|
SkString name;
|
|
|
|
|
face->getFamilyName(&name);
|
|
|
|
|
SkString a;
|
2013-10-25 07:02:14 +00:00
|
|
|
|
a.appendf("%s, paintGlyphIds[%d] = %d, faceGlyphIds[%d] = %d, face = %s",
|
2013-10-24 21:39:35 +00:00
|
|
|
|
test.name, i, (int)paintGlyphIds[i], i, (int)faceGlyphIds[i], name.c_str());
|
|
|
|
|
REPORTER_ASSERT_MESSAGE(reporter, paintGlyphIds[i] == faceGlyphIds[i], a.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream("/fonts/test.ttc"));
|
|
|
|
|
if (!stream) {
|
|
|
|
|
SkDebugf("Skipping FontHostTest::test_fontstream\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 20:13:36 +00:00
|
|
|
|
int count = SkFontStream::CountTTCEntries(stream);
|
|
|
|
|
#ifdef DUMP_TTC_TABLES
|
|
|
|
|
SkDebugf("CountTTCEntries %d\n", count);
|
|
|
|
|
#endif
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
|
test_fontstream(reporter, stream, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 20:38:12 +00:00
|
|
|
|
static void test_symbolfont(skiatest::Reporter* reporter) {
|
|
|
|
|
SkUnichar c = 0xf021;
|
|
|
|
|
uint16_t g;
|
|
|
|
|
SkPaint paint;
|
2016-05-12 17:09:30 +00:00
|
|
|
|
paint.setTypeface(MakeResourceAsTypeface("/fonts/SpiderSymbol.ttf"));
|
2015-06-05 20:38:12 +00:00
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
|
|
|
|
|
paint.textToGlyphs(&c, 4, &g);
|
2016-05-12 17:09:30 +00:00
|
|
|
|
|
|
|
|
|
if (!paint.getTypeface()) {
|
|
|
|
|
SkDebugf("Skipping FontHostTest::test_symbolfont\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-05 20:38:12 +00:00
|
|
|
|
REPORTER_ASSERT(reporter, g == 3);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 17:09:30 +00:00
|
|
|
|
static void test_tables(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
|
2012-06-06 12:03:39 +00:00
|
|
|
|
if (false) { // avoid bit rot, suppress warning
|
2013-01-30 20:33:12 +00:00
|
|
|
|
SkFontID fontID = face->uniqueID();
|
2012-06-06 12:03:39 +00:00
|
|
|
|
REPORTER_ASSERT(reporter, fontID);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
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);
|
2013-10-25 17:49:08 +00:00
|
|
|
|
test_charsToGlyphs(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 {
|
|
|
|
|
SkPaint::Hinting hinting;
|
|
|
|
|
unsigned flags;
|
|
|
|
|
} settings[] = {
|
|
|
|
|
{ SkPaint::kNo_Hinting, 0 },
|
|
|
|
|
{ SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
|
|
|
|
|
{ SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
|
|
|
|
|
{ SkPaint::kSlight_Hinting, 0 },
|
|
|
|
|
{ SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
|
|
|
|
|
{ SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
|
|
|
|
|
{ SkPaint::kNormal_Hinting, 0 },
|
|
|
|
|
{ SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
|
|
|
|
|
{ SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
|
|
|
|
|
};
|
|
|
|
|
|
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 },
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-23 18:11:47 +00:00
|
|
|
|
SkPaint paint;
|
|
|
|
|
char txt[] = "long.text.with.lots.of.dots.";
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
|
2016-05-31 18:42:36 +00:00
|
|
|
|
paint.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++) {
|
2012-07-18 13:45:58 +00:00
|
|
|
|
paint.setHinting(settings[j].hinting);
|
|
|
|
|
paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
|
|
|
|
|
paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
|
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) {
|
|
|
|
|
paint.setTextScaleX(gScaleRec[k].fScaleX);
|
|
|
|
|
paint.setTextSkewX(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.
|
|
|
|
|
SkScalar width1 = paint.measureText(txt, strlen(txt));
|
2012-03-23 18:11:47 +00:00
|
|
|
|
|
2012-07-18 13:45:58 +00:00
|
|
|
|
// Requesting the bounds forces a generateMetrics call.
|
|
|
|
|
SkScalar width2 = paint.measureText(txt, strlen(txt), &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
|