caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 13 | #include "sk_tool_utils.h" |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
| 15 | #include "SkTypeface.h" |
| 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class VertText2GM : public GM { |
| 20 | public: |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 21 | VertText2GM() {} |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 22 | |
| 23 | protected: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 24 | void onOnceBeforeDraw() override { |
| 25 | const int pointSize = 24; |
| 26 | textHeight = SkIntToScalar(pointSize); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 27 | fProp = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("sans-serif"), |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 28 | SkFontStyle()); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 29 | fMono = SkTypeface::MakeFromName(sk_tool_utils::platform_font_name("monospace"), |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 30 | SkFontStyle()); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 31 | } |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkString onShortName() override { |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 34 | SkString name("verttext2"); |
| 35 | name.append(sk_tool_utils::major_platform_os_name()); |
| 36 | return name; |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkISize onISize() override { return SkISize::Make(640, 480); } |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | void onDraw(SkCanvas* canvas) override { |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 42 | 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.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 54 | fProp, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 55 | drawText(canvas, SkString("< Proportional / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 56 | fProp, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 57 | drawText(canvas, SkString("Monospaced / Top Aligned"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 58 | fMono, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 59 | drawText(canvas, SkString("< Monospaced / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 60 | fMono, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 61 | 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, |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 70 | sk_sp<SkTypeface> family, SkPaint::Align alignment) { |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 71 | SkPaint paint; |
| 72 | paint.setColor(SK_ColorBLACK); |
| 73 | paint.setAntiAlias(true); |
| 74 | paint.setVerticalText(true); |
| 75 | paint.setTextAlign(alignment); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 76 | paint.setTypeface(std::move(family)); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 77 | paint.setTextSize(textHeight); |
| 78 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 79 | canvas->drawString(string, y, |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 80 | SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240), |
| 81 | paint); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 82 | y += textHeight; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | typedef GM INHERITED; |
| 87 | SkScalar y, textHeight; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 88 | sk_sp<SkTypeface> fProp; |
| 89 | sk_sp<SkTypeface> fMono; |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static GM* MyFactory(void*) { return new VertText2GM; } |
| 95 | static GMRegistry reg(MyFactory); |
| 96 | |
| 97 | } |