improve handling of colors as expressions

This commit is contained in:
Dane Springmeyer 2014-05-08 00:45:18 -07:00
parent 88701b8669
commit 7364a30e67
9 changed files with 26 additions and 22 deletions

View file

@ -25,14 +25,13 @@
// mapnik
#include <mapnik/color.hpp>
#include <mapnik/css_color_grammar.hpp>
//stl
#include <string>
namespace mapnik {
MAPNIK_DECL mapnik::color parse_color(std::string const& str);
MAPNIK_DECL mapnik::color parse_color(std::string const& str, mapnik::css_color_grammar<std::string::const_iterator> const& g);
}

View file

@ -194,7 +194,11 @@ struct evaluate_global_attributes : mapnik::noncopyable
void operator() (expression_ptr const& expr) const
{
auto const& meta = get_meta(prop_.first);
boost::apply_visitor(assign_value<expression_ptr,Attributes>(prop_.second, expr, attributes_), std::get<1>(meta));
try {
boost::apply_visitor(assign_value<expression_ptr,Attributes>(prop_.second, expr, attributes_), std::get<1>(meta));
} catch (std::exception const& ex) {
// no-op
}
}
template <typename T>

View file

@ -261,6 +261,8 @@ struct evaluate_expression_wrapper<mapnik::color>
mapnik::color operator() (T1 const& expr, T2 const& feature) const
{
mapnik::value_type val = boost::apply_visitor(mapnik::evaluate<mapnik::feature_impl,mapnik::value_type>(feature), expr);
// FIXME - throw instead?
if (val.is_null()) return mapnik::color(255,192,203); // pink
return mapnik::color(val.to_string());
}
};

View file

@ -27,25 +27,26 @@
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
namespace mapnik { namespace util {
template <typename Iterator>
bool parse_dasharray(Iterator first, Iterator last, std::vector<double>& dasharray)
{
using namespace boost::spirit;
qi::double_type double_;
qi::_1_type _1;
qi::lit_type lit;
qi::char_type char_;
qi::ascii::space_type space;
qi::no_skip_type no_skip;
using phoenix::push_back;
// SVG
// dasharray ::= (length | percentage) (comma-wsp dasharray)?
// no support for 'percentage' as viewport is unknown at load_map
//
bool r = qi::phrase_parse(first, last,
(double_[push_back(phoenix::ref(dasharray), _1)] %
(double_[boost::phoenix::push_back(boost::phoenix::ref(dasharray), _1)] %
no_skip[char_(", ")]
| lit("none")),
space);

View file

@ -31,6 +31,11 @@
// boost
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
// stl
#include <sstream>
@ -53,11 +58,11 @@ std::string color::to_string() const
std::back_insert_iterator<std::string> sink(str);
karma::generate(sink,
// begin grammar
kstring[ phoenix::if_(alpha()==255) [_1="rgb("].else_[_1="rgba("]]
kstring[ boost::phoenix::if_(alpha()==255) [_1="rgb("].else_[_1="rgba("]]
<< color_generator[_1 = red()] << ','
<< color_generator[_1 = green()] << ','
<< color_generator[_1 = blue()]
<< kstring[ phoenix::if_(alpha()==255) [_1 = ')'].else_[_1 =',']]
<< kstring[ boost::phoenix::if_(alpha()==255) [_1 = ')'].else_[_1 =',']]
<< eps(alpha()<255) << double_ [_1 = alpha()/255.0]
<< ')'
// end grammar

View file

@ -30,13 +30,8 @@ namespace mapnik {
color parse_color(std::string const& str)
{
// TODO - early return for @color?
static const css_color_grammar<std::string::const_iterator> g;
return parse_color(str, g);
}
color parse_color(std::string const& str,
css_color_grammar<std::string::const_iterator> const& g)
{
color c;
std::string::const_iterator first = str.begin();
std::string::const_iterator last = str.end();
@ -50,7 +45,7 @@ color parse_color(std::string const& str,
}
else
{
throw config_error( "Failed to parse color: \"" + str + "\"" );
throw config_error("Failed to a parse color: \"" + str + "\"");
}
}

View file

@ -26,9 +26,9 @@
#include <mapnik/unicode.hpp>
#include <mapnik/expression_node_types.hpp>
#include <mapnik/expression_grammar.hpp>
#include <boost/spirit/include/qi.hpp>
// boost
#include <boost/spirit/include/qi.hpp>
namespace mapnik
{
@ -47,7 +47,7 @@ expression_ptr parse_expression(std::string const& str, std::string const& encod
}
else
{
throw config_error( "Failed to parse expression: \"" + str + "\"" );
throw config_error("Failed to parse expression: \"" + str + "\"");
}
}

View file

@ -1021,8 +1021,7 @@ void map_parser::parse_markers_symbolizer(rule & rule, xml_node const& sym)
put(symbol, keys::image_transform, mapnik::parse_transform(*image_transform_wkt));
}
optional<color> c = sym.get_opt_attr<color>("fill");
if (c) put(symbol, keys::fill, *c);
set_symbolizer_property<markers_symbolizer,color>(symbol, keys::fill, sym);
optional<double> spacing = sym.get_opt_attr<double>("spacing");
if (spacing) put(symbol,keys::spacing, *spacing);
@ -1406,8 +1405,7 @@ void map_parser::parse_building_symbolizer(rule & rule, xml_node const & sym)
building_symbolizer building_sym;
// fill
optional<color> fill = sym.get_opt_attr<color>("fill");
if (fill) put(building_sym, keys::fill, *fill);
set_symbolizer_property<building_symbolizer,color>(building_sym, keys::fill, sym);
// fill-opacity
set_symbolizer_property<building_symbolizer,double>(building_sym, keys::fill_opacity, sym);
// height

View file

@ -42,9 +42,9 @@ static const property_meta_type key_meta[to_integral(keys::MAX_SYMBOLIZER_KEY)]
property_meta_type{ "comp-op", enumeration_wrapper(src_over),
[](enumeration_wrapper e) { return *comp_op_to_string(composite_mode_e(e.value)); }, property_types::target_comp_op},
property_meta_type{ "clip", false, nullptr, property_types::target_bool},
property_meta_type{ "fill", mapnik::color("gray"), nullptr, property_types::target_color},
property_meta_type{ "fill", mapnik::color(128,128,128), nullptr, property_types::target_color},
property_meta_type{ "fill-opacity", 1.0 , nullptr, property_types::target_double},
property_meta_type{ "stroke", mapnik::color("black"), nullptr, property_types::target_color},
property_meta_type{ "stroke", mapnik::color(0,0,0), nullptr, property_types::target_color},
property_meta_type{ "stroke-width", 1.0 , nullptr, property_types::target_double},
property_meta_type{ "stroke-opacity", 1.0, nullptr, property_types::target_double},
property_meta_type{ "stroke-linejoin", enumeration_wrapper(MITER_JOIN),