Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Functions
PaintTest.cpp File Reference
#include "include/core/SkBlendMode.h"
#include "include/core/SkBlurTypes.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkColorType.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPathUtils.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/effects/SkColorMatrix.h"
#include "include/private/base/SkTemplates.h"
#include "src/base/SkAutoMalloc.h"
#include "src/core/SkBlurMask.h"
#include "src/core/SkPaintPriv.h"
#include "src/core/SkReadBuffer.h"
#include "src/core/SkWriteBuffer.h"
#include "tests/Test.h"
#include "tools/fonts/FontToolUtils.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <optional>

Go to the source code of this file.

Macros

#define FOR_SETUP(index, array, setter)
 
#define ASSERT(expr)   REPORTER_ASSERT(r, expr)
 

Functions

 DEF_TEST (Paint_copy, reporter)
 
 DEF_TEST (Paint_regression_cubic, reporter)
 
 DEF_TEST (Paint_flattening, reporter)
 
 DEF_TEST (Paint_regression_measureText, reporter)
 
 DEF_TEST (Paint_MoreFlattening, r)
 
 DEF_TEST (Paint_nothingToDraw, r)
 
 DEF_TEST (Font_getpos, r)
 
 DEF_TEST (Paint_dither, reporter)
 

Macro Definition Documentation

◆ ASSERT

#define ASSERT (   expr)    REPORTER_ASSERT(r, expr)

Definition at line 161 of file PaintTest.cpp.

◆ FOR_SETUP

#define FOR_SETUP (   index,
  array,
  setter 
)
Value:
for (size_t index = 0; index < std::size(array); ++index) { \
paint.setter(array[index]);
const Paint & paint

Function Documentation

◆ DEF_TEST() [1/8]

DEF_TEST ( Font_getpos  ,
 
)

Definition at line 207 of file PaintTest.cpp.

207 {
209 const char text[] = "Hamburgefons!@#!#23425,./;'[]";
210 int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
211 AutoTArray<uint16_t> glyphStorage(count);
212 uint16_t* glyphs = glyphStorage.get();
213 (void)font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, glyphs, count);
214
215 AutoTArray<SkScalar> widthStorage(count);
216 AutoTArray<SkScalar> xposStorage(count);
217 AutoTArray<SkPoint> posStorage(count);
218
219 SkScalar* widths = widthStorage.get();
220 SkScalar* xpos = xposStorage.get();
221 SkPoint* pos = posStorage.get();
222
223 for (bool subpix : { false, true }) {
224 font.setSubpixel(subpix);
226 font.setHinting(hint);
227 for (auto size : { 1.0f, 12.0f, 100.0f }) {
228 font.setSize(size);
229
230 font.getWidths(glyphs, count, widths);
231 font.getXPos(glyphs, count, xpos, 10);
232 font.getPos(glyphs, count, pos, {10, 20});
233
234 auto nearly_eq = [](SkScalar a, SkScalar b) {
235 return SkScalarAbs(a - b) < 0.000001f;
236 };
237
238 SkScalar x = 10;
239 for (int i = 0; i < count; ++i) {
240 REPORTER_ASSERT(r, nearly_eq(x, xpos[i]));
241 REPORTER_ASSERT(r, nearly_eq(x, pos[i].fX));
242 REPORTER_ASSERT(r, nearly_eq(20, pos[i].fY));
243 x += widths[i];
244 }
245 }
246 }
247 }
248}
uint16_t glyphs[5]
int count
SkPoint pos
@ kNormal
glyph outlines modified to improve constrast
@ kNone
glyph outlines unchanged
@ kSlight
minimal modification to improve constrast
@ kFull
modifies glyph outlines for maximum constrast
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkScalarAbs(x)
Definition SkScalar.h:39
const SkScalar widths[]
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct a[10]
std::u16string text
double x
SkFont DefaultFont()
font
Font Metadata and Metrics.

◆ DEF_TEST() [2/8]

DEF_TEST ( Paint_copy  ,
reporter   
)

Definition at line 43 of file PaintTest.cpp.

43 {
45 // set a few member variables
47 paint.setStrokeWidth(SkIntToScalar(2));
48 // set a few pointers
51
52 // copy the paint using the copy constructor and check they are the same
53 SkPaint copiedPaint = paint;
54 REPORTER_ASSERT(reporter, paint == copiedPaint);
55
56 // copy the paint using the equal operator and check they are the same
57 copiedPaint = paint;
58 REPORTER_ASSERT(reporter, paint == copiedPaint);
59
60 // clean the paint and check they are back to their initial states
61 SkPaint cleanPaint;
62 paint.reset();
63 copiedPaint.reset();
64 REPORTER_ASSERT(reporter, cleanPaint == paint);
65 REPORTER_ASSERT(reporter, cleanPaint == copiedPaint);
66}
reporter
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition SkBlurTypes.h:12
#define SkIntToScalar(x)
Definition SkScalar.h:57
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void reset()
Definition SkPaint.cpp:103
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195

◆ DEF_TEST() [3/8]

DEF_TEST ( Paint_dither  ,
reporter   
)

Definition at line 250 of file PaintTest.cpp.

250 {
251 SkPaint p;
252 p.setDither(true);
253
255
256 REPORTER_ASSERT(reporter, !shouldDither);
257}
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
static bool ShouldDither(const SkPaint &, SkColorType)

◆ DEF_TEST() [4/8]

DEF_TEST ( Paint_flattening  ,
reporter   
)

Definition at line 102 of file PaintTest.cpp.

