From 1a16e9c5ab70032f50f017ef2134bc57dae0b656 Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Tue, 31 Jan 2012 16:24:58 +0100 Subject: [PATCH] Remove class text_processor. --- include/mapnik/text_placements.hpp | 106 ++++++++++++++---------- include/mapnik/text_processing.hpp | 126 +++++++++++------------------ src/load_map.cpp | 8 +- src/symbolizer_helpers.cpp | 2 +- src/text_placements.cpp | 61 +++++++++++--- src/text_processing.cpp | 57 +------------ src/text_symbolizer.cpp | 54 ++++++------- 7 files changed, 193 insertions(+), 221 deletions(-) diff --git a/include/mapnik/text_placements.hpp b/include/mapnik/text_placements.hpp index 80bb5f5f7..195b2ac8d 100644 --- a/include/mapnik/text_placements.hpp +++ b/include/mapnik/text_placements.hpp @@ -49,57 +49,50 @@ class text_placements; typedef std::pair position; typedef std::pair dimension_type; -enum label_placement_enum { - POINT_PLACEMENT, - LINE_PLACEMENT, - VERTEX_PLACEMENT, - INTERIOR_PLACEMENT, - label_placement_enum_MAX -}; - -DEFINE_ENUM( label_placement_e, label_placement_enum ); - -enum vertical_alignment +struct char_properties { - V_TOP = 0, - V_MIDDLE, - V_BOTTOM, - V_AUTO, - vertical_alignment_MAX + char_properties(); + /** Construct object from XML. */ + void from_xml(boost::property_tree::ptree const &sym, std::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; + font_set fontset; + float text_size; + double character_spacing; + double line_spacing; //Largest total height (fontsize+line_spacing) per line is chosen + double text_opacity; + bool wrap_before; + unsigned wrap_char; + text_transform_e text_transform; //Per expression + color fill; + color halo_fill; + double halo_radius; }; -DEFINE_ENUM( vertical_alignment_e, vertical_alignment ); - -enum horizontal_alignment -{ - H_LEFT = 0, - H_MIDDLE, - H_RIGHT, - H_AUTO, - horizontal_alignment_MAX -}; - -DEFINE_ENUM( horizontal_alignment_e, horizontal_alignment ); - -enum justify_alignment -{ - J_LEFT = 0, - J_MIDDLE, - J_RIGHT, - justify_alignment_MAX -}; - -DEFINE_ENUM( justify_alignment_e, justify_alignment ); - /** Contains all text symbolizer properties which are not directly related to text formating. */ struct text_symbolizer_properties { text_symbolizer_properties(); - /** Load all values and also the ```processor``` object from XML ptree. */ + /** Load all values from XML ptree. */ void from_xml(boost::property_tree::ptree const &sym, std::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; + /** Takes a feature and produces formated text as output. + * The output object has to be created by the caller and passed in for thread safety. + */ + void process(processed_text &output, Feature const& feature) const; + /** Automatically create processing instructions for a single expression. */ + void set_old_style_expression(expression_ptr expr); + /** Sets new format tree. */ + void set_format_tree(formating::node_ptr tree); + /** Get format tree. */ + formating::node_ptr format_tree() const; + /** Get a list of all expressions used in any placement. + * This function is used to collect attributes. */ + std::set get_all_expressions() const; + //Per symbolizer options expression_ptr orientation; position displacement; @@ -121,10 +114,39 @@ struct text_symbolizer_properties bool allow_overlap; unsigned text_ratio; unsigned wrap_width; - /** Contains everything related to text formating */ - text_processor processor; + /** Default values for char_properties. */ + char_properties default_format; +private: + formating::node_ptr tree_; }; +class processed_text : boost::noncopyable +{ +public: + class processed_expression + { + public: + processed_expression(char_properties const& properties, UnicodeString const& text) : + p(properties), str(text) {} + char_properties p; + UnicodeString str; + }; +public: + processed_text(face_manager & font_manager, double scale_factor); + void push_back(processed_expression const& exp); + unsigned size() const { return expr_list_.size(); } + unsigned empty() const { return expr_list_.empty(); } + void clear(); + typedef std::list expression_list; + expression_list::const_iterator begin() const; + expression_list::const_iterator end() const; + string_info &get_string_info(); +private: + expression_list expr_list_; + face_manager & font_manager_; + double scale_factor_; + string_info info_; +}; /** Generate a possible placement and store results of placement_finder. * This placement has first to be tested by placement_finder to verify it diff --git a/include/mapnik/text_processing.hpp b/include/mapnik/text_processing.hpp index 8bc4dcc1d..bd5698fc7 100644 --- a/include/mapnik/text_processing.hpp +++ b/include/mapnik/text_processing.hpp @@ -23,6 +23,8 @@ #define MAPNIK_TEXT_PROCESSING_HPP #include +#include + #include #include #include @@ -37,6 +39,50 @@ #include namespace mapnik { +class processed_text; +struct char_properties; + +enum label_placement_enum { + POINT_PLACEMENT, + LINE_PLACEMENT, + VERTEX_PLACEMENT, + INTERIOR_PLACEMENT, + label_placement_enum_MAX +}; + +DEFINE_ENUM( label_placement_e, label_placement_enum ); + +enum vertical_alignment +{ + V_TOP = 0, + V_MIDDLE, + V_BOTTOM, + V_AUTO, + vertical_alignment_MAX +}; + +DEFINE_ENUM( vertical_alignment_e, vertical_alignment ); + +enum horizontal_alignment +{ + H_LEFT = 0, + H_MIDDLE, + H_RIGHT, + H_AUTO, + horizontal_alignment_MAX +}; + +DEFINE_ENUM( horizontal_alignment_e, horizontal_alignment ); + +enum justify_alignment +{ + J_LEFT = 0, + J_MIDDLE, + J_RIGHT, + justify_alignment_MAX +}; + +DEFINE_ENUM( justify_alignment_e, justify_alignment ); enum text_transform { @@ -48,58 +94,6 @@ enum text_transform }; DEFINE_ENUM( text_transform_e, text_transform ); - -struct char_properties -{ - char_properties(); - /** Construct object from XML. */ - void from_xml(boost::property_tree::ptree const &sym, std::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; - font_set fontset; - float text_size; - double character_spacing; - double line_spacing; //Largest total height (fontsize+line_spacing) per line is chosen - double text_opacity; - bool wrap_before; - unsigned wrap_char; - text_transform_e text_transform; //Per expression - color fill; - color halo_fill; - double halo_radius; -}; - -class processed_expression -{ -public: - processed_expression(char_properties const& properties, UnicodeString const& text) : - p(properties), str(text) {} - char_properties p; - UnicodeString str; -}; - - -class processed_text : boost::noncopyable -{ -public: - processed_text(face_manager & font_manager, double scale_factor); - void push_back(processed_expression const& exp); - unsigned size() const { return expr_list_.size(); } - unsigned empty() const { return expr_list_.empty(); } - void clear(); - typedef std::list expression_list; - expression_list::const_iterator begin() const; - expression_list::const_iterator end() const; - string_info &get_string_info(); -private: - expression_list expr_list_; - face_manager & font_manager_; - double scale_factor_; - string_info info_; -}; - - namespace formating { class node; typedef boost::shared_ptr node_ptr; @@ -180,34 +174,6 @@ private: } //namespace formating -/** Stores formating information and uses this to produce formated text for a given feature. */ -class text_processor -{ -public: - text_processor(); - /** Construct object from XML. */ - void from_xml(boost::property_tree::ptree const& pt, std::map const &fontsets); - /** Write object to XML ptree. */ - void to_xml(boost::property_tree::ptree &node, bool explicit_defaults, text_processor const& dfl) const; - - /** Takes a feature and produces formated text as output. - * The output object has to be created by the caller and passed in for thread safety. - */ - void process(processed_text &output, Feature const& feature) const; - /** Automatically create processing instructions for a single expression. */ - void set_old_style_expression(expression_ptr expr); - /** Sets new format tree. */ - void set_format_tree(formating::node_ptr tree); - /** Get format tree. */ - formating::node_ptr get_format_tree() const; - /** Get a list of all expressions used in any placement. This function is used to collect attributes. */ - std::set get_all_expressions() const; - /** Default values for char_properties. */ - char_properties defaults; -private: - formating::node_ptr tree_; -}; - } /* namespace mapnik*/ #endif diff --git a/src/load_map.cpp b/src/load_map.cpp index 79f3d5c2e..b6ddae541 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -1282,7 +1282,7 @@ void map_parser::parse_text_symbolizer( rule & rule, ptree const & sym ) } placement_finder->properties.from_xml(sym, fontsets_); - if (strict_) ensure_font_face(placement_finder->properties.processor.defaults.face_name); + if (strict_) ensure_font_face(placement_finder->properties.default_format.face_name); if (list) { ptree::const_iterator symIter = sym.begin(); ptree::const_iterator endSym = sym.end(); @@ -1296,7 +1296,7 @@ void map_parser::parse_text_symbolizer( rule & rule, ptree const & sym ) ensure_attrs(symIter->second, "TextSymbolizer/Placement", s_common.str()); text_symbolizer_properties & p = list->add(); p.from_xml(symIter->second, fontsets_); - if (strict_) ensure_font_face(p.processor.defaults.face_name); + if (strict_) ensure_font_face(p.default_format.face_name); } } @@ -1351,7 +1351,7 @@ void map_parser::parse_shield_symbolizer( rule & rule, ptree const & sym ) } placement_finder->properties.from_xml(sym, fontsets_); - if (strict_) ensure_font_face(placement_finder->properties.processor.defaults.face_name); + if (strict_) ensure_font_face(placement_finder->properties.default_format.face_name); if (list) { ptree::const_iterator symIter = sym.begin(); ptree::const_iterator endSym = sym.end(); @@ -1365,7 +1365,7 @@ void map_parser::parse_shield_symbolizer( rule & rule, ptree const & sym ) ensure_attrs(symIter->second, "TextSymbolizer/Placement", s_common); text_symbolizer_properties & p = list->add(); p.from_xml(symIter->second, fontsets_); - if (strict_) ensure_font_face(p.processor.defaults.face_name); + if (strict_) ensure_font_face(p.default_format.face_name); } } diff --git a/src/symbolizer_helpers.cpp b/src/symbolizer_helpers.cpp index 72b9cc8f3..a71811d25 100644 --- a/src/symbolizer_helpers.cpp +++ b/src/symbolizer_helpers.cpp @@ -166,7 +166,7 @@ bool text_symbolizer_helper::next_placement() placement_valid_ = false; return false; } - placement_->properties.processor.process(text_, feature_); + placement_->properties.process(text_, feature_); info_ = &(text_.get_string_info()); if (placement_->properties.orientation) { diff --git a/src/text_placements.cpp b/src/text_placements.cpp index 457e24d78..d3703ce02 100644 --- a/src/text_placements.cpp +++ b/src/text_placements.cpp @@ -58,11 +58,33 @@ text_symbolizer_properties::text_symbolizer_properties() : allow_overlap(false), text_ratio(0), wrap_width(0), - processor() + tree_() { } +void text_symbolizer_properties::process(processed_text &output, Feature const& feature) const +{ + output.clear(); + if (tree_) { + tree_->apply(default_format, feature, output); + } else { +#ifdef MAPNIK_DEBUG + std::cerr << "Warning: text_symbolizer_properties can't produce text: No formating tree!\n"; +#endif + } +} + +void text_symbolizer_properties::set_format_tree(formating::node_ptr tree) +{ + tree_ = tree; +} + +formating::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) { optional placement_ = get_opt_attr(sym, "placement"); @@ -100,12 +122,16 @@ void text_symbolizer_properties::from_xml(boost::property_tree::ptree const &sym if (dy) displacement.second = *dy; optional max_char_angle_delta_ = get_opt_attr(sym, "max-char-angle-delta"); if (max_char_angle_delta_) max_char_angle_delta=(*max_char_angle_delta_)*(M_PI/180); - processor.from_xml(sym, fontsets); + optional name_ = get_opt_attr(sym, "name"); if (name_) { std::clog << "### WARNING: Using 'name' in TextSymbolizer/ShieldSymbolizer is deprecated!\n"; - processor.set_old_style_expression(parse_expression(*name_, "utf8")); + set_old_style_expression(parse_expression(*name_, "utf8")); } + + default_format.from_xml(sym, fontsets); + formating::node_ptr n(formating::node::from_xml(sym)); + if (n) set_format_tree(n); } void text_symbolizer_properties::to_xml(boost::property_tree::ptree &node, bool explicit_defaults, text_symbolizer_properties const &dfl) const @@ -186,7 +212,21 @@ void text_symbolizer_properties::to_xml(boost::property_tree::ptree &node, bool { set_attr(node, "vertical-alignment", valign); } - processor.to_xml(node, explicit_defaults, dfl.processor); + default_format.to_xml(node, explicit_defaults, dfl.default_format); + if (tree_) tree_->to_xml(node); +} + + +std::set text_symbolizer_properties::get_all_expressions() const +{ + std::set result; + if (tree_) tree_->add_expressions(result); + return result; +} + +void text_symbolizer_properties::set_old_style_expression(expression_ptr expr) +{ + tree_ = formating::node_ptr(new formating::text_node(expr)); } char_properties::char_properties() : @@ -319,7 +359,7 @@ text_placements::text_placements() : properties() std::set text_placements::get_all_expressions() { std::set result, tmp; - tmp = properties.processor.get_all_expressions(); + tmp = properties.get_all_expressions(); result.insert(tmp.begin(), tmp.end()); result.insert(properties.orientation); return result; @@ -358,11 +398,10 @@ text_placement_info_ptr text_placements_dummy::get_placement_info( bool text_placement_info_simple::next() { while (1) { - if (state == 0) { - properties.processor.defaults.text_size = parent_->properties.processor.defaults.text_size; - } else { + if (state > 0) + { if (state > parent_->text_sizes_.size()) return false; - properties.processor.defaults.text_size = parent_->text_sizes_[state-1]; + properties.default_format.text_size = parent_->text_sizes_[state-1]; } if (!next_position_only()) { state++; @@ -530,14 +569,14 @@ text_placements_list::text_placements_list() : text_placements(), list_(0) std::set text_placements_list::get_all_expressions() { std::set result, tmp; - tmp = properties.processor.get_all_expressions(); + tmp = properties.get_all_expressions(); result.insert(tmp.begin(), tmp.end()); result.insert(properties.orientation); std::vector::const_iterator it; for (it=list_.begin(); it != list_.end(); it++) { - tmp = it->processor.get_all_expressions(); + tmp = it->get_all_expressions(); result.insert(tmp.begin(), tmp.end()); result.insert(it->orientation); } diff --git a/src/text_processing.cpp b/src/text_processing.cpp index 8f65cbce9..b571e34d2 100644 --- a/src/text_processing.cpp +++ b/src/text_processing.cpp @@ -170,7 +170,7 @@ void text_node::apply(char_properties const& p, Feature const& feature, processe text_str = text_str.toTitle(NULL); } if (text_str.length() > 0) { - output.push_back(processed_expression(p, text_str)); + output.push_back(processed_text::processed_expression(p, text_str)); } else { #ifdef MAPNIK_DEBUG std::cerr << "Warning: Empty expression.\n"; @@ -346,61 +346,6 @@ void format_node::set_halo_radius(optional radius) /************************************************************/ -text_processor::text_processor(): - tree_() -{ -} - -void text_processor::set_format_tree(formating::node_ptr tree) -{ - tree_ = tree; -} - -formating::node_ptr text_processor::get_format_tree() const -{ - return tree_; -} - -void text_processor::from_xml(const boost::property_tree::ptree &pt, std::map const &fontsets) -{ - defaults.from_xml(pt, fontsets); - formating::node_ptr n = formating::node::from_xml(pt); - if (n) set_format_tree(n); -} - - -void text_processor::to_xml(boost::property_tree::ptree &node, bool explicit_defaults, text_processor const& dfl) const -{ - defaults.to_xml(node, explicit_defaults, dfl.defaults); - if (tree_) tree_->to_xml(node); -} - -void text_processor::process(processed_text &output, Feature const& feature) const -{ - output.clear(); - if (tree_) { - tree_->apply(defaults, feature, output); - } else { -#ifdef MAPNIK_DEBUG - std::cerr << "Warning: text_processor can't produce text: No formating tree!\n"; -#endif - } -} - -std::set text_processor::get_all_expressions() const -{ - std::set result; - if (tree_) tree_->add_expressions(result); - return result; -} - -void text_processor::set_old_style_expression(expression_ptr expr) -{ - tree_ = formating::node_ptr(new formating::text_node(expr)); -} - -/************************************************************/ - void processed_text::push_back(processed_expression const& exp) { expr_list_.push_back(exp); diff --git a/src/text_symbolizer.cpp b/src/text_symbolizer.cpp index 20e26ffdb..ed67c4c8e 100644 --- a/src/text_symbolizer.cpp +++ b/src/text_symbolizer.cpp @@ -137,7 +137,7 @@ expression_ptr text_symbolizer::get_name() const void text_symbolizer::set_name(expression_ptr name) { - placement_options_->properties.processor.set_old_style_expression(name); + placement_options_->properties.set_old_style_expression(name); } expression_ptr text_symbolizer::get_orientation() const @@ -152,22 +152,22 @@ void text_symbolizer::set_orientation(expression_ptr orientation) std::string const& text_symbolizer::get_face_name() const { - return placement_options_->properties.processor.defaults.face_name; + return placement_options_->properties.default_format.face_name; } void text_symbolizer::set_face_name(std::string face_name) { - placement_options_->properties.processor.defaults.face_name = face_name; + placement_options_->properties.default_format.face_name = face_name; } void text_symbolizer::set_fontset(font_set const& fontset) { - placement_options_->properties.processor.defaults.fontset = fontset; + placement_options_->properties.default_format.fontset = fontset; } font_set const& text_symbolizer::get_fontset() const { - return placement_options_->properties.processor.defaults.fontset; + return placement_options_->properties.default_format.fontset; } unsigned text_symbolizer::get_text_ratio() const @@ -192,62 +192,62 @@ void text_symbolizer::set_wrap_width(unsigned width) bool text_symbolizer::get_wrap_before() const { - return placement_options_->properties.processor.defaults.wrap_before; + return placement_options_->properties.default_format.wrap_before; } void text_symbolizer::set_wrap_before(bool wrap_before) { - placement_options_->properties.processor.defaults.wrap_before = wrap_before; + placement_options_->properties.default_format.wrap_before = wrap_before; } unsigned char text_symbolizer::get_wrap_char() const { - return placement_options_->properties.processor.defaults.wrap_char; + return placement_options_->properties.default_format.wrap_char; } std::string text_symbolizer::get_wrap_char_string() const { - return std::string(1, placement_options_->properties.processor.defaults.wrap_char); + return std::string(1, placement_options_->properties.default_format.wrap_char); } void text_symbolizer::set_wrap_char(unsigned char character) { - placement_options_->properties.processor.defaults.wrap_char = character; + placement_options_->properties.default_format.wrap_char = character; } void text_symbolizer::set_wrap_char_from_string(std::string const& character) { - placement_options_->properties.processor.defaults.wrap_char = (character)[0]; + placement_options_->properties.default_format.wrap_char = (character)[0]; } text_transform_e text_symbolizer::get_text_transform() const { - return placement_options_->properties.processor.defaults.text_transform; + return placement_options_->properties.default_format.text_transform; } void text_symbolizer::set_text_transform(text_transform_e convert) { - placement_options_->properties.processor.defaults.text_transform = convert; + placement_options_->properties.default_format.text_transform = convert; } unsigned text_symbolizer::get_line_spacing() const { - return placement_options_->properties.processor.defaults.line_spacing; + return placement_options_->properties.default_format.line_spacing; } void text_symbolizer::set_line_spacing(unsigned spacing) { - placement_options_->properties.processor.defaults.line_spacing = spacing; + placement_options_->properties.default_format.line_spacing = spacing; } unsigned text_symbolizer::get_character_spacing() const { - return placement_options_->properties.processor.defaults.character_spacing; + return placement_options_->properties.default_format.character_spacing; } void text_symbolizer::set_character_spacing(unsigned spacing) { - placement_options_->properties.processor.defaults.character_spacing = spacing; + placement_options_->properties.default_format.character_spacing = spacing; } unsigned text_symbolizer::get_label_spacing() const @@ -292,42 +292,42 @@ void text_symbolizer::set_max_char_angle_delta(double angle) void text_symbolizer::set_text_size(float size) { - placement_options_->properties.processor.defaults.text_size = size; + placement_options_->properties.default_format.text_size = size; } float text_symbolizer::get_text_size() const { - return placement_options_->properties.processor.defaults.text_size; + return placement_options_->properties.default_format.text_size; } void text_symbolizer::set_fill(color const& fill) { - placement_options_->properties.processor.defaults.fill = fill; + placement_options_->properties.default_format.fill = fill; } color const& text_symbolizer::get_fill() const { - return placement_options_->properties.processor.defaults.fill; + return placement_options_->properties.default_format.fill; } void text_symbolizer::set_halo_fill(color const& fill) { - placement_options_->properties.processor.defaults.halo_fill = fill; + placement_options_->properties.default_format.halo_fill = fill; } color const& text_symbolizer::get_halo_fill() const { - return placement_options_->properties.processor.defaults.halo_fill; + return placement_options_->properties.default_format.halo_fill; } void text_symbolizer::set_halo_radius(double radius) { - placement_options_->properties.processor.defaults.halo_radius = radius; + placement_options_->properties.default_format.halo_radius = radius; } double text_symbolizer::get_halo_radius() const { - return placement_options_->properties.processor.defaults.halo_radius; + return placement_options_->properties.default_format.halo_radius; } void text_symbolizer::set_label_placement(label_placement_e label_p) @@ -407,12 +407,12 @@ bool text_symbolizer::get_allow_overlap() const void text_symbolizer::set_text_opacity(double text_opacity) { - placement_options_->properties.processor.defaults.text_opacity = text_opacity; + placement_options_->properties.default_format.text_opacity = text_opacity; } double text_symbolizer::get_text_opacity() const { - return placement_options_->properties.processor.defaults.text_opacity; + return placement_options_->properties.default_format.text_opacity; } void text_symbolizer::set_vertical_alignment(vertical_alignment_e valign)