Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
stroketext.cpp
Go to the documentation of this file.
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"
12#include "include/core/SkFont.h"
26#include "tools/Resources.h"
27#include "tools/ToolUtils.h"
29
30static void test_nulldev(SkCanvas* canvas) {
31 SkBitmap bm;
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
45static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
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
64static 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
81namespace {
82 enum {
83 kBelowThreshold_TextSize = 255,
84 kAboveThreshold_TextSize = 257
85 };
86} // namespace
87
88DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
89 if (true) { test_nulldev(canvas); }
90
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
102DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 420) {
105
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 }
112 { SkSetFourByteTag('w','g','h','t'), 721.0f },
113 };
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.");
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
172}
static const int strokeWidth
Definition BlurTest.cpp:60
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition Resources.cpp:31
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition SkTypes.h:167
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition SkBitmap.cpp:114
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
bool writePixels(const SkImageInfo &info, const void *pixels, size_t rowBytes, int x, int y)
Definition SkCanvas.cpp:403
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
@ kRound_Join
adds circle
Definition SkPaint.h:360
static sk_sp< SkTextBlob > MakeFromPosText(const void *text, size_t byteLength, const SkPoint pos[], const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
const Paint & paint
const EmbeddedViewParams * params
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H)
Definition gm.h:62
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
sk_sp< SkFontMgr > TestFontMgr()
static void draw_text_set(SkCanvas *canvas, const SkPaint &paint, const SkFont &font)
static void draw_text_stroked(SkCanvas *canvas, const SkPaint &paint, const SkFont &font, SkScalar strokeWidth)
static void test_nulldev(SkCanvas *canvas)
SkFontArguments & setVariationDesignPosition(VariationPosition position)
static SkImageInfo MakeN32Premul(int width, int height)
float fX
x-axis value
float fY
y-axis value