summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/GraphicsPathParser.hpp
blob: 099614757df7420533528e33b765f002929f17f6 (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
/*************************************************************************
** GraphicsPathParser.hpp                                               **
**                                                                      **
** 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/>. **
*************************************************************************/

#pragma once

#include <cctype>
#include <cstring>
#include <istream>
#include <sstream>
#include "GraphicsPath.hpp"
#include "MessageException.hpp"

namespace internal {
	template <typename NumType>
	bool parse_number (std::istream &is, NumType &value) {
		is >> value;
		return !is.fail();
	}

	template <>
	bool parse_number (std::istream &is, double &value) {
		// don't use operator >> for parsing floating point values because it's implemented
		// differently in libstdc++ and libc++. Instead, use our own function to read the
		// value from the input stream.
		return util::read_double(is, value);
	}
}


struct GraphicsPathParserException : public MessageException {
	explicit GraphicsPathParserException (const std::string &msg) : MessageException(msg) {}
};


template <typename T>
class GraphicsPathParser {
	public:
		GraphicsPath<T> parse (std::istream &is);

		GraphicsPath<T> parse (const std::string &str) {
			std::istringstream iss(str);
			return parse(iss);
		}

	protected:
		template <typename NumType>
		NumType parseNumberOfType (std::istream &is) const {
			is >> std::ws;
			NumType number;
			if (!internal::parse_number(is, number))
				error("number expected", is);
			is >> std::ws;
			return number;
		}

		T parseNumber (std::istream &is) const {
			return parseNumberOfType<T>(is);
		}

		Pair<T> parsePair (std::istream &is) {
			T x = parseNumber(is);
			skipCommaAndWhitespace(is);
			T y = parseNumber(is);
			return Pair<T>(x, y);
		}

		void skipCommaAndWhitespace (std::istream &is) {
			is >> std::ws;
			if (is.peek() == ',') {
				is.get();
				is >> std::ws;
			}
		}

		void parseMoveTo (std::istream &is, bool relative);
		void parseClosePath ();
		void parseLineTo (std::istream &is, bool relative);
		void parseHorizontalLineTo (std::istream &is, bool relative);
		void parseVerticalLineTo (std::istream &is, bool relative);
		void parseCubicTo (std::istream &is, bool relative);
		void parseShortCubicTo (std::istream &is, bool relative);
		void parseQuadraticTo (std::istream &is, bool relative);
		void parseShortQuadraticTo (std::istream &is, bool relative);
		void parseArcTo (std::istream &is, bool relative);

		void error (const std::string &msg, std::istream &is) const {
			std::string postext;
			if (_startpos >= 0) { // valid start position?
				if (is)
					postext = " at position "+ std::to_string(is.tellg()-_startpos);
				else
					postext = " (premature end of data)";
			}
			throw GraphicsPathParserException(msg + postext);
		}

	private:
		std::istream::pos_type _startpos=0; ///< stream position where the parsing started
		GraphicsPath<T> *_path=nullptr;     ///< path being parsed
		Pair<T> _startPoint;                ///< start point of current subpath
		Pair<T> _currentPoint;              ///< current point reached by last path command
		Pair<T> _prevCtrlPoint;             ///< last control point of preceding curve command
};


/** Creates a GraphicsPath object from a SVG path data string read from a given input stream.
 *  @param[in] is stream to read from
 *  @return GraphicsPath object created from the SVG path description
 *  @throw GraphicsPathParserException if the path data contains syntax error */
template <typename T>
GraphicsPath<T> GraphicsPathParser<T>::parse (std::istream &is) {
	GraphicsPath<T> path;
	_path = &path;
	_startpos = is.tellg();
	_currentPoint = _startPoint = _prevCtrlPoint = Pair<T>(0, 0);
	int cmd=0;
	while (!is.eof()) {
		is >> std::ws;
		if (is.peek() < 0)
			break;
		if (isalpha(is.peek()))
			cmd = is.get();
		else {
			// further set of parameters appended to preceding command (command letter omitted)
			skipCommaAndWhitespace(is);
			// subsequent coordinate pairs of a "moveto" command lead to implicit "lineto" commands
			// https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands
			if (cmd == 'M')
				cmd = 'L';
			else if (cmd == 'm')
				cmd = 'l';
		}
		int lower_cmd = std::tolower(cmd);
		bool relative = (cmd == lower_cmd);
		switch (lower_cmd) {
			case 'm': parseMoveTo(is, relative); break;
			case 'z': parseClosePath(); break;
			case 'l': parseLineTo(is, relative); break;
			case 'h': parseHorizontalLineTo(is, relative); break;
			case 'v': parseVerticalLineTo(is, relative); break;
			case 'c': parseCubicTo(is, relative); break;
			case 's': parseShortCubicTo(is, relative); break;
			case 'q': parseQuadraticTo(is, relative); break;
			case 't': parseShortQuadraticTo(is, relative); break;
			case 'a': parseArcTo(is, relative); break;
			case  0 : error("missing command at beginning of SVG path", is);
			default : error("invalid SVG path command '"+std::string(1, char(cmd))+"'", is);
		}
		if (strchr("csqt", lower_cmd) == nullptr)  // not a Bézier curve command?
			_prevCtrlPoint = _currentPoint;            // => no control point, use current point
	}
	_path = nullptr;
	return path;
}


/** Parses a single parameter pair of a "moveto" command. */
template <typename T>
void GraphicsPathParser<T>::parseMoveTo (std::istream &is, bool relative) {
	Pair<T> p = parsePair(is);
	if (!relative || _path->empty())
		_currentPoint = p;
	else
		_currentPoint += p;
	_path->moveto(_currentPoint);
	_startPoint = _currentPoint;
}


/** Handles a "closepath" command. */
template <typename T>
void GraphicsPathParser<T>::parseClosePath () {
	_path->closepath();
	_currentPoint = _startPoint;
}


/** Parses a single parameter pair of a "lineto" command. */
template <typename T>
void GraphicsPathParser<T>::parseLineTo (std::istream &is, bool relative) {
	Pair<T> p = parsePair(is);
	if (relative)
		_currentPoint += p;
	else
		_currentPoint = p;
	_path->lineto(_currentPoint);
}


/** Parses a single parameter of a horizontal "lineto" command. */
template <typename T>
void GraphicsPathParser<T>::parseHorizontalLineTo (std::istream &is, bool relative) {
	T x = parseNumber(is);
	if (relative)
		_currentPoint += Pair<T>(x, 0);
	else
		_currentPoint = Pair<T>(x, _currentPoint.y());
	_path->lineto(_currentPoint);
}


/** Parses a single parameter of a vertical "lineto" command. */
template <typename T>
void GraphicsPathParser<T>::parseVerticalLineTo (std::istream &is, bool relative) {
	T y = parseNumber(is);
	if (relative)
		_currentPoint += Pair<T>(0, y);
	else
		_currentPoint = Pair<T>(_currentPoint.x(), y);
	_path->lineto(_currentPoint);
}


/** Parses a single parameter set a "cubicto" (cubic Bézier curve) command. */
template <typename T>
void GraphicsPathParser<T>::parseCubicTo (std::istream &is, bool relative) {
	Pair<T> p1 = parsePair(is);
	Pair<T> p2 = parsePair(is);
	Pair<T> pe = parsePair(is);
	if (!relative)
		_currentPoint = pe;
	else {
		p1 += _currentPoint;
		p2 += _currentPoint;
		_currentPoint += pe;
	}
	_path->cubicto(p1, p2, _currentPoint);
	_prevCtrlPoint = p2;
}


/** Parses a single parameter set a shorthand "cubicto" (cubic Bézier curve) command. */
template <typename T>
void GraphicsPathParser<T>::parseShortCubicTo (std::istream &is, bool relative) {
	Pair<T> p1 = _prevCtrlPoint + (_currentPoint-_prevCtrlPoint)*T(2);
	Pair<T> p2 = parsePair(is);
	Pair<T> pe = parsePair(is);
	if (!relative)
		_currentPoint = pe;
	else {
		p2 += _currentPoint;
		_currentPoint += pe;
	}
	_path->cubicto(p1, p2, _currentPoint);
	_prevCtrlPoint = p2;
}


/** Parses a single parameter set a "quadto" (quadratic Bézier curve) command. */
template <typename T>
void GraphicsPathParser<T>::parseQuadraticTo (std::istream &is, bool relative) {
	Pair<T> p1 = parsePair(is);
	Pair<T> pe = parsePair(is);
	if (!relative)
		_currentPoint = pe;
	else {
		p1 += _currentPoint;
		_currentPoint += pe;
	}
	_path->quadto(p1, _currentPoint);
	_prevCtrlPoint = p1;
}


/** Parses a single parameter set a shorthand "quadto" (quadratic Bézier curve) command. */
template <typename T>
void GraphicsPathParser<T>::parseShortQuadraticTo (std::istream &is, bool relative) {
	Pair<T> p1 = _prevCtrlPoint + (_currentPoint-_prevCtrlPoint)*T(2);
	Pair<T> pe = parsePair(is);
	if (relative)
		_currentPoint += pe;
	else
		_currentPoint = pe;
	_path->quadto(p1, _currentPoint);
	_prevCtrlPoint = p1;
}


/** Parses a single parameter set an "arcto" command. */
template <typename T>
void GraphicsPathParser<T>::parseArcTo (std::istream &is, bool relative) {
	Pair<T> r = parsePair(is);
	double xrot = parseNumberOfType<double>(is);
	int largeArgFlag = parseNumberOfType<int>(is);
	if (largeArgFlag != 0 && largeArgFlag != 1)
		error("large-arc-flag must be 0 or 1", is);
	int sweepFlag = parseNumberOfType<int>(is);
	if (sweepFlag != 0 && sweepFlag != 1)
		error("sweep-flag must be 0 or 1", is);
	T x = parseNumber(is);
	T y = parseNumber(is);
	Pair<T> p(x, y);
	if (relative)
		p += _currentPoint;
	_currentPoint = p;
	_path->arcto(r.x(), r.y(), xrot, bool(largeArgFlag), bool(sweepFlag), _currentPoint);
}