blob: c805abafd007da08eac34580ccca400e16fd2619 [file] [log] [blame]
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +00001/*
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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
Ben Wagnerb3275e12022-03-16 23:39:21 -040013#include "include/core/SkFontArguments.h"
14#include "include/core/SkFontMgr.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkFontTypes.h"
16#include "include/core/SkImageInfo.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPathEffect.h"
19#include "include/core/SkPoint.h"
20#include "include/core/SkScalar.h"
Kevin Lubickbe579dc2023-10-05 11:11:20 -040021#include "include/core/SkStream.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypeface.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkDashPathEffect.h"
Ben Wagnerd38f00a2020-01-27 17:43:41 -050026#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040028#include "tools/fonts/FontToolUtils.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000029
reed@google.com0d30c512014-03-14 14:02:58 +000030static void test_nulldev(SkCanvas* canvas) {
31 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070032 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000033 // notice: no pixels mom! be sure we don't crash
34 // https://code.google.com/p/chromium/issues/detail?id=352616
35 SkCanvas c(bm);
36
37 SkBitmap src;
38 src.allocN32Pixels(10, 10);
39 src.eraseColor(SK_ColorRED);
40
41 // ensure we don't crash
42 c.writePixels(src, 0, 0);
43}
44
Mike Reed088b74e2018-12-24 14:52:46 -050045static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
46 SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000047 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000048 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000049
commit-bot@chromium.org74266812014-05-22 17:41:41 +000050 if (strokeWidth > 0) {
51 p.setStyle(SkPaint::kFill_Style);
Ben Wagner51e15a62019-05-07 15:38:46 -040052 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050053 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000054 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000055
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000056 p.setColor(SK_ColorRED);
57 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000058 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000059
Ben Wagner51e15a62019-05-07 15:38:46 -040060 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050061 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000062}
63
Mike Reed088b74e2018-12-24 14:52:46 -050064static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
commit-bot@chromium.org74266812014-05-22 17:41:41 +000065 SkAutoCanvasRestore acr(canvas, true);
66
Mike Reed088b74e2018-12-24 14:52:46 -050067 draw_text_stroked(canvas, paint, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000068
69 canvas->translate(200, 0);
Mike Reed088b74e2018-12-24 14:52:46 -050070 draw_text_stroked(canvas, paint, font, 0);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000071
72 const SkScalar intervals[] = { 20, 10, 5, 10 };
73 const SkScalar phase = 0;
74
75 canvas->translate(200, 0);
76 SkPaint p(paint);
Herb Derbyc37b3862022-06-21 09:49:17 -040077 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), phase));
Mike Reed088b74e2018-12-24 14:52:46 -050078 draw_text_stroked(canvas, p, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000079}
80
halcanary2a243382015-09-09 08:16:41 -070081namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000082 enum {
83 kBelowThreshold_TextSize = 255,
84 kAboveThreshold_TextSize = 257
85 };
John Stilesa6841be2020-08-06 14:11:56 -040086} // namespace
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000087
halcanary2a243382015-09-09 08:16:41 -070088DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
Mike Reed088b74e2018-12-24 14:52:46 -050089 if (true) { test_nulldev(canvas); }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000090
Mike Reed088b74e2018-12-24 14:52:46 -050091 SkPaint paint;
92 paint.setAntiAlias(true);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000093
Kevin Lubicke836c3a2023-10-20 06:55:35 -040094 SkFont font(ToolUtils::DefaultPortableTypeface(), kBelowThreshold_TextSize);
Mike Reed088b74e2018-12-24 14:52:46 -050095 draw_text_set(canvas, paint, font);
96
97 canvas->translate(600, 0);
98 font.setSize(kAboveThreshold_TextSize);
99 draw_text_set(canvas, paint, font);
halcanary2a243382015-09-09 08:16:41 -0700100}
Ben Wagnerd38f00a2020-01-27 17:43:41 -0500101
Ben Wagnerb3275e12022-03-16 23:39:21 -0400102DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 420) {
Kevin Lubick1e971192023-11-10 16:14:44 -0500103 sk_sp<SkTypeface> ttf = ToolUtils::CreateTypefaceFromResource("fonts/Stroking.ttf");
104 sk_sp<SkTypeface> otf = ToolUtils::CreateTypefaceFromResource("fonts/Stroking.otf");
Ben Wagnerb3275e12022-03-16 23:39:21 -0400105
106 sk_sp<SkTypeface> overlap = []() -> sk_sp<SkTypeface>{
107 std::unique_ptr<SkStreamAsset> variableStream(GetResourceAsStream("fonts/Variable.ttf"));
108 if (!variableStream) {
109 return nullptr;
110 }
111 const SkFontArguments::VariationPosition::Coordinate position[] = {
112 { SkSetFourByteTag('w','g','h','t'), 721.0f },
113 };
114 SkFontArguments params;
Herb Derbyc37b3862022-06-21 09:49:17 -0400115 params.setVariationDesignPosition({position, std::size(position)});
Kevin Lubick1e971192023-11-10 16:14:44 -0500116 return ToolUtils::TestFontMgr()->makeFromStream(std::move(variableStream), params);
Ben Wagnerb3275e12022-03-16 23:39:21 -0400117 }();
118
119 if (!ttf && !otf && !overlap) {
Ben Wagnerd38f00a2020-01-27 17:43:41 -0500120 msg->append("No support for ttf or otf.");
121 return skiagm::DrawResult::kSkip;
122 }
123
124 SkPaint p;
125 p.setAntiAlias(true);
126 p.setStyle(SkPaint::kStroke_Style);
127 p.setStrokeWidth(10);
128 p.setStrokeCap(SkPaint::kRound_Cap);
129 p.setStrokeJoin(SkPaint::kRound_Join);
130 p.setARGB(0xff, 0xbb, 0x00, 0x00);
131
132 if (ttf) {
133 /* Stroking.ttf is structured like:
134 nothing U+25CB ○ (nothing inside)
135 something U+25C9 ◉ (a tiny thing inside)
136 - off (point off / empty quad with implicit end) (before U+207B ⁻ / after U+208B ₋)
137 + on (point on / empty line) (before U+207A ⁺ / after U+208A ₊)
138 0 off off (two implicit quads) (before U+2070 ⁰ / after U+2080 ₀)
139 1 off on (quad with implicit close around) (before U+00B9 ¹ / after U+2081 ₁)
140 2 on off (quad with implicit close) (before U+00B2 ² / after U+2082 ₂)
141 3 on on (empty line) (before U+00B3 ³ / after U+2083 ₃)
142 */
143 SkFont font(ttf, 100);
144 canvas->drawString("○◉ ⁻₋⁺₊", 10, 100, font, p);
145 canvas->drawString("⁰₀¹₁²₂³₃", 10, 200, font, p);
146 }
147
148 if (otf) {
149 /* Stroking.otf is structured like:
150 nothing U+25CB ○
151 something U+25C9 ◉
152 0 moveto, moveto (before U+2070 ⁰) (nothing there, FreeType ignores these)
153 1 moveto, empty line, moveto (before U+00B9 ¹) (degenerate lineto)
154 3 moveto, empty cubic, moveto (before U+00B3 ³) (degenerate cubicto)
155 f moveto, empty flex, moveto (before U+1DA0 ᶠ) (degenerate flex)
156 */
157 SkFont font(otf, 100);
158 canvas->drawString("○◉ ⁰¹³ᶠ", 10, 300, font, p);
159 }
160
Ben Wagnerb3275e12022-03-16 23:39:21 -0400161 if (overlap) {
162 /* Variable.ttf is structured like:
163 U+74 t (glyf outline has overlap flag)
164 U+167 ŧ (glyf outline does not have overlap flag)
165 */
166 SkFont font(overlap, 100);
167 p.setStrokeWidth(1);
168 canvas->drawString("tŧ", 10, 400, font, p);
169 }
170
Ben Wagnerd38f00a2020-01-27 17:43:41 -0500171 return skiagm::DrawResult::kOk;
172}