1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkTypeface.h"
12 #include "SkPath.h"
13 #include "SkRegion.h"
14 #include "SkShader.h"
15 #include "SkUtils.h"
16 #include "Sk1DPathEffect.h"
17 #include "SkCornerPathEffect.h"
18 #include "SkPathMeasure.h"
19 #include "SkRandom.h"
20 #include "SkColorPriv.h"
21 #include "SkColorFilter.h"
22 #include "SkDither.h"
23 
24 static const struct {
25     const char* fName;
26     SkTypeface::Style   fStyle;
27 } gFaces[] = {
28     { nullptr, SkTypeface::kNormal },
29     { nullptr, SkTypeface::kBold },
30     { "serif", SkTypeface::kNormal },
31     { "serif", SkTypeface::kBold },
32     { "serif", SkTypeface::kItalic },
33     { "serif", SkTypeface::kBoldItalic },
34     { "monospace", SkTypeface::kNormal }
35 };
36 
37 static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
38 
39 class FontScalerTestView : public SampleView {
40     SkTypeface* fFaces[gFaceCount];
41 
42 public:
FontScalerTestView()43     FontScalerTestView() {
44         for (int i = 0; i < gFaceCount; i++) {
45             fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName,
46                                                    gFaces[i].fStyle);
47         }
48 //        this->setBGColor(0xFFDDDDDD);
49     }
50 
~FontScalerTestView()51     virtual ~FontScalerTestView() {
52         for (int i = 0; i < gFaceCount; i++) {
53             SkSafeUnref(fFaces[i]);
54         }
55     }
56 
57 protected:
58     // overrides from SkEventSink
onQuery(SkEvent * evt)59     virtual bool onQuery(SkEvent* evt) {
60         if (SampleCode::TitleQ(*evt)) {
61             SampleCode::TitleR(evt, "FontScaler Test");
62             return true;
63         }
64         return this->INHERITED::onQuery(evt);
65     }
66 
rotate_about(SkCanvas * canvas,SkScalar degrees,SkScalar px,SkScalar py)67     static void rotate_about(SkCanvas* canvas, SkScalar degrees, SkScalar px, SkScalar py) {
68         canvas->translate(px, py);
69         canvas->rotate(degrees);
70         canvas->translate(-px, -py);
71     }
72 
onDrawContent(SkCanvas * canvas)73     virtual void onDrawContent(SkCanvas* canvas) {
74         SkPaint paint;
75 
76         // test handling of obscene cubic values (currently broken)
77         if (false) {
78             SkPoint pts[4];
79             pts[0].set(1.61061274e+09f, 6291456);
80             pts[1].set(-7.18397061e+15f,
81                        -1.53091184e+13f);
82             pts[2].set(-1.30077315e+16f,
83                        -2.77196141e+13f);
84             pts[3].set(-1.30077315e+16f,
85                        -2.77196162e+13f);
86 
87             SkPath path;
88             path.moveTo(pts[0]);
89             path.cubicTo(pts[1], pts[2], pts[3]);
90             canvas->drawPath(path, paint);
91         }
92 
93 //        paint.setSubpixelText(true);
94         paint.setAntiAlias(true);
95         paint.setLCDRenderText(true);
96         SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal)));
97 
98 //        const char* text = "abcdefghijklmnopqrstuvwxyz";
99         const char* text = "Hamburgefons ooo mmm";
100         const size_t textLen = strlen(text);
101 
102         for (int j = 0; j < 2; ++j) {
103             for (int i = 0; i < 6; ++i) {
104                 SkScalar x = SkIntToScalar(10);
105                 SkScalar y = SkIntToScalar(20);
106 
107                 SkAutoCanvasRestore acr(canvas, true);
108                 canvas->translate(SkIntToScalar(50 + i * 230),
109                                   SkIntToScalar(20));
110                 rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
111 
112                 {
113                     SkPaint p;
114                     p.setAntiAlias(true);
115                     SkRect r;
116                     r.set(x-3, 15, x-1, 280);
117                     canvas->drawRect(r, p);
118                 }
119 
120                 int index = 0;
121                 for (int ps = 6; ps <= 22; ps++) {
122                     paint.setTextSize(SkIntToScalar(ps));
123                     canvas->drawText(text, textLen, x, y, paint);
124                     y += paint.getFontMetrics(nullptr);
125                     index += 1;
126                 }
127             }
128             canvas->translate(0, 400);
129             paint.setSubpixelText(true);
130         }
131     }
132 
133 private:
134     typedef SkView INHERITED;
135 };
136 
137 //////////////////////////////////////////////////////////////////////////////
138 
MyFactory()139 static SkView* MyFactory() { return new FontScalerTestView; }
140 static SkViewRegister reg(MyFactory);
141