From 8502019310c635396746e54c98a535b200d42546 Mon Sep 17 00:00:00 2001 From: artemp Date: Wed, 23 Jul 2014 18:09:18 +0100 Subject: [PATCH] more format_properties expessions --- demo/c++/rundemo.cpp | 10 +-- include/mapnik/text/formatting/format.hpp | 11 +-- include/mapnik/text/text_properties.hpp | 19 ++-- src/load_map.cpp | 8 +- src/text/formatting/format.cpp | 49 +++++++---- src/text/placements/simple.cpp | 2 +- src/text/text_properties.cpp | 102 ++++++++++------------ tests/cpp_tests/fontset_runtime_test.cpp | 8 +- 8 files changed, 109 insertions(+), 100 deletions(-) diff --git a/demo/c++/rundemo.cpp b/demo/c++/rundemo.cpp index 533f91c06..999d401fe 100644 --- a/demo/c++/rundemo.cpp +++ b/demo/c++/rundemo.cpp @@ -208,11 +208,11 @@ int main ( int argc , char** argv) { text_symbolizer text_sym; text_placements_ptr placement_finder = std::make_shared(); - placement_finder->defaults.format_properties.face_name = "DejaVu Sans Book"; - placement_finder->defaults.format_properties.text_size = 10.0; - placement_finder->defaults.format_properties.fill = color(0,0,0); - placement_finder->defaults.format_properties.halo_fill = color(255,255,200); - placement_finder->defaults.format_properties.halo_radius = 1; + placement_finder->defaults.format_defaults.face_name = "DejaVu Sans Book"; + placement_finder->defaults.format_defaults.text_size = 10.0; + placement_finder->defaults.format_defaults.fill = color(0,0,0); + placement_finder->defaults.format_defaults.halo_fill = color(255,255,200); + placement_finder->defaults.format_defaults.halo_radius = 1.0; placement_finder->defaults.set_old_style_expression(parse_expression("[GEONAME]")); put(text_sym, keys::text_placements_, placement_finder); r.append(std::move(text_sym)); diff --git a/include/mapnik/text/formatting/format.hpp b/include/mapnik/text/formatting/format.hpp index f50f08e74..7210e7ff6 100644 --- a/include/mapnik/text/formatting/format.hpp +++ b/include/mapnik/text/formatting/format.hpp @@ -32,7 +32,8 @@ namespace mapnik { namespace formatting { -class MAPNIK_DECL format_node: public node { +class MAPNIK_DECL format_node: public node +{ public: void to_xml(boost::property_tree::ptree & xml) const; static node_ptr from_xml(xml_node const& xml); @@ -45,14 +46,14 @@ public: boost::optional face_name; boost::optional text_size; boost::optional character_spacing; - boost::optional line_spacing; - boost::optional text_opacity; + boost::optional line_spacing; + boost::optional text_opacity; boost::optional wrap_before; - boost::optional wrap_char; + boost::optional wrap_char; boost::optional text_transform; boost::optional fill; boost::optional halo_fill; - boost::optional halo_radius; + boost::optional halo_radius; private: node_ptr child_; diff --git a/include/mapnik/text/text_properties.hpp b/include/mapnik/text/text_properties.hpp index 8f7162d2d..7b8093875 100644 --- a/include/mapnik/text/text_properties.hpp +++ b/include/mapnik/text/text_properties.hpp @@ -68,18 +68,21 @@ struct MAPNIK_DECL format_properties void from_xml(xml_node const& sym, fontset_map const& fontsets); void to_xml(boost::property_tree::ptree & node, bool explicit_defaults, format_properties const& dfl = format_properties()) const; - std::string face_name; - boost::optional fontset; + // expressions symbolizer_base::value_type text_size; symbolizer_base::value_type character_spacing; - double line_spacing; //Largest total height (fontsize+line_spacing) per line is chosen - double text_opacity; - double halo_opacity; - unsigned wrap_char; + symbolizer_base::value_type line_spacing; //Largest total height (fontsize+line_spacing) per line is chosen + symbolizer_base::value_type text_opacity; + symbolizer_base::value_type halo_opacity; + symbolizer_base::value_type wrap_char; + symbolizer_base::value_type halo_radius; + // + std::string face_name; + boost::optional fontset; text_transform_e text_transform; //Per expression color fill; color halo_fill; - double halo_radius; + }; @@ -162,7 +165,7 @@ struct MAPNIK_DECL text_symbolizer_properties text_layout_properties layout_defaults; // Default values for format_properties. //char_properties_ptr format; - format_properties format_properties; + format_properties format_defaults; private: // A tree of formatting::nodes which contain text and formatting information. diff --git a/src/load_map.cpp b/src/load_map.cpp index 6ef7d5bc0..c54e67940 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -1227,9 +1227,9 @@ void map_parser::parse_text_symbolizer(rule & rule, xml_node const& node) } if (strict_ && - !placements->defaults.format_properties.fontset) + !placements->defaults.format_defaults.fontset) { - ensure_font_face(placements->defaults.format_properties.face_name); + ensure_font_face(placements->defaults.format_defaults.face_name); } text_symbolizer sym; parse_symbolizer_base(sym, node); @@ -1264,9 +1264,9 @@ void map_parser::parse_shield_symbolizer(rule & rule, xml_node const& node) } placements->defaults.from_xml(node, fontsets_); if (strict_ && - !placements->defaults.format_properties.fontset) + !placements->defaults.format_defaults.fontset) { - ensure_font_face(placements->defaults.format_properties.face_name); + ensure_font_face(placements->defaults.format_defaults.face_name); } shield_symbolizer sym; diff --git a/src/text/formatting/format.cpp b/src/text/formatting/format.cpp index 61eaab891..fdf95a28b 100644 --- a/src/text/formatting/format.cpp +++ b/src/text/formatting/format.cpp @@ -44,14 +44,19 @@ void format_node::to_xml(ptree & xml) const if (text_size) serialize_property("size", *text_size, xml); if (character_spacing) serialize_property("character-spacing", *character_spacing, xml); - if (line_spacing) set_attr(new_node, "line-spacing", *line_spacing); - if (text_opacity) set_attr(new_node, "opacity", *text_opacity); - if (wrap_before) set_attr(new_node, "wrap-before", *wrap_before); - if (wrap_char) set_attr(new_node, "wrap-character", *wrap_char); + if (line_spacing) serialize_property("line-spacing", *line_spacing, xml); + if (text_opacity) serialize_property("opacity", *text_opacity, xml); + + if (wrap_before) set_attr(new_node, "wrap-before", *wrap_before); // FIXME!!! + + if (wrap_char) serialize_property("wrap_char", *wrap_char, xml); + if (text_transform) set_attr(new_node, "text-transform", *text_transform); if (fill) set_attr(new_node, "fill", *fill); if (halo_fill) set_attr(new_node, "halo-fill", *halo_fill); - if (halo_radius) set_attr(new_node, "halo-radius", *halo_radius); + + if (halo_radius) serialize_property("halo-radius", *halo_radius, xml); + if (child_) child_->to_xml(new_node); } @@ -66,17 +71,19 @@ node_ptr format_node::from_xml(xml_node const& xml) n->face_name = xml.get_opt_attr("face-name"); //TODO: Fontset is problematic. We don't have the fontsets pointer here... - + // exprs set_property_from_xml(n->text_size, "size", xml); set_property_from_xml(n->character_spacing, "character-spacing", xml); - - n->line_spacing = xml.get_opt_attr("line-spacing"); - n->text_opacity = xml.get_opt_attr("opacity"); - n->wrap_char = xml.get_opt_attr("wrap-character"); + set_property_from_xml(n->line_spacing, "line-spacing", xml); + set_property_from_xml(n->text_opacity, "opacity", xml); + //set_property_from_xml(n->halo_opacity, "halo-opacity", xml); FIXME + set_property_from_xml(n->halo_radius, "halo-radius", xml); + set_property_from_xml(n->wrap_char, "wrap-character", xml); + // n->text_transform = xml.get_opt_attr("text-transform"); n->fill = xml.get_opt_attr("fill"); n->halo_fill = xml.get_opt_attr("halo-fill"); - n->halo_radius = xml.get_opt_attr("halo-radius"); + return np; } @@ -84,18 +91,26 @@ node_ptr format_node::from_xml(xml_node const& xml) void format_node::apply(char_properties_ptr p, feature_impl const& feature, attributes const& attrs, text_layout &output) const { char_properties_ptr new_properties = std::make_shared(*p); - if (face_name) new_properties->face_name = *face_name; if (text_size) new_properties->text_size = boost::apply_visitor(extract_value(feature,attrs), *text_size); - if (character_spacing) new_properties->character_spacing =boost::apply_visitor(extract_value(feature,attrs), *character_spacing); + if (character_spacing) new_properties->character_spacing = boost::apply_visitor(extract_value(feature,attrs), *character_spacing); + if (line_spacing) new_properties->line_spacing = boost::apply_visitor(extract_value(feature,attrs), *line_spacing); + if (text_opacity) new_properties->text_opacity = boost::apply_visitor(extract_value(feature,attrs), *text_opacity); - if (line_spacing) new_properties->line_spacing = *line_spacing; - if (text_opacity) new_properties->text_opacity = *text_opacity; - if (wrap_char) new_properties->wrap_char = *wrap_char; + if (wrap_char) + { + std::string str = boost::apply_visitor(extract_value(feature,attrs), *wrap_char); + if (!str.empty()) + { + new_properties->wrap_char = str[0]; + } + } + if (halo_radius) new_properties->halo_radius = boost::apply_visitor(extract_value(feature,attrs), *halo_radius); + + if (face_name) new_properties->face_name = *face_name; if (text_transform) new_properties->text_transform = *text_transform; if (fill) new_properties->fill = *fill; if (halo_fill) new_properties->halo_fill = *halo_fill; - if (halo_radius) new_properties->halo_radius = *halo_radius; if (child_) child_->apply(new_properties, feature, attrs, output); else MAPNIK_LOG_WARN(format) << "Useless format: No text to format"; diff --git a/src/text/placements/simple.cpp b/src/text/placements/simple.cpp index 95185b5ce..ba1660d6d 100644 --- a/src/text/placements/simple.cpp +++ b/src/text/placements/simple.cpp @@ -49,7 +49,7 @@ bool text_placement_info_simple::next() if (state > 0) { if (state > parent_->text_sizes_.size()) return false; - properties.format_properties.text_size = value_double(parent_->text_sizes_[state-1]); + properties.format_defaults.text_size = value_double(parent_->text_sizes_[state-1]); } if (!next_position_only()) { diff --git a/src/text/text_properties.cpp b/src/text/text_properties.cpp index 301102c1e..348d83496 100644 --- a/src/text/text_properties.cpp +++ b/src/text/text_properties.cpp @@ -53,7 +53,7 @@ text_symbolizer_properties::text_symbolizer_properties() largest_bbox_only(true), upright(UPRIGHT_AUTO), layout_defaults(), - format_properties(),//std::make_shared()), + format_defaults(),//std::make_shared()), tree_() {} void text_symbolizer_properties::process(text_layout & output, feature_impl const& feature, attributes const& attrs) const @@ -63,18 +63,25 @@ void text_symbolizer_properties::process(text_layout & output, feature_impl cons { //evaluate format properties char_properties_ptr format = std::make_shared(); - format->face_name = format_properties.face_name; - format->fontset = format_properties.fontset; - format->text_size = boost::apply_visitor(extract_value(feature,attrs), format_properties.text_size); - format->character_spacing = boost::apply_visitor(extract_value(feature,attrs), format_properties.character_spacing); - format->line_spacing = format_properties.line_spacing; - format->text_opacity = format_properties.text_opacity; - format->halo_opacity = format_properties.halo_opacity; - format->wrap_char = format_properties.wrap_char; - format->text_transform = format_properties.text_transform; - format->fill = format_properties.fill; - format->halo_fill = format_properties.halo_fill; - format->halo_radius = format_properties.halo_radius; + + format->text_size = boost::apply_visitor(extract_value(feature,attrs), format_defaults.text_size); + format->character_spacing = boost::apply_visitor(extract_value(feature,attrs), format_defaults.character_spacing); + format->line_spacing = boost::apply_visitor(extract_value(feature,attrs), format_defaults.line_spacing); + format->text_opacity = boost::apply_visitor(extract_value(feature,attrs), format_defaults.text_opacity); + format->halo_opacity = boost::apply_visitor(extract_value(feature,attrs), format_defaults.halo_opacity); + format->halo_radius = boost::apply_visitor(extract_value(feature,attrs), format_defaults.halo_radius); + + std::string const& wrap_char = boost::apply_visitor(extract_value(feature,attrs), format_defaults.wrap_char); + if (!wrap_char.empty()) + { + format->wrap_char = wrap_char[0]; + } + + format->face_name = format_defaults.face_name; + format->fontset = format_defaults.fontset; + format->text_transform = format_defaults.text_transform; + format->fill = format_defaults.fill; + format->halo_fill = format_defaults.halo_fill; tree_->apply(format, feature, attrs, output); } @@ -137,7 +144,7 @@ void text_symbolizer_properties::from_xml(xml_node const& node, fontset_map cons set_old_style_expression(*name_); } - format_properties.from_xml(node, fontsets); + format_defaults.from_xml(node, fontsets); formatting::node_ptr n(formatting::node::from_xml(node)); if (n) set_format_tree(n); } @@ -192,7 +199,7 @@ void text_symbolizer_properties::to_xml(boost::property_tree::ptree &node, } layout_defaults.to_xml(node, explicit_defaults, dfl.layout_defaults); - format_properties.to_xml(node, explicit_defaults, dfl.format_properties); + format_defaults.to_xml(node, explicit_defaults, dfl.format_defaults); if (tree_) tree_->to_xml(node); } @@ -200,6 +207,7 @@ void text_symbolizer_properties::to_xml(boost::property_tree::ptree &node, void text_symbolizer_properties::add_expressions(expression_set & output) const { layout_defaults.add_expressions(output); + //format_defaults.add_expressions(output); FIXME if (tree_) tree_->add_expressions(output); } @@ -267,45 +275,41 @@ format_properties::format_properties() fontset(), text_size(10.0), character_spacing(0.0), - line_spacing(0), + line_spacing(0.0), text_opacity(1.0), halo_opacity(1.0), - wrap_char(' '), + wrap_char(" "), text_transform(NONE), fill(color(0,0,0)), halo_fill(color(255,255,255)), - halo_radius(0) {} + halo_radius(0.0) {} void format_properties::from_xml(xml_node const& node, fontset_map const& fontsets) { set_property_from_xml(text_size, "size", node); set_property_from_xml(character_spacing, "character-spacing", node); + set_property_from_xml(line_spacing, "line-spacing", node); + set_property_from_xml(halo_radius, "halo-radius", node); + set_property_from_xml(text_opacity, "opacity", node); + set_property_from_xml(halo_opacity, "halo-opacity", node); + set_property_from_xml(wrap_char, "wrap-character", node); - //optional character_spacing_ = node.get_opt_attr("character-spacing"); - //if (character_spacing_) character_spacing = *character_spacing_; optional fill_ = node.get_opt_attr("fill"); if (fill_) fill = *fill_; optional halo_fill_ = node.get_opt_attr("halo-fill"); if (halo_fill_) halo_fill = *halo_fill_; - optional halo_radius_ = node.get_opt_attr("halo-radius"); - if (halo_radius_) halo_radius = *halo_radius_; + optional tconvert_ = node.get_opt_attr("text-transform"); if (tconvert_) text_transform = *tconvert_; - optional line_spacing_ = node.get_opt_attr("line-spacing"); - if (line_spacing_) line_spacing = *line_spacing_; - optional opacity_ = node.get_opt_attr("opacity"); - if (opacity_) text_opacity = *opacity_; - optional halo_opacity_ = node.get_opt_attr("halo-opacity"); - if (halo_opacity_) halo_opacity = *halo_opacity_; - optional wrap_char_ = node.get_opt_attr("wrap-character"); - if (wrap_char_ && (*wrap_char_).size() > 0) wrap_char = ((*wrap_char_)[0]); + optional face_name_ = node.get_opt_attr("face-name"); if (face_name_) { face_name = *face_name_; } optional fontset_name_ = node.get_opt_attr("fontset-name"); - if (fontset_name_) { + if (fontset_name_) + { std::map::const_iterator itr = fontsets.find(*fontset_name_); if (itr != fontsets.end()) { @@ -339,44 +343,30 @@ void format_properties::to_xml(boost::property_tree::ptree & node, bool explicit } if (!(text_size == dfl.text_size) || explicit_defaults) serialize_property("size", text_size, node); + if (!(character_spacing == dfl.character_spacing) || explicit_defaults) serialize_property("character-spacing", character_spacing, node); + if (!(line_spacing == dfl.line_spacing) || explicit_defaults) serialize_property("line-spacing", line_spacing, node); + if (!(halo_radius == dfl.halo_radius) || explicit_defaults) serialize_property("halo-radius", halo_radius, node); + if (!(wrap_char == dfl.wrap_char) || explicit_defaults) serialize_property("wrap-character", wrap_char, node); + + // for shield_symbolizer this is later overridden -- FIXME + if (!(text_opacity == dfl.text_opacity) || explicit_defaults) serialize_property("opacity", text_opacity, node); + if (!(halo_opacity == dfl.halo_opacity) || explicit_defaults) serialize_property("halo-opacity", halo_opacity, node); + // if (fill != dfl.fill || explicit_defaults) { set_attr(node, "fill", fill); } - if (halo_radius != dfl.halo_radius || explicit_defaults) - { - set_attr(node, "halo-radius", halo_radius); - } + if (halo_fill != dfl.halo_fill || explicit_defaults) { set_attr(node, "halo-fill", halo_fill); } - if (wrap_char != dfl.wrap_char || explicit_defaults) - { - set_attr(node, "wrap-character", std::string(1, wrap_char)); - } + if (text_transform != dfl.text_transform || explicit_defaults) { set_attr(node, "text-transform", text_transform); } - if (line_spacing != dfl.line_spacing || explicit_defaults) - { - set_attr(node, "line-spacing", line_spacing); - } - if (!(character_spacing == dfl.character_spacing) || explicit_defaults) - serialize_property("character-spacing", character_spacing, node); - - // for shield_symbolizer this is later overridden - if (text_opacity != dfl.text_opacity || explicit_defaults) - { - set_attr(node, "opacity", text_opacity); - } - // for shield_symbolizer this is later overridden - if (halo_opacity != dfl.halo_opacity || explicit_defaults) - { - set_attr(node, "halo-opacity", halo_opacity); - } } } //ns mapnik diff --git a/tests/cpp_tests/fontset_runtime_test.cpp b/tests/cpp_tests/fontset_runtime_test.cpp index dcc945e60..9e2ef1fe7 100644 --- a/tests/cpp_tests/fontset_runtime_test.cpp +++ b/tests/cpp_tests/fontset_runtime_test.cpp @@ -67,10 +67,10 @@ int main(int argc, char** argv) mapnik::rule r; mapnik::text_symbolizer text_sym; mapnik::text_placements_ptr placement_finder = std::make_shared(); - placement_finder->defaults.format_properties.face_name = "DejaVu Sans Book"; - placement_finder->defaults.format_properties.text_size = 10.0; - placement_finder->defaults.format_properties.fill = mapnik::color(0,0,0); - placement_finder->defaults.format_properties.fontset = fontset; + placement_finder->defaults.format_defaults.face_name = "DejaVu Sans Book"; + placement_finder->defaults.format_defaults.text_size = 10.0; + placement_finder->defaults.format_defaults.fill = mapnik::color(0,0,0); + placement_finder->defaults.format_defaults.fontset = fontset; placement_finder->defaults.set_old_style_expression(mapnik::parse_expression("[name]")); mapnik::put(text_sym, mapnik::keys::text_placements_, placement_finder); r.append(std::move(text_sym));