blob: 0da59ac6f9e78534b3ba87c17224a2d6cc59e573 [file] [log] [blame]
rmistryc4b84ae2014-06-23 06:59:15 -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 "SkTypeface.h"
11
12/* This test tries to define the effect of using hairline strokes on text.
13 * Provides non-hairline images for reference and consistency checks.
14 * glyph_pos_(h/n)_(s/f/b)
15 * -> test hairline/non-hairline stroke/fill/stroke+fill.
16 */
17static const SkScalar kTextHeight = 14.0f;
18static const char kText[] = "Proportional Hamburgefons #% fi";
19
20namespace skiagm {
21
22class GlyphPosGM : public GM {
23public:
24 GlyphPosGM(SkScalar strokeWidth, SkPaint::Style strokeStyle)
25 : fStrokeWidth(strokeWidth)
26 , fStrokeStyle(strokeStyle) {
27 }
28
29protected:
rmistryc4b84ae2014-06-23 06:59:15 -070030
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
rmistryc4b84ae2014-06-23 06:59:15 -070032 SkString str("glyph_pos");
33 if (fStrokeWidth == 0.0f) {
34 str.append("_h"); // h == Hairline.
35 } else {
36 str.append("_n"); // n == Normal.
37 }
38 if (fStrokeStyle == SkPaint::kStroke_Style) {
39 str.append("_s");
40 } else if (fStrokeStyle == SkPaint::kFill_Style) {
41 str.append("_f");
42 } else {
43 str.append("_b"); // b == Both.
44 }
45 return str;
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 SkISize onISize() override { return SkISize::Make(800, 600); }
rmistryc4b84ae2014-06-23 06:59:15 -070049
mtklein36352bf2015-03-25 18:17:31 -070050 void onDraw(SkCanvas* canvas) override {
rmistryc4b84ae2014-06-23 06:59:15 -070051 if (!fProp) {
Cary Clark992c7b02014-07-31 08:58:44 -040052 fProp.reset(sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal));
rmistryc4b84ae2014-06-23 06:59:15 -070053 }
54
55 // There's a black pixel at 40, 40 for reference.
56 canvas->drawPoint(40.0f, 40.0f, SK_ColorBLACK);
57
58 // Two reference images.
59 canvas->translate(50.0f, 50.0f);
60 drawTestCase(canvas, 1.0f);
61
62 canvas->translate(0.0f, 50.0f);
63 drawTestCase(canvas, 3.0f);
64
65 // Uniform scaling test.
66 canvas->translate(0.0f, 100.0f);
67 canvas->save();
68 canvas->scale(3.0f, 3.0f);
69 drawTestCase(canvas, 1.0f);
70 canvas->restore();
71
72 // Non-uniform scaling test.
73 canvas->translate(0.0f, 100.0f);
74 canvas->save();
75 canvas->scale(3.0f, 6.0f);
76 drawTestCase(canvas, 1.0f);
77 canvas->restore();
78
79 // Skew test.
80 canvas->translate(0.0f, 80.0f);
81 canvas->save();
82 canvas->scale(3.0f, 3.0f);
83 SkMatrix skew;
84 skew.setIdentity();
85 skew.setSkewX(SkScalarDiv(8.0f,
86 25.0f));
87 skew.setSkewY(SkScalarDiv(2.0f,
88 25.0f));
89 canvas->concat(skew);
90 drawTestCase(canvas, 1.0f);
91 canvas->restore();
92
93 // Perspective test.
94 canvas->translate(0.0f, 80.0f);
95 canvas->save();
96 SkMatrix perspective;
97 perspective.setIdentity();
98 perspective.setPerspX(-SkScalarDiv(SK_Scalar1, 340.0f));
99 perspective.setSkewX(SkScalarDiv(8.0f,
100 25.0f));
101 perspective.setSkewY(SkScalarDiv(2.0f,
102 25.0f));
103
104
105 canvas->concat(perspective);
106 drawTestCase(canvas, 1.0f);
107 canvas->restore();
108 }
109
110 void drawTestCase(SkCanvas* canvas, SkScalar textScale) {
111 SkPaint paint;
112 paint.setColor(SK_ColorBLACK);
113 paint.setAntiAlias(true);
114 paint.setTextSize(kTextHeight * textScale);
115 paint.setTypeface(fProp);
116 paint.setDevKernText(true);
117 paint.setStrokeWidth(fStrokeWidth);
118 paint.setStyle(fStrokeStyle);
119
120 // This demonstrates that we can not measure the text if there's a device transform. The
121 // canvas total matrix will end up being a device transform.
122 bool drawRef = !(canvas->getTotalMatrix().getType() &
123 ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
124
125 SkRect bounds;
126 if (drawRef) {
127 SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
128
129 paint.setStrokeWidth(0.0f);
130 paint.setStyle(SkPaint::kStroke_Style);
131
132 // Green box is the measured text bounds.
133 paint.setColor(SK_ColorGREEN);
134 canvas->drawRect(bounds, paint);
135
136 // Red line is the measured advance from the 0,0 of the text position.
137 paint.setColor(SK_ColorRED);
138 canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
139 }
140
141 // Black text is the testcase, eg. the text.
142 paint.setColor(SK_ColorBLACK);
143 paint.setStrokeWidth(fStrokeWidth);
144 paint.setStyle(fStrokeStyle);
145 canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint);
146
147 if (drawRef) {
148 SkScalar widths[sizeof(kText) - 1];
149 paint.getTextWidths(kText, sizeof(kText) - 1, widths, NULL);
150
151 paint.setStrokeWidth(0.0f);
152 paint.setStyle(SkPaint::kStroke_Style);
153
154 // Magenta lines are the positions for the characters.
155 paint.setColor(SK_ColorMAGENTA);
156 SkScalar w = bounds.x();
157 for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
158 canvas->drawLine(w, 0.0f, w, 5.0f, paint);
159 w += widths[i];
160 }
161 }
162 }
163
164private:
165 SkAutoTUnref<SkTypeface> fProp;
166 SkScalar fStrokeWidth;
167 SkPaint::Style fStrokeStyle;
168
169 typedef GM INHERITED;
170};
171
172//////////////////////////////////////////////////////////////////////////////
173
174static GM* GlyphPosHairlineStrokeAndFillFactory(void*) {
175 return new GlyphPosGM(0.0f, SkPaint::kStrokeAndFill_Style);
176}
177static GM* GlyphPosStrokeAndFillFactory(void*) {
178 return new GlyphPosGM(1.2f, SkPaint::kStrokeAndFill_Style);
179}
180static GM* GlyphPosHairlineStrokeFactory(void*) {
181 return new GlyphPosGM(0.0f, SkPaint::kStroke_Style);
182}
183static GM* GlyphPosStrokeFactory(void*) {
184 return new GlyphPosGM(1.2f, SkPaint::kStroke_Style);
185}
186static GM* GlyphPosHairlineFillFactory(void*) {
187 return new GlyphPosGM(0.0f, SkPaint::kFill_Style);
188}
189static GM* GlyphPosFillFactory(void*) {
190 return new GlyphPosGM(1.2f, SkPaint::kFill_Style);
191}
192
193static GMRegistry reg1(GlyphPosHairlineStrokeAndFillFactory);
194static GMRegistry reg2(GlyphPosStrokeAndFillFactory);
195static GMRegistry reg3(GlyphPosHairlineStrokeFactory);
196static GMRegistry reg4(GlyphPosStrokeFactory);
197static GMRegistry reg5(GlyphPosHairlineFillFactory);
198static GMRegistry reg6(GlyphPosFillFactory);
199
200
201}