blob: 09303251c2903d0d28b6bd6653aeac445063f762 [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"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040020#include "tools/fonts/FontToolUtils.h"
rmistryc4b84ae2014-06-23 06:59:15 -070021
22/* This test tries to define the effect of using hairline strokes on text.
23 * Provides non-hairline images for reference and consistency checks.
24 * glyph_pos_(h/n)_(s/f/b)
25 * -> test hairline/non-hairline stroke/fill/stroke+fill.
26 */
mtkleindbfd7ab2016-09-01 11:24:54 -070027constexpr SkScalar kTextHeight = 14.0f;
28constexpr char kText[] = "Proportional Hamburgefons #% fi";
rmistryc4b84ae2014-06-23 06:59:15 -070029
halcanary2a243382015-09-09 08:16:41 -070030static void drawTestCase(SkCanvas* canvas,
31 SkScalar textScale,
32 SkScalar strokeWidth,
33 SkPaint::Style strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070034
halcanary2a243382015-09-09 08:16:41 -070035static void draw_gm(SkCanvas* canvas,
36 SkScalar strokeWidth,
37 SkPaint::Style strokeStyle) {
Jim Van Verth54d9c882018-02-08 16:14:48 -050038 // There's a black pixel at 40, 40 for reference.
Mike Reed3661bc92017-02-22 13:21:42 -050039 canvas->drawPoint(40, 40, SkPaint());
rmistryc4b84ae2014-06-23 06:59:15 -070040
Mike Reed3661bc92017-02-22 13:21:42 -050041 // Two reference images.
42 canvas->translate(50.0f, 50.0f);
43 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070044
Mike Reed3661bc92017-02-22 13:21:42 -050045 canvas->translate(0.0f, 50.0f);
46 drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070047
Mike Reed3661bc92017-02-22 13:21:42 -050048 // Uniform scaling test.
49 canvas->translate(0.0f, 100.0f);
50 canvas->save();
51 canvas->scale(3.0f, 3.0f);
52 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
53 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070054
Mike Reed3661bc92017-02-22 13:21:42 -050055 // Non-uniform scaling test.
56 canvas->translate(0.0f, 100.0f);
57 canvas->save();
58 canvas->scale(3.0f, 6.0f);
59 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
60 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070061
Mike Reed3661bc92017-02-22 13:21:42 -050062 // Skew test.
63 canvas->translate(0.0f, 80.0f);
64 canvas->save();
65 canvas->scale(3.0f, 3.0f);
66 SkMatrix skew;
67 skew.setIdentity();
68 skew.setSkewX(8.0f / 25.0f);
69 skew.setSkewY(2.0f / 25.0f);
70 canvas->concat(skew);
71 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
72 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070073
Mike Reed3661bc92017-02-22 13:21:42 -050074 // Perspective test.
75 canvas->translate(0.0f, 80.0f);
76 canvas->save();
77 SkMatrix perspective;
78 perspective.setIdentity();
79 perspective.setPerspX(-SkScalarInvert(340));
80 perspective.setSkewX(8.0f / 25.0f);
81 perspective.setSkewY(2.0f / 25.0f);
rmistryc4b84ae2014-06-23 06:59:15 -070082
Mike Reed3661bc92017-02-22 13:21:42 -050083 canvas->concat(perspective);
84 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
85 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070086}
rmistryc4b84ae2014-06-23 06:59:15 -070087
halcanary2a243382015-09-09 08:16:41 -070088static void drawTestCase(SkCanvas* canvas,
89 SkScalar textScale,
90 SkScalar strokeWidth,
91 SkPaint::Style strokeStyle) {
Mike Reed6cd43b42018-12-16 15:57:29 -050092 SkPaint paint;
93 paint.setColor(SK_ColorBLACK);
94 paint.setAntiAlias(true);
95 paint.setStrokeWidth(strokeWidth);
96 paint.setStyle(strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070097
Kevin Lubicke836c3a2023-10-20 06:55:35 -040098 SkFont font(ToolUtils::DefaultPortableTypeface(), kTextHeight * textScale);
rmistryc4b84ae2014-06-23 06:59:15 -070099
Mike Reed6cd43b42018-12-16 15:57:29 -0500100 // This demonstrates that we can not measure the text if
101 // there's a device transform. The canvas total matrix will
102 // end up being a device transform.
Mike Reed1a4140e2020-12-03 11:21:31 -0500103 bool drawRef = !(canvas->getLocalToDeviceAs3x3().getType() &
Mike Reed6cd43b42018-12-16 15:57:29 -0500104 ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
rmistryc4b84ae2014-06-23 06:59:15 -0700105
Mike Reed6cd43b42018-12-16 15:57:29 -0500106 SkRect bounds;
107 if (drawRef) {
Ben Wagner51e15a62019-05-07 15:38:46 -0400108 SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
Mike Reed6cd43b42018-12-16 15:57:29 -0500109 &bounds, &paint);
rmistryc4b84ae2014-06-23 06:59:15 -0700110
Mike Reed6cd43b42018-12-16 15:57:29 -0500111 paint.setStrokeWidth(0.0f);
112 paint.setStyle(SkPaint::kStroke_Style);
rmistryc4b84ae2014-06-23 06:59:15 -0700113
Mike Reed6cd43b42018-12-16 15:57:29 -0500114 // Green box is the measured text bounds.
115 paint.setColor(SK_ColorGREEN);
116 canvas->drawRect(bounds, paint);
117
118 // Red line is the measured advance from the 0,0 of the text position.
119 paint.setColor(SK_ColorRED);
120 canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
121 }
122
123 // Black text is the testcase, eg. the text.
124 paint.setColor(SK_ColorBLACK);
125 paint.setStrokeWidth(strokeWidth);
126 paint.setStyle(strokeStyle);
Ben Wagner51e15a62019-05-07 15:38:46 -0400127 canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8,
128 0.0f, 0.0f, font, paint);
Mike Reed6cd43b42018-12-16 15:57:29 -0500129
130 if (drawRef) {
131 const size_t len = sizeof(kText) - 1;
132 SkGlyphID glyphs[len];
Ben Wagner51e15a62019-05-07 15:38:46 -0400133 const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len);
Mike Reed6cd43b42018-12-16 15:57:29 -0500134 SkScalar widths[len]; // len is conservative. we really only need 'count'
135 font.getWidthsBounds(glyphs, count, widths, nullptr, &paint);
136
137 paint.setStrokeWidth(0.0f);
138 paint.setStyle(SkPaint::kStroke_Style);
139
140 // Magenta lines are the positions for the characters.
141 paint.setColor(SK_ColorMAGENTA);
Ben Wagner53375a72021-02-09 15:23:18 -0500142 SkScalar w = 0;
Mike Reed6cd43b42018-12-16 15:57:29 -0500143 for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
144 canvas->drawLine(w, 0.0f, w, 5.0f, paint);
145 w += widths[i];
rmistryc4b84ae2014-06-23 06:59:15 -0700146 }
Mike Reed6cd43b42018-12-16 15:57:29 -0500147 }
rmistryc4b84ae2014-06-23 06:59:15 -0700148}
149
halcanary2a243382015-09-09 08:16:41 -0700150DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
151 draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
152}
153DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
154 draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
155}
156DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
157 draw_gm(c, 0.0f, SkPaint::kStroke_Style);
158}
159DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
160 draw_gm(c, 1.2f, SkPaint::kStroke_Style);
161}
162DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
163 draw_gm(c, 0.0f, SkPaint::kFill_Style);
164}
165DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
166 draw_gm(c, 1.2f, SkPaint::kFill_Style);
rmistryc4b84ae2014-06-23 06:59:15 -0700167}