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: |
| 21 | VertText2GM() { |
| 22 | const int pointSize = 24; |
| 23 | textHeight = SkIntToScalar(pointSize); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 24 | fProp = sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal); |
| 25 | fMono = sk_tool_utils::create_portable_typeface("Courier New", SkTypeface::kNormal); |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | virtual ~VertText2GM() { |
| 29 | SkSafeUnref(fProp); |
| 30 | SkSafeUnref(fMono); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 34 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 35 | return kSkipTiled_Flag; |
| 36 | } |
| 37 | |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 38 | |
| 39 | SkString onShortName() { |
| 40 | return SkString("verttext2"); |
| 41 | } |
| 42 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 43 | SkISize onISize() { return SkISize::Make(640, 480); } |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 44 | |
| 45 | virtual void onDraw(SkCanvas* canvas) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 47 | for (int i = 0; i < 3; ++i) { |
| 48 | SkPaint paint; |
| 49 | paint.setColor(SK_ColorRED); |
| 50 | paint.setAntiAlias(true); |
| 51 | y = textHeight; |
| 52 | canvas->drawLine(0, SkIntToScalar(10), |
| 53 | SkIntToScalar(110), SkIntToScalar(10), paint); |
| 54 | canvas->drawLine(0, SkIntToScalar(240), |
| 55 | SkIntToScalar(110), SkIntToScalar(240), paint); |
| 56 | canvas->drawLine(0, SkIntToScalar(470), |
| 57 | SkIntToScalar(110), SkIntToScalar(470), paint); |
| 58 | drawText(canvas, SkString("Proportional / Top Aligned"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 59 | fProp, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 60 | drawText(canvas, SkString("< Proportional / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 61 | fProp, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 62 | drawText(canvas, SkString("Monospaced / Top Aligned"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 63 | fMono, SkPaint::kLeft_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 64 | drawText(canvas, SkString("< Monospaced / Centered >"), |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 65 | fMono, SkPaint::kCenter_Align); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 66 | canvas->rotate(SkIntToScalar(-15)); |
| 67 | canvas->translate(textHeight * 4, SkIntToScalar(50)); |
| 68 | if (i > 0) { |
| 69 | canvas->translate(0, SkIntToScalar(50)); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void drawText(SkCanvas* canvas, const SkString& string, |
| 75 | SkTypeface* family, SkPaint::Align alignment) { |
| 76 | SkPaint paint; |
| 77 | paint.setColor(SK_ColorBLACK); |
| 78 | paint.setAntiAlias(true); |
| 79 | paint.setVerticalText(true); |
| 80 | paint.setTextAlign(alignment); |
| 81 | paint.setTypeface(family); |
| 82 | paint.setTextSize(textHeight); |
| 83 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 84 | canvas->drawText(string.c_str(), string.size(), y, |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 85 | SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240), |
| 86 | paint); |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 87 | y += textHeight; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef GM INHERITED; |
| 92 | SkScalar y, textHeight; |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 93 | SkTypeface* fProp; |
| 94 | SkTypeface* fMono; |
caryclark@google.com | 5fbb4dc | 2011-12-21 20:06:30 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | static GM* MyFactory(void*) { return new VertText2GM; } |
| 100 | static GMRegistry reg(MyFactory); |
| 101 | |
| 102 | } |