summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/DVIToSVG.hpp
blob: 0ac723019cd2ae4d0edee0a53368a307be8a9576 (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
/*************************************************************************
** DVIToSVG.hpp                                                         **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2022 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 DVITOSVG_HPP
#define DVITOSVG_HPP

#include <set>
#include <string>
#include <utility>
#include "DVIReader.hpp"
#include "FilePath.hpp"
#include "SVGTree.hpp"

struct DVIActions;
struct SVGOutputBase;
class HashFunction;

class DVIToSVG : public DVIReader {
	public:
		class HashSettings {
			public:
				enum Parameter {P_LIST, P_REPLACE};
				void setParameters (const std::string &paramstr);
				void setOptionHash (const std::string &optHash) {_optHash = optHash;}
				std::string algorithm () const {return _algo;}
				std::string optionsHash () const {return _optHash;}
				bool isSet (Parameter param) {return _params.find(param) != _params.end();}

			private:
				std::string _algo;
				std::string _optHash;
				std::set<Parameter> _params;
		};

	public:
		explicit DVIToSVG (std::istream &is, SVGOutputBase &out);
		DVIToSVG (const DVIToSVG&) =delete;
		void convert (const std::string &range, std::pair<int,int> *pageinfo=nullptr);
		void setPageSize (const std::string &format)         {_bboxFormatString = format;}
		void setPageTransformation (const std::string &cmds) {_transCmds = cmds;}
		Matrix getPageTransformation () const override;
		void translateToX (double x) override {_tx = x-dviState().h-_tx;}
		void translateToY (double y) override {_ty = y-dviState().v-_ty;}
		double getXPos() const override       {return dviState().h+_tx;}
		double getYPos() const override       {return dviState().v+_ty;}
		void finishLine () override           {_prevYPos = std::numeric_limits<double>::min();}
		void listHashes (const std::string &rangestr, std::ostream &os);

		FilePath getSVGFilePath (unsigned pageno) const;
		std::string getUserBBoxString () const  {return _bboxFormatString;}
		static void setProcessSpecials (const char *ignorelist=nullptr, bool pswarning=false);

	public:
		static bool COMPUTE_PROGRESS;  ///< if true, an action to handle the progress ratio of a page is triggered
		static char TRACE_MODE;
		static HashSettings PAGE_HASH_SETTINGS;

	protected:
		void convert (unsigned firstPage, unsigned lastPage, HashFunction *hashFunc);
		int executeCommand () override;
		void enterBeginPage (unsigned pageno, const std::vector<int32_t> &c);
		void leaveEndPage (unsigned pageno);
		void embedFonts (XMLElement *svgElement);
		void moveRight (double dx, MoveMode mode) override;
		void moveDown (double dy, MoveMode mode) override;

		void dviPost (uint16_t stdepth, uint16_t pages, uint32_t pw, uint32_t ph, uint32_t mag, uint32_t num, uint32_t den, uint32_t lbopofs) override;
		void dviBop (const std::vector<int32_t> &c, int32_t prevBopOffset) override;
		void dviEop () override;
		void dviSetChar0 (uint32_t c, const Font *font) override;
		void dviSetChar (uint32_t c, const Font *font) override;
		void dviPutChar (uint32_t c, const Font *font) override;
		void dviSetRule (double h, double w) override;
		void dviPutRule (double h, double w) override;
		void dviPop () override;
		void dviFontNum (uint32_t fontnum, SetFontMode mode, const Font *font) override;
		void dviDir (WritingMode dir) override;
		void dviXXX (const std::string &str) override;
		void dviXGlyphArray (std::vector<double> &dx, std::vector<double> &dy, std::vector<uint16_t> &glyphs, const Font &font) override;
		void dviXGlyphString (std::vector<double> &dx, std::vector<uint16_t> &glyphs, const Font &font) override;
		void dviXTextAndGlyphs (std::vector<double> &dx, std::vector<double> &dy, std::vector<uint16_t> &chars, std::vector<uint16_t> &glyphs, const Font &font) override;

	private:
		SVGTree _svg;
		SVGOutputBase &_out;
		std::unique_ptr<DVIActions> _actions;
		std::string _bboxFormatString;      ///< bounding box size/format set by the user
		std::string _transCmds;             ///< page transformation commands set by the user
		double _pageHeight=0, _pageWidth=0; ///< global page height and width stored in the postamble
		double _tx=0, _ty=0;                ///< translation of cursor position
		double _prevXPos, _prevYPos;        ///< previous cursor position
		WritingMode _prevWritingMode;       ///< previous writing mode
		std::streampos _pageByte=0;         ///< position of the stream pointer relative to the preceding bop (in bytes)
};

#endif