blob: bdd832c43ba95985edf7cb1d582ad393b07545bf [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
Hal Canaryff2742e2018-01-30 11:35:47 -05008
Alexander Midlash77e3afc2018-03-06 17:21:28 -08009#include "SkBitmap.h"
fmalita7a048692015-02-20 13:54:40 -080010#include "SkCanvas.h"
fmalita7a048692015-02-20 13:54:40 -080011#include "SkDOM.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080012#include "SkData.h"
13#include "SkImage.h"
14#include "SkImageShader.h"
fmalita7a048692015-02-20 13:54:40 -080015#include "SkParse.h"
fmalita7a048692015-02-20 13:54:40 -080016#include "SkSVGCanvas.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080017#include "SkShader.h"
18#include "SkStream.h"
fmalita7a048692015-02-20 13:54:40 -080019#include "SkXMLWriter.h"
20#include "Test.h"
21
22#include <string.h>
23
Alexander Midlash77e3afc2018-03-06 17:21:28 -080024#ifdef SK_XML
25
fmalita7a048692015-02-20 13:54:40 -080026namespace {
27
Alexander Midlash77e3afc2018-03-06 17:21:28 -080028
fmalita7a048692015-02-20 13:54:40 -080029void check_text_node(skiatest::Reporter* reporter,
30 const SkDOM& dom,
31 const SkDOM::Node* root,
32 const SkPoint& offset,
33 unsigned scalarsPerPos,
34 const char* expected) {
halcanary96fcdcc2015-08-27 07:41:13 -070035 if (root == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080036 ERRORF(reporter, "root element not found.");
37 return;
38 }
39
40 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
halcanary96fcdcc2015-08-27 07:41:13 -070041 if (textElem == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080042 ERRORF(reporter, "<text> element not found.");
43 return;
44 }
45 REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type);
46
47 const SkDOM::Node* textNode= dom.getFirstChild(textElem);
halcanary96fcdcc2015-08-27 07:41:13 -070048 REPORTER_ASSERT(reporter, textNode != nullptr);
49 if (textNode != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080050 REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
51 REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0);
52 }
53
54 int textLen = SkToInt(strlen(expected));
55
56 const char* x = dom.findAttr(textElem, "x");
halcanary96fcdcc2015-08-27 07:41:13 -070057 REPORTER_ASSERT(reporter, x != nullptr);
58 if (x != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080059 int xposCount = (scalarsPerPos < 1) ? 1 : textLen;
60 REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount);
61
62 SkAutoTMalloc<SkScalar> xpos(xposCount);
63 SkParse::FindScalars(x, xpos.get(), xposCount);
64 if (scalarsPerPos < 1) {
65 REPORTER_ASSERT(reporter, xpos[0] == offset.x());
66 } else {
67 for (int i = 0; i < xposCount; ++i) {
68 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
69 }
70 }
71 }
72
73 const char* y = dom.findAttr(textElem, "y");
halcanary96fcdcc2015-08-27 07:41:13 -070074 REPORTER_ASSERT(reporter, y != nullptr);
75 if (y != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080076 int yposCount = (scalarsPerPos < 2) ? 1 : textLen;
77 REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount);
78
79 SkAutoTMalloc<SkScalar> ypos(yposCount);
80 SkParse::FindScalars(y, ypos.get(), yposCount);
81 if (scalarsPerPos < 2) {
82 REPORTER_ASSERT(reporter, ypos[0] == offset.y());
83 } else {
84 for (int i = 0; i < yposCount; ++i) {
85 REPORTER_ASSERT(reporter, ypos[i] == -SkIntToScalar(expected[i]));
86 }
87 }
88 }
89}
90
91void test_whitespace_pos(skiatest::Reporter* reporter,
92 const char* txt,
93 const char* expected) {
94 size_t len = strlen(txt);
95
96 SkDOM dom;
97 SkPaint paint;
98 SkPoint offset = SkPoint::Make(10, 20);
99
100 {
101 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600102 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800103 svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint);
104 }
105 check_text_node(reporter, dom, dom.finishParsing(), offset, 0, expected);
106
107 {
108 SkAutoTMalloc<SkScalar> xpos(len);
109 for (int i = 0; i < SkToInt(len); ++i) {
110 xpos[i] = SkIntToScalar(txt[i]);
111 }
112
113 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600114 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800115 svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint);
116 }
117 check_text_node(reporter, dom, dom.finishParsing(), offset, 1, expected);
118
119 {
120 SkAutoTMalloc<SkPoint> pos(len);
121 for (int i = 0; i < SkToInt(len); ++i) {
122 pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
123 }
124
125 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600126 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800127 svgCanvas->drawPosText(txt, len, pos, paint);
128 }
129 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
130}
131
132}
133
134DEF_TEST(SVGDevice_whitespace_pos, reporter) {
135 static const struct {
136 const char* tst_in;
137 const char* tst_out;
138 } tests[] = {
139 { "abcd" , "abcd" },
140 { "ab cd" , "ab cd" },
141 { "ab \t\t cd", "ab cd" },
142 { " abcd" , "abcd" },
143 { " abcd" , "abcd" },
144 { " \t\t abcd", "abcd" },
145 { "abcd " , "abcd " }, // we allow one trailing whitespace char
146 { "abcd " , "abcd " }, // because it makes no difference and
147 { "abcd\t " , "abcd\t" }, // simplifies the implementation
148 { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " },
149 };
150
151 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
152 test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
153 }
154}
Hal Canaryff2742e2018-01-30 11:35:47 -0500155
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800156
157void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkShader::TileMode xTile,
158 SkShader::TileMode yTile) {
159 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
160 paint->setShader(SkImageShader::Make(surface->makeImageSnapshot(), xTile, yTile, nullptr));
161}
162
163// Attempt to find the three nodes on which we have expectations:
164// the pattern node, the image within that pattern, and the rect which
165// uses the pattern as a fill.
166// returns false if not all nodes are found.
167bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
168 const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
169 const SkDOM::Node** rectOut) {
170 if (root == nullptr || dom == nullptr) {
171 ERRORF(reporter, "root element not found");
172 return false;
173 }
174
175
176 const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
177 if (rect == nullptr) {
178 ERRORF(reporter, "rect not found");
179 return false;
180 }
181 *rectOut = rect;
182
183 const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
184 if (defs == nullptr) {
185 ERRORF(reporter, "defs not found");
186 return false;
187 }
188
189 const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
190 if (pattern == nullptr) {
191 ERRORF(reporter, "pattern not found");
192 return false;
193 }
194 *patternOut = pattern;
195
196 const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
197 if (image == nullptr) {
198 ERRORF(reporter, "image not found");
199 return false;
200 }
201 *imageOut = image;
202
203 return true;
204}
205
206void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
207 int rectWidth, int rectHeight, SkShader::TileMode xTile,
208 SkShader::TileMode yTile) {
209 SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
210 SkXMLParserWriter writer(dom->beginParsing());
211 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
212
213 SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
214 svgCanvas->drawRect(bounds, *paint);
215}
216
217
218DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
219 SkDOM dom;
220 SkPaint paint;
221 int imageWidth = 3, imageHeight = 3;
222 int rectWidth = 10, rectHeight = 10;
223 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
224 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
225
226 const SkDOM::Node* root = dom.finishParsing();
227
228 const SkDOM::Node *patternNode, *imageNode, *rectNode;
229 bool structureAppropriate =
230 FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
231 REPORTER_ASSERT(reporter, structureAppropriate);
232
233 // the image should always maintain its size.
234 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
235 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
236
237 // making the pattern as large as the container prevents
238 // it from repeating.
239 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
240 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
241}
242
243DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
244 SkDOM dom;
245 SkPaint paint;
246 int imageWidth = 3, imageHeight = 3;
247 int rectWidth = 10, rectHeight = 10;
248 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
249 SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode);
250
251 const SkDOM::Node* root = dom.finishParsing();
252 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
253 if (innerSvg == nullptr) {
254 ERRORF(reporter, "inner svg element not found");
255 return;
256 }
257
258 const SkDOM::Node *patternNode, *imageNode, *rectNode;
259 bool structureAppropriate =
260 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
261 REPORTER_ASSERT(reporter, structureAppropriate);
262
263 // the imageNode should always maintain its size.
264 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
265 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
266
267 // if the patternNode width matches the imageNode width,
268 // it will repeat in along the x axis.
269 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
270 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
271}
272
273DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
274 SkDOM dom;
275 SkPaint paint;
276 int imageNodeWidth = 3, imageNodeHeight = 3;
277 int rectNodeWidth = 10, rectNodeHeight = 10;
278 ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
279 rectNodeHeight, SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode);
280
281 const SkDOM::Node* root = dom.finishParsing();
282 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
283 if (innerSvg == nullptr) {
284 ERRORF(reporter, "inner svg element not found");
285 return;
286 }
287
288 const SkDOM::Node *patternNode, *imageNode, *rectNode;
289 bool structureAppropriate =
290 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
291 REPORTER_ASSERT(reporter, structureAppropriate);
292
293 // the imageNode should always maintain its size.
294 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
295 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
296
297 // making the patternNode as large as the container prevents
298 // it from repeating.
299 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
300 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
301}
302
303DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
304 SkDOM dom;
305 SkPaint paint;
306 int imageWidth = 3, imageHeight = 3;
307 int rectWidth = 10, rectHeight = 10;
308 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
309 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
310
311 const SkDOM::Node* root = dom.finishParsing();
312
313 const SkDOM::Node *patternNode, *imageNode, *rectNode;
314 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
315 if (innerSvg == nullptr) {
316 ERRORF(reporter, "inner svg element not found");
317 return;
318 }
319 bool structureAppropriate =
320 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
321 REPORTER_ASSERT(reporter, structureAppropriate);
322
323 // the imageNode should always maintain its size.
324 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
325 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
326
327 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
328 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
329}
330
Hal Canaryff2742e2018-01-30 11:35:47 -0500331#endif