blob: 9eba68b164b32efb8afd0d48937c5ee5aca7001b [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:
mtklein72c9faa2015-01-09 10:06:39 -080034 uint32_t onGetFlags() const SK_OVERRIDE {
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000035 return kSkipTiled_Flag;
36 }
37
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000038
tfarina752e7eb2014-12-20 06:53:43 -080039 SkString onShortName() SK_OVERRIDE {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000040 return SkString("verttext2");
41 }
42
tfarina752e7eb2014-12-20 06:53:43 -080043 SkISize onISize() SK_OVERRIDE { return SkISize::Make(640, 480); }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000044
mtklein72c9faa2015-01-09 10:06:39 -080045 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000046 for (int i = 0; i < 3; ++i) {
47 SkPaint paint;
48 paint.setColor(SK_ColorRED);
49 paint.setAntiAlias(true);
50 y = textHeight;
51 canvas->drawLine(0, SkIntToScalar(10),
52 SkIntToScalar(110), SkIntToScalar(10), paint);
53 canvas->drawLine(0, SkIntToScalar(240),
54 SkIntToScalar(110), SkIntToScalar(240), paint);
55 canvas->drawLine(0, SkIntToScalar(470),
56 SkIntToScalar(110), SkIntToScalar(470), paint);
57 drawText(canvas, SkString("Proportional / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000058 fProp, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000059 drawText(canvas, SkString("< Proportional / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000060 fProp, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000061 drawText(canvas, SkString("Monospaced / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000062 fMono, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000063 drawText(canvas, SkString("< Monospaced / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000064 fMono, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000065 canvas->rotate(SkIntToScalar(-15));
66 canvas->translate(textHeight * 4, SkIntToScalar(50));
67 if (i > 0) {
68 canvas->translate(0, SkIntToScalar(50));
69 }
70 }
71 }
72
73 void drawText(SkCanvas* canvas, const SkString& string,
74 SkTypeface* family, SkPaint::Align alignment) {
75 SkPaint paint;
76 paint.setColor(SK_ColorBLACK);
77 paint.setAntiAlias(true);
78 paint.setVerticalText(true);
79 paint.setTextAlign(alignment);
80 paint.setTypeface(family);
81 paint.setTextSize(textHeight);
82
rmistry@google.comae933ce2012-08-23 18:19:56 +000083 canvas->drawText(string.c_str(), string.size(), y,
tomhudson@google.com75589252012-04-10 17:42:21 +000084 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
85 paint);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000086 y += textHeight;
87 }
88
89private:
90 typedef GM INHERITED;
91 SkScalar y, textHeight;
reed@google.com563a3b42012-06-26 19:24:50 +000092 SkTypeface* fProp;
93 SkTypeface* fMono;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000094};
95
96///////////////////////////////////////////////////////////////////////////////
97
98static GM* MyFactory(void*) { return new VertText2GM; }
99static GMRegistry reg(MyFactory);
100
101}