fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 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 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkBitmap.h" |
| 17 | #include "include/core/SkCanvas.h" |
| 18 | #include "include/core/SkColorFilter.h" |
| 19 | #include "include/core/SkData.h" |
| 20 | #include "include/core/SkImage.h" |
| 21 | #include "include/core/SkShader.h" |
| 22 | #include "include/core/SkStream.h" |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 23 | #include "include/core/SkTextBlob.h" |
Bryce Thomas | 75e2907 | 2020-03-12 20:46:40 -0700 | [diff] [blame] | 24 | #include "include/effects/SkDashPathEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/private/SkTo.h" |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 26 | #include "include/svg/SkSVGCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/utils/SkParse.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/shaders/SkImageShader.h" |
| 29 | #include "tests/Test.h" |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 30 | #include "tools/ToolUtils.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 31 | |
| 32 | #include <string.h> |
| 33 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 34 | #ifdef SK_XML |
| 35 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/svg/SkSVGDevice.h" |
| 37 | #include "src/xml/SkDOM.h" |
| 38 | #include "src/xml/SkXMLWriter.h" |
Ben Wagner | 6ce482a | 2018-03-27 10:57:43 -0400 | [diff] [blame] | 39 | |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 40 | static std::unique_ptr<SkCanvas> MakeDOMCanvas(SkDOM* dom, uint32_t flags = 0) { |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 41 | auto svgDevice = SkSVGDevice::Make(SkISize::Make(100, 100), |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 42 | std::make_unique<SkXMLParserWriter>(dom->beginParsing()), |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 43 | flags); |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 44 | return svgDevice ? std::make_unique<SkCanvas>(svgDevice) |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 45 | : nullptr; |
| 46 | } |
| 47 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 48 | namespace { |
| 49 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 50 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 51 | void check_text_node(skiatest::Reporter* reporter, |
| 52 | const SkDOM& dom, |
| 53 | const SkDOM::Node* root, |
| 54 | const SkPoint& offset, |
| 55 | unsigned scalarsPerPos, |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 56 | const char* txt, |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 57 | const char* expected) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | if (root == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 59 | ERRORF(reporter, "root element not found."); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const SkDOM::Node* textElem = dom.getFirstChild(root, "text"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | if (textElem == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 65 | ERRORF(reporter, "<text> element not found."); |
| 66 | return; |
| 67 | } |
| 68 | REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type); |
| 69 | |
| 70 | const SkDOM::Node* textNode= dom.getFirstChild(textElem); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, textNode != nullptr); |
| 72 | if (textNode != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type); |
| 74 | REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0); |
| 75 | } |
| 76 | |
| 77 | int textLen = SkToInt(strlen(expected)); |
| 78 | |
| 79 | const char* x = dom.findAttr(textElem, "x"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | REPORTER_ASSERT(reporter, x != nullptr); |
| 81 | if (x != nullptr) { |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 82 | int xposCount = textLen; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount); |
| 84 | |
| 85 | SkAutoTMalloc<SkScalar> xpos(xposCount); |
| 86 | SkParse::FindScalars(x, xpos.get(), xposCount); |
| 87 | if (scalarsPerPos < 1) { |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 88 | // For default-positioned text, we cannot make any assumptions regarding |
| 89 | // the first glyph position when the string has leading whitespace (to be stripped). |
| 90 | if (txt[0] != ' ' && txt[0] != '\t') { |
| 91 | REPORTER_ASSERT(reporter, xpos[0] == offset.x()); |
| 92 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 93 | } else { |
| 94 | for (int i = 0; i < xposCount; ++i) { |
| 95 | REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i])); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | const char* y = dom.findAttr(textElem, "y"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, y != nullptr); |
| 102 | if (y != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 103 | int yposCount = (scalarsPerPos < 2) ? 1 : textLen; |
| 104 | REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount); |
| 105 | |
| 106 | SkAutoTMalloc<SkScalar> ypos(yposCount); |
| 107 | SkParse::FindScalars(y, ypos.get(), yposCount); |
| 108 | if (scalarsPerPos < 2) { |
| 109 | REPORTER_ASSERT(reporter, ypos[0] == offset.y()); |
| 110 | } else { |
| 111 | for (int i = 0; i < yposCount; ++i) { |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, ypos[i] == 150 - SkIntToScalar(expected[i])); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void test_whitespace_pos(skiatest::Reporter* reporter, |
| 119 | const char* txt, |
| 120 | const char* expected) { |
| 121 | size_t len = strlen(txt); |
| 122 | |
| 123 | SkDOM dom; |
| 124 | SkPaint paint; |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 125 | SkFont font(ToolUtils::create_portable_typeface()); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 126 | SkPoint offset = SkPoint::Make(10, 20); |
| 127 | |
| 128 | { |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 129 | MakeDOMCanvas(&dom)->drawSimpleText(txt, len, SkTextEncoding::kUTF8, |
| 130 | offset.x(), offset.y(), font, paint); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 131 | } |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 132 | check_text_node(reporter, dom, dom.finishParsing(), offset, 0, txt, expected); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 133 | |
| 134 | { |
| 135 | SkAutoTMalloc<SkScalar> xpos(len); |
| 136 | for (int i = 0; i < SkToInt(len); ++i) { |
| 137 | xpos[i] = SkIntToScalar(txt[i]); |
| 138 | } |
| 139 | |
Mike Reed | 212e906 | 2018-12-25 17:35:49 -0500 | [diff] [blame] | 140 | auto blob = SkTextBlob::MakeFromPosTextH(txt, len, &xpos[0], offset.y(), font); |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 141 | MakeDOMCanvas(&dom)->drawTextBlob(blob, 0, 0, paint); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 142 | } |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 143 | check_text_node(reporter, dom, dom.finishParsing(), offset, 1, txt, expected); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 144 | |
| 145 | { |
| 146 | SkAutoTMalloc<SkPoint> pos(len); |
| 147 | for (int i = 0; i < SkToInt(len); ++i) { |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 148 | pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), 150 - SkIntToScalar(txt[i])); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 151 | auto blob = SkTextBlob::MakeFromPosText(txt, len, &pos[0], font); |
| 152 | MakeDOMCanvas(&dom)->drawTextBlob(blob, 0, 0, paint); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 153 | } |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 154 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, txt, expected); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 157 | } // namespace |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 158 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 159 | DEF_TEST(SVGDevice_whitespace_pos, reporter) { |
| 160 | static const struct { |
| 161 | const char* tst_in; |
| 162 | const char* tst_out; |
| 163 | } tests[] = { |
| 164 | { "abcd" , "abcd" }, |
| 165 | { "ab cd" , "ab cd" }, |
| 166 | { "ab \t\t cd", "ab cd" }, |
| 167 | { " abcd" , "abcd" }, |
| 168 | { " abcd" , "abcd" }, |
| 169 | { " \t\t abcd", "abcd" }, |
| 170 | { "abcd " , "abcd " }, // we allow one trailing whitespace char |
| 171 | { "abcd " , "abcd " }, // because it makes no difference and |
Florin Malita | 673e87c | 2019-07-19 13:59:38 -0400 | [diff] [blame] | 172 | { "abcd\t " , "abcd " }, // simplifies the implementation |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 173 | { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " }, |
| 174 | }; |
| 175 | |
| 176 | for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) { |
| 177 | test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out); |
| 178 | } |
| 179 | } |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 180 | |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 181 | void SetImageShader(SkPaint* paint, int imageWidth, int imageHeight, SkTileMode xTile, |
| 182 | SkTileMode yTile) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 183 | auto surface = SkSurface::MakeRasterN32Premul(imageWidth, imageHeight); |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 184 | paint->setShader(surface->makeImageSnapshot()->makeShader(xTile, yTile, nullptr)); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 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. |
| 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 | |
| 230 | void ImageShaderTestSetup(SkDOM* dom, SkPaint* paint, int imageWidth, int imageHeight, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 231 | int rectWidth, int rectHeight, SkTileMode xTile, SkTileMode yTile) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 232 | SetImageShader(paint, imageWidth, imageHeight, xTile, yTile); |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 233 | auto svgCanvas = MakeDOMCanvas(dom); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 234 | |
| 235 | SkRect bounds{0, 0, SkIntToScalar(rectWidth), SkIntToScalar(rectHeight)}; |
| 236 | svgCanvas->drawRect(bounds, *paint); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | DEF_TEST(SVGDevice_image_shader_norepeat, reporter) { |
| 241 | SkDOM dom; |
| 242 | SkPaint paint; |
| 243 | int imageWidth = 3, imageHeight = 3; |
| 244 | int rectWidth = 10, rectHeight = 10; |
| 245 | ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 246 | SkTileMode::kClamp, SkTileMode::kClamp); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 247 | |
| 248 | const SkDOM::Node* root = dom.finishParsing(); |
| 249 | |
| 250 | const SkDOM::Node *patternNode, *imageNode, *rectNode; |
| 251 | bool structureAppropriate = |
| 252 | FindImageShaderNodes(reporter, &dom, root, &patternNode, &imageNode, &rectNode); |
| 253 | REPORTER_ASSERT(reporter, structureAppropriate); |
| 254 | |
| 255 | // the image should always maintain its size. |
| 256 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth); |
| 257 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight); |
| 258 | |
| 259 | // making the pattern as large as the container prevents |
| 260 | // it from repeating. |
| 261 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0); |
| 262 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0); |
| 263 | } |
| 264 | |
| 265 | DEF_TEST(SVGDevice_image_shader_tilex, reporter) { |
| 266 | SkDOM dom; |
| 267 | SkPaint paint; |
| 268 | int imageWidth = 3, imageHeight = 3; |
| 269 | int rectWidth = 10, rectHeight = 10; |
| 270 | ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 271 | SkTileMode::kRepeat, SkTileMode::kClamp); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 272 | |
| 273 | const SkDOM::Node* root = dom.finishParsing(); |
| 274 | const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg"); |
| 275 | if (innerSvg == nullptr) { |
| 276 | ERRORF(reporter, "inner svg element not found"); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | const SkDOM::Node *patternNode, *imageNode, *rectNode; |
| 281 | bool structureAppropriate = |
| 282 | FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode); |
| 283 | REPORTER_ASSERT(reporter, structureAppropriate); |
| 284 | |
| 285 | // the imageNode should always maintain its size. |
| 286 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth); |
| 287 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight); |
| 288 | |
| 289 | // if the patternNode width matches the imageNode width, |
| 290 | // it will repeat in along the x axis. |
| 291 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth); |
| 292 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "height"), "100%") == 0); |
| 293 | } |
| 294 | |
| 295 | DEF_TEST(SVGDevice_image_shader_tiley, reporter) { |
| 296 | SkDOM dom; |
| 297 | SkPaint paint; |
| 298 | int imageNodeWidth = 3, imageNodeHeight = 3; |
| 299 | int rectNodeWidth = 10, rectNodeHeight = 10; |
| 300 | ImageShaderTestSetup(&dom, &paint, imageNodeWidth, imageNodeHeight, rectNodeWidth, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 301 | rectNodeHeight, SkTileMode::kClamp, SkTileMode::kRepeat); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 302 | |
| 303 | const SkDOM::Node* root = dom.finishParsing(); |
| 304 | const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg"); |
| 305 | if (innerSvg == nullptr) { |
| 306 | ERRORF(reporter, "inner svg element not found"); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | const SkDOM::Node *patternNode, *imageNode, *rectNode; |
| 311 | bool structureAppropriate = |
| 312 | FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode); |
| 313 | REPORTER_ASSERT(reporter, structureAppropriate); |
| 314 | |
| 315 | // the imageNode should always maintain its size. |
| 316 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageNodeWidth); |
| 317 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageNodeHeight); |
| 318 | |
| 319 | // making the patternNode as large as the container prevents |
| 320 | // it from repeating. |
| 321 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(patternNode, "width"), "100%") == 0); |
| 322 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageNodeHeight); |
| 323 | } |
| 324 | |
| 325 | DEF_TEST(SVGDevice_image_shader_tileboth, reporter) { |
| 326 | SkDOM dom; |
| 327 | SkPaint paint; |
| 328 | int imageWidth = 3, imageHeight = 3; |
| 329 | int rectWidth = 10, rectHeight = 10; |
| 330 | ImageShaderTestSetup(&dom, &paint, imageWidth, imageHeight, rectWidth, rectHeight, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 331 | SkTileMode::kRepeat, SkTileMode::kRepeat); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 332 | |
| 333 | const SkDOM::Node* root = dom.finishParsing(); |
| 334 | |
| 335 | const SkDOM::Node *patternNode, *imageNode, *rectNode; |
| 336 | const SkDOM::Node* innerSvg = dom.getFirstChild(root, "svg"); |
| 337 | if (innerSvg == nullptr) { |
| 338 | ERRORF(reporter, "inner svg element not found"); |
| 339 | return; |
| 340 | } |
| 341 | bool structureAppropriate = |
| 342 | FindImageShaderNodes(reporter, &dom, innerSvg, &patternNode, &imageNode, &rectNode); |
| 343 | REPORTER_ASSERT(reporter, structureAppropriate); |
| 344 | |
| 345 | // the imageNode should always maintain its size. |
| 346 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "width")) == imageWidth); |
| 347 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(imageNode, "height")) == imageHeight); |
| 348 | |
| 349 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "width")) == imageWidth); |
| 350 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(patternNode, "height")) == imageHeight); |
| 351 | } |
| 352 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 353 | DEF_TEST(SVGDevice_ColorFilters, reporter) { |
| 354 | SkDOM dom; |
| 355 | SkPaint paint; |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 356 | paint.setColorFilter(SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kSrcIn)); |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 357 | { |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 358 | auto svgCanvas = MakeDOMCanvas(&dom); |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 359 | SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)}; |
| 360 | svgCanvas->drawRect(bounds, paint); |
| 361 | } |
| 362 | const SkDOM::Node* rootElement = dom.finishParsing(); |
| 363 | ABORT_TEST(reporter, !rootElement, "root element not found"); |
| 364 | |
| 365 | const SkDOM::Node* filterElement = dom.getFirstChild(rootElement, "filter"); |
| 366 | ABORT_TEST(reporter, !filterElement, "filter element not found"); |
| 367 | |
| 368 | const SkDOM::Node* floodElement = dom.getFirstChild(filterElement, "feFlood"); |
| 369 | ABORT_TEST(reporter, !floodElement, "feFlood element not found"); |
| 370 | |
| 371 | const SkDOM::Node* compositeElement = dom.getFirstChild(filterElement, "feComposite"); |
| 372 | ABORT_TEST(reporter, !compositeElement, "feComposite element not found"); |
| 373 | |
| 374 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "width"), "100%") == 0); |
| 375 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(filterElement, "height"), "100%") == 0); |
| 376 | |
| 377 | REPORTER_ASSERT(reporter, |
Florin Malita | 3f412f9 | 2019-08-22 13:37:32 -0400 | [diff] [blame] | 378 | strcmp(dom.findAttr(floodElement, "flood-color"), "red") == 0); |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 379 | REPORTER_ASSERT(reporter, atoi(dom.findAttr(floodElement, "flood-opacity")) == 1); |
| 380 | |
| 381 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "in"), "flood") == 0); |
| 382 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(compositeElement, "operator"), "in") == 0); |
| 383 | } |
| 384 | |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 385 | DEF_TEST(SVGDevice_textpath, reporter) { |
| 386 | SkDOM dom; |
| 387 | SkFont font(ToolUtils::create_portable_typeface()); |
| 388 | SkPaint paint; |
| 389 | |
Florin Malita | e05b1b7 | 2020-03-13 09:55:42 -0400 | [diff] [blame] | 390 | auto check_text = [&](uint32_t flags, bool expect_path) { |
| 391 | // By default, we emit <text> nodes. |
| 392 | { |
| 393 | auto svgCanvas = MakeDOMCanvas(&dom, flags); |
| 394 | svgCanvas->drawString("foo", 100, 100, font, paint); |
| 395 | } |
| 396 | const auto* rootElement = dom.finishParsing(); |
| 397 | REPORTER_ASSERT(reporter, rootElement, "root element not found"); |
| 398 | const auto* textElement = dom.getFirstChild(rootElement, "text"); |
| 399 | REPORTER_ASSERT(reporter, !!textElement == !expect_path, "unexpected text element"); |
| 400 | const auto* pathElement = dom.getFirstChild(rootElement, "path"); |
| 401 | REPORTER_ASSERT(reporter, !!pathElement == expect_path, "unexpected path element"); |
| 402 | }; |
| 403 | |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 404 | // By default, we emit <text> nodes. |
Florin Malita | e05b1b7 | 2020-03-13 09:55:42 -0400 | [diff] [blame] | 405 | check_text(0, /*expect_path=*/false); |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 406 | |
| 407 | // With kConvertTextToPaths_Flag, we emit <path> nodes. |
Florin Malita | e05b1b7 | 2020-03-13 09:55:42 -0400 | [diff] [blame] | 408 | check_text(SkSVGCanvas::kConvertTextToPaths_Flag, /*expect_path=*/true); |
| 409 | |
| 410 | // We also use paths in the presence of path effects. |
| 411 | SkScalar intervals[] = {10, 5}; |
| 412 | paint.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
| 413 | check_text(0, /*expect_path=*/true); |
Florin Malita | abc9653 | 2019-07-19 09:11:29 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Florin Malita | 05bab9a | 2019-08-21 12:13:21 -0400 | [diff] [blame] | 416 | DEF_TEST(SVGDevice_fill_stroke, reporter) { |
| 417 | struct { |
| 418 | SkColor color; |
| 419 | SkPaint::Style style; |
| 420 | const char* expected_fill; |
| 421 | const char* expected_stroke; |
| 422 | } gTests[] = { |
Florin Malita | 3f412f9 | 2019-08-22 13:37:32 -0400 | [diff] [blame] | 423 | { SK_ColorBLACK, SkPaint::kFill_Style , nullptr, nullptr }, |
| 424 | { SK_ColorBLACK, SkPaint::kStroke_Style, "none" , "black" }, |
| 425 | { SK_ColorRED , SkPaint::kFill_Style , "red" , nullptr }, |
| 426 | { SK_ColorRED , SkPaint::kStroke_Style, "none" , "red" }, |
Florin Malita | 05bab9a | 2019-08-21 12:13:21 -0400 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | for (const auto& tst : gTests) { |
| 430 | SkPaint p; |
| 431 | p.setColor(tst.color); |
| 432 | p.setStyle(tst.style); |
| 433 | |
| 434 | SkDOM dom; |
| 435 | { |
| 436 | MakeDOMCanvas(&dom)->drawRect(SkRect::MakeWH(100, 100), p); |
| 437 | } |
| 438 | |
| 439 | const auto* root = dom.finishParsing(); |
| 440 | REPORTER_ASSERT(reporter, root, "root element not found"); |
| 441 | const auto* rect = dom.getFirstChild(root, "rect"); |
| 442 | REPORTER_ASSERT(reporter, rect, "rect element not found"); |
| 443 | const auto* fill = dom.findAttr(rect, "fill"); |
| 444 | REPORTER_ASSERT(reporter, !!fill == !!tst.expected_fill); |
| 445 | if (fill) { |
| 446 | REPORTER_ASSERT(reporter, strcmp(fill, tst.expected_fill) == 0); |
| 447 | } |
| 448 | const auto* stroke = dom.findAttr(rect, "stroke"); |
| 449 | REPORTER_ASSERT(reporter, !!stroke == !!tst.expected_stroke); |
| 450 | if (stroke) { |
| 451 | REPORTER_ASSERT(reporter, strcmp(stroke, tst.expected_stroke) == 0); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Lam Lu | a9405c2 | 2020-02-07 14:08:04 -0800 | [diff] [blame] | 456 | DEF_TEST(SVGDevice_fill_rect_hex, reporter) { |
| 457 | SkDOM dom; |
| 458 | SkPaint paint; |
| 459 | paint.setColor(SK_ColorBLUE); |
| 460 | { |
| 461 | auto svgCanvas = MakeDOMCanvas(&dom); |
| 462 | SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)}; |
| 463 | svgCanvas->drawRect(bounds, paint); |
| 464 | } |
| 465 | const SkDOM::Node* rootElement = dom.finishParsing(); |
| 466 | ABORT_TEST(reporter, !rootElement, "root element not found"); |
| 467 | |
| 468 | const SkDOM::Node* rectElement = dom.getFirstChild(rootElement, "rect"); |
| 469 | ABORT_TEST(reporter, !rectElement, "rect element not found"); |
| 470 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "blue") == 0); |
| 471 | } |
| 472 | |
| 473 | DEF_TEST(SVGDevice_fill_rect_custom_hex, reporter) { |
| 474 | SkDOM dom; |
| 475 | { |
| 476 | SkPaint paint; |
| 477 | paint.setColor(0xFFAABCDE); |
| 478 | auto svgCanvas = MakeDOMCanvas(&dom); |
| 479 | SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)}; |
| 480 | svgCanvas->drawRect(bounds, paint); |
| 481 | paint.setColor(0xFFAABBCC); |
| 482 | svgCanvas->drawRect(bounds, paint); |
| 483 | paint.setColor(0xFFAA1123); |
| 484 | svgCanvas->drawRect(bounds, paint); |
| 485 | } |
| 486 | const SkDOM::Node* rootElement = dom.finishParsing(); |
| 487 | ABORT_TEST(reporter, !rootElement, "root element not found"); |
| 488 | |
| 489 | // Test 0xAABCDE filled rect. |
| 490 | const SkDOM::Node* rectElement = dom.getFirstChild(rootElement, "rect"); |
| 491 | ABORT_TEST(reporter, !rectElement, "rect element not found"); |
| 492 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#AABCDE") == 0); |
| 493 | |
| 494 | // Test 0xAABBCC filled rect. |
| 495 | rectElement = dom.getNextSibling(rectElement, "rect"); |
| 496 | ABORT_TEST(reporter, !rectElement, "rect element not found"); |
| 497 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#ABC") == 0); |
| 498 | |
| 499 | // Test 0xFFAA1123 filled rect. Make sure it does not turn into #A123. |
| 500 | rectElement = dom.getNextSibling(rectElement, "rect"); |
| 501 | ABORT_TEST(reporter, !rectElement, "rect element not found"); |
| 502 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectElement, "fill"), "#AA1123") == 0); |
| 503 | } |
| 504 | |
| 505 | DEF_TEST(SVGDevice_fill_stroke_rect_hex, reporter) { |
| 506 | SkDOM dom; |
| 507 | { |
| 508 | auto svgCanvas = MakeDOMCanvas(&dom); |
| 509 | SkRect bounds{0, 0, SkIntToScalar(100), SkIntToScalar(100)}; |
| 510 | |
| 511 | SkPaint paint; |
| 512 | paint.setColor(0xFF00BBAC); |
| 513 | svgCanvas->drawRect(bounds, paint); |
| 514 | paint.setStyle(SkPaint::kStroke_Style); |
| 515 | paint.setColor(0xFF123456); |
| 516 | paint.setStrokeWidth(1); |
| 517 | svgCanvas->drawRect(bounds, paint); |
| 518 | } |
| 519 | const SkDOM::Node* rootElement = dom.finishParsing(); |
| 520 | ABORT_TEST(reporter, !rootElement, "root element not found"); |
| 521 | |
| 522 | const SkDOM::Node* rectNode = dom.getFirstChild(rootElement, "rect"); |
| 523 | ABORT_TEST(reporter, !rectNode, "rect element not found"); |
| 524 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "fill"), "#00BBAC") == 0); |
| 525 | |
| 526 | rectNode = dom.getNextSibling(rectNode, "rect"); |
| 527 | ABORT_TEST(reporter, !rectNode, "rect element not found"); |
| 528 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "stroke"), "#123456") == 0); |
| 529 | REPORTER_ASSERT(reporter, strcmp(dom.findAttr(rectNode, "stroke-width"), "1") == 0); |
| 530 | } |
Bryce Thomas | 75e2907 | 2020-03-12 20:46:40 -0700 | [diff] [blame] | 531 | |
| 532 | DEF_TEST(SVGDevice_path_effect, reporter) { |
| 533 | SkDOM dom; |
| 534 | |
| 535 | SkPaint paint; |
| 536 | paint.setColor(SK_ColorRED); |
| 537 | paint.setStyle(SkPaint::kStroke_Style); |
| 538 | paint.setStrokeWidth(10); |
| 539 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 540 | |
| 541 | // Produces a line of three red dots. |
| 542 | SkScalar intervals[] = {0, 20}; |
| 543 | sk_sp<SkPathEffect> pathEffect = SkDashPathEffect::Make(intervals, 2, 0); |
| 544 | paint.setPathEffect(pathEffect); |
| 545 | SkPoint points[] = {{50, 15}, {100, 15}, {150, 15} }; |
| 546 | { |
| 547 | auto svgCanvas = MakeDOMCanvas(&dom); |
| 548 | svgCanvas->drawPoints(SkCanvas::kLines_PointMode, 3, points, paint); |
| 549 | } |
| 550 | const auto* rootElement = dom.finishParsing(); |
| 551 | REPORTER_ASSERT(reporter, rootElement, "root element not found"); |
| 552 | const auto* pathElement = dom.getFirstChild(rootElement, "path"); |
| 553 | REPORTER_ASSERT(reporter, pathElement, "path element not found"); |
| 554 | |
| 555 | // The SVG path to draw the three dots is a complex list of instructions. |
| 556 | // To avoid test brittleness, we don't attempt to match the entire path. |
| 557 | // Instead, we simply confirm there are three (M)ove instructions, one per |
| 558 | // dot. If path effects were not being honored, we would expect only one |
| 559 | // Move instruction, to the starting position, before drawing a continuous |
| 560 | // straight line. |
| 561 | const auto* d = dom.findAttr(pathElement, "d"); |
| 562 | int mCount = 0; |
| 563 | const char* pos; |
| 564 | for (pos = d; *pos != '\0'; pos++) { |
| 565 | mCount += (*pos == 'M') ? 1 : 0; |
| 566 | } |
| 567 | REPORTER_ASSERT(reporter, mCount == 3); |
| 568 | } |
| 569 | |
| 570 | |
Hal Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 571 | #endif |