-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagg_svg_gradient.h
290 lines (255 loc) · 9.9 KB
/
agg_svg_gradient.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: [email protected]
// http://www.antigrain.com
//----------------------------------------------------------------------------
//
// Gunnar Roth: add support for linear and radial gradients(support xlink attr),
// shape gradient opaqueness, rounded rects, circles,ellipses. support a command (arc) in pathes.
// set new origin correctly to last postion on z command in a path( was set to 0,0 before).
// enable parsing of colors written as rgb()
// some code was inspired by code from Haiku OS
/*
* Copyright 2006-2007, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <[email protected]>
*/
//
#ifndef SVG_GRADIENTS_H
#define SVG_GRADIENTS_H
#include <agg_color_rgba.h>
#include <agg_trans_affine.h>
#include <string.h>
#include <string>
#include <map>
#include "agg_gradient_lut.h"
namespace agg {
namespace svg {
template<class ColorInterpolator,
unsigned ColorLutSize=256> class gradient_lut_opaque
{
public:
typedef ColorInterpolator interpolator_type;
typedef typename interpolator_type::color_type color_type;
enum { color_lut_size = ColorLutSize };
//--------------------------------------------------------------------
gradient_lut_opaque() : m_color_lut(color_lut_size) {}
// Declare an assignment operator from another type.
template<typename W>
gradient_lut_opaque(const W& w);
// Build Gradient Lut
// First, call remove_all(), then add_color() at least twice,
// then build_lut(). Argument "offset" in add_color must be
// in range [0...1] and defines a color stop as it is described
// in SVG specification, section Gradients and Patterns.
// The simplest linear gradient is:
// gradient_lut.add_color(0.0, start_color);
// gradient_lut.add_color(1.0, end_color);
//--------------------------------------------------------------------
void remove_all();
void add_color(double offset, const color_type& color);
void build_lut(double opaque );
// Size-index Interface. This class can be used directly as the
// ColorF in span_gradient. All it needs is two access methods
// size() and operator [].
//--------------------------------------------------------------------
static unsigned size()
{
return color_lut_size;
}
const color_type& operator [] (unsigned i) const
{
return m_color_lut[i];
}
private:
//--------------------------------------------------------------------
struct color_point
{
double offset;
color_type color;
color_point() {}
color_point(double off, const color_type& c) :
offset(off), color(c)
{
if(offset < 0.0) offset = 0.0;
if(offset > 1.0) offset = 1.0;
}
};
typedef agg::pod_bvector<color_point, 4> color_profile_type;
typedef agg::pod_array<color_type> color_lut_type;
static bool offset_less(const color_point& a, const color_point& b)
{
return a.offset < b.offset;
}
static bool offset_equal(const color_point& a, const color_point& b)
{
return a.offset == b.offset;
}
//--------------------------------------------------------------------
color_profile_type m_color_profile;
color_lut_type m_color_lut;
};
//------------------------------------------------------------------------
template<class T, unsigned S>
void gradient_lut_opaque<T,S>::remove_all()
{
m_color_profile.remove_all();
}
//------------------------------------------------------------------------
template<class T, unsigned S>
void gradient_lut_opaque<T,S>::add_color(double offset, const color_type& color)
{
m_color_profile.add(color_point(offset, color));
}
//------------------------------------------------------------------------
template<class T, unsigned S>
void gradient_lut_opaque<T,S>::build_lut(double opaque)
{
quick_sort(m_color_profile, offset_less);
m_color_profile.cut_at(remove_duplicates(m_color_profile, offset_equal));
if(m_color_profile.size() >= 2)
{
unsigned i;
unsigned start = uround(m_color_profile[0].offset * color_lut_size);
unsigned end;
color_type c = m_color_profile[0].color;
c.opacity(c.opacity() * opaque);
for(i = 0; i < start; i++)
{
m_color_lut[i] = c;
}
for(i = 1; i < m_color_profile.size(); i++)
{
end = uround(m_color_profile[i].offset * color_lut_size);
color_type c1 = m_color_profile[i-1].color;
c1.opacity(c1.opacity() * opaque);
color_type c2 = m_color_profile[i].color;
c2.opacity(c2.opacity() * opaque);
interpolator_type ci(c1,
c2 ,
end - start + 1);
while(start < end)
{
m_color_lut[start] = ci.color();
++ci;
++start;
}
}
c = m_color_profile.last().color;
c.opacity(c.opacity() * opaque);
for(; end < m_color_lut.size(); end++)
{
m_color_lut[end] = c;
}
}
}
class gradient {
public:
static const int lut_range = 128;
typedef gradient_lut_opaque<agg::color_interpolator<agg::rgba8>, lut_range * 2> color_func_type;
enum gradients_type {
GRADIENT_LINEAR = 0,
GRADIENT_CIRCULAR,
GRADIENT_CIRCULAR_FOCAL,
GRADIENT_DIAMOND,
GRADIENT_CONIC,
GRADIENT_XY,
GRADIENT_SQRT_XY
};
gradient();
virtual ~gradient();
void set_id(const char* id);
const char* id() const;
void set_opaque(double opaque)
{
if(opaque != m_opaque)
{
m_opaque = opaque;
m_gradient_lut.build_lut(m_opaque);
}
}
virtual void add_stop(double offset, rgba8 color);
void set_transformation(const trans_affine& transform);
virtual void realize() = 0;
virtual gradient *clone() = 0;
void add_string(const char *name, const char *string)
{
m_mapStrings[name] = string;
}
color_func_type &lut()
{
return m_gradient_lut;
}
void set_type(gradients_type type)
{
m_type = type;
}
gradients_type type() const
{ return m_type; }
const trans_affine & transform()
{
return m_transform;
}
unsigned count_colors()
{
return m_colcnt;
}
std::string xlink()
{
std::string xlink;
find_string("xlink:href",&xlink);
return xlink;
}
protected:
bool find_string(const char * key,std::string * pVal ) const
{
std::map<std::string,std::string>::const_iterator it = m_mapStrings.find(key);
if(it == m_mapStrings.end())
return false;
*pVal=it->second;
return true;
}
trans_affine m_transform;
color_func_type m_gradient_lut;
unsigned m_colcnt;
private:
std::map<std::string,std::string> m_mapStrings;
gradients_type m_type;
double m_opaque;
std::string m_id;
};
class linear_gradient : public gradient {
public:
linear_gradient();
virtual ~linear_gradient();
void realize() override;
gradient *clone() override {
return new linear_gradient(*this);
}
};
class radial_gradient : public gradient {
public:
radial_gradient();
virtual ~radial_gradient();
void realize() override;
gradient *clone() override {
return new radial_gradient(*this);
}
};
template<>
template<>
gradient_lut_opaque<color_interpolator<gray8>, 256u>::gradient_lut_opaque(const gradient::color_func_type& rgb_lut);
} // namespace svg
} // namespace agg
#endif // SVG_GRADIENTS_H