remove state from freetype_engine - now fully static

This commit is contained in:
Dane Springmeyer 2014-10-01 20:03:03 -07:00
parent 940c4a321d
commit ad7e619ba3
10 changed files with 81 additions and 68 deletions

View file

@ -11,12 +11,17 @@ public:
{ {
std::size_t count = 0; std::size_t count = 0;
std::size_t expected_count = mapnik::freetype_engine::face_names().size(); std::size_t expected_count = mapnik::freetype_engine::face_names().size();
mapnik::freetype_engine engine;
mapnik::freetype_engine::font_file_mapping_type font_file_mapping; mapnik::freetype_engine::font_file_mapping_type font_file_mapping;
mapnik::freetype_engine::font_memory_cache_type font_cache; mapnik::freetype_engine::font_memory_cache_type font_cache;
mapnik::font_library library;
for (std::string const& name : mapnik::freetype_engine::face_names()) for (std::string const& name : mapnik::freetype_engine::face_names())
{ {
mapnik::face_ptr f = engine.create_face(name,engine.get_library(),font_file_mapping,font_cache); mapnik::face_ptr f = mapnik::freetype_engine::create_face(name,
library,
font_file_mapping,
font_cache,
mapnik::freetype_engine::get_mapping(),
mapnik::freetype_engine::get_cache());
if (f) ++count; if (f) ++count;
} }
return count == expected_count; return count == expected_count;
@ -27,12 +32,17 @@ public:
for (unsigned i=0;i<iterations_;++i) for (unsigned i=0;i<iterations_;++i)
{ {
std::size_t count = 0; std::size_t count = 0;
mapnik::freetype_engine engine;
mapnik::freetype_engine::font_file_mapping_type font_file_mapping; mapnik::freetype_engine::font_file_mapping_type font_file_mapping;
mapnik::freetype_engine::font_memory_cache_type font_cache; mapnik::freetype_engine::font_memory_cache_type font_cache;
mapnik::font_library library;
for (std::string const& name : mapnik::freetype_engine::face_names()) for (std::string const& name : mapnik::freetype_engine::face_names())
{ {
mapnik::face_ptr f = engine.create_face(name,engine.get_library(),font_file_mapping,font_cache); mapnik::face_ptr f = mapnik::freetype_engine::create_face(name,
library,
font_file_mapping,
font_cache,
mapnik::freetype_engine::get_mapping(),
mapnik::freetype_engine::get_cache());
if (f) ++count; if (f) ++count;
} }
if (count != expected_count) { if (count != expected_count) {

View file

@ -78,18 +78,18 @@ void check_object_status_and_throw_exception(T const& object)
class cairo_face : private mapnik::noncopyable class cairo_face : private mapnik::noncopyable
{ {
public: public:
cairo_face(std::shared_ptr<freetype_engine> const& engine, face_ptr const& face); cairo_face(std::shared_ptr<font_library> const& library, face_ptr const& face);
~cairo_face(); ~cairo_face();
cairo_font_face_t * face() const; cairo_font_face_t * face() const;
private: private:
class handle class handle
{ {
public: public:
handle(std::shared_ptr<freetype_engine> const& engine, face_ptr const& face) handle(std::shared_ptr<font_library> const& library, face_ptr const& face)
: engine_(engine), face_(face) {} : library_(library), face_(face) {}
private: private:
std::shared_ptr<freetype_engine> engine_; std::shared_ptr<font_library> library_;
face_ptr face_; face_ptr face_;
}; };
@ -109,12 +109,12 @@ using cairo_face_ptr = std::shared_ptr<cairo_face>;
class cairo_face_manager : private mapnik::noncopyable class cairo_face_manager : private mapnik::noncopyable
{ {
public: public:
cairo_face_manager(std::shared_ptr<freetype_engine> engine); cairo_face_manager(std::shared_ptr<font_library> library);
cairo_face_ptr get_face(face_ptr face); cairo_face_ptr get_face(face_ptr face);
private: private:
using cairo_face_cache = std::map<face_ptr,cairo_face_ptr>; using cairo_face_cache = std::map<face_ptr,cairo_face_ptr>;
std::shared_ptr<freetype_engine> font_engine_; std::shared_ptr<font_library> font_library_;
cairo_face_cache cache_; cairo_face_cache cache_;
}; };

View file

@ -70,13 +70,13 @@ public:
static bool register_fonts(std::string const& dir, bool recurse = false); static bool register_fonts(std::string const& dir, bool recurse = false);
static std::vector<std::string> face_names(); static std::vector<std::string> face_names();
static font_file_mapping_type const& get_mapping(); static font_file_mapping_type const& get_mapping();
static font_memory_cache_type & get_cache();
static face_ptr create_face(std::string const& family_name, static face_ptr create_face(std::string const& family_name,
font_library & library, font_library & library,
font_file_mapping_type const& font_file_mapping, font_file_mapping_type const& font_file_mapping,
freetype_engine::font_memory_cache_type const& font_cache); freetype_engine::font_memory_cache_type const& font_cache,
stroker_ptr create_stroker(); font_file_mapping_type const& global_font_file_mapping,
virtual ~freetype_engine(); freetype_engine::font_memory_cache_type & global_memory_fonts);
freetype_engine();
static bool register_font_impl(std::string const& file_name, static bool register_font_impl(std::string const& file_name,
font_library & libary, font_library & libary,
font_file_mapping_type & font_file_mapping); font_file_mapping_type & font_file_mapping);
@ -84,14 +84,11 @@ public:
font_library & libary, font_library & libary,
font_file_mapping_type & font_file_mapping, font_file_mapping_type & font_file_mapping,
bool recurse = false); bool recurse = false);
font_library & get_library() virtual ~freetype_engine();
{ freetype_engine();
return library_;
}
private: private:
static bool register_font_impl(std::string const& file_name, FT_LibraryRec_ * library); static bool register_font_impl(std::string const& file_name, FT_LibraryRec_ * library);
static bool register_fonts_impl(std::string const& dir, FT_LibraryRec_ * library, bool recurse = false); static bool register_fonts_impl(std::string const& dir, FT_LibraryRec_ * library, bool recurse = false);
font_library library_;
#ifdef MAPNIK_THREADSAFE #ifdef MAPNIK_THREADSAFE
static std::mutex mutex_; static std::mutex mutex_;
#endif #endif
@ -104,28 +101,20 @@ class MAPNIK_DECL face_manager : private mapnik::noncopyable
using face_ptr_cache_type = std::map<std::string, face_ptr>; using face_ptr_cache_type = std::map<std::string, face_ptr>;
public: public:
face_manager(freetype_engine & engine, face_manager(font_library & library,
freetype_engine::font_file_mapping_type const& font_file_mapping, freetype_engine::font_file_mapping_type const& font_file_mapping,
freetype_engine::font_memory_cache_type const& font_cache) freetype_engine::font_memory_cache_type const& font_cache);
: engine_(engine),
stroker_(engine_.create_stroker()),
face_ptr_cache_(),
library_(engine_.get_library()),
font_file_mapping_(font_file_mapping),
font_memory_cache_(font_cache) {}
face_ptr get_face(std::string const& name); face_ptr get_face(std::string const& name);
face_set_ptr get_face_set(std::string const& name); face_set_ptr get_face_set(std::string const& name);
face_set_ptr get_face_set(font_set const& fset); face_set_ptr get_face_set(font_set const& fset);
face_set_ptr get_face_set(std::string const& name, boost::optional<font_set> fset); face_set_ptr get_face_set(std::string const& name, boost::optional<font_set> fset);
inline stroker_ptr get_stroker() { return stroker_; } inline stroker_ptr get_stroker() { return stroker_; }
private: private:
freetype_engine & engine_;
stroker_ptr stroker_;
face_ptr_cache_type face_ptr_cache_; face_ptr_cache_type face_ptr_cache_;
font_library & library_; font_library & library_;
freetype_engine::font_file_mapping_type const& font_file_mapping_; freetype_engine::font_file_mapping_type const& font_file_mapping_;
freetype_engine::font_memory_cache_type const& font_memory_cache_; freetype_engine::font_memory_cache_type const& font_memory_cache_;
stroker_ptr stroker_;
}; };
using face_manager_freetype = face_manager; using face_manager_freetype = face_manager;

View file

@ -55,8 +55,8 @@ struct renderer_common : private mapnik::noncopyable
double scale_factor_; double scale_factor_;
attributes vars_; attributes vars_;
// TODO: dirty hack for cairo renderer, figure out how to remove this // TODO: dirty hack for cairo renderer, figure out how to remove this
std::shared_ptr<freetype_engine> shared_font_engine_; std::shared_ptr<font_library> shared_font_library_;
freetype_engine & font_engine_; font_library & font_library_;
face_manager_freetype font_manager_; face_manager_freetype font_manager_;
box2d<double> query_extent_; box2d<double> query_extent_;
view_transform t_; view_transform t_;

View file

@ -57,8 +57,8 @@ struct virtual_renderer_common : private mapnik::noncopyable
height_(common.height_), height_(common.height_),
scale_factor_(common.scale_factor_), scale_factor_(common.scale_factor_),
vars_(common.vars_), vars_(common.vars_),
shared_font_engine_(common.shared_font_engine_), shared_font_library_(common.shared_font_library_),
font_engine_(*shared_font_engine_), font_library_(*shared_font_library_),
font_manager_(common.font_manager_), font_manager_(common.font_manager_),
query_extent_(common.query_extent_), query_extent_(common.query_extent_),
t_(common.t_), t_(common.t_),
@ -69,8 +69,8 @@ struct virtual_renderer_common : private mapnik::noncopyable
double & scale_factor_; double & scale_factor_;
attributes & vars_; attributes & vars_;
// TODO: dirty hack for cairo renderer, figure out how to remove this // TODO: dirty hack for cairo renderer, figure out how to remove this
std::shared_ptr<freetype_engine> & shared_font_engine_; std::shared_ptr<font_library> & shared_font_library_;
freetype_engine & font_engine_; font_library & font_library_;
face_manager_freetype & font_manager_; face_manager_freetype & font_manager_;
box2d<double> & query_extent_; box2d<double> & query_extent_;
view_transform & t_; view_transform & t_;

View file

@ -32,12 +32,12 @@
#include <valarray> #include <valarray>
namespace mapnik { namespace mapnik {
cairo_face::cairo_face(std::shared_ptr<freetype_engine> const& engine, face_ptr const& face) cairo_face::cairo_face(std::shared_ptr<font_library> const& library, face_ptr const& face)
: face_(face) : face_(face)
{ {
static cairo_user_data_key_t key; static cairo_user_data_key_t key;
c_face_ = cairo_ft_font_face_create_for_ft_face(face->get_face(), FT_LOAD_NO_HINTING); c_face_ = cairo_ft_font_face_create_for_ft_face(face->get_face(), FT_LOAD_NO_HINTING);
cairo_font_face_set_user_data(c_face_, &key, new handle(engine, face), destroy); cairo_font_face_set_user_data(c_face_, &key, new handle(library, face), destroy);
} }
cairo_face::~cairo_face() cairo_face::~cairo_face()
@ -508,8 +508,8 @@ void cairo_context::add_text(glyph_positions_ptr path,
} }
cairo_face_manager::cairo_face_manager(std::shared_ptr<freetype_engine> font_engine) cairo_face_manager::cairo_face_manager(std::shared_ptr<font_library> font_library)
: font_engine_(font_engine) : font_library_(font_library)
{ {
} }
@ -524,7 +524,7 @@ cairo_face_ptr cairo_face_manager::get_face(face_ptr face)
} }
else else
{ {
entry = std::make_shared<cairo_face>(font_engine_, face); entry = std::make_shared<cairo_face>(font_library_, face);
cache_.emplace(face, entry); cache_.emplace(face, entry);
} }
return entry; return entry;

View file

@ -58,7 +58,7 @@ cairo_renderer<T>::cairo_renderer(Map const& m,
m_(m), m_(m),
context_(cairo), context_(cairo),
common_(m, attributes(), offset_x, offset_y, m.width(), m.height(), scale_factor), common_(m, attributes(), offset_x, offset_y, m.width(), m.height(), scale_factor),
face_manager_(common_.shared_font_engine_) face_manager_(common_.shared_font_library_)
{ {
setup(m); setup(m);
} }
@ -75,7 +75,7 @@ cairo_renderer<T>::cairo_renderer(Map const& m,
m_(m), m_(m),
context_(cairo), context_(cairo),
common_(m, req, vars, offset_x, offset_y, req.width(), req.height(), scale_factor), common_(m, req, vars, offset_x, offset_y, req.width(), req.height(), scale_factor),
face_manager_(common_.shared_font_engine_) face_manager_(common_.shared_font_library_)
{ {
setup(m); setup(m);
} }
@ -91,7 +91,7 @@ cairo_renderer<T>::cairo_renderer(Map const& m,
m_(m), m_(m),
context_(cairo), context_(cairo),
common_(m, attributes(), offset_x, offset_y, m.width(), m.height(), scale_factor, detector), common_(m, attributes(), offset_x, offset_y, m.width(), m.height(), scale_factor, detector),
face_manager_(common_.shared_font_engine_) face_manager_(common_.shared_font_library_)
{ {
setup(m); setup(m);
} }

View file

@ -52,9 +52,7 @@ extern "C"
namespace mapnik namespace mapnik
{ {
freetype_engine::freetype_engine() freetype_engine::freetype_engine() {}
: library_() {}
freetype_engine::~freetype_engine() {} freetype_engine::~freetype_engine() {}
bool freetype_engine::is_font_file(std::string const& file_name) bool freetype_engine::is_font_file(std::string const& file_name)
@ -256,11 +254,17 @@ freetype_engine::font_file_mapping_type const& freetype_engine::get_mapping()
return global_font_file_mapping_; return global_font_file_mapping_;
} }
freetype_engine::font_memory_cache_type & freetype_engine::get_cache()
{
return global_memory_fonts_;
}
face_ptr freetype_engine::create_face(std::string const& family_name, face_ptr freetype_engine::create_face(std::string const& family_name,
font_library & library, font_library & library,
freetype_engine::font_file_mapping_type const& font_file_mapping, freetype_engine::font_file_mapping_type const& font_file_mapping,
freetype_engine::font_memory_cache_type const& font_cache) freetype_engine::font_memory_cache_type const& font_cache,
freetype_engine::font_file_mapping_type const& global_font_file_mapping,
freetype_engine::font_memory_cache_type & global_memory_fonts)
{ {
bool found_font_file = false; bool found_font_file = false;
font_file_mapping_type::const_iterator itr = font_file_mapping.find(family_name); font_file_mapping_type::const_iterator itr = font_file_mapping.find(family_name);
@ -277,7 +281,6 @@ face_ptr freetype_engine::create_face(std::string const& family_name,
static_cast<FT_Long>(mem_font_itr->second.second), // size static_cast<FT_Long>(mem_font_itr->second.second), // size
itr->second.first, // face index itr->second.first, // face index
&face); &face);
if (!error) return std::make_shared<font_face>(face); if (!error) return std::make_shared<font_face>(face);
} }
// we don't add to cache here because the map and its font_cache // we don't add to cache here because the map and its font_cache
@ -287,8 +290,8 @@ face_ptr freetype_engine::create_face(std::string const& family_name,
else else
{ {
// otherwise search global registry // otherwise search global registry
itr = global_font_file_mapping_.find(family_name); itr = global_font_file_mapping.find(family_name);
if (itr != global_font_file_mapping_.end()) if (itr != global_font_file_mapping.end())
{ {
auto mem_font_itr = global_memory_fonts_.find(itr->second.second); auto mem_font_itr = global_memory_fonts_.find(itr->second.second);
// if font already in memory, use it // if font already in memory, use it
@ -300,7 +303,6 @@ face_ptr freetype_engine::create_face(std::string const& family_name,
static_cast<FT_Long>(mem_font_itr->second.second), // size static_cast<FT_Long>(mem_font_itr->second.second), // size
itr->second.first, // face index itr->second.first, // face index
&face); &face);
if (!error) return std::make_shared<font_face>(face); if (!error) return std::make_shared<font_face>(face);
} }
found_font_file = true; found_font_file = true;
@ -334,16 +336,22 @@ face_ptr freetype_engine::create_face(std::string const& family_name,
return face_ptr(); return face_ptr();
} }
stroker_ptr freetype_engine::create_stroker()
{ face_manager::face_manager(font_library & library,
FT_Stroker s; freetype_engine::font_file_mapping_type const& font_file_mapping,
FT_Error error = FT_Stroker_New(library_.get(), &s); freetype_engine::font_memory_cache_type const& font_cache)
if (!error) : face_ptr_cache_(),
{ library_(library),
return std::make_shared<stroker>(s); font_file_mapping_(font_file_mapping),
} font_memory_cache_(font_cache)
return stroker_ptr(); {
} FT_Stroker s;
FT_Error error = FT_Stroker_New(library_.get(), &s);
if (!error)
{
stroker_ = std::make_shared<stroker>(s);
}
}
face_ptr face_manager::get_face(std::string const& name) face_ptr face_manager::get_face(std::string const& name)
{ {
@ -354,7 +362,12 @@ face_ptr face_manager::get_face(std::string const& name)
} }
else else
{ {
face_ptr face = engine_.create_face(name,library_,font_file_mapping_,font_memory_cache_); face_ptr face = freetype_engine::create_face(name,
library_,
font_file_mapping_,
font_memory_cache_,
freetype_engine::get_mapping(),
freetype_engine::get_cache());
if (face) if (face)
{ {
face_ptr_cache_.emplace(name,face); face_ptr_cache_.emplace(name,face);

View file

@ -87,7 +87,8 @@ public:
map_parser(Map & map, bool strict, std::string const& filename = "") : map_parser(Map & map, bool strict, std::string const& filename = "") :
strict_(strict), strict_(strict),
filename_(filename), filename_(filename),
font_manager_(font_engine_,map.get_font_file_mapping(),map.get_font_memory_cache()), font_library_(),
font_manager_(font_library_,map.get_font_file_mapping(),map.get_font_memory_cache()),
xml_base_path_() xml_base_path_()
{} {}
@ -129,7 +130,7 @@ private:
bool strict_; bool strict_;
std::string filename_; std::string filename_;
std::map<std::string,parameters> datasource_templates_; std::map<std::string,parameters> datasource_templates_;
freetype_engine font_engine_; font_library font_library_;
face_manager_freetype font_manager_; face_manager_freetype font_manager_;
std::map<std::string,std::string> file_sources_; std::map<std::string,std::string> file_sources_;
std::map<std::string,font_set> fontsets_; std::map<std::string,font_set> fontsets_;

View file

@ -36,9 +36,9 @@ renderer_common::renderer_common(Map const& map, unsigned width, unsigned height
height_(height), height_(height),
scale_factor_(scale_factor), scale_factor_(scale_factor),
vars_(vars), vars_(vars),
shared_font_engine_(std::make_shared<freetype_engine>()), shared_font_library_(std::make_shared<font_library>()),
font_engine_(*shared_font_engine_), font_library_(*shared_font_library_),
font_manager_(font_engine_,map.get_font_file_mapping(),map.get_font_memory_cache()), font_manager_(font_library_,map.get_font_file_mapping(),map.get_font_memory_cache()),
query_extent_(), query_extent_(),
t_(t), t_(t),
detector_(detector) detector_(detector)