+ implement offset

```
offset = "<number> | <percentage>"
```
This commit is contained in:
artemp 2013-03-21 12:10:08 +00:00
parent 542805e4d6
commit fe092ac7a2
4 changed files with 56 additions and 17 deletions

View file

@ -417,9 +417,15 @@ void apply_filter(Src & src, colorize_alpha const& op)
double step = 1.0/(size-1); double step = 1.0/(size-1);
double offset = 0.0; double offset = 0.0;
BOOST_FOREACH( mapnik::color const& c, op) BOOST_FOREACH( mapnik::filter::color_stop const& stop, op)
{ {
grad_lut.add_color(offset, agg::rgba(c.red()/256.0, mapnik::color const& c = stop.color;
double stop_offset = stop.offset;
if (stop_offset == 0)
{
stop_offset = offset;
}
grad_lut.add_color(stop_offset, agg::rgba(c.red()/256.0,
c.green()/256.0, c.green()/256.0,
c.blue()/256.0, c.blue()/256.0,
c.alpha()/256.0)); c.alpha()/256.0));

View file

@ -25,17 +25,40 @@
// boost // boost
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
// mapnik // mapnik
#include <mapnik/css_color_grammar.hpp> #include <mapnik/css_color_grammar.hpp>
#include <mapnik/image_filter.hpp> #include <mapnik/image_filter.hpp>
// stl // stl
#include <vector> #include <vector>
BOOST_FUSION_ADAPT_STRUCT(
mapnik::filter::color_stop,
(mapnik::color, color )
(double, offset)
)
namespace mapnik { namespace mapnik {
namespace qi = boost::spirit::qi; namespace qi = boost::spirit::qi;
struct percent_offset_impl
{
template <typename T>
struct result
{
typedef double type;
};
double operator() (double val) const
{
double result = std::abs(val/100.0);
if (result > 1.0) result = 1.0;
return result;
}
};
template <typename Iterator, typename ContType> template <typename Iterator, typename ContType>
struct image_filter_grammar : struct image_filter_grammar :
qi::grammar<Iterator, ContType(), qi::ascii::space_type> qi::grammar<Iterator, ContType(), qi::ascii::space_type>
@ -46,10 +69,12 @@ struct image_filter_grammar :
qi::rule<Iterator, qi::locals<int,int>, void(ContType&), qi::ascii::space_type> agg_blur_filter; qi::rule<Iterator, qi::locals<int,int>, void(ContType&), qi::ascii::space_type> agg_blur_filter;
qi::rule<Iterator, qi::locals<double,double,double,double,double,double,double,double>, qi::rule<Iterator, qi::locals<double,double,double,double,double,double,double,double>,
void(ContType&), qi::ascii::space_type> hsla_filter; void(ContType&), qi::ascii::space_type> hsla_filter;
qi::rule<Iterator, qi::locals<mapnik::filter::colorize_alpha>, void(ContType&), qi::ascii::space_type> colorize_alpha_filter; qi::rule<Iterator, qi::locals<mapnik::filter::colorize_alpha, mapnik::filter::color_stop>, void(ContType&), qi::ascii::space_type> colorize_alpha_filter;
qi::rule<Iterator, qi::ascii::space_type> no_args; qi::rule<Iterator, qi::ascii::space_type> no_args;
qi::uint_parser< unsigned, 10, 1, 3 > radius_; qi::uint_parser< unsigned, 10, 1, 3 > radius_;
css_color_grammar<Iterator> css_color_; css_color_grammar<Iterator> css_color_;
qi::rule<Iterator,void(mapnik::filter::color_stop &),qi::ascii::space_type> color_stop_offset;
phoenix::function<percent_offset_impl> percent_offset;
}; };
} }

View file

@ -98,16 +98,18 @@ struct hsla
double a1; double a1;
}; };
struct colorize_alpha : std::vector<mapnik::color> struct color_stop
{
color_stop() {}
color_stop(mapnik::color const& c, double val = 0.0)
: color(c),offset(val) {}
mapnik::color color;
double offset;
};
struct colorize_alpha : std::vector<color_stop>
{ {
colorize_alpha() {} colorize_alpha() {}
colorize_alpha(mapnik::color const& c0, mapnik::color const& c1)
{
this->push_back(c0);
this->push_back(c1);
// TODO: support multiple color-stops
// https://developer.mozilla.org/en-US/docs/CSS/linear-gradient
}
}; };
typedef boost::variant<filter::blur, typedef boost::variant<filter::blur,

View file

@ -57,7 +57,7 @@ image_filter_grammar<Iterator,ContType>::image_filter_grammar()
using boost::spirit::ascii::string; using boost::spirit::ascii::string;
using phoenix::push_back; using phoenix::push_back;
using phoenix::construct; using phoenix::construct;
using phoenix::at_c;
#if BOOST_VERSION >= 104700 #if BOOST_VERSION >= 104700
using qi::no_skip; using qi::no_skip;
start = -(filter % no_skip[*char_(", ")]) start = -(filter % no_skip[*char_(", ")])
@ -111,11 +111,17 @@ image_filter_grammar<Iterator,ContType>::image_filter_grammar()
colorize_alpha_filter = lit("colorize-alpha")[_a = construct<mapnik::filter::colorize_alpha>()] colorize_alpha_filter = lit("colorize-alpha")[_a = construct<mapnik::filter::colorize_alpha>()]
>> lit('(') >> lit('(')
>> css_color_[push_back(_a, _1)] >> (css_color_[at_c<0>(_b) = _1, at_c<1>(_b) = 0]
>> +(lit(',') >> css_color_[push_back(_a, _1)]) >> -color_stop_offset(_b)) [push_back(_a,_b)]
>> +(lit(',') >> css_color_[at_c<0>(_b) =_1,at_c<1>(_b) = 0]
>> -color_stop_offset(_b))[push_back(_a,_b)]
>> lit(')') [push_back(_r1,_a)] >> lit(')') [push_back(_r1,_a)]
; ;
color_stop_offset = (double_ >> lit('%'))[at_c<1>(_r1) = percent_offset(_1)]
|
double_[at_c<1>(_r1) = _1]
;
no_args = -(lit('(') >> lit(')')); no_args = -(lit('(') >> lit(')'));
} }