• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
onGetFlags() const24     virtual uint32_t onGetFlags() const SK_OVERRIDE {
25         return kSkipTiled_Flag;
26     }
27 
onShortName()28     virtual SkString onShortName() SK_OVERRIDE {
29         return SkString("glyph_pos_align");
30     }
31 
onISize()32     virtual SkISize onISize() { return SkISize::Make(kWidth, kHeight); }
33 
onDraw(SkCanvas * canvas)34     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
35         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 
drawTestCase(SkCanvas * canvas,const char * text,SkScalar y,const SkPaint & paint)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 
86 private:
87 
88     typedef GM INHERITED;
89 };
90 
91 //////////////////////////////////////////////////////////////////////////////
92 
GlyphPosAlignFactory(void *)93 static GM* GlyphPosAlignFactory(void*) {
94     return new GlyphPosAlignGM();
95 }
96 
97 static GMRegistry reg(GlyphPosAlignFactory);
98 
99 }
100