blob: 1deec3d11385ac46cbeafd55a7c2bc50ec8e786d [file] [log] [blame]
fmalita7a048692015-02-20 13:54:40 -08001/*
2 * Copyright 2015 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
Anas AlJabri84dd1832018-07-31 15:53:05 -07008#define ABORT_TEST(r, cond, ...) \
9 do { \
10 if (cond) { \
11 REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \
12 return; \
13 } \
14 } while (0)
15
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkBitmap.h"
17#include "include/core/SkCanvas.h"
18#include "include/core/SkColorFilter.h"
19#include "include/core/SkData.h"
20#include "include/core/SkImage.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkStream.h"
23#include "include/private/SkTo.h"
24#include "include/utils/SkParse.h"
25#include "src/core/SkMakeUnique.h"
26#include "src/shaders/SkImageShader.h"
27#include "tests/Test.h"
fmalita7a048692015-02-20 13:54:40 -080028
29#include <string.h>
30
Alexander Midlash77e3afc2018-03-06 17:21:28 -080031#ifdef SK_XML
32
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/svg/SkSVGDevice.h"
34#include "src/xml/SkDOM.h"
35#include "src/xml/SkXMLWriter.h"
Ben Wagner6ce482a2018-03-27 10:57:43 -040036
Florin Malita562017b2019-02-14 13:42:15 -050037static std::unique_ptr<SkCanvas> MakeDOMCanvas(SkDOM* dom) {
38 auto svgDevice = SkSVGDevice::Make(SkISize::Make(100, 100),
39 skstd::make_unique<SkXMLParserWriter>(dom->beginParsing()));
40 return svgDevice ? skstd::make_unique<SkCanvas>(svgDevice)
41 : nullptr;
42}
43
Herb Derby41f4f312018-06-06 17:45:53 +000044#if 0
45Using the new system where devices only gets glyphs causes this to fail because the font has no
46glyph to unichar data.
fmalita7a048692015-02-20 13:54:40 -080047namespace {
48
Alexander Midlash77e3afc2018-03-06 17:21:28 -080049
fmalita7a048692015-02-20 13:54:40 -080050void check_text_node(skiatest::Reporter* reporter,
51 const SkDOM& dom,
52 const SkDOM::Node* root,
53 const SkPoint& offset,
54 unsigned scalarsPerPos,
55 const char* expected) {
halcanary96fcdcc2015-08-27 07:41:13 -070056 if (root == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080057 ERRORF(reporter, "root element not found.");
58 return;
59 }
60
61 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
halcanary96fcdcc2015-08-27 07:41:13 -070062 if (textElem == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080063 ERRORF(reporter, "<text> element not found.");
64 return;
65 }
66 REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type);
67
68 const SkDOM::Node* textNode= dom.getFirstChild(textElem);
halcanary96fcdcc2015-08-27 07:41:13 -070069 REPORTER_ASSERT(reporter, textNode != nullptr);
70 if (textNode != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080071 REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
Herb Derby41f4f312018-06-06 17:45:53 +000072 if (strcmp(expected, dom.getName(textNode)) != 0) {
73 SkDebugf("string fail %s == %s\n", expected, dom.getName(textNode));
74 }
fmalita7a048692015-02-20 13:54:40 -080075 REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0);
76 }
77
78 int textLen = SkToInt(strlen(expected));
79
80 const char* x = dom.findAttr(textElem, "x");
halcanary96fcdcc2015-08-27 07:41:13 -070081 REPORTER_ASSERT(reporter, x != nullptr);
82 if (x != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080083 int xposCount = (scalarsPerPos < 1) ? 1 : textLen;
84 REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount);
85
86 SkAutoTMalloc<SkScalar> xpos(xposCount);
87 SkParse::FindScalars(x, xpos.get(), xposCount);
88 if (scalarsPerPos < 1) {
89 REPORTER_ASSERT(reporter, xpos[0] == offset.x());
90 } else {
91 for (int i = 0; i < xposCount; ++i) {
Herb Derby41f4f312018-06-06 17:45:53 +000092 if (xpos[i] != SkIntToScalar(expected[i])) {
93 SkDebugf("Bad xs %g == %g\n", xpos[i], SkIntToScalar(expected[i]));
94 }
fmalita7a048692015-02-20 13:54:40 -080095 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
96 }
97 }
98 }
99
100 const char* y = dom.findAttr(textElem, "y");
halcanary96fcdcc2015-08-27 07:41:13 -0700101 REPORTER_ASSERT(reporter, y != nullptr);
102 if (y != nullptr) {
fmalita7a048692015-02-20 13:54:40 -0800103 int yposCount = (scalarsPerPos < 2) ? 1 : textLen;
104 REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount);
105
106 SkAutoTMalloc<SkScalar> ypos(yposCount);
107 SkParse::FindScalars(y, ypos.get(), yposCount);
108 if (scalarsPerPos < 2) {
109 REPORTER_ASSERT(reporter, ypos[0] == offset.y());
110 } else {
111 for (int i = 0; i < yposCount; ++i) {
112 REPORTER_ASSERT(reporter, ypos[i] == -SkIntToScalar(expected[i]));
113 }
114 }
115 }
116}
117
118void test_whitespace_pos(skiatest::Reporter* reporter,
119 const char* txt,
120 const char* expected) {
121 size_t len = strlen(txt);
122
123 SkDOM dom;
124 SkPaint paint;
Mike Reed212e9062018-12-25 17:35:49 -0500125 SkFont font;
fmalita7a048692015-02-20 13:54:40 -0800126 SkPoint offset = SkPoint::Make(10, 20);
127
128 {
Florin Malita562017b2019-02-14 13:42:15 -0500129 auto svgCanvas = MakeDOMCanvas(&dom);
Ben Wagner51e15a62019-05-07 15:38:46 -0400130 svgCanvas->drawSimpleText(txt, len, SkTextEncoding::kUTF8, offset.x(), offset.y(),
Mike Reed212e9062018-12-25 17:35:49 -0500131 font, paint);
fmalita7a048692015-02-20 13:54:40 -0800132 }
Herb Derby41f4f312018-06-06 17:45:53 +0000133 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800134
135 {
136 SkAutoTMalloc<SkScalar> xpos(len);
137 for (int i = 0; i < SkToInt(len); ++i) {
138 xpos[i] = SkIntToScalar(txt[i]);
139 }
140
Florin Malita562017b2019-02-14 13:42:15 -0500141 auto svgCanvas = MakeDOMCanvas(&dom);
Mike Reed212e9062018-12-25 17:35:49 -0500142 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &xpos[0], offset.y(), font);
143 svgCanvas->drawTextBlob(blob, 0, 0, paint);
fmalita7a048692015-02-20 13:54:40 -0800144 }
Herb Derby41f4f312018-06-06 17:45:53 +0000145 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800146
147 {
148 SkAutoTMalloc<SkPoint> pos(len);
149 for (int i = 0; i < SkToInt(len); ++i) {
150 pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
151 }
152
153 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed212e9062018-12-25 17:35:49 -0500154 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &pos[0], font);
155 svgCanvas->drawTextBlob(blob, 0, 0, paint);
fmalita7a048692015-02-20 13:54:40 -0800156 }
157 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
158}
159
160}
161
Herb Derby41f4f312018-06-06 17:45:53 +0000162
fmalita7a048692015-02-20 13:54:40 -0800163DEF_TEST(SVGDevice_whitespace_pos, reporter) {
164 static const struct {
165 const char* tst_in;
166 const char* tst_out;
167 } tests[] = {
168 { "abcd" , "abcd" },
169 { "ab cd" , "ab cd" },
170 { "ab \t\t cd", "ab cd" },
171 { " abcd" , "abcd" },
172 { " abcd" , "abcd" },
173 { " \t\t abcd", "abcd" },
174 { "abcd " , "abcd " }, // we allow one trailing whitespace char
175 { "abcd " , "abcd " }, // because it makes no difference and
176 { "abcd\t " , "abcd\t" }, // simplifies the implementation
177 { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " },
178 };
179
180 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
181 test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
182 }
183}
Herb Derby41f4f312018-06-06 17:45:53 +0000184#endif
Hal Canaryff2742e2018-01-30 11:35:47 -0500185
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800186
Mike Reede25b4472019-04-02 17:49:12 -0400187void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkTileMode xTile,
188 SkTileMode yTile) {
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800189 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
Mike Reede25b4472019-04-02 17:49:12 -0400190 paint->setShader(surface->makeImageSnapshot()->makeShader(xTile, yTile, nullptr));
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800191}
192
193// Attempt to find the three nodes on which we have expectations:
194// the pattern node, the image within that pattern, and the rect which
195// uses the pattern as a fill.
196// returns false if not all nodes are found.
197bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
198 const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
199 const SkDOM::Node** rectOut) {
200 if (root == nullptr || dom == nullptr) {
201 ERRORF(reporter, "root element not found");
202 return false;
203 }
204
205
206 const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
207 if (rect == nullptr) {
208 ERRORF(reporter, "rect not found");
209 return false;
210 }
211 *rectOut = rect;
212
213 const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
214 if (defs == nullptr) {
215 ERRORF(reporter, "defs not found");
216 return false;
217 }
218
219 const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
220 if (pattern == nullptr) {
221 ERRORF(reporter, "pattern not found");
222 return false;
223 }
224 *patternOut = pattern;
225
226 const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
227 if (image == nullptr) {
228 ERRORF(reporter, "image not found");
229 return false;
230 }
231 *imageOut = image;
232
233 return true;
234}
235
236void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
Mike Reede25b4472019-04-02 17:49:12 -0400237 int rectWidth, int rectHeight, SkTileMode xTile, SkTileMode yTile) {
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800238 SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
Florin Malita562017b2019-02-14 13:42:15 -0500239 auto svgCanvas = MakeDOMCanvas(dom);
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800240
241 SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
242 svgCanvas->drawRect(bounds, *paint);
243}
244
245
246DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
247 SkDOM dom;
248 SkPaint paint;
249 int imageWidth = 3, imageHeight = 3;
250 int rectWidth = 10, rectHeight = 10;
251 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
Mike Reede25b4472019-04-02 17:49:12 -0400252 SkTileMode::kClamp, SkTileMode::kClamp);
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800253
254 const SkDOM::Node* root = dom.finishParsing();
255
256 const SkDOM::Node *patternNode, *imageNode, *rectNode;
257 bool structureAppropriate =
258 FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
259 REPORTER_ASSERT(reporter, structureAppropriate);
260
261 // the image should always maintain its size.
262 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
263 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
264
265 // making the pattern as large as the container prevents
266 // it from repeating.
267 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
268 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
269}
270
271DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
272 SkDOM dom;
273 SkPaint paint;
274 int imageWidth = 3, imageHeight = 3;
275 int rectWidth = 10, rectHeight = 10;
276 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
Mike Reede25b4472019-04-02 17:49:12 -0400277 SkTileMode::kRepeat, SkTileMode::kClamp);
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800278
279 const SkDOM::Node* root = dom.finishParsing();
280 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
281 if (innerSvg == nullptr) {
282 ERRORF(reporter, "inner svg element not found");
283 return;
284 }
285
286 const SkDOM::Node *patternNode, *imageNode, *rectNode;
287 bool structureAppropriate =
288 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
289 REPORTER_ASSERT(reporter, structureAppropriate);
290
291 // the imageNode should always maintain its size.
292 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
293 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
294
295 // if the patternNode width matches the imageNode width,
296 // it will repeat in along the x axis.
297 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
298 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
299}
300
301DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
302 SkDOM dom;
303 SkPaint paint;
304 int imageNodeWidth = 3, imageNodeHeight = 3;
305 int rectNodeWidth = 10, rectNodeHeight = 10;
306 ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
Mike Reede25b4472019-04-02 17:49:12 -0400307 rectNodeHeight, SkTileMode::kClamp, SkTileMode::kRepeat);
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800308
309 const SkDOM::Node* root = dom.finishParsing();
310 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
311 if (innerSvg == nullptr) {
312 ERRORF(reporter, "inner svg element not found");
313 return;
314 }
315
316 const SkDOM::Node *patternNode, *imageNode, *rectNode;
317 bool structureAppropriate =
318 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
319 REPORTER_ASSERT(reporter, structureAppropriate);
320
321 // the imageNode should always maintain its size.
322 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
323 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
324
325 // making the patternNode as large as the container prevents
326 // it from repeating.
327 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
328 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
329}
330
331DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
332 SkDOM dom;
333 SkPaint paint;
334 int imageWidth = 3, imageHeight = 3;
335 int rectWidth = 10, rectHeight = 10;
336 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
Mike Reede25b4472019-04-02 17:49:12 -0400337 SkTileMode::kRepeat, SkTileMode::kRepeat);
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800338
339 const SkDOM::Node* root = dom.finishParsing();
340
341 const SkDOM::Node *patternNode, *imageNode, *rectNode;
342 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
343 if (innerSvg == nullptr) {
344 ERRORF(reporter, "inner svg element not found");
345 return;
346 }
347 bool structureAppropriate =
348 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
349 REPORTER_ASSERT(reporter, structureAppropriate);
350
351 // the imageNode should always maintain its size.
352 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
353 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
354
355 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
356 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
357}
358
Anas AlJabri84dd1832018-07-31 15:53:05 -0700359DEF_TEST(SVGDevice_ColorFilters, reporter) {
360 SkDOM dom;
361 SkPaint paint;
Mike Reedb286bc22019-04-08 16:23:20 -0400362 paint.setColorFilter(SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kSrcIn));
Anas AlJabri84dd1832018-07-31 15:53:05 -0700363 {
Florin Malita562017b2019-02-14 13:42:15 -0500364 auto svgCanvas = MakeDOMCanvas(&dom);
Anas AlJabri84dd1832018-07-31 15:53:05 -0700365 SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
366 svgCanvas->drawRect(bounds, paint);
367 }
368 const SkDOM::Node* rootElement = dom.finishParsing();
369 ABORT_TEST(reporter, !rootElement, "root element not found");
370
371 const SkDOM::Node* filterElement = dom.getFirstChild(rootElement, "filter");
372 ABORT_TEST(reporter, !filterElement, "filter element not found");
373
374 const SkDOM::Node* floodElement = dom.getFirstChild(filterElement, "feFlood");
375 ABORT_TEST(reporter, !floodElement, "feFlood element not found");
376
377 const SkDOM::Node* compositeElement = dom.getFirstChild(filterElement, "feComposite");
378 ABORT_TEST(reporter, !compositeElement, "feComposite element not found");
379
380 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "width"), "100%") == 0);
381 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "height"), "100%") == 0);
382
383 REPORTER_ASSERT(reporter,
384 strcmp(dom.findAttr(floodElement, "flood-color"), "rgb(255,0,0)") == 0);
385 REPORTER_ASSERT(reporter, atoi(dom.findAttr(floodElement, "flood-opacity")) == 1);
386
387 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "in"), "flood") == 0);
388 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "operator"), "in") == 0);
389}
390
Hal Canaryff2742e2018-01-30 11:35:47 -0500391#endif