summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/SVGCharPathHandler.cpp
blob: 2aa9209ddaf08639b157925ae0974eebf3f9abd8 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*************************************************************************
** SVGCharPathHandler.cpp                                               **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2021 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 "utility.hpp"
#include "SVGElement.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)
{
	if (createUseElements)
		_appendChar = &SVGCharPathHandler::appendUseElement;
	else
		_appendChar = &SVGCharPathHandler::appendPathElement;
}


void SVGCharPathHandler::resetContextNode () {
	SVGCharHandler::resetContextNode();
	_groupNode = nullptr;
}


/** 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->isIdentity();
	bool applyOpacity = !_opacity->isFillDefault();
	if (!_groupNode) {
		color.changed(applyColor);
		_matrix.changed(applyMatrix);
	}
	if (color.changed() || _matrix.changed() || _opacity.changed()) {
		resetContextNode();
		if (applyColor || applyMatrix || applyOpacity) {
			_groupNode = pushContextNode(util::make_unique<SVGElement>("g"));
			contextNode()->setFillColor(color);
			contextNode()->setFillOpacity(_opacity->fillalpha());
			contextNode()->setTransform(_matrix);
		}
		color.changed(false);
		_matrix.changed(false);
		_opacity.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 (auto pf = font_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, nullptr);
			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) {
	string id = "#g" + to_string(FontManager::instance().fontID(_font)) + "-" + to_string(c);
	auto useNode = util::make_unique<SVGElement>("use");
	useNode->addAttribute("x", x);
	useNode->addAttribute("y", y);
	useNode->addAttribute("xlink:href", id);
	useNode->setFillOpacity(_opacity->blendMode()); // add blend mode style here because it's not inheritable
	useNode->setTransform(matrix);
	contextNode()->append(std::move(useNode));
}


void SVGCharPathHandler::appendPathElement (uint32_t c, double x, double y, const Matrix &matrix) {
	Glyph glyph;
	auto pf = font_cast<const PhysicalFont*>(_font.get());
	if (pf && pf->getGlyph(c, glyph, nullptr)) {
		double sx = pf->scaledSize()/pf->unitsPerEm();
		double sy = -sx;
		ostringstream oss;
		glyph.writeSVG(oss, _relativePathCommands, sx, sy, x, y);
		auto glyphNode = util::make_unique<SVGElement>("path");
		glyphNode->addAttribute("d", oss.str());
		glyphNode->setTransform(matrix);
		contextNode()->append(std::move(glyphNode));
	}
}