blob: e8528047aa5dabfc0149e53b46445c29cf5e56cb [file] [log] [blame]
cdaltonb2808cd2014-07-25 14:13:57 -07001/*
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 */
15static const int kWidth = 480;
16static const int kHeight = 600;
17static const SkScalar kTextHeight = 64.0f;
18static const int kMaxStringLength = 12;
19
20namespace skiagm {
21
22class GlyphPosAlignGM : public GM {
23protected:
cdaltonb2808cd2014-07-25 14:13:57 -070024
mtklein36352bf2015-03-25 18:17:31 -070025 SkString onShortName() override {
cdaltonb2808cd2014-07-25 14:13:57 -070026 return SkString("glyph_pos_align");
27 }
28
mtklein36352bf2015-03-25 18:17:31 -070029 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
cdaltonb2808cd2014-07-25 14:13:57 -070030
mtklein36352bf2015-03-25 18:17:31 -070031 void onDraw(SkCanvas* canvas) override {
cdaltonb2808cd2014-07-25 14:13:57 -070032 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];
bsalomonfbaace02014-12-12 16:41:46 -080059 int length = SkToInt(strlen(text));
cdaltonb2808cd2014-07-25 14:13:57 -070060 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
83private:
84
85 typedef GM INHERITED;
86};
87
88//////////////////////////////////////////////////////////////////////////////
89
90static GM* GlyphPosAlignFactory(void*) {
91 return new GlyphPosAlignGM();
92}
93
94static GMRegistry reg(GlyphPosAlignFactory);
95
96}