2015-07-09 06:28:38 +00:00
|
|
|
#include "catch.hpp"
|
|
|
|
#include <mapnik/text/icu_shaper.hpp>
|
|
|
|
#include <mapnik/text/harfbuzz_shaper.hpp>
|
|
|
|
#include <mapnik/text/font_library.hpp>
|
2017-02-20 11:13:00 +00:00
|
|
|
#include <mapnik/unicode.hpp>
|
2021-05-17 15:06:34 +00:00
|
|
|
#include <mapnik/util/from_u8string.hpp>
|
2015-07-09 06:28:38 +00:00
|
|
|
|
2017-02-21 11:43:54 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-05-17 15:06:34 +00:00
|
|
|
using mapnik::util::from_u8string;
|
|
|
|
|
2022-01-26 22:25:53 +00:00
|
|
|
void test_shaping(mapnik::font_set const& fontset,
|
|
|
|
mapnik::face_manager& fm,
|
|
|
|
std::vector<std::pair<unsigned, unsigned>> const& expected,
|
|
|
|
char const* str,
|
|
|
|
bool debug = false)
|
2017-02-21 11:43:54 +00:00
|
|
|
{
|
|
|
|
mapnik::transcoder tr("utf8");
|
2022-01-26 22:25:53 +00:00
|
|
|
std::map<unsigned, double> width_map;
|
2017-02-21 11:43:54 +00:00
|
|
|
mapnik::text_itemizer itemizer;
|
|
|
|
auto props = std::make_unique<mapnik::detail::evaluated_format_properties>();
|
|
|
|
props->fontset = fontset;
|
|
|
|
props->text_size = 32;
|
|
|
|
|
|
|
|
double scale_factor = 1;
|
|
|
|
auto ustr = tr.transcode(str);
|
|
|
|
auto length = ustr.length();
|
|
|
|
itemizer.add_text(ustr, props);
|
|
|
|
|
|
|
|
mapnik::text_line line(0, length);
|
2022-01-26 22:25:53 +00:00
|
|
|
mapnik::harfbuzz_shaper::shape_text(line, itemizer, width_map, fm, scale_factor);
|
2017-02-21 11:43:54 +00:00
|
|
|
|
|
|
|
std::size_t index = 0;
|
|
|
|
for (auto const& g : line)
|
|
|
|
{
|
2017-02-21 12:17:41 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
2022-01-26 22:25:53 +00:00
|
|
|
if (index++ > 0)
|
|
|
|
std::cerr << ",";
|
|
|
|
std::cerr << "{" << g.glyph_index << ", "
|
|
|
|
<< g.char_index
|
|
|
|
//<< ", " << g.face->family_name() << ":" << g.face->style_name()
|
|
|
|
<< "}";
|
2017-02-21 12:17:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned glyph_index, char_index;
|
|
|
|
CHECK(index < expected.size());
|
|
|
|
std::tie(glyph_index, char_index) = expected[index++];
|
|
|
|
REQUIRE(glyph_index == g.glyph_index);
|
|
|
|
REQUIRE(char_index == g.char_index);
|
|
|
|
}
|
2017-02-21 11:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 22:25:53 +00:00
|
|
|
} // namespace
|
2017-02-21 11:43:54 +00:00
|
|
|
|
2017-02-20 11:13:00 +00:00
|
|
|
TEST_CASE("shaping")
|
|
|
|
{
|
|
|
|
mapnik::freetype_engine::register_font("test/data/fonts/NotoSans-Regular.ttc");
|
2017-02-20 13:23:09 +00:00
|
|
|
mapnik::freetype_engine::register_fonts("test/data/fonts/Noto");
|
2017-02-20 11:13:00 +00:00
|
|
|
mapnik::font_set fontset("fontset");
|
|
|
|
for (auto const& name : mapnik::freetype_engine::face_names())
|
|
|
|
{
|
|
|
|
fontset.add_face_name(name);
|
|
|
|
}
|
2017-02-21 11:43:54 +00:00
|
|
|
|
2015-07-09 06:28:38 +00:00
|
|
|
mapnik::font_library fl;
|
|
|
|
mapnik::freetype_engine::font_file_mapping_type font_file_mapping;
|
|
|
|
mapnik::freetype_engine::font_memory_cache_type font_memory_cache;
|
2017-02-20 11:13:00 +00:00
|
|
|
mapnik::face_manager fm(fl, font_file_mapping, font_memory_cache);
|
|
|
|
|
|
|
|
{
|
2017-03-07 13:08:04 +00:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> expected =
|
2022-01-26 22:25:53 +00:00
|
|
|
{{977, 0}, {1094, 3}, {1038, 4}, {1168, 4}, {9, 7}, {3, 8}, {11, 9}, {68, 10}, {69, 11}, {70, 12}, {12, 13}};
|
2021-05-17 15:06:34 +00:00
|
|
|
test_shaping(fontset, fm, expected, from_u8string(u8"སྤུ་ཧྲེང (abc)").c_str());
|
2017-02-20 11:13:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 11:43:54 +00:00
|
|
|
{
|
2017-03-07 13:08:04 +00:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> expected =
|
2022-01-26 22:25:53 +00:00
|
|
|
{{977, 0}, {1094, 3}, {1038, 4}, {1168, 4}, {9, 7}, {3, 8}, {11, 9}, {0, 10}, {0, 11}, {0, 12}, {12, 13}};
|
2021-05-17 15:06:34 +00:00
|
|
|
test_shaping(fontset, fm, expected, from_u8string(u8"སྤུ་ཧྲེང (普兰镇)").c_str());
|
2017-02-20 11:13:00 +00:00
|
|
|
}
|
2017-02-21 11:43:54 +00:00
|
|
|
|
2017-02-21 11:56:36 +00:00
|
|
|
{
|
2017-03-07 13:08:04 +00:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> expected =
|
2022-01-26 22:25:53 +00:00
|
|
|
{{68, 0}, {69, 1}, {70, 2}, {3, 3}, {11, 4}, {0, 5}, {0, 6}, {0, 7}, {12, 8}};
|
2021-05-17 15:06:34 +00:00
|
|
|
test_shaping(fontset, fm, expected, from_u8string(u8"abc (普兰镇)").c_str());
|
2017-02-21 11:56:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 12:17:41 +00:00
|
|
|
{
|
2017-03-07 13:08:04 +00:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> expected =
|
2022-01-26 22:25:53 +00:00
|
|
|
{{68, 0}, {69, 1}, {70, 2}, {3, 3}, {11, 4}, {68, 5}, {69, 6}, {70, 7}, {12, 8}};
|
2017-02-21 12:17:41 +00:00
|
|
|
test_shaping(fontset, fm, expected, "abc (abc)");
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:31:06 +00:00
|
|
|
{
|
|
|
|
// "ⵃⴰⵢ ⵚⵉⵏⴰⵄⵉ الحي الصناعي"
|
2022-01-26 22:25:53 +00:00
|
|
|
std::vector<std::pair<unsigned, unsigned>> expected = {
|
|
|
|
{0, 0}, {0, 1}, {0, 2}, {3, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7},
|
|
|
|
{0, 8}, {0, 9}, {3, 10}, {509, 22}, {481, 21}, {438, 20}, {503, 19}, {470, 18},
|
|
|
|
{496, 17}, {43, 16}, {3, 15}, {509, 14}, {454, 13}, {496, 12}, {43, 11}};
|
2021-05-17 15:06:34 +00:00
|
|
|
test_shaping(fontset, fm, expected, from_u8string(u8"ⵃⴰⵢ ⵚⵉⵏⴰⵄⵉ الحي الصناعي").c_str());
|
2017-02-22 10:31:06 +00:00
|
|
|
}
|
2017-02-20 11:13:00 +00:00
|
|
|
}
|