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
|
/*************************************************************************
** ImageToSVG.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
** Copyright (C) 2005-2024 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 <config.h>
#include <fstream>
#include <sstream>
#include "Calculator.hpp"
#include "DvisvgmSpecialHandler.hpp"
#include "ImageToSVG.hpp"
#include "Message.hpp"
#include "MessageException.hpp"
#include "PageRanges.hpp"
#include "PsSpecialHandler.hpp"
#include "optimizer/SVGOptimizer.hpp"
#include "SVGOutput.hpp"
#include "System.hpp"
#include "utility.hpp"
#include "version.hpp"
using namespace std;
ImageToSVG::ImageToSVG (std::string fname, SVGOutputBase &out)
: _fname(std::move(fname)), _out(out), _gsVersion(Ghostscript().revision())
{
}
void ImageToSVG::checkGSAndFileFormat () {
if (!_gsVersion)
throw MessageException("Ghostscript is required to process "+imageFormat()+" files");
if (!imageIsValid())
throw MessageException("invalid "+imageFormat()+" file");
}
void ImageToSVG::convert (int pageno) {
checkGSAndFileFormat();
BoundingBox bbox = imageBBox();
if (bbox.valid() && (bbox.width() == 0 || bbox.height() == 0))
Message::wstream(true) << "bounding box of " << imageFormat() << " file is empty\n";
Message::mstream().indent(0);
Message::mstream(false, Message::MC_PAGE_NUMBER) << "processing " << imageFormat() << " file\n";
Message::mstream().indent(1);
_svg.newPage(pageno);
// create a psfile special and forward it to the PsSpecialHandler
stringstream ss;
ss << "\"" << _fname << "\" "
"llx=" << bbox.minX() << " "
"lly=" << bbox.minY() << " "
"urx=" << bbox.maxX() << " "
"ury=" << bbox.maxY();
_currentPageNumber = pageno;
if (!isSinglePageFormat())
ss << " page=" << pageno << " proc=gs";
try {
_psHandler.process(psSpecialCmd(), ss, *this);
}
catch (...) {
progress(nullptr); // remove progress message
throw;
}
writeSVG(pageno);
}
void ImageToSVG::writeSVG (int pageno) {
progress(nullptr);
Matrix matrix = getUserMatrix(_bbox);
// output SVG file
SVGOptimizer(_svg).execute();
_svg.transformPage(matrix);
_bbox.transform(matrix);
_svg.setBBox(_bbox);
_svg.appendToDoc(util::make_unique<XMLComment>(" This file was generated by dvisvgm " + string(PROGRAM_VERSION) + " "));
bool success = _svg.write(_out.getPageStream(pageno, totalPageCount()));
string svgfname = _out.filepath(pageno, totalPageCount()).shorterAbsoluteOrRelative();
_out.finish();
if (svgfname.empty())
svgfname = "<stdout>";
if (!success)
Message::wstream() << "failed to write output to " << svgfname << '\n';
else {
const double bp2pt = 72.27/72;
const double bp2mm = 25.4/72;
Message::mstream(false,Message::MC_PAGE_SIZE)
<< "graphic size: " << XMLString(_bbox.width()*bp2pt) << "pt"
<< " x " << XMLString(_bbox.height()*bp2pt) << "pt"
<< " (" << XMLString(_bbox.width()*bp2mm) << "mm"
<< " x " << XMLString(_bbox.height()*bp2mm) << "mm)\n";
Message::mstream(false, Message::MC_PAGE_WRITTEN) << "output written to " << svgfname << '\n';
if (!_userMessage.empty()) {
string msg = expandText(_userMessage);
Message::ustream(true) << msg << "\n";
}
}
_bbox.invalidate();
_svg.reset();
}
void ImageToSVG::convert (int firstPage, int lastPage, pair<int,int> *pageinfo) {
checkGSAndFileFormat();
int pageCount = 1; // number of pages converted
if (isSinglePageFormat())
convert(1);
else {
if (firstPage > lastPage)
swap(firstPage, lastPage);
firstPage = max(1, firstPage);
if (firstPage > totalPageCount())
pageCount = 0;
else {
lastPage = min(totalPageCount(), lastPage);
pageCount = lastPage-firstPage+1;
for (int i=firstPage; i <= lastPage; i++)
convert(i);
}
}
if (pageinfo) {
pageinfo->first = pageCount;
pageinfo->second = totalPageCount();
}
}
void ImageToSVG::convert (const std::string &rangestr, pair<int,int> *pageinfo) {
checkGSAndFileFormat();
PageRanges ranges;
if (!ranges.parse(rangestr, totalPageCount()))
throw MessageException("invalid page range format");
int pageCount=0; // number of pages converted
for (const auto &range : ranges) {
convert(range.first, range.second, pageinfo);
if (pageinfo)
pageCount += pageinfo->first;
}
if (pageinfo)
pageinfo->first = pageCount;
}
FilePath ImageToSVG::getSVGFilePath (unsigned pageno) const {
FilePath path;
unsigned numPages = totalPageCount();
if (pageno >= 1 && pageno <= numPages)
path = _out.filepath(pageno, numPages);
return path;
}
void ImageToSVG::progress (const char *id) {
static double time=System::time();
static bool draw=false; // show progress indicator?
static size_t count=0;
count++;
// don't show the progress indicator before the given time has elapsed
if (!draw && System::time()-time > PROGRESSBAR_DELAY) {
draw = true;
Terminal::cursor(false);
Message::mstream(false) << "\n";
}
if (draw && ((System::time() - time > 0.05) || id == nullptr)) {
const size_t DIGITS=6;
Message::mstream(false, Message::MC_PROGRESS)
<< string(DIGITS-min(DIGITS, static_cast<size_t>(log10(count))), ' ')
<< count << " PostScript instructions processed\r";
// overprint indicator when finished
if (id == nullptr) {
Message::estream().clearline();
Terminal::cursor(true);
}
time = System::time();
}
}
/** Returns the matrix describing the graphics transformations
* given by the user in terms of transformation commands.
* @param[in] bbox bounding box of the graphics to transform */
Matrix ImageToSVG::getUserMatrix (const BoundingBox &bbox) const {
Matrix matrix(1);
if (!_transCmds.empty()) {
const double bp2pt = (1_bp).pt();
Calculator calc;
calc.setVariable("ux", bbox.minX()*bp2pt);
calc.setVariable("uy", bbox.minY()*bp2pt);
calc.setVariable("w", bbox.width()*bp2pt);
calc.setVariable("h", bbox.height()*bp2pt);
// add constants for length units to calculator
for (const auto &unit : Length::getUnits())
calc.setVariable(unit.first, Length(1, unit.second).pt());
matrix.set(_transCmds, calc);
}
return matrix;
}
|