summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-1.9.1/src/PSPattern.cpp
blob: 99c60c298279c732041d2e970904d94d5e753d91 (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
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
/*************************************************************************
** PSPattern.cpp                                                        **
**                                                                      **
** This file is part of dvisvgm -- the DVI to SVG converter             **
** Copyright (C) 2005-2015 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 <config.h>
#include <sstream>
#include <vector>
#include "BoundingBox.h"
#include "PSPattern.h"
#include "SpecialActions.h"
#include "SVGTree.h"
#include "XMLNode.h"

using namespace std;


string PSPattern::svgID () const {
	return XMLString("pat")+XMLString(_id);
}


/** Appends the definition of this pattern to the "def" section of the SVG tree. */
void PSPattern::apply (SpecialActions *actions) {
	if (XMLElementNode *pattern = createPatternNode())
		actions->appendToDefs(pattern);
}


/////////////////////////////////////////////////////////////////////////////

PSTilingPattern::PSTilingPattern (int id, BoundingBox &bbox, Matrix &matrix, double xstep, double ystep)
	: PSPattern(id), _bbox(bbox), _matrix(matrix), _xstep(xstep), _ystep(ystep), _groupNode(0)
{
	_groupNode = PSTilingPattern::createGroupNode();
}


PSTilingPattern::~PSTilingPattern () {
	delete _groupNode;
}


/** Creates a new pattern element representing the pattern defined in the PS code. */
XMLElementNode* PSTilingPattern::createPatternNode () const {
	if (!_groupNode)
		return 0;
	BoundingBox box(_bbox.minX(), _bbox.minY(), _bbox.minX()+_xstep, _bbox.minY()+_ystep);
	XMLElementNode *pattern = new XMLElementNode("pattern");
	pattern->addAttribute("id", svgID());
	pattern->addAttribute("x", box.minX());
	pattern->addAttribute("y", box.minY());
	pattern->addAttribute("width", box.width());
	pattern->addAttribute("height", box.height());
	pattern->addAttribute("viewBox", box.toSVGViewBox());
	pattern->addAttribute("patternUnits", "userSpaceOnUse");
	if (!_matrix.isIdentity())
		pattern->addAttribute("patternTransform", _matrix.getSVG());
	if (_xstep < _bbox.width() || _ystep < _bbox.height()) {  // overlapping tiles?
		// disable clipping at the tile borders => tiles become "transparent"
		pattern->addAttribute("overflow", "visible");
	}
	if (XMLElementNode *clip=createClipNode())
		pattern->append(clip);
	pattern->append(_groupNode);
	return pattern;
}


/** Creates a new clip element restricting the drawing area to the
 *  dimensions given in the definition of the pattern. */
XMLElementNode* PSTilingPattern::createClipNode() const {
	XMLElementNode *clip = new XMLElementNode("clipPath");
	clip->addAttribute("id", "pc"+XMLString(psID()));
	XMLElementNode *rect = new XMLElementNode("rect");
	rect->addAttribute("x", _bbox.minX());
	rect->addAttribute("y", _bbox.minY());
	rect->addAttribute("width", _bbox.width());
	rect->addAttribute("height", _bbox.height());
	clip->append(rect);
	return clip;
}


/** Creates a new group element that contains all "drawing" elements that
 *  define the pattern graphic. */
XMLElementNode* PSTilingPattern::createGroupNode () const {
	// add all succeeding path elements to this group
	XMLElementNode *group = new XMLElementNode("g");
	group->addAttribute("clip-path", XMLString("url(#pc")+XMLString(psID())+")");
	return group;
}


void PSTilingPattern::apply (SpecialActions *actions) {
	PSPattern::apply(actions);
	_groupNode = 0;
}


/////////////////////////////////////////////////////////////////////////////

PSColoredTilingPattern::PSColoredTilingPattern (int id, BoundingBox &bbox, Matrix &matrix, double xstep, double ystep)
	: PSTilingPattern(id, bbox, matrix, xstep, ystep)
{
}


/////////////////////////////////////////////////////////////////////////////

PSUncoloredTilingPattern::PSUncoloredTilingPattern (int id, BoundingBox &bbox, Matrix &matrix, double xstep, double ystep)
	: PSTilingPattern(id, bbox, matrix, xstep, ystep), _applied(false)
{
}


PSUncoloredTilingPattern::~PSUncoloredTilingPattern () {
	if (_applied)
		setGroupNode(0);  // prevent deleting the group node in the parent destructor
}


/** Returns an SVG id value that identifies this pattern with the current color applied. */
string PSUncoloredTilingPattern::svgID () const {
	ostringstream oss;
	oss << PSPattern::svgID() << '-' << hex << _currentColor;
	return oss.str();
}


/** Appends the definition of this pattern with the current color applied
 *  to the "def" section of the SVG tree. */
void PSUncoloredTilingPattern::apply (SpecialActions* actions) {
	set<Color>::iterator it=_colors.find(_currentColor);
	if (it == _colors.end()) {
		if (_applied)
			setGroupNode(static_cast<XMLElementNode*>(getGroupNode()->clone()));
		// assign current color to the pattern graphic
		vector<XMLElementNode*> colored_elems;
		const char *attribs[] = {"fill", "stroke"};
		for (int i=0; i < 2; i++) {
			getGroupNode()->getDescendants(0, attribs[i], colored_elems);
			for (vector<XMLElementNode*>::iterator it=colored_elems.begin(); it != colored_elems.end(); ++it)
				if (string((*it)->getAttributeValue(attribs[i])) != "none")
					(*it)->addAttribute(attribs[i], _currentColor.rgbString());
			colored_elems.clear();
		}
		PSPattern::apply(actions);
		_colors.insert(_currentColor);
		_applied = true;
	}
}


XMLElementNode* PSUncoloredTilingPattern::createClipNode() const {
	// only the first instance of this patterns get a clip element
	if (_colors.empty())
		return PSTilingPattern::createClipNode();
	return 0;
}