blob: 99cff6e4d38a0c95cab7fc7d22132edd2c514083 [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,
bungemanc9232dc2014-11-10 13:29:33 -080036 const char* fontName, const char* bcp47[], int bcp47Count,
djsollen0d393a92014-08-27 07:03:13 -070037 const SkFontStyle& fontStyle) {
38 // find typeface containing the requested character and draw it
39 SkString ch;
40 ch.appendUnichar(character);
bungemanc9232dc2014-11-10 13:29:33 -080041 SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle,
42 bcp47, bcp47Count, character);
djsollen0d393a92014-08-27 07:03:13 -070043 SkSafeUnref(paint.setTypeface(typeface));
44 x = drawString(canvas, ch, x, y, paint) + 20;
45
46 if (NULL == typeface) {
47 return x;
48 }
49
50 // repeat the process, but this time use the family name of the typeface
51 // from the first pass. This emulates the behavior in Blink where it
52 // it expects to get the same glyph when following this pattern.
53 SkString familyName;
54 typeface->getFamilyName(&familyName);
55 SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style());
56 SkSafeUnref(paint.setTypeface(typefaceCopy));
57 return drawString(canvas, ch, x, y, paint) + 20;
58}
59
bungemanc9232dc2014-11-10 13:29:33 -080060static const char* zh = "zh";
61static const char* ja = "ja";
62
reed@google.comaf0fa6a2013-03-28 13:39:35 +000063class FontMgrGM : public skiagm::GM {
64public:
bungeman@google.com6eddc772014-03-31 19:18:07 +000065 FontMgrGM(SkFontMgr* fontMgr = NULL) {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000066 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000067
68 fName.set("fontmgr_iter");
bungeman@google.com6eddc772014-03-31 19:18:07 +000069 if (fontMgr) {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000070 fName.append("_factory");
bungeman@google.com6eddc772014-03-31 19:18:07 +000071 fFM.reset(fontMgr);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000072 } else {
73 fFM.reset(SkFontMgr::RefDefault());
74 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000075 }
76
77protected:
78 virtual SkString onShortName() {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000079 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000080 }
81
82 virtual SkISize onISize() {
djsollen0d393a92014-08-27 07:03:13 -070083 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000084 }
85
86 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
87 SkScalar y = 20;
88 SkPaint paint;
89 paint.setAntiAlias(true);
90 paint.setLCDRenderText(true);
91 paint.setSubpixelText(true);
92 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000093
bungeman@google.combfc6cc42013-08-21 15:20:43 +000094 SkFontMgr* fm = fFM;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000095 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
96
97 for (int i = 0; i < count; ++i) {
98 SkString fname;
99 fm->getFamilyName(i, &fname);
100 paint.setTypeface(NULL);
101 (void)drawString(canvas, fname, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000102
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000103 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +0000104
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000105 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
106 for (int j = 0; j < set->count(); ++j) {
107 SkString sname;
108 SkFontStyle fs;
109 set->getStyle(j, &fs, &sname);
reed@google.com51116482013-04-24 19:14:39 +0000110 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000111
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000112 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
113 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -0700114
115 // check to see that we get different glyphs in japanese and chinese
bungemanc9232dc2014-11-10 13:29:33 -0800116 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &zh, 1, fs);
117 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &ja, 1, fs);
djsollen0d393a92014-08-27 07:03:13 -0700118 // check that emoji characters are found
bungemanc9232dc2014-11-10 13:29:33 -0800119 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, 0, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000120 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000121 y += 24;
122 }
123 }
124
reed@google.com6518daf2013-03-28 15:03:22 +0000125 virtual uint32_t onGetFlags() const SK_OVERRIDE {
126 // fontdescriptors (and therefore serialization) don't yet understand
127 // these new styles, so skip tests that exercise that for now.
bungeman@google.com71033442013-05-01 14:21:20 +0000128
129 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
130 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
131
132 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
reed@google.com6518daf2013-03-28 15:03:22 +0000133 }
134
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000135private:
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000136 SkAutoTUnref<SkFontMgr> fFM;
137 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000138 typedef GM INHERITED;
139};
140
reed@google.com964988f2013-03-29 14:57:22 +0000141class FontMgrMatchGM : public skiagm::GM {
142 SkAutoTUnref<SkFontMgr> fFM;
143
144public:
145 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
146 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
147 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000148
reed@google.com964988f2013-03-29 14:57:22 +0000149protected:
150 virtual SkString onShortName() {
151 return SkString("fontmgr_match");
152 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000153
reed@google.com964988f2013-03-29 14:57:22 +0000154 virtual SkISize onISize() {
155 return SkISize::Make(640, 1024);
156 }
157
158 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
159 SkFontStyleSet* fset) {
160 SkPaint p(paint);
161 SkScalar y = 0;
162
163 for (int j = 0; j < fset->count(); ++j) {
164 SkString sname;
165 SkFontStyle fs;
166 fset->getStyle(j, &fs, &sname);
167
168 sname.appendf(" [%d %d]", fs.weight(), fs.width());
169
170 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
171 (void)drawString(canvas, sname, 0, y, p);
172 y += 24;
173 }
174 }
175
176 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
177 SkFontStyleSet* fset) {
178 SkPaint p(paint);
179 SkScalar y = 0;
180
181 for (int weight = 100; weight <= 900; weight += 200) {
182 for (int width = 1; width <= 9; width += 2) {
183 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
184 SkTypeface* face = fset->matchStyle(fs);
185 if (face) {
186 SkString str;
187 str.printf("request [%d %d]", fs.weight(), fs.width());
188 p.setTypeface(face)->unref();
189 (void)drawString(canvas, str, 0, y, p);
190 y += 24;
191 }
192 }
193 }
194 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000195
reed@google.com964988f2013-03-29 14:57:22 +0000196 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
197 SkPaint paint;
198 paint.setAntiAlias(true);
199 paint.setLCDRenderText(true);
200 paint.setSubpixelText(true);
201 paint.setTextSize(17);
202
203 static const char* gNames[] = {
204 "Helvetica Neue", "Arial"
205 };
206
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000207 SkAutoTUnref<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000208 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000209 fset.reset(fFM->matchFamily(gNames[i]));
210 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000211 break;
212 }
213 }
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000214 if (NULL == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000215 return;
216 }
reed@google.com964988f2013-03-29 14:57:22 +0000217
218 canvas->translate(20, 40);
219 this->exploreFamily(canvas, paint, fset);
220 canvas->translate(150, 0);
221 this->iterateFamily(canvas, paint, fset);
222 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000223
reed@google.com964988f2013-03-29 14:57:22 +0000224 virtual uint32_t onGetFlags() const SK_OVERRIDE {
225 // fontdescriptors (and therefore serialization) don't yet understand
226 // these new styles, so skip tests that exercise that for now.
227 return kSkipPicture_Flag | kSkipPipe_Flag;
228 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000229
reed@google.com964988f2013-03-29 14:57:22 +0000230private:
231 typedef GM INHERITED;
232};
233
reeda0c814c2014-10-22 13:20:58 -0700234class FontMgrBoundsGM : public skiagm::GM {
235public:
236 FontMgrBoundsGM() {
237 fName.set("fontmgr_bounds");
238 fFM.reset(SkFontMgr::RefDefault());
239 }
240
241 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
242 SkColor boundsColor) {
243 const char str[] = "jyHO[]{}@-_&%$";
244
245 const SkTypeface* tf = paint.getTypeface();
246 for (int i = 0; str[i]; ++i) {
247 canvas->drawText(&str[i], 1, x, y, paint);
248 }
249
250 SkRect r = tf->getBounds();
251 scale(&r, paint.getTextSize());
252 r.offset(x, y);
253 SkPaint p(paint);
254 p.setColor(boundsColor);
255 canvas->drawRect(r, p);
256 }
257
258protected:
259 virtual SkString onShortName() {
260 return fName;
261 }
262
263 virtual SkISize onISize() {
264 return SkISize::Make(1024, 850);
265 }
266
267 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
268 SkPaint paint;
269 paint.setAntiAlias(true);
270 paint.setSubpixelText(true);
271 paint.setTextSize(100);
272 paint.setStyle(SkPaint::kStroke_Style);
273
274 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
275
276 SkFontMgr* fm = fFM;
277 int count = SkMin32(fm->countFamilies(), 32);
278
279 int index = 0;
280 SkScalar x = 0, y = 0;
281
282 canvas->translate(80, 120);
283
284 for (int i = 0; i < count; ++i) {
285 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
286 for (int j = 0; j < set->count(); ++j) {
287 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
288 if (paint.getTypeface()) {
289 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
290 index += 1;
291 x += 160;
292 if (0 == (index % 6)) {
293 x = 0;
294 y += 160;
295 }
296 if (index >= 30) {
297 return;
298 }
299 }
300 }
301 }
302 }
303
304 virtual uint32_t onGetFlags() const SK_OVERRIDE {
305 // fontdescriptors (and therefore serialization) don't yet understand
306 // these new styles, so skip tests that exercise that for now.
307
308 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
309 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
310
311 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
312 }
313
314private:
315 SkAutoTUnref<SkFontMgr> fFM;
316 SkString fName;
317 typedef GM INHERITED;
318};
319
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000320//////////////////////////////////////////////////////////////////////////////
321
322DEF_GM( return SkNEW(FontMgrGM); )
reeda0c814c2014-10-22 13:20:58 -0700323DEF_GM( return SkNEW(FontMgrBoundsGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000324DEF_GM( return SkNEW(FontMgrMatchGM); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000325
326#ifdef SK_BUILD_FOR_WIN
bungeman@google.com6eddc772014-03-31 19:18:07 +0000327 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000328#endif