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/SkTextBlob.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkDashPathEffect.h"
25 #include "tools/Resources.h"
26 #include "tools/ToolUtils.h"
27 
test_nulldev(SkCanvas * canvas)28 static void test_nulldev(SkCanvas* canvas) {
29     SkBitmap bm;
30     bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
31     // notice: no pixels mom! be sure we don't crash
32     // https://code.google.com/p/chromium/issues/detail?id=352616
33     SkCanvas c(bm);
34 
35     SkBitmap src;
36     src.allocN32Pixels(10, 10);
37     src.eraseColor(SK_ColorRED);
38 
39     // ensure we don't crash
40     c.writePixels(src, 0, 0);
41 }
42 
draw_text_stroked(SkCanvas * canvas,const SkPaint & paint,const SkFont & font,SkScalar strokeWidth)43 static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
44                               SkScalar strokeWidth) {
45     SkPaint p(paint);
46     SkPoint loc = { 20, 435 };
47 
48     if (strokeWidth > 0) {
49         p.setStyle(SkPaint::kFill_Style);
50         canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
51         canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
52     }
53 
54     p.setColor(SK_ColorRED);
55     p.setStyle(SkPaint::kStroke_Style);
56     p.setStrokeWidth(strokeWidth);
57 
58     canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
59     canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
60 }
61 
draw_text_set(SkCanvas * canvas,const SkPaint & paint,const SkFont & font)62 static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
63     SkAutoCanvasRestore acr(canvas, true);
64 
65     draw_text_stroked(canvas, paint, font, 10);
66 
67     canvas->translate(200, 0);
68     draw_text_stroked(canvas, paint, font, 0);
69 
70     const SkScalar intervals[] = { 20, 10, 5, 10 };
71     const SkScalar phase = 0;
72 
73     canvas->translate(200, 0);
74     SkPaint p(paint);
75     p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), phase));
76     draw_text_stroked(canvas, p, font, 10);
77 }
78 
79 namespace {
80     enum {
81         kBelowThreshold_TextSize = 255,
82         kAboveThreshold_TextSize = 257
83     };
84 }  // namespace
85 
86 DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
87     if (true) { test_nulldev(canvas); }
88 
89     SkPaint paint;
90     paint.setAntiAlias(true);
91 
92     SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize);
93     draw_text_set(canvas, paint, font);
94 
95     canvas->translate(600, 0);
96     font.setSize(kAboveThreshold_TextSize);
97     draw_text_set(canvas, paint, font);
98 }
99 
100 DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 420) {
101     sk_sp<SkTypeface> ttf = MakeResourceAsTypeface("fonts/Stroking.ttf");
102     sk_sp<SkTypeface> otf = MakeResourceAsTypeface("fonts/Stroking.otf");
103 
__anonce7de39e0302() 104     sk_sp<SkTypeface> overlap = []() -> sk_sp<SkTypeface>{
105         std::unique_ptr<SkStreamAsset> variableStream(GetResourceAsStream("fonts/Variable.ttf"));
106         if (!variableStream) {
107             return nullptr;
108         }
109         const SkFontArguments::VariationPosition::Coordinate position[] = {
110             { SkSetFourByteTag('w','g','h','t'), 721.0f },
111         };
112         SkFontArguments params;
113         params.setVariationDesignPosition({position, std::size(position)});
114         return SkFontMgr::RefDefault()->makeFromStream(std::move(variableStream), params);
115     }();
116 
117     if (!ttf && !otf && !overlap) {
118         msg->append("No support for ttf or otf.");
119         return skiagm::DrawResult::kSkip;
120     }
121 
122     SkPaint p;
123     p.setAntiAlias(true);
124     p.setStyle(SkPaint::kStroke_Style);
125     p.setStrokeWidth(10);
126     p.setStrokeCap(SkPaint::kRound_Cap);
127     p.setStrokeJoin(SkPaint::kRound_Join);
128     p.setARGB(0xff, 0xbb, 0x00, 0x00);
129 
130     if (ttf) {
131         /* Stroking.ttf is structured like:
132             nothing U+25CB ○ (nothing inside)
133             something U+25C9 ◉ (a tiny thing inside)
134             - off (point off / empty quad with implicit end) (before U+207B ⁻ / after U+208B ₋)
135             + on  (point on / empty line) (before U+207A ⁺ / after U+208A ₊)
136             0 off off (two implicit quads) (before U+2070 ⁰ / after U+2080 ₀)
137             1 off on  (quad with implicit close around) (before U+00B9 ¹ / after U+2081 ₁)
138             2 on  off (quad with implicit close) (before U+00B2 ² / after U+2082 ₂)
139             3 on  on  (empty line) (before U+00B3 ³ / after U+2083 ₃)
140         */
141         SkFont font(ttf, 100);
142         canvas->drawString("○◉  ⁻₋⁺₊", 10, 100, font, p);
143         canvas->drawString("⁰₀¹₁²₂³₃", 10, 200, font, p);
144     }
145 
146     if (otf) {
147         /* Stroking.otf is structured like:
148             nothing U+25CB ○
149             something U+25C9 ◉
150             0 moveto, moveto (before U+2070 ⁰) (nothing there, FreeType ignores these)
151             1 moveto, empty line, moveto (before U+00B9 ¹) (degenerate lineto)
152             3 moveto, empty cubic, moveto (before U+00B3 ³) (degenerate cubicto)
153             f moveto, empty flex, moveto (before U+1DA0 ᶠ) (degenerate flex)
154         */
155         SkFont font(otf, 100);
156         canvas->drawString("○◉  ⁰¹³ᶠ", 10, 300, font, p);
157     }
158 
159     if (overlap) {
160         /* Variable.ttf is structured like:
161             U+74 t (glyf outline has overlap flag)
162             U+167 ŧ (glyf outline does not have overlap flag)
163         */
164         SkFont font(overlap, 100);
165         p.setStrokeWidth(1);
166         canvas->drawString("tŧ", 10, 400, font, p);
167     }
168 
169     return skiagm::DrawResult::kOk;
170 }
171