reed@google.com | af0fa6a | 2013-03-28 13:39:35 +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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFontMetrics.h" |
| 13 | #include "include/core/SkFontMgr.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkFontStyle.h" |
| 15 | #include "include/core/SkFontTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkGraphics.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/core/SkPath.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/core/SkPoint.h" |
| 20 | #include "include/core/SkRect.h" |
| 21 | #include "include/core/SkRefCnt.h" |
| 22 | #include "include/core/SkScalar.h" |
| 23 | #include "include/core/SkSize.h" |
| 24 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/core/SkFontPriv.h" |
Hal Canary | e574f1e | 2019-07-15 14:01:37 -0400 | [diff] [blame] | 28 | #include "tools/SkMetaData.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "tools/ToolUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | |
| 31 | #include <utility> |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 32 | |
| 33 | // limit this just so we don't take too long to draw |
| 34 | #define MAX_FAMILIES 30 |
| 35 | |
| 36 | static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 37 | SkScalar y, const SkFont& font) { |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 38 | canvas->drawString(text, x, y, font, SkPaint()); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 39 | return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8); |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 40 | } |
| 41 | |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 42 | static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x, |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 43 | SkScalar y, const SkFont& origFont, SkFontMgr* fm, |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 44 | const char* fontName, const char* bcp47[], int bcp47Count, |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 45 | const SkFontStyle& fontStyle) { |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 46 | SkFont font = origFont; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 47 | // find typeface containing the requested character and draw it |
| 48 | SkString ch; |
| 49 | ch.appendUnichar(character); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 50 | sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(fontName, fontStyle, |
| 51 | bcp47, bcp47Count, character)); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 52 | font.setTypeface(typeface); |
| 53 | x = drawString(canvas, ch, x, y, font) + 20; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 54 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (nullptr == typeface) { |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 56 | return x; |
| 57 | } |
| 58 | |
| 59 | // repeat the process, but this time use the family name of the typeface |
| 60 | // from the first pass. This emulates the behavior in Blink where it |
| 61 | // it expects to get the same glyph when following this pattern. |
| 62 | SkString familyName; |
| 63 | typeface->getFamilyName(&familyName); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 64 | font.setTypeface(fm->legacyMakeTypeface(familyName.c_str(), typeface->fontStyle())); |
| 65 | return drawString(canvas, ch, x, y, font) + 20; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 66 | } |
| 67 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 68 | static const char* zh = "zh"; |
| 69 | static const char* ja = "ja"; |
| 70 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 71 | class FontMgrGM : public skiagm::GM { |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 72 | sk_sp<SkFontMgr> fFM; |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 73 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 74 | void onOnceBeforeDraw() override { |
| 75 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
Mike Klein | 8073e79 | 2017-11-13 18:10:54 -0500 | [diff] [blame] | 76 | fFM = SkFontMgr::RefDefault(); |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 77 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 78 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 79 | SkString onShortName() override { return SkString("fontmgr_iter"); } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 80 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 81 | SkISize onISize() override { return {1536, 768}; } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 82 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 84 | SkScalar y = 20; |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 85 | SkFont font; |
| 86 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 87 | font.setSubpixel(true); |
| 88 | font.setSize(17); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 89 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 90 | SkFontMgr* fm = fFM.get(); |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 91 | int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); |
| 92 | |
| 93 | for (int i = 0; i < count; ++i) { |
bungeman | c64239a | 2015-04-29 08:15:31 -0700 | [diff] [blame] | 94 | SkString familyName; |
| 95 | fm->getFamilyName(i, &familyName); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 96 | font.setTypeface(nullptr); |
| 97 | (void)drawString(canvas, familyName, 20, y, font); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 98 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 99 | SkScalar x = 220; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 100 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 101 | sk_sp<SkFontStyleSet> set(fm->createStyleSet(i)); |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 102 | for (int j = 0; j < set->count(); ++j) { |
| 103 | SkString sname; |
| 104 | SkFontStyle fs; |
| 105 | set->getStyle(j, &fs, &sname); |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 106 | sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.slant()); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 107 | |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 108 | font.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j))); |
| 109 | x = drawString(canvas, sname, x, y, font) + 20; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 110 | |
| 111 | // check to see that we get different glyphs in japanese and chinese |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 112 | x = drawCharacter(canvas, 0x5203, x, y, font, fm, familyName.c_str(), &zh, 1, fs); |
| 113 | x = drawCharacter(canvas, 0x5203, x, y, font, fm, familyName.c_str(), &ja, 1, fs); |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 114 | // check that emoji characters are found |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 115 | x = drawCharacter(canvas, 0x1f601, x, y, font, fm, familyName.c_str(), nullptr,0, fs); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 116 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 117 | y += 24; |
| 118 | } |
| 119 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 122 | class FontMgrMatchGM : public skiagm::GM { |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 123 | sk_sp<SkFontMgr> fFM; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 124 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 125 | void onOnceBeforeDraw() override { |
| 126 | fFM = SkFontMgr::RefDefault(); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 127 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
| 128 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 129 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 130 | SkString onShortName() override { return SkString("fontmgr_match"); } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 131 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 132 | SkISize onISize() override { return {640, 1024}; } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 133 | |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 134 | void iterateFamily(SkCanvas* canvas, const SkFont& font, SkFontStyleSet* fset) { |
| 135 | SkFont f(font); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 136 | SkScalar y = 0; |
| 137 | |
| 138 | for (int j = 0; j < fset->count(); ++j) { |
| 139 | SkString sname; |
| 140 | SkFontStyle fs; |
| 141 | fset->getStyle(j, &fs, &sname); |
| 142 | |
| 143 | sname.appendf(" [%d %d]", fs.weight(), fs.width()); |
| 144 | |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 145 | f.setTypeface(sk_sp<SkTypeface>(fset->createTypeface(j))); |
| 146 | (void)drawString(canvas, sname, 0, y, f); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 147 | y += 24; |
| 148 | } |
| 149 | } |
| 150 | |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 151 | void exploreFamily(SkCanvas* canvas, const SkFont& font, SkFontStyleSet* fset) { |
| 152 | SkFont f(font); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 153 | SkScalar y = 0; |
| 154 | |
| 155 | for (int weight = 100; weight <= 900; weight += 200) { |
| 156 | for (int width = 1; width <= 9; width += 2) { |
| 157 | SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 158 | sk_sp<SkTypeface> face(fset->matchStyle(fs)); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 159 | if (face) { |
| 160 | SkString str; |
| 161 | str.printf("request [%d %d]", fs.weight(), fs.width()); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 162 | f.setTypeface(std::move(face)); |
| 163 | (void)drawString(canvas, str, 0, y, f); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 164 | y += 24; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 169 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 170 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 171 | SkFont font; |
| 172 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 173 | font.setSubpixel(true); |
| 174 | font.setSize(17); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 175 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 176 | const char* gNames[] = { |
Ben Wagner | 83c6b96 | 2018-07-10 19:40:15 -0400 | [diff] [blame] | 177 | "Helvetica Neue", "Arial", "sans" |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 180 | sk_sp<SkFontStyleSet> fset; |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 181 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) { |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 182 | fset.reset(fFM->matchFamily(gNames[i])); |
| 183 | if (fset->count() > 0) { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | if (nullptr == fset.get()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 188 | *errorMsg = "No SkFontStyleSet"; |
| 189 | return DrawResult::kFail; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 190 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 191 | |
| 192 | canvas->translate(20, 40); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 193 | this->exploreFamily(canvas, font, fset.get()); |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 194 | canvas->translate(150, 0); |
Mike Reed | 560d5b3 | 2018-12-16 16:36:48 -0500 | [diff] [blame] | 195 | this->iterateFamily(canvas, font, fset.get()); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 196 | return DrawResult::kOk; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 197 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 200 | class FontMgrBoundsGM : public skiagm::GM { |
| 201 | public: |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 202 | FontMgrBoundsGM(float scale, float skew) : fScaleX(scale) , fSkewX(skew) {} |
| 203 | |
| 204 | private: |
| 205 | SkString onShortName() override { |
| 206 | if (fScaleX != 1 || fSkewX != 0) { |
| 207 | return SkStringPrintf("fontmgr_bounds_%g_%g", fScaleX, fSkewX); |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 208 | } |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 209 | return SkString("fontmgr_bounds"); |
| 210 | } |
| 211 | |
| 212 | void onOnceBeforeDraw() override { |
| 213 | fFM = SkFontMgr::RefDefault(); |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool onGetControls(SkMetaData* controls) override { |
| 217 | controls->setBool("Label Bounds", fLabelBounds); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void onSetControls(const SkMetaData& controls) override { |
| 222 | controls.findBool("Label Bounds", &fLabelBounds); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 225 | static void show_bounds(SkCanvas* canvas, const SkFont& font, SkScalar x, SkScalar y, |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 226 | SkColor boundsColor, bool labelBounds) |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 227 | { |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 228 | SkRect fontBounds = SkFontPriv::GetFontBounds(font).makeOffset(x, y); |
| 229 | |
| 230 | SkPaint boundsPaint; |
| 231 | boundsPaint.setAntiAlias(true); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 232 | boundsPaint.setColor(boundsColor); |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 233 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 234 | canvas->drawRect(fontBounds, boundsPaint); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 235 | |
Mike Reed | cb6f53e | 2018-11-06 12:44:54 -0500 | [diff] [blame] | 236 | SkFontMetrics fm; |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 237 | font.getMetrics(&fm); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 238 | SkPaint metricsPaint(boundsPaint); |
| 239 | metricsPaint.setStyle(SkPaint::kFill_Style); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 240 | metricsPaint.setAlphaf(0.25f); |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 241 | if ((fm.fFlags & SkFontMetrics::kUnderlinePositionIsValid_Flag) && |
Ben Wagner | 9ca55f2 | 2019-02-12 15:04:17 -0500 | [diff] [blame] | 242 | (fm.fFlags & SkFontMetrics::kUnderlineThicknessIsValid_Flag)) |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 243 | { |
| 244 | SkRect underline{ fontBounds.fLeft, fm.fUnderlinePosition+y, |
| 245 | fontBounds.fRight, fm.fUnderlinePosition+y + fm.fUnderlineThickness }; |
| 246 | canvas->drawRect(underline, metricsPaint); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 247 | } |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame] | 248 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 249 | if ((fm.fFlags & SkFontMetrics::kStrikeoutPositionIsValid_Flag) && |
Ben Wagner | 9ca55f2 | 2019-02-12 15:04:17 -0500 | [diff] [blame] | 250 | (fm.fFlags & SkFontMetrics::kStrikeoutThicknessIsValid_Flag)) |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 251 | { |
| 252 | SkRect strikeout{ fontBounds.fLeft, fm.fStrikeoutPosition+y - fm.fStrikeoutThickness, |
| 253 | fontBounds.fRight, fm.fStrikeoutPosition+y }; |
| 254 | canvas->drawRect(strikeout, metricsPaint); |
| 255 | } |
| 256 | |
| 257 | SkGlyphID left = 0, right = 0, top = 0, bottom = 0; |
| 258 | { |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 259 | int numGlyphs = font.getTypefaceOrDefault()->countGlyphs(); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 260 | SkRect min = {0, 0, 0, 0}; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 261 | for (int i = 0; i < numGlyphs; ++i) { |
| 262 | SkGlyphID glyphId = i; |
| 263 | SkRect cur; |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 264 | font.getBounds(&glyphId, 1, &cur, nullptr); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 265 | if (cur.fLeft < min.fLeft ) { min.fLeft = cur.fLeft; left = i; } |
| 266 | if (cur.fTop < min.fTop ) { min.fTop = cur.fTop ; top = i; } |
| 267 | if (min.fRight < cur.fRight ) { min.fRight = cur.fRight; right = i; } |
| 268 | if (min.fBottom < cur.fBottom) { min.fBottom = cur.fBottom; bottom = i; } |
| 269 | } |
| 270 | } |
| 271 | SkGlyphID str[] = { left, right, top, bottom }; |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 272 | SkPoint location[] = { |
| 273 | {fontBounds.left(), fontBounds.centerY()}, |
| 274 | {fontBounds.right(), fontBounds.centerY()}, |
| 275 | {fontBounds.centerX(), fontBounds.top()}, |
| 276 | {fontBounds.centerX(), fontBounds.bottom()} |
| 277 | }; |
| 278 | |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 279 | SkFont labelFont; |
| 280 | labelFont.setEdging(SkFont::Edging::kAntiAlias); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 281 | labelFont.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 282 | |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 283 | if (labelBounds) { |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 284 | SkString name; |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 285 | font.getTypefaceOrDefault()->getFamilyName(&name); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 286 | canvas->drawString(name, fontBounds.fLeft, fontBounds.fBottom, labelFont, SkPaint()); |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 287 | } |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 288 | for (size_t i = 0; i < SK_ARRAY_COUNT(str); ++i) { |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 289 | SkPath path; |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 290 | font.getPath(str[i], &path); |
| 291 | path.offset(x, y); |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 292 | SkPaint::Style style = path.isEmpty() ? SkPaint::kFill_Style : SkPaint::kStroke_Style; |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 293 | SkPaint glyphPaint; |
Ben Wagner | 97182cc | 2018-02-15 10:20:04 -0500 | [diff] [blame] | 294 | glyphPaint.setStyle(style); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 295 | canvas->drawSimpleText(&str[i], sizeof(str[0]), SkTextEncoding::kGlyphID, x, y, font, glyphPaint); |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 296 | |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 297 | if (labelBounds) { |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 298 | SkString glyphStr; |
| 299 | glyphStr.appendS32(str[i]); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 300 | canvas->drawString(glyphStr, location[i].fX, location[i].fY, labelFont, SkPaint()); |
Ben Wagner | 17bca9a | 2018-06-22 15:27:52 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 303 | } |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 306 | SkISize onISize() override { return {1024, 850}; } |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame] | 307 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 308 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 309 | SkFont font; |
| 310 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 311 | font.setSubpixel(true); |
| 312 | font.setSize(100); |
| 313 | font.setScaleX(fScaleX); |
| 314 | font.setSkewX(fSkewX); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 315 | |
| 316 | const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 317 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 318 | SkFontMgr* fm = fFM.get(); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 319 | int count = SkMin32(fm->countFamilies(), 32); |
| 320 | |
| 321 | int index = 0; |
| 322 | SkScalar x = 0, y = 0; |
| 323 | |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 324 | canvas->translate(10, 120); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 325 | |
| 326 | for (int i = 0; i < count; ++i) { |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 327 | sk_sp<SkFontStyleSet> set(fm->createStyleSet(i)); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 328 | for (int j = 0; j < set->count() && j < 3; ++j) { |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 329 | font.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j))); |
Ben Wagner | 2112df0 | 2017-07-24 11:04:21 -0400 | [diff] [blame] | 330 | // Fonts with lots of glyphs are interesting, but can take a long time to find |
| 331 | // the glyphs which make up the maximum extent. |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 332 | if (font.getTypefaceOrDefault() && font.getTypefaceOrDefault()->countGlyphs() < 1000) { |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 333 | SkRect fontBounds = SkFontPriv::GetFontBounds(font); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 334 | x -= fontBounds.fLeft; |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 335 | show_bounds(canvas, font, x, y, boundsColors[index & 1], fLabelBounds); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 336 | x += fontBounds.fRight + 20; |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 337 | index += 1; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 338 | if (x > 900) { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 339 | x = 0; |
| 340 | y += 160; |
| 341 | } |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 342 | if (y >= 700) { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 349 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 350 | sk_sp<SkFontMgr> fFM; |
Ben Wagner | 110c703 | 2019-03-22 17:03:59 -0400 | [diff] [blame] | 351 | const SkScalar fScaleX; |
| 352 | const SkScalar fSkewX; |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 353 | bool fLabelBounds = false; |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 356 | ////////////////////////////////////////////////////////////////////////////// |
| 357 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 358 | DEF_GM(return new FontMgrGM;) |
| 359 | DEF_GM(return new FontMgrMatchGM;) |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 360 | DEF_GM(return new FontMgrBoundsGM(1, 0);) |
| 361 | DEF_GM(return new FontMgrBoundsGM(0.75f, 0);) |
| 362 | DEF_GM(return new FontMgrBoundsGM(1, -0.25f);) |