3f1ea35e0ac1e4e549cc7b38827604f92cf8cfc4
[platform/upstream/libSkiaSharp.git] / gm / glyph_pos.cpp
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 "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  */
17 static const SkScalar kTextHeight = 14.0f;
18 static const char kText[] = "Proportional Hamburgefons #% fi";
19
20 namespace skiagm {
21
22 class GlyphPosGM : public GM {
23 public:
24     GlyphPosGM(SkScalar strokeWidth, SkPaint::Style strokeStyle)
25         : fStrokeWidth(strokeWidth)
26         , fStrokeStyle(strokeStyle) {
27         }
28
29 protected:
30
31     SkString onShortName() SK_OVERRIDE {
32         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
48     SkISize onISize() SK_OVERRIDE { return SkISize::Make(800, 600); }
49
50     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
51         if (!fProp) {
52             fProp.reset(sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal));
53         }
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
164 private:
165     SkAutoTUnref<SkTypeface> fProp;
166     SkScalar fStrokeWidth;
167     SkPaint::Style fStrokeStyle;
168
169     typedef GM INHERITED;
170 };
171
172 //////////////////////////////////////////////////////////////////////////////
173
174 static GM* GlyphPosHairlineStrokeAndFillFactory(void*) {
175     return new GlyphPosGM(0.0f, SkPaint::kStrokeAndFill_Style);
176 }
177 static GM* GlyphPosStrokeAndFillFactory(void*) {
178     return new GlyphPosGM(1.2f, SkPaint::kStrokeAndFill_Style);
179 }
180 static GM* GlyphPosHairlineStrokeFactory(void*) {
181     return new GlyphPosGM(0.0f, SkPaint::kStroke_Style);
182 }
183 static GM* GlyphPosStrokeFactory(void*) {
184     return new GlyphPosGM(1.2f, SkPaint::kStroke_Style);
185 }
186 static GM* GlyphPosHairlineFillFactory(void*) {
187     return new GlyphPosGM(0.0f, SkPaint::kFill_Style);
188 }
189 static GM* GlyphPosFillFactory(void*) {
190     return new GlyphPosGM(1.2f, SkPaint::kFill_Style);
191 }
192
193 static GMRegistry reg1(GlyphPosHairlineStrokeAndFillFactory);
194 static GMRegistry reg2(GlyphPosStrokeAndFillFactory);
195 static GMRegistry reg3(GlyphPosHairlineStrokeFactory);
196 static GMRegistry reg4(GlyphPosStrokeFactory);
197 static GMRegistry reg5(GlyphPosHairlineFillFactory);
198 static GMRegistry reg6(GlyphPosFillFactory);
199
200
201 }