Replace dimension_t by char_info.

This commit is contained in:
Hermann Kraus 2012-01-21 00:35:24 +01:00
parent 533b95f0e6
commit 34405a5d9e
4 changed files with 24 additions and 30 deletions

View file

@ -145,13 +145,6 @@ private:
class MAPNIK_DECL font_face_set : private boost::noncopyable class MAPNIK_DECL font_face_set : private boost::noncopyable
{ {
public: public:
class dimension_t {
public:
dimension_t(unsigned width_, int ymax_, int ymin_) : width(width_), height(ymax_-ymin_), ymin(ymin_) {}
unsigned width, height;
int ymin;
};
font_face_set(void) font_face_set(void)
: faces_() {} : faces_() {}
@ -179,7 +172,7 @@ public:
return boost::make_shared<font_glyph>(*faces_.begin(), 0); return boost::make_shared<font_glyph>(*faces_.begin(), 0);
} }
dimension_t character_dimensions(const unsigned c); char_info character_dimensions(const unsigned c);
void get_string_info(string_info & info); void get_string_info(string_info & info);
@ -200,7 +193,7 @@ public:
} }
private: private:
std::vector<face_ptr> faces_; std::vector<face_ptr> faces_;
std::map<unsigned, dimension_t> dimension_cache_; std::map<unsigned, char_info> dimension_cache_;
}; };
// FT_Stroker wrapper // FT_Stroker wrapper

View file

@ -23,9 +23,13 @@
#ifndef MAPNIK_TEXT_PATH_HPP #ifndef MAPNIK_TEXT_PATH_HPP
#define MAPNIK_TEXT_PATH_HPP #define MAPNIK_TEXT_PATH_HPP
// mapnik
#include <mapnik/char_info.hpp>
// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
// uci // uci
#include <unicode/unistr.h> #include <unicode/unistr.h>

View file

@ -19,7 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*****************************************************************************/ *****************************************************************************/
//$Id$
// mapnik // mapnik
#include <mapnik/font_engine_freetype.hpp> #include <mapnik/font_engine_freetype.hpp>
@ -192,9 +191,10 @@ stroker_ptr freetype_engine::create_stroker()
return stroker_ptr(); return stroker_ptr();
} }
font_face_set::dimension_t font_face_set::character_dimensions(const unsigned c) char_info font_face_set::character_dimensions(const unsigned c)
{ {
std::map<unsigned, dimension_t>::const_iterator itr; //Check if char is already in cache
std::map<unsigned, char_info>::const_iterator itr;
itr = dimension_cache_.find(c); itr = dimension_cache_.find(c);
if (itr != dimension_cache_.end()) { if (itr != dimension_cache_.end()) {
return itr->second; return itr->second;
@ -222,21 +222,18 @@ font_face_set::dimension_t font_face_set::character_dimensions(const unsigned c)
error = FT_Load_Glyph (face, glyph->get_index(), FT_LOAD_NO_HINTING); error = FT_Load_Glyph (face, glyph->get_index(), FT_LOAD_NO_HINTING);
if ( error ) if ( error )
return dimension_t(0, 0, 0); return char_info();
error = FT_Get_Glyph(face->glyph, &image); error = FT_Get_Glyph(face->glyph, &image);
if ( error ) if ( error )
return dimension_t(0, 0, 0); return char_info();
FT_Glyph_Get_CBox(image, ft_glyph_bbox_pixels, &glyph_bbox); FT_Glyph_Get_CBox(image, ft_glyph_bbox_pixels, &glyph_bbox);
FT_Done_Glyph(image); FT_Done_Glyph(image);
unsigned tempx = face->glyph->advance.x >> 6; unsigned tempx = face->glyph->advance.x >> 6;
char_info dim(c, tempx, glyph_bbox.yMax, glyph_bbox.yMin, face->size->metrics.height/64.0 /* >> 6 */);
//std::clog << "glyph: " << glyph_index << " x: " << tempx << " y: " << tempy << std::endl; dimension_cache_.insert(std::pair<unsigned, char_info>(c, dim));
dimension_t dim(tempx, glyph_bbox.yMax, glyph_bbox.yMin);
//dimension_cache_[c] = dim; would need an default constructor for dimension_t
dimension_cache_.insert(std::pair<unsigned, dimension_t>(c, dim));
return dim; return dim;
} }
@ -270,10 +267,10 @@ void font_face_set::get_string_info(string_info & info)
StringCharacterIterator iter(shaped); StringCharacterIterator iter(shaped);
for (iter.setToStart(); iter.hasNext();) { for (iter.setToStart(); iter.hasNext();) {
UChar ch = iter.nextPostInc(); UChar ch = iter.nextPostInc();
dimension_t char_dim = character_dimensions(ch); char_info char_dim = character_dimensions(ch);
info.add_info(ch, char_dim.width, char_dim.height); info.add_info(ch, char_dim.width, char_dim.height());
width += char_dim.width; width += char_dim.width;
height = (char_dim.height > height) ? char_dim.height : height; height = (char_dim.height() > height) ? char_dim.height() : height;
} }
} }

