blob: 7044559d83c8834311d1d3952231c6ccd6f9e646 [file] [log] [blame]
fmalita7a048692015-02-20 13:54:40 -08001/*
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 Canaryff2742e2018-01-30 11:35:47 -05008#include "SkTypes.h"
9
10#ifdef SK_XML
11
fmalita7a048692015-02-20 13:54:40 -080012#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) {
halcanary96fcdcc2015-08-27 07:41:13 -070031 if (root == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080032 ERRORF(reporter, "root element not found.");
33 return;
34 }
35
36 const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (textElem == nullptr) {
fmalita7a048692015-02-20 13:54:40 -080038 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);
halcanary96fcdcc2015-08-27 07:41:13 -070044 REPORTER_ASSERT(reporter, textNode != nullptr);
45 if (textNode != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080046 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");
halcanary96fcdcc2015-08-27 07:41:13 -070053 REPORTER_ASSERT(reporter, x != nullptr);
54 if (x != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080055 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");
halcanary96fcdcc2015-08-27 07:41:13 -070070 REPORTER_ASSERT(reporter, y != nullptr);
71 if (y != nullptr) {
fmalita7a048692015-02-20 13:54:40 -080072 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());
Mike Reed5df49342016-11-12 08:06:55 -060098 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -080099 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 Reed5df49342016-11-12 08:06:55 -0600110 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800111 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 Reed5df49342016-11-12 08:06:55 -0600122 std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
fmalita7a048692015-02-20 13:54:40 -0800123 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}
Hal Canaryff2742e2018-01-30 11:35:47 -0500151
152#endif