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 fProp = sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal);
25 fMono = sk_tool_utils::create_portable_typeface("Courier New", SkTypeface::kNormal);
26 }
27
~VertText2GM()28 virtual ~VertText2GM() {
29 SkSafeUnref(fProp);
30 SkSafeUnref(fMono);
31 }
32
33 protected:
onGetFlags() const34 virtual uint32_t onGetFlags() const SK_OVERRIDE {
35 return kSkipTiled_Flag;
36 }
37
38
onShortName()39 SkString onShortName() {
40 return SkString("verttext2");
41 }
42
onISize()43 SkISize onISize() { return SkISize::Make(640, 480); }
44
onDraw(SkCanvas * canvas)45 virtual void onDraw(SkCanvas* canvas) {
46
47 for (int i = 0; i < 3; ++i) {
48 SkPaint paint;
49 paint.setColor(SK_ColorRED);
50 paint.setAntiAlias(true);
51 y = textHeight;
52 canvas->drawLine(0, SkIntToScalar(10),
53 SkIntToScalar(110), SkIntToScalar(10), paint);
54 canvas->drawLine(0, SkIntToScalar(240),
55 SkIntToScalar(110), SkIntToScalar(240), paint);
56 canvas->drawLine(0, SkIntToScalar(470),
57 SkIntToScalar(110), SkIntToScalar(470), paint);
58 drawText(canvas, SkString("Proportional / Top Aligned"),
59 fProp, SkPaint::kLeft_Align);
60 drawText(canvas, SkString("< Proportional / Centered >"),
61 fProp, SkPaint::kCenter_Align);
62 drawText(canvas, SkString("Monospaced / Top Aligned"),
63 fMono, SkPaint::kLeft_Align);
64 drawText(canvas, SkString("< Monospaced / Centered >"),
65 fMono, SkPaint::kCenter_Align);
66 canvas->rotate(SkIntToScalar(-15));
67 canvas->translate(textHeight * 4, SkIntToScalar(50));
68 if (i > 0) {
69 canvas->translate(0, SkIntToScalar(50));
70 }
71 }
72 }
73
drawText(SkCanvas * canvas,const SkString & string,SkTypeface * family,SkPaint::Align alignment)74 void drawText(SkCanvas* canvas, const SkString& string,
75 SkTypeface* family, SkPaint::Align alignment) {
76 SkPaint paint;
77 paint.setColor(SK_ColorBLACK);
78 paint.setAntiAlias(true);
79 paint.setVerticalText(true);
80 paint.setTextAlign(alignment);
81 paint.setTypeface(family);
82 paint.setTextSize(textHeight);
83
84 canvas->drawText(string.c_str(), string.size(), y,
85 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
86 paint);
87 y += textHeight;
88 }
89
90 private:
91 typedef GM INHERITED;
92 SkScalar y, textHeight;
93 SkTypeface* fProp;
94 SkTypeface* fMono;
95 };
96
97 ///////////////////////////////////////////////////////////////////////////////
98
MyFactory(void *)99 static GM* MyFactory(void*) { return new VertText2GM; }
100 static GMRegistry reg(MyFactory);
101
102 }
103