• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     { NULL, SkTypeface::kNormal },
29     { NULL, 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, -1.53091184e+13f);
81             pts[2].set(-1.30077315e+16f, -2.77196141e+13f);
82             pts[3].set(-1.30077315e+16f, -2.77196162e+13f);
83 
84             SkPath path;
85             path.moveTo(pts[0]);
86             path.cubicTo(pts[1], pts[2], pts[3]);
87             canvas->drawPath(path, paint);
88         }
89 
90 //        paint.setSubpixelText(true);
91         paint.setAntiAlias(true);
92         paint.setLCDRenderText(true);
93         SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal)));
94 
95 //        const char* text = "abcdefghijklmnopqrstuvwxyz";
96         const char* text = "Hamburgefons ooo mmm";
97         const size_t textLen = strlen(text);
98 
99         for (int j = 0; j < 2; ++j) {
100             for (int i = 0; i < 6; ++i) {
101                 SkScalar x = SkIntToScalar(10);
102                 SkScalar y = SkIntToScalar(20);
103 
104                 SkAutoCanvasRestore acr(canvas, true);
105                 canvas->translate(SkIntToScalar(50 + i * 230),
106                                   SkIntToScalar(20));
107                 rotate_about(canvas, i * 5, x, y * 10);
108 
109                 {
110                     SkPaint p;
111                     p.setAntiAlias(true);
112                     SkRect r;
113                     r.set(x-3, 15, x-1, 280);
114                     canvas->drawRect(r, p);
115                 }
116 
117                 int index = 0;
118                 for (int ps = 6; ps <= 22; ps++) {
119                     paint.setTextSize(SkIntToScalar(ps));
120                     canvas->drawText(text, textLen, x, y, paint);
121                     y += paint.getFontMetrics(NULL);
122                     index += 1;
123                 }
124             }
125             canvas->translate(0, 400);
126             paint.setSubpixelText(true);
127         }
128     }
129 
130 private:
131     typedef SkView INHERITED;
132 };
133 
134 //////////////////////////////////////////////////////////////////////////////
135 
MyFactory()136 static SkView* MyFactory() { return new FontScalerTestView; }
137 static SkViewRegister reg(MyFactory);
138 
139