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 #include "include/core/SkTypes.h"
9 #ifdef SK_XML
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkData.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkPathEffect.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkStream.h"
19 #include "include/core/SkTextBlob.h"
20 #include "include/effects/SkDashPathEffect.h"
21 #include "include/private/base/SkTo.h"
22 #include "include/svg/SkSVGCanvas.h"
23 #include "include/utils/SkParse.h"
24 #include "src/shaders/SkImageShader.h"
25 #include "src/svg/SkSVGDevice.h"
26 #include "src/xml/SkDOM.h"
27 #include "src/xml/SkXMLWriter.h"
28 #include "tests/Test.h"
29 #include "tools/ToolUtils.h"
30 
31 #include <string>
32 
33 using namespace skia_private;
34 
35 #define ABORT_TEST(r, cond, ...)                                   \
36     do {                                                           \
37         if (cond) {                                                \
38             REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \
39             return;                                                \
40         }                                                          \
41     } while (0)
42 
43 
MakeDOMCanvas(SkDOM * dom,uint32_t flags=0)44 static std::unique_ptr<SkCanvas> MakeDOMCanvas(SkDOM* dom, uint32_t flags = 0) {
45     auto svgDevice = SkSVGDevice::Make(SkISize::Make(100, 100),
46                                        std::make_unique<SkXMLParserWriter>(dom->beginParsing()),
47                                        flags);
48     return svgDevice ? std::make_unique<SkCanvas>(svgDevice)
49                      : nullptr;
50 }
51 
52 namespace {
53 
54 
check_text_node(skiatest::Reporter * reporter,const SkDOM & dom,const SkDOM::Node * root,const SkPoint & offset,unsigned scalarsPerPos,const char * txt,const char * expected)55 void check_text_node(skiatest::Reporter* reporter,
56                      const SkDOM& dom,
57                      const SkDOM::Node* root,
58                      const SkPoint& offset,
59                      unsigned scalarsPerPos,
60                      const char* txt,
61                      const char* expected) {
62     if (root == nullptr) {
63         ERRORF(reporter, "root element not found.");
64         return;
65     }
66 
67     const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
68     if (textElem == nullptr) {
69         ERRORF(reporter, "<text> element not found.");
70         return;
71     }
72     REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type);
73 
74     const SkDOM::Node* textNode= dom.getFirstChild(textElem);
75     REPORTER_ASSERT(reporter, textNode != nullptr);
76     if (textNode != nullptr) {
77         REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type);
78         REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0);
79     }
80 
81     int textLen = SkToInt(strlen(expected));
82 
83     const char* x = dom.findAttr(textElem, "x");
84     REPORTER_ASSERT(reporter, x != nullptr);
85     if (x != nullptr) {
86         int xposCount = textLen;
87         REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount);
88 
89         AutoTMalloc<SkScalar> xpos(xposCount);
90         SkParse::FindScalars(x, xpos.get(), xposCount);
91         if (scalarsPerPos < 1) {
92             // For default-positioned text, we cannot make any assumptions regarding
93             // the first glyph position when the string has leading whitespace (to be stripped).
94             if (txt[0] != ' ' && txt[0] != '\t') {
95                 REPORTER_ASSERT(reporter, xpos[0] == offset.x());
96             }
97         } else {
98             for (int i = 0; i < xposCount; ++i) {
99                 REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i]));
100             }
101         }
102     }
103 
104     const char* y = dom.findAttr(textElem, "y");
105     REPORTER_ASSERT(reporter, y != nullptr);
106     if (y != nullptr) {
107         int yposCount = (scalarsPerPos < 2) ? 1 : textLen;
108         REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount);
109 
110         AutoTMalloc<SkScalar> ypos(yposCount);
111         SkParse::FindScalars(y, ypos.get(), yposCount);
112         if (scalarsPerPos < 2) {
113             REPORTER_ASSERT(reporter, ypos[0] == offset.y());
114         } else {
115             for (int i = 0; i < yposCount; ++i) {
116                 REPORTER_ASSERT(reporter, ypos[i] == 150 - SkIntToScalar(expected[i]));
117             }
118         }
119     }
120 }
121 
test_whitespace_pos(skiatest::Reporter * reporter,const char * txt,const char * expected)122 void test_whitespace_pos(skiatest::Reporter* reporter,
123                          const char* txt,
124                          const char* expected) {
125     size_t len = strlen(txt);
126 
127     SkDOM dom;
128     SkPaint paint;
129     SkFont font(ToolUtils::create_portable_typeface());
130     SkPoint offset = SkPoint::Make(10, 20);
131 
132     {
133         MakeDOMCanvas(&dom)->drawSimpleText(txt, len, SkTextEncoding::kUTF8,
134                                             offset.x(), offset.y(), font, paint);
135     }
136     check_text_node(reporter, dom, dom.finishParsing(), offset, 0, txt, expected);
137 
138     {
139         AutoTMalloc<SkScalar> xpos(len);
140         for (int i = 0; i < SkToInt(len); ++i) {
141             xpos[i] = SkIntToScalar(txt[i]);
142         }
143 
144         auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &xpos[0], offset.y(), font);
145         MakeDOMCanvas(&dom)->drawTextBlob(blob, 0, 0, paint);
146     }
147     check_text_node(reporter, dom, dom.finishParsing(), offset, 1, txt, expected);
148 
149     {
150         AutoTMalloc<SkPoint> pos(len);
151         for (int i = 0; i < SkToInt(len); ++i) {
152             pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), 150 - SkIntToScalar(txt[i]));
153         }
154 
155         auto blob = SkTextBlob::MakeFromPosText(txt, len, &pos[0], font);
156         MakeDOMCanvas(&dom)->drawTextBlob(blob, 0, 0, paint);
157     }
158     check_text_node(reporter, dom, dom.finishParsing(), offset, 2, txt, expected);
159 }
160 
161 } // namespace
162 
DEF_TEST(SVGDevice_whitespace_pos,reporter)163 DEF_TEST(SVGDevice_whitespace_pos, reporter) {
164     static const struct {
165         const char* tst_in;
166         const char* tst_out;
167     } tests[] = {
168         { "abcd"      , "abcd" },
169         { "ab cd"     , "ab cd" },
170         { "ab \t\t cd", "ab cd" },
171         { " abcd"     , "abcd" },
172         { "  abcd"    , "abcd" },
173         { " \t\t abcd", "abcd" },
174         { "abcd "     , "abcd " }, // we allow one trailing whitespace char
175         { "abcd  "    , "abcd " }, // because it makes no difference and
176         { "abcd\t  "  , "abcd " }, // simplifies the implementation
177         { "\t\t  \t ab \t\t  \t cd \t\t   \t  ", "ab cd " },
178     };
179 
180     for (unsigned i = 0; i < std::size(tests); ++i) {
181         test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
182     }
183 }
184 
SetImageShader(SkPaint * paint,int imageWidth,int imageHeight,SkTileMode xTile,SkTileMode yTile)185 void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkTileMode xTile,
186                     SkTileMode yTile) {
187     auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight);
188     paint->setShader(surface->makeImageSnapshot()->makeShader(xTile, yTile, SkSamplingOptions()));
189 }
190 
191 // Attempt to find the three nodes on which we have expectations:
192 // the pattern node, the image within that pattern, and the rect which
193 // uses the pattern as a fill.
194 // 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)195 bool FindImageShaderNodes(skiatest::Reporter* reporter, const SkDOM* dom, const SkDOM::Node* root,
196                           const SkDOM::Node** patternOut, const SkDOM::Node** imageOut,
197                           const SkDOM::Node** rectOut) {
198     if (root == nullptr || dom == nullptr) {
199         ERRORF(reporter, "root element not found");
200         return false;
201     }
202 
203 
204     const SkDOM::Node* rect = dom->getFirstChild(root, "rect");
205     if (rect == nullptr) {
206         ERRORF(reporter, "rect not found");
207         return false;
208     }
209     *rectOut = rect;
210 
211     const SkDOM::Node* defs = dom->getFirstChild(root, "defs");
212     if (defs == nullptr) {
213         ERRORF(reporter, "defs not found");
214         return false;
215     }
216 
217     const SkDOM::Node* pattern = dom->getFirstChild(defs, "pattern");
218     if (pattern == nullptr) {
219         ERRORF(reporter, "pattern not found");
220         return false;
221     }
222     *patternOut = pattern;
223 
224     const SkDOM::Node* image = dom->getFirstChild(pattern, "image");
225     if (image == nullptr) {
226         ERRORF(reporter, "image not found");
227         return false;
228     }
229     *imageOut = image;
230 
231     return true;
232 }
233 
ImageShaderTestSetup(SkDOM * dom,SkPaint * paint,int imageWidth,int imageHeight,int rectWidth,int rectHeight,SkTileMode xTile,SkTileMode yTile)234 void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight,
235                           int rectWidth, int rectHeight, SkTileMode xTile, SkTileMode yTile) {
236     SetImageShader(paint, imageWidth, imageHeight, xTile, yTile);
237     auto svgCanvas = MakeDOMCanvas(dom);
238 
239     SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)};
240     svgCanvas->drawRect(bounds, *paint);
241 }
242 
243 
DEF_TEST(SVGDevice_image_shader_norepeat,reporter)244 DEF_TEST(SVGDevice_image_shader_norepeat, reporter) {
245     SkDOM dom;
246     SkPaint paint;
247     int imageWidth = 3, imageHeight = 3;
248     int rectWidth = 10, rectHeight = 10;
249     ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
250                          SkTileMode::kClamp, SkTileMode::kClamp);
251 
252     const SkDOM::Node* root = dom.finishParsing();
253 
254     const SkDOM::Node *patternNode, *imageNode, *rectNode;
255     bool structureAppropriate =
256             FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode);
257     REPORTER_ASSERT(reporter, structureAppropriate);
258 
259     // the image should always maintain its size.
260     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
261     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
262 
263     // making the pattern as large as the container prevents
264     // it from repeating.
265     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
266     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
267 }
268 
DEF_TEST(SVGDevice_image_shader_tilex,reporter)269 DEF_TEST(SVGDevice_image_shader_tilex, reporter) {
270     SkDOM dom;
271     SkPaint paint;
272     int imageWidth = 3, imageHeight = 3;
273     int rectWidth = 10, rectHeight = 10;
274     ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
275                          SkTileMode::kRepeat, SkTileMode::kClamp);
276 
277     const SkDOM::Node* root = dom.finishParsing();
278     const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
279     if (innerSvg == nullptr) {
280         ERRORF(reporter, "inner svg element not found");
281         return;
282     }
283 
284     const SkDOM::Node *patternNode, *imageNode, *rectNode;
285     bool structureAppropriate =
286             FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
287     REPORTER_ASSERT(reporter, structureAppropriate);
288 
289     // the imageNode should always maintain its size.
290     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
291     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
292 
293     // if the patternNode width matches the imageNode width,
294     // it will repeat in along the x axis.
295     REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
296     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0);
297 }
298 
DEF_TEST(SVGDevice_image_shader_tiley,reporter)299 DEF_TEST(SVGDevice_image_shader_tiley, reporter) {
300     SkDOM dom;
301     SkPaint paint;
302     int imageNodeWidth = 3, imageNodeHeight = 3;
303     int rectNodeWidth = 10, rectNodeHeight = 10;
304     ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth,
305                          rectNodeHeight, SkTileMode::kClamp, SkTileMode::kRepeat);
306 
307     const SkDOM::Node* root = dom.finishParsing();
308     const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
309     if (innerSvg == nullptr) {
310         ERRORF(reporter, "inner svg element not found");
311         return;
312     }
313 
314     const SkDOM::Node *patternNode, *imageNode, *rectNode;
315     bool structureAppropriate =
316             FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
317     REPORTER_ASSERT(reporter, structureAppropriate);
318 
319     // the imageNode should always maintain its size.
320     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth);
321     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight);
322 
323     // making the patternNode as large as the container prevents
324     // it from repeating.
325     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0);
326     REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight);
327 }
328 
DEF_TEST(SVGDevice_image_shader_tileboth,reporter)329 DEF_TEST(SVGDevice_image_shader_tileboth, reporter) {
330     SkDOM dom;
331     SkPaint paint;
332     int imageWidth = 3, imageHeight = 3;
333     int rectWidth = 10, rectHeight = 10;
334     ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight,
335                          SkTileMode::kRepeat, SkTileMode::kRepeat);
336 
337     const SkDOM::Node* root = dom.finishParsing();
338 
339     const SkDOM::Node *patternNode, *imageNode, *rectNode;
340     const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg");
341     if (innerSvg == nullptr) {
342         ERRORF(reporter, "inner svg element not found");
343         return;
344     }
345     bool structureAppropriate =
346             FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode);
347     REPORTER_ASSERT(reporter, structureAppropriate);
348 
349     // the imageNode should always maintain its size.
350     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth);
351     REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight);
352 
353     REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth);
354     REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight);
355 }
356 
DEF_TEST(SVGDevice_ColorFilters,reporter)357 DEF_TEST(SVGDevice_ColorFilters, reporter) {
358     SkDOM dom;
359     SkPaint paint;
360     paint.setColorFilter(SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kSrcIn));
361     {
362         auto svgCanvas = MakeDOMCanvas(&dom);
363         SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
364         svgCanvas->drawRect(bounds, paint);
365     }
366     const SkDOM::Node* rootElement = dom.finishParsing();
367     ABORT_TEST(reporter, !rootElement, "root element not found");
368 
369     const SkDOM::Node* filterElement = dom.getFirstChild(rootElement, "filter");
370     ABORT_TEST(reporter, !filterElement, "filter element not found");
371 
372     const SkDOM::Node* floodElement = dom.getFirstChild(filterElement, "feFlood");
373     ABORT_TEST(reporter, !floodElement, "feFlood element not found");
374 
375     const SkDOM::Node* compositeElement = dom.getFirstChild(filterElement, "feComposite");
376     ABORT_TEST(reporter, !compositeElement, "feComposite element not found");
377 
378     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "width"), "100%") == 0);
379     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "height"), "100%") == 0);
380 
381     REPORTER_ASSERT(reporter,
382                     strcmp(dom.findAttr(floodElement, "flood-color"), "red") == 0);
383     REPORTER_ASSERT(reporter, atoi(dom.findAttr(floodElement, "flood-opacity")) == 1);
384 
385     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "in"), "flood") == 0);
386     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "operator"), "in") == 0);
387 }
388 
DEF_TEST(SVGDevice_textpath,reporter)389 DEF_TEST(SVGDevice_textpath, reporter) {
390     SkDOM dom;
391     SkFont font(ToolUtils::create_portable_typeface());
392     SkPaint paint;
393 
394     auto check_text = [&](uint32_t flags, bool expect_path) {
395         // By default, we emit <text> nodes.
396         {
397             auto svgCanvas = MakeDOMCanvas(&dom, flags);
398             svgCanvas->drawString("foo", 100, 100, font, paint);
399         }
400         const auto* rootElement = dom.finishParsing();
401         REPORTER_ASSERT(reporter, rootElement, "root element not found");
402         const auto* textElement = dom.getFirstChild(rootElement, "text");
403         REPORTER_ASSERT(reporter, !!textElement == !expect_path, "unexpected text element");
404         const auto* pathElement = dom.getFirstChild(rootElement, "path");
405         REPORTER_ASSERT(reporter, !!pathElement == expect_path, "unexpected path element");
406     };
407 
408     // By default, we emit <text> nodes.
409     check_text(0, /*expect_path=*/false);
410 
411     // With kConvertTextToPaths_Flag, we emit <path> nodes.
412     check_text(SkSVGCanvas::kConvertTextToPaths_Flag, /*expect_path=*/true);
413 
414     // We also use paths in the presence of path effects.
415     SkScalar intervals[] = {10, 5};
416     paint.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
417     check_text(0, /*expect_path=*/true);
418 }
419 
DEF_TEST(SVGDevice_fill_stroke,reporter)420 DEF_TEST(SVGDevice_fill_stroke, reporter) {
421     struct {
422         SkColor        color;
423         SkPaint::Style style;
424         const char*    expected_fill;
425         const char*    expected_stroke;
426     } gTests[] = {
427         { SK_ColorBLACK, SkPaint::kFill_Style  , nullptr, nullptr },
428         { SK_ColorBLACK, SkPaint::kStroke_Style, "none" , "black" },
429         { SK_ColorRED  , SkPaint::kFill_Style  , "red"  , nullptr },
430         { SK_ColorRED  , SkPaint::kStroke_Style, "none" , "red"   },
431     };
432 
433     for (const auto& tst : gTests) {
434         SkPaint p;
435         p.setColor(tst.color);
436         p.setStyle(tst.style);
437 
438         SkDOM dom;
439         {
440             MakeDOMCanvas(&dom)->drawRect(SkRect::MakeWH(100, 100), p);
441         }
442 
443         const auto* root = dom.finishParsing();
444         REPORTER_ASSERT(reporter, root, "root element not found");
445         const auto* rect = dom.getFirstChild(root, "rect");
446         REPORTER_ASSERT(reporter, rect, "rect element not found");
447         const auto* fill = dom.findAttr(rect, "fill");
448         REPORTER_ASSERT(reporter, !!fill == !!tst.expected_fill);
449         if (fill) {
450             REPORTER_ASSERT(reporter, strcmp(fill, tst.expected_fill) == 0);
451         }
452         const auto* stroke = dom.findAttr(rect, "stroke");
453         REPORTER_ASSERT(reporter, !!stroke == !!tst.expected_stroke);
454         if (stroke) {
455             REPORTER_ASSERT(reporter, strcmp(stroke, tst.expected_stroke) == 0);
456         }
457     }
458 }
459 
DEF_TEST(SVGDevice_fill_rect_hex,reporter)460 DEF_TEST(SVGDevice_fill_rect_hex, reporter) {
461     SkDOM dom;
462     SkPaint paint;
463     paint.setColor(SK_ColorBLUE);
464     {
465         auto svgCanvas = MakeDOMCanvas(&dom);
466         SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
467         svgCanvas->drawRect(bounds, paint);
468     }
469     const SkDOM::Node* rootElement = dom.finishParsing();
470     ABORT_TEST(reporter, !rootElement, "root element not found");
471 
472     const SkDOM::Node* rectElement = dom.getFirstChild(rootElement, "rect");
473     ABORT_TEST(reporter, !rectElement, "rect element not found");
474     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "blue") == 0);
475 }
476 
DEF_TEST(SVGDevice_fill_rect_custom_hex,reporter)477 DEF_TEST(SVGDevice_fill_rect_custom_hex, reporter) {
478     SkDOM dom;
479     {
480         SkPaint paint;
481         paint.setColor(0xFFAABCDE);
482         auto svgCanvas = MakeDOMCanvas(&dom);
483         SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
484         svgCanvas->drawRect(bounds, paint);
485         paint.setColor(0xFFAABBCC);
486         svgCanvas->drawRect(bounds, paint);
487         paint.setColor(0xFFAA1123);
488         svgCanvas->drawRect(bounds, paint);
489     }
490     const SkDOM::Node* rootElement = dom.finishParsing();
491     ABORT_TEST(reporter, !rootElement, "root element not found");
492 
493     // Test 0xAABCDE filled rect.
494     const SkDOM::Node* rectElement = dom.getFirstChild(rootElement, "rect");
495     ABORT_TEST(reporter, !rectElement, "rect element not found");
496     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#AABCDE") == 0);
497 
498     // Test 0xAABBCC filled rect.
499     rectElement = dom.getNextSibling(rectElement, "rect");
500     ABORT_TEST(reporter, !rectElement, "rect element not found");
501     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#ABC") == 0);
502 
503     // Test 0xFFAA1123 filled rect. Make sure it does not turn into #A123.
504     rectElement = dom.getNextSibling(rectElement, "rect");
505     ABORT_TEST(reporter, !rectElement, "rect element not found");
506     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#AA1123") == 0);
507 }
508 
DEF_TEST(SVGDevice_fill_stroke_rect_hex,reporter)509 DEF_TEST(SVGDevice_fill_stroke_rect_hex, reporter) {
510     SkDOM dom;
511     {
512         auto svgCanvas = MakeDOMCanvas(&dom);
513         SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)};
514 
515         SkPaint paint;
516         paint.setColor(0xFF00BBAC);
517         svgCanvas->drawRect(bounds, paint);
518         paint.setStyle(SkPaint::kStroke_Style);
519         paint.setColor(0xFF123456);
520         paint.setStrokeWidth(1);
521         svgCanvas->drawRect(bounds, paint);
522     }
523     const SkDOM::Node* rootElement = dom.finishParsing();
524     ABORT_TEST(reporter, !rootElement, "root element not found");
525 
526     const SkDOM::Node* rectNode = dom.getFirstChild(rootElement, "rect");
527     ABORT_TEST(reporter, !rectNode, "rect element not found");
528     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "fill"), "#00BBAC") == 0);
529 
530     rectNode = dom.getNextSibling(rectNode, "rect");
531     ABORT_TEST(reporter, !rectNode, "rect element not found");
532     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "stroke"), "#123456") == 0);
533     REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "stroke-width"), "1") == 0);
534 }
535 
DEF_TEST(SVGDevice_path_effect,reporter)536 DEF_TEST(SVGDevice_path_effect, reporter) {
537     SkDOM dom;
538 
539     SkPaint paint;
540     paint.setColor(SK_ColorRED);
541     paint.setStyle(SkPaint::kStroke_Style);
542     paint.setStrokeWidth(10);
543     paint.setStrokeCap(SkPaint::kRound_Cap);
544 
545     // Produces a line of three red dots.
546     SkScalar intervals[] = {0, 20};
547     sk_sp<SkPathEffect> pathEffect = SkDashPathEffect::Make(intervals, 2, 0);
548     paint.setPathEffect(pathEffect);
549     SkPoint points[] = {{50, 15}, {100, 15}, {150, 15} };
550     {
551         auto svgCanvas = MakeDOMCanvas(&dom);
552         svgCanvas->drawPoints(SkCanvas::kLines_PointMode, 3, points, paint);
553     }
554     const auto* rootElement = dom.finishParsing();
555     REPORTER_ASSERT(reporter, rootElement, "root element not found");
556     const auto* pathElement = dom.getFirstChild(rootElement, "path");
557     REPORTER_ASSERT(reporter, pathElement, "path element not found");
558 
559     // The SVG path to draw the three dots is a complex list of instructions.
560     // To avoid test brittleness, we don't attempt to match the entire path.
561     // Instead, we simply confirm there are three (M)ove instructions, one per
562     // dot.  If path effects were not being honored, we would expect only one
563     // Move instruction, to the starting position, before drawing a continuous
564     // straight line.
565     const auto* d = dom.findAttr(pathElement, "d");
566     int mCount = 0;
567     const char* pos;
568     for (pos = d; *pos != '\0'; pos++) {
569       mCount += (*pos == 'M') ? 1 : 0;
570     }
571     REPORTER_ASSERT(reporter, mCount == 3);
572 }
573 
DEF_TEST(SVGDevice_relative_path_encoding,reporter)574 DEF_TEST(SVGDevice_relative_path_encoding, reporter) {
575     SkDOM dom;
576     {
577         auto svgCanvas = MakeDOMCanvas(&dom, SkSVGCanvas::kRelativePathEncoding_Flag);
578         SkPath path;
579         path.moveTo(100, 50);
580         path.lineTo(200, 50);
581         path.lineTo(200, 150);
582         path.close();
583 
584         svgCanvas->drawPath(path, SkPaint());
585     }
586 
587     const auto* rootElement = dom.finishParsing();
588     REPORTER_ASSERT(reporter, rootElement, "root element not found");
589     const auto* pathElement = dom.getFirstChild(rootElement, "path");
590     REPORTER_ASSERT(reporter, pathElement, "path element not found");
591     const auto* d = dom.findAttr(pathElement, "d");
592     REPORTER_ASSERT(reporter, !strcmp(d, "m100 50l100 0l0 100l-100 -100Z"));
593 }
594 
DEF_TEST(SVGDevice_color_shader,reporter)595 DEF_TEST(SVGDevice_color_shader, reporter) {
596     SkDOM dom;
597     {
598         auto svgCanvas = MakeDOMCanvas(&dom);
599 
600         SkPaint paint;
601         paint.setShader(SkShaders::Color(0xffffff00));
602 
603         svgCanvas->drawCircle(100, 100, 100, paint);
604     }
605 
606     const auto* rootElement = dom.finishParsing();
607     REPORTER_ASSERT(reporter, rootElement, "root element not found");
608     const auto* ellipseElement = dom.getFirstChild(rootElement, "ellipse");
609     REPORTER_ASSERT(reporter, ellipseElement, "ellipse element not found");
610     const auto* fill = dom.findAttr(ellipseElement, "fill");
611     REPORTER_ASSERT(reporter, fill, "fill attribute not found");
612     REPORTER_ASSERT(reporter, !strcmp(fill, "yellow"));
613 }
614 
DEF_TEST(SVGDevice_parse_minmax,reporter)615 DEF_TEST(SVGDevice_parse_minmax, reporter) {
616     auto check = [&](int64_t n, bool expected) {
617         const auto str = std::to_string(n);
618 
619         int val;
620         REPORTER_ASSERT(reporter, SkToBool(SkParse::FindS32(str.c_str(), &val)) == expected);
621         if (expected) {
622             REPORTER_ASSERT(reporter, val == n);
623         }
624     };
625 
626     check(std::numeric_limits<int>::max(), true);
627     check(std::numeric_limits<int>::min(), true);
628     check(static_cast<int64_t>(std::numeric_limits<int>::max()) + 1, false);
629     check(static_cast<int64_t>(std::numeric_limits<int>::min()) - 1, false);
630 }
631 
632 #endif
633