blob: 14ea7ea3770218e9181adc92684d627fc85ca706 [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"
9#include "SkCanvas.h"
10#include "SkFontMgr.h"
11#include "SkGraphics.h"
12#include "SkTypeface.h"
13
bungeman@google.combfc6cc42013-08-21 15:20:43 +000014#ifdef SK_BUILD_FOR_WIN
reed@google.comd1bcfc92013-08-28 20:31:58 +000015 #include "SkTypeface_win.h"
bungeman@google.combfc6cc42013-08-21 15:20:43 +000016#endif
17
reed@google.comaf0fa6a2013-03-28 13:39:35 +000018// limit this just so we don't take too long to draw
19#define MAX_FAMILIES 30
20
21static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
22 SkScalar y, const SkPaint& paint) {
23 canvas->drawText(text.c_str(), text.size(), x, y, paint);
24 return x + paint.measureText(text.c_str(), text.size());
25}
26
djsollen0d393a92014-08-27 07:03:13 -070027static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
28 SkScalar y, SkPaint& paint, SkFontMgr* fm,
bungemanc9232dc2014-11-10 13:29:33 -080029 const char* fontName, const char* bcp47[], int bcp47Count,
djsollen0d393a92014-08-27 07:03:13 -070030 const SkFontStyle& fontStyle) {
31 // find typeface containing the requested character and draw it
32 SkString ch;
33 ch.appendUnichar(character);
bungemanc9232dc2014-11-10 13:29:33 -080034 SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle,
35 bcp47, bcp47Count, character);
djsollen0d393a92014-08-27 07:03:13 -070036 SkSafeUnref(paint.setTypeface(typeface));
37 x = drawString(canvas, ch, x, y, paint) + 20;
38
39 if (NULL == typeface) {
40 return x;
41 }
42
43 // repeat the process, but this time use the family name of the typeface
44 // from the first pass. This emulates the behavior in Blink where it
45 // it expects to get the same glyph when following this pattern.
46 SkString familyName;
47 typeface->getFamilyName(&familyName);
48 SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style());
49 SkSafeUnref(paint.setTypeface(typefaceCopy));
50 return drawString(canvas, ch, x, y, paint) + 20;
51}
52
bungemanc9232dc2014-11-10 13:29:33 -080053static const char* zh = "zh";
54static const char* ja = "ja";
55
reed@google.comaf0fa6a2013-03-28 13:39:35 +000056class FontMgrGM : public skiagm::GM {
57public:
bungeman@google.com6eddc772014-03-31 19:18:07 +000058 FontMgrGM(SkFontMgr* fontMgr = NULL) {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000059 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000060
61 fName.set("fontmgr_iter");
bungeman@google.com6eddc772014-03-31 19:18:07 +000062 if (fontMgr) {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000063 fName.append("_factory");
bungeman@google.com6eddc772014-03-31 19:18:07 +000064 fFM.reset(fontMgr);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000065 } else {
66 fFM.reset(SkFontMgr::RefDefault());
67 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000068 }
69
70protected:
mtklein36352bf2015-03-25 18:17:31 -070071 SkString onShortName() override {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000072 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000073 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 SkISize onISize() override {
djsollen0d393a92014-08-27 07:03:13 -070076 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000077 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 void onDraw(SkCanvas* canvas) override {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000080 SkScalar y = 20;
81 SkPaint paint;
82 paint.setAntiAlias(true);
83 paint.setLCDRenderText(true);
84 paint.setSubpixelText(true);
85 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000086
bungeman@google.combfc6cc42013-08-21 15:20:43 +000087 SkFontMgr* fm = fFM;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000088 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
89
90 for (int i = 0; i < count; ++i) {
bungemanc64239a2015-04-29 08:15:31 -070091 SkString familyName;
92 fm->getFamilyName(i, &familyName);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000093 paint.setTypeface(NULL);
bungemanc64239a2015-04-29 08:15:31 -070094 (void)drawString(canvas, familyName, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000095
reed@google.comaf0fa6a2013-03-28 13:39:35 +000096 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +000097
reed@google.comaf0fa6a2013-03-28 13:39:35 +000098 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
99 for (int j = 0; j < set->count(); ++j) {
100 SkString sname;
101 SkFontStyle fs;
102 set->getStyle(j, &fs, &sname);
reed@google.com51116482013-04-24 19:14:39 +0000103 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000104
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000105 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
106 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -0700107
108 // check to see that we get different glyphs in japanese and chinese
bungemanc64239a2015-04-29 08:15:31 -0700109 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &zh, 1, fs);
110 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, familyName.c_str(), &ja, 1, fs);
djsollen0d393a92014-08-27 07:03:13 -0700111 // check that emoji characters are found
bungemanc64239a2015-04-29 08:15:31 -0700112 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, familyName.c_str(), NULL,0, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000113 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000114 y += 24;
115 }
116 }
117
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000118private:
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000119 SkAutoTUnref<SkFontMgr> fFM;
120 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000121 typedef GM INHERITED;
122};
123
reed@google.com964988f2013-03-29 14:57:22 +0000124class FontMgrMatchGM : public skiagm::GM {
125 SkAutoTUnref<SkFontMgr> fFM;
126
127public:
128 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
129 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
130 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000131
reed@google.com964988f2013-03-29 14:57:22 +0000132protected:
mtklein36352bf2015-03-25 18:17:31 -0700133 SkString onShortName() override {
reed@google.com964988f2013-03-29 14:57:22 +0000134 return SkString("fontmgr_match");
135 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000136
mtklein36352bf2015-03-25 18:17:31 -0700137 SkISize onISize() override {
reed@google.com964988f2013-03-29 14:57:22 +0000138 return SkISize::Make(640, 1024);
139 }
140
141 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
142 SkFontStyleSet* fset) {
143 SkPaint p(paint);
144 SkScalar y = 0;
145
146 for (int j = 0; j < fset->count(); ++j) {
147 SkString sname;
148 SkFontStyle fs;
149 fset->getStyle(j, &fs, &sname);
150
151 sname.appendf(" [%d %d]", fs.weight(), fs.width());
152
153 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
154 (void)drawString(canvas, sname, 0, y, p);
155 y += 24;
156 }
157 }
158
159 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
160 SkFontStyleSet* fset) {
161 SkPaint p(paint);
162 SkScalar y = 0;
163
164 for (int weight = 100; weight <= 900; weight += 200) {
165 for (int width = 1; width <= 9; width += 2) {
166 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
167 SkTypeface* face = fset->matchStyle(fs);
168 if (face) {
169 SkString str;
170 str.printf("request [%d %d]", fs.weight(), fs.width());
171 p.setTypeface(face)->unref();
172 (void)drawString(canvas, str, 0, y, p);
173 y += 24;
174 }
175 }
176 }
177 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000178
mtklein36352bf2015-03-25 18:17:31 -0700179 void onDraw(SkCanvas* canvas) override {
reed@google.com964988f2013-03-29 14:57:22 +0000180 SkPaint paint;
181 paint.setAntiAlias(true);
182 paint.setLCDRenderText(true);
183 paint.setSubpixelText(true);
184 paint.setTextSize(17);
185
186 static const char* gNames[] = {
187 "Helvetica Neue", "Arial"
188 };
189
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000190 SkAutoTUnref<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000191 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000192 fset.reset(fFM->matchFamily(gNames[i]));
193 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000194 break;
195 }
196 }
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000197 if (NULL == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000198 return;
199 }
reed@google.com964988f2013-03-29 14:57:22 +0000200
201 canvas->translate(20, 40);
202 this->exploreFamily(canvas, paint, fset);
203 canvas->translate(150, 0);
204 this->iterateFamily(canvas, paint, fset);
205 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000206
reed@google.com964988f2013-03-29 14:57:22 +0000207private:
208 typedef GM INHERITED;
209};
210
reeda0c814c2014-10-22 13:20:58 -0700211class FontMgrBoundsGM : public skiagm::GM {
212public:
reed8893e5f2014-12-15 13:27:26 -0800213 FontMgrBoundsGM(double scale, double skew)
214 : fScaleX(SkDoubleToScalar(scale))
215 , fSkewX(SkDoubleToScalar(skew))
216 {
reeda0c814c2014-10-22 13:20:58 -0700217 fName.set("fontmgr_bounds");
reed8893e5f2014-12-15 13:27:26 -0800218 if (scale != 1 || skew != 0) {
219 fName.appendf("_%g_%g", scale, skew);
220 }
reeda0c814c2014-10-22 13:20:58 -0700221 fFM.reset(SkFontMgr::RefDefault());
222 }
223
224 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
225 SkColor boundsColor) {
226 const char str[] = "jyHO[]{}@-_&%$";
227
reeda0c814c2014-10-22 13:20:58 -0700228 for (int i = 0; str[i]; ++i) {
229 canvas->drawText(&str[i], 1, x, y, paint);
230 }
tfarinaaa458fb2015-01-05 17:18:51 -0800231
reed8893e5f2014-12-15 13:27:26 -0800232 SkRect r = paint.getFontBounds();
reeda0c814c2014-10-22 13:20:58 -0700233 r.offset(x, y);
234 SkPaint p(paint);
235 p.setColor(boundsColor);
236 canvas->drawRect(r, p);
237 }
238
239protected:
mtklein36352bf2015-03-25 18:17:31 -0700240 SkString onShortName() override {
reeda0c814c2014-10-22 13:20:58 -0700241 return fName;
242 }
tfarinaaa458fb2015-01-05 17:18:51 -0800243
mtklein36352bf2015-03-25 18:17:31 -0700244 SkISize onISize() override {
reeda0c814c2014-10-22 13:20:58 -0700245 return SkISize::Make(1024, 850);
246 }
tfarinaaa458fb2015-01-05 17:18:51 -0800247
mtklein36352bf2015-03-25 18:17:31 -0700248 void onDraw(SkCanvas* canvas) override {
reeda0c814c2014-10-22 13:20:58 -0700249 SkPaint paint;
250 paint.setAntiAlias(true);
251 paint.setSubpixelText(true);
252 paint.setTextSize(100);
253 paint.setStyle(SkPaint::kStroke_Style);
reed8893e5f2014-12-15 13:27:26 -0800254 paint.setTextScaleX(fScaleX);
255 paint.setTextSkewX(fSkewX);
reeda0c814c2014-10-22 13:20:58 -0700256
257 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
258
259 SkFontMgr* fm = fFM;
260 int count = SkMin32(fm->countFamilies(), 32);
261
262 int index = 0;
263 SkScalar x = 0, y = 0;
264
265 canvas->translate(80, 120);
266
267 for (int i = 0; i < count; ++i) {
268 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
269 for (int j = 0; j < set->count(); ++j) {
270 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
271 if (paint.getTypeface()) {
272 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
273 index += 1;
274 x += 160;
275 if (0 == (index % 6)) {
276 x = 0;
277 y += 160;
278 }
279 if (index >= 30) {
280 return;
281 }
282 }
283 }
284 }
285 }
mtklein1c402922015-01-23 11:07:07 -0800286
reeda0c814c2014-10-22 13:20:58 -0700287private:
288 SkAutoTUnref<SkFontMgr> fFM;
289 SkString fName;
reed8893e5f2014-12-15 13:27:26 -0800290 SkScalar fScaleX, fSkewX;
reeda0c814c2014-10-22 13:20:58 -0700291 typedef GM INHERITED;
292};
293
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000294//////////////////////////////////////////////////////////////////////////////
295
296DEF_GM( return SkNEW(FontMgrGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000297DEF_GM( return SkNEW(FontMgrMatchGM); )
reed8893e5f2014-12-15 13:27:26 -0800298DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, 0)); )
299DEF_GM( return SkNEW(FontMgrBoundsGM(0.75, 0)); )
300DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, -0.25)); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000301
302#ifdef SK_BUILD_FOR_WIN
bungeman@google.com6eddc772014-03-31 19:18:07 +0000303 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000304#endif