blob: a1e7ff2a6861cf27b608d4aa687548c51350cd05 [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
reeda0c814c2014-10-22 13:20:58 -070018static void scale(SkRect* rect, SkScalar scale) {
19 rect->fLeft *= scale;
20 rect->fTop *= scale;
21 rect->fRight *= scale;
22 rect->fBottom *= scale;
23}
24
reed@google.comaf0fa6a2013-03-28 13:39:35 +000025// limit this just so we don't take too long to draw
26#define MAX_FAMILIES 30
27
28static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
29 SkScalar y, const SkPaint& paint) {
30 canvas->drawText(text.c_str(), text.size(), x, y, paint);
31 return x + paint.measureText(text.c_str(), text.size());
32}
33
djsollen0d393a92014-08-27 07:03:13 -070034static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
35 SkScalar y, SkPaint& paint, SkFontMgr* fm,
36 const char* fontName, const char* bpc47,
37 const SkFontStyle& fontStyle) {
38 // find typeface containing the requested character and draw it
39 SkString ch;
40 ch.appendUnichar(character);
41 SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle, bpc47, character);
42 SkSafeUnref(paint.setTypeface(typeface));
43 x = drawString(canvas, ch, x, y, paint) + 20;
44
45 if (NULL == typeface) {
46 return x;
47 }
48
49 // repeat the process, but this time use the family name of the typeface
50 // from the first pass. This emulates the behavior in Blink where it
51 // it expects to get the same glyph when following this pattern.
52 SkString familyName;
53 typeface->getFamilyName(&familyName);
54 SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style());
55 SkSafeUnref(paint.setTypeface(typefaceCopy));
56 return drawString(canvas, ch, x, y, paint) + 20;
57}
58
reed@google.comaf0fa6a2013-03-28 13:39:35 +000059class FontMgrGM : public skiagm::GM {
60public:
bungeman@google.com6eddc772014-03-31 19:18:07 +000061 FontMgrGM(SkFontMgr* fontMgr = NULL) {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000062 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000063
64 fName.set("fontmgr_iter");
bungeman@google.com6eddc772014-03-31 19:18:07 +000065 if (fontMgr) {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000066 fName.append("_factory");
bungeman@google.com6eddc772014-03-31 19:18:07 +000067 fFM.reset(fontMgr);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000068 } else {
69 fFM.reset(SkFontMgr::RefDefault());
70 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000071 }
72
73protected:
74 virtual SkString onShortName() {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000075 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000076 }
77
78 virtual SkISize onISize() {
djsollen0d393a92014-08-27 07:03:13 -070079 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000080 }
81
82 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
83 SkScalar y = 20;
84 SkPaint paint;
85 paint.setAntiAlias(true);
86 paint.setLCDRenderText(true);
87 paint.setSubpixelText(true);
88 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000089
bungeman@google.combfc6cc42013-08-21 15:20:43 +000090 SkFontMgr* fm = fFM;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000091 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
92
93 for (int i = 0; i < count; ++i) {
94 SkString fname;
95 fm->getFamilyName(i, &fname);
96 paint.setTypeface(NULL);
97 (void)drawString(canvas, fname, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000098
reed@google.comaf0fa6a2013-03-28 13:39:35 +000099 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +0000100
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000101 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
102 for (int j = 0; j < set->count(); ++j) {
103 SkString sname;
104 SkFontStyle fs;
105 set->getStyle(j, &fs, &sname);
reed@google.com51116482013-04-24 19:14:39 +0000106 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000107
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000108 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
109 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -0700110
111 // check to see that we get different glyphs in japanese and chinese
112 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "zh", fs);
113 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "ja", fs);
114 // check that emoji characters are found
115 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000116 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000117 y += 24;
118 }
119 }
120
reed@google.com6518daf2013-03-28 15:03:22 +0000121 virtual uint32_t onGetFlags() const SK_OVERRIDE {
122 // fontdescriptors (and therefore serialization) don't yet understand
123 // these new styles, so skip tests that exercise that for now.
bungeman@google.com71033442013-05-01 14:21:20 +0000124
125 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
126 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
127
128 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
reed@google.com6518daf2013-03-28 15:03:22 +0000129 }
130
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000131private:
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000132 SkAutoTUnref<SkFontMgr> fFM;
133 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000134 typedef GM INHERITED;
135};
136
reed@google.com964988f2013-03-29 14:57:22 +0000137class FontMgrMatchGM : public skiagm::GM {
138 SkAutoTUnref<SkFontMgr> fFM;
139
140public:
141 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
142 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
143 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000144
reed@google.com964988f2013-03-29 14:57:22 +0000145protected:
146 virtual SkString onShortName() {
147 return SkString("fontmgr_match");
148 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000149
reed@google.com964988f2013-03-29 14:57:22 +0000150 virtual SkISize onISize() {
151 return SkISize::Make(640, 1024);
152 }
153
154 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
155 SkFontStyleSet* fset) {
156 SkPaint p(paint);
157 SkScalar y = 0;
158
159 for (int j = 0; j < fset->count(); ++j) {
160 SkString sname;
161 SkFontStyle fs;
162 fset->getStyle(j, &fs, &sname);
163
164 sname.appendf(" [%d %d]", fs.weight(), fs.width());
165
166 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
167 (void)drawString(canvas, sname, 0, y, p);
168 y += 24;
169 }
170 }
171
172 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
173 SkFontStyleSet* fset) {
174 SkPaint p(paint);
175 SkScalar y = 0;
176
177 for (int weight = 100; weight <= 900; weight += 200) {
178 for (int width = 1; width <= 9; width += 2) {
179 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
180 SkTypeface* face = fset->matchStyle(fs);
181 if (face) {
182 SkString str;
183 str.printf("request [%d %d]", fs.weight(), fs.width());
184 p.setTypeface(face)->unref();
185 (void)drawString(canvas, str, 0, y, p);
186 y += 24;
187 }
188 }
189 }
190 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000191
reed@google.com964988f2013-03-29 14:57:22 +0000192 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
193 SkPaint paint;
194 paint.setAntiAlias(true);
195 paint.setLCDRenderText(true);
196 paint.setSubpixelText(true);
197 paint.setTextSize(17);
198
199 static const char* gNames[] = {
200 "Helvetica Neue", "Arial"
201 };
202
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000203 SkAutoTUnref<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000204 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000205 fset.reset(fFM->matchFamily(gNames[i]));
206 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000207 break;
208 }
209 }
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000210 if (NULL == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000211 return;
212 }
reed@google.com964988f2013-03-29 14:57:22 +0000213
214 canvas->translate(20, 40);
215 this->exploreFamily(canvas, paint, fset);
216 canvas->translate(150, 0);
217 this->iterateFamily(canvas, paint, fset);
218 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000219
reed@google.com964988f2013-03-29 14:57:22 +0000220 virtual uint32_t onGetFlags() const SK_OVERRIDE {
221 // fontdescriptors (and therefore serialization) don't yet understand
222 // these new styles, so skip tests that exercise that for now.
223 return kSkipPicture_Flag | kSkipPipe_Flag;
224 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000225
reed@google.com964988f2013-03-29 14:57:22 +0000226private:
227 typedef GM INHERITED;
228};
229
reeda0c814c2014-10-22 13:20:58 -0700230class FontMgrBoundsGM : public skiagm::GM {
231public:
232 FontMgrBoundsGM() {
233 fName.set("fontmgr_bounds");
234 fFM.reset(SkFontMgr::RefDefault());
235 }
236
237 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
238 SkColor boundsColor) {
239 const char str[] = "jyHO[]{}@-_&%$";
240
241 const SkTypeface* tf = paint.getTypeface();
242 for (int i = 0; str[i]; ++i) {
243 canvas->drawText(&str[i], 1, x, y, paint);
244 }
245
246 SkRect r = tf->getBounds();
247 scale(&r, paint.getTextSize());
248 r.offset(x, y);
249 SkPaint p(paint);
250 p.setColor(boundsColor);
251 canvas->drawRect(r, p);
252 }
253
254protected:
255 virtual SkString onShortName() {
256 return fName;
257 }
258
259 virtual SkISize onISize() {
260 return SkISize::Make(1024, 850);
261 }
262
263 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
264 SkPaint paint;
265 paint.setAntiAlias(true);
266 paint.setSubpixelText(true);
267 paint.setTextSize(100);
268 paint.setStyle(SkPaint::kStroke_Style);
269
270 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
271
272 SkFontMgr* fm = fFM;
273 int count = SkMin32(fm->countFamilies(), 32);
274
275 int index = 0;
276 SkScalar x = 0, y = 0;
277
278 canvas->translate(80, 120);
279
280 for (int i = 0; i < count; ++i) {
281 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
282 for (int j = 0; j < set->count(); ++j) {
283 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
284 if (paint.getTypeface()) {
285 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
286 index += 1;
287 x += 160;
288 if (0 == (index % 6)) {
289 x = 0;
290 y += 160;
291 }
292 if (index >= 30) {
293 return;
294 }
295 }
296 }
297 }
298 }
299
300 virtual uint32_t onGetFlags() const SK_OVERRIDE {
301 // fontdescriptors (and therefore serialization) don't yet understand
302 // these new styles, so skip tests that exercise that for now.
303
304 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
305 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
306
307 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
308 }
309
310private:
311 SkAutoTUnref<SkFontMgr> fFM;
312 SkString fName;
313 typedef GM INHERITED;
314};
315
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000316//////////////////////////////////////////////////////////////////////////////
317
318DEF_GM( return SkNEW(FontMgrGM); )
reeda0c814c2014-10-22 13:20:58 -0700319DEF_GM( return SkNEW(FontMgrBoundsGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000320DEF_GM( return SkNEW(FontMgrMatchGM); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000321
322#ifdef SK_BUILD_FOR_WIN
bungeman@google.com6eddc772014-03-31 19:18:07 +0000323 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000324#endif