font_engine : use FT_New_Library/FT_Done_Library with custom memory management (ref #2209 #2210)

This commit is contained in:
artemp 2014-05-13 12:43:59 +01:00
parent 039e620147
commit f0fd0aa8ce
2 changed files with 28 additions and 4 deletions

View file

@ -44,6 +44,7 @@
#include <vector>
struct FT_LibraryRec_;
struct FT_MemoryRec_;
namespace mapnik
{
@ -78,7 +79,8 @@ public:
virtual ~freetype_engine();
freetype_engine();
private:
FT_LibraryRec_ *library_;
FT_LibraryRec_ * library_;
std::unique_ptr<FT_MemoryRec_> memory_;
#ifdef MAPNIK_THREADSAFE
static std::mutex mutex_;
#endif

View file

@ -34,6 +34,7 @@
// stl
#include <algorithm>
#include <stdexcept>
#include <cstdlib>
// freetype2
extern "C"
@ -41,25 +42,46 @@ extern "C"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H
#include FT_MODULE_H
}
void* _Alloc_Func(FT_Memory memory, long size)
{
return std::malloc(size);
}
void _Free_Func(FT_Memory memory, void *block)
{
std::free(block);
}
void* _Realloc_Func(FT_Memory memory, long cur_size, long new_size, void* block)
{
return std::realloc(block, new_size);
}
namespace mapnik
{
freetype_engine::freetype_engine() :
library_(nullptr)
library_(nullptr),
memory_(new FT_MemoryRec_)
{
FT_Error error = FT_Init_FreeType( &library_ );
memory_->alloc = _Alloc_Func;
memory_->free = _Free_Func;
memory_->realloc = _Realloc_Func;
FT_Error error = FT_New_Library( &*memory_, &library_ );
if (error)
{
throw std::runtime_error("can not load FreeType2 library");
}
FT_Add_Default_Modules(library_);
}
freetype_engine::~freetype_engine()
{
FT_Done_FreeType(library_);
FT_Done_Library(library_);
}
bool freetype_engine::is_font_file(std::string const& file_name)