summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-1.0.1/src/DVIReader.cpp
blob: 1c1343300b230cdb3bdc7beca162f8d9615ced34 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*************************************************************************
** DVIReader.cpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2010 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 <cstdarg>
#include <fstream>
#include <iostream>
#include <sstream>
#include "types.h"
#include "DVIActions.h"
#include "DVIReader.h"
#include "Font.h"
#include "FontManager.h"
#include "Message.h"
#include "VectorStream.h"
#include "macros.h"

using namespace std;

bool DVIReader::COMPUTE_PAGE_LENGTH = false;


DVIReader::DVIReader (istream &is, DVIActions *a) : StreamReader(is), _actions(a)
{
	_inPage = false;
	_pageHeight = _pageWidth = 0;
	_scaleFactor = 0.0;
	_inPostamble = false;
	_totalPages = 0;  // we don't know the correct value yet
	_currFontNum = 0;
	_currPageNum = 0;
	_pageLength = _pagePos = 0;
}


DVIActions* DVIReader::replaceActions (DVIActions *a) {
	DVIActions *prev_actions = _actions;
	_actions = a;
	return prev_actions;
}


/** Evaluates the next DVI command, and computes the corresponding handler.
 *  @param[in] compute_size if true, the size of variable-length parameters is computed
 *  @param[out] handler handler for current DVI command
 *  @param[out] number of parameter bytes
 *  @param[out] param the handler must be called with this parameter
 *  @return opcode of current DVI command */
int DVIReader::evalCommand (bool compute_size, CommandHandler &handler, int &length, int &param) {
	struct DVICommand {
		CommandHandler handler;
		int length;  // number of parameter bytes
	};

   /* Each cmdFOO command reads the necessary number of bytes from the stream, so executeCommand
   doesn't need to know the exact DVI command format. Some cmdFOO methods are used for multiple
	DVI commands because they only differ in length of their parameters. */
	static const DVICommand commands[] = {
		{&DVIReader::cmdSetChar, 1}, {&DVIReader::cmdSetChar, 2}, {&DVIReader::cmdSetChar, 3}, {&DVIReader::cmdSetChar, 4}, // 128-131
		{&DVIReader::cmdSetRule, 8},                                                                                        // 132
		{&DVIReader::cmdPutChar, 1}, {&DVIReader::cmdPutChar, 2}, {&DVIReader::cmdPutChar, 3}, {&DVIReader::cmdPutChar, 4}, // 133-136
		{&DVIReader::cmdPutRule, 8},                                                                                        // 137
		{&DVIReader::cmdNop, 0},                                                                                            // 138
		{&DVIReader::cmdBop, 44},    {&DVIReader::cmdEop, 0},                                                               // 139-140
		{&DVIReader::cmdPush, 0},    {&DVIReader::cmdPop, 0},                                                               // 141-142
		{&DVIReader::cmdRight, 1},   {&DVIReader::cmdRight, 2},   {&DVIReader::cmdRight, 3},   {&DVIReader::cmdRight, 4},   // 143-146
		{&DVIReader::cmdW0, 0},                                                                                             // 147
		{&DVIReader::cmdW, 1},       {&DVIReader::cmdW, 2},       {&DVIReader::cmdW, 3},       {&DVIReader::cmdW, 4},       // 148-151
		{&DVIReader::cmdX0, 0},                                                                                             // 152
		{&DVIReader::cmdX, 1},       {&DVIReader::cmdX, 2},       {&DVIReader::cmdX, 3},       {&DVIReader::cmdX, 4},       // 153-156
		{&DVIReader::cmdDown, 1},    {&DVIReader::cmdDown, 2},    {&DVIReader::cmdDown, 3},    {&DVIReader::cmdDown, 4},    // 157-160
		{&DVIReader::cmdY0, 0},                                                                                             // 161
		{&DVIReader::cmdY, 1},       {&DVIReader::cmdY, 2},       {&DVIReader::cmdY, 3},       {&DVIReader::cmdY, 4},       // 162-165
		{&DVIReader::cmdZ0, 0},                                                                                             // 166
		{&DVIReader::cmdZ, 1},       {&DVIReader::cmdZ, 2},       {&DVIReader::cmdZ, 3},       {&DVIReader::cmdZ, 4},       // 167-170
		{&DVIReader::cmdFontNum, 1}, {&DVIReader::cmdFontNum, 2}, {&DVIReader::cmdFontNum, 3}, {&DVIReader::cmdFontNum, 4}, // 235-238
		{&DVIReader::cmdXXX, 1},     {&DVIReader::cmdXXX, 2},     {&DVIReader::cmdXXX, 3},     {&DVIReader::cmdXXX, 4},     // 239-242
		{&DVIReader::cmdFontDef, 1}, {&DVIReader::cmdFontDef, 2}, {&DVIReader::cmdFontDef, 3}, {&DVIReader::cmdFontDef, 4}, // 243-246
		{&DVIReader::cmdPre, 0},     {&DVIReader::cmdPost, 0},    {&DVIReader::cmdPostPost, 0}                              // 247-249
	};

	const int opcode = in().get();
	if (!in() || opcode < 0)  // at end of file
		throw InvalidDVIFileException("invalid file");

	param = -1;
	if (opcode >= 0 && opcode <= 127) {
		handler = &DVIReader::cmdSetChar0;
		length = 0;
		param = opcode;
	}
	else if (opcode >= 171 && opcode <= 234) {
		handler = &DVIReader::cmdFontNum0;
		length = 0;
		param = opcode-171;
	}
	else if (opcode >= 250) {
		ostringstream oss;
		oss << "undefined DVI command (opcode " << opcode << ')';
		throw DVIException(oss.str());
	}
	else {
		const int offset = opcode <= 170 ? 128 : 235-(170-128+1);
		handler = commands[opcode-offset].handler;
		if (!compute_size)
			length = commands[opcode-offset].length;
		else {
			if (opcode >= 239 && opcode <= 242) { // specials
				int len = opcode-238;
				UInt32 bytes = readUnsigned(len);
				in().seekg(-len, ios_base::cur);
				length = len+bytes;
			}
			else if (opcode >= 243 && opcode <= 246) { // fontdefs
				int len = opcode-242;
				len += 12;
				in().seekg(len, ios_base::cur);   // skip fontnum, checksum, ssize, dsize
				UInt32 bytes = readUnsigned(1);   // length of font path
				bytes += readUnsigned(1);         // length of font name
				in().seekg(-len-2, ios_base::cur);
				length = len+bytes;
			}
			else
				length = commands[opcode-offset].length;
		}
	}
	if (param < 0)
		param = length;
	return opcode;
}


