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 | #include "SkTypes.h" |
| 9 | |
| 10 | #ifdef SK_XML |
| 11 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkData.h" |
| 14 | #include "SkDOM.h" |
| 15 | #include "SkParse.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkSVGCanvas.h" |
| 18 | #include "SkXMLWriter.h" |
| 19 | #include "Test.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | void check_text_node(skiatest::Reporter* reporter, |
| 26 | const SkDOM& dom, |
| 27 | const SkDOM::Node* root, |
| 28 | const SkPoint& offset, |
| 29 | unsigned scalarsPerPos, |
| 30 | const char* expected) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | if (root == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 32 | ERRORF(reporter, "root element not found."); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | const SkDOM::Node* textElem = dom.getFirstChild(root, "text"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | if (textElem == nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 38 | ERRORF(reporter, "<text> element not found."); |
| 39 | return; |
| 40 | } |
| 41 | REPORTER_ASSERT(reporter, dom.getType(textElem) == SkDOM::kElement_Type); |
| 42 | |
| 43 | const SkDOM::Node* textNode= dom.getFirstChild(textElem); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, textNode != nullptr); |
| 45 | if (textNode != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, dom.getType(textNode) == SkDOM::kText_Type); |
| 47 | REPORTER_ASSERT(reporter, strcmp(expected, dom.getName(textNode)) == 0); |
| 48 | } |
| 49 | |
| 50 | int textLen = SkToInt(strlen(expected)); |
| 51 | |
| 52 | const char* x = dom.findAttr(textElem, "x"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, x != nullptr); |
| 54 | if (x != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 55 | int xposCount = (scalarsPerPos < 1) ? 1 : textLen; |
| 56 | REPORTER_ASSERT(reporter, SkParse::Count(x) == xposCount); |
| 57 | |
| 58 | SkAutoTMalloc<SkScalar> xpos(xposCount); |
| 59 | SkParse::FindScalars(x, xpos.get(), xposCount); |
| 60 | if (scalarsPerPos < 1) { |
| 61 | REPORTER_ASSERT(reporter, xpos[0] == offset.x()); |
| 62 | } else { |
| 63 | for (int i = 0; i < xposCount; ++i) { |
| 64 | REPORTER_ASSERT(reporter, xpos[i] == SkIntToScalar(expected[i])); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const char* y = dom.findAttr(textElem, "y"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, y != nullptr); |
| 71 | if (y != nullptr) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 72 | int yposCount = (scalarsPerPos < 2) ? 1 : textLen; |
| 73 | REPORTER_ASSERT(reporter, SkParse::Count(y) == yposCount); |
| 74 | |
| 75 | SkAutoTMalloc<SkScalar> ypos(yposCount); |
| 76 | SkParse::FindScalars(y, ypos.get(), yposCount); |
| 77 | if (scalarsPerPos < 2) { |
| 78 | REPORTER_ASSERT(reporter, ypos[0] == offset.y()); |
| 79 | } else { |
| 80 | for (int i = 0; i < yposCount; ++i) { |
| 81 | REPORTER_ASSERT(reporter, ypos[i] == -SkIntToScalar(expected[i])); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void test_whitespace_pos(skiatest::Reporter* reporter, |
| 88 | const char* txt, |
| 89 | const char* expected) { |
| 90 | size_t len = strlen(txt); |
| 91 | |
| 92 | SkDOM dom; |
| 93 | SkPaint paint; |
| 94 | SkPoint offset = SkPoint::Make(10, 20); |
| 95 | |
| 96 | { |
| 97 | SkXMLParserWriter writer(dom.beginParsing()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 98 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 99 | svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint); |
| 100 | } |
| 101 | check_text_node(reporter, dom, dom.finishParsing(), offset, 0, expected); |
| 102 | |
| 103 | { |
| 104 | SkAutoTMalloc<SkScalar> xpos(len); |
| 105 | for (int i = 0; i < SkToInt(len); ++i) { |
| 106 | xpos[i] = SkIntToScalar(txt[i]); |
| 107 | } |
| 108 | |
| 109 | SkXMLParserWriter writer(dom.beginParsing()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 110 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 111 | svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint); |
| 112 | } |
| 113 | check_text_node(reporter, dom, dom.finishParsing(), offset, 1, expected); |
| 114 | |
| 115 | { |
| 116 | SkAutoTMalloc<SkPoint> pos(len); |
| 117 | for (int i = 0; i < SkToInt(len); ++i) { |
| 118 | pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i])); |
| 119 | } |
| 120 | |
| 121 | SkXMLParserWriter writer(dom.beginParsing()); |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 122 | std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 123 | svgCanvas->drawPosText(txt, len, pos, paint); |
| 124 | } |
| 125 | check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | DEF_TEST(SVGDevice_whitespace_pos, reporter) { |
| 131 | static const struct { |
| 132 | const char* tst_in; |
| 133 | const char* tst_out; |
| 134 | } tests[] = { |
| 135 | { "abcd" , "abcd" }, |
| 136 | { "ab cd" , "ab cd" }, |
| 137 | { "ab \t\t cd", "ab cd" }, |
| 138 | { " abcd" , "abcd" }, |
| 139 | { " abcd" , "abcd" }, |
| 140 | { " \t\t abcd", "abcd" }, |
| 141 | { "abcd " , "abcd " }, // we allow one trailing whitespace char |
| 142 | { "abcd " , "abcd " }, // because it makes no difference and |
| 143 | { "abcd\t " , "abcd\t" }, // simplifies the implementation |
| 144 | { "\t\t \t ab \t\t \t cd \t\t \t ", "ab cd " }, |
| 145 | }; |
| 146 | |
| 147 | for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) { |
| 148 | test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out); |
| 149 | } |
| 150 | } |
Hal Canary | ff2742e | 2018-01-30 11:35:47 -0500 | [diff] [blame] | 151 | |
| 152 | #endif |