summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/GFReader.cpp
blob: fba6e665de518fbd6ed18bf35ef3b2a60ee9de03 (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
/*************************************************************************
** GFReader.cpp                                                         **
**                                                                      **
** 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/>. **
*************************************************************************/

#include <algorithm>
#include <sstream>
#include "FixWord.hpp"
#include "GFReader.hpp"
#include "Length.hpp"
#include "SignalHandler.hpp"

using namespace std;


struct GFCommand {
	void (GFReader::*method)(int);
	int numBytes;
};


/** Converts a scaled value to double */
static inline double scaled2double (int32_t scaled) {
	return double(scaled)/(1 << 16);
}


uint32_t GFReader::readUnsigned (int bytes) {
	uint32_t ret = 0;
	for (int i=bytes-1; i >= 0 && !_in.eof(); i--) {
		uint32_t b = _in.get();
		ret |= b << (8*i);
	}
	return ret;
}


int32_t GFReader::readSigned (int bytes) {
	uint32_t ret = _in.get();
	if (ret & 128)        // negative value?
		ret |= 0xffffff00;
	for (int i=bytes-2; i >= 0 && !_in.eof(); i--)
		ret = (ret << 8) | _in.get();
	return int32_t(ret);
}


string GFReader::readString (int bytes) {
	bytes = max(0, bytes);
	string str(bytes, '\0');
	_in.read(&str[0], bytes);
	return str;
}


/** Reads a single GF command from the current position of the input stream and calls the
 *  corresponding cmdFOO method.
 *  @return opcode of the executed command */
int GFReader::executeCommand () {
	SignalHandler::instance().check();
	/* Each cmdFOO command reads the necessary number of bytes from the stream so executeCommand
	doesn't need to know the exact GF command format. Some cmdFOO methods are used for multiple
	GF commands because they only differ in the size of their parameters. */
	static const GFCommand commands[] = {
		{&GFReader::cmdPaint, 1}, {&GFReader::cmdPaint, 2}, {&GFReader::cmdPaint, 3},                        // 64-66
		{&GFReader::cmdBoc, 0},   {&GFReader::cmdBoc1, 0},                                                   // 67-68
		{&GFReader::cmdEoc, 0},                                                                              // 69
		{&GFReader::cmdSkip, 0},  {&GFReader::cmdSkip, 1},  {&GFReader::cmdSkip, 2},{&GFReader::cmdSkip, 3}, // 70-73
		{&GFReader::cmdXXX, 1},   {&GFReader::cmdXXX, 2},   {&GFReader::cmdXXX, 3}, {&GFReader::cmdXXX, 4},  // 239-242
		{&GFReader::cmdYYY, 0},                                                                              // 243
		{&GFReader::cmdNop, 0},                                                                              // 244
		{&GFReader::cmdCharLoc, 0}, {&GFReader::cmdCharLoc0, 0},                                             // 245-246
		{&GFReader::cmdPre, 0},     {&GFReader::cmdPost, 0}, {&GFReader::cmdPostPost, 0}                     // 247-249
	};

	int opcode = _in.get();
	if (opcode < 0)  // at end of file?
		throw GFException("unexpected end of file");

	if (opcode <= 63)
		cmdPaint0(opcode);
	else if (opcode >= 74 && opcode <= 238)
		cmdNewRow(opcode-74);
	else if (opcode >= 250)
		throw GFException("undefined GF command (opcode " + std::to_string(opcode) + ")");
	else {
		int offset = opcode <= 73 ? 64 : 239-(73-64+1);
		const GFCommand &cmd = commands[opcode-offset];
		if (cmd.method)
			(this->*cmd.method)(cmd.numBytes);
	}
	return opcode;
}


bool GFReader::executeChar (uint8_t c) {
	_in.clear();
	if (_charInfoMap.empty())
		executePostamble();          // read character info
	_in.clear();
	auto it = _charInfoMap.find(c);
	if (_in && it != _charInfoMap.end()) {
		_in.seekg(it->second.location);
		while (executeCommand() != 69);  // execute all commands until eoc is reached
		return true;
	}
	return false;
}


bool GFReader::executeAllChars () {
	_in.clear();
	if (_charInfoMap.empty())
		executePostamble();   // read character info
	_in.clear();
	if (_in) {
		_in.seekg(0);
		while (executeCommand() != 248); // execute all commands until postamble is reached
		return true;
	}
	return false;
}


bool GFReader::executePreamble () {
	_in.clear();
	if (!_in)
		return false;
	_in.seekg(0);
	executeCommand();
	return true;
}


bool GFReader::executePostamble () {
	_in.clear();
	if (!_in)
		return false;
	_in.seekg(-1, ios::end);
	while (_in.peek() == 223)        // skip fill bytes
		_in.seekg(-1, ios::cur);
	if (_in.peek() != 131)
		throw GFException("invalid identification byte in postpost");
	_in.seekg(-5, ios::cur);         // now on postpost
	if (_in.get() != 249)
		throw GFException("invalid GF file");
	uint32_t q = readUnsigned(4);    // pointer to begin of postamble
	_in.seekg(q);                    // now on begin of postamble
	while (executeCommand() != 249); // execute all commands until postpost is reached
	return true;
}


/** Returns the width of character c in PS point units */
double GFReader::getCharWidth (uint32_t c) const {
	auto it = _charInfoMap.find(c%256);
	return it == _charInfoMap.end() ? 0 : it->second.width*getDesignSize()/(1<<24);
}

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


/** Reads the preamble. */
void GFReader::cmdPre (int) {
	uint32_t i = readUnsigned(1);
	if (i != 131)
		throw GFException("invalid identification byte in preamble");
	else {
		uint32_t k = readUnsigned(1);
		string s = readString(k);
		preamble(s);
	}
}


/** Reads the postamble. */
void GFReader::cmdPost (int) {
	readUnsigned(4);                // pointer to byte after final eoc
	_designSize = double(FixWord(readUnsigned(4)))*Length::pt2bp; // design size of font in points
	_checksum   = readUnsigned(4);  // checksum
	_hppp       = scaled2double(readUnsigned(4))/Length::pt2bp; // horizontal pixels per PS point
	_vppp       = scaled2double(readUnsigned(4))/Length::pt2bp; // vertical pixels per PS point
	_in.seekg(16, ios::cur);        // skip x and y bounds
	postamble();
}


/** Reads trailing bytes at end of stream. */
void GFReader::cmdPostPost (int) {
	readUnsigned(4);   // pointer to begin of postamble
	uint32_t i = readUnsigned(1);
	if (i != 131)
		throw GFException("invalid identification byte in postpost");
	while (readUnsigned(1) == 223); // skip fill bytes
}


/** Inverts "paint color" (black to white or vice versa) of n pixels
 *  and advances the cursor by n.
 *  @param[in] n number of pixels to be inverted */
void GFReader::cmdPaint0 (int n) {
	if (!_insideCharDef)
		throw GFException("character-related command outside BOC and EOC");
	if (_penDown)  // set pixels?
		_bitmap.setBits(_y, _x, n);
	_x += n;
	_penDown = !_penDown;  // invert pen state
}


/** Inverts "paint color" (black to white or vice versa) of n pixels
 *  and advances the cursor by n. The number n of pixels is read from
 *  the input stream.
 *  @param[in] len size of n in bytes */
void GFReader::cmdPaint (int len) {
	uint32_t pixels = readUnsigned(len);
	cmdPaint0(pixels);
}


/** Beginning of character (generic format). */
void GFReader::cmdBoc (int) {
	_currentChar = readSigned(4);
	readSigned(4);  // pointer to previous boc with same c mod 256
	_minX = readSigned(4);
	_maxX = readSigned(4);
	_minY = readSigned(4);
	_maxY = readSigned(4);
	_x = _minX;
	_y = _maxY;
	_penDown = false;
	_bitmap.resize(_minX, _maxX, _minY, _maxY);
	_insideCharDef = true;
	beginChar(_currentChar);
}


/** Beginning of character (compact format). */
void GFReader::cmdBoc1 (int) {
	_currentChar = readUnsigned(1);
	uint32_t dx = readUnsigned(1);
	_maxX = readUnsigned(1);
	_minX = _maxX - dx;
	uint32_t dy = readUnsigned(1);
	_maxY = readUnsigned(1);
	_minY = _maxY - dy;
	_x = _minX;
	_y = _maxY;
	_penDown = false;
	_bitmap.resize(_minX, _maxX, _minY, _maxY);
	_insideCharDef = true;
	beginChar(_currentChar);
}


/** End of character. */
void GFReader::cmdEoc (int) {
	if (!_insideCharDef)
		throw GFException("misplaced EOC");
	_insideCharDef = false;
	endChar(_currentChar);
}


/** Moves cursor to the beginning of a following row and sets
 *  paint color to white.
 *  @param[in] len if 0: move to next row, otherwise: number of bytes to read.
 *                 The read value denotes the number of rows to be skipped.  */
void GFReader::cmdSkip (int len) {
	if (!_insideCharDef)
		throw GFException("character-related command outside BOC and EOC");
	if (len == 0)
		_y--;
	else
		_y -= readUnsigned(len)+1;
	_x = _minX;
	_penDown = false;
}


/** Moves cursor to pixel number 'col' in the next row and sets
 *  the paint color to black.
 *  @param[in] col pixel/column number */
void GFReader::cmdNewRow (int col) {
	if (!_insideCharDef)
		throw GFException("character-related command outside BOC and EOC");
	_x = _minX + col ;
	_y--;
	_penDown = true;
}


void GFReader::cmdXXX (int len) {
	uint32_t n = readUnsigned(len);
	string str = readString(n);
	special(str);
}


void GFReader::cmdYYY (int) {
	int32_t y = readSigned(4);
	numspecial(y);
}


/** Does nothing. */
void GFReader::cmdNop (int) {
}


/** Reads character locator (part of postamble) */
void GFReader::cmdCharLoc0 (int) {
	uint8_t c  = readUnsigned(1); // character code mod 256
	uint8_t dm = readUnsigned(1); // = dx/65536
	int32_t w  = readSigned(4);   // (1<<24)*characterWidth/designSize
	int32_t p  = readSigned(4);   // pointer to begin of (last) character data
	int32_t dx = 65536*dm;        // horizontal escapement
	int32_t dy = 0;               // vertical escapement
	_charInfoMap.emplace(c, CharInfo(dx, dy, w, p));
}


/** Reads character locator (part of postamble) */
void GFReader::cmdCharLoc (int) {
	uint8_t c  = readUnsigned(1); // character code mod 256
	int32_t dx = readSigned(4);   // horizontal escapement (scaled pixel units)
	int32_t dy = readSigned(4);   // vertical escapement (scaled pixel units)
	int32_t w  = readSigned(4);   // (1<<24)*characterWidth/designSize
	int32_t p  = readSigned(4);   // pointer to begin of (last) character data
	_charInfoMap.emplace(c, CharInfo(dx, dy, w, p));
}