• 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 constexpr int kWidth = 480;
16 constexpr int kHeight = 600;
17 constexpr SkScalar kTextHeight = 64.0f;
18 constexpr int kMaxStringLength = 12;
19 
20 static void drawTestCase(SkCanvas*, const char*, SkScalar, const SkPaint&);
21 
DEF_SIMPLE_GM_BG(glyph_pos_align,canvas,kWidth,kHeight,SK_ColorBLACK)22 DEF_SIMPLE_GM_BG(glyph_pos_align, canvas, kWidth, kHeight, SK_ColorBLACK) {
23         SkPaint paint;
24         paint.setTextSize(kTextHeight);
25         paint.setFakeBoldText(true);
26         const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
27         const SkPoint pts[] = {{0, 0}, {kWidth, kHeight}};
28         paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
29                                                      SkShader::kMirror_TileMode));
30         paint.setTextAlign(SkPaint::kRight_Align);
31         drawTestCase(canvas, "Right Align", kTextHeight, paint);
32 
33         paint.setTextAlign(SkPaint::kCenter_Align);
34         drawTestCase(canvas, "Center Align", 4 * kTextHeight, paint);
35 
36         paint.setTextAlign(SkPaint::kLeft_Align);
37         drawTestCase(canvas, "Left Align", 7 * kTextHeight, paint);
38 }
39 
drawTestCase(SkCanvas * canvas,const char * text,SkScalar y,const SkPaint & paint)40 void drawTestCase(SkCanvas* canvas, const char* text, SkScalar y, const SkPaint& paint) {
41         SkScalar widths[kMaxStringLength];
42         SkScalar posX[kMaxStringLength];
43         SkPoint pos[kMaxStringLength];
44         int length = SkToInt(strlen(text));
45         SkASSERT(length <= kMaxStringLength);
46 
47         paint.getTextWidths(text, length, widths);
48 
49         float originX;
50         switch (paint.getTextAlign()) {
51             case SkPaint::kRight_Align: originX = 1; break;
52             case SkPaint::kCenter_Align: originX = 0.5f; break;
53             case SkPaint::kLeft_Align: originX = 0; break;
54             default: SkFAIL("Invalid paint origin"); return;
55         }
56 
57         float x = kTextHeight;
58         for (int i = 0; i < length; ++i) {
59             posX[i] = x + originX * widths[i];
60             pos[i].set(posX[i], i ? pos[i - 1].y() + 3 : y + kTextHeight);
61             x += widths[i];
62         }
63 
64         canvas->drawPosTextH(text, length, posX, y, paint);
65         canvas->drawPosText(text, length, pos, paint);
66 }
67