new util::file class wrapping cstdio + more library usage
This commit is contained in:
parent
3cdd42bab4
commit
23cb5dd47d
2 changed files with 12 additions and 37 deletions
|
@ -102,8 +102,6 @@ public:
|
||||||
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:
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include <mapnik/pixel_position.hpp>
|
#include <mapnik/pixel_position.hpp>
|
||||||
#include <mapnik/text/face.hpp>
|
#include <mapnik/text/face.hpp>
|
||||||
#include <mapnik/util/fs.hpp>
|
#include <mapnik/util/fs.hpp>
|
||||||
|
#include <mapnik/util/file_io.hpp>
|
||||||
#include <mapnik/utils.hpp>
|
#include <mapnik/utils.hpp>
|
||||||
|
|
||||||
// boost
|
// boost
|
||||||
|
@ -109,37 +110,26 @@ bool freetype_engine::register_font(std::string const& file_name)
|
||||||
#ifdef MAPNIK_THREADSAFE
|
#ifdef MAPNIK_THREADSAFE
|
||||||
mapnik::scoped_lock lock(mutex_);
|
mapnik::scoped_lock lock(mutex_);
|
||||||
#endif
|
#endif
|
||||||
|
font_library library;
|
||||||
FT_Library library = 0;
|
bool result = register_font_impl(file_name, library.get());
|
||||||
std::unique_ptr<FT_MemoryRec_> memory(new FT_MemoryRec_);
|
|
||||||
init_freetype(&*memory, library);
|
|
||||||
bool result = register_font_impl(file_name, library);
|
|
||||||
FT_Done_Library(library);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool freetype_engine::register_font_impl(std::string const& file_name, FT_LibraryRec_ * library)
|
bool freetype_engine::register_font_impl(std::string const& file_name, FT_LibraryRec_ * library)
|
||||||
{
|
{
|
||||||
MAPNIK_LOG_DEBUG(font_engine_freetype) << "registering: " << file_name;
|
MAPNIK_LOG_DEBUG(font_engine_freetype) << "registering: " << file_name;
|
||||||
#ifdef _WINDOWS
|
mapnik::util::file file(file_name);
|
||||||
FILE * file = _wfopen(mapnik::utf8_to_utf16(file_name).c_str(), L"rb");
|
if (!file.open()) return false;
|
||||||
#else
|
|
||||||
FILE * file = std::fopen(file_name.c_str(),"rb");
|
|
||||||
#endif
|
|
||||||
if (file == nullptr) return false;
|
|
||||||
|
|
||||||
FT_Face face = 0;
|
FT_Face face = 0;
|
||||||
FT_Open_Args args;
|
FT_Open_Args args;
|
||||||
FT_StreamRec streamRec;
|
FT_StreamRec streamRec;
|
||||||
memset(&args, 0, sizeof(args));
|
memset(&args, 0, sizeof(args));
|
||||||
memset(&streamRec, 0, sizeof(streamRec));
|
memset(&streamRec, 0, sizeof(streamRec));
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
std::size_t file_size = std::ftell(file);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
streamRec.base = 0;
|
streamRec.base = 0;
|
||||||
streamRec.pos = 0;
|
streamRec.pos = 0;
|
||||||
streamRec.size = file_size;
|
streamRec.size = file.size();
|
||||||
streamRec.descriptor.pointer = file;
|
streamRec.descriptor.pointer = file.get();
|
||||||
streamRec.read = ft_read_cb;
|
streamRec.read = ft_read_cb;
|
||||||
streamRec.close = NULL;
|
streamRec.close = NULL;
|
||||||
args.flags = FT_OPEN_STREAM;
|
args.flags = FT_OPEN_STREAM;
|
||||||
|
@ -193,7 +183,6 @@ bool freetype_engine::register_font_impl(std::string const& file_name, FT_Librar
|
||||||
}
|
}
|
||||||
if (face) FT_Done_Face(face);
|
if (face) FT_Done_Face(face);
|
||||||
}
|
}
|
||||||
std::fclose(file);
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,11 +191,8 @@ bool freetype_engine::register_fonts(std::string const& dir, bool recurse)
|
||||||
#ifdef MAPNIK_THREADSAFE
|
#ifdef MAPNIK_THREADSAFE
|
||||||
mapnik::scoped_lock lock(mutex_);
|
mapnik::scoped_lock lock(mutex_);
|
||||||
#endif
|
#endif
|
||||||
std::unique_ptr<FT_MemoryRec_> memory(new FT_MemoryRec_);
|
font_library library;
|
||||||
FT_Library library = 0;
|
bool result = register_fonts_impl(dir, library.get(), recurse);
|
||||||
init_freetype(&*memory, library);
|
|
||||||
bool result = register_fonts_impl(dir, library, recurse);
|
|
||||||
FT_Done_Library(library);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,19 +304,10 @@ face_ptr freetype_engine::create_face(std::string const& family_name)
|
||||||
mapnik::scoped_lock lock(mutex_);
|
mapnik::scoped_lock lock(mutex_);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
mapnik::util::file file(itr->second.second);
|
||||||
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(_wfopen(mapnik::utf8_to_utf16(itr->second.second).c_str(), L"rb"), fclose);
|
if (file.open())
|
||||||
#else
|
|
||||||
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(std::fopen(itr->second.second.c_str(),"rb"), std::fclose);
|
|
||||||
#endif
|
|
||||||
if (file != nullptr)
|
|
||||||
{
|
{
|
||||||
std::fseek(file.get(), 0, SEEK_END);
|
auto result = memory_fonts_.emplace(itr->second.second, std::make_pair(std::move(file.data()),file.size()));
|
||||||
std::size_t file_size = std::ftell(file.get());
|
|
||||||
std::fseek(file.get(), 0, SEEK_SET);
|
|
||||||
std::unique_ptr<char[]> buffer(new char[file_size]);
|
|
||||||
std::fread(buffer.get(), file_size, 1, file.get());
|
|
||||||
auto result = memory_fonts_.emplace(itr->second.second, std::make_pair(std::move(buffer),file_size));
|
|
||||||
FT_Error error = FT_New_Memory_Face (library_.get(),
|
FT_Error error = FT_New_Memory_Face (library_.get(),
|
||||||
reinterpret_cast<FT_Byte const*>(result.first->second.first.get()),
|
reinterpret_cast<FT_Byte const*>(result.first->second.first.get()),
|
||||||
static_cast<FT_Long>(result.first->second.second),
|
static_cast<FT_Long>(result.first->second.second),
|
||||||
|
|
Loading…
Add table
Reference in a new issue