blob: d3ab256524accd082a9f870cf8eb391b8c0e6294 [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() {
31 return SkString("fontmgr");
32 }
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);
45
46 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);
54
55 SkScalar x = 220;
56 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
57 for (int j = 0; j < set->count(); ++j) {
58 SkString sname;
59 SkFontStyle fs;
60 set->getStyle(j, &fs, &sname);
61
62 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
63 x = drawString(canvas, sname, x, y, paint) + 20;
64 }
65 y += 24;
66 }
67 }
68
69private:
70 typedef GM INHERITED;
71};
72
73//////////////////////////////////////////////////////////////////////////////
74
75DEF_GM( return SkNEW(FontMgrGM); )
76