add helper functions for recursively registering fonts, and ensure they match one of the known freetype2 supported fonts (by ext) - closes #559

This commit is contained in:
Dane Springmeyer 2010-07-15 23:04:51 +00:00
parent 4f9d6e135c
commit 7918184d13
3 changed files with 46 additions and 4 deletions

View file

@ -613,11 +613,11 @@ def register_plugins(path=inputpluginspath):
"""Register plugins located by specified path"""
DatasourceCache.instance().register_datasources(path)
def register_fonts(path=fontscollectionpath):
def register_fonts(path=fontscollectionpath,valid_extensions=['.ttf','.otf','.ttc','.pfa','.pfb','.ttc','.dfont']):
"""Recursively register fonts using path argument as base directory"""
for dirpath, _, filenames in os.walk(path):
for filename in filenames:
if os.path.splitext(filename)[1] == '.ttf':
if os.path.splitext(filename)[1] in valid_extensions:
FontEngine.instance().register_font(os.path.join(dirpath, filename))
# auto-register known plugins and fonts

View file

@ -345,7 +345,9 @@ typedef boost::shared_ptr<stroker> stroker_ptr;
class MAPNIK_DECL freetype_engine
{
public:
static bool is_font_file(std::string const& file_name);
static bool register_font(std::string const& file_name);
static bool register_fonts(std::string const& dir, bool recurse = false);
static std::vector<std::string> face_names ();
face_ptr create_face(std::string const& family_name);
stroker_ptr create_stroker();

View file

@ -21,8 +21,13 @@
*****************************************************************************/
//$Id$
// mapnik
#include <mapnik/font_engine_freetype.hpp>
// boost
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
namespace mapnik
{
freetype_engine::freetype_engine()
@ -38,9 +43,24 @@ freetype_engine::~freetype_engine()
{
FT_Done_FreeType(library_);
}
bool freetype_engine::is_font_file(std::string const& file_name)
{
/** only accept files that will be matched by freetype2's `figurefiletype()` */
std::string const& fn = boost::algorithm::to_lower_copy(file_name);
return boost::algorithm::ends_with(fn,std::string(".ttf")) ||
boost::algorithm::ends_with(fn,std::string(".otf")) ||
boost::algorithm::ends_with(fn,std::string(".ttc")) ||
boost::algorithm::ends_with(fn,std::string(".pfa")) ||
boost::algorithm::ends_with(fn,std::string(".pfb")) ||
boost::algorithm::ends_with(fn,std::string(".ttc")) ||
/** Plus OSX custom ext */
boost::algorithm::ends_with(fn,std::string(".dfont"));
}
bool freetype_engine::register_font(std::string const& file_name)
{
if (!boost::filesystem::is_regular_file(file_name) || !is_font_file(file_name)) return false;
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
@ -64,7 +84,27 @@ bool freetype_engine::register_font(std::string const& file_name)
FT_Done_FreeType(library);
return true;
}
bool freetype_engine::register_fonts(std::string const& dir, bool recurse)
{
boost::filesystem::path path(dir);
if (!boost::filesystem::exists(path) || !boost::filesystem::is_directory(path)) return false;
boost::filesystem::directory_iterator end_itr;
for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr)
{
if (boost::filesystem::is_directory(*itr) && recurse)
{
if (!register_fonts(itr->string(), true)) return false;
}
else
{
mapnik::freetype_engine::register_font(itr->string());
}
}
return true;
}
std::vector<std::string> freetype_engine::face_names ()
{
std::vector<std::string> names;