commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkFont.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkTypeface.h" |
| 11 | #include "Test.h" |
| 12 | |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 13 | static void test_cachedfont(skiatest::Reporter* reporter, |
| 14 | const SkPaint& paint, const SkFont& font) { |
reed@google.com | 4927092 | 2014-05-30 17:15:23 +0000 | [diff] [blame] | 15 | // Currently SkFont resolves null into the default, so only test if paint's is not null |
| 16 | if (paint.getTypeface()) { |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 17 | REPORTER_ASSERT(reporter, font.getTypeface() == paint.getTypeface()); |
reed@google.com | 4927092 | 2014-05-30 17:15:23 +0000 | [diff] [blame] | 18 | } |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 19 | REPORTER_ASSERT(reporter, font.getSize() == paint.getTextSize()); |
| 20 | REPORTER_ASSERT(reporter, font.getScaleX() == paint.getTextScaleX()); |
| 21 | REPORTER_ASSERT(reporter, font.getSkewX() == paint.getTextSkewX()); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 22 | |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 23 | uint32_t mask = SkPaint::kLinearText_Flag | |
| 24 | SkPaint::kSubpixelText_Flag | |
| 25 | SkPaint::kFakeBoldText_Flag | |
| 26 | SkPaint::kEmbeddedBitmapText_Flag | |
| 27 | SkPaint::kAutoHinting_Flag; |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 28 | |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 29 | SkPaint p; |
| 30 | font.LEGACY_applyToPaint(&p); |
| 31 | uint32_t oldFlags = paint.getFlags() & mask; |
| 32 | uint32_t newFlags = p.getFlags() & mask; |
| 33 | REPORTER_ASSERT(reporter, oldFlags == newFlags); |
| 34 | REPORTER_ASSERT(reporter, paint.getHinting() == p.getHinting()); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Mike Reed | 0243949 | 2018-11-12 12:03:58 +0000 | [diff] [blame] | 37 | static void test_fontmetrics(skiatest::Reporter* reporter, |
| 38 | const SkPaint& paint, const SkFont& font) { |
| 39 | SkFontMetrics fm0, fm1; |
| 40 | SkScalar h0 = paint.getFontMetrics(&fm0); |
| 41 | SkScalar h1 = font.getMetrics(&fm1); |
| 42 | |
| 43 | REPORTER_ASSERT(reporter, h0 == h1); |
| 44 | #define CMP(field) REPORTER_ASSERT(reporter, fm0.field == fm1.field) |
| 45 | CMP(fFlags); |
| 46 | CMP(fTop); |
| 47 | CMP(fAscent); |
| 48 | CMP(fDescent); |
| 49 | CMP(fBottom); |
| 50 | CMP(fLeading); |
| 51 | #undef CMP |
| 52 | } |
| 53 | |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 54 | static void test_cachedfont(skiatest::Reporter* reporter) { |
| 55 | static const char* const faces[] = { |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 56 | nullptr, |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 57 | "Arial", "Times", "Times New Roman", "Helvetica", "Courier", |
| 58 | "Courier New", "Verdana", "monospace", |
| 59 | }; |
| 60 | |
| 61 | static const struct { |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 62 | SkScalar fScaleX; |
| 63 | SkScalar fSkewX; |
| 64 | } gScaleRec[] = { |
| 65 | { SK_Scalar1, 0 }, |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 66 | { SK_Scalar1/2, -SK_Scalar1/4 }, |
| 67 | }; |
| 68 | |
| 69 | SkPaint paint; |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 70 | char txt[] = "long .text .with .lots .of.dots."; |
| 71 | |
| 72 | unsigned mask = SkPaint::kAntiAlias_Flag | |
| 73 | SkPaint::kFakeBoldText_Flag | |
| 74 | SkPaint::kLinearText_Flag | |
| 75 | SkPaint::kSubpixelText_Flag | |
| 76 | SkPaint::kLCDRenderText_Flag | |
| 77 | SkPaint::kEmbeddedBitmapText_Flag | |
| 78 | SkPaint::kAutoHinting_Flag; |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 79 | |
| 80 | for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 81 | paint.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle())); |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 82 | for (unsigned flags = 0; flags <= 0xFFF; ++flags) { |
| 83 | if (flags & ~mask) { |
| 84 | continue; |
| 85 | } |
| 86 | paint.setFlags(flags); |
| 87 | for (int hint = 0; hint <= 3; ++hint) { |
| 88 | paint.setHinting((SkFontHinting)hint); |
| 89 | for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) { |
| 90 | paint.setTextScaleX(gScaleRec[k].fScaleX); |
| 91 | paint.setTextSkewX(gScaleRec[k].fSkewX); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 92 | |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 93 | const SkFont font(SkFont::LEGACY_ExtractFromPaint(paint)); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 94 | |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 95 | test_cachedfont(reporter, paint, font); |
| 96 | test_fontmetrics(reporter, paint, font); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 97 | |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 98 | SkRect pbounds, fbounds; |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 99 | |
Mike Reed | d017e51 | 2018-11-17 16:27:41 -0500 | [diff] [blame] | 100 | // Requesting the bounds forces a generateMetrics call. |
| 101 | SkScalar pwidth = paint.measureText(txt, strlen(txt), &pbounds); |
| 102 | SkScalar fwidth = font.measureText(txt, strlen(txt), kUTF8_SkTextEncoding, |
| 103 | &fbounds); |
| 104 | REPORTER_ASSERT(reporter, pwidth == fwidth); |
| 105 | REPORTER_ASSERT(reporter, pbounds == fbounds); |
| 106 | } |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 112 | static void test_aa_hinting(skiatest::Reporter* reporter) { |
| 113 | SkPaint paint; |
| 114 | |
| 115 | for (bool aa : {false, true}) { |
| 116 | paint.setAntiAlias(aa); |
| 117 | for (int hint = 0; hint <= 3; ++hint) { |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame] | 118 | paint.setHinting((SkFontHinting)hint); |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 119 | SkFont font = SkFont::LEGACY_ExtractFromPaint(paint); |
| 120 | |
| 121 | SkPaint p2; |
| 122 | font.LEGACY_applyToPaint(&p2); |
| 123 | REPORTER_ASSERT(reporter, paint.isAntiAlias() == p2.isAntiAlias()); |
| 124 | REPORTER_ASSERT(reporter, paint.getHinting() == p2.getHinting()); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 129 | DEF_TEST(FontObj, reporter) { |
| 130 | test_cachedfont(reporter); |
Mike Reed | fe4fc14 | 2018-10-22 14:24:07 -0400 | [diff] [blame] | 131 | test_aa_hinting(reporter); |
commit-bot@chromium.org | 7ae034d | 2014-05-30 16:46:10 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // need tests for SkStrSearch |