Skip to content

Don't set a default size for FT2Font #30319

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

Merged
merged 1 commit into from
Jul 16, 2025
Merged
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
9 changes: 9 additions & 0 deletions doc/api/next_api_changes/behavior/30318-ES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FT2Font no longer sets a default size
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the interest of handling non-scalable fonts and reducing font initialization, the
`.FT2Font` constructor no longer sets a default size. Non-scalable fonts are sometimes
used for bitmap-backed emoji fonts.

If metrics are important (i.e., if you are loading character glyphs, or setting a text
string), then explicitly call `.FT2Font.set_size` beforehand.
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/test_ft2font.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def test_ft2font_clear():

def test_ft2font_set_size():
file = fm.findfont('DejaVu Sans')
# Default is 12pt @ 72 dpi.
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=1)
font.set_size(12, 72)
font.set_text('ABabCDcd')
orig = font.get_width_height()
font.set_size(24, 72)
Expand Down Expand Up @@ -757,6 +757,7 @@ def test_ft2font_get_kerning(left, right, unscaled, unfitted, default):
def test_ft2font_set_text():
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_size(12, 72)
xys = font.set_text('')
np.testing.assert_array_equal(xys, np.empty((0, 2)))
assert font.get_width_height() == (0, 0)
Expand All @@ -778,6 +779,7 @@ def test_ft2font_set_text():
def test_ft2font_loading():
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_size(12, 72)
for glyph in [font.load_char(ord('M')),
font.load_glyph(font.get_char_index(ord('M')))]:
assert glyph is not None
Expand Down Expand Up @@ -818,11 +820,13 @@ def test_ft2font_drawing():
expected *= 255
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_size(12, 72)
font.set_text('M')
font.draw_glyphs_to_bitmap(antialiased=False)
image = font.get_image()
np.testing.assert_array_equal(image, expected)
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_size(12, 72)
glyph = font.load_char(ord('M'))
image = np.zeros(expected.shape, np.uint8)
font.draw_glyph_to_bitmap(image, -1, 1, glyph, antialiased=False)
Expand All @@ -832,6 +836,7 @@ def test_ft2font_drawing():
def test_ft2font_get_path():
file = fm.findfont('DejaVu Sans')
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
font.set_size(12, 72)
vertices, codes = font.get_path()
assert vertices.shape == (0, 2)
assert codes.shape == (0, )
Expand Down
6 changes: 0 additions & 6 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,6 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
if (open_args.stream != nullptr) {
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
}
try {
set_size(12., 72.); // Set a default fontsize 12 pt at 72dpi.
} catch (...) {
FT_Done_Face(face);
throw;
}
// Set fallbacks
std::copy(fallback_list.begin(), fallback_list.end(), std::back_inserter(fallbacks));
}
Expand Down