• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Sk1DPathEffect.h"
9 #include "SkCanvas.h"
10 #include "SkColorFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkCornerPathEffect.h"
13 #include "SkDither.h"
14 #include "SkFontStyle.h"
15 #include "SkPath.h"
16 #include "SkPathMeasure.h"
17 #include "SkRandom.h"
18 #include "SkRegion.h"
19 #include "SkShader.h"
20 #include "SkTypeface.h"
21 #include "SkUtils.h"
22 #include "SkView.h"
23 
24 static constexpr struct {
25     const char* fName;
26     SkFontStyle fStyle;
27 } gFaces[] = {
28     { nullptr, SkFontStyle::Normal() },
29     { nullptr, SkFontStyle::Bold() },
30     { "serif", SkFontStyle::Normal() },
31     { "serif", SkFontStyle::Bold() },
32     { "serif", SkFontStyle::Italic() },
33     { "serif", SkFontStyle::BoldItalic() },
34     { "monospace", SkFontStyle::Normal() }
35 };
36 
37 static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
38 
39 class FontScalerTestView : public SampleView {
40     sk_sp<SkTypeface> fFaces[gFaceCount];
41 
42 public:
FontScalerTestView()43     FontScalerTestView() {
44         for (int i = 0; i < gFaceCount; i++) {
45             fFaces[i] = SkTypeface::MakeFromName(gFaces[i].fName, gFaces[i].fStyle);
46         }
47     }
48 
49 protected:
50     // overrides from SkEventSink
onQuery(SkEvent * evt)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 
onDrawContent(SkCanvas * canvas)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 
119 private:
120     typedef SkView INHERITED;
121 };
122 
123 //////////////////////////////////////////////////////////////////////////////
124 
MyFactory()125 static SkView* MyFactory() { return new FontScalerTestView; }
126 static SkViewRegister reg(MyFactory);
127