summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/tests/TpicSpecialTest.cpp
blob: 04f112792a70d681ce3117c1b2e3b0a14d9188c3 (plain)
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
/*************************************************************************
** TpicSpecialTest.cpp                                                  **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2023 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 <sstream>
#include "SpecialActions.hpp"
#include "TpicSpecialHandler.hpp"
#include "utility.hpp"
#include "XMLNode.hpp"
#include "XMLString.hpp"

using namespace std;


class TpicSpecialTest : public ::testing::Test {
	protected:
		class ActionsRecorder : public EmptySpecialActions {
			public:
				ActionsRecorder () : x(), y() {}
				void embed (const BoundingBox &bb) override  {bbox.embed(bb);}
				void setX (double xx) override               {x = xx;}
				void setY (double yy) override               {x = yy;}
				double getX () const override                {return x;}
				double getY () const override                {return y;}
				Color getColor () const override             {return color;}
				void setColor (const Color &c) override      {color = c;}
				const Matrix& getMatrix () const override    {static Matrix m(1); return m;}

				string getXMLSnippet () const {
					ostringstream oss;
					for (XMLNode *child : *svgTree().pageNode())
						child->write(oss);
					return oss.str();
				}

				void clear () {
					SpecialActions::svgTree().reset();
					SpecialActions::svgTree().newPage(1);
					bbox = BoundingBox(0, 0, 0, 0);
				}

			private:
				double x, y;
				Color color;
				BoundingBox bbox;
		};


		class MyTpicSpecialHandler : public TpicSpecialHandler {
			public:
				explicit MyTpicSpecialHandler (SpecialActions &a) : actions(a) {}
				void finishPage () {dviEndPage(0, actions);}
				bool processSpecial (const string &cmd, string params="") {
					stringstream ss;
					ss << params;
					return process(cmd, ss, actions);
				}

			protected:
				SpecialActions &actions;
		};

	public:
		TpicSpecialTest () : handler(recorder) {}

		void SetUp () override {
			XMLString::DECIMAL_PLACES=2;
			recorder.clear();
		}


	protected:
		ActionsRecorder recorder;
		MyTpicSpecialHandler handler;
};


TEST_F(TpicSpecialTest, name) {
	EXPECT_EQ(handler.name(), "tpic");
}


TEST_F(TpicSpecialTest, fail) {
	EXPECT_FALSE(handler.processSpecial(""));
	EXPECT_FALSE(handler.processSpecial("x"));
	EXPECT_FALSE(handler.processSpecial("xy"));
	EXPECT_FALSE(handler.processSpecial("xyz"));
	EXPECT_FALSE(handler.processSpecial("bk "));
}


TEST_F(TpicSpecialTest, set_fill_intensity) {
	EXPECT_LT(handler.grayLevel(), 0);  // no fill color set by default
	EXPECT_TRUE(handler.processSpecial("bk"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	EXPECT_TRUE(handler.processSpecial("wh"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0);
	EXPECT_TRUE(handler.processSpecial("sh"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0.5);
	EXPECT_TRUE(handler.processSpecial("sh", "0.1"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0.1);
	EXPECT_TRUE(handler.processSpecial("sh", "0.6"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0.6);
	EXPECT_TRUE(handler.processSpecial("sh", "2"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	EXPECT_TRUE(handler.processSpecial("sh", "-2"));
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, set_penwidth) {
	const double mi2bp=0.072;  // milli-inch to PS points
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1);
	handler.processSpecial("pn", "1000");
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1000*mi2bp);
	handler.processSpecial("pn", "-10");
	EXPECT_DOUBLE_EQ(handler.penwidth(), 0);
}


TEST_F(TpicSpecialTest, dot) {
	handler.processSpecial("pn", "1000");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("bk");
	handler.processSpecial("fp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<circle cx='0' cy='0' r='36'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_polyline) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("fp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polyline fill='none' stroke-linecap='round' points='0 0 72 72 72 0' stroke='#000'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_polygon) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("fp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polygon fill='none' points='0 0 72 72 72 0' stroke='#000'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, fill_polygon) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("wh");
	handler.processSpecial("fp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polygon fill='#fff' points='0 0 72 72 72 0' stroke='#000'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
	recorder.clear();
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("wh");
	handler.processSpecial("ip");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polygon fill='#fff' points='0 0 72 72 72 0'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, fill_dashed_polygon) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("wh");
	handler.processSpecial("da", "2");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polygon fill='#fff' points='0 0 72 72 72 0' stroke='#000' stroke-dasharray='144'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, fill_dotted_polygon) {
	handler.processSpecial("pn", "500");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1000 0");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("wh");
	handler.processSpecial("dt", "2 2");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polygon fill='#fff' points='0 0 72 72 72 0' stroke='#000' stroke-width='36' stroke-dasharray='36 144'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_spline) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("sp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<polyline fill='none' stroke-linecap='round' points='0 0 72 72' stroke='#000'/>"
	);
	recorder.clear();
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1500 500");
	handler.processSpecial("pa", "2000 1000");
	handler.processSpecial("pa", "3000 2000");
	handler.processSpecial("pa", "1000 500");
	handler.processSpecial("sp");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path fill='none' d='M0 0L36 36Q72 72 90 54T126 54T180 108T144 90L72 36' stroke='#000'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_dashed_spline) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1500 500");
	handler.processSpecial("pa", "2000 1000");
	handler.processSpecial("pa", "3000 2000");
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("sp", "1");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path fill='none' d='M0 0L36 36Q72 72 90 54T126 54T180 108T108 72Z' stroke='#000' stroke-dasharray='72'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_dotted_spline) {
	handler.processSpecial("pa", "0 0");
	handler.processSpecial("pa", "1000 1000");
	handler.processSpecial("pa", "1500 500");
	handler.processSpecial("pa", "2000 1000");
	handler.processSpecial("pa", "3000 2000");
	handler.processSpecial("pa", "1000 500");
	handler.processSpecial("sp", "-1");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path fill='none' d='M0 0L36 36Q72 72 90 54T126 54T180 108T144 90L72 36' stroke='#000' stroke-dasharray='1 72'/>"
	);
	EXPECT_DOUBLE_EQ(handler.penwidth(), 1.0);
	EXPECT_LT(handler.grayLevel(), 0);
}


TEST_F(TpicSpecialTest, stroke_ellipse) {
	handler.processSpecial("ar", "0 0 500 500 0 7");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<circle cx='0' cy='0' r='36' stroke='#000' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 7");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<ellipse cx='0' cy='0' rx='72' ry='36' stroke='#000' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("pn", "100");
	handler.processSpecial("ar", "0 0 1000 500 -1 -8");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<ellipse cx='0' cy='0' rx='72' ry='36' stroke-width='7.2' stroke='#000' fill='none'/>"
	);
}


TEST_F(TpicSpecialTest, fill_ellipse) {
	handler.processSpecial("bk");
	handler.processSpecial("ia", "0 0 500 500 0 7");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<circle cx='0' cy='0' r='36'/>"
	);
	recorder.clear();
	handler.processSpecial("bk");
	handler.processSpecial("ia", "0 0 1000 500 0 7");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<ellipse cx='0' cy='0' rx='72' ry='36'/>"
	);
	recorder.clear();
	handler.processSpecial("pn", "100");
	handler.processSpecial("wh");
	handler.processSpecial("ia", "0 0 1000 500 -1 -8");
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<ellipse cx='0' cy='0' rx='72' ry='36' fill='#fff'/>"
	);
}


TEST_F(TpicSpecialTest, stroke_arc) {
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(math::PI/4));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 0 1 50.91 25.46' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(math::PI/2));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 0 1 0 36' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(3*math::PI/4));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 0 1-50.91 25.46' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(math::PI));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 1 1-72 0' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(5*math::PI/4));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 1 1-50.91-25.46' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(3*math::PI/2));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 1 1 0-36' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
	recorder.clear();
	handler.processSpecial("ar", "0 0 1000 500 0 "+to_string(-3*math::PI/2));
	EXPECT_EQ(recorder.getXMLSnippet(),
		"<path d='M72 0A72 36 0 0 1 0 36' stroke='#000' stroke-linecap='round' fill='none'/>"
	);
}


TEST_F(TpicSpecialTest, bit_pattern) {
	handler.processSpecial("tx", "");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	handler.processSpecial("tx", " \t   \n  ");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	handler.processSpecial("tx", "INVALID");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	handler.processSpecial("tx", "0");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1);
	handler.processSpecial("tx", "f");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 0);
	handler.processSpecial("tx", "1248");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-4.0/16.0);
	handler.processSpecial("tx", "12480");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-4.0/20.0);
	handler.processSpecial("tx", "DEAD BEEF");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-24.0/32.0);
	handler.processSpecial("tx", "123456789abcdef");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-32.0/60.0);
	handler.processSpecial("tx", "123456789ABCDEF0");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-32.0/64.0);
	handler.processSpecial("tx", "1234 5678 9abc def ");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-32.0/60.0);
	handler.processSpecial("tx", "1234 5678 X 9abc def");
	EXPECT_DOUBLE_EQ(handler.grayLevel(), 1.0-13.0/32.0);
}