blob: 58c569c3004c2c9ef39aab4cf5836d66b5cec375 [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"
Ben Wagner97182cc2018-02-15 10:20:04 -050012#include "SkPath.h"
reed@google.comaf0fa6a2013-03-28 13:39:35 +000013#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
19static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
20 SkScalar y, const SkPaint& paint) {
Cary Clark2a475ea2017-04-28 15:35:12 -040021 canvas->drawString(text, x, y, paint);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000022 return x + paint.measureText(text.c_str(), text.size());
23}
24
djsollen0d393a92014-08-27 07:03:13 -070025static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
26 SkScalar y, SkPaint& paint, SkFontMgr* fm,
bungemanc9232dc2014-11-10 13:29:33 -080027 const char* fontName, const char* bcp47[], int bcp47Count,
djsollen0d393a92014-08-27 07:03:13 -070028 const SkFontStyle& fontStyle) {
29 // find typeface containing the requested character and draw it
30 SkString ch;
31 ch.appendUnichar(character);
bungeman13b9c952016-05-12 10:09:30 -070032 sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(fontName, fontStyle,
33 bcp47, bcp47Count, character));
34 paint.setTypeface(typeface);
djsollen0d393a92014-08-27 07:03:13 -070035 x = drawString(canvas, ch, x, y, paint) + 20;
36
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (nullptr == typeface) {
djsollen0d393a92014-08-27 07:03:13 -070038 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 Reed59227392017-09-26 09:46:08 -040046 paint.setTypeface(fm->legacyMakeTypeface(familyName.c_str(), typeface->fontStyle()));
djsollen0d393a92014-08-27 07:03:13 -070047 return drawString(canvas, ch, x, y, paint) + 20;
48}
49
bungemanc9232dc2014-11-10 13:29:33 -080050static const char* zh = "zh";
51static const char* ja = "ja";
52
reed@google.comaf0fa6a2013-03-28 13:39:35 +000053class FontMgrGM : public skiagm::GM {
54public:
Mike Klein8073e792017-11-13 18:10:54 -050055 FontMgrGM() {
Ben Wagner3546ff12017-01-03 13:32:36 -050056 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
57
58 fName.set("fontmgr_iter");
Mike Klein8073e792017-11-13 18:10:54 -050059 fFM = SkFontMgr::RefDefault();
Mike Kleinb251b722017-11-13 11:03:16 -050060 fName.append(sk_tool_utils::platform_font_manager());
Ben Wagner3546ff12017-01-03 13:32:36 -050061 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000062
63protected:
mtklein36352bf2015-03-25 18:17:31 -070064 SkString onShortName() override {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000065 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000066 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 SkISize onISize() override {
djsollen0d393a92014-08-27 07:03:13 -070069 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000070 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 void onDraw(SkCanvas* canvas) override {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000073 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.com6acd09e2013-03-29 07:01:22 +000079
Hal Canarycefc4312016-11-04 16:26:16 -040080 SkFontMgr* fm = fFM.get();
reed@google.comaf0fa6a2013-03-28 13:39:35 +000081 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
82
83 for (int i = 0; i < count; ++i) {
bungemanc64239a2015-04-29 08:15:31 -070084 SkString familyName;
85 fm->getFamilyName(i, &familyName);
halcanary96fcdcc2015-08-27 07:41:13 -070086 paint.setTypeface(nullptr);
bungemanc64239a2015-04-29 08:15:31 -070087 (void)drawString(canvas, familyName, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000088
reed@google.comaf0fa6a2013-03-28 13:39:35 +000089 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +000090
Hal Canarycefc4312016-11-04 16:26:16 -040091 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
reed@google.comaf0fa6a2013-03-28 13:39:35 +000092 for (int j = 0; j < set->count(); ++j) {
93 SkString sname;
94 SkFontStyle fs;
95 set->getStyle(j, &fs, &sname);
bungemanb4bb7d82016-04-27 10:21:04 -070096 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.slant());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000097
bungeman13b9c952016-05-12 10:09:30 -070098 paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
reed@google.comaf0fa6a2013-03-28 13:39:35 +000099 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -0700100
101 // check to see that we get different glyphs in japanese and chinese
bungemanc64239a2015-04-29 08:15:31 -0700102 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);
djsollen0d393a92014-08-27 07:03:13 -0700104 // check that emoji characters are found
halcanary96fcdcc2015-08-27 07:41:13 -0700105 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, familyName.c_str(), nullptr,0, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000106 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000107 y += 24;
108 }
109 }
110
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000111private:
Hal Canarycefc4312016-11-04 16:26:16 -0400112 sk_sp<SkFontMgr> fFM;
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000113 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000114 typedef GM INHERITED;
115};
116
reed@google.com964988f2013-03-29 14:57:22 +0000117class FontMgrMatchGM : public skiagm::GM {
Hal Canarycefc4312016-11-04 16:26:16 -0400118 sk_sp<SkFontMgr> fFM;
reed@google.com964988f2013-03-29 14:57:22 +0000119
120public:
121 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
122 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
123 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000124
reed@google.com964988f2013-03-29 14:57:22 +0000125protected:
mtklein36352bf2015-03-25 18:17:31 -0700126 SkString onShortName() override {
caryclark6531c362015-07-20 13:38:56 -0700127 SkString name("fontmgr_match");
Mike Kleinb251b722017-11-13 11:03:16 -0500128 name.append(sk_tool_utils::platform_font_manager());
caryclark6531c362015-07-20 13:38:56 -0700129 return name;
reed@google.com964988f2013-03-29 14:57:22 +0000130 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000131
mtklein36352bf2015-03-25 18:17:31 -0700132 SkISize onISize() override {
reed@google.com964988f2013-03-29 14:57:22 +0000133 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
bungeman13b9c952016-05-12 10:09:30 -0700148 p.setTypeface(sk_sp<SkTypeface>(fset->createTypeface(j)));
reed@google.com964988f2013-03-29 14:57:22 +0000149 (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);
bungeman13b9c952016-05-12 10:09:30 -0700162 sk_sp<SkTypeface> face(fset->matchStyle(fs));
reed@google.com964988f2013-03-29 14:57:22 +0000163 if (face) {
164 SkString str;
165 str.printf("request [%d %d]", fs.weight(), fs.width());
bungeman13b9c952016-05-12 10:09:30 -0700166 p.setTypeface(std::move(face));
reed@google.com964988f2013-03-29 14:57:22 +0000167 (void)drawString(canvas, str, 0, y, p);
168 y += 24;
169 }
170 }
171 }
172 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000173
mtklein36352bf2015-03-25 18:17:31 -0700174 void onDraw(SkCanvas* canvas) override {
reed@google.com964988f2013-03-29 14:57:22 +0000175 SkPaint paint;
176 paint.setAntiAlias(true);
177 paint.setLCDRenderText(true);
178 paint.setSubpixelText(true);
179 paint.setTextSize(17);
180
mtkleindbfd7ab2016-09-01 11:24:54 -0700181 const char* gNames[] = {
reed@google.com964988f2013-03-29 14:57:22 +0000182 "Helvetica Neue", "Arial"
183 };
184
Hal Canarycefc4312016-11-04 16:26:16 -0400185 sk_sp<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000186 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000187 fset.reset(fFM->matchFamily(gNames[i]));
188 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000189 break;
190 }
191 }
halcanary96fcdcc2015-08-27 07:41:13 -0700192 if (nullptr == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000193 return;
194 }
reed@google.com964988f2013-03-29 14:57:22 +0000195
196 canvas->translate(20, 40);
Hal Canarycefc4312016-11-04 16:26:16 -0400197 this->exploreFamily(canvas, paint, fset.get());
reed@google.com964988f2013-03-29 14:57:22 +0000198 canvas->translate(150, 0);
Hal Canarycefc4312016-11-04 16:26:16 -0400199 this->iterateFamily(canvas, paint, fset.get());
reed@google.com964988f2013-03-29 14:57:22 +0000200 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000201
reed@google.com964988f2013-03-29 14:57:22 +0000202private:
203 typedef GM INHERITED;
204};
205
reeda0c814c2014-10-22 13:20:58 -0700206class FontMgrBoundsGM : public skiagm::GM {
207public:
reed8893e5f2014-12-15 13:27:26 -0800208 FontMgrBoundsGM(double scale, double skew)
209 : fScaleX(SkDoubleToScalar(scale))
210 , fSkewX(SkDoubleToScalar(skew))
211 {
reeda0c814c2014-10-22 13:20:58 -0700212 fName.set("fontmgr_bounds");
reed8893e5f2014-12-15 13:27:26 -0800213 if (scale != 1 || skew != 0) {
214 fName.appendf("_%g_%g", scale, skew);
215 }
Mike Kleinb251b722017-11-13 11:03:16 -0500216 fName.append(sk_tool_utils::platform_font_manager());
Ben Wagner3546ff12017-01-03 13:32:36 -0500217 fFM = SkFontMgr::RefDefault();
reeda0c814c2014-10-22 13:20:58 -0700218 }
219
220 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
Ben Wagner219f3622017-07-17 15:32:25 -0400221 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 Wagner97182cc2018-02-15 10:20:04 -0500228 boundsPaint.setStyle(SkPaint::kStroke_Style);
Ben Wagner219f3622017-07-17 15:32:25 -0400229 canvas->drawRect(fontBounds, boundsPaint);
reeda0c814c2014-10-22 13:20:58 -0700230
Ben Wagner219f3622017-07-17 15:32:25 -0400231 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);
reeda0c814c2014-10-22 13:20:58 -0700242 }
tfarinaaa458fb2015-01-05 17:18:51 -0800243
Ben Wagner219f3622017-07-17 15:32:25 -0400244 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 Wagner97182cc2018-02-15 10:20:04 -0500269 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 Wagner219f3622017-07-17 15:32:25 -0400273 canvas->drawText(&str[i], sizeof(str[0]), x, y, glyphPaint);
274 }
reeda0c814c2014-10-22 13:20:58 -0700275 }
276
277protected:
mtklein36352bf2015-03-25 18:17:31 -0700278 SkString onShortName() override {
reeda0c814c2014-10-22 13:20:58 -0700279 return fName;
280 }
tfarinaaa458fb2015-01-05 17:18:51 -0800281
mtklein36352bf2015-03-25 18:17:31 -0700282 SkISize onISize() override {
reeda0c814c2014-10-22 13:20:58 -0700283 return SkISize::Make(1024, 850);
284 }
tfarinaaa458fb2015-01-05 17:18:51 -0800285
mtklein36352bf2015-03-25 18:17:31 -0700286 void onDraw(SkCanvas* canvas) override {
reeda0c814c2014-10-22 13:20:58 -0700287 SkPaint paint;
288 paint.setAntiAlias(true);
289 paint.setSubpixelText(true);
290 paint.setTextSize(100);
reed8893e5f2014-12-15 13:27:26 -0800291 paint.setTextScaleX(fScaleX);
292 paint.setTextSkewX(fSkewX);
reeda0c814c2014-10-22 13:20:58 -0700293
294 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
halcanary9d524f22016-03-29 09:03:52 -0700295
Hal Canarycefc4312016-11-04 16:26:16 -0400296 SkFontMgr* fm = fFM.get();
reeda0c814c2014-10-22 13:20:58 -0700297 int count = SkMin32(fm->countFamilies(), 32);
298
299 int index = 0;
300 SkScalar x = 0, y = 0;
301
Ben Wagner219f3622017-07-17 15:32:25 -0400302 canvas->translate(10, 120);
reeda0c814c2014-10-22 13:20:58 -0700303
304 for (int i = 0; i < count; ++i) {
Hal Canarycefc4312016-11-04 16:26:16 -0400305 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
Ben Wagner219f3622017-07-17 15:32:25 -0400306 for (int j = 0; j < set->count() && j < 3; ++j) {
bungeman13b9c952016-05-12 10:09:30 -0700307 paint.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
Ben Wagner2112df02017-07-24 11:04:21 -0400308 // 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 Wagner219f3622017-07-17 15:32:25 -0400311 SkRect fontBounds = paint.getFontBounds();
312 x -= fontBounds.fLeft;
reeda0c814c2014-10-22 13:20:58 -0700313 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
Ben Wagner219f3622017-07-17 15:32:25 -0400314 x += fontBounds.fRight + 20;
reeda0c814c2014-10-22 13:20:58 -0700315 index += 1;
Ben Wagner219f3622017-07-17 15:32:25 -0400316 if (x > 900) {
reeda0c814c2014-10-22 13:20:58 -0700317 x = 0;
318 y += 160;
319 }
Ben Wagner219f3622017-07-17 15:32:25 -0400320 if (y >= 700) {
reeda0c814c2014-10-22 13:20:58 -0700321 return;
322 }
323 }
324 }
325 }
326 }
mtklein1c402922015-01-23 11:07:07 -0800327
reeda0c814c2014-10-22 13:20:58 -0700328private:
Hal Canarycefc4312016-11-04 16:26:16 -0400329 sk_sp<SkFontMgr> fFM;
reeda0c814c2014-10-22 13:20:58 -0700330 SkString fName;
reed8893e5f2014-12-15 13:27:26 -0800331 SkScalar fScaleX, fSkewX;
reeda0c814c2014-10-22 13:20:58 -0700332 typedef GM INHERITED;
333};
334
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000335//////////////////////////////////////////////////////////////////////////////
336
halcanary385fe4d2015-08-26 13:07:48 -0700337DEF_GM(return new FontMgrGM;)
338DEF_GM(return new FontMgrMatchGM;)
339DEF_GM(return new FontMgrBoundsGM(1.0, 0);)
340DEF_GM(return new FontMgrBoundsGM(0.75, 0);)
341DEF_GM(return new FontMgrBoundsGM(1.0, -0.25);)