blob: 9afedc6078a25e965698e54519d1829273e76328 [file] [log] [blame]
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9/* Tests text vertical text rendering with different fonts and centering.
10 */
11
12#include "gm.h"
13#include "SkCanvas.h"
14#include "SkTypeface.h"
15
16namespace skiagm {
17
18class VertText2GM : public GM {
19public:
bungeman13b9c952016-05-12 10:09:30 -070020 VertText2GM() {}
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000021
22protected:
kkinnunenb4a797f2015-05-21 06:15:28 -070023 void onOnceBeforeDraw() override {
24 const int pointSize = 24;
25 textHeight = SkIntToScalar(pointSize);
bungeman13b9c952016-05-12 10:09:30 -070026 fProp = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("sans-serif"),
mbocee6a9912016-05-31 11:42:36 -070027 SkFontStyle());
bungeman13b9c952016-05-12 10:09:30 -070028 fMono = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("monospace"),
mbocee6a9912016-05-31 11:42:36 -070029 SkFontStyle());
kkinnunenb4a797f2015-05-21 06:15:28 -070030 }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000031
mtklein36352bf2015-03-25 18:17:31 -070032 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070033 SkString name("verttext2");
34 name.append(sk_tool_utils::major_platform_os_name());
35 return name;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000036 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override { return SkISize::Make(640, 480); }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000039
mtklein36352bf2015-03-25 18:17:31 -070040 void onDraw(SkCanvas* canvas) override {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000041 for (int i = 0; i < 3; ++i) {
42 SkPaint paint;
43 paint.setColor(SK_ColorRED);
44 paint.setAntiAlias(true);
45 y = textHeight;
46 canvas->drawLine(0, SkIntToScalar(10),
47 SkIntToScalar(110), SkIntToScalar(10), paint);
48 canvas->drawLine(0, SkIntToScalar(240),
49 SkIntToScalar(110), SkIntToScalar(240), paint);
50 canvas->drawLine(0, SkIntToScalar(470),
51 SkIntToScalar(110), SkIntToScalar(470), paint);
52 drawText(canvas, SkString("Proportional / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000053 fProp, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000054 drawText(canvas, SkString("< Proportional / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000055 fProp, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000056 drawText(canvas, SkString("Monospaced / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000057 fMono, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000058 drawText(canvas, SkString("< Monospaced / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000059 fMono, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000060 canvas->rotate(SkIntToScalar(-15));
61 canvas->translate(textHeight * 4, SkIntToScalar(50));
62 if (i > 0) {
63 canvas->translate(0, SkIntToScalar(50));
64 }
65 }
66 }
67
68 void drawText(SkCanvas* canvas, const SkString& string,
bungeman13b9c952016-05-12 10:09:30 -070069 sk_sp<SkTypeface> family, SkPaint::Align alignment) {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000070 SkPaint paint;
71 paint.setColor(SK_ColorBLACK);
72 paint.setAntiAlias(true);
73 paint.setVerticalText(true);
74 paint.setTextAlign(alignment);
bungeman13b9c952016-05-12 10:09:30 -070075 paint.setTypeface(std::move(family));
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000076 paint.setTextSize(textHeight);
77
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 canvas->drawText(string.c_str(), string.size(), y,
tomhudson@google.com75589252012-04-10 17:42:21 +000079 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
80 paint);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000081 y += textHeight;
82 }
83
84private:
85 typedef GM INHERITED;
86 SkScalar y, textHeight;
bungeman13b9c952016-05-12 10:09:30 -070087 sk_sp<SkTypeface> fProp;
88 sk_sp<SkTypeface> fMono;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000089};
90
91///////////////////////////////////////////////////////////////////////////////
92
93static GM* MyFactory(void*) { return new VertText2GM; }
94static GMRegistry reg(MyFactory);
95
96}