1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*************************************************************************
** DvisvgmSpecialTest.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
** Copyright (C) 2005-2025 Martin Gieseking <martin.gieseking@uos.de> **
** **
** This program is free software; you can redistribute it and/or **
** modify it under the terms of the GNU General Public License as **
** published by the Free Software Foundation; either version 3 of **
** the License, or (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, but **
** WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/
#include <gtest/gtest.h>
#include <array>
#include <regex>
#include <sstream>
#include "DvisvgmSpecialHandler.hpp"
#include "SpecialActions.hpp"
#include "XMLNode.hpp"
#ifndef SRCDIR
#define SRCDIR "."
#endif
using namespace std;
class MyDvisvgmSpecialHandler : public DvisvgmSpecialHandler {
public:
explicit MyDvisvgmSpecialHandler (EmptySpecialActions &actions) : _actions(actions) {}
void finishPreprocessing () {dviPreprocessingFinished();}
void beginPage () {dviBeginPage(0, _actions);}
void finishPage () {dviEndPage(0, _actions);}
protected:
EmptySpecialActions &_actions;
};
class DvisvgmSpecialTest : public ::testing::Test {
public:
DvisvgmSpecialTest () : handler(recorder) {}
protected:
class ActionsRecorder : public EmptySpecialActions {
public:
void embed (const BoundingBox &bb) override {bbox.embed(bb);}
double getX () const override {return -42;}
double getY () const override {return 14;}
unsigned getCurrentPageNumber() const override {return 1;}
Color getFillColor () const override {return Color(0xff0000);}
Color getStrokeColor () const override {return Color(0x00ff00);}
FilePath getSVGFilePath(unsigned pageno) const override {return FilePath("test.svg");}
bool defsEquals (const string &str) const {return defsString() == str;}
bool pageEquals (const string &str) const {return pageString() == str;}
bool pageMatches (const string &pattern) const {return regex_match(pageString(), regex(pattern));}
bool bboxEquals (const string &str) const {return bbox.svgViewBoxString() == str;}
string bboxString () const {return bbox.svgViewBoxString();}
string defsString () const {return toString(svgTree().defsNode());}
string pageString () const {return toString(svgTree().pageNode());}
void clear () {
SpecialActions::svgTree().reset();
SpecialActions::svgTree().newPage(1);
bbox = BoundingBox(0, 0, 0, 0);
}
protected:
static string toString (const XMLNode *node) {
ostringstream oss;
if (node)
node->write(oss);
return oss.str();
}
private:
BoundingBox bbox;
};
void SetUp () override {
recorder.clear();
handler.beginPage();
XMLElement::WRITE_NEWLINES = false;
}
protected:
ActionsRecorder recorder;
MyDvisvgmSpecialHandler handler;
};
TEST_F(DvisvgmSpecialTest, basic) {
EXPECT_EQ(handler.name(), "dvisvgm");
}
TEST_F(DvisvgmSpecialTest, rawText) {
istringstream iss("raw first{?nl}{?x},{?y}");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'>first\n-42,14</g>")) << recorder.pageString();
iss.clear(); iss.str("raw \t ;{?(-x+2*y-5)}{?(-y+2*x-5)}second {?bbox dummy} \t");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'>first\n-42,14;65-103second 0 0 0 0</g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawPage1) {
istringstream iss("raw <elem attr1='1' attr2='20'>text1<inner><text2</inner>text3</elem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><elem attr1='1' attr2='20'>text1<inner><text2</inner>text3</elem></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawPage2) {
istringstream iss("raw <elem attr1='1' attr2='20'>text1");
handler.process("", iss, recorder);
iss.clear(); iss.str("raw text2<inner>text3</inner><my:empty-elem/>text4");
handler.process("", iss, recorder);
iss.clear(); iss.str("raw </elem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><elem attr1='1' attr2='20'>text1text2<inner>text3</inner><my:empty-elem/>text4</elem></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawPage3) {
istringstream iss("raw <elem attr1='1' attr2=");
handler.process("", iss, recorder);
iss.clear(); iss.str("raw '20'>text2<inner>text3</inner>text4</e");
handler.process("", iss, recorder);
iss.clear(); iss.str("raw lem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><elem attr1='1' attr2='20'>text2<inner>text3</inner>text4</elem></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawDefs1) {
istringstream iss("rawdef <elem attr1='1' attr2='20'>text1<inner><text2</inner>text3</elem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals("<defs><elem attr1='1' attr2='20'>text1<inner><text2</inner>text3</elem></defs>")) << recorder.defsString();
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
}
TEST_F(DvisvgmSpecialTest, rawDefs2) {
istringstream iss("rawdef <elem attr1='1' attr2='20'>text1");
handler.process("", iss, recorder);
iss.clear(); iss.str("rawdef text2<inner>text3</inner>text4");
handler.process("", iss, recorder);
iss.clear(); iss.str("rawdef </elem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals("<defs><elem attr1='1' attr2='20'>text1text2<inner>text3</inner>text4</elem></defs>"));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
}
TEST_F(DvisvgmSpecialTest, rawDefs3) {
istringstream iss("rawdef <elem attr1='1' a");
handler.process("", iss, recorder);
iss.clear(); iss.str("rawdef ttr2='20'>text2<inner>text3</in");
handler.process("", iss, recorder);
iss.clear(); iss.str("rawdef ner>text4</elem>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals("<defs><elem attr1='1' attr2='20'>text2<inner>text3</inner>text4</elem></defs>"));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
}
TEST_F(DvisvgmSpecialTest, rawCDATA) {
istringstream iss("raw <outer>text1<![CDATA[1 < 2 <!--test-->]]>text2</outer>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><outer>text1<![CDATA[1 < 2 <!--test-->]]>text2</outer></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawComments) {
istringstream iss("raw <first/><second><!-- 1 < 2 ->--->text</second>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><first/><second><!-- 1 < 2 ->--->text</second></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawPI) {
istringstream iss("raw <first/><?pi1 whatever?><second><?pi2 whatever?></second>");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'><first/><?pi1 whatever?><second><?pi2 whatever?></second></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, rawPageFail) {
istringstream iss("raw <elem attr1='1' attr2='20'");
EXPECT_THROW({handler.process("", iss, recorder); handler.finishPage();}, XMLParserException); // incomplete opening tag
iss.clear(); iss.str("raw </elem>");
EXPECT_THROW(handler.process("", iss, recorder), XMLParserException); // spurious closing tag
iss.clear(); iss.str("raw <open>text</close>");
EXPECT_THROW(handler.process("", iss, recorder), XMLParserException); // mismatching tags
}
TEST_F(DvisvgmSpecialTest, rawDefsFail) {
istringstream iss("rawdef <elem attr1='1' attr2='20'");
EXPECT_THROW({handler.process("", iss, recorder); handler.finishPage();}, XMLParserException); // incomplete opening tag
iss.clear(); iss.str("rawdef </elem>");
EXPECT_THROW(handler.process("", iss, recorder), XMLParserException); // spurious closing tag
iss.clear(); iss.str("rawdef <open>text</close>");
EXPECT_THROW(handler.process("", iss, recorder), XMLParserException); // mismatching tags
}
TEST_F(DvisvgmSpecialTest, rawdef) {
std::istringstream iss("rawdef first");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals("<defs>first</defs>")) << recorder.defsString();
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
iss.clear(); iss.str("rawdef \t <second></second> \t");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals("<defs>first<second/></defs>"));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
}
TEST_F(DvisvgmSpecialTest, pattern1) {
const auto cmds = {
"rawset pat1",
"raw text1",
"raw <elem>text2</elem>",
"endrawset",
"raw first",
"rawput pat1",
"rawput pat1",
};
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.preprocess("", iss, recorder);
}
handler.finishPreprocessing();
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.process("", iss, recorder);
}
handler.finishPage();
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'>firsttext1<elem>text2</elem>text1<elem>text2</elem></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, pattern2) {
const auto cmds = {
"rawset pat2",
"rawdef text1",
"rawdef <elem>text2</elem>",
"endrawset",
"rawdef first",
"rawput pat2",
"rawput pat2",
};
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.preprocess("", iss, recorder);
}
handler.finishPreprocessing();
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.process("", iss, recorder);
}
handler.finishPage();
EXPECT_TRUE(recorder.defsEquals("<defs>firsttext1<elem>text2</elem></defs>")) << recorder.defsString();
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
}
TEST_F(DvisvgmSpecialTest, pattern3) {
const auto cmds = {
"rawset pat3",
"raw <elem first='a' second='x\"y\"'>text<empty/></elem>",
"rawdef <a/>text2",
"endrawset",
"rawdef first",
"raw second",
"rawput pat3",
"rawput pat3",
};
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.preprocess("", iss, recorder);
}
handler.finishPreprocessing();
for (const char *cmd : cmds) {
std::istringstream iss(cmd);
handler.process("", iss, recorder);
}
EXPECT_TRUE(recorder.defsEquals("<defs>first<a/>text2</defs>"));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'>second<elem first='a' second='x\"y\"'>text<empty/></elem><elem first='a' second='x\"y\"'>text<empty/></elem></g>")) << recorder.pageString();
handler.finishPage();
}
TEST_F(DvisvgmSpecialTest, fail1) {
std::istringstream iss("rawset"); // pattern name missing
EXPECT_THROW(handler.preprocess("", iss, recorder), SpecialException);
handler.finishPreprocessing();
}
TEST_F(DvisvgmSpecialTest, fail2) {
std::istringstream iss("rawset pat"); // endrawset missing
handler.preprocess("", iss, recorder);
EXPECT_THROW(handler.finishPreprocessing(), SpecialException);
}
TEST_F(DvisvgmSpecialTest, processImg) {
std::istringstream iss("img 72.27 72.27 " SRCDIR "/data/rectangle.png");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageMatches("<g id='page1'><image x='-42' y='-58' width='72' height='72' xlink:href='.*rectangle.png'/></g>")) << recorder.pageString();
recorder.clear();
iss.clear();
iss.str("img 10bp 20bp " SRCDIR "/data/rectangle.png");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.pageMatches("<g id='page1'><image x='-42' y='-6' width='10' height='20' xlink:href='.*rectangle.png'/></g>")) << recorder.pageString();
}
TEST_F(DvisvgmSpecialTest, fail3) {
std::istringstream iss("img 10 20xy test.png"); // unknown unit
EXPECT_THROW(handler.process("", iss, recorder), SpecialException);
}
TEST_F(DvisvgmSpecialTest, processBBox) {
std::istringstream iss("bbox abs 0 0 72.27 72.27");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.defsEquals(""));
EXPECT_TRUE(recorder.pageEquals("<g id='page1'/>"));
EXPECT_TRUE(recorder.bboxEquals("0 0 72 72"));
recorder.clear();
iss.clear();
iss.str("bbox 72.27 72.27");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72"));
recorder.clear();
iss.clear();
iss.str("bbox 72bp 72bp");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72"));
recorder.clear();
iss.clear();
iss.str("bbox rel 72.27 72.27");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.bboxEquals("-42 -58 72 72"));
recorder.clear();
iss.clear();
iss.str("bbox new name");
handler.process("", iss, recorder);
EXPECT_TRUE(recorder.bboxEquals("0 0 0 0"));
}
TEST_F(DvisvgmSpecialTest, expandText) {
EXPECT_EQ(recorder.expandText(""), "");
EXPECT_EQ(recorder.expandText("static text"), "static text");
EXPECT_EQ(recorder.expandText("x={?x}, y={?y}"), "x=-42, y=14");
EXPECT_EQ(recorder.expandText("page:{?pageno}, file:{?svgfile}"), "page:1, file:test.svg");
EXPECT_EQ(recorder.expandText("xxx{?cmyk(1,0,1,0)}yyy"), "xxx#00a650yyy");
EXPECT_EQ(recorder.expandText("xxx{?cmyk(1,0,1,0)}yyy{?cmyk(1,0,0,0)}zzz"), "xxx#00a650yyy#00aeefzzz");
EXPECT_EQ(recorder.expandText("fill='{?fillcolor}' stroke='{?strokecolor}'"), "fill='#f00' stroke='#0f0'");
EXPECT_EQ(recorder.expandText("stroke='{?color}'"), "stroke='#f00'");
}
TEST_F(DvisvgmSpecialTest, fail4) {
std::istringstream iss("bbox abs 0 0 72.27xx 72.27"); // unknown unit
EXPECT_THROW(handler.process("", iss, recorder), SpecialException);
}
|