refactor(cairo): Less greedy glyph matching

Only match glyphs for consecutive characters when
the default/preferred font is being tested.

Refs #374
This commit is contained in:
Michael Carlberg 2017-01-25 12:01:16 +01:00
parent c7f33e2567
commit 14fb86ec70
2 changed files with 16 additions and 2 deletions

View File

@ -172,8 +172,15 @@ namespace cairo {
while (!chars.empty()) {
auto remaining = chars.size();
for (auto&& f : fns) {
auto matches = f->match(chars);
if (!matches) {
unsigned int matches;
// Match as many glyphs as possible if the default/preferred font
// is being tested. Otherwise test one glyph at a time against
// the remaining fonts. Roll back to the top of the font list
// when a glyph has been found.
if (f == fns.front() && (matches = f->match(chars)) == 0) {
continue;
} else if (f != fns.front() && (matches = f->match(chars.front())) == 0) {
continue;
}

View File

@ -38,6 +38,7 @@ namespace cairo {
cairo_set_font_face(m_cairo, cairo_font_face_reference(m_font_face));
}
virtual size_t match(utils::unicode_character& character) = 0;
virtual size_t match(utils::unicode_charlist& charlist) = 0;
virtual size_t render(const string& text, double x = 0.0, double y = 0.0) = 0;
virtual void textwidth(const string& text, cairo_text_extents_t* extents) = 0;
@ -130,6 +131,12 @@ namespace cairo {
cairo_set_scaled_font(m_cairo, m_scaled);
}
size_t match(utils::unicode_character& character) override {
auto lock = make_unique<utils::ft_face_lock>(m_scaled);
auto face = static_cast<FT_Face>(*lock);
return FT_Get_Char_Index(face, character.codepoint) ? 1 : 0;
}
size_t match(utils::unicode_charlist& charlist) override {
auto lock = make_unique<utils::ft_face_lock>(m_scaled);
auto face = static_cast<FT_Face>(*lock);