blob: 79868982f4a25854a5d7b4bee1aa498b3188438d [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,
29 const char* fontName, const char* bpc47,
30 const SkFontStyle& fontStyle) {
31 // find typeface containing the requested character and draw it
32 SkString ch;
33 ch.appendUnichar(character);
34 SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle, bpc47, character);
35 SkSafeUnref(paint.setTypeface(typeface));
36 x = drawString(canvas, ch, x, y, paint) + 20;
37
38 if (NULL == typeface) {
39 return x;
40 }
41
42 // repeat the process, but this time use the family name of the typeface
43 // from the first pass. This emulates the behavior in Blink where it
44 // it expects to get the same glyph when following this pattern.
45 SkString familyName;
46 typeface->getFamilyName(&familyName);
47 SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style());
48 SkSafeUnref(paint.setTypeface(typefaceCopy));
49 return drawString(canvas, ch, x, y, paint) + 20;
50}
51
reed@google.comaf0fa6a2013-03-28 13:39:35 +000052class FontMgrGM : public skiagm::GM {
53public:
bungeman@google.com6eddc772014-03-31 19:18:07 +000054 FontMgrGM(SkFontMgr* fontMgr = NULL) {
reed@google.comaf0fa6a2013-03-28 13:39:35 +000055 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000056
57 fName.set("fontmgr_iter");
bungeman@google.com6eddc772014-03-31 19:18:07 +000058 if (fontMgr) {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000059 fName.append("_factory");
bungeman@google.com6eddc772014-03-31 19:18:07 +000060 fFM.reset(fontMgr);
bungeman@google.combfc6cc42013-08-21 15:20:43 +000061 } else {
62 fFM.reset(SkFontMgr::RefDefault());
63 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000064 }
65
66protected:
67 virtual SkString onShortName() {
bungeman@google.combfc6cc42013-08-21 15:20:43 +000068 return fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000069 }
70
71 virtual SkISize onISize() {
djsollen0d393a92014-08-27 07:03:13 -070072 return SkISize::Make(1536, 768);
reed@google.comaf0fa6a2013-03-28 13:39:35 +000073 }
74
75 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
76 SkScalar y = 20;
77 SkPaint paint;
78 paint.setAntiAlias(true);
79 paint.setLCDRenderText(true);
80 paint.setSubpixelText(true);
81 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000082
bungeman@google.combfc6cc42013-08-21 15:20:43 +000083 SkFontMgr* fm = fFM;
reed@google.comaf0fa6a2013-03-28 13:39:35 +000084 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
85
86 for (int i = 0; i < count; ++i) {
87 SkString fname;
88 fm->getFamilyName(i, &fname);
89 paint.setTypeface(NULL);
90 (void)drawString(canvas, fname, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000091
reed@google.comaf0fa6a2013-03-28 13:39:35 +000092 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +000093
reed@google.comaf0fa6a2013-03-28 13:39:35 +000094 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
95 for (int j = 0; j < set->count(); ++j) {
96 SkString sname;
97 SkFontStyle fs;
98 set->getStyle(j, &fs, &sname);
reed@google.com51116482013-04-24 19:14:39 +000099 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000100
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000101 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
102 x = drawString(canvas, sname, x, y, paint) + 20;
djsollen0d393a92014-08-27 07:03:13 -0700103
104 // check to see that we get different glyphs in japanese and chinese
105 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "zh", fs);
106 x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "ja", fs);
107 // check that emoji characters are found
108 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, fs);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +0000109 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000110 y += 24;
111 }
112 }
113
reed@google.com6518daf2013-03-28 15:03:22 +0000114 virtual uint32_t onGetFlags() const SK_OVERRIDE {
115 // fontdescriptors (and therefore serialization) don't yet understand
116 // these new styles, so skip tests that exercise that for now.
bungeman@google.com71033442013-05-01 14:21:20 +0000117
118 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold),
119 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
120
121 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
reed@google.com6518daf2013-03-28 15:03:22 +0000122 }
123
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000124private:
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000125 SkAutoTUnref<SkFontMgr> fFM;
126 SkString fName;
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000127 typedef GM INHERITED;
128};
129
reed@google.com964988f2013-03-29 14:57:22 +0000130class FontMgrMatchGM : public skiagm::GM {
131 SkAutoTUnref<SkFontMgr> fFM;
132
133public:
134 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
135 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
136 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000137
reed@google.com964988f2013-03-29 14:57:22 +0000138protected:
139 virtual SkString onShortName() {
140 return SkString("fontmgr_match");
141 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000142
reed@google.com964988f2013-03-29 14:57:22 +0000143 virtual SkISize onISize() {
144 return SkISize::Make(640, 1024);
145 }
146
147 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
148 SkFontStyleSet* fset) {
149 SkPaint p(paint);
150 SkScalar y = 0;
151
152 for (int j = 0; j < fset->count(); ++j) {
153 SkString sname;
154 SkFontStyle fs;
155 fset->getStyle(j, &fs, &sname);
156
157 sname.appendf(" [%d %d]", fs.weight(), fs.width());
158
159 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
160 (void)drawString(canvas, sname, 0, y, p);
161 y += 24;
162 }
163 }
164
165 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
166 SkFontStyleSet* fset) {
167 SkPaint p(paint);
168 SkScalar y = 0;
169
170 for (int weight = 100; weight <= 900; weight += 200) {
171 for (int width = 1; width <= 9; width += 2) {
172 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
173 SkTypeface* face = fset->matchStyle(fs);
174 if (face) {
175 SkString str;
176 str.printf("request [%d %d]", fs.weight(), fs.width());
177 p.setTypeface(face)->unref();
178 (void)drawString(canvas, str, 0, y, p);
179 y += 24;
180 }
181 }
182 }
183 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000184
reed@google.com964988f2013-03-29 14:57:22 +0000185 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
186 SkPaint paint;
187 paint.setAntiAlias(true);
188 paint.setLCDRenderText(true);
189 paint.setSubpixelText(true);
190 paint.setTextSize(17);
191
192 static const char* gNames[] = {
193 "Helvetica Neue", "Arial"
194 };
195
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000196 SkAutoTUnref<SkFontStyleSet> fset;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000197 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000198 fset.reset(fFM->matchFamily(gNames[i]));
199 if (fset->count() > 0) {
reed@google.com964988f2013-03-29 14:57:22 +0000200 break;
201 }
202 }
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000203 if (NULL == fset.get()) {
reed@google.com964988f2013-03-29 14:57:22 +0000204 return;
205 }
reed@google.com964988f2013-03-29 14:57:22 +0000206
207 canvas->translate(20, 40);
208 this->exploreFamily(canvas, paint, fset);
209 canvas->translate(150, 0);
210 this->iterateFamily(canvas, paint, fset);
211 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000212
reed@google.com964988f2013-03-29 14:57:22 +0000213 virtual uint32_t onGetFlags() const SK_OVERRIDE {
214 // fontdescriptors (and therefore serialization) don't yet understand
215 // these new styles, so skip tests that exercise that for now.
216 return kSkipPicture_Flag | kSkipPipe_Flag;
217 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000218
reed@google.com964988f2013-03-29 14:57:22 +0000219private:
220 typedef GM INHERITED;
221};
222
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000223//////////////////////////////////////////////////////////////////////////////
224
225DEF_GM( return SkNEW(FontMgrGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000226DEF_GM( return SkNEW(FontMgrMatchGM); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000227
228#ifdef SK_BUILD_FOR_WIN
bungeman@google.com6eddc772014-03-31 19:18:07 +0000229 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
bungeman@google.combfc6cc42013-08-21 15:20:43 +0000230#endif