caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class VertText2GM : public GM { |
| 20 | public: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 21 | VertText2GM() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | : fProp(nullptr) |
| 23 | , fMono(nullptr) { |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | virtual ~VertText2GM() { |
| 27 | SkSafeUnref(fProp); |
| 28 | SkSafeUnref(fMono); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | protected: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 32 | void onOnceBeforeDraw() override { |
| 33 | const int pointSize = 24; |
| 34 | textHeight = SkIntToScalar(pointSize); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 35 | 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); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 39 | } |
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 | SkString onShortName() override { |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 42 | SkString name("verttext2"); |
| 43 | name.append(sk_tool_utils::major_platform_os_name()); |
| 44 | return name; |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | SkISize onISize() override { return SkISize::Make(640, 480); } |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | void onDraw(SkCanvas* canvas) override { |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 50 | 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.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 62 | fProp, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 63 | drawText(canvas, SkString("< Proportional / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 64 | fProp, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 65 | drawText(canvas, SkString("Monospaced / Top Aligned"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 66 | fMono, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 67 | drawText(canvas, SkString("< Monospaced / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 68 | fMono, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 69 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | canvas->drawText(string.c_str(), string.size(), y, |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 88 | SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240), |
| 89 | paint); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 90 | y += textHeight; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | typedef GM INHERITED; |
| 95 | SkScalar y, textHeight; |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 96 | SkTypeface* fProp; |
| 97 | SkTypeface* fMono; |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | static GM* MyFactory(void*) { return new VertText2GM; } |
| 103 | static GMRegistry reg(MyFactory); |
| 104 | |
| 105 | } |