1 /*
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
8 #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
16 #include "SkBitmap.h"
17 #include "SkCanvas.h"
18 #include "SkColorFilter.h"
19 #include "SkData.h"
20 #include "SkImage.h"
21 #include "SkImageShader.h"
22 #include "SkParse.h"
23 #include "SkShader.h"
24 #include "SkStream.h"
25 #include "SkTo.h"
26 #include "Test.h"
27
28 #include <string.h>
29
30 #ifdef SK_XML
31
32 #include "SkDOM.h"
33 #include "SkSVGCanvas.h"
34 #include "SkXMLWriter.h"
35
36 #if 0
37 Using the new system where devices only gets glyphs causes this to fail because the font has no
38 glyph to unichar data.
39 namespace {
40
41
42 void 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) {
48 if (root == nullptr) {
49 ERRORF(reporter, "root element not found.");
50 return;
51 }
52
53 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
54 if (textElem == nullptr) {
55 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);
61 REPORTER_ASSERT(reporter, textNode != nullptr);
62 if (textNode != nullptr) {
63 REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
64 if (strcmp(expected, dom.getName(textNode)) != 0) {
65 SkDebugf("string fail %s == %s\n", expected, dom.getName(textNode));
66 }
67 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");
73 REPORTER_ASSERT(reporter, x != nullptr);
74 if (x != nullptr) {
75 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) {
84 if (xpos[i] != SkIntToScalar(expected[i])) {
85 SkDebugf("Bad xs %g == %g\n", xpos[i], SkIntToScalar(expected[i]));
86 }
87 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
88 }
89 }
90 }
91
92 const char* y = dom.findAttr(textElem, "y");
93 REPORTER_ASSERT(reporter, y != nullptr);
94 if (y != nullptr) {
95 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
110 void 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 SkFont font;
118 SkPoint offset = SkPoint::Make(10, 20);
119
120 {
121 SkXMLParserWriter writer(dom.beginParsing());
122 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
123 svgCanvas->drawSimpleText(txt, len, kUTF8_SkTextEncoding, offset.x(), offset.y(),
124 font, paint);
125 }
126 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
127
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());
135 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
136 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &xpos[0], offset.y(), font);
137 svgCanvas->drawTextBlob(blob, 0, 0, paint);
138 }
139 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
140
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());
148 auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &pos[0], font);
149 svgCanvas->drawTextBlob(blob, 0, 0, paint);
150 }
151 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
152 }
153
154 }
155
156
157 DEF_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 }
178 #endif
179
180
SetImageShader(SkPaint * paint,int imageWidth,int imageHeight,SkShader::TileMode xTile,SkShader::TileMode yTile)181 void 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.
FindImageShaderNodes(skiatest::Reporter * reporter,const SkDOM * dom,const SkDOM::Node * root,const SkDOM::Node ** patternOut,const SkDOM::Node ** imageOut,const SkDOM::Node ** rectOut)191 bool 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
ImageShaderTestSetup(SkDOM * dom,SkPaint * paint,int imageWidth,int imageHeight,int rectWidth,int rectHeight,SkShader::TileMode xTile,SkShader::TileMode yTile)230 void 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
DEF_TEST(SVGDevice_image_shader_norepeat,reporter)242 DEF_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
DEF_TEST(SVGDevice_image_shader_tilex,reporter)267 DEF_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
DEF_TEST(SVGDevice_image_shader_tiley,reporter)297 DEF_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
DEF_TEST(SVGDevice_image_shader_tileboth,reporter)327 DEF_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
DEF_TEST(SVGDevice_ColorFilters,reporter)355 DEF_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
388 #endif
389