1 /*************************************************************************
2 ** DVIReader.h                                                          **
3 **                                                                      **
4 ** This file is part of dvisvgm -- the DVI to SVG converter             **
5 ** Copyright (C) 2005-2015 Martin Gieseking <martin.gieseking@uos.de>   **
6 **                                                                      **
7 ** This program is free software; you can redistribute it and/or        **
8 ** modify it under the terms of the GNU General Public License as       **
9 ** published by the Free Software Foundation; either version 3 of       **
10 ** the License, or (at your option) any later version.                  **
11 **                                                                      **
12 ** This program is distributed in the hope that it will be useful, but  **
13 ** WITHOUT ANY WARRANTY; without even the implied warranty of           **
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
15 ** GNU General Public License for more details.                         **
16 **                                                                      **
17 ** You should have received a copy of the GNU General Public License    **
18 ** along with this program; if not, see <http://www.gnu.org/licenses/>. **
19 *************************************************************************/
20 
21 #ifndef DVISVGM_DVIREADER_H
22 #define DVISVGM_DVIREADER_H
23 
24 #include <limits>
25 #include <map>
26 #include <stack>
27 #include <string>
28 #include "BasicDVIReader.h"
29 #include "MessageException.h"
30 #include "StreamReader.h"
31 #include "VFActions.h"
32 #include "types.h"
33 
34 
35 struct DVIActions;
36 
37 class DVIReader : public BasicDVIReader, protected VFActions
38 {
39 	enum WritingMode {WMODE_LR=0, WMODE_TB=1, WMODE_BT=3};
40 
41 	struct DVIState
42 	{
43 		double h, v;        ///< horizontal and vertical cursor position
44 		double x, w, y, z;  ///< additional registers to store horizontal (x, w) and vertical (y, z) positions
45 		WritingMode d;      ///< direction: 0: horizontal, 1: vertical(top->bottom), 3: vertical (bottom->top)
DVIStateDVIState46 		DVIState ()   {reset();}
resetDVIState47 		void reset () {h = v = x = w = y = z = 0.0; d=WMODE_LR;}
48 	};
49 
50 	public:
51 		DVIReader (std::istream &is, DVIActions *a=0);
52 
53 		bool executeDocument ();
54 		void executeAll ();
55 		void executePreamble ();
56 		void executePostamble ();
57 		bool executePage (unsigned n);
inPostamble()58 		bool inPostamble () const              {return _inPostamble;}
59 		double getXPos () const;
60 		double getYPos () const;
finishLine()61 		void finishLine ()                     {_prevYPos = std::numeric_limits<double>::min();}
translateToX(double x)62 		void translateToX (double x)           {_tx = x-_dviState.h-_tx;}
translateToY(double y)63 		void translateToY (double y)           {_ty = y-_dviState.v-_ty;}
getPageWidth()64 		double getPageWidth () const           {return _pageWidth;}
getPageHeight()65 		double getPageHeight () const          {return _pageHeight;}
getStackDepth()66 		int getStackDepth () const             {return _stateStack.size();}
getCurrentFontNumber()67 		int getCurrentFontNumber () const      {return _currFontNum;}
getCurrentPageNumber()68 		unsigned getCurrentPageNumber () const {return _currPageNum;}
numberOfPages()69 		unsigned numberOfPages () const        {return _bopOffsets.empty() ? 0 : _bopOffsets.size()-1;}
getActions()70 		DVIActions* getActions () const        {return _actions;}
71 		DVIActions* replaceActions (DVIActions *a);
72 
73 	protected:
74 		void collectBopOffsets ();
numberOfPageBytes(int n)75 		size_t numberOfPageBytes (int n) const {return _bopOffsets.size() > 1 ? _bopOffsets[n+1]-_bopOffsets[n] : 0;}
76 		int executeCommand ();
77 		void moveRight (double dx);
78 		void moveDown (double dy);
79 		void putChar (UInt32 c, bool moveCursor);
80 		void putGlyphArray (bool xonly);
81 		void defineFont (UInt32 fontnum, const std::string &name, UInt32 cs, double ds, double ss);
beginPage(unsigned pageno,Int32 * c)82 		virtual void beginPage (unsigned pageno, Int32 *c) {}
endPage(unsigned pageno)83 		virtual void endPage (unsigned pageno) {}
84 
85 		// VFAction methods
86 		void defineVFFont (UInt32 fontnum, std::string path, std::string name, UInt32 checksum, double dsize, double ssize);
87 		void defineVFChar (UInt32 c, std::vector<UInt8> *dvi);
88 
89 		// the following methods represent the DVI commands
90 		// they are called by executeCommand and should not be used directly
91 		void cmdSetChar0 (int c);
92 		void cmdSetChar (int len);
93 		void cmdPutChar (int len);
94 		void cmdSetRule (int len);
95 		void cmdPutRule (int len);
96 		void cmdNop (int len);
97 		void cmdBop (int len);
98 		void cmdEop (int len);
99 		void cmdPush (int len);
100 		void cmdPop (int len);
101 		void cmdDir (int len);
102 		void cmdRight (int len);
103 		void cmdDown (int len);
104 		void cmdX0 (int len);
105 		void cmdY0 (int len);
106 		void cmdW0 (int len);
107 		void cmdZ0 (int len);
108 		void cmdX (int len);
109 		void cmdY (int len);
110 		void cmdW (int len);
111 		void cmdZ (int len);
112 		void cmdFontDef (int len);
113 		void cmdFontNum0 (int n);
114 		void cmdFontNum (int len);
115 		void cmdXXX (int len);
116 		void cmdPre (int len);
117 		void cmdPost (int len);
118 		void cmdPostPost (int len);
119 		void cmdXPic (int len);
120 		void cmdXFontDef (int len);
121 		void cmdXGlyphA (int len);
122 		void cmdXGlyphS (int len);
123 
124 	private:
125 		DVIActions *_actions;    ///< actions to be performed on various DVI events
126 		bool _inPage;            ///< true if between bop and eop
127 		unsigned _currPageNum;   ///< current page number (1 is first page)
128 		int _currFontNum;        ///< current font number
129 		double _dvi2bp;          ///< factor to convert dvi units to PS points
130 		UInt32 _mag;             ///< magnification factor * 1000
131 		bool _inPostamble;       ///< true if stream pointer is inside the postamble
132 		double _pageHeight, _pageWidth;  ///< page height and width in PS points
133 		DVIState _dviState;      ///< current cursor position
134 		std::stack<DVIState> _stateStack;
135 		std::vector<UInt32> _bopOffsets;
136 		double _prevYPos;        ///< previous vertical cursor position
137 		double _tx, _ty;         ///< tranlation of cursor position
138 		std::streampos _pagePos; ///< distance of current DVI command from bop (in bytes)
139 
140 	public:
141 		static bool COMPUTE_PROGRESS;  ///< if true, an action to handle the progress ratio of a page is triggered
142 };
143 
144 #endif
145