~ubuntu-branches/ubuntu/karmic/gnash/karmic

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
// 
//   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
// 
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA




#include "DynamicShape.h"

#include <algorithm>


namespace gnash {

DynamicShape::DynamicShape()
	:
	shape_character_def(),
	_currpath(0),
	_currfill(0),
	_currline(0),
	_x(0),
	_y(0),
	_changed(0)
{}

void
DynamicShape::clear()
{
	//clear_meshes();
	m_paths.clear();
	m_fill_styles.clear();
	m_line_styles.clear();
	m_bound.set_null();
	_currpath=0; // or would point to invalid memory
	_currfill = _currline = 0; // or would point to cleared m_fill_style and m_line_styles respectively

	// TODO: worth setting _changed=true ? 
}

void
DynamicShape::add_path(const path& pth)
{
	m_paths.push_back(pth);
	_currpath = &(m_paths.back());
	//compute_bound(&m_bound);
}

void
DynamicShape::endFill()
{
	// Close the path
	if ( _currpath ) _currpath->close();

	// Remove reference to the "current" path, as
	// next drawing will happen on a different one
	_currpath = NULL;

	// Remove fill information
	_currfill = 0;

	// TODO: should I also clear _currline ?
}

void
DynamicShape::beginFill(const rgba& color)
{
	// Add the new fill style and set as current
	fill_style style; style.setSolid(color);

	endFill();

	_currfill = add_fill_style(style);
	// TODO: how to know wheter the fill should be set
	//       as *left* or *right* fill ?
	//       A quick test shows that *left* always work fine !
	path newPath(_x, _y, _currfill, 0, _currline, true); // new fill start new subshapes
	add_path(newPath);
}

void
DynamicShape::beginLinearGradientFill(const std::vector<gradient_record>& grad, const matrix& mat)
{
	// Add the new fill style and set as current
	fill_style style; style.setLinearGradient(grad, mat);

	endFill();

	_currfill = add_fill_style(style);
	// TODO: how to know wheter the fill should be set
	//       as *left* or *right* fill ?
	//       A quick test shows that *left* always work fine !
	path newPath(_x, _y, _currfill, 0, _currline, true); // new fill start new subshapes
	add_path(newPath);
}

void
DynamicShape::beginRadialGradientFill(const std::vector<gradient_record>& grad, const matrix& mat)
{
	// Add the new fill style and set as current
	fill_style style; style.setRadialGradient(grad, mat);

	endFill();

	_currfill = add_fill_style(style);
	// TODO: how to know wheter the fill should be set
	//       as *left* or *right* fill ?
	//       A quick test shows that *left* always work fine !
	path newPath(_x, _y, _currfill, 0, _currline, true); // new fill start new subshapes
	add_path(newPath);
}

void
DynamicShape::startNewPath(bool newShape)
{
	// Close any pending filled path
	if ( _currpath && _currfill) _currpath->close();

	// The DrawingApiTest.swf file shows we should NOT
	// necessarely end the current fill when starting a new one.
	//endFill();

	// A quick test shows that *left* always work fine !
	// More than that, using a *right* fill seems to break the tests !
	path newPath(_x, _y, _currfill, 0, _currline, newShape);
	add_path(newPath);
}

void
DynamicShape::finalize()
{
	// Nothing to do if not changed
	if ( ! _changed ) return;

	// Close any pending filled path (_currpath should be last path)
	if ( _currpath && _currfill)
	{
		assert( ! m_paths.empty() );
		assert( _currpath == &(m_paths.back()) );
		_currpath->close();
	}

	// TODO: check consistency of fills and such !

	_changed = false;
}

void
DynamicShape::lineStyle(boost::uint16_t thickness, const rgba& color,
	bool vScale, bool hScale, bool pixelHinting, bool noClose,
	cap_style_e startCapStyle, cap_style_e endCapStyle,
	join_style_e joinStyle, float miterLimitFactor)
{
	line_style style(thickness, color, vScale, hScale, pixelHinting,
		noClose, startCapStyle, endCapStyle, joinStyle,
		miterLimitFactor);

	_currline = add_line_style(style);
	startNewPath(false); // don't make this the start of a new subshape (to verify)
}

void
DynamicShape::resetLineStyle()
{
	_currline = 0;
	startNewPath(false); // don't make this the start of a new subshape (to verify)
}

void
DynamicShape::moveTo(boost::int32_t x, boost::int32_t y)
{
	if ( x != _x || y != _y )
	{
		_x = x;
		_y = y;

		// TODO: close previous path if any and filled ?
		startNewPath(false); // don't make this the start of a new subshape (to verify)
	}
}

void
DynamicShape::lineTo(boost::int32_t x, boost::int32_t y, int swfVersion)
{
	if ( ! _currpath ) startNewPath(true); // first shape is always new (I hope this doesn't break anything)
	assert(_currpath);

	_currpath->drawLineTo(x, y);

	// Update bounds 
	unsigned thickness = _currline ? m_line_styles[_currline-1].getThickness() : 0;
	if ( _currpath->size() == 1 ) {
		_currpath->expandBounds(m_bound, thickness, swfVersion);
	} else {
		m_bound.expand_to_circle(x, y, swfVersion < 8 ? thickness : thickness/2.0);
	}
    
	// Update current pen position
	_x = x;
	_y = y;

	// Mark as changed
	changed();
}

void
DynamicShape::curveTo(boost::int32_t cx, boost::int32_t cy, 
                      boost::int32_t ax, boost::int32_t ay, int swfVersion)
{
	if ( ! _currpath ) startNewPath(true); // first shape is always new (I hope this doesn't break anything)
	assert(_currpath);

	_currpath->drawCurveTo(cx, cy, ax, ay);

	// Update bounds 
	unsigned thickness = _currline ? m_line_styles[_currline-1].getThickness() : 0;
	if ( _currpath->size() == 1 ) {
		_currpath->expandBounds(m_bound, thickness, swfVersion);
	} else {
		m_bound.expand_to_circle(ax, ay, swfVersion < 8 ? thickness : thickness/2.0);
		m_bound.expand_to_circle(cx, cy, swfVersion < 8 ? thickness : thickness/2.0);
	}

	// Update current pen position
	_x = ax;
	_y = ay;

	// Mark as changed
	changed();
}

size_t
DynamicShape::add_fill_style(const fill_style& stl)
{
	typedef FillStyleVect V;
	V& v=m_fill_styles;

	// TODO: check if the style is already in our list
	//       (needs operator== defined for fill_style)
	v.push_back(stl);
	return v.size(); // 1-based !
}

size_t
DynamicShape::add_line_style(const line_style& stl)
{
	typedef LineStyleVect V;
	V& v=m_line_styles;

	// TODO: check if the style is already in our list
	//       (needs operator== defined for line_style)
	v.push_back(stl);
	return v.size(); // 1-based !
}
	
}	// end namespace gnash


// Local Variables:
// mode: C++
// indent-tabs-mode: t
// End: