blob: 0a02f7c3fd7c6ae520e79559f515e079b0f3696b [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;
Mike Reed212e9062018-12-25 17:35:49 -0500117 SkFont font;
fmalita7a048692015-02-20 13:54:40 -0800118 SkPoint offset = SkPoint::Make(10, 20);
119
120 {
121 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600122 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
Mike Reed212e9062018-12-25 17:35:49 -0500123 svgCanvas->drawSimpleText(txt, len, kUTF8_SkTextEncoding, offset.x(), offset.y(),
124 font, paint);
fmalita7a048692015-02-20 13:54:40 -0800125 }
Herb Derby41f4f312018-06-06 17:45:53 +0000126 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800127
128 {
129 SkAutoTMalloc<SkScalar> xpos(len);
130 for (int i = 0; i < SkToInt(len); ++i) {
131 xpos[i] = SkIntToScalar(txt[i]);
132 }
133
134 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600135 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
Mike Reed212e9062018-12-25 17:35:49 -0500136 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &xpos[0], offset.y(), font);
137 svgCanvas->drawTextBlob(blob, 0, 0, paint);
fmalita7a048692015-02-20 13:54:40 -0800138 }
Herb Derby41f4f312018-06-06 17:45:53 +0000139 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800140
141 {
142 SkAutoTMalloc<SkPoint> pos(len);
143 for (int i = 0; i < SkToInt(len); ++i) {
144 pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
145 }
146
147 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed212e9062018-12-25 17:35:49 -0500148 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &pos[0], font);
149 svgCanvas->drawTextBlob(blob, 0, 0, paint);
fmalita7a048692015-02-20 13:54:40 -0800150 }
151 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
152}
153
154}
155
Herb Derby41f4f312018-06-06 17:45:53 +0000156
fmalita7a048692015-02-20 13:54:40 -0800157DEF_TEST(SVGDevice_whitespace_pos, reporter) {
158 static const struct {
159 const char* tst_in;
160 const char* tst_out;
161 } tests[] = {
162 { "abcd" , "abcd" },
163 { "ab cd" , "ab cd" },
164 { "ab \t\t cd", "ab cd" },
165 { " abcd" , "abcd" },
166 { " abcd" , "abcd" },
167 { " \t\t abcd", "abcd" },
168 { "abcd " , "abcd " }, // we allow one trailing whitespace char
169 { "abcd " , "abcd " }, // because it makes no difference and
170 { "abcd\t " , "abcd\t" }, // simplifies the implementation
171 { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " },
172 };
173
174 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
175 test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
176 }
177}
Herb Derby41f4f312018-06-06 17:45:53 +0000178#endif
Hal Canaryff2742e2018-01-30 11:35:47 -0500179
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800180
181void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkShader::TileMode xTile,
182 SkShader::TileMode yTile) {
183 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
184 paint->setShader(SkImageShader::Make(surface->makeImageSnapshot(), xTile, yTile, nullptr));
185}
186
187// Attempt to find the three nodes on which we have expectations:
188// the pattern node, the image within that pattern, and the rect which
189// uses the pattern as a fill.
190// returns false if not all nodes are found.
191bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
192 const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
193 const SkDOM::Node** rectOut) {
194 if (root == nullptr || dom == nullptr) {
195 ERRORF(reporter, "root element not found");
196 return false;
197 }
198
199
200 const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
201 if (rect == nullptr) {
202 ERRORF(reporter, "rect not found");
203 return false;
204 }
205 *rectOut = rect;
206
207 const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
208 if (defs == nullptr) {
209 ERRORF(reporter, "defs not found");
210 return false;
211 }
212
213 const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
214 if (pattern == nullptr) {
215 ERRORF(reporter, "pattern not found");
216 return false;
217 }
218 *patternOut = pattern;
219
220 const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
221 if (image == nullptr) {
222 ERRORF(reporter, "image not found");
223 return false;
224 }
225 *imageOut = image;
226
227 return true;
228}
229
230void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
231 int rectWidth, int rectHeight, SkShader::TileMode xTile,
232 SkShader::TileMode yTile) {
233 SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
234 SkXMLParserWriter writer(dom->beginParsing());
235 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
236
237 SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
238 svgCanvas->drawRect(bounds, *paint);
239}
240
241
242DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
243 SkDOM dom;
244 SkPaint paint;
245 int imageWidth = 3, imageHeight = 3;
246 int rectWidth = 10, rectHeight = 10;
247 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
248 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
249
250 const SkDOM::Node* root = dom.finishParsing();
251
252 const SkDOM::Node *patternNode, *imageNode, *rectNode;
253 bool structureAppropriate =
254 FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
255 REPORTER_ASSERT(reporter, structureAppropriate);
256
257 // the image should always maintain its size.
258 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
259 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
260
261 // making the pattern as large as the container prevents
262 // it from repeating.
263 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
264 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
265}
266
267DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
268 SkDOM dom;
269 SkPaint paint;
270 int imageWidth = 3, imageHeight = 3;
271 int rectWidth = 10, rectHeight = 10;
272 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
273 SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode);
274
275 const SkDOM::Node* root = dom.finishParsing();
276 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
277 if (innerSvg == nullptr) {
278 ERRORF(reporter, "inner svg element not found");
279 return;
280 }
281
282 const SkDOM::Node *patternNode, *imageNode, *rectNode;
283 bool structureAppropriate =
284 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
285 REPORTER_ASSERT(reporter, structureAppropriate);
286
287 // the imageNode should always maintain its size.
288 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
289 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
290
291 // if the patternNode width matches the imageNode width,
292 // it will repeat in along the x axis.
293 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
294 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
295}
296
297DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
298 SkDOM dom;
299 SkPaint paint;
300 int imageNodeWidth = 3, imageNodeHeight = 3;
301 int rectNodeWidth = 10, rectNodeHeight = 10;
302 ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
303 rectNodeHeight, SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode);
304
305 const SkDOM::Node* root = dom.finishParsing();
306 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
307 if (innerSvg == nullptr) {
308 ERRORF(reporter, "inner svg element not found");
309 return;
310 }
311
312 const SkDOM::Node *patternNode, *imageNode, *rectNode;
313 bool structureAppropriate =
314 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
315 REPORTER_ASSERT(reporter, structureAppropriate);
316
317 // the imageNode should always maintain its size.
318 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
319 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
320
321 // making the patternNode as large as the container prevents
322 // it from repeating.
323 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
324 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
325}
326
327DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
328 SkDOM dom;
329 SkPaint paint;
330 int imageWidth = 3, imageHeight = 3;
331 int rectWidth = 10, rectHeight = 10;
332 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
333 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
334
335 const SkDOM::Node* root = dom.finishParsing();
336
337 const SkDOM::Node *patternNode, *imageNode, *rectNode;
338 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
339 if (innerSvg == nullptr) {
340 ERRORF(reporter, "inner svg element not found");
341 return;
342 }
343 bool structureAppropriate =
344 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
345 REPORTER_ASSERT(reporter, structureAppropriate);
346
347 // the imageNode should always maintain its size.
348 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
349 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
350
351 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
352 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
353}
354
Anas AlJabri84dd1832018-07-31 15:53:05 -0700355DEF_TEST(SVGDevice_ColorFilters, reporter) {
356 SkDOM dom;
357 SkPaint paint;
358 paint.setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kSrcIn));
359 {
360 SkXMLParserWriter writer(dom.beginParsing());
361 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
362 SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
363 svgCanvas->drawRect(bounds, paint);
364 }
365 const SkDOM::Node* rootElement = dom.finishParsing();
366 ABORT_TEST(reporter, !rootElement, "root element not found");
367
368 const SkDOM::Node* filterElement = dom.getFirstChild(rootElement, "filter");
369 ABORT_TEST(reporter, !filterElement, "filter element not found");
370
371 const SkDOM::Node* floodElement = dom.getFirstChild(filterElement, "feFlood");
372 ABORT_TEST(reporter, !floodElement, "feFlood element not found");
373
374 const SkDOM::Node* compositeElement = dom.getFirstChild(filterElement, "feComposite");
375 ABORT_TEST(reporter, !compositeElement, "feComposite element not found");
376
377 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "width"), "100%") == 0);
378 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "height"), "100%") == 0);
379
380 REPORTER_ASSERT(reporter,
381 strcmp(dom.findAttr(floodElement, "flood-color"), "rgb(255,0,0)") == 0);
382 REPORTER_ASSERT(reporter, atoi(dom.findAttr(floodElement, "flood-opacity")) == 1);
383
384 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "in"), "flood") == 0);
385 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "operator"), "in") == 0);
386}
387
Hal Canaryff2742e2018-01-30 11:35:47 -0500388#endif