rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkFont.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "tools/ToolUtils.h" |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 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 | */ |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 26 | constexpr SkScalar kTextHeight = 14.0f; |
| 27 | constexpr char kText[] = "Proportional Hamburgefons #% fi"; |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 28 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 29 | static void drawTestCase(SkCanvas* canvas, |
| 30 | SkScalar textScale, |
| 31 | SkScalar strokeWidth, |
| 32 | SkPaint::Style strokeStyle); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 33 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 34 | static void draw_gm(SkCanvas* canvas, |
| 35 | SkScalar strokeWidth, |
| 36 | SkPaint::Style strokeStyle) { |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 37 | // There's a black pixel at 40, 40 for reference. |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 38 | canvas->drawPoint(40, 40, SkPaint()); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 39 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 40 | // Two reference images. |
| 41 | canvas->translate(50.0f, 50.0f); |
| 42 | drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 43 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 44 | canvas->translate(0.0f, 50.0f); |
| 45 | drawTestCase(canvas, 3.0f, strokeWidth, strokeStyle); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 46 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 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(); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 53 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 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(); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 60 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 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(); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 72 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 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); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 81 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 82 | canvas->concat(perspective); |
| 83 | drawTestCase(canvas, 1.0f, strokeWidth, strokeStyle); |
| 84 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 85 | } |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 86 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 87 | static void drawTestCase(SkCanvas* canvas, |
| 88 | SkScalar textScale, |
| 89 | SkScalar strokeWidth, |
| 90 | SkPaint::Style strokeStyle) { |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 91 | SkPaint paint; |
| 92 | paint.setColor(SK_ColorBLACK); |
| 93 | paint.setAntiAlias(true); |
| 94 | paint.setStrokeWidth(strokeWidth); |
| 95 | paint.setStyle(strokeStyle); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 96 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 97 | SkFont font(ToolUtils::create_portable_typeface(), kTextHeight * textScale); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 98 | |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 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. |
Mike Reed | 1a4140e | 2020-12-03 11:21:31 -0500 | [diff] [blame] | 102 | bool drawRef = !(canvas->getLocalToDeviceAs3x3().getType() & |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 103 | ~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask)); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 104 | |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 105 | SkRect bounds; |
| 106 | if (drawRef) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 107 | SkScalar advance = font.measureText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8, |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 108 | &bounds, &paint); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 109 | |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 110 | paint.setStrokeWidth(0.0f); |
| 111 | paint.setStyle(SkPaint::kStroke_Style); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 112 | |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 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); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 126 | canvas->drawSimpleText(kText, sizeof(kText) - 1, SkTextEncoding::kUTF8, |
| 127 | 0.0f, 0.0f, font, paint); |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 128 | |
| 129 | if (drawRef) { |
| 130 | const size_t len = sizeof(kText) - 1; |
| 131 | SkGlyphID glyphs[len]; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 132 | const int count = font.textToGlyphs(kText, len, SkTextEncoding::kUTF8, glyphs, len); |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 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); |
Ben Wagner | 53375a7 | 2021-02-09 15:23:18 -0500 | [diff] [blame] | 141 | SkScalar w = 0; |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 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]; |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 145 | } |
Mike Reed | 6cd43b4 | 2018-12-16 15:57:29 -0500 | [diff] [blame] | 146 | } |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 147 | } |
| 148 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 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); |
rmistry | c4b84ae | 2014-06-23 06:59:15 -0700 | [diff] [blame] | 166 | } |