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 | |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkFontMgr.h" |
| 10 | #include "SkTypeface.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 12 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 13 | #include "SkFont.h" |
| 14 | #include "SkPaint.h" |
| 15 | static void test_font(skiatest::Reporter* reporter) { |
| 16 | uint32_t flags = 0; |
| 17 | SkAutoTUnref<SkFont> font(SkFont::Create(NULL, 24, SkFont::kA8_MaskType, flags)); |
skia.committer@gmail.com | e1d9443 | 2014-04-09 03:04:11 +0000 | [diff] [blame] | 18 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 19 | REPORTER_ASSERT(reporter, font->getTypeface()); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 20 | REPORTER_ASSERT(reporter, 24 == font->getSize()); |
| 21 | REPORTER_ASSERT(reporter, 1 == font->getScaleX()); |
| 22 | REPORTER_ASSERT(reporter, 0 == font->getSkewX()); |
| 23 | REPORTER_ASSERT(reporter, SkFont::kA8_MaskType == font->getMaskType()); |
| 24 | |
| 25 | uint16_t glyphs[5]; |
| 26 | sk_bzero(glyphs, sizeof(glyphs)); |
| 27 | |
| 28 | int count = font->textToGlyphs("Hello", 5, kUTF8_SkTextEncoding, glyphs, SK_ARRAY_COUNT(glyphs)); |
| 29 | |
| 30 | REPORTER_ASSERT(reporter, 5 == count); |
| 31 | for (int i = 0; i < count; ++i) { |
| 32 | REPORTER_ASSERT(reporter, 0 != glyphs[i]); |
| 33 | } |
| 34 | REPORTER_ASSERT(reporter, glyphs[0] != glyphs[1]); // 'h' != 'e' |
| 35 | REPORTER_ASSERT(reporter, glyphs[2] == glyphs[3]); // 'l' == 'l' |
skia.committer@gmail.com | e1d9443 | 2014-04-09 03:04:11 +0000 | [diff] [blame] | 36 | |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 37 | SkAutoTUnref<SkFont> newFont(font->cloneWithSize(36)); |
| 38 | REPORTER_ASSERT(reporter, newFont.get()); |
| 39 | REPORTER_ASSERT(reporter, font->getTypeface() == newFont->getTypeface()); |
| 40 | REPORTER_ASSERT(reporter, 36 == newFont->getSize()); // double check we haven't changed |
| 41 | REPORTER_ASSERT(reporter, 24 == font->getSize()); // double check we haven't changed |
| 42 | |
| 43 | SkPaint paint; |
| 44 | paint.setTextSize(18); |
| 45 | font.reset(SkFont::Testing_CreateFromPaint(paint)); |
| 46 | REPORTER_ASSERT(reporter, font.get()); |
| 47 | REPORTER_ASSERT(reporter, font->getSize() == paint.getTextSize()); |
| 48 | REPORTER_ASSERT(reporter, SkFont::kBW_MaskType == font->getMaskType()); |
| 49 | } |
| 50 | |
reed@google.com | d44d988 | 2013-09-18 20:32:25 +0000 | [diff] [blame] | 51 | /* |
| 52 | * If the font backend is going to "alias" some font names to other fonts |
| 53 | * (e.g. sans -> Arial) then we want to at least get the same typeface back |
| 54 | * if we request the alias name multiple times. |
| 55 | */ |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 56 | static void test_alias_names(skiatest::Reporter* reporter) { |
| 57 | const char* inNames[] = { |
| 58 | "sans", "sans-serif", "serif", "monospace", "times", "helvetica" |
| 59 | }; |
skia.committer@gmail.com | f91e3d4 | 2013-09-20 07:01:33 +0000 | [diff] [blame] | 60 | |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 61 | for (size_t i = 0; i < SK_ARRAY_COUNT(inNames); ++i) { |
| 62 | SkAutoTUnref<SkTypeface> first(SkTypeface::CreateFromName(inNames[i], |
| 63 | SkTypeface::kNormal)); |
| 64 | if (NULL == first.get()) { |
| 65 | continue; |
| 66 | } |
| 67 | for (int j = 0; j < 10; ++j) { |
| 68 | SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(inNames[i], |
| 69 | SkTypeface::kNormal)); |
| 70 | #if 0 |
| 71 | SkString name; |
| 72 | face->getFamilyName(&name); |
| 73 | printf("request %s, received %s, first id %x received %x\n", |
| 74 | inNames[i], name.c_str(), first->uniqueID(), face->uniqueID()); |
| 75 | #endif |
| 76 | REPORTER_ASSERT(reporter, first->uniqueID() == face->uniqueID()); |
| 77 | } |
reed@google.com | d44d988 | 2013-09-18 20:32:25 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 81 | static void test_fontiter(skiatest::Reporter* reporter, bool verbose) { |
| 82 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 83 | int count = fm->countFamilies(); |
| 84 | |
| 85 | for (int i = 0; i < count; ++i) { |
| 86 | SkString fname; |
| 87 | fm->getFamilyName(i, &fname); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 88 | |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 89 | SkAutoTUnref<SkFontStyleSet> fnset(fm->matchFamily(fname.c_str())); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 90 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, fnset->count() == set->count()); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 92 | |
| 93 | if (verbose) { |
| 94 | SkDebugf("[%2d] %s\n", i, fname.c_str()); |
| 95 | } |
| 96 | |
| 97 | for (int j = 0; j < set->count(); ++j) { |
| 98 | SkString sname; |
| 99 | SkFontStyle fs; |
| 100 | set->getStyle(j, &fs, &sname); |
reed@google.com | eb02957 | 2013-04-25 14:59:32 +0000 | [diff] [blame] | 101 | // REPORTER_ASSERT(reporter, sname.size() > 0); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 102 | |
| 103 | SkAutoTUnref<SkTypeface> face(set->createTypeface(j)); |
reed@google.com | c1ccda3 | 2013-04-19 14:28:54 +0000 | [diff] [blame] | 104 | // REPORTER_ASSERT(reporter, face.get()); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 105 | |
| 106 | if (verbose) { |
| 107 | SkDebugf("\t[%d] %s [%3d %d %d]\n", j, sname.c_str(), |
| 108 | fs.weight(), fs.width(), fs.isItalic()); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
reed@google.com | 66c9f99 | 2013-04-11 19:04:34 +0000 | [diff] [blame] | 114 | DEFINE_bool(verboseFontMgr, false, "run verbose fontmgr tests."); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 115 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 116 | DEF_TEST(FontMgr, reporter) { |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 117 | test_fontiter(reporter, FLAGS_verboseFontMgr); |
reed@google.com | c1a81eb | 2013-09-19 18:06:03 +0000 | [diff] [blame] | 118 | test_alias_names(reporter); |
reed@google.com | 43c2758 | 2014-04-08 15:04:29 +0000 | [diff] [blame] | 119 | test_font(reporter); |
reed@google.com | 83165a5 | 2013-04-11 18:31:25 +0000 | [diff] [blame] | 120 | } |