1 /*
2 * Copyright 2011 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
9 #include "gm.h"
10 #include "SkCanvas.h"
11
12 namespace skiagm {
13
14 #define TEXT_SIZE 48
15 static const char gText[] = "Hello";
16 static const size_t gLen = sizeof(gText) - 1;
17
18 class VertTextGM : public GM {
19 public:
VertTextGM()20 VertTextGM() {}
21
22 protected:
23
onShortName()24 SkString onShortName() {
25 return SkString("verttext");
26 }
27
onISize()28 SkISize onISize() { return make_isize(640, 480); }
29
drawBaseline(SkCanvas * canvas,const SkPaint & paint,SkScalar x,SkScalar y)30 static void drawBaseline(SkCanvas* canvas, const SkPaint& paint,
31 SkScalar x, SkScalar y) {
32 SkScalar total = paint.measureText(gText, gLen);
33
34 SkPaint p;
35 p.setAntiAlias(true);
36 p.setColor(0x80FF0000);
37 canvas->drawLine(x, y,
38 paint.isVerticalText() ? x : x + total,
39 paint.isVerticalText() ? y + total : y,
40 p);
41
42 p.setColor(0xFF0000FF);
43 SkScalar adv[gLen];
44 paint.getTextWidths(gText, gLen, adv, NULL);
45 for (size_t i = 0; i < gLen; ++i) {
46 canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
47 if (paint.isVerticalText()) {
48 y += adv[i];
49 } else {
50 x += adv[i];
51 }
52 }
53 canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
54 }
55
onDraw(SkCanvas * canvas)56 virtual void onDraw(SkCanvas* canvas) {
57 SkScalar x = SkIntToScalar(100);
58 SkScalar y = SkIntToScalar(50);
59
60 for (int i = 0; i < 4; ++i) {
61 SkPaint paint;
62 paint.setAntiAlias(true);
63 paint.setTextSize(SkIntToScalar(TEXT_SIZE));
64
65
66 paint.setVerticalText(false);
67 drawBaseline(canvas, paint, x, y);
68 canvas->drawText(gText, gLen, x, y, paint);
69
70 paint.setVerticalText(true);
71 drawBaseline(canvas, paint, x, y);
72 canvas->drawText(gText, gLen, x, y, paint);
73
74 x += SkIntToScalar(40);
75 y += SkIntToScalar(120);
76
77 canvas->rotate(SkIntToScalar(-15));
78 }
79 }
80
81 private:
82 typedef GM INHERITED;
83 };
84
85 ///////////////////////////////////////////////////////////////////////////////
86
MyFactory(void *)87 static GM* MyFactory(void*) { return new VertTextGM; }
88 static GMRegistry reg(MyFactory);
89
90 }
91