Skip to content

Commit 41cc933

Browse files
committed
Remove fallback code for glyph indices
Glyph indices are specific to each font. It does not make sense to fall back based on glyph index to another font. This could only really be populated by calling `FT2Font.set_text`, but even that was fragile. If a fallback font was used for a character with the same glyph index as a previous character in the main font, then these lookups could be overwritten to the fallback instead of the main font, with a completely different character! Fortunately, nothing actually uses or requires a fallback through glyph indices.
1 parent f1cdc19 commit 41cc933

File tree

3 files changed

+14
-71
lines changed

3 files changed

+14
-71
lines changed

src/ft2font.cpp

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ void FT2Font::clear()
253253
}
254254

255255
glyphs.clear();
256-
glyph_to_font.clear();
257256
char_to_font.clear();
258257

259258
for (auto & fallback : fallbacks) {
@@ -287,35 +286,13 @@ void FT2Font::select_charmap(unsigned long i)
287286
FT_CHECK(FT_Select_Charmap, face, (FT_Encoding)i);
288287
}
289288

290-
int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
291-
bool fallback = false)
292-
{
293-
if (fallback && glyph_to_font.find(left) != glyph_to_font.end() &&
294-
glyph_to_font.find(right) != glyph_to_font.end()) {
295-
FT2Font *left_ft_object = glyph_to_font[left];
296-
FT2Font *right_ft_object = glyph_to_font[right];
297-
if (left_ft_object != right_ft_object) {
298-
// we do not know how to do kerning between different fonts
299-
return 0;
300-
}
301-
// if left_ft_object is the same as right_ft_object,
302-
// do the exact same thing which set_text does.
303-
return right_ft_object->get_kerning(left, right, mode, false);
304-
}
305-
else
306-
{
307-
FT_Vector delta;
308-
return get_kerning(left, right, mode, delta);
309-
}
310-
}
311-
312-
int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
313-
FT_Vector &delta)
289+
int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode)
314290
{
315291
if (!FT_HAS_KERNING(face)) {
316292
return 0;
317293
}
318294

295+
FT_Vector delta;
319296
if (!FT_Get_Kerning(face, left, right, mode, &delta)) {
320297
return (int)(delta.x) / (hinting_factor << kerning_factor);
321298
} else {
@@ -364,16 +341,15 @@ void FT2Font::set_text(
364341
std::set<FT_String*> glyph_seen_fonts;
365342
FT2Font *ft_object_with_glyph = this;
366343
bool was_found = load_char_with_fallback(ft_object_with_glyph, glyph_index, glyphs,
367-
char_to_font, glyph_to_font, codepoint, flags,
344+
char_to_font, codepoint, flags,
368345
charcode_error, glyph_error, glyph_seen_fonts, false);
369346
if (!was_found) {
370347
ft_glyph_warn((FT_ULong)codepoint, glyph_seen_fonts);
371348
// render missing glyph tofu
372349
// come back to top-most font
373350
ft_object_with_glyph = this;
374351
char_to_font[codepoint] = ft_object_with_glyph;
375-
glyph_to_font[glyph_index] = ft_object_with_glyph;
376-
ft_object_with_glyph->load_glyph(glyph_index, flags, ft_object_with_glyph, false);
352+
ft_object_with_glyph->load_glyph(glyph_index, flags);
377353
} else if (ft_object_with_glyph->warn_if_used) {
378354
ft_glyph_warn((FT_ULong)codepoint, glyph_seen_fonts);
379355
}
@@ -383,8 +359,7 @@ void FT2Font::set_text(
383359
ft_object_with_glyph->has_kerning() && // if the font knows how to kern
384360
previous && glyph_index // and we really have 2 glyphs
385361
) {
386-
FT_Vector delta;
387-
pen.x += ft_object_with_glyph->get_kerning(previous, glyph_index, FT_KERNING_DEFAULT, delta);
362+
pen.x += ft_object_with_glyph->get_kerning(previous, glyph_index, FT_KERNING_DEFAULT);
388363
}
389364

390365
// extract glyph image and store it in our table
@@ -434,7 +409,7 @@ void FT2Font::load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool
434409
FT_Error charcode_error, glyph_error;
435410
FT2Font *ft_object_with_glyph = this;
436411
bool was_found = load_char_with_fallback(ft_object_with_glyph, final_glyph_index,
437-
glyphs, char_to_font, glyph_to_font,
412+
glyphs, char_to_font,
438413
charcode, flags, charcode_error, glyph_error,
439414
glyph_seen_fonts, true);
440415
if (!was_found) {
@@ -493,7 +468,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
493468
FT_UInt &final_glyph_index,
494469
std::vector<FT_Glyph> &parent_glyphs,
495470
std::unordered_map<long, FT2Font *> &parent_char_to_font,
496-
std::unordered_map<FT_UInt, FT2Font *> &parent_glyph_to_font,
497471
long charcode,
498472
FT_Int32 flags,
499473
FT_Error &charcode_error,
@@ -523,7 +497,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
523497
// need to store this for anytime a character is loaded from a parent
524498
// FT2Font object or to generate a mapping of individual characters to fonts
525499
ft_object_with_glyph = this;
526-
parent_glyph_to_font[final_glyph_index] = this;
527500
parent_char_to_font[charcode] = this;
528501
parent_glyphs.push_back(thisGlyph);
529502
return true;
@@ -532,7 +505,7 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
532505
for (auto & fallback : fallbacks) {
533506
bool was_found = fallback->load_char_with_fallback(
534507
ft_object_with_glyph, final_glyph_index, parent_glyphs,
535-
parent_char_to_font, parent_glyph_to_font, charcode, flags,
508+
parent_char_to_font, charcode, flags,
536509
charcode_error, glyph_error, glyph_seen_fonts, override);
537510
if (was_found) {
538511
return true;
@@ -542,21 +515,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
542515
}
543516
}
544517

545-
void FT2Font::load_glyph(FT_UInt glyph_index,
546-
FT_Int32 flags,
547-
FT2Font *&ft_object,
548-
bool fallback = false)
549-
{
550-
// cache is only for parent FT2Font
551-
if (fallback && glyph_to_font.find(glyph_index) != glyph_to_font.end()) {
552-
ft_object = glyph_to_font[glyph_index];
553-
} else {
554-
ft_object = this;
555-
}
556-
557-
ft_object->load_glyph(glyph_index, flags);
558-
}
559-
560518
void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
561519
{
562520
FT_CHECK(FT_Load_Glyph, face, glyph_index, flags);
@@ -644,15 +602,8 @@ void FT2Font::draw_glyph_to_bitmap(
644602
draw_bitmap(im, &bitmap->bitmap, x + bitmap->left, y);
645603
}
646604

647-
void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer,
648-
bool fallback = false)
605+
void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer)
649606
{
650-
if (fallback && glyph_to_font.find(glyph_number) != glyph_to_font.end()) {
651-
// cache is only for parent FT2Font
652-
FT2Font *ft_object = glyph_to_font[glyph_number];
653-
ft_object->get_glyph_name(glyph_number, buffer, false);
654-
return;
655-
}
656607
if (!FT_HAS_GLYPH_NAMES(face)) {
657608
/* Note that this generated name must match the name that
658609
is generated by ttconv in ttfont_CharStrings_getname. */

src/ft2font.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,19 @@ class FT2Font
108108
void select_charmap(unsigned long i);
109109
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
110110
std::vector<double> &xys);
111-
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode, bool fallback);
112-
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode, FT_Vector &delta);
111+
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode);
113112
void set_kerning_factor(int factor);
114113
void load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool fallback);
115114
bool load_char_with_fallback(FT2Font *&ft_object_with_glyph,
116115
FT_UInt &final_glyph_index,
117116
std::vector<FT_Glyph> &parent_glyphs,
118117
std::unordered_map<long, FT2Font *> &parent_char_to_font,
119-
std::unordered_map<FT_UInt, FT2Font *> &parent_glyph_to_font,
120118
long charcode,
121119
FT_Int32 flags,
122120
FT_Error &charcode_error,
123121
FT_Error &glyph_error,
124122
std::set<FT_String*> &glyph_seen_fonts,
125123
bool override);
126-
void load_glyph(FT_UInt glyph_index, FT_Int32 flags, FT2Font *&ft_object, bool fallback);
127124
void load_glyph(FT_UInt glyph_index, FT_Int32 flags);
128125
void get_width_height(long *width, long *height);
129126
void get_bitmap_offset(long *x, long *y);
@@ -132,7 +129,7 @@ class FT2Font
132129
void draw_glyph_to_bitmap(
133130
py::array_t<uint8_t, py::array::c_style> im,
134131
int x, int y, size_t glyphInd, bool antialiased);
135-
void get_glyph_name(unsigned int glyph_number, std::string &buffer, bool fallback);
132+
void get_glyph_name(unsigned int glyph_number, std::string &buffer);
136133
long get_name_index(char *name);
137134
FT_UInt get_char_index(FT_ULong charcode, bool fallback);
138135
void get_path(std::vector<double> &vertices, std::vector<unsigned char> &codes);
@@ -176,7 +173,6 @@ class FT2Font
176173
FT_Vector pen; /* untransformed origin */
177174
std::vector<FT_Glyph> glyphs;
178175
std::vector<FT2Font *> fallbacks;
179-
std::unordered_map<FT_UInt, FT2Font *> glyph_to_font;
180176
std::unordered_map<long, FT2Font *> char_to_font;
181177
FT_BBox bbox;
182178
FT_Pos advance;

