• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
19 namespace {
20 
check_text_node(skiatest::Reporter * reporter,const SkDOM & dom,const SkDOM::Node * root,const SkPoint & offset,unsigned scalarsPerPos,const char * expected)21 void 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) {
27     if (root == nullptr) {
28         ERRORF(reporter, "root element not found.");
29         return;
30     }
31 
32     const SkDOM::Node* textElem = dom.getFirstChild(root, "text");
33     if (textElem == nullptr) {
34         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);
40     REPORTER_ASSERT(reporter, textNode != nullptr);
41     if (textNode != nullptr) {
42         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");
49     REPORTER_ASSERT(reporter, x != nullptr);
50     if (x != nullptr) {
51         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");
66     REPORTER_ASSERT(reporter, y != nullptr);
67     if (y != nullptr) {
68         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 
test_whitespace_pos(skiatest::Reporter * reporter,const char * txt,const char * expected)83 void 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());
94         std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
95         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());
106         std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
107         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());
118         std::unique_ptr<SkCanvas> svgCanvas = SkSVGCanvas::Make(SkRect::MakeWH(100, 100), &writer);
119         svgCanvas->drawPosText(txt, len, pos, paint);
120     }
121     check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
122 }
123 
124 }
125 
DEF_TEST(SVGDevice_whitespace_pos,reporter)126 DEF_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 }
147