blob: 865090a4c44fbac05233b7b55dabbc57000ff843 [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:
71 virtual SkString onShortName() {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000072 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000073 }
74
75 virtual SkISize onISize() {
djsollen0d393a92014-08-27 07:03:13 -070076 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000077 }
78
79 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
80 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) {
91 SkString fname;
92 fm->getFamilyName(i, &fname);
93 paint.setTypeface(NULL);
94 (void)drawString(canvas, fname, 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
bungemanc9232dc2014-11-10 13:29:33 -0800109 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &zh, 1, fs);
110 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &ja, 1, fs);
djsollen0d393a92014-08-27 07:03:13 -0700111 // check that emoji characters are found
bungemanc9232dc2014-11-10 13:29:33 -0800112 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.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.com6518daf2013-03-28 15:03:22 +0000118 virtual uint32_t onGetFlags() const SK_OVERRIDE {
119 // fontdescriptors (and therefore serialization) don't yet understand
120 // these new styles, so skip tests that exercise that for now.
bungeman@google.com71033442013-05-01 14:21:20 +0000121
122 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
123 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
124
125 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
reed@google.com6518daf2013-03-28 15:03:22 +0000126 }
127
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000128private:
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000129 SkAutoTUnref<SkFontMgr> fFM;
130 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000131 typedef GM INHERITED;
132};
133
reed@google.com964988f2013-03-29 14:57:22 +0000134class FontMgrMatchGM : public skiagm::GM {
135 SkAutoTUnref<SkFontMgr> fFM;
136
137public:
138 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
139 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
140 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000141
reed@google.com964988f2013-03-29 14:57:22 +0000142protected:
143 virtual SkString onShortName() {
144 return SkString("fontmgr_match");
145 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000146
reed@google.com964988f2013-03-29 14:57:22 +0000147 virtual SkISize onISize() {
148 return SkISize::Make(640, 1024);
149 }
150
151 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
152 SkFontStyleSet* fset) {
153 SkPaint p(paint);
154 SkScalar y = 0;
155
156 for (int j = 0; j < fset->count(); ++j) {
157 SkString sname;
158 SkFontStyle fs;
159 fset->getStyle(j, &fs, &sname);
160
161 sname.appendf(" [%d %d]", fs.weight(), fs.width());
162
163 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
164 (void)drawString(canvas, sname, 0, y, p);
165 y += 24;
166 }
167 }
168
169 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
170 SkFontStyleSet* fset) {
171 SkPaint p(paint);
172 SkScalar y = 0;
173
174 for (int weight = 100; weight <= 900; weight += 200) {
175 for (int width = 1; width <= 9; width += 2) {
176 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
177 SkTypeface* face = fset->matchStyle(fs);
178 if (face) {
179 SkString str;
180 str.printf("request [%d %d]", fs.weight(), fs.width());
181 p.setTypeface(face)->unref();
182 (void)drawString(canvas, str, 0, y, p);
183 y += 24;
184 }
185 }
186 }
187 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000188
reed@google.com964988f2013-03-29 14:57:22 +0000189 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
190 SkPaint paint;
191 paint.setAntiAlias(true);
192 paint.setLCDRenderText(true);
193 paint.setSubpixelText(true);
194 paint.setTextSize(17);
195
196 static const char* gNames[] = {
197 "Helvetica Neue", "Arial"
198 };
199
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000200 SkAutoTUnref<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000201 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000202 fset.reset(fFM->matchFamily(gNames[i]));
203 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000204 break;
205 }
206 }
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000207 if (NULL == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000208 return;
209 }
reed@google.com964988f2013-03-29 14:57:22 +0000210
211 canvas->translate(20, 40);
212 this->exploreFamily(canvas, paint, fset);
213 canvas->translate(150, 0);
214 this->iterateFamily(canvas, paint, fset);
215 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000216
reed@google.com964988f2013-03-29 14:57:22 +0000217 virtual uint32_t onGetFlags() const SK_OVERRIDE {
218 // fontdescriptors (and therefore serialization) don't yet understand
219 // these new styles, so skip tests that exercise that for now.
220 return kSkipPicture_Flag | kSkipPipe_Flag;
221 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000222
reed@google.com964988f2013-03-29 14:57:22 +0000223private:
224 typedef GM INHERITED;
225};
226
reeda0c814c2014-10-22 13:20:58 -0700227class FontMgrBoundsGM : public skiagm::GM {
228public:
reed8893e5f2014-12-15 13:27:26 -0800229 FontMgrBoundsGM(double scale, double skew)
230 : fScaleX(SkDoubleToScalar(scale))
231 , fSkewX(SkDoubleToScalar(skew))
232 {
reeda0c814c2014-10-22 13:20:58 -0700233 fName.set("fontmgr_bounds");
reed8893e5f2014-12-15 13:27:26 -0800234 if (scale != 1 || skew != 0) {
235 fName.appendf("_%g_%g", scale, skew);
236 }
reeda0c814c2014-10-22 13:20:58 -0700237 fFM.reset(SkFontMgr::RefDefault());
238 }
239
240 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
241 SkColor boundsColor) {
242 const char str[] = "jyHO[]{}@-_&%$";
243
reeda0c814c2014-10-22 13:20:58 -0700244 for (int i = 0; str[i]; ++i) {
245 canvas->drawText(&str[i], 1, x, y, paint);
246 }
247
reed8893e5f2014-12-15 13:27:26 -0800248 SkRect r = paint.getFontBounds();
reeda0c814c2014-10-22 13:20:58 -0700249 r.offset(x, y);
250 SkPaint p(paint);
251 p.setColor(boundsColor);
252 canvas->drawRect(r, p);
253 }
254
255protected:
256 virtual SkString onShortName() {
257 return fName;
258 }
259
260 virtual SkISize onISize() {
261 return SkISize::Make(1024, 850);
262 }
263
264 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
265 SkPaint paint;
266 paint.setAntiAlias(true);
267 paint.setSubpixelText(true);
268 paint.setTextSize(100);
269 paint.setStyle(SkPaint::kStroke_Style);
reed8893e5f2014-12-15 13:27:26 -0800270 paint.setTextScaleX(fScaleX);
271 paint.setTextSkewX(fSkewX);
reeda0c814c2014-10-22 13:20:58 -0700272
273 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
274
275 SkFontMgr* fm = fFM;
276 int count = SkMin32(fm->countFamilies(), 32);
277
278 int index = 0;
279 SkScalar x = 0, y = 0;
280
281 canvas->translate(80, 120);
282
283 for (int i = 0; i < count; ++i) {
284 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
285 for (int j = 0; j < set->count(); ++j) {
286 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
287 if (paint.getTypeface()) {
288 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
289 index += 1;
290 x += 160;
291 if (0 == (index % 6)) {
292 x = 0;
293 y += 160;
294 }
295 if (index >= 30) {
296 return;
297 }
298 }
299 }
300 }
301 }
302
303 virtual uint32_t onGetFlags() const SK_OVERRIDE {
304 // fontdescriptors (and therefore serialization) don't yet understand
305 // these new styles, so skip tests that exercise that for now.
306
307 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
308 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
309
310 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
311 }
312
313private:
314 SkAutoTUnref<SkFontMgr> fFM;
315 SkString fName;
reed8893e5f2014-12-15 13:27:26 -0800316 SkScalar fScaleX, fSkewX;
reeda0c814c2014-10-22 13:20:58 -0700317 typedef GM INHERITED;
318};
319
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000320//////////////////////////////////////////////////////////////////////////////
321
322DEF_GM( return SkNEW(FontMgrGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000323DEF_GM( return SkNEW(FontMgrMatchGM); )
reed8893e5f2014-12-15 13:27:26 -0800324DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, 0)); )
325DEF_GM( return SkNEW(FontMgrBoundsGM(0.75, 0)); )
326DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, -0.25)); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000327
328#ifdef SK_BUILD_FOR_WIN
bungeman@google.com6eddc772014-03-31 19:18:07 +0000329 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000330#endif