src/ft2font_wrapper.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@ static int
618618
PyFT2Font_get_kerning(PyFT2Font *self, FT_UInt left, FT_UInt right,
619619
std::variant<FT_Kerning_Mode, FT_UInt> mode_or_int)
620620
{
621-
bool fallback = true;
622621
FT_Kerning_Mode mode;
623622

624623
if (auto value = std::get_if<FT_UInt>(&mode_or_int)) {
@@ -636,7 +635,7 @@ PyFT2Font_get_kerning(PyFT2Font *self, FT_UInt left, FT_UInt right,
636635
throw py::type_error("mode must be Kerning or int");
637636
}
638637

639-
return self->x->get_kerning(left, right, mode, fallback);
638+
return self->x->get_kerning(left, right, mode);
640639
}
641640

642641
const char *PyFT2Font_get_fontmap__doc__ = R"""(
@@ -834,8 +833,6 @@ static PyGlyph *
834833
PyFT2Font_load_glyph(PyFT2Font *self, FT_UInt glyph_index,
835834
std::variant<LoadFlags, FT_Int32> flags_or_int = LoadFlags::FORCE_AUTOHINT)
836835
{
837-
bool fallback = true;
838-
FT2Font *ft_object = nullptr;
839836
LoadFlags flags;
840837

841838
if (auto value = std::get_if<FT_Int32>(&flags_or_int)) {
@@ -853,9 +850,9 @@ PyFT2Font_load_glyph(PyFT2Font *self, FT_UInt glyph_index,
853850
throw py::type_error("flags must be LoadFlags or int");
854851
}
855852

856-
self->x->load_glyph(glyph_index, static_cast<FT_Int32>(flags), ft_object, fallback);
853+
self->x->load_glyph(glyph_index, static_cast<FT_Int32>(flags));
857854

858-
return PyGlyph_from_FT2Font(ft_object);
855+
return PyGlyph_from_FT2Font(self->x);
859856
}
860857

861858
const char *PyFT2Font_get_width_height__doc__ = R"""(
@@ -1022,10 +1019,9 @@ static py::str
10221019
PyFT2Font_get_glyph_name(PyFT2Font *self, unsigned int glyph_number)
10231020
{
10241021
std::string buffer;
1025-
bool fallback = true;
10261022

10271023
buffer.resize(128);
1028-
self->x->get_glyph_name(glyph_number, buffer, fallback);
1024+
self->x->get_glyph_name(glyph_number, buffer);
10291025
return buffer;
10301026
}
10311027

0 commit comments

Comments
 (0)