From 7364a30e678f7a57442fe5003d14e4bfc8493e9e Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 8 May 2014 00:45:18 -0700 Subject: [PATCH] improve handling of colors as expressions --- include/mapnik/color_factory.hpp | 3 +-- include/mapnik/evaluate_global_attributes.hpp | 6 +++++- include/mapnik/symbolizer.hpp | 2 ++ include/mapnik/util/dasharray_parser.hpp | 5 +++-- src/color.cpp | 9 +++++++-- src/color_factory.cpp | 9 ++------- src/expression.cpp | 4 ++-- src/load_map.cpp | 6 ++---- src/symbolizer_keys.cpp | 4 ++-- 9 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/mapnik/color_factory.hpp b/include/mapnik/color_factory.hpp index 312a4b3ec..033363472 100644 --- a/include/mapnik/color_factory.hpp +++ b/include/mapnik/color_factory.hpp @@ -25,14 +25,13 @@ // mapnik #include -#include +//stl #include 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 const& g); } diff --git a/include/mapnik/evaluate_global_attributes.hpp b/include/mapnik/evaluate_global_attributes.hpp index dffdcf7cb..5c32a71b3 100644 --- a/include/mapnik/evaluate_global_attributes.hpp +++ b/include/mapnik/evaluate_global_attributes.hpp @@ -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(prop_.second, expr, attributes_), std::get<1>(meta)); + try { + boost::apply_visitor(assign_value(prop_.second, expr, attributes_), std::get<1>(meta)); + } catch (std::exception const& ex) { + // no-op + } } template diff --git a/include/mapnik/symbolizer.hpp b/include/mapnik/symbolizer.hpp index 90ee58bfd..8fed474f5 100644 --- a/include/mapnik/symbolizer.hpp +++ b/include/mapnik/symbolizer.hpp @@ -261,6 +261,8 @@ struct evaluate_expression_wrapper mapnik::color operator() (T1 const& expr, T2 const& feature) const { mapnik::value_type val = boost::apply_visitor(mapnik::evaluate(feature), expr); + // FIXME - throw instead? + if (val.is_null()) return mapnik::color(255,192,203); // pink return mapnik::color(val.to_string()); } }; diff --git a/include/mapnik/util/dasharray_parser.hpp b/include/mapnik/util/dasharray_parser.hpp index 5db02c24f..1c0747413 100644 --- a/include/mapnik/util/dasharray_parser.hpp +++ b/include/mapnik/util/dasharray_parser.hpp @@ -27,25 +27,26 @@ #include #include #include +#include namespace mapnik { namespace util { template bool parse_dasharray(Iterator first, Iterator last, std::vector& 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); diff --git a/src/color.cpp b/src/color.cpp index 7243a240c..1b8a87d73 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -31,6 +31,11 @@ // boost #include #include +#include +#include +#include +#include + // stl #include @@ -53,11 +58,11 @@ std::string color::to_string() const std::back_insert_iterator 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 diff --git a/src/color_factory.cpp b/src/color_factory.cpp index 773c15515..c509c7f85 100644 --- a/src/color_factory.cpp +++ b/src/color_factory.cpp @@ -30,13 +30,8 @@ namespace mapnik { color parse_color(std::string const& str) { + // TODO - early return for @color? static const css_color_grammar g; - return parse_color(str, g); -} - -color parse_color(std::string const& str, - css_color_grammar 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 + "\""); } } diff --git a/src/expression.cpp b/src/expression.cpp index 191685909..d56c338c9 100644 --- a/src/expression.cpp +++ b/src/expression.cpp @@ -26,9 +26,9 @@ #include #include #include -#include // boost +#include 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 + "\""); } } diff --git a/src/load_map.cpp b/src/load_map.cpp index 5edacaf7c..d67a006f7 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -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 c = sym.get_opt_attr("fill"); - if (c) put(symbol, keys::fill, *c); + set_symbolizer_property(symbol, keys::fill, sym); optional spacing = sym.get_opt_attr("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 fill = sym.get_opt_attr("fill"); - if (fill) put(building_sym, keys::fill, *fill); + set_symbolizer_property(building_sym, keys::fill, sym); // fill-opacity set_symbolizer_property(building_sym, keys::fill_opacity, sym); // height diff --git a/src/symbolizer_keys.cpp b/src/symbolizer_keys.cpp index 4e07eb7bc..e488ff2bc 100644 --- a/src/symbolizer_keys.cpp +++ b/src/symbolizer_keys.cpp @@ -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),