blob: cfc318d4b462fa3bd88436cb2011b51b486328b4 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
rmistryc4b84ae2014-06-23 06:59:15 -070010#include "SkCanvas.h"
11#include "SkTypeface.h"
12
13/* This test tries to define the effect of using hairline strokes on text.
14 * Provides non-hairline images for reference and consistency checks.
15 * glyph_pos_(h/n)_(s/f/b)
16 * -> test hairline/non-hairline stroke/fill/stroke+fill.
17 */
mtkleindbfd7ab2016-09-01 11:24:54 -070018constexpr SkScalar kTextHeight = 14.0f;
19constexpr char kText[] = "Proportional Hamburgefons #% fi";
rmistryc4b84ae2014-06-23 06:59:15 -070020
halcanary2a243382015-09-09 08:16:41 -070021static void drawTestCase(SkCanvas* canvas,
22 SkScalar textScale,
23 SkScalar strokeWidth,
24 SkPaint::Style strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070025
halcanary2a243382015-09-09 08:16:41 -070026static void draw_gm(SkCanvas* canvas,
27 SkScalar strokeWidth,
28 SkPaint::Style strokeStyle) {
rmistryc4b84ae2014-06-23 06:59:15 -070029 // There's a black pixel at 40, 40 for reference.
Mike Reed3661bc92017-02-22 13:21:42 -050030 canvas->drawPoint(40, 40, SkPaint());
rmistryc4b84ae2014-06-23 06:59:15 -070031
Mike Reed3661bc92017-02-22 13:21:42 -050032 // Two reference images.
33 canvas->translate(50.0f, 50.0f);
34 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070035
Mike Reed3661bc92017-02-22 13:21:42 -050036 canvas->translate(0.0f, 50.0f);
37 drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070038
Mike Reed3661bc92017-02-22 13:21:42 -050039 // Uniform scaling test.
40 canvas->translate(0.0f, 100.0f);
41 canvas->save();
42 canvas->scale(3.0f, 3.0f);
43 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
44 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070045
Mike Reed3661bc92017-02-22 13:21:42 -050046 // Non-uniform scaling test.
47 canvas->translate(0.0f, 100.0f);
48 canvas->save();
49 canvas->scale(3.0f, 6.0f);
50 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
51 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070052
Mike Reed3661bc92017-02-22 13:21:42 -050053 // Skew test.
54 canvas->translate(0.0f, 80.0f);
55 canvas->save();
56 canvas->scale(3.0f, 3.0f);
57 SkMatrix skew;
58 skew.setIdentity();
59 skew.setSkewX(8.0f / 25.0f);
60 skew.setSkewY(2.0f / 25.0f);
61 canvas->concat(skew);
62 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
63 canvas->restore();
rmistryc4b84ae2014-06-23 06:59:15 -070064
Mike Reed3661bc92017-02-22 13:21:42 -050065 // Perspective test.
66 canvas->translate(0.0f, 80.0f);
67 canvas->save();
68 SkMatrix perspective;
69 perspective.setIdentity();
70 perspective.setPerspX(-SkScalarInvert(340));
71 perspective.setSkewX(8.0f / 25.0f);
72 perspective.setSkewY(2.0f / 25.0f);
rmistryc4b84ae2014-06-23 06:59:15 -070073
Mike Reed3661bc92017-02-22 13:21:42 -050074 canvas->concat(perspective);
75 drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle);
76 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -070077}
rmistryc4b84ae2014-06-23 06:59:15 -070078
halcanary2a243382015-09-09 08:16:41 -070079static void drawTestCase(SkCanvas* canvas,
80 SkScalar textScale,
81 SkScalar strokeWidth,
82 SkPaint::Style strokeStyle) {
rmistryc4b84ae2014-06-23 06:59:15 -070083 SkPaint paint;
84 paint.setColor(SK_ColorBLACK);
85 paint.setAntiAlias(true);
86 paint.setTextSize(kTextHeight * textScale);
caryclark1818acb2015-07-24 12:09:25 -070087 sk_tool_utils::set_portable_typeface(&paint);
halcanary2a243382015-09-09 08:16:41 -070088 paint.setStrokeWidth(strokeWidth);
89 paint.setStyle(strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -070090
halcanary2a243382015-09-09 08:16:41 -070091 // This demonstrates that we can not measure the text if
92 // there's a device transform. The canvas total matrix will
93 // end up being a device transform.
rmistryc4b84ae2014-06-23 06:59:15 -070094 bool drawRef = !(canvas->getTotalMatrix().getType() &
95 ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
96
97 SkRect bounds;
98 if (drawRef) {
99 SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
100
101 paint.setStrokeWidth(0.0f);
102 paint.setStyle(SkPaint::kStroke_Style);
103
104 // Green box is the measured text bounds.
105 paint.setColor(SK_ColorGREEN);
106 canvas->drawRect(bounds, paint);
107
108 // Red line is the measured advance from the 0,0 of the text position.
109 paint.setColor(SK_ColorRED);
110 canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
111 }
112
113 // Black text is the testcase, eg. the text.
114 paint.setColor(SK_ColorBLACK);
halcanary2a243382015-09-09 08:16:41 -0700115 paint.setStrokeWidth(strokeWidth);
116 paint.setStyle(strokeStyle);
rmistryc4b84ae2014-06-23 06:59:15 -0700117 canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint);
118
119 if (drawRef) {
120 SkScalar widths[sizeof(kText) - 1];
halcanary96fcdcc2015-08-27 07:41:13 -0700121 paint.getTextWidths(kText, sizeof(kText) - 1, widths, nullptr);
rmistryc4b84ae2014-06-23 06:59:15 -0700122
123 paint.setStrokeWidth(0.0f);
124 paint.setStyle(SkPaint::kStroke_Style);
125
126 // Magenta lines are the positions for the characters.
127 paint.setColor(SK_ColorMAGENTA);
128 SkScalar w = bounds.x();
129 for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
130 canvas->drawLine(w, 0.0f, w, 5.0f, paint);
131 w += widths[i];
132 }
133 }
rmistryc4b84ae2014-06-23 06:59:15 -0700134}
135
halcanary2a243382015-09-09 08:16:41 -0700136DEF_SIMPLE_GM(glyph_pos_h_b, c, 800, 600) {
137 draw_gm(c, 0.0f, SkPaint::kStrokeAndFill_Style);
138}
139DEF_SIMPLE_GM(glyph_pos_n_b, c, 800, 600) {
140 draw_gm(c, 1.2f, SkPaint::kStrokeAndFill_Style);
141}
142DEF_SIMPLE_GM(glyph_pos_h_s, c, 800, 600) {
143 draw_gm(c, 0.0f, SkPaint::kStroke_Style);
144}
145DEF_SIMPLE_GM(glyph_pos_n_s, c, 800, 600) {
146 draw_gm(c, 1.2f, SkPaint::kStroke_Style);
147}
148DEF_SIMPLE_GM(glyph_pos_h_f, c, 800, 600) {
149 draw_gm(c, 0.0f, SkPaint::kFill_Style);
150}
151DEF_SIMPLE_GM(glyph_pos_n_f, c, 800, 600) {
152 draw_gm(c, 1.2f, SkPaint::kFill_Style);
rmistryc4b84ae2014-06-23 06:59:15 -0700153}