From dfa8f100b29d85c90da26009902dbf70fcc11633 Mon Sep 17 00:00:00 2001 From: artemp Date: Thu, 1 Jun 2017 12:47:18 +0200 Subject: [PATCH] freetype_engine - port `singleton` changes from v3.0.x --- include/mapnik/font_engine_freetype.hpp | 81 +++++++++++++------------ src/font_engine_freetype.cpp | 77 +++++++++++++++++------ src/map.cpp | 2 +- 3 files changed, 102 insertions(+), 58 deletions(-) diff --git a/include/mapnik/font_engine_freetype.hpp b/include/mapnik/font_engine_freetype.hpp index 6a62f3c02..f35fa3dc1 100644 --- a/include/mapnik/font_engine_freetype.hpp +++ b/include/mapnik/font_engine_freetype.hpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2017 Artem Pavlenko + * Copyright (C) 2015 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -25,9 +25,10 @@ // mapnik #include +#include +#include #include #include -#include // stl #include @@ -35,10 +36,6 @@ #include // pair #include -#ifdef MAPNIK_THREADSAFE -#include -#endif - namespace boost { template class optional; } namespace mapnik @@ -51,22 +48,17 @@ using face_set_ptr = std::unique_ptr; class font_face; using face_ptr = std::shared_ptr; -class MAPNIK_DECL freetype_engine +class MAPNIK_DECL freetype_engine : public singleton, + private util::noncopyable { + friend class CreateUsingNew; + friend class Map; public: using font_file_mapping_type = std::map>; using font_memory_cache_type = std::map, std::size_t>>; + static bool is_font_file(std::string const& file_name); - /*! \brief register a font file - * @param file_name path to a font file. - * @return bool - true if at least one face was successfully registered in the file. - */ static bool register_font(std::string const& file_name); - /*! \brief register a font files - * @param dir - path to a directory containing fonts or subdirectories. - * @param recurse - default false, whether to search for fonts in sub directories. - * @return bool - true if at least one face was successfully registered. - */ static bool register_fonts(std::string const& dir, bool recurse = false); static std::vector face_names(); static font_file_mapping_type const& get_mapping(); @@ -75,29 +67,42 @@ public: font_library & library, font_file_mapping_type const& font_file_mapping, font_file_mapping_type const& global_font_file_mapping); + static face_ptr create_face(std::string const& face_name, - font_library & library, - font_file_mapping_type const& font_file_mapping, - freetype_engine::font_memory_cache_type const& font_cache, - font_file_mapping_type const& global_font_file_mapping, - freetype_engine::font_memory_cache_type & global_memory_fonts); - static bool register_font_impl(std::string const& file_name, - font_library & libary, - font_file_mapping_type & font_file_mapping); - static bool register_fonts_impl(std::string const& dir, - font_library & libary, - font_file_mapping_type & font_file_mapping, - bool recurse = false); - virtual ~freetype_engine(); - freetype_engine(); + font_library & library, + font_file_mapping_type const& font_file_mapping, + freetype_engine::font_memory_cache_type const& font_cache, + font_file_mapping_type const& global_font_file_mapping, + freetype_engine::font_memory_cache_type & global_memory_fonts); private: - 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); -#ifdef MAPNIK_THREADSAFE - static std::mutex mutex_; -#endif - static font_file_mapping_type global_font_file_mapping_; - static font_memory_cache_type global_memory_fonts_; + bool is_font_file_impl(std::string const& file_name); + std::vector face_names_impl(); + font_file_mapping_type const& get_mapping_impl(); + font_memory_cache_type& get_cache_impl(); + bool can_open_impl(std::string const& face_name, + font_library & library, + font_file_mapping_type const& font_file_mapping, + font_file_mapping_type const& global_font_file_mapping); + + face_ptr create_face_impl(std::string const& face_name, + font_library & library, + font_file_mapping_type const& font_file_mapping, + freetype_engine::font_memory_cache_type const& font_cache, + font_file_mapping_type const& global_font_file_mapping, + freetype_engine::font_memory_cache_type & global_memory_fonts); + bool register_font_impl(std::string const& file_name); + bool register_fonts_impl(std::string const& dir, bool recurse); + bool register_font_impl(std::string const& file_name, FT_LibraryRec_ * library); + bool register_fonts_impl(std::string const& dir, FT_LibraryRec_ * library, bool recurse = false); + bool register_font_impl(std::string const& file_name, + font_library & libary, + font_file_mapping_type & font_file_mapping); + bool register_fonts_impl(std::string const& dir, + font_library & libary, + font_file_mapping_type & font_file_mapping, + bool recurse = false); + font_file_mapping_type global_font_file_mapping_; + font_memory_cache_type global_memory_fonts_; }; class MAPNIK_DECL face_manager @@ -124,7 +129,7 @@ private: }; using face_manager_freetype = face_manager; - +extern template class MAPNIK_DECL singleton; } #endif // MAPNIK_FONT_ENGINE_FREETYPE_HPP diff --git a/src/font_engine_freetype.cpp b/src/font_engine_freetype.cpp index f2b169898..4d504a8ff 100644 --- a/src/font_engine_freetype.cpp +++ b/src/font_engine_freetype.cpp @@ -2,7 +2,7 @@ * * This file is part of Mapnik (c++ mapping toolkit) * - * Copyright (C) 2017 Artem Pavlenko + * Copyright (C) 2015 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -48,11 +48,10 @@ extern "C" #include #include + namespace mapnik { - -freetype_engine::freetype_engine() {} -freetype_engine::~freetype_engine() {} +template class MAPNIK_DECL singleton; bool freetype_engine::is_font_file(std::string const& file_name) { @@ -78,6 +77,11 @@ unsigned long ft_read_cb(FT_Stream stream, unsigned long offset, unsigned char * } bool freetype_engine::register_font(std::string const& file_name) +{ + return instance().register_font_impl(file_name); +} + +bool freetype_engine::register_font_impl(std::string const& file_name) { #ifdef MAPNIK_THREADSAFE std::lock_guard lock(mutex_); @@ -160,6 +164,11 @@ bool freetype_engine::register_font_impl(std::string const& file_name, } bool freetype_engine::register_fonts(std::string const& dir, bool recurse) +{ + return instance().register_fonts_impl(dir, recurse); +} + +bool freetype_engine::register_fonts_impl(std::string const& dir, bool recurse) { #ifdef MAPNIK_THREADSAFE std::lock_guard lock(mutex_); @@ -215,8 +224,12 @@ bool freetype_engine::register_fonts_impl(std::string const& dir, return success; } +std::vector freetype_engine::face_names() +{ + return instance().face_names_impl(); +} -std::vector freetype_engine::face_names () +std::vector freetype_engine::face_names_impl() { std::vector names; for (auto const& kv : global_font_file_mapping_) @@ -227,11 +240,21 @@ std::vector freetype_engine::face_names () } freetype_engine::font_file_mapping_type const& freetype_engine::get_mapping() +{ + return instance().get_mapping_impl(); +} + +freetype_engine::font_file_mapping_type const& freetype_engine::get_mapping_impl() { return global_font_file_mapping_; } freetype_engine::font_memory_cache_type & freetype_engine::get_cache() +{ + return instance().get_cache_impl(); +} + +freetype_engine::font_memory_cache_type & freetype_engine::get_cache_impl() { return global_memory_fonts_; } @@ -240,6 +263,14 @@ bool freetype_engine::can_open(std::string const& face_name, font_library & library, font_file_mapping_type const& font_file_mapping, font_file_mapping_type const& global_font_file_mapping) +{ + return instance().can_open_impl(face_name, library, font_file_mapping, global_font_file_mapping); +} + +bool freetype_engine::can_open_impl(std::string const& face_name, + font_library & library, + font_file_mapping_type const& font_file_mapping, + font_file_mapping_type const& global_font_file_mapping) { bool found_font_file = false; font_file_mapping_type::const_iterator itr = font_file_mapping.find(face_name); @@ -278,12 +309,12 @@ bool freetype_engine::can_open(std::string const& face_name, return true; } -face_ptr freetype_engine::create_face(std::string const& family_name, - font_library & library, - freetype_engine::font_file_mapping_type const& font_file_mapping, - 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) +face_ptr freetype_engine::create_face_impl(std::string const& family_name, + font_library & library, + freetype_engine::font_file_mapping_type const& font_file_mapping, + 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; font_file_mapping_type::const_iterator itr = font_file_mapping.find(family_name); @@ -355,6 +386,20 @@ face_ptr freetype_engine::create_face(std::string const& family_name, return face_ptr(); } +face_ptr freetype_engine::create_face(std::string const& family_name, + font_library & library, + freetype_engine::font_file_mapping_type const& font_file_mapping, + 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) +{ + return instance().create_face_impl(family_name, + library, + font_file_mapping, + font_cache, + global_font_file_mapping, + global_memory_fonts); +} face_manager::face_manager(font_library & library, freetype_engine::font_file_mapping_type const& font_file_mapping, @@ -385,8 +430,8 @@ face_ptr face_manager::get_face(std::string const& name) library_, font_file_mapping_, font_memory_cache_, - freetype_engine::get_mapping(), - freetype_engine::get_cache()); + freetype_engine::instance().get_mapping(), + freetype_engine::instance().get_cache()); if (face) { face_cache_->emplace(name, face); @@ -440,10 +485,4 @@ face_set_ptr face_manager::get_face_set(std::string const& name, boost::optional } } -#ifdef MAPNIK_THREADSAFE -std::mutex freetype_engine::mutex_; -#endif -freetype_engine::font_file_mapping_type freetype_engine::global_font_file_mapping_; -freetype_engine::font_memory_cache_type freetype_engine::global_memory_fonts_; - } diff --git a/src/map.cpp b/src/map.cpp index 4b20a685e..c8fd3cf48 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -285,7 +285,7 @@ std::map & Map::fontsets() bool Map::register_fonts(std::string const& dir, bool recurse) { font_library library; - return freetype_engine::register_fonts_impl(dir, library, font_file_mapping_, recurse); + return freetype_engine::instance().register_fonts_impl(dir, library, font_file_mapping_, recurse); } bool Map::load_fonts()