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