/** Reads a single DVI command from the current position of the input stream and calls the
 *  corresponding cmdFOO method.
 *  @return opcode of the executed command */
int DVIReader::executeCommand () {
	CommandHandler handler;
	int len;   // number of parameter bytes
	int param; // parameter of handler
	streampos pos = in().tellg();
	int opcode = evalCommand(false, handler, len, param);
	(this->*handler)(param);
	if (COMPUTE_PAGE_LENGTH && _inPage && _actions) {
		// ensure progress() is called at 0%
		if (opcode == 139) // bop?
			_actions->progress(0, _pageLength);
		// ensure progress() is called at 100%
		if (in().peek() == 140)  // eop reached?
			_pagePos = _pageLength;
		else
			_pagePos += in().tellg()-pos;
		_actions->progress(_pagePos, _pageLength);
	}
	return opcode;
}


/** Executes all DVI commands read from the input stream. */
void DVIReader::executeAll () {
	int opcode = 0;
	while (!in().eof() && opcode >= 0) {
		try {
			opcode = executeCommand();
		}
		catch (const InvalidDVIFileException &e) {
			// end of stream reached
			opcode = -1;
		}
	}
}


#if 0
/** Executes all DVI commands from the preamble to postpost. */
bool DVIReader::executeDocument () {
	in().clear();   // reset all status bits
	if (!in())
		return false;
	in().seekg(0);  // move file pointer to first byte of the input stream
	while (!in().eof() && executeCommand() != 249); // stop reading after postpost (249)
	return true;
}

bool DVIReader::executeAllPages () {
	in().clear();   // reset all status bits
	if (!in())
		return false;
	in().seekg(0);  // move file pointer to first byte of the input stream
	while (!in().eof() && executeCommand() != 248); // stop reading when postamble (248) is reached
	return true;
}
#endif


/** Reads and executes the commands of a single page.
 *  This methods stops reading after the page's eop command has been executed.
 *  @param[in] n number of page to be executed
 *  @returns true if page was read successfully */
