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 "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkTypeface.h"
11#include "SkPath.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
15#include "Sk1DPathEffect.h"
16#include "SkCornerPathEffect.h"
17#include "SkPathMeasure.h"
18#include "SkRandom.h"
19#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkDither.h"
22
23static const struct {
24    const char* fName;
25    SkTypeface::Style   fStyle;
26} gFaces[] = {
27    { nullptr, SkTypeface::kNormal },
28    { nullptr, SkTypeface::kBold },
29    { "serif", SkTypeface::kNormal },
30    { "serif", SkTypeface::kBold },
31    { "serif", SkTypeface::kItalic },
32    { "serif", SkTypeface::kBoldItalic },
33    { "monospace", SkTypeface::kNormal }
34};
35
36static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
37
38class FontScalerTestView : public SampleView {
39    sk_sp<SkTypeface> fFaces[gFaceCount];
40
41public:
42    FontScalerTestView() {
43        for (int i = 0; i < gFaceCount; i++) {
44            fFaces[i] = SkTypeface::MakeFromName(
45                gFaces[i].fName, SkFontStyle::FromOldStyle(gFaces[i].fStyle));
46        }
47    }
48
49protected:
50    // overrides from SkEventSink
51    virtual bool onQuery(SkEvent* evt) {
52        if (SampleCode::TitleQ(*evt)) {
53            SampleCode::TitleR(evt, "FontScaler Test");
54            return true;
55        }
56        return this->INHERITED::onQuery(evt);
57    }
58
59    virtual void onDrawContent(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,
67                       -1.53091184e+13f);
68            pts[2].set(-1.30077315e+16f,
69                       -2.77196141e+13f);
70            pts[3].set(-1.30077315e+16f,
71                       -2.77196162e+13f);
72
73            SkPath path;
74            path.moveTo(pts[0]);
75            path.cubicTo(pts[1], pts[2], pts[3]);
76            canvas->drawPath(path, paint);
77        }
78
79//        paint.setSubpixelText(true);
80        paint.setAntiAlias(true);
81        paint.setLCDRenderText(true);
82        paint.setTypeface(SkTypeface::MakeFromName("Times Roman", SkFontStyle()));
83
84//        const char* text = "abcdefghijklmnopqrstuvwxyz";
85        const char* text = "Hamburgefons ooo mmm";
86        const size_t textLen = strlen(text);
87
88        for (int j = 0; j < 2; ++j) {
89            for (int i = 0; i < 6; ++i) {
90                SkScalar x = SkIntToScalar(10);
91                SkScalar y = SkIntToScalar(20);
92
93                SkAutoCanvasRestore acr(canvas, true);
94                canvas->translate(SkIntToScalar(50 + i * 230),
95                                  SkIntToScalar(20));
96                canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
97
98                {
99                    SkPaint p;
100                    p.setAntiAlias(true);
101                    SkRect r;
102                    r.set(x-3, 15, x-1, 280);
103                    canvas->drawRect(r, p);
104                }
105
106                int index = 0;
107                for (int ps = 6; ps <= 22; ps++) {
108                    paint.setTextSize(SkIntToScalar(ps));
109                    canvas->drawText(text, textLen, x, y, paint);
110                    y += paint.getFontMetrics(nullptr);
111                    index += 1;
112                }
113            }
114            canvas->translate(0, 400);
115            paint.setSubpixelText(true);
116        }
117    }
118
119private:
120    typedef SkView INHERITED;
121};
122
123//////////////////////////////////////////////////////////////////////////////
124
125static SkView* MyFactory() { return new FontScalerTestView; }
126static SkViewRegister reg(MyFactory);
127