Example #1
0
bool freetype_engine::register_fonts(std::string const& dir, bool recurse)
{
    boost::filesystem::path path(dir);

    if (!boost::filesystem::exists(path))
        return false;

    if (!boost::filesystem::is_directory(path))
        return mapnik::freetype_engine::register_font(dir);

    boost::filesystem::directory_iterator end_itr;
    bool success = false;
    for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr)
    {
#if (BOOST_FILESYSTEM_VERSION == 3)
        std::string const& file_name = itr->path().string();
#else // v2
        std::string const& file_name = itr->string();
#endif
        if (boost::filesystem::is_directory(*itr) && recurse)
        {
            success = register_fonts(file_name, true);
        }
        else if (boost::filesystem::is_regular_file(file_name) && is_font_file(file_name))
        {
            success = mapnik::freetype_engine::register_font(file_name);
        }
    }
    return success;
}
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
    FT_Library library;
    FT_Error error = FT_Init_FreeType(&library);
    if (error)
    {
        throw std::runtime_error("Failed to initialize FreeType2 library");
    }
      
    FT_Face face;
    error = FT_New_Face (library,file_name.c_str(),0,&face);
    if (error)
    {
        FT_Done_FreeType(library);
        return false;
    }
    std::string name = std::string(face->family_name) + " " + std::string(face->style_name);
    name2file_.insert(std::make_pair(name,file_name));
    FT_Done_Face(face );   
    FT_Done_FreeType(library);
    return true;
}
Example #3
0
bool freetype_engine::register_fonts_impl(std::string const& dir,
                                          font_library & library,
                                          freetype_engine::font_file_mapping_type & font_file_mapping,
                                          bool recurse)
{
    if (!mapnik::util::exists(dir))
    {
        return false;
    }
    if (!mapnik::util::is_directory(dir))
    {
        return register_font_impl(dir, library, font_file_mapping);
    }
    bool success = false;
    try
    {
        boost::filesystem::directory_iterator end_itr;
#ifdef _WINDOWS
        std::wstring wide_dir(mapnik::utf8_to_utf16(dir));
        for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr)
        {
            std::string file_name = mapnik::utf16_to_utf8(itr->path().wstring());
#else
        for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr)
        {
            std::string file_name = itr->path().string();
#endif
            if (boost::filesystem::is_directory(*itr) && recurse)
            {
                if (register_fonts_impl(file_name, library, font_file_mapping, true))
                {
                    success = true;
                }
            }
            else
            {
                std::string base_name = itr->path().filename().string();
                if (!boost::algorithm::starts_with(base_name,".") &&
                    mapnik::util::is_regular_file(file_name) &&
                    is_font_file(file_name))
                {
                    if (register_font_impl(file_name, library, font_file_mapping))
                    {
                        success = true;
                    }
                }
            }
        }
    }
    catch (std::exception const& ex)
    {
        MAPNIK_LOG_ERROR(font_engine_freetype) << "register_fonts: " << ex.what();
    }
    return success;
}
bool freetype_engine::register_fonts(std::string const& dir, bool recurse)
{
    if (!mapnik::util::exists(dir))
    {
        return false;
    }
    if (!mapnik::util::is_directory(dir))
    {
        return mapnik::freetype_engine::register_font(dir);
    }
    bool success = false;
    try
    {
        boost::filesystem::directory_iterator end_itr;
        for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr)
        {
    #if (BOOST_FILESYSTEM_VERSION == 3)
            std::string file_name = itr->path().string();
    #else // v2
            std::string file_name = itr->string();
    #endif
            if (boost::filesystem::is_directory(*itr) && recurse)
            {
                if (register_fonts(file_name, true))
                {
                    success = true;
                }
            }
            else
            {
    #if (BOOST_FILESYSTEM_VERSION == 3)
                std::string base_name = itr->path().filename().string();
    #else // v2
                std::string base_name = itr->filename();
    #endif
                if (!boost::algorithm::starts_with(base_name,".") &&
                    boost::filesystem::is_regular_file(file_name) &&
                    is_font_file(file_name))
                {
                    if (mapnik::freetype_engine::register_font(file_name))
                    {
                        success = true;
                    }
                }
            }
        }
    }
    catch (std::exception const& ex)
    {
        MAPNIK_LOG_ERROR(font_engine_freetype) << "register_fonts: " << ex.what();
    }
    return success;
}
Example #5
0
bool freetype_engine::register_fonts_impl(std::string const& dir,
                                          font_library & library,
                                          freetype_engine::font_file_mapping_type & font_file_mapping,
                                          bool recurse)
{
    if (!mapnik::util::exists(dir))
    {
        return false;
    }
    if (!mapnik::util::is_directory(dir))
    {
        return register_font_impl(dir, library, font_file_mapping);
    }
    bool success = false;
    try
    {
        for (std::string const& file_name : mapnik::util::list_directory(dir))
        {
            if (mapnik::util::is_directory(file_name) && recurse)
            {
                if (register_fonts_impl(file_name, library, font_file_mapping, true))
                {
                    success = true;
                }
            }
            else
            {
                std::string base_name = mapnik::util::basename(file_name);
                if (!boost::algorithm::starts_with(base_name,".") &&
                    mapnik::util::is_regular_file(file_name) &&
                    is_font_file(file_name))
                {
                    if (register_font_impl(file_name, library, font_file_mapping))
                    {
                        success = true;
                    }
                }
            }
        }
    }
    catch (std::exception const& ex)
    {
        MAPNIK_LOG_ERROR(font_engine_freetype) << "register_fonts: " << ex.what();
    }
    return success;
}
Example #6
0
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
    FT_Library library;
    FT_Error error = FT_Init_FreeType(&library);
    if (error)
    {
        throw std::runtime_error("Failed to initialize FreeType2 library");
    }

    FT_Face face = 0;
    // some font files have multiple fonts in a file
    // the count is in the 'root' face library[0]
    // see the FT_FaceRec in freetype.h
    for ( int i = 0; face == 0 || i < face->num_faces; i++ ) {
        // if face is null then this is the first face
        error = FT_New_Face (library,file_name.c_str(),i,&face);
        if (error)
        {
            FT_Done_FreeType(library);
            return false;
        }
        // some fonts can lack names, skip them
        // http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html#FT_FaceRec
        if (face->family_name && face->style_name) {
            std::string name = std::string(face->family_name) + " " + std::string(face->style_name);
            name2file_.insert(std::make_pair(name, std::make_pair(i,file_name)));
            FT_Done_Face(face);
            //FT_Done_FreeType(library);
            //return true;
        } else {
            FT_Done_Face(face);
            FT_Done_FreeType(library);
            std::ostringstream s;
            s << "Error: unable to load invalid font file which lacks identifiable family and style name: '"
              << file_name << "'";
            throw std::runtime_error(s.str());
        }
    }
    FT_Done_FreeType(library);
    return true;
}