blob: 4ff55a7a6c3c4e01b1e33ed470c4967bb18eda28 [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
14// limit this just so we don't take too long to draw
15#define MAX_FAMILIES 30
16
17static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
18 SkScalar y, const SkPaint& paint) {
19 canvas->drawText(text.c_str(), text.size(), x, y, paint);
20 return x + paint.measureText(text.c_str(), text.size());
21}
22
23class FontMgrGM : public skiagm::GM {
24public:
25 FontMgrGM() {
26 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
27 }
28
29protected:
30 virtual SkString onShortName() {
reed@google.com964988f2013-03-29 14:57:22 +000031 return SkString("fontmgr_iter");
reed@google.comaf0fa6a2013-03-28 13:39:35 +000032 }
33
34 virtual SkISize onISize() {
35 return SkISize::Make(640, 1024);
36 }
37
38 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
39 SkScalar y = 20;
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setLCDRenderText(true);
43 paint.setSubpixelText(true);
44 paint.setTextSize(17);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000045
reed@google.comaf0fa6a2013-03-28 13:39:35 +000046 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
47 int count = SkMin32(fm->countFamilies(), MAX_FAMILIES);
48
49 for (int i = 0; i < count; ++i) {
50 SkString fname;
51 fm->getFamilyName(i, &fname);
52 paint.setTypeface(NULL);
53 (void)drawString(canvas, fname, 20, y, paint);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000054
reed@google.comaf0fa6a2013-03-28 13:39:35 +000055 SkScalar x = 220;
reed@google.com964988f2013-03-29 14:57:22 +000056
reed@google.comaf0fa6a2013-03-28 13:39:35 +000057 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
58 for (int j = 0; j < set->count(); ++j) {
59 SkString sname;
60 SkFontStyle fs;
61 set->getStyle(j, &fs, &sname);
reed@google.com51116482013-04-24 19:14:39 +000062 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic());
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000063
reed@google.comaf0fa6a2013-03-28 13:39:35 +000064 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
65 x = drawString(canvas, sname, x, y, paint) + 20;
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000066 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000067 y += 24;
68 }
69 }
70
reed@google.com6518daf2013-03-28 15:03:22 +000071 virtual uint32_t onGetFlags() const SK_OVERRIDE {
72 // fontdescriptors (and therefore serialization) don't yet understand
73 // these new styles, so skip tests that exercise that for now.
74 return kSkipPicture_Flag | kSkipPipe_Flag;
75 }
76
reed@google.comaf0fa6a2013-03-28 13:39:35 +000077private:
78 typedef GM INHERITED;
79};
80
reed@google.com964988f2013-03-29 14:57:22 +000081class FontMgrMatchGM : public skiagm::GM {
82 SkAutoTUnref<SkFontMgr> fFM;
83
84public:
85 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
86 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
87 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +000088
reed@google.com964988f2013-03-29 14:57:22 +000089protected:
90 virtual SkString onShortName() {
91 return SkString("fontmgr_match");
92 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +000093
reed@google.com964988f2013-03-29 14:57:22 +000094 virtual SkISize onISize() {
95 return SkISize::Make(640, 1024);
96 }
97
98 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
99 SkFontStyleSet* fset) {
100 SkPaint p(paint);
101 SkScalar y = 0;
102
103 for (int j = 0; j < fset->count(); ++j) {
104 SkString sname;
105 SkFontStyle fs;
106 fset->getStyle(j, &fs, &sname);
107
108 sname.appendf(" [%d %d]", fs.weight(), fs.width());
109
110 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
111 (void)drawString(canvas, sname, 0, y, p);
112 y += 24;
113 }
114 }
115
116 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
117 SkFontStyleSet* fset) {
118 SkPaint p(paint);
119 SkScalar y = 0;
120
121 for (int weight = 100; weight <= 900; weight += 200) {
122 for (int width = 1; width <= 9; width += 2) {
123 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
124 SkTypeface* face = fset->matchStyle(fs);
125 if (face) {
126 SkString str;
127 str.printf("request [%d %d]", fs.weight(), fs.width());
128 p.setTypeface(face)->unref();
129 (void)drawString(canvas, str, 0, y, p);
130 y += 24;
131 }
132 }
133 }
134 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000135
reed@google.com964988f2013-03-29 14:57:22 +0000136 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
137 SkPaint paint;
138 paint.setAntiAlias(true);
139 paint.setLCDRenderText(true);
140 paint.setSubpixelText(true);
141 paint.setTextSize(17);
142
143 static const char* gNames[] = {
144 "Helvetica Neue", "Arial"
145 };
146
147 SkFontStyleSet* fset = NULL;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000148 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
reed@google.com964988f2013-03-29 14:57:22 +0000149 fset = fFM->matchFamily(gNames[i]);
150 if (fset && fset->count() > 0) {
151 break;
152 }
153 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000154
reed@google.com964988f2013-03-29 14:57:22 +0000155 if (NULL == fset) {
156 return;
157 }
158 SkAutoUnref aur(fset);
159
160 canvas->translate(20, 40);
161 this->exploreFamily(canvas, paint, fset);
162 canvas->translate(150, 0);
163 this->iterateFamily(canvas, paint, fset);
164 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000165
reed@google.com964988f2013-03-29 14:57:22 +0000166 virtual uint32_t onGetFlags() const SK_OVERRIDE {
167 // fontdescriptors (and therefore serialization) don't yet understand
168 // these new styles, so skip tests that exercise that for now.
169 return kSkipPicture_Flag | kSkipPipe_Flag;
170 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000171
reed@google.com964988f2013-03-29 14:57:22 +0000172private:
173 typedef GM INHERITED;
174};
175
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000176//////////////////////////////////////////////////////////////////////////////
177
178DEF_GM( return SkNEW(FontMgrGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000179DEF_GM( return SkNEW(FontMgrMatchGM); )