1 // FillStyle.h: variant fill styles
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_FILL_STYLE_H
20 #define GNASH_FILL_STYLE_H
22 #include <boost/variant.hpp>
25 #include <boost/intrusive_ptr.hpp>
28 #include "SWFMatrix.h"
33 class movie_definition
;
43 GradientRecord(boost::uint8_t ratio
, const rgba
& color
)
57 /// BitmapFills can refer to a parsed bitmap tag or be constructed from
58 /// bitmap data. They are used for Bitmap characters.
60 /// Presently all members are immutable after construction. It is of course
61 /// possible to change the appearance of the fill by changing the CachedBitmap
64 /// Special member functions (ctor, dtor etc) are not inlined to avoid
65 /// requiring the definition of movie_definition.
67 /// TODO: check the following:
69 /// It may be necessary to allow setting the smoothing policy; the use of
70 /// this should certainly be extended to non-static BitmapFills.
71 class DSOEXPORT BitmapFill
75 /// How to smooth the bitmap.
76 enum SmoothingPolicy
{
77 SMOOTHING_UNSPECIFIED
,
82 /// Whether the fill is tiled or clipped.
84 /// Clipped fills use the edge pixels to fill any area outside the bounds
91 /// Construct a BitmapFill from arbitrary bitmap data.
93 /// TODO: check the smoothing policy here!
94 BitmapFill(Type t
, const CachedBitmap
* bi
, const SWFMatrix
& m
,
97 /// Construct a static BitmapFill using a SWF tag.
98 BitmapFill(SWF::FillType t
, movie_definition
* md
, boost::uint16_t id
,
104 /// Copy a BitmapFill
106 /// The copied BitmapFill refers to the same bitmap id in the same
107 /// movie_definition as the original.
108 BitmapFill(const BitmapFill
& other
);
110 BitmapFill
& operator=(const BitmapFill
& other
);
112 /// Set this fill to a lerp of two other BitmapFills.
113 void setLerp(const BitmapFill
& a
, const BitmapFill
& b
, double ratio
);
115 /// Get the Type of this BitmapFill
117 /// BitmapFills are either tiled or clipped.
122 /// Get the smoothing policy of this BitmapFill.
123 SmoothingPolicy
smoothingPolicy() const {
124 return _smoothingPolicy
;
127 /// Get the actual Bitmap data.
128 const CachedBitmap
* bitmap() const;
130 /// Get the matrix of this BitmapFill.
131 const SWFMatrix
& matrix() const {
139 SmoothingPolicy _smoothingPolicy
;
143 /// A Bitmap, used for dynamic fills and to cache parsed bitmaps.
144 mutable boost::intrusive_ptr
<const CachedBitmap
> _bitmapInfo
;
146 /// The movie definition containing the bitmap
147 movie_definition
* _md
;
149 // The id of the tag containing the bitmap
155 /// TODO: clean this up!
156 class DSOEXPORT GradientFill
160 /// The type of GradientFill
162 /// A Focal fill is a gradient fill with a focal point.
174 typedef std::vector
<GradientRecord
> GradientRecords
;
176 /// Construct a GradientFill
178 /// Optionally the records can be passed here.
180 /// The actual matrix of the gradient depends on the type; the constructor
181 /// handles this, and users should just pass the user matrix.
182 GradientFill(Type t
, const SWFMatrix
& m
,
183 const GradientRecords
& = GradientRecords());
189 const SWFMatrix
& matrix() const {
193 /// Set this fill to a lerp of two other GradientFills.
194 void setLerp(const GradientFill
& a
, const GradientFill
& b
, double ratio
);
196 void setRecords(const GradientRecords
& recs
) {
197 assert(recs
.size() > 1);
201 /// Get the number of records in this GradientFill
202 size_t recordCount() const {
203 return _gradients
.size();
206 /// Query the GradientRecord at the specified index
208 /// There are recordCount() records.
209 const GradientRecord
& record(size_t i
) const {
210 assert(i
< _gradients
.size());
211 return _gradients
[i
];
214 /// Set the focal point.
216 /// Value will be clamped to the range -1..1; callers don't need to check.
217 void setFocalPoint(double d
);
219 /// Get the focal point of this GradientFill
221 /// If the focal point is 0.0, it is a simple radial fill.
222 double focalPoint() const {
226 SpreadMode spreadMode
;
227 SWF::InterpolationMode interpolation
;
232 GradientRecords _gradients
;
237 /// A SolidFill containing one color.
239 /// SolidFills are the simplest fill, containing only a single color.
240 struct DSOEXPORT SolidFill
244 /// Construct a SolidFill.
245 explicit SolidFill(const rgba
& c
)
250 /// Copy a SolidFill.
251 SolidFill(const SolidFill
& other
)
256 /// Set this fill to a lerp of two other SolidFills.
257 void setLerp(const SolidFill
& a
, const SolidFill
& b
, double ratio
) {
258 _color
.set_lerp(a
.color(), b
.color(), ratio
);
261 /// Get the color of the fill.
270 /// FillStyle describes the various fill styles for shapes
272 /// The FillStyle class is effectively a boost::variant, but to allow passing
273 /// FillStyles using a forward declaration (and reducing compile times),
274 /// it's necessary to use a class.
275 class DSOEXPORT FillStyle
279 typedef boost::variant
<BitmapFill
, SolidFill
, GradientFill
> Fill
;
281 /// Construct a FillStyle from any Fill.
283 /// The non-explicit templated contructor allows the same syntax as a
284 /// simple boost::variant:
285 /// FillStyle f = GradientFill();
286 template<typename T
> FillStyle(const T
& f
) : fill(f
) {}
288 FillStyle(const FillStyle
& other
)
297 /// Set the FillStyle to a lerp of a and b.
299 /// Callers must ensure that all FillStyles have exactly the same type! Most
300 /// errors are caught by type-checking and will throw an unhandled exception.
301 void setLerp(FillStyle
& f
, const FillStyle
& a
, const FillStyle
& b
, double t
);
303 DSOEXPORT
std::ostream
& operator<<(std::ostream
& os
,
304 const BitmapFill::SmoothingPolicy
& p
);
313 // indent-tabs-mode: t