blob: a0ebd01812dc79ad295e278b7c2aa466ec501ed3 [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"
Mike Klein33d20552017-03-22 13:47:51 -040013#include "sk_tool_utils.h"
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000014#include "SkCanvas.h"
15#include "SkTypeface.h"
16
17namespace skiagm {
18
19class VertText2GM : public GM {
20public:
bungeman13b9c952016-05-12 10:09:30 -070021 VertText2GM() {}
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000022
23protected:
kkinnunenb4a797f2015-05-21 06:15:28 -070024 void onOnceBeforeDraw() override {
25 const int pointSize = 24;
26 textHeight = SkIntToScalar(pointSize);
bungeman13b9c952016-05-12 10:09:30 -070027 fProp = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("sans-serif"),
mbocee6a9912016-05-31 11:42:36 -070028 SkFontStyle());
bungeman13b9c952016-05-12 10:09:30 -070029 fMono = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("monospace"),
mbocee6a9912016-05-31 11:42:36 -070030 SkFontStyle());
kkinnunenb4a797f2015-05-21 06:15:28 -070031 }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000032
mtklein36352bf2015-03-25 18:17:31 -070033 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070034 SkString name("verttext2");
35 name.append(sk_tool_utils::major_platform_os_name());
36 return name;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000037 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 SkISize onISize() override { return SkISize::Make(640, 480); }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 void onDraw(SkCanvas* canvas) override {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000042 for (int i = 0; i < 3; ++i) {
43 SkPaint paint;
44 paint.setColor(SK_ColorRED);
45 paint.setAntiAlias(true);
46 y = textHeight;
47 canvas->drawLine(0, SkIntToScalar(10),
48 SkIntToScalar(110), SkIntToScalar(10), paint);
49 canvas->drawLine(0, SkIntToScalar(240),
50 SkIntToScalar(110), SkIntToScalar(240), paint);
51 canvas->drawLine(0, SkIntToScalar(470),
52 SkIntToScalar(110), SkIntToScalar(470), paint);
53 drawText(canvas, SkString("Proportional / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000054 fProp, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000055 drawText(canvas, SkString("< Proportional / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000056 fProp, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000057 drawText(canvas, SkString("Monospaced / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000058 fMono, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000059 drawText(canvas, SkString("< Monospaced / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000060 fMono, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000061 canvas->rotate(SkIntToScalar(-15));
62 canvas->translate(textHeight * 4, SkIntToScalar(50));
63 if (i > 0) {
64 canvas->translate(0, SkIntToScalar(50));
65 }
66 }
67 }
68
69 void drawText(SkCanvas* canvas, const SkString& string,
bungeman13b9c952016-05-12 10:09:30 -070070 sk_sp<SkTypeface> family, SkPaint::Align alignment) {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000071 SkPaint paint;
72 paint.setColor(SK_ColorBLACK);
73 paint.setAntiAlias(true);
74 paint.setVerticalText(true);
75 paint.setTextAlign(alignment);
bungeman13b9c952016-05-12 10:09:30 -070076 paint.setTypeface(std::move(family));
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000077 paint.setTextSize(textHeight);
78
Cary Clark2a475ea2017-04-28 15:35:12 -040079 canvas->drawString(string, y,
tomhudson@google.com75589252012-04-10 17:42:21 +000080 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
81 paint);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000082 y += textHeight;
83 }
84
85private:
86 typedef GM INHERITED;
87 SkScalar y, textHeight;
bungeman13b9c952016-05-12 10:09:30 -070088 sk_sp<SkTypeface> fProp;
89 sk_sp<SkTypeface> fMono;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000090};
91
92///////////////////////////////////////////////////////////////////////////////
93
94static GM* MyFactory(void*) { return new VertText2GM; }
95static GMRegistry reg(MyFactory);
96
97}