fontscaler.cpp revision 24babf45b19bd400a301972dc0d7e3e4007c03bc
1/*
2 * Copyright 2011 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#include "gm.h"
8#include "SkTypeface.h"
9
10namespace skiagm {
11
12static const struct {
13    const char* fName;
14    SkTypeface::Style fStyle;
15} gFaces[] = {
16    { NULL, SkTypeface::kNormal },
17    { NULL, SkTypeface::kBold },
18    { "serif", SkTypeface::kNormal },
19    { "serif", SkTypeface::kBold },
20    { "serif", SkTypeface::kItalic },
21    { "serif", SkTypeface::kBoldItalic },
22    { "monospace", SkTypeface::kNormal }
23};
24
25static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
26
27class FontScalerGM : public GM {
28public:
29    FontScalerGM() {
30        this->setBGColor(0xFFFFFFFF);
31
32        for (int i = 0; i < gFaceCount; i++) {
33            fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName,
34                                                   gFaces[i].fStyle);
35        }
36    }
37
38    virtual ~FontScalerGM() {
39        for (int i = 0; i < gFaceCount; i++) {
40            SkSafeUnref(fFaces[i]);
41        }
42    }
43
44protected:
45    virtual SkString onShortName() {
46        return SkString("fontscaler");
47    }
48
49    virtual SkISize onISize() {
50        return make_isize(1450, 750);
51    }
52
53    static void rotate_about(SkCanvas* canvas, SkScalar degrees, SkScalar px, SkScalar py) {
54        canvas->translate(px, py);
55        canvas->rotate(degrees);
56        canvas->translate(-px, -py);
57    }
58
59    virtual void onDraw(SkCanvas* canvas) {
60        SkPaint paint;
61
62        // test handling of obscene cubic values (currently broken)
63        if (false) {
64            SkPoint pts[4];
65            pts[0].set(1.61061274e+09f, 6291456);
66            pts[1].set(-7.18397061e+15f, -1.53091184e+13f);
67            pts[2].set(-1.30077315e+16f, -2.77196141e+13f);
68            pts[3].set(-1.30077315e+16f, -2.77196162e+13f);
69
70            SkPath path;
71            path.moveTo(pts[0]);
72            path.cubicTo(pts[1], pts[2], pts[3]);
73            canvas->drawPath(path, paint);
74        }
75
76        paint.setAntiAlias(true);
77        paint.setLCDRenderText(true);
78        //With freetype the default (normal hinting) can be really ugly.
79        //Most distros now set slight (vertical hinting only) in any event.
80        paint.setHinting(SkPaint::kSlight_Hinting);
81        SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal)));
82
83//        const char* text = "abcdefghijklmnopqrstuvwxyz";
84        const char* text = "Hamburgefons ooo mmm";
85        const size_t textLen = strlen(text);
86
87        for (int j = 0; j < 2; ++j) {
88            for (int i = 0; i < 6; ++i) {
89                SkScalar x = SkIntToScalar(10);
90                SkScalar y = SkIntToScalar(20);
91
92                SkAutoCanvasRestore acr(canvas, true);
93                canvas->translate(SkIntToScalar(50 + i * 230),
94                                  SkIntToScalar(20));
95                rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
96
97                {
98                    SkPaint p;
99                    p.setAntiAlias(true);
100                    SkRect r;
101                    r.set(x-3, 15, x-1, 280);
102                    canvas->drawRect(r, p);
103                }
104
105                int index = 0;
106                for (int ps = 6; ps <= 22; ps++) {
107                    paint.setTextSize(SkIntToScalar(ps));
108                    canvas->drawText(text, textLen, x, y, paint);
109                    y += paint.getFontMetrics(NULL);
110                    index += 1;
111                }
112            }
113            canvas->translate(0, 360);
114            paint.setSubpixelText(true);
115        }
116    }
117
118private:
119    SkTypeface* fFaces[gFaceCount];
120    typedef GM INHERITED;
121};
122
123//////////////////////////////////////////////////////////////////////////////
124
125static GM* MyFactory(void*) { return new FontScalerGM; }
126static GMRegistry reg(MyFactory);
127
128}
129
130