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
|
/*************************************************************************
** DVIToSVGActions.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 <cstring>
#include <ctime>
#include "BoundingBox.h"
#include "DVIToSVG.h"
#include "DVIToSVGActions.h"
#include "Font.h"
#include "FontManager.h"
#include "Ghostscript.h"
#include "GlyphTracerMessages.h"
#include "SpecialManager.h"
#include "System.h"
#include "XMLNode.h"
#include "XMLString.h"
using namespace std;
double DVIToSVGActions::PROGRESSBAR_DELAY=1000; // initial delay in seconds (values >= 1000 disable the progressbar)
bool DVIToSVGActions::EXACT_BBOX = false;
DVIToSVGActions::DVIToSVGActions (DVIToSVG &dvisvg, SVGTree &svg)
: _svg(svg), _dvisvg(dvisvg), _pageMatrix(0), _bgcolor(Color::WHITE), _boxes(0)
{
_currentFontNum = -1;
_pageCount = 0;
}
DVIToSVGActions::~DVIToSVGActions () {
delete _pageMatrix;
delete _boxes;
}
void DVIToSVGActions::reset() {
_usedChars.clear();
_usedFonts.clear();
_bbox = BoundingBox();
_currentFontNum = -1;
_bgcolor = Color::WHITE;
}
void DVIToSVGActions::setPageMatrix (const Matrix &matrix) {
delete _pageMatrix;
_pageMatrix = new Matrix(matrix);
}
/** This method is called when a "set char" command was found in the DVI file.
* It draws a character of the current font.
* @param[in] x horizontal position of left bounding box edge
* @param[in] y vertical position of the character's baseline
* @param[in] c character code relative to the current font
* @param[in] font font to be used */
void DVIToSVGActions::setChar (double x, double y, unsigned c, const Font *font) {
// If we use SVG fonts there is no need to record all font name/char/size combinations
// because the SVG font mechanism handles this automatically. It's sufficient to
// record font names and chars. The various font sizes can be ignored here.
// For a given font object, Font::uniqueFont() returns the same unique font object for
// all fonts with the same name.
_usedChars[SVGTree::USE_FONTS ? font->uniqueFont() : font].insert(c);
// However, we record all required fonts
_usedFonts.insert(font);
_svg.appendChar(c, x, y, *font);
// update bounding box
double wl=0, wr=0, h=0, d=0; // left/right width, height, and depth of character c
static string fontname;
GlyphTracerMessages callback(fontname != font->name(), false);
fontname = font->name();
BoundingBox charbox;
const PhysicalFont *ph_font = dynamic_cast<const PhysicalFont*>(font);
if (EXACT_BBOX && ph_font && ph_font->getGlyphBox(c, charbox, &callback)) {
if ((wl = charbox.minX()) > 0) wl=0;
if ((wr = charbox.maxX()) < 0) wr=0;
if ((h = charbox.maxY()) < 0) h=0;
if ((d = -charbox.minY()) < 0) d=0;
}
else {
double s = font->scaleFactor();
wr = s*(font->charWidth(c) + font->italicCorr(c));
h = s*font->charHeight(c);
d = s*font->charDepth(c);
}
BoundingBox bbox(x+wl, y-h, x+wr, y+d);
/* XMLElementNode *rect = new XMLElementNode("rect");
rect->addAttribute("x", x+wl);
rect->addAttribute("y", y-h);
rect->addAttribute("width", (-wl+wr));
rect->addAttribute("height", (h+d));
rect->addAttribute("fill", "none");
rect->addAttribute("stroke", "red");
rect->addAttribute("stroke-width", "0.5");
_svg.appendToPage(rect);*/
if (!getMatrix().isIdentity())
bbox.transform(getMatrix());
embed(bbox);
}
/** This method is called when a "set rule" or "put rule" command was found in the
* DVI file. It draws a solid unrotated rectangle.
* @param[in] x horizontal position of left edge
* @param[in] y vertical position of bottom(!) edge
* @param[in] height length of the vertical edges
* @param[in] width length of the horizontal edges */
void DVIToSVGActions::setRule (double x, double y, double height, double width) {
/* x *= BP;
y *= BP;
height *= BP;
width *= BP; */
// (x,y) is the lower left corner of the rectangle
XMLElementNode *rect = new XMLElementNode("rect");
rect->addAttribute("x", x);
rect->addAttribute("y", y-height);
rect->addAttribute("height", height);
rect->addAttribute("width", width);
if (!getMatrix().isIdentity())
rect->addAttribute("transform", getMatrix().getSVG());
if (getColor() != Color::BLACK)
rect->addAttribute("fill", _svg.getColor().rgbString());
_svg.appendToPage(rect);
// update bounding box
BoundingBox bb(x, y+height, x+width, y);
if (!getMatrix().isIdentity())
bb.transform(getMatrix());
embed(bb);
}
/** This method is called when a "set font" command was found in the DVI file. The
* font must be previously defined.
* @param[in] num unique number of the font in the DVI file (not necessarily equal to the DVI font number)
* @param[in] font pointer to the font object (always represents a physical font and never a virtual font) */
void DVIToSVGActions::setFont (int num, const Font *font) {
_currentFontNum = num;
_svg.setFont(num, font);
}
/** This method is called when a "special" command was found in the DVI file.
* @param[in] s the special expression */
void DVIToSVGActions::special (const string &s) {
try {
_dvisvg.specialManager().process(s, this, this);
// @@ output message in case of unsupported specials?
}
catch (const SpecialException &e) {
Message::estream(true) << "error in special '" << s << "': " << e.getMessage() << '\n';
}
}
void DVIToSVGActions::beginSpecial (const char *prefix) {
}
void DVIToSVGActions::endSpecial (const char *) {
}
/** This method is called when the DVI preamble was read
* @param[in] cmt preamble comment text. */
void DVIToSVGActions::preamble (const string &cmt) {
}
void DVIToSVGActions::postamble () {
}
/** This method is called when a "begin of page (bop)" command was found in the DVI file.
* @param[in] c array with 10 components representing \count0 ... \count9. c[0] contains the
* current (printed) page number (may differ from page count) */
void DVIToSVGActions::beginPage (unsigned n, Int32 *c) {
_svg.newPage(++_pageCount);
_bbox = BoundingBox(); // clear bounding box
if (_boxes)
_boxes->clear();
}
/** This method is called when an "end of page (eop)" command was found in the DVI file. */
void DVIToSVGActions::endPage () {
if (_pageMatrix)
_svg.transformPage(*_pageMatrix);
if (_bgcolor != Color::WHITE) {
XMLElementNode *r = new XMLElementNode("rect");
r->addAttribute("x", _bbox.minX());
r->addAttribute("y", _bbox.minY());
r->addAttribute("width", _bbox.width());
r->addAttribute("height", _bbox.height());
r->addAttribute("fill", _bgcolor.rgbString());
_svg.prependToPage(r);
}
}
void DVIToSVGActions::setBgColor (const Color &color) {
_bgcolor = color;
}
void DVIToSVGActions::embed(const BoundingBox& bbox) {
_bbox.embed(bbox);
if (_boxes) {
FORALL(*_boxes, BoxMap::iterator, it)
it->second.embed(bbox);
}
}
void DVIToSVGActions::embed(const DPair& p, double r) {
if (r == 0)
_bbox.embed(p);
else
_bbox.embed(p, r);
if (_boxes)
FORALL(*_boxes, BoxMap::iterator, it)
it->second.embed(p, r);
}
BoundingBox& DVIToSVGActions::bbox(const string& name, bool reset) {
if (!_boxes)
_boxes = new BoxMap;
BoundingBox &box = (*_boxes)[name];
if (reset)
box = BoundingBox();
return box;
}
/** This method is called by subprocesses like the PS handler when
* a computation step has finished. Since the total number of steps
* can't be determined in advance, we don't show a percent value but
* a rotating dash. */
void DVIToSVGActions::progress (const char *id) {
if (PROGRESSBAR_DELAY < 1000) {
static double time=0;
// slow down updating of the progress indicator to prevent flickering
if (System::time() - time > 0.1) {
progress(0, 0, id);
time = System::time();
}
}
}
/** Returns the number of digits of a given integer. */
static int digits (int n) {
if (n == 0)
return 1;
if (n > 0)
return log10(double(n))+1;
return log10(double(-n))+2;
}
/** Draws a simple progress indicator.
* @param[in] current current iteration step (of 'total' steps)
* @param[in] total total number of iteration steps
* @param[in] id ID of the subprocess providing the information */
void DVIToSVGActions::progress (size_t current, size_t total, const char *id) {
static double time=0;
static bool draw=false; // show progress indicator?
static int step = -1; // >=0: rotating dash
static size_t prev_current=0, prev_total=1;
static const char *prev_id=0;
const char *tips = "-\\|/";
if (current == 0 && total > 0) {
time = System::time();
draw = false;
Message::mstream() << '\n';
}
// don't show the progress indicator before the given time has elapsed
if (!draw && System::time()-time > PROGRESSBAR_DELAY)
draw = true;
if (draw && (System::time() - time > 0.1 || (total > 0 && current == total) || prev_id != id)) {
if (total == 0) {
current = prev_current;
total = prev_total;
step = (step+1) % 4;
}
else {
prev_current = current;
prev_total = total;
step = -1;
}
// adapt length of progress indicator to terminal width
int cols = Terminal::columns();
int width = (cols == 0 || cols > 60) ? 50 : 49-60+cols;
double factor = double(current)/total;
int length = width*factor;
Message::mstream(false, Terminal::MAGENTA)
<< '[' << string(length, '=')
<< (factor < 1.0 ? (step < 0 ? ' ' : tips[step]) : '=')
<< string(width-length, ' ')
<< "] " << string(3-digits(100*factor), ' ') << int(100*factor)
<< "%\r";
// overprint indicator when finished
if (factor == 1.0)
Message::estream().clearline();
time = System::time();
prev_id = id;
}
}
|