2013-07-02 22:48:16 +02:00
|
|
|
#include <mapnik/font_engine_freetype.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2014-10-02 09:48:02 +02:00
|
|
|
#include <mapnik/map.hpp>
|
|
|
|
#include <mapnik/load_map.hpp>
|
2013-07-02 22:48:16 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2010-07-16 01:09:33 +02:00
|
|
|
|
|
|
|
#include <boost/detail/lightweight_test.hpp>
|
2013-07-02 22:48:16 +02:00
|
|
|
|
2010-07-16 01:09:33 +02:00
|
|
|
#include <iostream>
|
2013-05-25 02:21:55 +02:00
|
|
|
#include <vector>
|
2013-05-26 01:04:40 +02:00
|
|
|
#include <algorithm>
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2013-05-26 02:55:41 +02:00
|
|
|
#include "utils.hpp"
|
|
|
|
|
2013-05-25 02:21:55 +02:00
|
|
|
int main(int argc, char** argv)
|
2010-07-16 01:09:33 +02:00
|
|
|
{
|
2013-05-25 02:21:55 +02:00
|
|
|
std::vector<std::string> args;
|
|
|
|
for (int i=1;i<argc;++i)
|
|
|
|
{
|
|
|
|
args.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
bool quiet = std::find(args.begin(), args.end(), "-q")!=args.end();
|
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
try
|
|
|
|
{
|
2013-07-02 22:48:16 +02:00
|
|
|
mapnik::logger logger;
|
2014-08-12 04:25:30 +02:00
|
|
|
mapnik::logger::severity_type original_severity = logger.get_severity();
|
2013-05-26 02:55:41 +02:00
|
|
|
|
|
|
|
BOOST_TEST(set_working_dir(args));
|
|
|
|
|
2014-10-06 23:31:27 +02:00
|
|
|
|
|
|
|
// grab references to global statics of registered/cached fonts
|
|
|
|
auto const& global_mapping = mapnik::freetype_engine::get_mapping();
|
|
|
|
auto const& global_cache = mapnik::freetype_engine::get_cache();
|
|
|
|
|
|
|
|
// mapnik.Map object has parallel structure for localized fonts
|
|
|
|
mapnik::Map m(1,1);
|
|
|
|
auto const& local_mapping = m.get_font_file_mapping();
|
|
|
|
auto const& local_cache = m.get_font_memory_cache();
|
|
|
|
|
|
|
|
// should be empty to start
|
|
|
|
BOOST_TEST( global_mapping.empty() );
|
|
|
|
BOOST_TEST( global_cache.empty() );
|
|
|
|
BOOST_TEST( local_mapping.empty() );
|
|
|
|
BOOST_TEST( local_cache.empty() );
|
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
std::string fontdir("fonts/");
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
BOOST_TEST( mapnik::util::exists( fontdir ) );
|
|
|
|
BOOST_TEST( mapnik::util::is_directory( fontdir ) );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2014-10-02 09:48:02 +02:00
|
|
|
// test map cached fonts
|
|
|
|
BOOST_TEST( m.register_fonts(fontdir , false ) );
|
|
|
|
BOOST_TEST( m.get_font_memory_cache().size() == 0 );
|
|
|
|
BOOST_TEST( m.get_font_file_mapping().size() == 1 );
|
|
|
|
BOOST_TEST( m.load_fonts() );
|
|
|
|
BOOST_TEST( m.get_font_memory_cache().size() == 1 );
|
|
|
|
BOOST_TEST( m.register_fonts(fontdir , true ) );
|
|
|
|
BOOST_TEST( m.get_font_file_mapping().size() == 22 );
|
|
|
|
BOOST_TEST( m.load_fonts() );
|
|
|
|
BOOST_TEST( m.get_font_memory_cache().size() == 22 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2014-10-02 09:48:02 +02:00
|
|
|
// copy discards memory cache but not file mapping
|
|
|
|
mapnik::Map m2(m);
|
|
|
|
BOOST_TEST( m2.get_font_memory_cache().size() == 0 );
|
|
|
|
BOOST_TEST( m2.get_font_file_mapping().size() == 22 );
|
|
|
|
BOOST_TEST( m2.load_fonts() );
|
|
|
|
BOOST_TEST( m2.get_font_memory_cache().size() == 22 );
|
|
|
|
|
|
|
|
// test font-directory from XML
|
|
|
|
mapnik::Map m3(1,1);
|
|
|
|
mapnik::load_map_string(m3,"<Map font-directory=\"fonts/\"></Map>");
|
|
|
|
BOOST_TEST( m3.get_font_memory_cache().size() == 0 );
|
|
|
|
BOOST_TEST( m3.load_fonts() );
|
|
|
|
BOOST_TEST( m3.get_font_memory_cache().size() == 1 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2014-10-02 09:48:02 +02:00
|
|
|
std::vector<std::string> face_names;
|
|
|
|
std::string foo("foo");
|
2012-12-07 00:15:21 +01:00
|
|
|
// fake directories
|
|
|
|
BOOST_TEST( !mapnik::freetype_engine::register_fonts(foo , true ) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 0 );
|
|
|
|
BOOST_TEST( !mapnik::freetype_engine::register_fonts(foo) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 0 );
|
2012-03-23 23:07:28 +01:00
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// directories without fonts
|
2014-08-12 04:25:30 +02:00
|
|
|
// silence warnings here by altering the logging severity
|
|
|
|
logger.set_severity(mapnik::logger::none);
|
2012-12-07 00:15:21 +01:00
|
|
|
std::string src("src");
|
|
|
|
// an empty directory will not return true
|
|
|
|
// we need to register at least one font and not fail on any
|
|
|
|
// to return true
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_font(src) == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts(src, true) == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::face_names().size() == 0 );
|
2012-03-23 23:07:28 +01:00
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// bogus, emtpy file that looks like font
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_font("tests/data/fonts/fake.ttf") == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts("tests/data/fonts/fake.ttf") == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::face_names().size() == 0 );
|
2012-03-23 23:07:28 +01:00
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_font("tests/data/fonts/intentionally-broken.ttf") == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts("tests/data/fonts/intentionally-broken.ttf") == false );
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::face_names().size() == 0 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2014-08-12 04:25:30 +02:00
|
|
|
// now restore the original severity
|
|
|
|
logger.set_severity(original_severity);
|
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// register unifont, since we know it sits in the root fonts/ dir
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts(fontdir) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() > 0 );
|
|
|
|
BOOST_TEST( face_names.size() == 1 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// re-register unifont, should not have any affect
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts(fontdir, false) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 1 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// register a single dejavu font
|
|
|
|
std::string dejavu_bold_oblique("tests/data/fonts/DejaVuSansMono-BoldOblique.ttf");
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_font(dejavu_bold_oblique) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 2 );
|
2010-07-16 01:09:33 +02:00
|
|
|
|
2014-09-25 03:22:45 +02:00
|
|
|
// now, inspect font mapping and confirm the correct 'DejaVu Sans Mono Bold Oblique' is registered
|
|
|
|
using font_file_mapping = std::map<std::string, std::pair<int,std::string> >;
|
|
|
|
font_file_mapping const& name2file = mapnik::freetype_engine::get_mapping();
|
|
|
|
bool found_dejavu = false;
|
|
|
|
for (auto const& item : name2file)
|
|
|
|
{
|
|
|
|
if (item.first == "DejaVu Sans Mono Bold Oblique")
|
|
|
|
{
|
|
|
|
found_dejavu = true;
|
|
|
|
BOOST_TEST( item.second.first == 0 );
|
|
|
|
BOOST_TEST( item.second.second == dejavu_bold_oblique );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_TEST( found_dejavu );
|
|
|
|
|
2012-12-07 00:15:21 +01:00
|
|
|
// recurse to find all dejavu fonts
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_fonts(fontdir, true) );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 22 );
|
2014-08-12 04:25:30 +02:00
|
|
|
|
2014-09-25 03:22:45 +02:00
|
|
|
// we should have re-registered 'DejaVu Sans Mono Bold Oblique' again,
|
|
|
|
// but now at a new path
|
|
|
|
bool found_dejavu2 = false;
|
|
|
|
for (auto const& item : name2file)
|
|
|
|
{
|
|
|
|
if (item.first == "DejaVu Sans Mono Bold Oblique")
|
|
|
|
{
|
|
|
|
found_dejavu2 = true;
|
|
|
|
BOOST_TEST( item.second.first == 0 );
|
|
|
|
// path should be different
|
|
|
|
BOOST_TEST( item.second.second != dejavu_bold_oblique );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_TEST( found_dejavu2 );
|
|
|
|
|
2014-08-12 04:25:30 +02:00
|
|
|
// check that we can correctly read a .ttc containing
|
|
|
|
// multiple valid faces
|
|
|
|
// https://github.com/mapnik/mapnik/issues/2274
|
|
|
|
BOOST_TEST( mapnik::freetype_engine::register_font("tests/data/fonts/NotoSans-Regular.ttc") );
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() == 24 );
|
|
|
|
|
|
|
|
// now blindly register as many system fonts as possible
|
|
|
|
// the goal here to make sure we don't crash
|
|
|
|
// linux
|
|
|
|
mapnik::freetype_engine::register_fonts("/usr/share/fonts/", true);
|
|
|
|
mapnik::freetype_engine::register_fonts("/usr/local/share/fonts/", true);
|
|
|
|
// osx
|
|
|
|
mapnik::freetype_engine::register_fonts("/Library/Fonts/", true);
|
|
|
|
mapnik::freetype_engine::register_fonts("/System/Library/Fonts/", true);
|
|
|
|
// windows
|
|
|
|
mapnik::freetype_engine::register_fonts("C:\\Windows\\Fonts", true);
|
|
|
|
face_names = mapnik::freetype_engine::face_names();
|
|
|
|
BOOST_TEST( face_names.size() > 22 );
|
2012-12-07 00:15:21 +01:00
|
|
|
}
|
|
|
|
catch (std::exception const & ex)
|
|
|
|
{
|
|
|
|
std::clog << "C++ fonts registration problem: " << ex.what() << "\n";
|
|
|
|
BOOST_TEST(false);
|
|
|
|
}
|
2012-04-17 07:33:37 +02:00
|
|
|
|
|
|
|
if (!::boost::detail::test_errors()) {
|
2013-05-25 02:21:55 +02:00
|
|
|
if (quiet) std::clog << "\x1b[1;32m.\x1b[0m";
|
|
|
|
else std::clog << "C++ fonts registration: \x1b[1;32m✓ \x1b[0m\n";
|
2012-05-15 20:45:55 +02:00
|
|
|
::boost::detail::report_errors_remind().called_report_errors_function = true;
|
2012-04-17 07:33:37 +02:00
|
|
|
} else {
|
|
|
|
return ::boost::report_errors();
|
|
|
|
}
|
2010-07-16 01:09:33 +02:00
|
|
|
}
|