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