bool DVIReader::executePage (unsigned n) {
	in().clear();    // reset all status bits
	if (!in())
		throw DVIException("invalid DVI file");
	in().seekg(-1, ios_base::end);      // stream pointer to last byte
	while (in().peek() == 223)
		in().seekg(-1, ios_base::cur);   // skip fill bytes
	in().seekg(-4, ios_base::cur);      // now on first byte of q (pointer to begin of postamble)
	UInt32 q = readUnsigned(4);         // pointer to begin of postamble
	in().seekg(q, ios_base::beg);       // now on begin of postamble
	if (executeCommand() != 248)        // execute postamble command but not the fontdefs
		return false;
	if (n < 1 || n > _totalPages)
		return false;
	in().seekg(_prevBop, ios_base::beg); // now on last bop
	_inPostamble = false;               // we jumped out of the postamble
	unsigned pageCount = _totalPages;
	for (; pageCount > n && _prevBop > 0; pageCount--) {
		in().seekg(41, ios_base::cur);   // skip bop and 10*4 \count bytes => now on pointer to prev bop
		_prevBop = readSigned(4);
		in().seekg(_prevBop, ios_base::beg);
	}
	_currPageNum = n;
	while (pageCount == n && executeCommand() != 140); // 140 == eop
	return true;
}


bool DVIReader::executePages (unsigned first, unsigned last) {
	in().clear();
	if (!in())
		throw DVIException("invalid DVI file");
	if (first > last)
		swap(first, last);
	in().seekg(-1, ios_base::end);      // stream pointer to last byte
	while (in().peek() == 223)
		in().seekg(-1, ios_base::cur);   // skip fill bytes
	in().seekg(-4, ios_base::cur);      // now on first byte of q (pointer to begin of postamble)
	UInt32 q = readUnsigned(4);         // pointer to begin of postamble
	in().seekg(q, ios_base::beg);       // now on begin of postamble
	if (executeCommand() != 248)        // execute postamble command but not the fontdefs
		return false;
	first = max(1u, min(first, _totalPages));
	last = max(1u, min(last, _totalPages));
	in().seekg(_prevBop, ios_base::beg); // now on last bop
	_inPostamble = false;               // we jumped out of the postamble
	unsigned count = _totalPages;
	for (; count > first && _prevBop > 0; count--) {
		in().seekg(41, ios_base::cur);   // skip bop and 10*4 \count bytes => now on pointer to prev bop
		_prevBop = readSigned(4);
		in().seekg(_prevBop, ios_base::beg);
	}
	while (first <= last) {
		_currPageNum = first++;
		while (executeCommand () != 140); // 140 == eop
	}
	return true;
}


/** Reads and executes the commands of the postamble. */
void DVIReader::executePostamble () {
	in().clear();  // reset all status bits
	if (!in())
		throw DVIException("invalid DVI file");
	in().seekg(-1, ios_base::end);    // stream pointer to last byte
	while (in().peek() == 223)
		in().seekg(-1, ios_base::cur); // skip fill bytes

	in().seekg(-4, ios_base::cur);    // now on first byte of q (pointer to begin of postamble)
	UInt32 q = readUnsigned(4);       // pointer to begin of postamble
	in().seekg(q, ios_base::beg);     // now on begin of postamble
	while (executeCommand() != 249);  // read all commands until postpost (= 249) is reached
}


/** Returns the current x coordinate in TeX point units.
 *  This is the horizontal position where the next output would be placed. */
double DVIReader::getXPos () const {
	return _currPos.h;
}


/** Returns the current y coordinate in TeX point units.
 *  This is the vertical position where the next output would be placed. */
double DVIReader::getYPos () const {
	return _currPos.v;
}


/** Sets the horizontal position in TeX point units. */
void DVIReader::setXPos (double x) {
	_currPos.h = x;
}


/** Sets the vertical position in TeX point units. */
void DVIReader::setYPos (double y) {
	_currPos.v = y;
}


double DVIReader::getPageHeight () const {
	return _pageHeight;
}


double DVIReader::getPageWidth () const {
	return _pageWidth;
}

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

/** Reads and executes DVI preamble command. */
void DVIReader::cmdPre (int) {
	UInt32 i   = readUnsigned(1);  // identification number (should be 2)
	UInt32 num = readUnsigned(4);  // numerator units of measurement
	UInt32 den = readUnsigned(4);  // denominator units of measurement
	_mag = readUnsigned(4);        // magnification
	UInt32 k   = readUnsigned(1);  // length of following comment
	string cmt = readString(k);    // comment
	if (i != 2)
		throw DVIException("invalid identification value in DVI preamble");
	// 1 dviunit * num/den == multiples of 0.0000001m
	// 1 dviunit * _scaleFactor: length of 1 dviunit in TeX points * _mag/1000
	_scaleFactor = num/25400000.0*7227.0/den*_mag/1000.0;
	if (_actions)
		_actions->preamble(cmt);
}


