cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | |
| 12 | /** |
| 13 | * This test exercises drawPosTextH and drawPosText with every text align. |
| 14 | */ |
| 15 | static const int kWidth = 480; |
| 16 | static const int kHeight = 600; |
| 17 | static const SkScalar kTextHeight = 64.0f; |
| 18 | static const int kMaxStringLength = 12; |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | class GlyphPosAlignGM : public GM { |
| 23 | protected: |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkString onShortName() override { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 26 | return SkString("glyph_pos_align"); |
| 27 | } |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | void onDraw(SkCanvas* canvas) override { |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 32 | canvas->clear(SK_ColorBLACK); |
| 33 | |
| 34 | SkPaint paint; |
| 35 | paint.setTextSize(kTextHeight); |
| 36 | paint.setFakeBoldText(true); |
| 37 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 38 | const SkPoint pts[] = {{0, 0}, {kWidth, kHeight}}; |
| 39 | SkAutoTUnref<SkShader> grad(SkGradientShader::CreateLinear(pts, colors, NULL, |
| 40 | SK_ARRAY_COUNT(colors), |
| 41 | SkShader::kMirror_TileMode)); |
| 42 | paint.setShader(grad); |
| 43 | |
| 44 | |
| 45 | paint.setTextAlign(SkPaint::kRight_Align); |
| 46 | drawTestCase(canvas, "Right Align", kTextHeight, paint); |
| 47 | |
| 48 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 49 | drawTestCase(canvas, "Center Align", 4 * kTextHeight, paint); |
| 50 | |
| 51 | paint.setTextAlign(SkPaint::kLeft_Align); |
| 52 | drawTestCase(canvas, "Left Align", 7 * kTextHeight, paint); |
| 53 | } |
| 54 | |
| 55 | void drawTestCase(SkCanvas* canvas, const char* text, SkScalar y, const SkPaint& paint) { |
| 56 | SkScalar widths[kMaxStringLength]; |
| 57 | SkScalar posX[kMaxStringLength]; |
| 58 | SkPoint pos[kMaxStringLength]; |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 59 | int length = SkToInt(strlen(text)); |
cdalton | b2808cd | 2014-07-25 14:13:57 -0700 | [diff] [blame] | 60 | SkASSERT(length <= kMaxStringLength); |
| 61 | |
| 62 | paint.getTextWidths(text, length, widths); |
| 63 | |
| 64 | float originX; |
| 65 | switch (paint.getTextAlign()) { |
| 66 | case SkPaint::kRight_Align: originX = 1; break; |
| 67 | case SkPaint::kCenter_Align: originX = 0.5f; break; |
| 68 | case SkPaint::kLeft_Align: originX = 0; break; |
| 69 | default: SkFAIL("Invalid paint origin"); return; |
| 70 | } |
| 71 | |
| 72 | float x = kTextHeight; |
| 73 | for (int i = 0; i < length; ++i) { |
| 74 | posX[i] = x + originX * widths[i]; |
| 75 | pos[i].set(posX[i], i ? pos[i - 1].y() + 3 : y + kTextHeight); |
| 76 | x += widths[i]; |
| 77 | } |
| 78 | |
| 79 | canvas->drawPosTextH(text, length, posX, y, paint); |
| 80 | canvas->drawPosText(text, length, pos, paint); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | |
| 85 | typedef GM INHERITED; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | static GM* GlyphPosAlignFactory(void*) { |
| 91 | return new GlyphPosAlignGM(); |
| 92 | } |
| 93 | |
| 94 | static GMRegistry reg(GlyphPosAlignFactory); |
| 95 | |
| 96 | } |