View file

@ -218,12 +218,12 @@ void metawriter_json_stream::add_text(placement const& p,
double minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN; double minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
for (int i = 0; i < current_placement.num_nodes(); ++i) { for (int i = 0; i < current_placement.num_nodes(); ++i) {
current_placement.vertex(&c, &x, &y, &angle); current_placement.vertex(&c, &x, &y, &angle);
font_face_set::dimension_t ci = face->character_dimensions(c); char_info ci = face->character_dimensions(c);
if (x < minx) minx = x; if (x < minx) minx = x;
if (x+ci.width > maxx) maxx = x+ci.width; if (x+ci.width > maxx) maxx = x+ci.width;
if (y+ci.height+ci.ymin > maxy) maxy = y+ci.height+ci.ymin; if (y+ci.height()+ci.ymin > maxy) maxy = y+ci.height()+ci.ymin;
if (y+ci.ymin < miny) miny = y+ci.ymin; if (y+ci.ymin < miny) miny = y+ci.ymin;
// std::cout << (char) c << " height:" << ci.height << " ymin:" << ci.ymin << " y:" << y << " miny:"<< miny << " maxy:"<< maxy <<"\n"; // std::cout << (char) c << " height:" << ci.height() << " ymin:" << ci.ymin << " y:" << y << " miny:"<< miny << " maxy:"<< maxy <<"\n";
} }
add_box(box2d<double>(current_placement.starting_x+minx, add_box(box2d<double>(current_placement.starting_x+minx,
@ -242,7 +242,7 @@ void metawriter_json_stream::add_text(placement const& p,
} }
current_placement.vertex(&c, &x, &y, &angle); current_placement.vertex(&c, &x, &y, &angle);
if (c == ' ') continue; if (c == ' ') continue;
font_face_set::dimension_t ci = face->character_dimensions(c); char_info ci = face->character_dimensions(c);
double x0, y0, x1, y1, x2, y2, x3, y3; double x0, y0, x1, y1, x2, y2, x3, y3;
double sina = sin(angle); double sina = sin(angle);
@ -251,10 +251,10 @@ void metawriter_json_stream::add_text(placement const& p,
y0 = current_placement.starting_y - y - cosa*ci.ymin; y0 = current_placement.starting_y - y - cosa*ci.ymin;
x1 = x0 + ci.width * cosa; x1 = x0 + ci.width * cosa;
y1 = y0 - ci.width * sina; y1 = y0 - ci.width * sina;
x2 = x0 + (ci.width * cosa - ci.height * sina); x2 = x0 + (ci.width * cosa - ci.height() * sina);
y2 = y0 - (ci.width * sina + ci.height * cosa); y2 = y0 - (ci.width * sina + ci.height() * cosa);
x3 = x0 - ci.height * sina; x3 = x0 - ci.height() * sina;
y3 = y0 - ci.height * cosa; y3 = y0 - ci.height() * cosa;
*f_ << "\n [["; *f_ << "\n [[";
write_point(t, x0, y0); write_point(t, x0, y0);