2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkTypeface.h"
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.
17 static const SkScalar kTextHeight = 14.0f;
18 static const char kText[] = "Proportional Hamburgefons #% fi";
22 class GlyphPosGM : public GM {
24 GlyphPosGM(SkScalar strokeWidth, SkPaint::Style strokeStyle)
25 : fStrokeWidth(strokeWidth)
26 , fStrokeStyle(strokeStyle) {
31 SkString onShortName() SK_OVERRIDE {
32 SkString str("glyph_pos");
33 if (fStrokeWidth == 0.0f) {
34 str.append("_h"); // h == Hairline.
36 str.append("_n"); // n == Normal.
38 if (fStrokeStyle == SkPaint::kStroke_Style) {
40 } else if (fStrokeStyle == SkPaint::kFill_Style) {
43 str.append("_b"); // b == Both.
48 SkISize onISize() SK_OVERRIDE { return SkISize::Make(800, 600); }
50 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
52 fProp.reset(sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal));
55 // There's a black pixel at 40, 40 for reference.
56 canvas->drawPoint(40.0f, 40.0f, SK_ColorBLACK);
58 // Two reference images.
59 canvas->translate(50.0f, 50.0f);
60 drawTestCase(canvas, 1.0f);
62 canvas->translate(0.0f, 50.0f);
63 drawTestCase(canvas, 3.0f);
65 // Uniform scaling test.
66 canvas->translate(0.0f, 100.0f);
68 canvas->scale(3.0f, 3.0f);
69 drawTestCase(canvas, 1.0f);
72 // Non-uniform scaling test.
73 canvas->translate(0.0f, 100.0f);
75 canvas->scale(3.0f, 6.0f);
76 drawTestCase(canvas, 1.0f);
80 canvas->translate(0.0f, 80.0f);
82 canvas->scale(3.0f, 3.0f);
85 skew.setSkewX(SkScalarDiv(8.0f,
87 skew.setSkewY(SkScalarDiv(2.0f,
90 drawTestCase(canvas, 1.0f);
94 canvas->translate(0.0f, 80.0f);
97 perspective.setIdentity();
98 perspective.setPerspX(-SkScalarDiv(SK_Scalar1, 340.0f));
99 perspective.setSkewX(SkScalarDiv(8.0f,
101 perspective.setSkewY(SkScalarDiv(2.0f,
105 canvas->concat(perspective);
106 drawTestCase(canvas, 1.0f);
110 void drawTestCase(SkCanvas* canvas, SkScalar textScale) {
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);
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));
127 SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
129 paint.setStrokeWidth(0.0f);
130 paint.setStyle(SkPaint::kStroke_Style);
132 // Green box is the measured text bounds.
133 paint.setColor(SK_ColorGREEN);
134 canvas->drawRect(bounds, paint);
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);
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);
148 SkScalar widths[sizeof(kText) - 1];
149 paint.getTextWidths(kText, sizeof(kText) - 1, widths, NULL);
151 paint.setStrokeWidth(0.0f);
152 paint.setStyle(SkPaint::kStroke_Style);
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);
165 SkAutoTUnref<SkTypeface> fProp;
166 SkScalar fStrokeWidth;
167 SkPaint::Style fStrokeStyle;
169 typedef GM INHERITED;
172 //////////////////////////////////////////////////////////////////////////////
174 static GM* GlyphPosHairlineStrokeAndFillFactory(void*) {
175 return new GlyphPosGM(0.0f, SkPaint::kStrokeAndFill_Style);
177 static GM* GlyphPosStrokeAndFillFactory(void*) {
178 return new GlyphPosGM(1.2f, SkPaint::kStrokeAndFill_Style);
180 static GM* GlyphPosHairlineStrokeFactory(void*) {
181 return new GlyphPosGM(0.0f, SkPaint::kStroke_Style);
183 static GM* GlyphPosStrokeFactory(void*) {
184 return new GlyphPosGM(1.2f, SkPaint::kStroke_Style);
186 static GM* GlyphPosHairlineFillFactory(void*) {
187 return new GlyphPosGM(0.0f, SkPaint::kFill_Style);
189 static GM* GlyphPosFillFactory(void*) {
190 return new GlyphPosGM(1.2f, SkPaint::kFill_Style);
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);