Home | History | Annotate | Download | only in tests
      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 
     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 
     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         SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create(SkRect::MakeWH(100, 100),
     95                                                              &writer));
     96         svgCanvas->drawText(txt, len, offset.x(), offset.y(), paint);
     97     }
     98     check_text_node(reporter, dom, dom.finishParsing(), offset, 0, expected);
     99 
    100     {
    101         SkAutoTMalloc<SkScalar> xpos(len);
    102         for (int i = 0; i < SkToInt(len); ++i) {
    103             xpos[i] = SkIntToScalar(txt[i]);
    104         }
    105 
    106         SkXMLParserWriter writer(dom.beginParsing());
    107         SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create(SkRect::MakeWH(100, 100),
    108                                                              &writer));
    109         svgCanvas->drawPosTextH(txt, len, xpos, offset.y(), paint);
    110     }
    111     check_text_node(reporter, dom, dom.finishParsing(), offset, 1, expected);
    112 
    113     {
    114         SkAutoTMalloc<SkPoint> pos(len);
    115         for (int i = 0; i < SkToInt(len); ++i) {
    116             pos[i] = SkPoint::Make(SkIntToScalar(txt[i]), -SkIntToScalar(txt[i]));
    117         }
    118 
    119         SkXMLParserWriter writer(dom.beginParsing());
    120         SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create(SkRect::MakeWH(100, 100),
    121                                                              &writer));
    122         svgCanvas->drawPosText(txt, len, pos, paint);
    123     }
    124     check_text_node(reporter, dom, dom.finishParsing(), offset, 2, expected);
    125 }
    126 
    127 }
    128 
    129 DEF_TEST(SVGDevice_whitespace_pos, reporter) {
    130     static const struct {
    131         const char* tst_in;
    132         const char* tst_out;
    133     } tests[] = {
    134         { "abcd"      , "abcd" },
    135         { "ab cd"     , "ab cd" },
    136         { "ab \t\t cd", "ab cd" },
    137         { " abcd"     , "abcd" },
    138         { "  abcd"    , "abcd" },
    139         { " \t\t abcd", "abcd" },
    140         { "abcd "     , "abcd " }, // we allow one trailing whitespace char
    141         { "abcd  "    , "abcd " }, // because it makes no difference and
    142         { "abcd\t  "  , "abcd\t" }, // simplifies the implementation
    143         { "\t\t  \t ab \t\t  \t cd \t\t   \t  ", "ab cd " },
    144     };
    145 
    146     for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
    147         test_whitespace_pos(reporter, tests[i].tst_in, tests[i].tst_out);
    148     }
    149 }
    150