blob: 10ab21383ca157afadc1b9c54623ededb253322c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "tools/ToolUtils.h"
rmistryc4b84ae2014-06-23 06:59:15 -070020
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 */
mtkleindbfd7ab2016-09-01 11:24:54 -070026constexpr SkScalar kTextHeight = 14.0f;
27constexpr char kText[] = "Proportional Hamburgefons #% fi";
rmistryc4b84ae2014-06-23 06:59:15 -070028
halcanary2a243382015-09-09 08:16:41 -070029static void drawTestCase(SkCanvas* canvas,
30 SkScalar textScale,
31 SkScalar strokeWidth,
32 SkPaint::Style strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070033
halcanary2a243382015-09-09 08:16:41 -070034static void draw_gm(SkCanvas* canvas,
35 SkScalar strokeWidth,
36 SkPaint::Style strokeStyle) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050037 // There's a black pixel at 40, 40 for reference.
Mike Reed3661bc92017-02-22 13:21:42 -050038 canvas->drawPoint(40, 40, SkPaint());
rmistryc4b84ae2014-06-23 06:59:15 -070039
Mike Reed3661bc92017-02-22 13:21:42 -050040 // Two reference images.
41 canvas->translate(50.0f, 50.0f);
42 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070043
Mike Reed3661bc92017-02-22 13:21:42 -050044 canvas->translate(0.0f, 50.0f);
45 drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070046
Mike Reed3661bc92017-02-22 13:21:42 -050047 // 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();
rmistryc4b84ae2014-06-23 06:59:15 -070053
Mike Reed3661bc92017-02-22 13:21:42 -050054 // 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();
rmistryc4b84ae2014-06-23 06:59:15 -070060
Mike Reed3661bc92017-02-22 13:21:42 -050061 // 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();
rmistryc4b84ae2014-06-23 06:59:15 -070072
Mike Reed3661bc92017-02-22 13:21:42 -050073 // 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);
rmistryc4b84ae2014-06-23 06:59:15 -070081
Mike Reed3661bc92017-02-22 13:21:42 -050082 canvas->concat(perspective);
83 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
84 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070085}
rmistryc4b84ae2014-06-23 06:59:15 -070086
halcanary2a243382015-09-09 08:16:41 -070087static void drawTestCase(SkCanvas* canvas,
88 SkScalar textScale,
89 SkScalar strokeWidth,
90 SkPaint::Style strokeStyle) {
Mike Reed6cd43b42018-12-16 15:57:29 -050091 SkPaint paint;
92 paint.setColor(SK_ColorBLACK);
93 paint.setAntiAlias(true);
94 paint.setStrokeWidth(strokeWidth);
95 paint.setStyle(strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070096
Mike Kleinea3f0142019-03-20 11:12:10 -050097 SkFont font(ToolUtils::create_portable_typeface(), kTextHeight * textScale);
rmistryc4b84ae2014-06-23 06:59:15 -070098
Mike Reed6cd43b42018-12-16 15:57:29 -050099 // 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));
rmistryc4b84ae2014-06-23 06:59:15 -0700104
Mike Reed6cd43b42018-12-16 15:57:29 -0500105 SkRect bounds;
106 if (drawRef) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400107 SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
Mike Reed6cd43b42018-12-16 15:57:29 -0500108 &bounds, &paint);
rmistryc4b84ae2014-06-23 06:59:15 -0700109
Mike Reed6cd43b42018-12-16 15:57:29 -0500110 paint.setStrokeWidth(0.0f);
111 paint.setStyle(SkPaint::kStroke_Style);
rmistryc4b84ae2014-06-23 06:59:15 -0700112
Mike Reed6cd43b42018-12-16 15:57:29 -0500113 // 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);
Ben Wagner51e15a62019-05-07 15:38:46 -0400126 canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
127 0.0f, 0.0f, font, paint);
Mike Reed6cd43b42018-12-16 15:57:29 -0500128
129 if (drawRef) {
130 const size_t len = sizeof(kText) - 1;
131 SkGlyphID glyphs[len];
Ben Wagner51e15a62019-05-07 15:38:46 -0400132 const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
Mike Reed6cd43b42018-12-16 15:57:29 -0500133 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];
rmistryc4b84ae2014-06-23 06:59:15 -0700145 }
Mike Reed6cd43b42018-12-16 15:57:29 -0500146 }
rmistryc4b84ae2014-06-23 06:59:15 -0700147}
148
halcanary2a243382015-09-09 08:16:41 -0700149DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
150 draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
151}
152DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
153 draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
154}
155DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
156 draw_gm(c, 0.0f, SkPaint::kStroke_Style);
157}
158DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
159 draw_gm(c, 1.2f, SkPaint::kStroke_Style);
160}
161DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
162 draw_gm(c, 0.0f, SkPaint::kFill_Style);
163}
164DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
165 draw_gm(c, 1.2f, SkPaint::kFill_Style);
rmistryc4b84ae2014-06-23 06:59:15 -0700166}