From accbc139a57e5447fc753e1caba8a050917c7e14 Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sun, 19 Aug 2012 00:09:45 +0200 Subject: [PATCH] Preallocate memory for glyphs. --- include/mapnik/text/layout.hpp | 4 ++++ include/mapnik/text/placements_list.hpp | 3 +++ src/text/layout.cpp | 14 +++++++++++++- src/text/placement_finder_ng.cpp | 10 ++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/mapnik/text/layout.hpp b/include/mapnik/text/layout.hpp index a74ea3d67..4aa20db4e 100644 --- a/include/mapnik/text/layout.hpp +++ b/include/mapnik/text/layout.hpp @@ -68,6 +68,8 @@ public: unsigned get_first_char() const; unsigned get_last_char() const; + + unsigned size() const; private: glyph_vector glyphs_; double line_height_; //Includes line spacing (returned by freetype) @@ -100,6 +102,7 @@ public: unsigned size() const; double cluster_width(unsigned cluster) const; + unsigned glyphs_count() const; private: void break_line(text_line_ptr line, double wrap_width, unsigned text_ratio); @@ -115,6 +118,7 @@ private: std::map width_map_; double width_; double height_; + unsigned glyphs_count_; //output diff --git a/include/mapnik/text/placements_list.hpp b/include/mapnik/text/placements_list.hpp index a2f3ba7cc..211d7f01f 100644 --- a/include/mapnik/text/placements_list.hpp +++ b/include/mapnik/text/placements_list.hpp @@ -73,17 +73,20 @@ public: const_iterator end() const; void push_back(glyph_info const& glyph, pixel_position offset, rotation const& rot); + void reserve(unsigned count); pixel_position const& get_base_point() const; void set_base_point(pixel_position base_point); void set_marker(marker_info_ptr marker, pixel_position const& marker_pos); marker_info_ptr marker() const; pixel_position const& marker_pos() const; + box2d const & bbox() const; private: std::vector data_; pixel_position base_point_; marker_info_ptr marker_; pixel_position marker_pos_; + box2d bbox_; }; typedef boost::shared_ptr glyph_positions_ptr; diff --git a/src/text/layout.cpp b/src/text/layout.cpp index aed2e04c9..4b06c1523 100644 --- a/src/text/layout.cpp +++ b/src/text/layout.cpp @@ -38,7 +38,8 @@ namespace mapnik { text_layout::text_layout(face_manager_freetype &font_manager) - : font_manager_(font_manager), itemizer_(), width_(0), height_(0), lines_() + : font_manager_(font_manager), itemizer_(), width_(0), height_(0), glyphs_count_(0), + lines_() { } @@ -205,6 +206,7 @@ void text_layout::add_line(text_line_ptr line) lines_.push_back(line); width_ = std::max(width_, line->width()); height_ += line->height(); + glyphs_count_ += line->size(); } void text_layout::clear() @@ -248,6 +250,11 @@ double text_layout::cluster_width(unsigned cluster) const return 0; } +unsigned text_layout::glyphs_count() const +{ + return glyphs_count_; +} + /*********************************************************************************************/ text_line::text_line(unsigned first_char, unsigned last_char) @@ -311,4 +318,9 @@ unsigned text_line::get_last_char() const return last_char_; } +unsigned text_line::size() const +{ + glyphs_.size(); +} + } //ns mapnik diff --git a/src/text/placement_finder_ng.cpp b/src/text/placement_finder_ng.cpp index 964225575..e6256d7ba 100644 --- a/src/text/placement_finder_ng.cpp +++ b/src/text/placement_finder_ng.cpp @@ -204,6 +204,7 @@ bool placement_finder_ng::find_point_placement(pixel_position pos) // set for upper left corner of text envelope for the first line, top left of first character y = layout_.height() / 2.0; + glyphs->reserve(layout_.glyphs_count()); text_layout::const_iterator line_itr = layout_.begin(), line_end = layout_.end(); for (; line_itr != line_end; line_itr++) { @@ -316,6 +317,7 @@ bool placement_finder_ng::single_line_placement(vertex_cache &pp, text_upright_e double sign = (real_orientation == UPRIGHT_LEFT) ? -1 : 1; double offset = alignment_offset().y + info_->properties.displacement.y + sign * layout_.height()/2.; + glyphs->reserve(layout_.glyphs_count()); text_layout::const_iterator line_itr = layout_.begin(), line_end = layout_.end(); for (; line_itr != line_end; line_itr++) { @@ -496,7 +498,7 @@ box2d placement_finder_ng::get_bbox(glyph_info const& glyph, pixel_posit glyph_positions::glyph_positions() - : base_point_() + : data_(), base_point_(), marker_(), marker_pos_(), bbox_() { } @@ -513,10 +515,14 @@ glyph_positions::const_iterator glyph_positions::end() const void glyph_positions::push_back(const glyph_info &glyph, pixel_position offset, rotation const& rot) { - data_.push_back(glyph_position(glyph, offset, rot)); } +void glyph_positions::reserve(unsigned count) +{ + data_.reserve(count); +} + pixel_position const& glyph_positions::get_base_point() const { return base_point_;