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