• 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 
9 
10 /* Tests text vertical text rendering with different fonts and centering.
11  */
12 
13 #include "gm.h"
14 #include "SkCanvas.h"
15 #include "SkTypeface.h"
16 
17 namespace skiagm {
18 
19 class VertText2GM : public GM {
20 public:
VertText2GM()21     VertText2GM() {
22         const int pointSize = 24;
23         textHeight = SkIntToScalar(pointSize);
24         prop = SkTypeface::CreateFromName("Helvetica", SkTypeface::kNormal);
25         mono = SkTypeface::CreateFromName("Courier New", SkTypeface::kNormal);
26     }
27 
28 protected:
29 
onShortName()30     SkString onShortName() {
31         return SkString("verttext2");
32     }
33 
onISize()34     SkISize onISize() { return make_isize(640, 480); }
35 
onDraw(SkCanvas * canvas)36     virtual void onDraw(SkCanvas* canvas) {
37 
38         for (int i = 0; i < 3; ++i) {
39             SkPaint paint;
40             paint.setColor(SK_ColorRED);
41             paint.setAntiAlias(true);
42             y = textHeight;
43             canvas->drawLine(0, SkIntToScalar(10),
44                     SkIntToScalar(110), SkIntToScalar(10), paint);
45             canvas->drawLine(0, SkIntToScalar(240),
46                     SkIntToScalar(110), SkIntToScalar(240), paint);
47             canvas->drawLine(0, SkIntToScalar(470),
48                     SkIntToScalar(110), SkIntToScalar(470), paint);
49             drawText(canvas, SkString("Proportional / Top Aligned"),
50                      prop,  SkPaint::kLeft_Align);
51             drawText(canvas, SkString("<   Proportional / Centered   >"),
52                      prop,  SkPaint::kCenter_Align);
53             drawText(canvas, SkString("Monospaced / Top Aligned"),
54                      mono, SkPaint::kLeft_Align);
55             drawText(canvas, SkString("<    Monospaced / Centered    >"),
56                      mono, SkPaint::kCenter_Align);
57             canvas->rotate(SkIntToScalar(-15));
58             canvas->translate(textHeight * 4, SkIntToScalar(50));
59             if (i > 0) {
60                 canvas->translate(0, SkIntToScalar(50));
61             }
62         }
63     }
64 
drawText(SkCanvas * canvas,const SkString & string,SkTypeface * family,SkPaint::Align alignment)65     void drawText(SkCanvas* canvas, const SkString& string,
66                   SkTypeface* family, SkPaint::Align alignment) {
67         SkPaint paint;
68         paint.setColor(SK_ColorBLACK);
69         paint.setAntiAlias(true);
70         paint.setVerticalText(true);
71         paint.setTextAlign(alignment);
72         paint.setTypeface(family);
73         paint.setTextSize(textHeight);
74 
75         canvas->drawText(string.c_str(), string.size(), y,
76                 alignment == SkPaint::kLeft_Align ? 10 : 240, paint);
77         y += textHeight;
78     }
79 
80 private:
81     typedef GM INHERITED;
82     SkScalar y, textHeight;
83     SkTypeface* prop;
84     SkTypeface* mono;
85 };
86 
87 ///////////////////////////////////////////////////////////////////////////////
88 
MyFactory(void *)89 static GM* MyFactory(void*) { return new VertText2GM; }
90 static GMRegistry reg(MyFactory);
91 
92 }
93