blob: 4c2baa11f46760f678d2e8d4fbbc8a437b73de79 [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"
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 Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkDashPathEffect.h"
Ben Wagnerd38f00a2020-01-27 17:43:41 -050023#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000025
reed@google.com0d30c512014-03-14 14:02:58 +000026static void test_nulldev(SkCanvas* canvas) {
27 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070028 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000029 // 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 Reed088b74e2018-12-24 14:52:46 -050041static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
42 SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000043 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000044 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000045
commit-bot@chromium.org74266812014-05-22 17:41:41 +000046 if (strokeWidth > 0) {
47 p.setStyle(SkPaint::kFill_Style);
Ben Wagner51e15a62019-05-07 15:38:46 -040048 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050049 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000050 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000051
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000052 p.setColor(SK_ColorRED);
53 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000054 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000055
Ben Wagner51e15a62019-05-07 15:38:46 -040056 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050057 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000058}
59
Mike Reed088b74e2018-12-24 14:52:46 -050060static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
commit-bot@chromium.org74266812014-05-22 17:41:41 +000061 SkAutoCanvasRestore acr(canvas, true);
62
Mike Reed088b74e2018-12-24 14:52:46 -050063 draw_text_stroked(canvas, paint, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000064
65 canvas->translate(200, 0);
Mike Reed088b74e2018-12-24 14:52:46 -050066 draw_text_stroked(canvas, paint, font, 0);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000067
68 const SkScalar intervals[] = { 20, 10, 5, 10 };
69 const SkScalar phase = 0;
70
71 canvas->translate(200, 0);
72 SkPaint p(paint);
reeda4393342016-03-18 11:22:57 -070073 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
Mike Reed088b74e2018-12-24 14:52:46 -050074 draw_text_stroked(canvas, p, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000075}
76
halcanary2a243382015-09-09 08:16:41 -070077namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000078 enum {
79 kBelowThreshold_TextSize = 255,
80 kAboveThreshold_TextSize = 257
81 };
John Stilesa6841be2020-08-06 14:11:56 -040082} // namespace
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000083
halcanary2a243382015-09-09 08:16:41 -070084DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
Mike Reed088b74e2018-12-24 14:52:46 -050085 if (true) { test_nulldev(canvas); }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000086
Mike Reed088b74e2018-12-24 14:52:46 -050087 SkPaint paint;
88 paint.setAntiAlias(true);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000089
Mike Kleinea3f0142019-03-20 11:12:10 -050090 SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize);
Mike Reed088b74e2018-12-24 14:52:46 -050091 draw_text_set(canvas, paint, font);
92
93 canvas->translate(600, 0);
94 font.setSize(kAboveThreshold_TextSize);
95 draw_text_set(canvas, paint, font);
halcanary2a243382015-09-09 08:16:41 -070096}
Ben Wagnerd38f00a2020-01-27 17:43:41 -050097
98DEF_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}