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 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 8 | #include "SkBitmap.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 10 | #include "SkData.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkImageShader.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 13 | #include "SkParse.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 16 | #include "SkTo.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 17 | #include "Test.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 21 | #ifdef SK_XML |
| 22 | |
Ben Wagner | 6ce482a | 2018-03-27 10:57:43 -0400 | [diff] [blame] | 23 | #include "SkDOM.h" |
| 24 | #include "SkSVGCanvas.h" |
| 25 | #include "SkXMLWriter.h" |
| 26 | |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 27 | #if 0 |
| 28 | Using the new system where devices only gets glyphs causes this to fail because the font has no |
| 29 | glyph to unichar data. |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 30 | namespace { |
| 31 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 32 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 33 | void 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | if (root == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 40 | ERRORF(reporter, "root element not found."); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | const SkDOM::Node* textElem = dom.getFirstChild(root, "text"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 45 | if (textElem == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 46 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, textNode != nullptr); |
| 53 | if (textNode != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type); |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 55 | if (strcmp(expected, dom.getName(textNode)) != 0) { |
| 56 | SkDebugf("string fail %s == %s\n", expected, dom.getName(textNode)); |
| 57 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 58 | 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"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, x != nullptr); |
| 65 | if (x != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 66 | 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 Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 75 | if (xpos[i] != SkIntToScalar(expected[i])) { |
| 76 | SkDebugf("Bad xs %g == %g\n", xpos[i], SkIntToScalar(expected[i])); |
| 77 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i])); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const char* y = dom.findAttr(textElem, "y"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | REPORTER_ASSERT(reporter, y != nullptr); |
| 85 | if (y != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 86 | 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 | |
| 101 | void 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 112 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 113 | svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint); |
| 114 | } |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 115 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 116 | |
| 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 124 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 125 | svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint); |
| 126 | } |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 127 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 128 | |
| 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 Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 136 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 137 | svgCanvas->drawPosText(txt, len, pos, paint); |
| 138 | } |
| 139 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 144 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 145 | DEF_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 Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 166 | #endif |
Hal Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 167 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 168 | |
| 169 | void 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. |
| 179 | bool 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 | |
| 218 | void 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 | |
| 230 | DEF_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 | |
| 255 | DEF_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 | |
| 285 | DEF_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 | |
| 315 | DEF_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 Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 343 | #endif |