/** Reads and executes DVI postamble command. */
void DVIReader::cmdPost (int) {
	_prevBop   = readUnsigned(4);  // pointer to previous bop
	UInt32 num = readUnsigned(4);
	UInt32 den = readUnsigned(4);
	_mag = readUnsigned(4);
	_pageHeight = readUnsigned(4); // height of tallest page in dvi units
	_pageWidth  = readUnsigned(4); // width of widest page in dvi units
	readUnsigned(2);               // max. stack depth
	_totalPages = readUnsigned(2); // total number of pages
	// 1 dviunit * num/den == multiples of 0.0000001m
	// 1 dviunit * _scaleFactor: length of 1 dviunit in TeX points * _mag/1000
	_scaleFactor = num/25400000.0*7227.0/den*_mag/1000.0;
	_pageHeight *= _scaleFactor;     // to pt units
	_pageWidth *= _scaleFactor;
	_inPostamble = true;
	if (_actions)
		_actions->postamble();
}


/** Reads and executes DVI postpost command. */
void DVIReader::cmdPostPost (int) {
	_inPostamble = false;
	readUnsigned(4);               // pointer to begin of postamble
	UInt32 i = readUnsigned(1);    // identification byte (should be 2)
	if (i == 2)
		while (readUnsigned(1) == 223);  // skip fill bytes (223), eof bit should be set now
	else
		throw DVIException("invalid identification value in postpost");
}


/** Reads and executes Begin-Of-Page command. */
void DVIReader::cmdBop (int) {
	Int32 c[10];
	for (int i=0; i < 10; i++)
		c[i] = readSigned(4);
	readSigned(4);        // pointer to peceeding bop (-1 in case of first page)
	_currPos.reset();     // set all DVI registers to 0
	while (!_posStack.empty())
		_posStack.pop();
	_currFontNum = 0;
	_inPage = true;
	_pageLength = _pagePos = 0;
	if (COMPUTE_PAGE_LENGTH) {
		// compute number of bytes in current page
		int length, param;
		CommandHandler handler;
		// read all commands until eop is found
		while (evalCommand(true, handler, length, param) != 140) {
			in().seekg(length, ios_base::cur);
			_pageLength += length+1;  // parameter length + opcode length (1 byte)
		}
		++_pageLength;  // add length of eop command (1 byte)
		in().seekg(-int(_pageLength), ios_base::cur);  // go back to first command following bop
		_pageLength += 45; // add length of bop command
	}
	beginPage(_currPageNum, c);
	if (_actions)
		_actions->beginPage(_currPageNum, c);
}


/** Reads and executes End-Of-Page command. */
void DVIReader::cmdEop (int) {
	if (!_posStack.empty())
		throw DVIException("stack not empty at end of page");
	_inPage = false;
	endPage();
	if (_actions)
		_actions->endPage();
}


/** Reads and executes push command. */
void DVIReader::cmdPush (int) {
	_posStack.push(_currPos);
}


/** Reads and executes pop command (restores pushed position information). */
void DVIReader::cmdPop (int) {
	if (_posStack.empty())
		throw DVIException("stack empty at pop command");
	else {
		DVIPosition prevPos = _currPos;
		_currPos = _posStack.top();
		_posStack.pop();
		if (_actions) {
			if (prevPos.h != _currPos.h)
				_actions->moveToX(_currPos.h);
			if (prevPos.v != _currPos.v)
				_actions->moveToY(_currPos.v);
		}
	}
}


