blob: 4d1d414bf95745e2b2cc3067f483d999874b2220 [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
Alexander Midlash77e3afc2018-03-06 17:21:28 -08008#include "SkBitmap.h"
fmalita7a048692015-02-20 13:54:40 -08009#include "SkCanvas.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080010#include "SkData.h"
11#include "SkImage.h"
12#include "SkImageShader.h"
fmalita7a048692015-02-20 13:54:40 -080013#include "SkParse.h"
Alexander Midlash77e3afc2018-03-06 17:21:28 -080014#include "SkShader.h"
15#include "SkStream.h"
Hal Canary2a2f6752018-06-11 21:44:01 -040016#include "SkTo.h"
fmalita7a048692015-02-20 13:54:40 -080017#include "Test.h"
18
19#include <string.h>
20
Alexander Midlash77e3afc2018-03-06 17:21:28 -080021#ifdef SK_XML
22
Ben Wagner6ce482a2018-03-27 10:57:43 -040023#include "SkDOM.h"
24#include "SkSVGCanvas.h"
25#include "SkXMLWriter.h"
26
Herb Derby41f4f312018-06-06 17:45:53 +000027#if 0
28Using the new system where devices only gets glyphs causes this to fail because the font has no
29glyph to unichar data.
fmalita7a048692015-02-20 13:54:40 -080030namespace {
31
Alexander Midlash77e3afc2018-03-06 17:21:28 -080032
fmalita7a048692015-02-20 13:54:40 -080033void check_text_node(skiatest::Reporter* reporter,
34 const SkDOM& dom,
35 const SkDOM::Node* root,
36 const SkPoint& offset,
37 unsigned scalarsPerPos,
38 const char* expected) {
halcanary96fcdcc2015-08-27 07:41:13 -070039 if (root == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080040 ERRORF(reporter, "root element not found.");
41 return;
42 }
43
44 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
halcanary96fcdcc2015-08-27 07:41:13 -070045 if (textElem == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080046 ERRORF(reporter, "<text> element not found.");
47 return;
48 }
49 REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type);
50
51 const SkDOM::Node* textNode= dom.getFirstChild(textElem);
halcanary96fcdcc2015-08-27 07:41:13 -070052 REPORTER_ASSERT(reporter, textNode != nullptr);
53 if (textNode != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080054 REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
Herb Derby41f4f312018-06-06 17:45:53 +000055 if (strcmp(expected, dom.getName(textNode)) != 0) {
56 SkDebugf("string fail %s == %s\n", expected, dom.getName(textNode));
57 }
fmalita7a048692015-02-20 13:54:40 -080058 REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0);
59 }
60
61 int textLen = SkToInt(strlen(expected));
62
63 const char* x = dom.findAttr(textElem, "x");
halcanary96fcdcc2015-08-27 07:41:13 -070064 REPORTER_ASSERT(reporter, x != nullptr);
65 if (x != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080066 int xposCount = (scalarsPerPos < 1) ? 1 : textLen;
67 REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount);
68
69 SkAutoTMalloc<SkScalar> xpos(xposCount);
70 SkParse::FindScalars(x, xpos.get(), xposCount);
71 if (scalarsPerPos < 1) {
72 REPORTER_ASSERT(reporter, xpos[0] == offset.x());
73 } else {
74 for (int i = 0; i < xposCount; ++i) {
Herb Derby41f4f312018-06-06 17:45:53 +000075 if (xpos[i] != SkIntToScalar(expected[i])) {
76 SkDebugf("Bad xs %g == %g\n", xpos[i], SkIntToScalar(expected[i]));
77 }
fmalita7a048692015-02-20 13:54:40 -080078 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
79 }
80 }
81 }
82
83 const char* y = dom.findAttr(textElem, "y");
halcanary96fcdcc2015-08-27 07:41:13 -070084 REPORTER_ASSERT(reporter, y != nullptr);
85 if (y != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080086 int yposCount = (scalarsPerPos < 2) ? 1 : textLen;
87 REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount);
88
89 SkAutoTMalloc<SkScalar> ypos(yposCount);
90 SkParse::FindScalars(y, ypos.get(), yposCount);
91 if (scalarsPerPos < 2) {
92 REPORTER_ASSERT(reporter, ypos[0] == offset.y());
93 } else {
94 for (int i = 0; i < yposCount; ++i) {
95 REPORTER_ASSERT(reporter, ypos[i] == -SkIntToScalar(expected[i]));
96 }
97 }
98 }
99}
100
101void test_whitespace_pos(skiatest::Reporter* reporter,
102 const char* txt,
103 const char* expected) {
104 size_t len = strlen(txt);
105
106 SkDOM dom;
107 SkPaint paint;
108 SkPoint offset = SkPoint::Make(10, 20);
109
110 {
111 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600112 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800113 svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint);
114 }
Herb Derby41f4f312018-06-06 17:45:53 +0000115 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800116
117 {
118 SkAutoTMalloc<SkScalar> xpos(len);
119 for (int i = 0; i < SkToInt(len); ++i) {
120 xpos[i] = SkIntToScalar(txt[i]);
121 }
122
123 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600124 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800125 svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint);
126 }
Herb Derby41f4f312018-06-06 17:45:53 +0000127 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
fmalita7a048692015-02-20 13:54:40 -0800128
129 {
130 SkAutoTMalloc<SkPoint> pos(len);
131 for (int i = 0; i < SkToInt(len); ++i) {
132 pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
133 }
134
135 SkXMLParserWriter writer(dom.beginParsing());
Mike Reed5df49342016-11-12 08:06:55 -0600136 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800137 svgCanvas->drawPosText(txt, len, pos, paint);
138 }
139 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
140}
141
142}
143
Herb Derby41f4f312018-06-06 17:45:53 +0000144
fmalita7a048692015-02-20 13:54:40 -0800145DEF_TEST(SVGDevice_whitespace_pos, reporter) {
146 static const struct {
147 const char* tst_in;
148 const char* tst_out;
149 } tests[] = {
150 { "abcd" , "abcd" },
151 { "ab cd" , "ab cd" },
152 { "ab \t\t cd", "ab cd" },
153 { " abcd" , "abcd" },
154 { " abcd" , "abcd" },
155 { " \t\t abcd", "abcd" },
156 { "abcd " , "abcd " }, // we allow one trailing whitespace char
157 { "abcd " , "abcd " }, // because it makes no difference and
158 { "abcd\t " , "abcd\t" }, // simplifies the implementation
159 { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " },
160 };
161
162 for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
163 test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
164 }
165}
Herb Derby41f4f312018-06-06 17:45:53 +0000166#endif
Hal Canaryff2742e2018-01-30 11:35:47 -0500167
Alexander Midlash77e3afc2018-03-06 17:21:28 -0800168
169void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkShader::TileMode xTile,
170 SkShader::TileMode yTile) {
171 auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
172 paint->setShader(SkImageShader::Make(surface->makeImageSnapshot(), xTile, yTile, nullptr));
173}
174
175// Attempt to find the three nodes on which we have expectations:
176// the pattern node, the image within that pattern, and the rect which
177// uses the pattern as a fill.
178// returns false if not all nodes are found.
179bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
180 const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
181 const SkDOM::Node** rectOut) {
182 if (root == nullptr || dom == nullptr) {
183 ERRORF(reporter, "root element not found");
184 return false;
185 }
186
187
188 const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
189 if (rect == nullptr) {
190 ERRORF(reporter, "rect not found");
191 return false;
192 }
193 *rectOut = rect;
194
195 const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
196 if (defs == nullptr) {
197 ERRORF(reporter, "defs not found");
198 return false;
199 }
200
201 const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
202 if (pattern == nullptr) {
203 ERRORF(reporter, "pattern not found");
204 return false;
205 }
206 *patternOut = pattern;
207
208 const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
209 if (image == nullptr) {
210 ERRORF(reporter, "image not found");
211 return false;
212 }
213 *imageOut = image;
214
215 return true;
216}
217
218void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
219 int rectWidth, int rectHeight, SkShader::TileMode xTile,
220 SkShader::TileMode yTile) {
221 SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
222 SkXMLParserWriter writer(dom->beginParsing());
223 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
224
225 SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
226 svgCanvas->drawRect(bounds, *paint);
227}
228
229
230DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
231 SkDOM dom;
232 SkPaint paint;
233 int imageWidth = 3, imageHeight = 3;
234 int rectWidth = 10, rectHeight = 10;
235 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
236 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
237
238 const SkDOM::Node* root = dom.finishParsing();
239
240 const SkDOM::Node *patternNode, *imageNode, *rectNode;
241 bool structureAppropriate =
242 FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
243 REPORTER_ASSERT(reporter, structureAppropriate);
244
245 // the image should always maintain its size.
246 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
247 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
248
249 // making the pattern as large as the container prevents
250 // it from repeating.
251 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
252 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
253}
254
255DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
256 SkDOM dom;
257 SkPaint paint;
258 int imageWidth = 3, imageHeight = 3;
259 int rectWidth = 10, rectHeight = 10;
260 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
261 SkShader::kRepeat_TileMode, SkShader::kClamp_TileMode);
262
263 const SkDOM::Node* root = dom.finishParsing();
264 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
265 if (innerSvg == nullptr) {
266 ERRORF(reporter, "inner svg element not found");
267 return;
268 }
269
270 const SkDOM::Node *patternNode, *imageNode, *rectNode;
271 bool structureAppropriate =
272 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
273 REPORTER_ASSERT(reporter, structureAppropriate);
274
275 // the imageNode should always maintain its size.
276 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
277 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
278
279 // if the patternNode width matches the imageNode width,
280 // it will repeat in along the x axis.
281 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
282 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
283}
284
285DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
286 SkDOM dom;
287 SkPaint paint;
288 int imageNodeWidth = 3, imageNodeHeight = 3;
289 int rectNodeWidth = 10, rectNodeHeight = 10;
290 ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
291 rectNodeHeight, SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode);
292
293 const SkDOM::Node* root = dom.finishParsing();
294 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
295 if (innerSvg == nullptr) {
296 ERRORF(reporter, "inner svg element not found");
297 return;
298 }
299
300 const SkDOM::Node *patternNode, *imageNode, *rectNode;
301 bool structureAppropriate =
302 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
303 REPORTER_ASSERT(reporter, structureAppropriate);
304
305 // the imageNode should always maintain its size.
306 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
307 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
308
309 // making the patternNode as large as the container prevents
310 // it from repeating.
311 REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
312 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
313}
314
315DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
316 SkDOM dom;
317 SkPaint paint;
318 int imageWidth = 3, imageHeight = 3;
319 int rectWidth = 10, rectHeight = 10;
320 ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
321 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
322
323 const SkDOM::Node* root = dom.finishParsing();
324
325 const SkDOM::Node *patternNode, *imageNode, *rectNode;
326 const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
327 if (innerSvg == nullptr) {
328 ERRORF(reporter, "inner svg element not found");
329 return;
330 }
331 bool structureAppropriate =
332 FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
333 REPORTER_ASSERT(reporter, structureAppropriate);
334
335 // the imageNode should always maintain its size.
336 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
337 REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
338
339 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
340 REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
341}
342
Hal Canaryff2742e2018-01-30 11:35:47 -0500343#endif