blob: 121843572b391596e930b0bbbc555fe9370b8a7b [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);
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000062
reed@google.comaf0fa6a2013-03-28 13:39:35 +000063 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
64 x = drawString(canvas, sname, x, y, paint) + 20;
skia.committer@gmail.com6acd09e2013-03-29 07:01:22 +000065 }
reed@google.comaf0fa6a2013-03-28 13:39:35 +000066 y += 24;
67 }
68 }
69
reed@google.com6518daf2013-03-28 15:03:22 +000070 virtual uint32_t onGetFlags() const SK_OVERRIDE {
71 // fontdescriptors (and therefore serialization) don't yet understand
72 // these new styles, so skip tests that exercise that for now.
73 return kSkipPicture_Flag | kSkipPipe_Flag;
74 }
75
reed@google.comaf0fa6a2013-03-28 13:39:35 +000076private:
77 typedef GM INHERITED;
78};
79
reed@google.com964988f2013-03-29 14:57:22 +000080class FontMgrMatchGM : public skiagm::GM {
81 SkAutoTUnref<SkFontMgr> fFM;
82
83public:
84 FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) {
85 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
86 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +000087
reed@google.com964988f2013-03-29 14:57:22 +000088protected:
89 virtual SkString onShortName() {
90 return SkString("fontmgr_match");
91 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +000092
reed@google.com964988f2013-03-29 14:57:22 +000093 virtual SkISize onISize() {
94 return SkISize::Make(640, 1024);
95 }
96
97 void iterateFamily(SkCanvas* canvas, const SkPaint& paint,
98 SkFontStyleSet* fset) {
99 SkPaint p(paint);
100 SkScalar y = 0;
101
102 for (int j = 0; j < fset->count(); ++j) {
103 SkString sname;
104 SkFontStyle fs;
105 fset->getStyle(j, &fs, &sname);
106
107 sname.appendf(" [%d %d]", fs.weight(), fs.width());
108
109 SkSafeUnref(p.setTypeface(fset->createTypeface(j)));
110 (void)drawString(canvas, sname, 0, y, p);
111 y += 24;
112 }
113 }
114
115 void exploreFamily(SkCanvas* canvas, const SkPaint& paint,
116 SkFontStyleSet* fset) {
117 SkPaint p(paint);
118 SkScalar y = 0;
119
120 for (int weight = 100; weight <= 900; weight += 200) {
121 for (int width = 1; width <= 9; width += 2) {
122 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
123 SkTypeface* face = fset->matchStyle(fs);
124 if (face) {
125 SkString str;
126 str.printf("request [%d %d]", fs.weight(), fs.width());
127 p.setTypeface(face)->unref();
128 (void)drawString(canvas, str, 0, y, p);
129 y += 24;
130 }
131 }
132 }
133 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000134
reed@google.com964988f2013-03-29 14:57:22 +0000135 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 paint.setLCDRenderText(true);
139 paint.setSubpixelText(true);
140 paint.setTextSize(17);
141
142 static const char* gNames[] = {
143 "Helvetica Neue", "Arial"
144 };
145
146 SkFontStyleSet* fset = NULL;
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000147 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
reed@google.com964988f2013-03-29 14:57:22 +0000148 fset = fFM->matchFamily(gNames[i]);
149 if (fset && fset->count() > 0) {
150 break;
151 }
152 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000153
reed@google.com964988f2013-03-29 14:57:22 +0000154 if (NULL == fset) {
155 return;
156 }
157 SkAutoUnref aur(fset);
158
159 canvas->translate(20, 40);
160 this->exploreFamily(canvas, paint, fset);
161 canvas->translate(150, 0);
162 this->iterateFamily(canvas, paint, fset);
163 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000164
reed@google.com964988f2013-03-29 14:57:22 +0000165 virtual uint32_t onGetFlags() const SK_OVERRIDE {
166 // fontdescriptors (and therefore serialization) don't yet understand
167 // these new styles, so skip tests that exercise that for now.
168 return kSkipPicture_Flag | kSkipPipe_Flag;
169 }
skia.committer@gmail.comd55846d2013-03-30 07:01:27 +0000170
reed@google.com964988f2013-03-29 14:57:22 +0000171private:
172 typedef GM INHERITED;
173};
174
reed@google.comaf0fa6a2013-03-28 13:39:35 +0000175//////////////////////////////////////////////////////////////////////////////
176
177DEF_GM( return SkNEW(FontMgrGM); )
reed@google.com964988f2013-03-29 14:57:22 +0000178DEF_GM( return SkNEW(FontMgrMatchGM); )