diff --git a/include/mapnik/text_placements/list.hpp b/include/mapnik/text_placements/list.hpp index 7ccea3c9d..951f00b79 100644 --- a/include/mapnik/text_placements/list.hpp +++ b/include/mapnik/text_placements/list.hpp @@ -39,6 +39,7 @@ public: text_symbolizer_properties & add(); text_symbolizer_properties & get(unsigned i); unsigned size() const; + static text_placements_ptr from_xml(boost::property_tree::ptree const &xml, fontset_map const & fontsets); private: std::vector list_; friend class text_placement_info_list; diff --git a/include/mapnik/text_placements/registry.hpp b/include/mapnik/text_placements/registry.hpp index aaea3078e..c171a5089 100644 --- a/include/mapnik/text_placements/registry.hpp +++ b/include/mapnik/text_placements/registry.hpp @@ -38,7 +38,8 @@ namespace mapnik namespace placements { -typedef text_placements_ptr (*from_xml_function_ptr)(boost::property_tree::ptree const& xml); +typedef text_placements_ptr (*from_xml_function_ptr)( + boost::property_tree::ptree const& xml, fontset_map const & fontsets); class registry : public singleton, private boost::noncopyable @@ -47,7 +48,9 @@ public: registry(); ~registry() {} void register_name(std::string name, from_xml_function_ptr ptr, bool overwrite=false); - text_placements_ptr from_xml(std::string name, boost::property_tree::ptree const& xml); + text_placements_ptr from_xml(std::string name, + boost::property_tree::ptree const& xml, + fontset_map const & fontsets); private: std::map map_; }; diff --git a/include/mapnik/text_placements/simple.hpp b/include/mapnik/text_placements/simple.hpp index 25fd145f7..a7461ffad 100644 --- a/include/mapnik/text_placements/simple.hpp +++ b/include/mapnik/text_placements/simple.hpp @@ -53,6 +53,7 @@ public: double scale_factor, dimension_type dim, bool has_dimensions) const; void set_positions(std::string positions); std::string get_positions(); + static text_placements_ptr from_xml(boost::property_tree::ptree const &xml, fontset_map const & fontsets); private: std::string positions_; std::vector direction_; diff --git a/include/mapnik/text_properties.hpp b/include/mapnik/text_properties.hpp index 2cc0c12ac..4362a632f 100644 --- a/include/mapnik/text_properties.hpp +++ b/include/mapnik/text_properties.hpp @@ -48,11 +48,13 @@ enum text_transform }; DEFINE_ENUM(text_transform_e, text_transform); +typedef std::map fontset_map; + struct char_properties { char_properties(); /** Construct object from XML. */ - void from_xml(boost::property_tree::ptree const &sym, std::map const & fontsets); + void from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets); /** Write object to XML ptree. */ void to_xml(boost::property_tree::ptree &node, bool explicit_defaults, char_properties const &dfl=char_properties()) const; std::string face_name; @@ -122,7 +124,7 @@ struct text_symbolizer_properties { text_symbolizer_properties(); /** Load all values from XML ptree. */ - void from_xml(boost::property_tree::ptree const &sym, std::map const & fontsets); + void from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets); /** Save all values to XML ptree (but does not create a new parent node!). */ void to_xml(boost::property_tree::ptree &node, bool explicit_defaults, text_symbolizer_properties const &dfl=text_symbolizer_properties()) const; diff --git a/src/load_map.cpp b/src/load_map.cpp index c56071cb0..3bf00e0d3 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -47,8 +47,7 @@ #include -#include -#include +#include #include #include #include @@ -1263,46 +1262,16 @@ void map_parser::parse_text_symbolizer( rule & rule, ptree const & sym ) try { text_placements_ptr placement_finder; - text_placements_list *list = 0; optional placement_type = get_opt_attr(sym, "placement-type"); if (placement_type) { - if (*placement_type == "simple") { - placement_finder = text_placements_ptr( - new text_placements_simple( - get_attr(sym, "placements", "X"))); - } else if (*placement_type == "list") { - list = new text_placements_list(); - placement_finder = text_placements_ptr(list); - } else if (*placement_type != "dummy" && *placement_type != "") { - throw config_error(std::string("Unknown placement type '"+*placement_type+"'")); - } - } - if (!placement_finder) { + placement_finder = placements::registry::instance()->from_xml(*placement_type, sym, fontsets_); + } else { placement_finder = text_placements_ptr(new text_placements_dummy()); + placement_finder->defaults.from_xml(sym, fontsets_); } - - placement_finder->defaults.from_xml(sym, fontsets_); if (strict_ && !placement_finder->defaults.format.fontset.size()) ensure_font_face(placement_finder->defaults.format.face_name); - if (list) { - ptree::const_iterator symIter = sym.begin(); - ptree::const_iterator endSym = sym.end(); - for( ;symIter != endSym; ++symIter) { - if (symIter->first.find('<') != std::string::npos) continue; - if (symIter->first != "Placement") - { -// throw config_error("Unknown element '" + symIter->first + "'"); TODO - continue; - } - ensure_attrs(symIter->second, "TextSymbolizer/Placement", s_common.str()); - text_symbolizer_properties & p = list->add(); - p.from_xml(symIter->second, fontsets_); - if (strict_ && - !p.format.fontset.size()) - ensure_font_face(p.format.face_name); - } - } text_symbolizer text_symbol = text_symbolizer(placement_finder); parse_metawriter_in_symbolizer(text_symbol, sym); @@ -1336,46 +1305,16 @@ void map_parser::parse_shield_symbolizer( rule & rule, ptree const & sym ) try { text_placements_ptr placement_finder; - text_placements_list *list = 0; optional placement_type = get_opt_attr(sym, "placement-type"); if (placement_type) { - if (*placement_type == "simple") { - placement_finder = text_placements_ptr( - new text_placements_simple( - get_attr(sym, "placements", "X"))); - } else if (*placement_type == "list") { - list = new text_placements_list(); - placement_finder = text_placements_ptr(list); - } else if (*placement_type != "dummy" && *placement_type != "") { - throw config_error(std::string("Unknown placement type '"+*placement_type+"'")); - } - } - if (!placement_finder) { + placement_finder = placements::registry::instance()->from_xml(*placement_type, sym, fontsets_); + } else { placement_finder = text_placements_ptr(new text_placements_dummy()); } - placement_finder->defaults.from_xml(sym, fontsets_); if (strict_ && !placement_finder->defaults.format.fontset.size()) ensure_font_face(placement_finder->defaults.format.face_name); - if (list) { - ptree::const_iterator symIter = sym.begin(); - ptree::const_iterator endSym = sym.end(); - for( ;symIter != endSym; ++symIter) { - if (symIter->first.find('<') != std::string::npos) continue; - if (symIter->first != "Placement") - { -// throw config_error("Unknown element '" + symIter->first + "'"); TODO - continue; - } - ensure_attrs(symIter->second, "TextSymbolizer/Placement", s_common); - text_symbolizer_properties & p = list->add(); - p.from_xml(symIter->second, fontsets_); - if (strict_&& - !p.format.fontset.size()) - ensure_font_face(p.format.face_name); - } - } shield_symbolizer shield_symbol = shield_symbolizer(placement_finder); /* Symbolizer specific attributes. */ diff --git a/src/text_placements/list.cpp b/src/text_placements/list.cpp index 47688ac90..9d93fda94 100644 --- a/src/text_placements/list.cpp +++ b/src/text_placements/list.cpp @@ -81,5 +81,25 @@ unsigned text_placements_list::size() const return list_.size(); } +text_placements_ptr text_placements_list::from_xml(boost::property_tree::ptree const &xml, fontset_map const & fontsets) +{ + using boost::property_tree::ptree; + text_placements_list *list = new text_placements_list; + text_placements_ptr ptr = text_placements_ptr(list); + list->defaults.from_xml(xml, fontsets); + ptree::const_iterator itr = xml.begin(); + ptree::const_iterator end = xml.end(); + for( ;itr != end; ++itr) { + if ((itr->first.find('<') != std::string::npos) || (itr->first != "Placement")) continue; +//TODO: ensure_attrs(symIter->second, "TextSymbolizer/Placement", s_common.str()); + text_symbolizer_properties &p = list->add(); + p.from_xml(itr->second, fontsets); +//TODO: if (strict_ && +// !p.format.fontset.size()) +// ensure_font_face(p.format.face_name); + } + return ptr; +} + } //ns mapnik diff --git a/src/text_placements/registry.cpp b/src/text_placements/registry.cpp index bc4fe2626..30668b78f 100644 --- a/src/text_placements/registry.cpp +++ b/src/text_placements/registry.cpp @@ -21,6 +21,8 @@ *****************************************************************************/ // mapnik #include +#include +#include namespace mapnik { @@ -29,7 +31,8 @@ namespace placements registry::registry() { - + register_name("simple", &text_placements_simple::from_xml); + register_name("list", &text_placements_list::from_xml); } void registry::register_name(std::string name, from_xml_function_ptr ptr, bool overwrite) @@ -41,11 +44,11 @@ void registry::register_name(std::string name, from_xml_function_ptr ptr, bool o } } -text_placements_ptr registry::from_xml(std::string name, const boost::property_tree::ptree &xml) +text_placements_ptr registry::from_xml(std::string name, const boost::property_tree::ptree &xml, fontset_map const& fontsets) { std::map::const_iterator itr = map_.find(name); if (itr == map_.end()) throw config_error("Unknown placement-type '" + name + "'"); - return itr->second(xml); + return itr->second(xml, fontsets); } } //ns formatting } //ns mapnik diff --git a/src/text_placements/simple.cpp b/src/text_placements/simple.cpp index 9037389ae..81af87195 100644 --- a/src/text_placements/simple.cpp +++ b/src/text_placements/simple.cpp @@ -22,6 +22,7 @@ // mapnik #include +#include // boost #include @@ -165,4 +166,12 @@ std::string text_placements_simple::get_positions() return positions_; //TODO: Build string from data in direction_ and text_sizes_ } +text_placements_ptr text_placements_simple::from_xml(boost::property_tree::ptree const &xml, fontset_map const & fontsets) +{ + text_placements_ptr ptr = text_placements_ptr(new text_placements_simple( + get_attr(xml, "placements", "X"))); + ptr->defaults.from_xml(xml, fontsets); + return ptr; +} + } //ns mapnik diff --git a/src/text_properties.cpp b/src/text_properties.cpp index d54ec67d0..864cdd4ec 100644 --- a/src/text_properties.cpp +++ b/src/text_properties.cpp @@ -72,7 +72,7 @@ formatting::node_ptr text_symbolizer_properties::format_tree() const return tree_; } -void text_symbolizer_properties::from_xml(boost::property_tree::ptree const &sym, std::map const & fontsets) +void text_symbolizer_properties::from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets) { optional placement_ = get_opt_attr(sym, "placement"); if (placement_) label_placement = *placement_; @@ -230,7 +230,7 @@ char_properties::char_properties() : } -void char_properties::from_xml(boost::property_tree::ptree const &sym, std::map const & fontsets) +void char_properties::from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets) { optional text_size_ = get_opt_attr(sym, "size"); if (text_size_) text_size = *text_size_;