102 {
103 const SkPaint::Cap caps[] = {
107 };
108 const SkPaint::Join joins[] = {
112 };
113 const SkPaint::Style styles[] = {
117 };
118
119#define FOR_SETUP(index, array, setter) \
120 for (size_t index = 0; index < std::size(array); ++index) { \
121 paint.setter(array[index]);
122
124 paint.setAntiAlias(true);
125
126 // we don't serialize hinting or encoding -- soon to be removed from paint
127
128 FOR_SETUP(l, caps, setStrokeCap)
129 FOR_SETUP(m, joins, setStrokeJoin)
130 FOR_SETUP(p, styles, setStyle)
131
132 SkBinaryWriteBuffer writer({});
134
135 SkAutoMalloc buf(writer.bytesWritten());
136 writer.writeToMemory(buf.get());
137 SkReadBuffer reader(buf.get(), writer.bytesWritten());
138
139 SkPaint paint2 = reader.readPaint();
140 REPORTER_ASSERT(reporter, paint2 == paint);
141
142 }}}
#define FOR_SETUP(index, array, setter)
static void Flatten(const SkPaint &paint, SkWriteBuffer &buffer)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition SkPaint.h:334
@ kSquare_Cap
adds square
Definition SkPaint.h:336
@ 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
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition SkPaint.h:361

◆ DEF_TEST() [5/8]

DEF_TEST ( Paint_MoreFlattening  ,
 
)

Definition at line 163 of file PaintTest.cpp.

163 {
165 paint.setColor(0x00AABBCC);
166 paint.setBlendMode(SkBlendMode::kModulate);
167
168 SkBinaryWriteBuffer writer({});
170
171 SkAutoMalloc buf(writer.bytesWritten());
172 writer.writeToMemory(buf.get());
173 SkReadBuffer reader(buf.get(), writer.bytesWritten());
174
175 SkPaint other = reader.readPaint();
176 ASSERT(reader.offset() == writer.bytesWritten());
177
178 // No matter the encoding, these must always hold.
179 ASSERT(other.getColor() == paint.getColor());
180 ASSERT(other.asBlendMode() == paint.asBlendMode());
181}
#define ASSERT(expr)
@ kModulate
r = s*d
SkColor getColor() const
Definition SkPaint.h:225
std::optional< SkBlendMode > asBlendMode() const
Definition SkPaint.cpp:138

◆ DEF_TEST() [6/8]

DEF_TEST ( Paint_nothingToDraw  ,
 
)

Definition at line 183 of file PaintTest.cpp.

183 {
185
186 REPORTER_ASSERT(r, !paint.nothingToDraw());
187 paint.setAlpha(0);
188 REPORTER_ASSERT(r, paint.nothingToDraw());
189
190 paint.setAlpha(0xFF);
191 paint.setBlendMode(SkBlendMode::kDst);
192 REPORTER_ASSERT(r, paint.nothingToDraw());
193
194 paint.setAlpha(0);
195 paint.setBlendMode(SkBlendMode::kSrcOver);
196
197 SkColorMatrix cm;
198 cm.setIdentity(); // does not change alpha
199 paint.setColorFilter(SkColorFilters::Matrix(cm));
200 REPORTER_ASSERT(r, paint.nothingToDraw());
201
202 cm.postTranslate(0, 0, 0, 1.0f/255); // wacks alpha
203 paint.setColorFilter(SkColorFilters::Matrix(cm));
204 REPORTER_ASSERT(r, !paint.nothingToDraw());
205}
@ kSrcOver
r = s + (1-sa)*d
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void postTranslate(float dr, float dg, float db, float da)

◆ DEF_TEST() [7/8]

DEF_TEST ( Paint_regression_cubic  ,
reporter   
)

Definition at line 70 of file PaintTest.cpp.

70 {
71 SkPath path, stroke;
73
74 path.moveTo(460.2881309415525f,
75 303.250847066498f);
76 path.cubicTo(463.36378422175284f,
77 302.1169735073363f,
78 456.32239330810046f,
79 304.720354932878f,
80 453.15255460013304f,
81 305.788586869862f);
82
83 SkRect fillR, strokeR;
84 fillR = path.getBounds();
85
87 paint.setStrokeWidth(SkIntToScalar(2));
89 strokeR = stroke.getBounds();
90
91 SkRect maxR = fillR;
92 SkScalar miter = std::max(SK_Scalar1, paint.getStrokeMiter());
93 SkScalar inset = paint.getStrokeJoin() == SkPaint::kMiter_Join ?
94 paint.getStrokeWidth() * miter :
95 paint.getStrokeWidth();
96 maxR.inset(-inset, -inset);
97
98 // test that our stroke didn't explode
99 REPORTER_ASSERT(reporter, maxR.contains(strokeR));
100}
#define SK_Scalar1
Definition SkScalar.h:18
const SkRect & getBounds() const
Definition SkPath.cpp:420
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57
SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst, const SkRect *cullRect, SkScalar resScale=1)
static SkRect inset(const SkRect &r)
void inset(float dx, float dy)
Definition SkRect.h:1060
bool contains(SkScalar x, SkScalar y) const
Definition extension.cpp:19

◆ DEF_TEST() [8/8]

DEF_TEST ( Paint_regression_measureText  ,
reporter   
)

Definition at line 148 of file PaintTest.cpp.

148 {
149
151 font.setSize(12.0f);
152
153 SkRect r;
155
156 // test that the rect was reset
157 font.measureText("", 0, SkTextEncoding::kUTF8, &r);
159}
#define SK_ScalarNaN
Definition SkScalar.h:28
void setLTRB(float left, float top, float right, float bottom)
Definition SkRect.h:865
bool isEmpty() const
Definition SkRect.h:693