commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [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" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkFontTypes.h" |
| 14 | #include "include/core/SkImageInfo.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkPathEffect.h" |
| 17 | #include "include/core/SkPoint.h" |
| 18 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkTextBlob.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/core/SkTypeface.h" |
| 21 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/effects/SkDashPathEffect.h" |
Ben Wagner | d38f00a | 2020-01-27 17:43:41 -0500 | [diff] [blame] | 23 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 25 | |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 26 | static void test_nulldev(SkCanvas* canvas) { |
| 27 | SkBitmap bm; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 28 | bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 29 | // notice: no pixels mom! be sure we don't crash |
| 30 | // https://code.google.com/p/chromium/issues/detail?id=352616 |
| 31 | SkCanvas c(bm); |
| 32 | |
| 33 | SkBitmap src; |
| 34 | src.allocN32Pixels(10, 10); |
| 35 | src.eraseColor(SK_ColorRED); |
| 36 | |
| 37 | // ensure we don't crash |
| 38 | c.writePixels(src, 0, 0); |
| 39 | } |
| 40 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 41 | static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font, |
| 42 | SkScalar strokeWidth) { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 43 | SkPaint p(paint); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 44 | SkPoint loc = { 20, 435 }; |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 46 | if (strokeWidth > 0) { |
| 47 | p.setStyle(SkPaint::kFill_Style); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 48 | canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 49 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 50 | } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 52 | p.setColor(SK_ColorRED); |
| 53 | p.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 54 | p.setStrokeWidth(strokeWidth); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 55 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 56 | canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 57 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 60 | static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) { |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 61 | SkAutoCanvasRestore acr(canvas, true); |
| 62 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 63 | draw_text_stroked(canvas, paint, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 64 | |
| 65 | canvas->translate(200, 0); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 66 | draw_text_stroked(canvas, paint, font, 0); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 67 | |
| 68 | const SkScalar intervals[] = { 20, 10, 5, 10 }; |
| 69 | const SkScalar phase = 0; |
| 70 | |
| 71 | canvas->translate(200, 0); |
| 72 | SkPaint p(paint); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 73 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 74 | draw_text_stroked(canvas, p, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 75 | } |
| 76 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 77 | namespace { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 78 | enum { |
| 79 | kBelowThreshold_TextSize = 255, |
| 80 | kAboveThreshold_TextSize = 257 |
| 81 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 82 | } // namespace |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 83 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 84 | DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 85 | if (true) { test_nulldev(canvas); } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 86 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 87 | SkPaint paint; |
| 88 | paint.setAntiAlias(true); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 89 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 90 | SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 91 | draw_text_set(canvas, paint, font); |
| 92 | |
| 93 | canvas->translate(600, 0); |
| 94 | font.setSize(kAboveThreshold_TextSize); |
| 95 | draw_text_set(canvas, paint, font); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 96 | } |
Ben Wagner | d38f00a | 2020-01-27 17:43:41 -0500 | [diff] [blame] | 97 | |
| 98 | DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 320) { |
| 99 | sk_sp<SkTypeface> ttf = MakeResourceAsTypeface("fonts/Stroking.ttf"); |
| 100 | sk_sp<SkTypeface> otf = MakeResourceAsTypeface("fonts/Stroking.otf"); |
| 101 | if (!ttf && !otf) { |
| 102 | msg->append("No support for ttf or otf."); |
| 103 | return skiagm::DrawResult::kSkip; |
| 104 | } |
| 105 | |
| 106 | SkPaint p; |
| 107 | p.setAntiAlias(true); |
| 108 | p.setStyle(SkPaint::kStroke_Style); |
| 109 | p.setStrokeWidth(10); |
| 110 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 111 | p.setStrokeJoin(SkPaint::kRound_Join); |
| 112 | p.setARGB(0xff, 0xbb, 0x00, 0x00); |
| 113 | |
| 114 | if (ttf) { |
| 115 | /* Stroking.ttf is structured like: |
| 116 | nothing U+25CB ○ (nothing inside) |
| 117 | something U+25C9 ◉ (a tiny thing inside) |
| 118 | - off (point off / empty quad with implicit end) (before U+207B ⁻ / after U+208B ₋) |
| 119 | + on (point on / empty line) (before U+207A ⁺ / after U+208A ₊) |
| 120 | 0 off off (two implicit quads) (before U+2070 ⁰ / after U+2080 ₀) |
| 121 | 1 off on (quad with implicit close around) (before U+00B9 ¹ / after U+2081 ₁) |
| 122 | 2 on off (quad with implicit close) (before U+00B2 ² / after U+2082 ₂) |
| 123 | 3 on on (empty line) (before U+00B3 ³ / after U+2083 ₃) |
| 124 | */ |
| 125 | SkFont font(ttf, 100); |
| 126 | canvas->drawString("○◉ ⁻₋⁺₊", 10, 100, font, p); |
| 127 | canvas->drawString("⁰₀¹₁²₂³₃", 10, 200, font, p); |
| 128 | } |
| 129 | |
| 130 | if (otf) { |
| 131 | /* Stroking.otf is structured like: |
| 132 | nothing U+25CB ○ |
| 133 | something U+25C9 ◉ |
| 134 | 0 moveto, moveto (before U+2070 ⁰) (nothing there, FreeType ignores these) |
| 135 | 1 moveto, empty line, moveto (before U+00B9 ¹) (degenerate lineto) |
| 136 | 3 moveto, empty cubic, moveto (before U+00B3 ³) (degenerate cubicto) |
| 137 | f moveto, empty flex, moveto (before U+1DA0 ᶠ) (degenerate flex) |
| 138 | */ |
| 139 | SkFont font(otf, 100); |
| 140 | canvas->drawString("○◉ ⁰¹³ᶠ", 10, 300, font, p); |
| 141 | } |
| 142 | |
| 143 | return skiagm::DrawResult::kOk; |
| 144 | } |