summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/optimizer/TransformSimplifier.cpp
blob: 06d28926d4d649faef2aad120c906795d9dfb5c1 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
/*************************************************************************
** TransformSimplifier.cpp                                              **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2019 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 "TransformSimplifier.hpp"
#include "../Matrix.hpp"
#include "../utility.hpp"
#include "../XMLNode.hpp"
#include "../XMLString.hpp"

using namespace std;

const char* TransformSimplifier::info () const {
	return "try to simplify and shorten the values of 'transform' attributes";
}


/** Tries to simplify the transform attributes of the context node and all its descendants. */
void TransformSimplifier::execute (XMLElement *context) {
	if (!context)
		return;
	if (const char *transform = context->getAttributeValue("transform")) {
		Matrix matrix = Matrix::parseSVGTransform(transform);
		string decomp = decompose(matrix);
		if (decomp.length() > matrix.toSVG().length())
			context->addAttribute("transform", matrix.toSVG());
		else {
			if (decomp.empty())
				context->removeAttribute("transform");
			else
				context->addAttribute("transform", decomp);
		}
	}
	// continue with child elements
	for (XMLNode *child : *context) {
		if (XMLElement *elem = child->toElement())
			execute(elem);
	}
}


static string translate_cmd (double dx, double dy) {
	string ret;
	XMLString dxstr(dx), dystr(dy);
	if (dxstr != "0" || dystr != "0") {
		ret = "translate("+dxstr;
		if (dystr != "0")
			ret += " "+dystr;
		ret += ')';
	}
	return ret;
}


static string scale_cmd (double sx, double sy) {
	string ret;
	XMLString sxstr(sx), systr(sy);
	if (sxstr != "1" || systr != "1") {
		ret = "scale("+sxstr;
		if (systr != "1")
			ret += " "+systr;
		ret += ')';
	}
	return ret;
}


static string rotate_cmd (double rad) {
	string ret;
	XMLString degstr(math::rad2deg(fmod(rad, math::TWO_PI)));
	if (degstr != "0")
		ret = "rotate("+degstr+")";
	return ret;
}


static string skewx_cmd (double rad) {
	string ret;
	XMLString degstr(math::rad2deg(fmod(rad, math::PI)));
	if (degstr != "0")
		ret = "skewX("+degstr+")";
	return ret;
}


static string skewy_cmd (double rad) {
	string ret;
	XMLString degstr(math::rad2deg(fmod(rad, math::PI)));
	if (degstr != "0")
		ret = "skewY("+degstr+")";
	return ret;
}


static bool not_equal (double x, double y) {
	return abs(x-y) >= 1e-6;
}


/** Decomposes a transformation matrix into a sequence of basic SVG transformations, i.e.
 *  translation, rotation, scaling, and skewing. The algorithm (QR-based decomposition)
 *  is taken from http://frederic-wang.fr/decomposition-of-2d-transform-matrices.html.
 *  @param[in] matrix matrix to decompose
 *  @return string containing the SVG transformation commands */
string TransformSimplifier::decompose (const Matrix &matrix) {
	// transformation matrix [a b c d e f] according to
	// https://www.w3.org/TR/SVG11/coords.html#EstablishingANewUserSpace
	double a = matrix.get(0, 0);
	double b = matrix.get(1, 0);
	double c = matrix.get(0, 1);
	double d = matrix.get(1, 1);
	double e = matrix.get(0, 2);
	double f = matrix.get(1, 2);
	string ret = translate_cmd(e, f);
	double delta = a*d - b*c;
	if (not_equal(a, 0) || not_equal(b, 0)) {
		double r = sqrt(a*a + b*b);
		ret += rotate_cmd(b > 0 ? acos(a/r) : -acos(a/r));
		ret += scale_cmd(r, delta/r);
		ret += skewx_cmd(atan((a*c + b*d)/(r*r)));
	}
	else if (not_equal(c, 0) || not_equal(d, 0)) {
		double s = sqrt(c*c + d*d);
		ret += rotate_cmd(math::HALF_PI - (d > 0 ? acos(-c/s) : -acos(c/s)));
		ret += scale_cmd(delta/s, s);
		ret += skewy_cmd(atan((a*c + b*d)/(s*s)));
	}
	else
		ret += scale_cmd(0, 0);
	return ret;
}