blob: 0ccdec19cadfce01896b0437a69276b029f57121 [file] [log] [blame]
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +00001
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
17namespace skiagm {
18
19class VertText2GM : public GM {
20public:
21 VertText2GM() {
22 const int pointSize = 24;
23 textHeight = SkIntToScalar(pointSize);
Cary Clark992c7b02014-07-31 08:58:44 -040024 fProp = sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal);
25 fMono = sk_tool_utils::create_portable_typeface("Courier New", SkTypeface::kNormal);
reed@google.com563a3b42012-06-26 19:24:50 +000026 }
27
28 virtual ~VertText2GM() {
29 SkSafeUnref(fProp);
30 SkSafeUnref(fMono);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000031 }
32
33protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000034 virtual uint32_t onGetFlags() const SK_OVERRIDE {
35 return kSkipTiled_Flag;
36 }
37
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000038
39 SkString onShortName() {
40 return SkString("verttext2");
41 }
42
tfarinaf5393182014-06-09 23:59:03 -070043 SkISize onISize() { return SkISize::Make(640, 480); }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000044
45 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000047 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"),
reed@google.com563a3b42012-06-26 19:24:50 +000059 fProp, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000060 drawText(canvas, SkString("< Proportional / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000061 fProp, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000062 drawText(canvas, SkString("Monospaced / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000063 fMono, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000064 drawText(canvas, SkString("< Monospaced / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000065 fMono, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000066 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
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
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 canvas->drawText(string.c_str(), string.size(), y,
tomhudson@google.com75589252012-04-10 17:42:21 +000085 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
86 paint);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000087 y += textHeight;
88 }
89
90private:
91 typedef GM INHERITED;
92 SkScalar y, textHeight;
reed@google.com563a3b42012-06-26 19:24:50 +000093 SkTypeface* fProp;
94 SkTypeface* fMono;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000095};
96
97///////////////////////////////////////////////////////////////////////////////
98
99static GM* MyFactory(void*) { return new VertText2GM; }
100static GMRegistry reg(MyFactory);
101
102}