blob: a0b2f5b44a2086ba2d5dfdc4afb9d589a807c280 [file] [log] [blame]
reed@google.comaf0fa6a2013-03-28 13:39:35 +00001/*
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 Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.comaf0fa6a2013-03-28 13:39:35 +000010#include "SkCanvas.h"
11#include "SkFontMgr.h"
12#include "SkGraphics.h"
13#include "SkTypeface.h"
14
15// limit this just so we don't take too long to draw
16#define MAX_FAMILIES 30
17
18static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
19 SkScalar y, const SkPaint& paint) {
Cary Clark2a475ea2017-04-28 15:35:12 -040020 canvas->drawString(text, x, y, paint);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000021 return x + paint.measureText(text.c_str(), text.size());
22}
23
djsollen0d393a92014-08-27 07:03:13 -070024static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
25 SkScalar y, SkPaint& paint, SkFontMgr* fm,
bungemanc9232dc2014-11-10 13:29:33 -080026 const char* fontName, const char* bcp47[], int bcp47Count,
djsollen0d393a92014-08-27 07:03:13 -070027 const SkFontStyle& fontStyle) {
28 // find typeface containing the requested character and draw it
29 SkString ch;
30 ch.appendUnichar(character);
bungeman13b9c952016-05-12 10:09:30 -070031 sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(fontName, fontStyle,
32 bcp47, bcp47Count, character));
33 paint.setTypeface(typeface);
djsollen0d393a92014-08-27 07:03:13 -070034 x = drawString(canvas, ch, x, y, paint) + 20;
35
halcanary96fcdcc2015-08-27 07:41:13 -070036 if (nullptr == typeface) {
djsollen0d393a92014-08-27 07:03:13 -070037 return x;
38 }
39
40 // repeat the process, but this time use the family name of the typeface
41 // from the first pass. This emulates the behavior in Blink where it
42 // it expects to get the same glyph when following this pattern.
43 SkString familyName;
44 typeface->getFamilyName(&familyName);
Mike Reed59227392017-09-26 09:46:08 -040045 paint.setTypeface(fm->legacyMakeTypeface(familyName.c_str(), typeface->fontStyle()));
djsollen0d393a92014-08-27 07:03:13 -070046 return drawString(canvas, ch, x, y, paint) + 20;
47}
48
bungemanc9232dc2014-11-10 13:29:33 -080049static const char* zh = "zh";
50static const char* ja = "ja";
51
reed@google.comaf0fa6a2013-03-28 13:39:35 +000052class FontMgrGM : public skiagm::GM {
53public:
Mike Klein8073e792017-11-13 18:10:54 -050054 FontMgrGM() {
Ben Wagner3546ff12017-01-03 13:32:36 -050055 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
56
57 fName.set("fontmgr_iter");
Mike Klein8073e792017-11-13 18:10:54 -050058 fFM = SkFontMgr::RefDefault();
Mike Kleinb251b722017-11-13 11:03:16 -050059 fName.append(sk_tool_utils::platform_font_manager());
Ben Wagner3546ff12017-01-03 13:32:36 -050060 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000061
62protected:
mtklein36352bf2015-03-25 18:17:31 -070063 SkString onShortName() override {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000064 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000065 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 SkISize onISize() override {
djsollen0d393a92014-08-27 07:03:13 -070068 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000069 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 void onDraw(SkCanvas* canvas) override {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000072 SkScalar y = 20;
73 SkPaint paint;
74 paint.setAntiAlias(true);
75 paint.setLCDRenderText(true);
76 paint.setSubpixelText(true);
77 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000078
Hal Canarycefc4312016-11-04 16:26:16 -040079 SkFontMgr* fm = fFM.get();
reed@google.comaf0fa6a2013-03-28 13:39:35 +000080 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
81
82 for (int i = 0; i < count; ++i) {
bungemanc64239a2015-04-29 08:15:31 -070083 SkString familyName;
84 fm->getFamilyName(i, &familyName);
halcanary96fcdcc2015-08-27 07:41:13 -070085 paint.setTypeface(nullptr);
bungemanc64239a2015-04-29 08:15:31 -070086 (void)drawString(canvas, familyName, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000087
reed@google.comaf0fa6a2013-03-28 13:39:35 +000088 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +000089
Hal Canarycefc4312016-11-04 16:26:16 -040090 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
reed@google.comaf0fa6a2013-03-28 13:39:35 +000091 for (int j = 0; j < set->count(); ++j) {
92 SkString sname;
93 SkFontStyle fs;
94 set->getStyle(j, &fs, &sname);
bungemanb4bb7d82016-04-27 10:21:04 -070095 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.slant());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000096
bungeman13b9c952016-05-12 10:09:30 -070097 paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
reed@google.comaf0fa6a2013-03-28 13:39:35 +000098 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -070099
100 // check to see that we get different glyphs in japanese and chinese
bungemanc64239a2015-04-29 08:15:31 -0700101 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &zh, 1, fs);
102 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &ja, 1, fs);
djsollen0d393a92014-08-27 07:03:13 -0700103 // check that emoji characters are found
halcanary96fcdcc2015-08-27 07:41:13 -0700104 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, familyName.c_str(), nullptr,0, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000105 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000106 y += 24;
107 }
108 }
109
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000110private:
Hal Canarycefc4312016-11-04 16:26:16 -0400111 sk_sp<SkFontMgr> fFM;
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000112 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000113 typedef GM INHERITED;
114};
115
reed@google.com964988f2013-03-29 14:57:22 +0000116class FontMgrMatchGM : public skiagm::GM {
Hal Canarycefc4312016-11-04 16:26:16 -0400117 sk_sp<SkFontMgr> fFM;
reed@google.com964988f2013-03-29 14:57:22 +0000118
119public:
120 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
121 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
122 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000123
reed@google.com964988f2013-03-29 14:57:22 +0000124protected:
mtklein36352bf2015-03-25 18:17:31 -0700125 SkString onShortName() override {
caryclark6531c362015-07-20 13:38:56 -0700126 SkString name("fontmgr_match");
Mike Kleinb251b722017-11-13 11:03:16 -0500127 name.append(sk_tool_utils::platform_font_manager());
caryclark6531c362015-07-20 13:38:56 -0700128 return name;
reed@google.com964988f2013-03-29 14:57:22 +0000129 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000130
mtklein36352bf2015-03-25 18:17:31 -0700131 SkISize onISize() override {
reed@google.com964988f2013-03-29 14:57:22 +0000132 return SkISize::Make(640, 1024);
133 }
134
135 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
136 SkFontStyleSet* fset) {
137 SkPaint p(paint);
138 SkScalar y = 0;
139
140 for (int j = 0; j < fset->count(); ++j) {
141 SkString sname;
142 SkFontStyle fs;
143 fset->getStyle(j, &fs, &sname);
144
145 sname.appendf(" [%d %d]", fs.weight(), fs.width());
146
bungeman13b9c952016-05-12 10:09:30 -0700147 p.setTypeface(sk_sp<SkTypeface>(fset->createTypeface(j)));
reed@google.com964988f2013-03-29 14:57:22 +0000148 (void)drawString(canvas, sname, 0, y, p);
149 y += 24;
150 }
151 }
152
153 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
154 SkFontStyleSet* fset) {
155 SkPaint p(paint);
156 SkScalar y = 0;
157
158 for (int weight = 100; weight <= 900; weight += 200) {
159 for (int width = 1; width <= 9; width += 2) {
160 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
bungeman13b9c952016-05-12 10:09:30 -0700161 sk_sp<SkTypeface> face(fset->matchStyle(fs));
reed@google.com964988f2013-03-29 14:57:22 +0000162 if (face) {
163 SkString str;
164 str.printf("request [%d %d]", fs.weight(), fs.width());
bungeman13b9c952016-05-12 10:09:30 -0700165 p.setTypeface(std::move(face));
reed@google.com964988f2013-03-29 14:57:22 +0000166 (void)drawString(canvas, str, 0, y, p);
167 y += 24;
168 }
169 }
170 }
171 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000172
mtklein36352bf2015-03-25 18:17:31 -0700173 void onDraw(SkCanvas* canvas) override {
reed@google.com964988f2013-03-29 14:57:22 +0000174 SkPaint paint;
175 paint.setAntiAlias(true);
176 paint.setLCDRenderText(true);
177 paint.setSubpixelText(true);
178 paint.setTextSize(17);
179
mtkleindbfd7ab2016-09-01 11:24:54 -0700180 const char* gNames[] = {
reed@google.com964988f2013-03-29 14:57:22 +0000181 "Helvetica Neue", "Arial"
182 };
183
Hal Canarycefc4312016-11-04 16:26:16 -0400184 sk_sp<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000185 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000186 fset.reset(fFM->matchFamily(gNames[i]));
187 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000188 break;
189 }
190 }
halcanary96fcdcc2015-08-27 07:41:13 -0700191 if (nullptr == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000192 return;
193 }
reed@google.com964988f2013-03-29 14:57:22 +0000194
195 canvas->translate(20, 40);
Hal Canarycefc4312016-11-04 16:26:16 -0400196 this->exploreFamily(canvas, paint, fset.get());
reed@google.com964988f2013-03-29 14:57:22 +0000197 canvas->translate(150, 0);
Hal Canarycefc4312016-11-04 16:26:16 -0400198 this->iterateFamily(canvas, paint, fset.get());
reed@google.com964988f2013-03-29 14:57:22 +0000199 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000200
reed@google.com964988f2013-03-29 14:57:22 +0000201private:
202 typedef GM INHERITED;
203};
204
reeda0c814c2014-10-22 13:20:58 -0700205class FontMgrBoundsGM : public skiagm::GM {
206public:
reed8893e5f2014-12-15 13:27:26 -0800207 FontMgrBoundsGM(double scale, double skew)
208 : fScaleX(SkDoubleToScalar(scale))
209 , fSkewX(SkDoubleToScalar(skew))
210 {
reeda0c814c2014-10-22 13:20:58 -0700211 fName.set("fontmgr_bounds");
reed8893e5f2014-12-15 13:27:26 -0800212 if (scale != 1 || skew != 0) {
213 fName.appendf("_%g_%g", scale, skew);
214 }
Mike Kleinb251b722017-11-13 11:03:16 -0500215 fName.append(sk_tool_utils::platform_font_manager());
Ben Wagner3546ff12017-01-03 13:32:36 -0500216 fFM = SkFontMgr::RefDefault();
reeda0c814c2014-10-22 13:20:58 -0700217 }
218
219 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
Ben Wagner219f3622017-07-17 15:32:25 -0400220 SkColor boundsColor)
221 {
222 SkPaint glyphPaint(paint);
223 SkRect fontBounds = glyphPaint.getFontBounds();
224 fontBounds.offset(x, y);
225 SkPaint boundsPaint(glyphPaint);
226 boundsPaint.setColor(boundsColor);
227 canvas->drawRect(fontBounds, boundsPaint);
reeda0c814c2014-10-22 13:20:58 -0700228
Ben Wagner219f3622017-07-17 15:32:25 -0400229 SkPaint::FontMetrics fm;
230 glyphPaint.getFontMetrics(&fm);
231 SkPaint metricsPaint(boundsPaint);
232 metricsPaint.setStyle(SkPaint::kFill_Style);
233 metricsPaint.setAlpha(0x40);
234 if ((fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag) &&
235 (fm.fFlags & SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag))
236 {
237 SkRect underline{ fontBounds.fLeft, fm.fUnderlinePosition+y,
238 fontBounds.fRight, fm.fUnderlinePosition+y + fm.fUnderlineThickness };
239 canvas->drawRect(underline, metricsPaint);
reeda0c814c2014-10-22 13:20:58 -0700240 }
tfarinaaa458fb2015-01-05 17:18:51 -0800241
Ben Wagner219f3622017-07-17 15:32:25 -0400242 if ((fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag) &&
243 (fm.fFlags & SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag))
244 {
245 SkRect strikeout{ fontBounds.fLeft, fm.fStrikeoutPosition+y - fm.fStrikeoutThickness,
246 fontBounds.fRight, fm.fStrikeoutPosition+y };
247 canvas->drawRect(strikeout, metricsPaint);
248 }
249
250 SkGlyphID left = 0, right = 0, top = 0, bottom = 0;
251 {
252 int numGlyphs = glyphPaint.getTypeface()->countGlyphs();
253 SkRect min = {0, 0, 0, 0};
254 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
255 for (int i = 0; i < numGlyphs; ++i) {
256 SkGlyphID glyphId = i;
257 SkRect cur;
258 glyphPaint.measureText(&glyphId, sizeof(glyphId), &cur);
259 if (cur.fLeft < min.fLeft ) { min.fLeft = cur.fLeft; left = i; }
260 if (cur.fTop < min.fTop ) { min.fTop = cur.fTop ; top = i; }
261 if (min.fRight < cur.fRight ) { min.fRight = cur.fRight; right = i; }
262 if (min.fBottom < cur.fBottom) { min.fBottom = cur.fBottom; bottom = i; }
263 }
264 }
265 SkGlyphID str[] = { left, right, top, bottom };
266 for (size_t i = 0; i < SK_ARRAY_COUNT(str); ++i) {
267 canvas->drawText(&str[i], sizeof(str[0]), x, y, glyphPaint);
268 }
reeda0c814c2014-10-22 13:20:58 -0700269 }
270
271protected:
mtklein36352bf2015-03-25 18:17:31 -0700272 SkString onShortName() override {
reeda0c814c2014-10-22 13:20:58 -0700273 return fName;
274 }
tfarinaaa458fb2015-01-05 17:18:51 -0800275
mtklein36352bf2015-03-25 18:17:31 -0700276 SkISize onISize() override {
reeda0c814c2014-10-22 13:20:58 -0700277 return SkISize::Make(1024, 850);
278 }
tfarinaaa458fb2015-01-05 17:18:51 -0800279
mtklein36352bf2015-03-25 18:17:31 -0700280 void onDraw(SkCanvas* canvas) override {
reeda0c814c2014-10-22 13:20:58 -0700281 SkPaint paint;
282 paint.setAntiAlias(true);
283 paint.setSubpixelText(true);
284 paint.setTextSize(100);
285 paint.setStyle(SkPaint::kStroke_Style);
reed8893e5f2014-12-15 13:27:26 -0800286 paint.setTextScaleX(fScaleX);
287 paint.setTextSkewX(fSkewX);
reeda0c814c2014-10-22 13:20:58 -0700288
289 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
halcanary9d524f22016-03-29 09:03:52 -0700290
Hal Canarycefc4312016-11-04 16:26:16 -0400291 SkFontMgr* fm = fFM.get();
reeda0c814c2014-10-22 13:20:58 -0700292 int count = SkMin32(fm->countFamilies(), 32);
293
294 int index = 0;
295 SkScalar x = 0, y = 0;
296
Ben Wagner219f3622017-07-17 15:32:25 -0400297 canvas->translate(10, 120);
reeda0c814c2014-10-22 13:20:58 -0700298
299 for (int i = 0; i < count; ++i) {
Hal Canarycefc4312016-11-04 16:26:16 -0400300 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
Ben Wagner219f3622017-07-17 15:32:25 -0400301 for (int j = 0; j < set->count() && j < 3; ++j) {
bungeman13b9c952016-05-12 10:09:30 -0700302 paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
Ben Wagner2112df02017-07-24 11:04:21 -0400303 // Fonts with lots of glyphs are interesting, but can take a long time to find
304 // the glyphs which make up the maximum extent.
305 if (paint.getTypeface() && paint.getTypeface()->countGlyphs() < 1000) {
Ben Wagner219f3622017-07-17 15:32:25 -0400306 SkRect fontBounds = paint.getFontBounds();
307 x -= fontBounds.fLeft;
reeda0c814c2014-10-22 13:20:58 -0700308 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
Ben Wagner219f3622017-07-17 15:32:25 -0400309 x += fontBounds.fRight + 20;
reeda0c814c2014-10-22 13:20:58 -0700310 index += 1;
Ben Wagner219f3622017-07-17 15:32:25 -0400311 if (x > 900) {
reeda0c814c2014-10-22 13:20:58 -0700312 x = 0;
313 y += 160;
314 }
Ben Wagner219f3622017-07-17 15:32:25 -0400315 if (y >= 700) {
reeda0c814c2014-10-22 13:20:58 -0700316 return;
317 }
318 }
319 }
320 }
321 }
mtklein1c402922015-01-23 11:07:07 -0800322
reeda0c814c2014-10-22 13:20:58 -0700323private:
Hal Canarycefc4312016-11-04 16:26:16 -0400324 sk_sp<SkFontMgr> fFM;
reeda0c814c2014-10-22 13:20:58 -0700325 SkString fName;
reed8893e5f2014-12-15 13:27:26 -0800326 SkScalar fScaleX, fSkewX;
reeda0c814c2014-10-22 13:20:58 -0700327 typedef GM INHERITED;
328};
329
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000330//////////////////////////////////////////////////////////////////////////////
331
halcanary385fe4d2015-08-26 13:07:48 -0700332DEF_GM(return new FontMgrGM;)
333DEF_GM(return new FontMgrMatchGM;)
334DEF_GM(return new FontMgrBoundsGM(1.0, 0);)
335DEF_GM(return new FontMgrBoundsGM(0.75, 0);)
336DEF_GM(return new FontMgrBoundsGM(1.0, -0.25);)