reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 10 | #include "SkCommandLineFlags.h" |
| 11 | #include "SkFontMgr.h" |
| 12 | #include "SkTypeface.h" |
| 13 | |
reed@google.com | d44d988 | 2013-09-18 20:32:25 +0000 | [diff] [blame] | 14 | /* |
| 15 | * If the font backend is going to "alias" some font names to other fonts |
| 16 | * (e.g. sans -> Arial) then we want to at least get the same typeface back |
| 17 | * if we request the alias name multiple times. |
| 18 | */ |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 19 | static void test_alias_names(skiatest::Reporter* reporter) { |
| 20 | const char* inNames[] = { |
| 21 | "sans", "sans-serif", "serif", "monospace", "times", "helvetica" |
| 22 | }; |
skia.committer@gmail.com | f91e3d4 | 2013-09-20 07:01:33 +0000 | [diff] [blame] | 23 | |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 24 | for (size_t i = 0; i < SK_ARRAY_COUNT(inNames); ++i) { |
| 25 | SkAutoTUnref<SkTypeface> first(SkTypeface::CreateFromName(inNames[i], |
| 26 | SkTypeface::kNormal)); |
| 27 | if (NULL == first.get()) { |
| 28 | continue; |
| 29 | } |
| 30 | for (int j = 0; j < 10; ++j) { |
| 31 | SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(inNames[i], |
| 32 | SkTypeface::kNormal)); |
| 33 | #if 0 |
| 34 | SkString name; |
| 35 | face->getFamilyName(&name); |
| 36 | printf("request %s, received %s, first id %x received %x\n", |
| 37 | inNames[i], name.c_str(), first->uniqueID(), face->uniqueID()); |
| 38 | #endif |
| 39 | REPORTER_ASSERT(reporter, first->uniqueID() == face->uniqueID()); |
| 40 | } |
reed@google.com | d44d988 | 2013-09-18 20:32:25 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 44 | static void test_fontiter(skiatest::Reporter* reporter, bool verbose) { |
| 45 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 46 | int count = fm->countFamilies(); |
| 47 | |
| 48 | for (int i = 0; i < count; ++i) { |
| 49 | SkString fname; |
| 50 | fm->getFamilyName(i, &fname); |
| 51 | REPORTER_ASSERT(reporter, fname.size() > 0); |
| 52 | |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 53 | SkAutoTUnref<SkFontStyleSet> fnset(fm->matchFamily(fname.c_str())); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 54 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, fnset->count() == set->count()); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 56 | |
| 57 | if (verbose) { |
| 58 | SkDebugf("[%2d] %s\n", i, fname.c_str()); |
| 59 | } |
| 60 | |
| 61 | for (int j = 0; j < set->count(); ++j) { |
| 62 | SkString sname; |
| 63 | SkFontStyle fs; |
| 64 | set->getStyle(j, &fs, &sname); |
reed@google.com | eb02957 | 2013-04-25 14:59:32 +0000 | [diff] [blame] | 65 | // REPORTER_ASSERT(reporter, sname.size() > 0); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 66 | |
| 67 | SkAutoTUnref<SkTypeface> face(set->createTypeface(j)); |
reed@google.com | c1ccda3 | 2013-04-19 14:28:54 +0000 | [diff] [blame] | 68 | // REPORTER_ASSERT(reporter, face.get()); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 69 | |
| 70 | if (verbose) { |
| 71 | SkDebugf("\t[%d] %s [%3d %d %d]\n", j, sname.c_str(), |
| 72 | fs.weight(), fs.width(), fs.isItalic()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
reed@google.com | 66c9f99 | 2013-04-11 19:04:34 +0000 | [diff] [blame] | 78 | DEFINE_bool(verboseFontMgr, false, "run verbose fontmgr tests."); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 79 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 80 | DEF_TEST(FontMgr, reporter) { |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 81 | test_fontiter(reporter, FLAGS_verboseFontMgr); |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 82 | test_alias_names(reporter); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 83 | } |