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 | |
Hal Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 8 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 9 | #include "SkBitmap.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 10 | #include "SkCanvas.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 11 | #include "SkDOM.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 12 | #include "SkData.h" |
| 13 | #include "SkImage.h" |
| 14 | #include "SkImageShader.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 15 | #include "SkParse.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 16 | #include "SkSVGCanvas.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 17 | #include "SkShader.h" |
| 18 | #include "SkStream.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 19 | #include "SkXMLWriter.h" |
| 20 | #include "Test.h" |
| 21 | |
| 22 | #include <string.h> |
| 23 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 24 | #ifdef SK_XML |
| 25 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 26 | namespace { |
| 27 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 28 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 29 | void 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | if (root == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 36 | ERRORF(reporter, "root element not found."); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | const SkDOM::Node* textElem = dom.getFirstChild(root, "text"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | if (textElem == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 42 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, textNode != nullptr); |
| 49 | if (textNode != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 50 | 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"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, x != nullptr); |
| 58 | if (x != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 59 | 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"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, y != nullptr); |
| 75 | if (y != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 76 | 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 | |
| 91 | void 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 102 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 103 | 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 114 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 115 | 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 126 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 127 | svgCanvas->drawPosText(txt, len, pos, paint); |
| 128 | } |
| 129 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | DEF_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 Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 155 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 156 | |
| 157 | void 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. |
| 167 | bool 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 | |
| 206 | void 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 | |
| 218 | DEF_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 | |
| 243 | DEF_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 | |
| 273 | DEF_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 | |
| 303 | DEF_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 Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 331 | #endif |