blob: ef2eae24ab23c1560d2f5cec12d3dd2927ebd701 [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:
mtklein72c9faa2015-01-09 10:06:39 -080024 uint32_t onGetFlags() const SK_OVERRIDE {
cdaltonb2808cd2014-07-25 14:13:57 -070025 return kSkipTiled_Flag;
26 }
27
mtklein72c9faa2015-01-09 10:06:39 -080028 SkString onShortName() SK_OVERRIDE {
cdaltonb2808cd2014-07-25 14:13:57 -070029 return SkString("glyph_pos_align");
30 }
31
mtklein72c9faa2015-01-09 10:06:39 -080032 SkISize onISize() SK_OVERRIDE { return SkISize::Make(kWidth, kHeight); }
cdaltonb2808cd2014-07-25 14:13:57 -070033
mtklein72c9faa2015-01-09 10:06:39 -080034 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
cdaltonb2808cd2014-07-25 14:13:57 -070035 canvas->clear(SK_ColorBLACK);
36
37 SkPaint paint;
38 paint.setTextSize(kTextHeight);
39 paint.setFakeBoldText(true);
40 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
41 const SkPoint pts[] = {{0, 0}, {kWidth, kHeight}};
42 SkAutoTUnref<SkShader> grad(SkGradientShader::CreateLinear(pts, colors, NULL,
43 SK_ARRAY_COUNT(colors),
44 SkShader::kMirror_TileMode));
45 paint.setShader(grad);
46
47
48 paint.setTextAlign(SkPaint::kRight_Align);
49 drawTestCase(canvas, "Right Align", kTextHeight, paint);
50
51 paint.setTextAlign(SkPaint::kCenter_Align);
52 drawTestCase(canvas, "Center Align", 4 * kTextHeight, paint);
53
54 paint.setTextAlign(SkPaint::kLeft_Align);
55 drawTestCase(canvas, "Left Align", 7 * kTextHeight, paint);
56 }
57
58 void drawTestCase(SkCanvas* canvas, const char* text, SkScalar y, const SkPaint& paint) {
59 SkScalar widths[kMaxStringLength];
60 SkScalar posX[kMaxStringLength];
61 SkPoint pos[kMaxStringLength];
bsalomonfbaace02014-12-12 16:41:46 -080062 int length = SkToInt(strlen(text));
cdaltonb2808cd2014-07-25 14:13:57 -070063 SkASSERT(length <= kMaxStringLength);
64
65 paint.getTextWidths(text, length, widths);
66
67 float originX;
68 switch (paint.getTextAlign()) {
69 case SkPaint::kRight_Align: originX = 1; break;
70 case SkPaint::kCenter_Align: originX = 0.5f; break;
71 case SkPaint::kLeft_Align: originX = 0; break;
72 default: SkFAIL("Invalid paint origin"); return;
73 }
74
75 float x = kTextHeight;
76 for (int i = 0; i < length; ++i) {
77 posX[i] = x + originX * widths[i];
78 pos[i].set(posX[i], i ? pos[i - 1].y() + 3 : y + kTextHeight);
79 x += widths[i];
80 }
81
82 canvas->drawPosTextH(text, length, posX, y, paint);
83 canvas->drawPosText(text, length, pos, paint);
84 }
85
86private:
87
88 typedef GM INHERITED;
89};
90
91//////////////////////////////////////////////////////////////////////////////
92
93static GM* GlyphPosAlignFactory(void*) {
94 return new GlyphPosAlignGM();
95}
96
97static GMRegistry reg(GlyphPosAlignFactory);
98
99}