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