/** Helper function that actually sets/puts a charater. It is called by the
 *  cmdSetChar and cmdPutChar methods.
 * @param[in] c character to typeset
 * @param[in] moveCursor if true, register h is increased by the character width
 * @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::putChar (UInt32 c, bool moveCursor) {
	if (_inPage) {
      FontManager &fm = FontManager::instance();
		Font *font = fm.getFont(_currFontNum);
		if (VirtualFont *vf = dynamic_cast<VirtualFont*>(font)) {    // is current font a virtual font?
			vector<UInt8> *dvi = const_cast<vector<UInt8>*>(vf->getDVI(c)); // get DVI snippet that describes character c
			if (dvi) {
				DVIPosition pos = _currPos;       // save current cursor position
				_currPos.x = _currPos.y = _currPos.w = _currPos.z = 0;
				int save_fontnum = _currFontNum; // save current font number
				fm.enterVF(vf);        // new font number context
				cmdFontNum0(fm.vfFirstFontNum(vf));
				double save_scale = _scaleFactor;
				_scaleFactor = vf->scaledSize()/(1 << 20);

				VectorInputStream<UInt8> vis(*dvi);
				istream &is = replaceStream(vis);
				try {
					executeAll();            // execute DVI fragment
				}
				catch (const DVIException &e) {
//					Message::estream(true) << "invalid dvi in vf: " << e.getMessage() << endl; // @@
				}
				replaceStream(is);          // restore previous input stream
				_scaleFactor = save_scale;  // restore previous scale factor
				fm.leaveVF();    // restore previous font number context
				cmdFontNum0(save_fontnum);  // restore previous font number
				_currPos = pos;             // restore previous cursor position
			}
		}
		else if (_actions) {
			_actions->setChar(_currPos.h, _currPos.v, c, font);
		}
		if (moveCursor)
			_currPos.h += font->charWidth(c) * font->scaleFactor() * _mag/1000.0;
	}
	else
		throw DVIException("set_char or put_char outside page");
}


/** Reads and executes set_char_x command. Puts a character at the current
 *  position and advances the cursor.
 *  @param[in] c character to set
 *  @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::cmdSetChar0 (int c) {
	putChar(c, true);
}


/** Reads and executes setx command. Puts a character at the current
 *  position and advances the cursor.
 *  @param[in] len number of parameter bytes (possible values: 1-4)
 *  @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::cmdSetChar (int len) {
   // According to the dvi specification all character codes are unsigned
   // except len == 4. At the moment all char codes are treated as unsigned...
	UInt32 c = readUnsigned(len); // if len == 4 c may be signed
	putChar(c, true);
}


/** Reads and executes putx command. Puts a character at the current
 *  position but doesn't change the cursor position.
 *  @param[in] len number of parameter bytes (possible values: 1-4)
 *  @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::cmdPutChar (int len) {
   // According to the dvi specification all character codes are unsigned
   // except len == 4. At the moment all char codes are treated as unsigned...
	Int32 c = readUnsigned(len);
	putChar(c, false);
}


/** Reads and executes set_rule command. Puts a solid rectangle at the current
 *  position and updates the cursor position.
 *  @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::cmdSetRule (int) {
	if (_inPage) {
		double height = _scaleFactor*readSigned(4);
		double width  = _scaleFactor*readSigned(4);
		if (_actions && height > 0 && width > 0)
			_actions->setRule(_currPos.h, _currPos.v, height, width);
		_currPos.h += width;
		if (_actions && (height <= 0 || width <= 0))
			_actions->moveToX(_currPos.h);
	}
	else
		throw DVIException("set_rule outside of page");
}


/** Reads and executes set_rule command. Puts a solid rectangle at the current
 *  position but leaves the cursor position unchanged.
 *  @throw DVIException if method is called ouside a bop/eop pair */
void DVIReader::cmdPutRule (int) {
	if (_inPage) {
		double height = _scaleFactor*readSigned(4);
		double width  = _scaleFactor*readSigned(4);
		if (_actions && height > 0 && width > 0)
			_actions->setRule(_currPos.h, _currPos.v, height, width);
	}
	else
		throw DVIException("put_rule outside of page");
}


void DVIReader::cmdNop (int)       {}

void DVIReader::cmdRight (int len) {_currPos.h += _scaleFactor*readSigned(len); if (_actions) _actions->moveToX(_currPos.h);}
void DVIReader::cmdDown (int len)  {_currPos.v += _scaleFactor*readSigned(len); if (_actions) _actions->moveToY(_currPos.v);}
void DVIReader::cmdX0 (int)        {_currPos.h += _currPos.x;                   if (_actions) _actions->moveToX(_currPos.h);}
void DVIReader::cmdY0 (int)        {_currPos.v += _currPos.y;                   if (_actions) _actions->moveToY(_currPos.v);}
void DVIReader::cmdW0 (int)        {_currPos.h += _currPos.w;                   if (_actions) _actions->moveToX(_currPos.h);}
void DVIReader::cmdZ0 (int)        {_currPos.v += _currPos.z;                   if (_actions) _actions->moveToY(_currPos.v);}
void DVIReader::cmdX (int len)     {_currPos.x = _scaleFactor*readSigned(len);  cmdX0(0);}
void DVIReader::cmdY (int len)     {_currPos.y = _scaleFactor*readSigned(len);  cmdY0(0);}
void DVIReader::cmdW (int len)     {_currPos.w = _scaleFactor*readSigned(len);  cmdW0(0);}
void DVIReader::cmdZ (int len)     {_currPos.z = _scaleFactor*readSigned(len);  cmdZ0(0);}


