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