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
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontArguments.h"
14 #include "include/core/SkFontMgr.h"
15 #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"
21 #include "include/core/SkStream.h"
22 #include "include/core/SkTextBlob.h"
23 #include "include/core/SkTypeface.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkDashPathEffect.h"
26 #include "tools/Resources.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29
test_nulldev(SkCanvas * canvas)30 static void test_nulldev(SkCanvas* canvas) {
31 SkBitmap bm;
32 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
33 // 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
draw_text_stroked(SkCanvas * canvas,const SkPaint & paint,const SkFont & font,SkScalar strokeWidth)45 static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
46 SkScalar strokeWidth) {
47 SkPaint p(paint);
48 SkPoint loc = { 20, 435 };
49
50 if (strokeWidth > 0) {
51 p.setStyle(SkPaint::kFill_Style);
52 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
53 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
54 }
55
56 p.setColor(SK_ColorRED);
57 p.setStyle(SkPaint::kStroke_Style);
58 p.setStrokeWidth(strokeWidth);
59
60 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
61 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
62 }
63
draw_text_set(SkCanvas * canvas,const SkPaint & paint,const SkFont & font)64 static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
65 SkAutoCanvasRestore acr(canvas, true);
66
67 draw_text_stroked(canvas, paint, font, 10);
68
69 canvas->translate(200, 0);
70 draw_text_stroked(canvas, paint, font, 0);
71
72 const SkScalar intervals[] = { 20, 10, 5, 10 };
73 const SkScalar phase = 0;
74
75 canvas->translate(200, 0);
76 SkPaint p(paint);
77 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), phase));
78 draw_text_stroked(canvas, p, font, 10);
79 }
80
81 namespace {
82 enum {
83 kBelowThreshold_TextSize = 255,
84 kAboveThreshold_TextSize = 257
85 };
86 } // namespace
87
88 DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
89 if (true) { test_nulldev(canvas); }
90
91 SkPaint paint;
92 paint.setAntiAlias(true);
93
94 SkFont font(ToolUtils::DefaultPortableTypeface(), kBelowThreshold_TextSize);
95 draw_text_set(canvas, paint, font);
96
97 canvas->translate(600, 0);
98 font.setSize(kAboveThreshold_TextSize);
99 draw_text_set(canvas, paint, font);
100 }
101
102 DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 420) {
103 sk_sp<SkTypeface> ttf = ToolUtils::CreateTypefaceFromResource("fonts/Stroking.ttf");
104 sk_sp<SkTypeface> otf = ToolUtils::CreateTypefaceFromResource("fonts/Stroking.otf");
105
__anon8d109e2f0302() 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;
115 params.setVariationDesignPosition({position, std::size(position)});
116 return ToolUtils::TestFontMgr()->makeFromStream(std::move(variableStream), params);
117 }();
118
119 if (!ttf && !otf && !overlap) {
120 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
161 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
171 return skiagm::DrawResult::kOk;
172 }
173