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