blob: 92070abce8b5b2e2031d401b7c2dcf167869d857 [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
Alexander Midlash77e3afc2018-03-06 17:21:28 -080016#include "SkBitmap.h"
fmalita7a048692015-02-20 13:54:40 -080017#include "SkCanvas.h"
Anas AlJabri84dd1832018-07-31 15:53:05 -070018#include "SkColorFilter.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080019#include "SkData.h"
20#include "SkImage.h"
21#include "SkImageShader.h"
fmalita7a048692015-02-20 13:54:40 -080022#include "SkParse.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080023#include "SkShader.h"
24#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040025#include "SkTo.h"
fmalita7a048692015-02-20 13:54:40 -080026#include "Test.h"
27
28#include <string.h>
29
Alexander Midlash77e3afc2018-03-06 17:21:28 -080030#ifdef SK_XML
31
Ben Wagner6ce482a2018-03-27 10:57:43 -040032#include "SkDOM.h"
33#include "SkSVGCanvas.h"
34#include "SkXMLWriter.h"
35
Herb Derby41f4f312018-06-06 17:45:53 +000036#if 0
37Using the new system where devices only gets glyphs causes this to fail because the font has no
38glyph to unichar data.
fmalita7a048692015-02-20 13:54:40 -080039namespace {
40
Alexander Midlash77e3afc2018-03-06 17:21:28 -080041
fmalita7a048692015-02-20 13:54:40 -080042void check_text_node(skiatest::Reporter* reporter,
43 const SkDOM& dom,
44 const SkDOM::Node* root,
45 const SkPoint& offset,
46 unsigned scalarsPerPos,
47 const char* expected) {
halcanary96fcdcc2015-08-27 07:41:13 -070048 if (root == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080049 ERRORF(reporter, "root element not found.");
50 return;
51 }
52
53 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
halcanary96fcdcc2015-08-27 07:41:13 -070054 if (textElem == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080055 ERRORF(reporter, "<text> element not found.");
56 return;
57 }
58 REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type);
59
60 const SkDOM::Node* textNode= dom.getFirstChild(textElem);
halcanary96fcdcc2015-08-27 07:41:13 -070061 REPORTER_ASSERT(reporter, textNode != nullptr);
62 if (textNode != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080063 REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
Herb Derby41f4f312018-06-06 17:45:53 +000064 if (strcmp(expected, dom.getName(textNode)) != 0) {
65 SkDebugf("string fail %s == %s\n", expected, dom.getName(textNode));
66 }
fmalita7a048692015-02-20 13:54:40 -080067 REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0);
68 }
69
70 int textLen = SkToInt(strlen(expected));
71
72 const char* x = dom.findAttr(textElem, "x");
halcanary96fcdcc2015-08-27 07:41:13 -070073 REPORTER_ASSERT(reporter, x != nullptr);
74 if (x != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080075 int xposCount = (scalarsPerPos < 1) ? 1 : textLen;
76 REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount);
77
78 SkAutoTMalloc<SkScalar> xpos(xposCount);
79 SkParse::FindScalars(x, xpos.get(), xposCount);
80 if (scalarsPerPos < 1) {
81 REPORTER_ASSERT(reporter, xpos[0] == offset.x());
82 } else {
83 for (int i = 0; i < xposCount; ++i) {
Herb Derby41f4f312018-06-06 17:45:53 +000084 if (xpos[i] != SkIntToScalar(expected[i])) {
85 SkDebugf("Bad xs %g == %g\n", xpos[i], SkIntToScalar(expected[i]));
86 }
fmalita7a048692015-02-20 13:54:40 -080087 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
88 }
89 }
90 }
91
92 const char* y = dom.findAttr(textElem, "y");
halcanary96fcdcc2015-08-27 07:41:13 -070093 REPORTER_ASSERT(reporter, y != nullptr);
94 if (y != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080095 int yposCount = (scalarsPerPos < 2) ? 1 : textLen;
96 REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount);
97
98 SkAutoTMalloc<SkScalar> ypos(yposCount);
99 SkParse::FindScalars(y, ypos.get(), yposCount);
100 if (scalarsPerPos < 2) {
101 REPORTER_ASSERT(reporter, ypos[0] == offset.y());
102 } else {
103 for (int i = 0; i < yposCount; ++i) {
104 REPORTER_ASSERT(reporter, ypos[i] == -SkIntToScalar(expected[i]));
105 }
106 }
107 }
108}
109
110void test_whitespace_pos(skiatest::Reporter* reporter,
111 const char* txt,
112 const char* expected) {
113 size_t len = strlen(txt);
114
115 SkDOM dom;
116 SkPaint paint;
117 SkPoint offset = SkPoint::Make(10, 20);
118
119 {
120 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600121 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800122 svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint);
123 }
Herb Derby41f4f312018-06-06 17:45:53 +0000124 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800125
126 {
127 SkAutoTMalloc<SkScalar> xpos(len);
128 for (int i = 0; i < SkToInt(len); ++i) {
129 xpos[i] = SkIntToScalar(txt[i]);
130 }
131
132 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600133 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800134 svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint);
135 }
Herb Derby41f4f312018-06-06 17:45:53 +0000136 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800137
138 {
139 SkAutoTMalloc<SkPoint> pos(len);
140 for (int i = 0; i < SkToInt(len); ++i) {
141 pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
142 }
143
144 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600145 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800146 svgCanvas->drawPosText(txt, len, pos, paint);
147 }
148 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
149}
150
151}
152
Herb Derby41f4f312018-06-06 17:45:53 +0000153
fmalita7a048692015-02-20 13:54:40 -0800154DEF_TEST(SVGDevice_whitespace_pos, reporter) {
155 static const struct {
156 const char* tst_in;
157 const char* tst_out;
158 } tests[] = {
159 { "abcd" , "abcd" },
160 { "ab cd" , "ab cd" },
161 { "ab \t\t cd", "ab cd" },
162 { " abcd" , "abcd" },
163 { " abcd" , "abcd" },
164 { " \t\t abcd", "abcd" },
165 { "abcd " , "abcd " }, // we allow one trailing whitespace char
166 { "abcd " , "abcd " }, // because it makes no difference and
167 { "abcd\t " , "abcd\t" }, // simplifies the implementation
168 { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " },
169 };
170
171 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
172 test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
173 }
174}
Herb Derby41f4f312018-06-06 17:45:53 +0000175#endif
Hal Canaryff2742e2018-01-30 11:35:47 -0500176
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800177
178void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkShader::TileMode xTile,
179 SkShader::TileMode yTile) {
180 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
181 paint->setShader(SkImageShader::Make(surface->makeImageSnapshot(), xTile, yTile, nullptr));
182}
183
184// Attempt to find the three nodes on which we have expectations:
185// the pattern node, the image within that pattern, and the rect which
186// uses the pattern as a fill.
187// returns false if not all nodes are found.
188bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
189 const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
190 const SkDOM::Node** rectOut) {
191 if (root == nullptr || dom == nullptr) {
192 ERRORF(reporter, "root element not found");
193 return false;
194 }
195
196
197 const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
198 if (rect == nullptr) {
199 ERRORF(reporter, "rect not found");
200 return false;
201 }
202 *rectOut = rect;
203
204 const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
205 if (defs == nullptr) {
206 ERRORF(reporter, "defs not found");
207 return false;
208 }
209
210 const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
211 if (pattern == nullptr) {
212 ERRORF(reporter, "pattern not found");
213 return false;
214 }
215 *patternOut = pattern;
216
217 const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
218 if (image == nullptr) {
219 ERRORF(reporter, "image not found");
220 return false;
221 }
222 *imageOut = image;
223
224 return true;
225}
226
227void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
228 int rectWidth, int rectHeight, SkShader::TileMode xTile,
229 SkShader::TileMode yTile) {
230 SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
231 SkXMLParserWriter writer(dom->beginParsing());
232 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
233
234 SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
235 svgCanvas->drawRect(bounds, *paint);
236}
237
238
239DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
240 SkDOM dom;
241 SkPaint paint;
242 int imageWidth = 3, imageHeight = 3;
243 int rectWidth = 10, rectHeight = 10;
244 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
245 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
246
247 const SkDOM::Node* root = dom.finishParsing();
248
249 const SkDOM::Node *patternNode, *imageNode, *rectNode;
250 bool structureAppropriate =
251 FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
252 REPORTER_ASSERT(reporter, structureAppropriate);
253
254 // the image should always maintain its size.
255 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
256 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
257
258 // making the pattern as large as the container prevents
259 // it from repeating.
260 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
261 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
262}
263
264DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
265 SkDOM dom;
266 SkPaint paint;
267 int imageWidth = 3, imageHeight = 3;
268 int rectWidth = 10, rectHeight = 10;
269 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
270 SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode);
271
272 const SkDOM::Node* root = dom.finishParsing();
273 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
274 if (innerSvg == nullptr) {
275 ERRORF(reporter, "inner svg element not found");
276 return;
277 }
278
279 const SkDOM::Node *patternNode, *imageNode, *rectNode;
280 bool structureAppropriate =
281 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
282 REPORTER_ASSERT(reporter, structureAppropriate);
283
284 // the imageNode should always maintain its size.
285 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
286 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
287
288 // if the patternNode width matches the imageNode width,
289 // it will repeat in along the x axis.
290 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
291 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
292}
293
294DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
295 SkDOM dom;
296 SkPaint paint;
297 int imageNodeWidth = 3, imageNodeHeight = 3;
298 int rectNodeWidth = 10, rectNodeHeight = 10;
299 ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
300 rectNodeHeight, SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode);
301
302 const SkDOM::Node* root = dom.finishParsing();
303 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
304 if (innerSvg == nullptr) {
305 ERRORF(reporter, "inner svg element not found");
306 return;
307 }
308
309 const SkDOM::Node *patternNode, *imageNode, *rectNode;
310 bool structureAppropriate =
311 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
312 REPORTER_ASSERT(reporter, structureAppropriate);
313
314 // the imageNode should always maintain its size.
315 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
316 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
317
318 // making the patternNode as large as the container prevents
319 // it from repeating.
320 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
321 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
322}
323
324DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
325 SkDOM dom;
326 SkPaint paint;
327 int imageWidth = 3, imageHeight = 3;
328 int rectWidth = 10, rectHeight = 10;
329 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
330 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
331
332 const SkDOM::Node* root = dom.finishParsing();
333
334 const SkDOM::Node *patternNode, *imageNode, *rectNode;
335 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
336 if (innerSvg == nullptr) {
337 ERRORF(reporter, "inner svg element not found");
338 return;
339 }
340 bool structureAppropriate =
341 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
342 REPORTER_ASSERT(reporter, structureAppropriate);
343
344 // the imageNode should always maintain its size.
345 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
346 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
347
348 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
349 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
350}
351
Anas AlJabri84dd1832018-07-31 15:53:05 -0700352DEF_TEST(SVGDevice_ColorFilters, reporter) {
353 SkDOM dom;
354 SkPaint paint;
355 paint.setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kSrcIn));
356 {
357 SkXMLParserWriter writer(dom.beginParsing());
358 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
359 SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
360 svgCanvas->drawRect(bounds, paint);
361 }
362 const SkDOM::Node* rootElement = dom.finishParsing();
363 ABORT_TEST(reporter, !rootElement, "root element not found");
364
365 const SkDOM::Node* filterElement = dom.getFirstChild(rootElement, "filter");
366 ABORT_TEST(reporter, !filterElement, "filter element not found");
367
368 const SkDOM::Node* floodElement = dom.getFirstChild(filterElement, "feFlood");
369 ABORT_TEST(reporter, !floodElement, "feFlood element not found");
370
371 const SkDOM::Node* compositeElement = dom.getFirstChild(filterElement, "feComposite");
372 ABORT_TEST(reporter, !compositeElement, "feComposite element not found");
373
374 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "width"), "100%") == 0);
375 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "height"), "100%") == 0);
376
377 REPORTER_ASSERT(reporter,
378 strcmp(dom.findAttr(floodElement, "flood-color"), "rgb(255,0,0)") == 0);
379 REPORTER_ASSERT(reporter, atoi(dom.findAttr(floodElement, "flood-opacity")) == 1);
380
381 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "in"), "flood") == 0);
382 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "operator"), "in") == 0);
383}
384
Hal Canaryff2742e2018-01-30 11:35:47 -0500385#endif