Use text_placements registry.
This commit is contained in:
parent
6db956ca83
commit
980cdd9e92
9 changed files with 54 additions and 76 deletions
|
@ -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<text_symbolizer_properties> list_;
|
||||
friend class text_placement_info_list;
|
||||
|
|
|
@ -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<registry, CreateStatic>,
|
||||
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<std::string, from_xml_function_ptr> map_;
|
||||
};
|
||||
|
|
|
@ -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<directions_t> direction_;
|
||||
|
|
|
@ -48,11 +48,13 @@ enum text_transform
|
|||
};
|
||||
DEFINE_ENUM(text_transform_e, text_transform);
|
||||
|
||||
typedef std::map<std::string, font_set> fontset_map;
|
||||
|
||||
struct char_properties
|
||||
{
|
||||
char_properties();
|
||||
/** Construct object from XML. */
|
||||
void from_xml(boost::property_tree::ptree const &sym, std::map<std::string,font_set> 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<std::string,font_set> 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;
|
||||
|
||||
|
|
|
@ -47,8 +47,7 @@
|
|||
|
||||
#include <mapnik/metawriter_factory.hpp>
|
||||
|
||||
#include <mapnik/text_placements/simple.hpp>
|
||||
#include <mapnik/text_placements/list.hpp>
|
||||
#include <mapnik/text_placements/registry.hpp>
|
||||
#include <mapnik/text_placements/dummy.hpp>
|
||||
#include <mapnik/symbolizer.hpp>
|
||||
#include <mapnik/rule.hpp>
|
||||
|
@ -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<std::string> placement_type = get_opt_attr<std::string>(sym, "placement-type");
|
||||
if (placement_type) {
|
||||
if (*placement_type == "simple") {
|
||||
placement_finder = text_placements_ptr(
|
||||
new text_placements_simple(
|
||||
get_attr<std::string>(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<std::string> placement_type = get_opt_attr<std::string>(sym, "placement-type");
|
||||
if (placement_type) {
|
||||
if (*placement_type == "simple") {
|
||||
placement_finder = text_placements_ptr(
|
||||
new text_placements_simple(
|
||||
get_attr<std::string>(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. */
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
*****************************************************************************/
|
||||
// mapnik
|
||||
#include <mapnik/text_placements/registry.hpp>
|
||||
#include <mapnik/text_placements/simple.hpp>
|
||||
#include <mapnik/text_placements/list.hpp>
|
||||
|
||||
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<std::string, from_xml_function_ptr>::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
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
// mapnik
|
||||
#include <mapnik/text_placements/simple.hpp>
|
||||
#include <mapnik/ptree_helpers.hpp>
|
||||
|
||||
// boost
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
|
@ -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<std::string>(xml, "placements", "X")));
|
||||
ptr->defaults.from_xml(xml, fontsets);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
} //ns mapnik
|
||||
|
|
|
@ -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<std::string,font_set> const & fontsets)
|
||||
void text_symbolizer_properties::from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets)
|
||||
{
|
||||
optional<label_placement_e> placement_ = get_opt_attr<label_placement_e>(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<std::string,font_set> const & fontsets)
|
||||
void char_properties::from_xml(boost::property_tree::ptree const &sym, fontset_map const & fontsets)
|
||||
{
|
||||
optional<double> text_size_ = get_opt_attr<double>(sym, "size");
|
||||
if (text_size_) text_size = *text_size_;
|
||||
|
|
Loading…
Reference in a new issue