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
|
/*************************************************************************
** dvisvgm.cpp **
** **
** This file is part of dvisvgm -- the DVI to SVG converter **
** Copyright (C) 2005-2011 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 <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "gzstream.h"
#include "CommandLine.h"
#include "DVIToSVG.h"
#include "DVIToSVGActions.h"
#include "FilePath.h"
#include "FileSystem.h"
#include "Font.h"
#include "FontCache.h"
#include "FontEngine.h"
#include "Ghostscript.h"
#include "InputReader.h"
#include "Message.h"
#include "FileFinder.h"
#include "PageSize.h"
#include "SignalHandler.h"
#include "SpecialManager.h"
#include "System.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define TARGET_SYSTEM ""
#endif
#ifdef __MSVC__
#include <potracelib.h>
#else
extern "C" {
#include <potracelib.h>
}
#endif
using namespace std;
class SVGOutput : public DVIToSVG::Output
{
public:
SVGOutput (const char *base=0, string pattern="", int zip_level=0)
: _path(base ? base : ""),
_pattern(pattern),
_stdout(base == 0),
_zipLevel(zip_level),
_page(-1),
_os(0) {}
~SVGOutput () {
delete _os;
}
/** Returns an output stream for the given page.
* @param[in] page number of current page
* @param[in] numPages total number of pages in the DVI file
* @return output stream for the given page */
ostream& getPageStream (int page, int numPages) const {
string fname = filename(page, numPages);
if (fname.empty()) {
delete _os;
_os = 0;
return cout;
}
if (page == _page)
return *_os;
_page = page;
delete _os;
if (_zipLevel > 0)
_os = new ogzstream(fname.c_str(), _zipLevel);
else
_os = new ofstream(fname.c_str());
if (!_os || !*_os) {
delete _os;
_os = 0;
throw MessageException("can't open file "+fname+" for writing");
}
return *_os;
}
/** Returns the name of the SVG file containing the given page.
* @param[in] page number of page */
string filename (int page, int numPages) const {
if (_stdout)
return "";
string fname = _pattern;
if (fname.empty())
fname = numPages > 1 ? "%f-%p" : "%f";
else if (numPages > 1 && fname.find("%p") == string::npos)
fname += FileSystem::isDirectory(fname.c_str()) ? "/%f-%p" : "-%p";
// replace pattern variables by their actual values
// %f: basename of the DVI file
// %p: current page number
ostringstream oss;
oss << setfill('0') << setw(max(2, int(1+log10((double)numPages)))) << page;
size_t pos=0;
while ((pos = fname.find('%', pos)) != string::npos && pos < fname.length()-1) {
switch (fname[pos+1]) {
case 'f': fname.replace(pos, 2, _path.basename()); pos += _path.basename().length(); break;
case 'p': fname.replace(pos, 2, oss.str()); pos += oss.str().length(); break;
default : ++pos;
}
}
FilePath outpath(fname, true);
if (outpath.suffix().empty())
outpath.suffix(_zipLevel > 0 ? "svgz" : "svg");
string apath = outpath.absolute();
string rpath = outpath.relative();
return apath.length() < rpath.length() ? apath : rpath;
}
private:
FilePath _path;
string _pattern;
bool _stdout;
int _zipLevel;
mutable int _page; // number of current page being written
mutable ostream *_os;
};
static void show_help (const CommandLine &cmd) {
cout << PACKAGE_STRING "\n\n";
cmd.help();
cout << "\nCopyright (C) 2005-2011 Martin Gieseking <martin.gieseking@uos.de> \n\n";
}
static string remove_path (string fname) {
fname = FileSystem::adaptPathSeperators(fname);
size_t slashpos = fname.rfind('/');
if (slashpos == string::npos)
return fname;
return fname.substr(slashpos+1);
}
static string ensure_suffix (string fname, const string &suffix) {
size_t dotpos = remove_path(fname).rfind('.');
if (dotpos == string::npos) {
dotpos = fname.length();
fname += "."+suffix;
}
return fname;
}
static void set_trans (DVIToSVG &dvisvg, const CommandLine &args) {
ostringstream oss;
if (args.rotate_given())
oss << 'R' << args.rotate_arg() << ",w/2,h/2";
if (args.translate_given())
oss << 'T' << args.translate_arg();
if (args.scale_given())
oss << 'S' << args.scale_arg();
if (args.transform_given())
oss << args.transform_arg();
dvisvg.setTransformation(oss.str());
}
static void set_libgs (CommandLine &args) {
#if !defined(DISABLE_GS) && !defined(HAVE_LIBGS)
if (args.libgs_given())
Ghostscript::LIBGS_NAME = args.libgs_arg();
else if (getenv("LIBGS"))
Ghostscript::LIBGS_NAME = getenv("LIBGS");
#endif
}
static bool set_cache_dir (const CommandLine &args) {
if (args.cache_given() && !args.cache_arg().empty()) {
if (args.cache_arg() == "none")
PhysicalFont::CACHE_PATH = 0;
else if (FileSystem::exists(args.cache_arg().c_str()))
PhysicalFont::CACHE_PATH = args.cache_arg().c_str();
else
Message::wstream(true) << "cache directory '" << args.cache_arg() << "' does not exist (caching disabled)\n";
}
else {
if (const char *userdir = FileSystem::userdir()) {
static string path = userdir;
path += "/.dvisvgm";
path = FileSystem::adaptPathSeperators(path);
const string cachepath = path+"/cache";
if (!FileSystem::exists(cachepath.c_str())) {
if (!FileSystem::exists(path.c_str()))
FileSystem::mkdir(cachepath.c_str());
else {
FileSystem::mkdir(cachepath.c_str());
// move existing cache files from former location to new one
vector<string> files;
FileSystem::collect(path.c_str(), files);
FORALL(files, vector<string>::iterator, it) {
if (it->at(0) == 'f' && it->length() > 4 && it->substr(it->length()-4, 4) == ".fgd") {
const char *fname = it->c_str()+1;
FileSystem::copy(path+"/"+fname, cachepath+"/"+fname, true);
}
}
}
}
path = cachepath;
PhysicalFont::CACHE_PATH = path.c_str();
}
if (args.cache_given() && args.cache_arg().empty()) {
cout << "cache directory: " << (PhysicalFont::CACHE_PATH ? PhysicalFont::CACHE_PATH : "(none)") << '\n';
FontCache::fontinfo(PhysicalFont::CACHE_PATH, cout, true);
return false;
}
}
return true;
}
static bool check_bbox (const string &bboxstr) {
const char *formats[] = {"none", "min", "dvi", 0};
for (const char **p=formats; *p; ++p)
if (bboxstr == *p)
return true;
if (isalpha(bboxstr[0])) {
try {
PageSize size(bboxstr);
return true;
}
catch (const PageSizeException &e) {
Message::estream(true) << "invalid bounding box format '" << bboxstr << "'\n";
return false;
}
}
try {
BoundingBox bbox;
bbox.set(bboxstr);
return true;
}
catch (const MessageException &e) {
Message::estream(true) << e.what() << '\n';
return false;
}
}
static void print_version (bool extended) {
ostringstream oss;
oss << PACKAGE_STRING;
if (extended) {
oss << " (" TARGET_SYSTEM ")\n";
oss << string(oss.str().length()-1, '-') << "\n"
"freetype: " << FontEngine::version() << "\n"
#ifdef MIKTEX
"MiKTeX: " << FileFinder::version() << "\n"
#else
"kpathsea: " << FileFinder::version() << "\n"
#endif
"potrace: " << (strchr(potrace_version(), ' ') ? strchr(potrace_version(), ' ')+1 : "unknown") << "\n"
"zlib: " << zlibVersion();
}
cout << oss.str() << endl;
}
int main (int argc, char *argv[]) {
CommandLine args;
args.parse(argc, argv);
if (args.error())
return 1;
Message::COLORIZE = args.color_given();
set_libgs(args);
if (args.version_given()) {
print_version(args.version_arg());
return 0;
}
if (args.list_specials_given()) {
SVGOutput out;
DVIToSVG dvisvg(cin, out);
if (const SpecialManager *sm = dvisvg.setProcessSpecials())
sm->writeHandlerInfo(cout);
return 0;
}
if (!set_cache_dir(args))
return 0;
if (argc == 1 || args.help_given()) {
show_help(args);
return 0;
}
if (argc > 1 && args.numFiles() < 1) {
Message::estream(true) << "no input file given\n";
return 1;
}
if (args.stdout_given() && args.zip_given()) {
Message::estream(true) << "writing SVGZ files to stdout is not supported\n";
return 1;
}
if (!check_bbox(args.bbox_arg()))
return 1;
if (args.progress_given()) {
DVIReader::COMPUTE_PAGE_LENGTH = args.progress_given();
DVIToSVGActions::PROGRESSBAR_DELAY = args.progress_arg();
}
SVGTree::CREATE_STYLE = !args.no_styles_given();
SVGTree::USE_FONTS = !args.no_fonts_given();
SVGTree::CREATE_USE_ELEMENTS = args.no_fonts_arg() < 1;
DVIToSVGActions::EXACT_BBOX = args.exact_given();
DVIToSVG::TRACE_MODE = args.trace_all_given() ? (args.trace_all_arg() ? 'a' : 'm') : 0;
PhysicalFont::KEEP_TEMP_FILES = args.keep_given();
PhysicalFont::METAFONT_MAG = args.mag_arg();
double start_time = System::time();
string dvifile = ensure_suffix(args.file(0), "dvi");
ifstream ifs(dvifile.c_str(), ios_base::binary|ios_base::in);
if (!ifs)
Message::estream(true) << "can't open file '" << dvifile << "' for reading\n";
else {
SVGOutput out(args.stdout_given() ? 0 : dvifile.c_str(), args.output_arg(), args.zip_given() ? args.zip_arg() : 0);
Message::LEVEL = args.verbosity_arg();
DVIToSVG dvisvg(ifs, out);
const char *ignore_specials = args.no_specials_given() ? (args.no_specials_arg().empty() ? "*" : args.no_specials_arg().c_str()) : 0;
dvisvg.setProcessSpecials(ignore_specials);
set_trans(dvisvg, args);
dvisvg.setPageSize(args.bbox_arg());
try {
const char *usermap = args.map_file_given() ? args.map_file_arg().c_str() : 0;
FileFinder::init(argv[0], !args.no_mktexmf_given(), usermap);
pair<int,int> pageinfo;
SignalHandler::instance().start();
dvisvg.convert(args.page_arg(), &pageinfo);
Message::mstream().indent(0);
Message::mstream(false, Terminal::BLUE, true) << "\n" << pageinfo.first << " of " << pageinfo.second << " page";
if (pageinfo.second > 1)
Message::mstream(false, Terminal::BLUE, true) << 's';
Message::mstream(false, Terminal::BLUE, true) << " converted in " << (System::time()-start_time) << " seconds\n";
}
catch (DVIException &e) {
Message::estream() << "\nDVI error: " << e.what() << '\n';
}
catch (SignalException &e) {
Message::wstream(true) << "execution interrupted by user\n";
}
catch (MessageException &e) {
Message::estream(true) << e.what() << '\n';
}
}
FileFinder::finish();
return 0;
}
|