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