blob: 4896cc992a49dad96b36a8e134cae631bad920d4 [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:
kkinnunenb4a797f2015-05-21 06:15:28 -070021 VertText2GM()
halcanary96fcdcc2015-08-27 07:41:13 -070022 : fProp(nullptr)
23 , fMono(nullptr) {
reed@google.com563a3b42012-06-26 19:24:50 +000024 }
25
26 virtual ~VertText2GM() {
27 SkSafeUnref(fProp);
28 SkSafeUnref(fMono);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000029 }
30
31protected:
kkinnunenb4a797f2015-05-21 06:15:28 -070032 void onOnceBeforeDraw() override {
33 const int pointSize = 24;
34 textHeight = SkIntToScalar(pointSize);
caryclark37213552015-07-24 11:08:01 -070035 fProp = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("sans-serif"),
36 SkTypeface::kNormal);
37 fMono = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("monospace"),
38 SkTypeface::kNormal);
kkinnunenb4a797f2015-05-21 06:15:28 -070039 }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070042 SkString name("verttext2");
43 name.append(sk_tool_utils::major_platform_os_name());
44 return name;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 SkISize onISize() override { return SkISize::Make(640, 480); }
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000048
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000050 for (int i = 0; i < 3; ++i) {
51 SkPaint paint;
52 paint.setColor(SK_ColorRED);
53 paint.setAntiAlias(true);
54 y = textHeight;
55 canvas->drawLine(0, SkIntToScalar(10),
56 SkIntToScalar(110), SkIntToScalar(10), paint);
57 canvas->drawLine(0, SkIntToScalar(240),
58 SkIntToScalar(110), SkIntToScalar(240), paint);
59 canvas->drawLine(0, SkIntToScalar(470),
60 SkIntToScalar(110), SkIntToScalar(470), paint);
61 drawText(canvas, SkString("Proportional / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000062 fProp, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000063 drawText(canvas, SkString("< Proportional / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000064 fProp, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000065 drawText(canvas, SkString("Monospaced / Top Aligned"),
reed@google.com563a3b42012-06-26 19:24:50 +000066 fMono, SkPaint::kLeft_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000067 drawText(canvas, SkString("< Monospaced / Centered >"),
reed@google.com563a3b42012-06-26 19:24:50 +000068 fMono, SkPaint::kCenter_Align);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000069 canvas->rotate(SkIntToScalar(-15));
70 canvas->translate(textHeight * 4, SkIntToScalar(50));
71 if (i > 0) {
72 canvas->translate(0, SkIntToScalar(50));
73 }
74 }
75 }
76
77 void drawText(SkCanvas* canvas, const SkString& string,
78 SkTypeface* family, SkPaint::Align alignment) {
79 SkPaint paint;
80 paint.setColor(SK_ColorBLACK);
81 paint.setAntiAlias(true);
82 paint.setVerticalText(true);
83 paint.setTextAlign(alignment);
84 paint.setTypeface(family);
85 paint.setTextSize(textHeight);
86
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 canvas->drawText(string.c_str(), string.size(), y,
tomhudson@google.com75589252012-04-10 17:42:21 +000088 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
89 paint);
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000090 y += textHeight;
91 }
92
93private:
94 typedef GM INHERITED;
95 SkScalar y, textHeight;
reed@google.com563a3b42012-06-26 19:24:50 +000096 SkTypeface* fProp;
97 SkTypeface* fMono;
caryclark@google.com5fbb4dc2011-12-21 20:06:30 +000098};
99
100///////////////////////////////////////////////////////////////////////////////
101
102static GM* MyFactory(void*) { return new VertText2GM; }
103static GMRegistry reg(MyFactory);
104
105}