Skip to content

Commit 5fc9559

Browse files
committed
Don't set a default size for FT2Font
In the interest of handling non-scalable fonts and reducing font initialization, drop the default size from the `FT2Font` constructor. Non-scalable fonts are sometimes used for bitmap-backed emoji fonts. When we start supporting collection fonts (`.ttc`), then setting a size is a waste, as we will just need to read the count of fonts within. The renderer method `Renderer.draw_text` always sets a size immediately after creating the font object, so this doesn't affect anything in most cases. Only the direct `FT2Font` tests need changes.
1 parent 8839576 commit 5fc9559

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FT2Font no longer sets a default size
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
In the interest of handling non-scalable fonts and reducing font initialization, the
5+
`.FT2Font` constructor no longer sets a default size. Non-scalable fonts are sometimes
6+
used for bitmap-backed emoji fonts.
7+
8+
If metrics are important (i.e., if you are loading character glyphs, or setting a text
9+
string), then explicitly call `.FT2Font.set_size` beforehand.

lib/matplotlib/tests/test_ft2font.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def test_ft2font_clear():
188188

189189
def test_ft2font_set_size():
190190
file = fm.findfont('DejaVu Sans')
191-
# Default is 12pt @ 72 dpi.
192191
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=1)
192+
font.set_size(12, 72)
193193
font.set_text('ABabCDcd')
194194
orig = font.get_width_height()
195195
font.set_size(24, 72)
@@ -757,6 +757,7 @@ def test_ft2font_get_kerning(left, right, unscaled, unfitted, default):
757757
def test_ft2font_set_text():
758758
file = fm.findfont('DejaVu Sans')
759759
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
760+
font.set_size(12, 72)
760761
xys = font.set_text('')
761762
np.testing.assert_array_equal(xys, np.empty((0, 2)))
762763
assert font.get_width_height() == (0, 0)
@@ -778,6 +779,7 @@ def test_ft2font_set_text():
778779
def test_ft2font_loading():
779780
file = fm.findfont('DejaVu Sans')
780781
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
782+
font.set_size(12, 72)
781783
for glyph in [font.load_char(ord('M')),
782784
font.load_glyph(font.get_char_index(ord('M')))]:
783785
assert glyph is not None
@@ -818,11 +820,13 @@ def test_ft2font_drawing():
818820
expected *= 255
819821
file = fm.findfont('DejaVu Sans')
820822
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
823+
font.set_size(12, 72)
821824
font.set_text('M')
822825
font.draw_glyphs_to_bitmap(antialiased=False)
823826
image = font.get_image()
824827
np.testing.assert_array_equal(image, expected)
825828
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
829+
font.set_size(12, 72)
826830
glyph = font.load_char(ord('M'))
827831
image = np.zeros(expected.shape, np.uint8)
828832
font.draw_glyph_to_bitmap(image, -1, 1, glyph, antialiased=False)
@@ -832,6 +836,7 @@ def test_ft2font_drawing():
832836
def test_ft2font_get_path():
833837
file = fm.findfont('DejaVu Sans')
834838
font = ft2font.FT2Font(file, hinting_factor=1, _kerning_factor=0)
839+
font.set_size(12, 72)
835840
vertices, codes = font.get_path()
836841
assert vertices.shape == (0, 2)
837842
assert codes.shape == (0, )

src/ft2font.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,6 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
221221
if (open_args.stream != nullptr) {
222222
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
223223
}
224-
try {
225-
set_size(12., 72.); // Set a default fontsize 12 pt at 72dpi.
226-
} catch (...) {
227-
FT_Done_Face(face);
228-
throw;
229-
}
230224
// Set fallbacks
231225
std::copy(fallback_list.begin(), fallback_list.end(), std::back_inserter(fallbacks));
232226
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy