1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2014 Google Inc.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Use of this source code is governed by a BSD-style license that can be
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * found in the LICENSE file.
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gm.h"
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "SkCanvas.h"
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "SkGradientShader.h"
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * This test exercises drawPosTextH and drawPosText with every text align.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const int kWidth = 480;
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const int kHeight = 600;
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const SkScalar kTextHeight = 64.0f;
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const int kMaxStringLength = 12;
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace skiagm {
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass GlyphPosAlignGM : public GM {
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprotected:
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    virtual uint32_t onGetFlags() const SK_OVERRIDE {
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        return kSkipTiled_Flag;
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    virtual SkString onShortName() SK_OVERRIDE {
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        return SkString("glyph_pos_align");
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    }
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    virtual SkISize onISize() { return SkISize::Make(kWidth, kHeight); }
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        canvas->clear(SK_ColorBLACK);
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        SkPaint paint;
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        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];
62        int length = strlen(text);
63        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}
100