• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "sk_tool_utils.h"
14 #include "SkCanvas.h"
15 #include "SkTypeface.h"
16 
17 namespace skiagm {
18 
19 class VertText2GM : public GM {
20 public:
VertText2GM()21     VertText2GM() {}
22 
23 protected:
onOnceBeforeDraw()24     void onOnceBeforeDraw() override {
25         const int pointSize = 24;
26         textHeight = SkIntToScalar(pointSize);
27         fProp = SkTypeface::MakeFromName("sans-serif", SkFontStyle());
28         fMono = SkTypeface::MakeFromName("monospace",  SkFontStyle());
29     }
30 
onShortName()31     SkString onShortName() override {
32         SkString name("verttext2");
33         name.append(sk_tool_utils::platform_font_manager());
34         return name;
35     }
36 
onISize()37     SkISize onISize() override { return SkISize::Make(640, 480); }
38 
onDraw(SkCanvas * canvas)39     void onDraw(SkCanvas* canvas) override {
40         for (int i = 0; i < 3; ++i) {
41             SkPaint paint;
42             paint.setColor(SK_ColorRED);
43             paint.setAntiAlias(true);
44             y = textHeight;
45             canvas->drawLine(0, SkIntToScalar(10),
46                     SkIntToScalar(110), SkIntToScalar(10), paint);
47             canvas->drawLine(0, SkIntToScalar(240),
48                     SkIntToScalar(110), SkIntToScalar(240), paint);
49             canvas->drawLine(0, SkIntToScalar(470),
50                     SkIntToScalar(110), SkIntToScalar(470), paint);
51             drawText(canvas, SkString("Proportional / Top Aligned"),
52                      fProp,  SkPaint::kLeft_Align);
53             drawText(canvas, SkString("<   Proportional / Centered   >"),
54                      fProp,  SkPaint::kCenter_Align);
55             drawText(canvas, SkString("Monospaced / Top Aligned"),
56                      fMono, SkPaint::kLeft_Align);
57             drawText(canvas, SkString("<    Monospaced / Centered    >"),
58                      fMono, SkPaint::kCenter_Align);
59             canvas->rotate(SkIntToScalar(-15));
60             canvas->translate(textHeight * 4, SkIntToScalar(50));
61             if (i > 0) {
62                 canvas->translate(0, SkIntToScalar(50));
63             }
64         }
65     }
66 
drawText(SkCanvas * canvas,const SkString & string,sk_sp<SkTypeface> family,SkPaint::Align alignment)67     void drawText(SkCanvas* canvas, const SkString& string,
68                   sk_sp<SkTypeface> family, SkPaint::Align alignment) {
69         SkPaint paint;
70         paint.setColor(SK_ColorBLACK);
71         paint.setAntiAlias(true);
72         paint.setVerticalText(true);
73         paint.setTextAlign(alignment);
74         paint.setTypeface(std::move(family));
75         paint.setTextSize(textHeight);
76 
77         canvas->drawString(string, y,
78                 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
79                 paint);
80         y += textHeight;
81     }
82 
83 private:
84     typedef GM INHERITED;
85     SkScalar y, textHeight;
86     sk_sp<SkTypeface> fProp;
87     sk_sp<SkTypeface> fMono;
88 };
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 
MyFactory(void *)92 static GM* MyFactory(void*) { return new VertText2GM; }
93 static GMRegistry reg(MyFactory);
94 
95 }
96