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
|
/*************************************************************************
** SVGCharPathHandler.cpp **
** **
** This file is part of dvisvgm -- a fast DVI to SVG converter **
** Copyright (C) 2005-2017 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 <sstream>
#include "Font.hpp"
#include "FontManager.hpp"
#include "SVGCharPathHandler.hpp"
#include "XMLNode.hpp"
using namespace std;
/** Constructs a new path builder.
* @param[in] createUseElements determines whether to create "use" elements to reference previous paths or not
* @param[in] relativePathCommands determines whether to create relative or absolute SVG path commands */
SVGCharPathHandler::SVGCharPathHandler (bool createUseElements, bool relativePathCommands)
: _relativePathCommands(relativePathCommands), _groupNode(0), _fontColor(Color::BLACK)
{
if (createUseElements)
_appendChar = &SVGCharPathHandler::appendUseElement;
else
_appendChar = &SVGCharPathHandler::appendPathElement;
}
void SVGCharPathHandler::resetContextNode () {
SVGCharHandler::resetContextNode();
_groupNode = 0;
}
/** Appends the path representation of a single character to the context element.
* @param[in] c code of the character to be appended
* @param[in] x horizontal position of the character (in bp units)
* @param[in] y vertical position of the character (in bp units) */
void SVGCharPathHandler::appendChar (uint32_t c, double x, double y) {
if (_font.changed()) {
_fontColor.set(_font.get()->color());
if (_fontColor.changed() && _fontColor.get() != Color::BLACK)
_color.changed(true); // ensure application of text color when resetting the font color to black
_font.changed(false);
}
// Apply text color changes only if the color of the entire font is black.
// Glyphs of non-black fonts (e.g. defined in a XeTeX document) can't change their color.
CharProperty<Color> &color = (_fontColor.get() != Color::BLACK) ? _fontColor : _color;
bool applyColor = color.get() != Color::BLACK;
bool applyMatrix = !_matrix.get().isIdentity();
if (!_groupNode) {
color.changed(applyColor);
_matrix.changed(applyMatrix);
}
if (color.changed() || _matrix.changed()) {
resetContextNode();
if (applyColor || applyMatrix) {
_groupNode = new XMLElementNode("g");
pushContextNode(_groupNode);
if (applyColor)
contextNode()->addAttribute("fill", color.get().svgColorString());
if (applyMatrix)
contextNode()->addAttribute("transform", _matrix.get().getSVG());
}
color.changed(false);
_matrix.changed(false);
}
const Font *font = _font.get();
if (font->verticalLayout()) {
// move glyph graphics so that its origin is located at the top center position
GlyphMetrics metrics;
font->getGlyphMetrics(c, _vertical, metrics);
x -= metrics.wl;
if (const PhysicalFont *pf = dynamic_cast<const PhysicalFont*>(font)) {
// Center glyph between top and bottom border of the TFM box.
// This is just an approximation used until I find a way to compute
// the exact location in vertical mode.
GlyphMetrics exact_metrics;
pf->getExactGlyphBox(c, exact_metrics, false);
y += exact_metrics.h+(metrics.d-exact_metrics.h-exact_metrics.d)/2;
}
else
y += metrics.d;
}
Matrix rotation(1);
if (_vertical && !font->verticalLayout()) {
// alphabetic text designed for horizontal mode
// must be rotated by 90 degrees if in vertical mode
rotation.translate(-x, -y);
rotation.rotate(90);
rotation.translate(x, y);
}
(this->*_appendChar)(c, x, y, rotation);
}
void SVGCharPathHandler::appendUseElement (uint32_t c, double x, double y, const Matrix &matrix) {
ostringstream oss;
oss << "#g" << FontManager::instance().fontID(_font) << '-' << c;
XMLElementNode *useNode = new XMLElementNode("use");
useNode->addAttribute("x", XMLString(x));
useNode->addAttribute("y", XMLString(y));
useNode->addAttribute("xlink:href", oss.str());
if (!matrix.isIdentity())
useNode->addAttribute("transform", matrix.getSVG());
contextNode()->append(useNode);
}
void SVGCharPathHandler::appendPathElement (uint32_t c, double x, double y, const Matrix &matrix) {
Glyph glyph;
const PhysicalFont *pf = dynamic_cast<const PhysicalFont*>(_font.get());
if (pf && pf->getGlyph(c, glyph)) {
double sx = pf->scaledSize()/pf->unitsPerEm();
double sy = -sx;
ostringstream oss;
glyph.writeSVG(oss, _relativePathCommands, sx, sy, x, y);
XMLElementNode *glyphNode = new XMLElementNode("path");
glyphNode->addAttribute("d", oss.str());
if (!matrix.isIdentity())
glyphNode->addAttribute("transform", matrix.getSVG());
contextNode()->append(glyphNode);
}
}
|