void DVIReader::cmdXXX (int len) {
	if (_inPage) {
		UInt32 numBytes = readUnsigned(len);
		string s = readString(numBytes);
		if (_actions)
			_actions->special(s);
	}
	else
		throw DVIException("special outside of page");
}


/** Selects a previously defined font by its number.
 * @param[in] num font number
 * @throw DVIException if font number is undefined */
void DVIReader::cmdFontNum0 (int num) {
	if (Font *font = FontManager::instance().getFont(num)) {
		_currFontNum = num;
		if (_actions && !dynamic_cast<VirtualFont*>(font))
			_actions->setFont(FontManager::instance().fontID(num), font);  // all fonts get a recomputed ID
	}
	else {
		ostringstream oss;
		oss << "undefined font number " << num;
		throw DVIException(oss.str());
	}
}


/** Selects a previously defined font.
 * @param[in] len size of font number variable (in bytes)
 * @throw DVIException if font number is undefined */
void DVIReader::cmdFontNum (int len) {
	UInt32 num = readUnsigned(len);
	cmdFontNum0(num);
}


/** Helper function to handle a font definition.
 *  @param[in] fontnum local font number
 *  @param[in] name font name
 *  @param[in] checksum checksum to be compared with TFM checksum
 *  @param[in] ds design size in TeX point units
 *  @param[in] ss scaled size in TeX point units */
void DVIReader::defineFont (UInt32 fontnum, const string &name, UInt32 cs, double ds, double ss) {
   FontManager &fm = FontManager::instance();
	int id = fm.registerFont(fontnum, name, cs, ds, ss);
	Font *font = fm.getFontById(id);
	if (VirtualFont *vf = dynamic_cast<VirtualFont*>(font)) {
		// read vf file, register its font and character definitions
		fm.enterVF(vf);
		ifstream ifs(vf->path(), ios::binary);
		VFReader vfReader(ifs);
		vfReader.replaceActions(this);
		vfReader.executeAll();
		fm.leaveVF();
	}
	if (_actions)
		_actions->defineFont(id, font);
}


/** Defines a new font.
 * @param[in] len size of font number variable (in bytes) */
void DVIReader::cmdFontDef (int len) {
	UInt32 fontnum  = readUnsigned(len);   // font number
	UInt32 checksum = readUnsigned(4);     // font checksum (to be compared with corresponding TFM checksum)
	UInt32 ssize    = readUnsigned(4);     // scaled size of font in DVI units
	UInt32 dsize    = readUnsigned(4);     // design size of font in DVI units
	UInt32 pathlen  = readUnsigned(1);     // length of font path
	UInt32 namelen  = readUnsigned(1);     // length of font name
	string fontpath = readString(pathlen);
	string fontname = readString(namelen);

	defineFont(fontnum, fontname, checksum, dsize*_scaleFactor, ssize*_scaleFactor);
}


/** This template method is called by the VFReader after reading a font definition from a VF file.
 *  @param[in] fontnum local font number
 *  @param[in] name font name
 *  @param[in] checksum checksum to be compared with TFM checksum
 *  @param[in] dsize design size in TeX point units
 *  @param[in] ssize scaled size in TeX point units */
void DVIReader::defineVFFont (UInt32 fontnum, string path, string name, UInt32 checksum, double dsize, double ssize) {
	VirtualFont *vf = FontManager::instance().getVF();
	defineFont(fontnum, name, checksum, dsize, ssize * vf->scaleFactor());
}


/** This template method is called by the VFReader after reading a character definition from a VF file.
 *  @param[in] c character number
 *  @param[in] dvi DVI fragment describing the character */
void DVIReader::defineVFChar (UInt32 c, vector<UInt8> *dvi) {
	FontManager::instance().assignVfChar(c, dvi);
}