2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkFontMgr.h"
11 #include "SkGraphics.h"
12 #include "SkTypeface.h"
14 #ifdef SK_BUILD_FOR_WIN
15 #include "SkTypeface_win.h"
18 // limit this just so we don't take too long to draw
19 #define MAX_FAMILIES 30
21 static 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());
27 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
28 SkScalar y, SkPaint& paint, SkFontMgr* fm,
29 const char* fontName, const char* bcp47[], int bcp47Count,
30 const SkFontStyle& fontStyle) {
31 // find typeface containing the requested character and draw it
33 ch.appendUnichar(character);
34 SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle,
35 bcp47, bcp47Count, character);
36 SkSafeUnref(paint.setTypeface(typeface));
37 x = drawString(canvas, ch, x, y, paint) + 20;
39 if (NULL == typeface) {
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.
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;
53 static const char* zh = "zh";
54 static const char* ja = "ja";
56 class FontMgrGM : public skiagm::GM {
58 FontMgrGM(SkFontMgr* fontMgr = NULL) {
59 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
61 fName.set("fontmgr_iter");
63 fName.append("_factory");
66 fFM.reset(SkFontMgr::RefDefault());
71 SkString onShortName() SK_OVERRIDE {
75 SkISize onISize() SK_OVERRIDE {
76 return SkISize::Make(1536, 768);
79 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
82 paint.setAntiAlias(true);
83 paint.setLCDRenderText(true);
84 paint.setSubpixelText(true);
85 paint.setTextSize(17);
88 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
90 for (int i = 0; i < count; ++i) {
92 fm->getFamilyName(i, &fname);
93 paint.setTypeface(NULL);
94 (void)drawString(canvas, fname, 20, y, paint);
98 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
99 for (int j = 0; j < set->count(); ++j) {
102 set->getStyle(j, &fs, &sname);
103 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
105 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
106 x = drawString(canvas, sname, x, y, paint) + 20;
108 // check to see that we get different glyphs in japanese and chinese
109 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);
111 // check that emoji characters are found
112 x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, 0, fs);
119 SkAutoTUnref<SkFontMgr> fFM;
121 typedef GM INHERITED;
124 class FontMgrMatchGM : public skiagm::GM {
125 SkAutoTUnref<SkFontMgr> fFM;
128 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
129 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
133 SkString onShortName() SK_OVERRIDE {
134 return SkString("fontmgr_match");
137 SkISize onISize() SK_OVERRIDE {
138 return SkISize::Make(640, 1024);
141 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
142 SkFontStyleSet* fset) {
146 for (int j = 0; j < fset->count(); ++j) {
149 fset->getStyle(j, &fs, &sname);
151 sname.appendf(" [%d %d]", fs.weight(), fs.width());
153 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
154 (void)drawString(canvas, sname, 0, y, p);
159 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
160 SkFontStyleSet* fset) {
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);
170 str.printf("request [%d %d]", fs.weight(), fs.width());
171 p.setTypeface(face)->unref();
172 (void)drawString(canvas, str, 0, y, p);
179 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
181 paint.setAntiAlias(true);
182 paint.setLCDRenderText(true);
183 paint.setSubpixelText(true);
184 paint.setTextSize(17);
186 static const char* gNames[] = {
187 "Helvetica Neue", "Arial"
190 SkAutoTUnref<SkFontStyleSet> fset;
191 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
192 fset.reset(fFM->matchFamily(gNames[i]));
193 if (fset->count() > 0) {
197 if (NULL == fset.get()) {
201 canvas->translate(20, 40);
202 this->exploreFamily(canvas, paint, fset);
203 canvas->translate(150, 0);
204 this->iterateFamily(canvas, paint, fset);
208 typedef GM INHERITED;
211 class FontMgrBoundsGM : public skiagm::GM {
213 FontMgrBoundsGM(double scale, double skew)
214 : fScaleX(SkDoubleToScalar(scale))
215 , fSkewX(SkDoubleToScalar(skew))
217 fName.set("fontmgr_bounds");
218 if (scale != 1 || skew != 0) {
219 fName.appendf("_%g_%g", scale, skew);
221 fFM.reset(SkFontMgr::RefDefault());
224 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
225 SkColor boundsColor) {
226 const char str[] = "jyHO[]{}@-_&%$";
228 for (int i = 0; str[i]; ++i) {
229 canvas->drawText(&str[i], 1, x, y, paint);
232 SkRect r = paint.getFontBounds();
235 p.setColor(boundsColor);
236 canvas->drawRect(r, p);
240 SkString onShortName() SK_OVERRIDE {
244 SkISize onISize() SK_OVERRIDE {
245 return SkISize::Make(1024, 850);
248 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
250 paint.setAntiAlias(true);
251 paint.setSubpixelText(true);
252 paint.setTextSize(100);
253 paint.setStyle(SkPaint::kStroke_Style);
254 paint.setTextScaleX(fScaleX);
255 paint.setTextSkewX(fSkewX);
257 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
260 int count = SkMin32(fm->countFamilies(), 32);
263 SkScalar x = 0, y = 0;
265 canvas->translate(80, 120);
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]);
275 if (0 == (index % 6)) {
288 SkAutoTUnref<SkFontMgr> fFM;
290 SkScalar fScaleX, fSkewX;
291 typedef GM INHERITED;
294 //////////////////////////////////////////////////////////////////////////////
296 DEF_GM( return SkNEW(FontMgrGM); )
297 DEF_GM( return SkNEW(FontMgrMatchGM); )
298 DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, 0)); )
299 DEF_GM( return SkNEW(FontMgrBoundsGM(0.75, 0)); )
300 DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, -0.25)); )
302 #ifdef SK_BUILD_FOR_WIN
303 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )