• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontTypes.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTypeface.h"
18 #include "include/core/SkTypes.h"
19 #include "tools/ToolUtils.h"
20 
21 /* This test tries to define the effect of using hairline strokes on text.
22  * Provides non-hairline images for reference and consistency checks.
23  * glyph_pos_(h/n)_(s/f/b)
24  *   -> test hairline/non-hairline stroke/fill/stroke+fill.
25  */
26 constexpr SkScalar kTextHeight = 14.0f;
27 constexpr char kText[] = "Proportional Hamburgefons #% fi";
28 
29 static void drawTestCase(SkCanvas* canvas,
30                          SkScalar textScale,
31                          SkScalar strokeWidth,
32                          SkPaint::Style strokeStyle);
33 
draw_gm(SkCanvas * canvas,SkScalar strokeWidth,SkPaint::Style strokeStyle)34 static void draw_gm(SkCanvas* canvas,
35                     SkScalar strokeWidth,
36                     SkPaint::Style strokeStyle) {
37     // There's a black pixel at 40, 40 for reference.
38     canvas->drawPoint(40, 40, SkPaint());
39 
40     // Two reference images.
41     canvas->translate(50.0f, 50.0f);
42     drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
43 
44     canvas->translate(0.0f, 50.0f);
45     drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
46 
47     // Uniform scaling test.
48     canvas->translate(0.0f, 100.0f);
49     canvas->save();
50     canvas->scale(3.0f, 3.0f);
51     drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
52     canvas->restore();
53 
54     // Non-uniform scaling test.
55     canvas->translate(0.0f, 100.0f);
56     canvas->save();
57     canvas->scale(3.0f, 6.0f);
58     drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
59     canvas->restore();
60 
61     // Skew test.
62     canvas->translate(0.0f, 80.0f);
63     canvas->save();
64     canvas->scale(3.0f, 3.0f);
65     SkMatrix skew;
66     skew.setIdentity();
67     skew.setSkewX(8.0f / 25.0f);
68     skew.setSkewY(2.0f / 25.0f);
69     canvas->concat(skew);
70     drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
71     canvas->restore();
72 
73     // Perspective test.
74     canvas->translate(0.0f, 80.0f);
75     canvas->save();
76     SkMatrix perspective;
77     perspective.setIdentity();
78     perspective.setPerspX(-SkScalarInvert(340));
79     perspective.setSkewX(8.0f / 25.0f);
80     perspective.setSkewY(2.0f / 25.0f);
81 
82     canvas->concat(perspective);
83     drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
84     canvas->restore();
85 }
86 
drawTestCase(SkCanvas * canvas,SkScalar textScale,SkScalar strokeWidth,SkPaint::Style strokeStyle)87 static void drawTestCase(SkCanvas* canvas,
88                          SkScalar textScale,
89                          SkScalar strokeWidth,
90                          SkPaint::Style strokeStyle) {
91     SkPaint paint;
92     paint.setColor(SK_ColorBLACK);
93     paint.setAntiAlias(true);
94     paint.setStrokeWidth(strokeWidth);
95     paint.setStyle(strokeStyle);
96 
97     SkFont font(ToolUtils::create_portable_typeface(), kTextHeight * textScale);
98 
99     // This demonstrates that we can not measure the text if
100     // there's a device transform. The canvas total matrix will
101     // end up being a device transform.
102     bool drawRef = !(canvas->getTotalMatrix().getType() &
103                      ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
104 
105     SkRect bounds;
106     if (drawRef) {
107         SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
108                                             &bounds, &paint);
109 
110         paint.setStrokeWidth(0.0f);
111         paint.setStyle(SkPaint::kStroke_Style);
112 
113         // Green box is the measured text bounds.
114         paint.setColor(SK_ColorGREEN);
115         canvas->drawRect(bounds, paint);
116 
117         // Red line is the measured advance from the 0,0 of the text position.
118         paint.setColor(SK_ColorRED);
119         canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
120     }
121 
122     // Black text is the testcase, eg. the text.
123     paint.setColor(SK_ColorBLACK);
124     paint.setStrokeWidth(strokeWidth);
125     paint.setStyle(strokeStyle);
126     canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
127                            0.0f, 0.0f, font, paint);
128 
129     if (drawRef) {
130         const size_t len = sizeof(kText) - 1;
131         SkGlyphID glyphs[len];
132         const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
133         SkScalar widths[len]; // len is conservative. we really only need 'count'
134         font.getWidthsBounds(glyphs, count, widths, nullptr, &paint);
135 
136         paint.setStrokeWidth(0.0f);
137         paint.setStyle(SkPaint::kStroke_Style);
138 
139         // Magenta lines are the positions for the characters.
140         paint.setColor(SK_ColorMAGENTA);
141         SkScalar w = bounds.x();
142         for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
143             canvas->drawLine(w, 0.0f, w, 5.0f, paint);
144             w += widths[i];
145         }
146     }
147 }
148 
149 DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
150     draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
151 }
152 DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
153     draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
154 }
155 DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
156     draw_gm(c, 0.0f, SkPaint::kStroke_Style);
157 }
158 DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
159     draw_gm(c, 1.2f, SkPaint::kStroke_Style);
160 }
161 DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
162     draw_gm(c, 0.0f, SkPaint::kFill_Style);
163 }
164 DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
165     draw_gm(c, 1.2f, SkPaint::kFill_Style);
166 }
167