blob: afe618c201668585a7520c7728f01c24495c8313 [file] [log] [blame]
reed@google.com83165a52013-04-11 18:31:25 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@google.com83165a52013-04-11 18:31:25 +00008#include "SkCommandLineFlags.h"
9#include "SkFontMgr.h"
10#include "SkTypeface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
reed@google.com83165a52013-04-11 18:31:25 +000012
reed@google.comd44d9882013-09-18 20:32:25 +000013/*
14 * If the font backend is going to "alias" some font names to other fonts
15 * (e.g. sans -> Arial) then we want to at least get the same typeface back
16 * if we request the alias name multiple times.
17 */
reed@google.comc1a81eb2013-09-19 18:06:03 +000018static void test_alias_names(skiatest::Reporter* reporter) {
19 const char* inNames[] = {
20 "sans", "sans-serif", "serif", "monospace", "times", "helvetica"
21 };
skia.committer@gmail.comf91e3d42013-09-20 07:01:33 +000022
reed@google.comc1a81eb2013-09-19 18:06:03 +000023 for (size_t i = 0; i < SK_ARRAY_COUNT(inNames); ++i) {
24 SkAutoTUnref<SkTypeface> first(SkTypeface::CreateFromName(inNames[i],
25 SkTypeface::kNormal));
26 if (NULL == first.get()) {
27 continue;
28 }
29 for (int j = 0; j < 10; ++j) {
30 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(inNames[i],
31 SkTypeface::kNormal));
32 #if 0
33 SkString name;
34 face->getFamilyName(&name);
35 printf("request %s, received %s, first id %x received %x\n",
36 inNames[i], name.c_str(), first->uniqueID(), face->uniqueID());
37 #endif
38 REPORTER_ASSERT(reporter, first->uniqueID() == face->uniqueID());
39 }
reed@google.comd44d9882013-09-18 20:32:25 +000040 }
41}
42
reed@google.com83165a52013-04-11 18:31:25 +000043static void test_fontiter(skiatest::Reporter* reporter, bool verbose) {
44 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
45 int count = fm->countFamilies();
46
47 for (int i = 0; i < count; ++i) {
48 SkString fname;
49 fm->getFamilyName(i, &fname);
50 REPORTER_ASSERT(reporter, fname.size() > 0);
51
bungeman@google.com71033442013-05-01 14:21:20 +000052 SkAutoTUnref<SkFontStyleSet> fnset(fm->matchFamily(fname.c_str()));
reed@google.com83165a52013-04-11 18:31:25 +000053 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
bungeman@google.com71033442013-05-01 14:21:20 +000054 REPORTER_ASSERT(reporter, fnset->count() == set->count());
reed@google.com83165a52013-04-11 18:31:25 +000055
56 if (verbose) {
57 SkDebugf("[%2d] %s\n", i, fname.c_str());
58 }
59
60 for (int j = 0; j < set->count(); ++j) {
61 SkString sname;
62 SkFontStyle fs;
63 set->getStyle(j, &fs, &sname);
reed@google.comeb029572013-04-25 14:59:32 +000064// REPORTER_ASSERT(reporter, sname.size() > 0);
reed@google.com83165a52013-04-11 18:31:25 +000065
66 SkAutoTUnref<SkTypeface> face(set->createTypeface(j));
reed@google.comc1ccda32013-04-19 14:28:54 +000067// REPORTER_ASSERT(reporter, face.get());
reed@google.com83165a52013-04-11 18:31:25 +000068
69 if (verbose) {
70 SkDebugf("\t[%d] %s [%3d %d %d]\n", j, sname.c_str(),
71 fs.weight(), fs.width(), fs.isItalic());
72 }
73 }
74 }
75}
76
reed@google.com66c9f992013-04-11 19:04:34 +000077DEFINE_bool(verboseFontMgr, false, "run verbose fontmgr tests.");
reed@google.com83165a52013-04-11 18:31:25 +000078
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000079DEF_TEST(FontMgr, reporter) {
reed@google.com83165a52013-04-11 18:31:25 +000080 test_fontiter(reporter, FLAGS_verboseFontMgr);
reed@google.comc1a81eb2013-09-19 18:06:03 +000081 test_alias_names(reporter);
reed@google.com83165a52013-04-11 18:31:25 +000082}