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 "SkTypes.h" 9 10#ifdef SK_XML 11 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 23namespace { 24 25void 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) { 31 if (root == nullptr) { 32 ERRORF(reporter, "root element not found."); 33 return; 34 } 35 36 const SkDOM::Node* textElem = dom.getFirstChild(root, "text"); 37 if (textElem == nullptr) { 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); 44 REPORTER_ASSERT(reporter, textNode != nullptr); 45 if (textNode != nullptr) { 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"); 53 REPORTER_ASSERT(reporter, x != nullptr); 54 if (x != nullptr) { 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"); 70 REPORTER_ASSERT(reporter, y != nullptr); 71 if (y != nullptr) { 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 87void 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()); 98 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); 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()); 110 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); 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()); 122 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer); 123 svgCanvas->drawPosText(txt, len, pos, paint); 124 } 125 check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected); 126} 127 128} 129 130DEF_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} 151 152#endif 153