summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/PDFParser.hpp
blob: a342e740cdb9f9f9a1411791573cdb971f778a69 (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
/*************************************************************************
** PDFParser.hpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2021 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/>. **
*************************************************************************/

#ifndef PDFPARSER_HPP
#define PDFPARSER_HPP

#include <initializer_list>
#include <istream>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <mpark/variant.hpp>
#include "MessageException.hpp"

template <typename K, typename V>
class Dictionary {
	using Map = std::map<K, V>;

	public:
		typename Map::const_iterator begin () const {return _map.begin();}
		typename Map::const_iterator end () const   {return _map.end();}
		typename Map::iterator begin () {return _map.begin();}
		typename Map::iterator end ()   {return _map.end();}
		typename Map::const_iterator find (const K &key) const {return _map.find(key);}
		bool empty () const {return _map.empty();}
		bool exists (const K &key) const {return _map.find(key) != _map.end();}
		size_t size () const {return _map.size();}

		const V* get (const K &key) const {
			auto it = _map.find(key);
			if (it != _map.end())
				return &it->second;
			return nullptr;
		}

		std::pair<typename Map::iterator,bool> emplace (const K &key, V &&value) {
			return _map.emplace(key, std::forward<V>(value));
		}

	private:
		Map _map;
};

//////////////////////////////////////////////////////////////////////////
// PDF object types

struct PDFNull {};
struct PDFStream {};

struct PDFIndirectObject {
	PDFIndirectObject (int n, int gen) : objnum(n), gennum(gen) {}
	int objnum, gennum;
};

struct PDFObjectRef {
	PDFObjectRef (int n, int gen) : objnum(n), gennum(gen) {}
	int objnum, gennum;
};

struct PDFOperator {
	explicit PDFOperator (std::string name) : opname(std::move(name)) {}
	std::string opname;
};

struct PDFName {
	explicit PDFName (std::string val) : str(std::move(val)) {}
	bool operator == (const PDFName &name) const {return str == name.str;}
	std::string str;
};

class PDFObject;

using PDFArray = std::vector<PDFObject>;
using PDFDict = Dictionary<std::string, PDFObject>;

//////////////////////////////////////////////////////////////////////////

/** This class represents a single variadic PDF object. */
class PDFObject {
	using Value = mpark::variant<
		PDFNull,
		bool,
		int,
		double,
		PDFName,
		PDFStream,
		PDFIndirectObject,
		PDFObjectRef,
		PDFOperator,
		std::string,
		std::unique_ptr<PDFArray>,
		std::unique_ptr<PDFDict>
	>;

	public:
		PDFObject () : _value(0) {}

		template <typename T>
		explicit PDFObject (T &&value) : _value(std::forward<T>(value)) {}

		explicit PDFObject (const char *value) : _value(std::string(value)) {}
		explicit operator std::string () const;
		explicit operator double () const;

		template <typename T>
		const T* get () const {return mpark::get_if<T>(&_value);}

		void write (std::ostream &os) const;

	private:
		Value _value;
};


template<> inline const PDFArray* PDFObject::get() const {
	if (auto p = mpark::get_if<std::unique_ptr<PDFArray>>(&_value))
		return &(**p);
	return nullptr;
}


template<> inline const PDFDict* PDFObject::get() const {
	if (auto p = mpark::get_if<std::unique_ptr<PDFDict>>(&_value))
		return &(**p);
	return nullptr;
}


inline std::ostream& operator << (std::ostream &os, const PDFObject &obj) {
	obj.write(os);
	return os;
}

//////////////////////////////////////////////////////////////////////////

class InputReader;

class PDFParser {
	public:
		using PDFOperatorHandler = std::function<void (const std::string&, std::vector<PDFObject>&)>;

	public:
		std::vector<PDFObject> parse (std::istream &is);
		std::vector<PDFObject> parse (const std::string &str);
		std::vector<PDFObject> parse (InputReader &ir);
		void parse (InputReader &ir, std::vector<PDFObject> &objects);

		std::vector<PDFObject> parse (std::istream &is, const PDFOperatorHandler &opHandler);
		std::vector<PDFObject> parse (const std::string &str, const PDFOperatorHandler &opHandler);
		std::vector<PDFObject> parse (InputReader &ir, const PDFOperatorHandler &opHandler);
		void parse (InputReader &ir, std::vector<PDFObject> &objects, const PDFOperatorHandler &opHandler);

	protected:
		PDFArray parseArray (InputReader &ir, const PDFOperatorHandler &opHandler);
		PDFDict parseDict (InputReader &ir, const PDFOperatorHandler &opHandler);
};


/** If errors occur while parsing a sequence of PDF objects, an instance of this exception is thrown. */
struct PDFException : public MessageException {
	explicit PDFException (const std::string &msg) : MessageException(msg) {}
};

#endif