blob: 90329ed7fdc530982224b0d51cf9e49d2f445f9c [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
52 // There's a black pixel at 40, 40 for reference.
53 canvas->drawPoint(40.0f, 40.0f, SK_ColorBLACK);
54
55 // Two reference images.
56 canvas->translate(50.0f, 50.0f);
57 drawTestCase(canvas, 1.0f);
58
59 canvas->translate(0.0f, 50.0f);
60 drawTestCase(canvas, 3.0f);
61
62 // Uniform scaling test.
63 canvas->translate(0.0f, 100.0f);
64 canvas->save();
65 canvas->scale(3.0f, 3.0f);
66 drawTestCase(canvas, 1.0f);
67 canvas->restore();
68
69 // Non-uniform scaling test.
70 canvas->translate(0.0f, 100.0f);
71 canvas->save();
72 canvas->scale(3.0f, 6.0f);
73 drawTestCase(canvas, 1.0f);
74 canvas->restore();
75
76 // Skew test.
77 canvas->translate(0.0f, 80.0f);
78 canvas->save();
79 canvas->scale(3.0f, 3.0f);
80 SkMatrix skew;
81 skew.setIdentity();
reed80ea19c2015-05-12 10:37:34 -070082 skew.setSkewX(8.0f / 25.0f);
83 skew.setSkewY(2.0f / 25.0f);
rmistryc4b84ae2014-06-23 06:59:15 -070084 canvas->concat(skew);
85 drawTestCase(canvas, 1.0f);
86 canvas->restore();
87
88 // Perspective test.
89 canvas->translate(0.0f, 80.0f);
90 canvas->save();
91 SkMatrix perspective;
92 perspective.setIdentity();
reed80ea19c2015-05-12 10:37:34 -070093 perspective.setPerspX(-SkScalarInvert(340));
94 perspective.setSkewX(8.0f / 25.0f);
95 perspective.setSkewY(2.0f / 25.0f);
rmistryc4b84ae2014-06-23 06:59:15 -070096
97
98 canvas->concat(perspective);
99 drawTestCase(canvas, 1.0f);
100 canvas->restore();
101 }
102
103 void drawTestCase(SkCanvas* canvas, SkScalar textScale) {
104 SkPaint paint;
105 paint.setColor(SK_ColorBLACK);
106 paint.setAntiAlias(true);
107 paint.setTextSize(kTextHeight * textScale);
caryclarkef14cb32015-07-16 14:16:04 -0700108 sk_tool_utils::set_portable_typeface_always(&paint);
rmistryc4b84ae2014-06-23 06:59:15 -0700109 paint.setStrokeWidth(fStrokeWidth);
110 paint.setStyle(fStrokeStyle);
111
112 // This demonstrates that we can not measure the text if there's a device transform. The
113 // canvas total matrix will end up being a device transform.
114 bool drawRef = !(canvas->getTotalMatrix().getType() &
115 ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
116
117 SkRect bounds;
118 if (drawRef) {
119 SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
120
121 paint.setStrokeWidth(0.0f);
122 paint.setStyle(SkPaint::kStroke_Style);
123
124 // Green box is the measured text bounds.
125 paint.setColor(SK_ColorGREEN);
126 canvas->drawRect(bounds, paint);
127
128 // Red line is the measured advance from the 0,0 of the text position.
129 paint.setColor(SK_ColorRED);
130 canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
131 }
132
133 // Black text is the testcase, eg. the text.
134 paint.setColor(SK_ColorBLACK);
135 paint.setStrokeWidth(fStrokeWidth);
136 paint.setStyle(fStrokeStyle);
137 canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint);
138
139 if (drawRef) {
140 SkScalar widths[sizeof(kText) - 1];
141 paint.getTextWidths(kText, sizeof(kText) - 1, widths, NULL);
142
143 paint.setStrokeWidth(0.0f);
144 paint.setStyle(SkPaint::kStroke_Style);
145
146 // Magenta lines are the positions for the characters.
147 paint.setColor(SK_ColorMAGENTA);
148 SkScalar w = bounds.x();
149 for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
150 canvas->drawLine(w, 0.0f, w, 5.0f, paint);
151 w += widths[i];
152 }
153 }
154 }
155
156private:
rmistryc4b84ae2014-06-23 06:59:15 -0700157 SkScalar fStrokeWidth;
158 SkPaint::Style fStrokeStyle;
159
160 typedef GM INHERITED;
161};
162
163//////////////////////////////////////////////////////////////////////////////
164
165static GM* GlyphPosHairlineStrokeAndFillFactory(void*) {
166 return new GlyphPosGM(0.0f, SkPaint::kStrokeAndFill_Style);
167}
168static GM* GlyphPosStrokeAndFillFactory(void*) {
169 return new GlyphPosGM(1.2f, SkPaint::kStrokeAndFill_Style);
170}
171static GM* GlyphPosHairlineStrokeFactory(void*) {
172 return new GlyphPosGM(0.0f, SkPaint::kStroke_Style);
173}
174static GM* GlyphPosStrokeFactory(void*) {
175 return new GlyphPosGM(1.2f, SkPaint::kStroke_Style);
176}
177static GM* GlyphPosHairlineFillFactory(void*) {
178 return new GlyphPosGM(0.0f, SkPaint::kFill_Style);
179}
180static GM* GlyphPosFillFactory(void*) {
181 return new GlyphPosGM(1.2f, SkPaint::kFill_Style);
182}
183
184static GMRegistry reg1(GlyphPosHairlineStrokeAndFillFactory);
185static GMRegistry reg2(GlyphPosStrokeAndFillFactory);
186static GMRegistry reg3(GlyphPosHairlineStrokeFactory);
187static GMRegistry reg4(GlyphPosStrokeFactory);
188static GMRegistry reg5(GlyphPosHairlineFillFactory);
189static GMRegistry reg6(GlyphPosFillFactory);
190
191
192}