Skip to content

Remove fallback code for glyph indices #30168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: text-overhaul
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/fill_units.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified lib/matplotlib/tests/baseline_images/test_units/plot_pint.png
65 changes: 8 additions & 57 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ void FT2Font::clear()
}

glyphs.clear();
glyph_to_font.clear();
char_to_font.clear();

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

int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
bool fallback = false)
{
if (fallback && glyph_to_font.find(left) != glyph_to_font.end() &&
glyph_to_font.find(right) != glyph_to_font.end()) {
FT2Font *left_ft_object = glyph_to_font[left];
FT2Font *right_ft_object = glyph_to_font[right];
if (left_ft_object != right_ft_object) {
// we do not know how to do kerning between different fonts
return 0;
}
// if left_ft_object is the same as right_ft_object,
// do the exact same thing which set_text does.
return right_ft_object->get_kerning(left, right, mode, false);
}
else
{
FT_Vector delta;
return get_kerning(left, right, mode, delta);
}
}

int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
FT_Vector &delta)
int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode)
{
if (!FT_HAS_KERNING(face)) {
return 0;
}

FT_Vector delta;
if (!FT_Get_Kerning(face, left, right, mode, &delta)) {
return (int)(delta.x) / (hinting_factor << kerning_factor);
} else {
Expand Down Expand Up @@ -364,16 +341,15 @@ void FT2Font::set_text(
std::set<FT_String*> glyph_seen_fonts;
FT2Font *ft_object_with_glyph = this;
bool was_found = load_char_with_fallback(ft_object_with_glyph, glyph_index, glyphs,
char_to_font, glyph_to_font, codepoint, flags,
char_to_font, codepoint, flags,
charcode_error, glyph_error, glyph_seen_fonts, false);
if (!was_found) {
ft_glyph_warn((FT_ULong)codepoint, glyph_seen_fonts);
// render missing glyph tofu
// come back to top-most font
ft_object_with_glyph = this;
char_to_font[codepoint] = ft_object_with_glyph;
glyph_to_font[glyph_index] = ft_object_with_glyph;
ft_object_with_glyph->load_glyph(glyph_index, flags, ft_object_with_glyph, false);
ft_object_with_glyph->load_glyph(glyph_index, flags);
} else if (ft_object_with_glyph->warn_if_used) {
ft_glyph_warn((FT_ULong)codepoint, glyph_seen_fonts);
}
Expand All @@ -383,8 +359,7 @@ void FT2Font::set_text(
ft_object_with_glyph->has_kerning() && // if the font knows how to kern
previous && glyph_index // and we really have 2 glyphs
) {
FT_Vector delta;
pen.x += ft_object_with_glyph->get_kerning(previous, glyph_index, FT_KERNING_DEFAULT, delta);
pen.x += ft_object_with_glyph->get_kerning(previous, glyph_index, FT_KERNING_DEFAULT);
}

// extract glyph image and store it in our table
Expand Down Expand Up @@ -434,7 +409,7 @@ void FT2Font::load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool
FT_Error charcode_error, glyph_error;
FT2Font *ft_object_with_glyph = this;
bool was_found = load_char_with_fallback(ft_object_with_glyph, final_glyph_index,
glyphs, char_to_font, glyph_to_font,
glyphs, char_to_font,
charcode, flags, charcode_error, glyph_error,
glyph_seen_fonts, true);
if (!was_found) {
Expand Down Expand Up @@ -493,7 +468,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
FT_UInt &final_glyph_index,
std::vector<FT_Glyph> &parent_glyphs,
std::unordered_map<long, FT2Font *> &parent_char_to_font,
std::unordered_map<FT_UInt, FT2Font *> &parent_glyph_to_font,
long charcode,
FT_Int32 flags,
FT_Error &charcode_error,
Expand Down Expand Up @@ -523,7 +497,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
// need to store this for anytime a character is loaded from a parent
// FT2Font object or to generate a mapping of individual characters to fonts
ft_object_with_glyph = this;
parent_glyph_to_font[final_glyph_index] = this;
parent_char_to_font[charcode] = this;
parent_glyphs.push_back(thisGlyph);
return true;
Expand All @@ -532,7 +505,7 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
for (auto & fallback : fallbacks) {
bool was_found = fallback->load_char_with_fallback(
ft_object_with_glyph, final_glyph_index, parent_glyphs,
parent_char_to_font, parent_glyph_to_font, charcode, flags,
parent_char_to_font, charcode, flags,
charcode_error, glyph_error, glyph_seen_fonts, override);
if (was_found) {
return true;
Expand All @@ -542,21 +515,6 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
}
}

void FT2Font::load_glyph(FT_UInt glyph_index,
FT_Int32 flags,
FT2Font *&ft_object,
bool fallback = false)
{
// cache is only for parent FT2Font
if (fallback && glyph_to_font.find(glyph_index) != glyph_to_font.end()) {
ft_object = glyph_to_font[glyph_index];
} else {
ft_object = this;
}

ft_object->load_glyph(glyph_index, flags);
}

void FT2Font::load_glyph(FT_UInt glyph_index, FT_Int32 flags)
{
FT_CHECK(FT_Load_Glyph, face, glyph_index, flags);
Expand Down Expand Up @@ -644,15 +602,8 @@ void FT2Font::draw_glyph_to_bitmap(
draw_bitmap(im, &bitmap->bitmap, x + bitmap->left, y);
}

void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer,
bool fallback = false)
void FT2Font::get_glyph_name(unsigned int glyph_number, std::string &buffer)
{
if (fallback && glyph_to_font.find(glyph_number) != glyph_to_font.end()) {
// cache is only for parent FT2Font
FT2Font *ft_object = glyph_to_font[glyph_number];
ft_object->get_glyph_name(glyph_number, buffer, false);
return;
}
if (!FT_HAS_GLYPH_NAMES(face)) {
/* Note that this generated name must match the name that
is generated by ttconv in ttfont_CharStrings_getname. */
Expand Down
8 changes: 2 additions & 6 deletions src/ft2font.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,19 @@ class FT2Font
void select_charmap(unsigned long i);
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
std::vector<double> &xys);
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode, bool fallback);
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode, FT_Vector &delta);
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode);
void set_kerning_factor(int factor);
void load_char(long charcode, FT_Int32 flags, FT2Font *&ft_object, bool fallback);
bool load_char_with_fallback(FT2Font *&ft_object_with_glyph,
FT_UInt &final_glyph_index,
std::vector<FT_Glyph> &parent_glyphs,
std::unordered_map<long, FT2Font *> &parent_char_to_font,
std::unordered_map<FT_UInt, FT2Font *> &parent_glyph_to_font,
long charcode,
FT_Int32 flags,
FT_Error &charcode_error,
FT_Error &glyph_error,
std::set<FT_String*> &glyph_seen_fonts,
bool override);
void load_glyph(FT_UInt glyph_index, FT_Int32 flags, FT2Font *&ft_object, bool fallback);
void load_glyph(FT_UInt glyph_index, FT_Int32 flags);
void get_width_height(long *width, long *height);
void get_bitmap_offset(long *x, long *y);
Expand All @@ -132,7 +129,7 @@ class FT2Font
void draw_glyph_to_bitmap(
py::array_t<uint8_t, py::array::c_style> im,
int x, int y, size_t glyphInd, bool antialiased);
void get_glyph_name(unsigned int glyph_number, std::string &buffer, bool fallback);
void get_glyph_name(unsigned int glyph_number, std::string &buffer);
long get_name_index(char *name);
FT_UInt get_char_index(FT_ULong charcode, bool fallback);
void get_path(std::vector<double> &vertices, std::vector<unsigned char> &codes);
Expand Down Expand Up @@ -176,7 +173,6 @@ class FT2Font
FT_Vector pen; /* untransformed origin */
std::vector<FT_Glyph> glyphs;
std::vector<FT2Font *> fallbacks;
std::unordered_map<FT_UInt, FT2Font *> glyph_to_font;
std::unordered_map<long, FT2Font *> char_to_font;
FT_BBox bbox;
FT_Pos advance;
Expand Down
12 changes: 4 additions & 8 deletions src/ft2font_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ static int
PyFT2Font_get_kerning(PyFT2Font *self, FT_UInt left, FT_UInt right,
std::variant<FT_Kerning_Mode, FT_UInt> mode_or_int)
{
bool fallback = true;
FT_Kerning_Mode mode;

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

return self->x->get_kerning(left, right, mode, fallback);
return self->x->get_kerning(left, right, mode);
}

const char *PyFT2Font_get_fontmap__doc__ = R"""(
Expand Down Expand Up @@ -834,8 +833,6 @@ static PyGlyph *
PyFT2Font_load_glyph(PyFT2Font *self, FT_UInt glyph_index,
std::variant<LoadFlags, FT_Int32> flags_or_int = LoadFlags::FORCE_AUTOHINT)
{
bool fallback = true;
FT2Font *ft_object = nullptr;
LoadFlags flags;

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

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

return PyGlyph_from_FT2Font(ft_object);
return PyGlyph_from_FT2Font(self->x);
}

const char *PyFT2Font_get_width_height__doc__ = R"""(
Expand Down Expand Up @@ -1022,10 +1019,9 @@ static py::str
PyFT2Font_get_glyph_name(PyFT2Font *self, unsigned int glyph_number)
{
std::string buffer;
bool fallback = true;

buffer.resize(128);
self->x->get_glyph_name(glyph_number, buffer, fallback);
self->x->get_glyph_name(glyph_number, buffer);
return buffer;
}

Expand Down
Loading