🌐 AI搜索 & 代理 主页
Skip to content

Commit b2bbbfd

Browse files
authored
Merge pull request #9481 from QuLogic/fix-hinting-factor
Apply hinting factor rcParam in all cases.
2 parents 0084e4a + 6a685d2 commit b2bbbfd

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ def _get_agg_font(self, prop):
267267
Get the font for text instance t, cacheing for efficiency
268268
"""
269269
fname = findfont(prop)
270-
font = get_font(
271-
fname,
272-
hinting_factor=rcParams['text.hinting_factor'])
270+
font = get_font(fname)
273271

274272
font.clear()
275273
size = prop.get_size_in_points()

lib/matplotlib/font_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,12 @@ def is_opentype_cff_font(filename):
13691369
_fmcache = None
13701370

13711371

1372-
get_font = lru_cache(64)(ft2font.FT2Font)
1372+
_get_font = lru_cache(64)(ft2font.FT2Font)
1373+
1374+
def get_font(filename, hinting_factor=None):
1375+
if hinting_factor is None:
1376+
hinting_factor = rcParams['text.hinting_factor']
1377+
return _get_font(filename, hinting_factor)
13731378

13741379

13751380
# The experimental fontconfig-based backend.

lib/matplotlib/tests/test_font_manager.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tempfile
88
import warnings
99

10+
import numpy as np
1011
import pytest
1112

1213
from matplotlib.font_manager import (
@@ -86,3 +87,22 @@ def test_otf():
8687
@pytest.mark.skipif(not has_fclist, reason='no fontconfig installed')
8788
def test_get_fontconfig_fonts():
8889
assert len(get_fontconfig_fonts()) > 1
90+
91+
92+
@pytest.mark.parametrize('factor', [2, 4, 6, 8])
93+
def test_hinting_factor(factor):
94+
font = findfont(FontProperties(family=["sans-serif"]))
95+
96+
font1 = get_font(font, hinting_factor=1)
97+
font1.clear()
98+
font1.set_size(12, 100)
99+
font1.set_text('abc')
100+
expected = font1.get_width_height()
101+
102+
hinted_font = get_font(font, hinting_factor=factor)
103+
hinted_font.clear()
104+
hinted_font.set_size(12, 100)
105+
hinted_font.set_text('abc')
106+
# Check that hinting only changes text layout by a small (10%) amount.
107+
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
108+
rtol=0.1)

lib/matplotlib/tests/test_text.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
unicode_literals)
33

44
import six
5+
6+
import io
57
import warnings
68

79
import numpy as np
@@ -448,3 +450,17 @@ def test_nonfinite_pos():
448450
ax.text(0, np.nan, 'nan')
449451
ax.text(np.inf, 0, 'inf')
450452
fig.canvas.draw()
453+
454+
455+
def test_hinting_factor_backends():
456+
plt.rcParams['text.hinting_factor'] = 1
457+
fig = plt.figure()
458+
t = fig.text(0.5, 0.5, 'some text')
459+
460+
fig.savefig(io.BytesIO(), format='svg')
461+
expected = t.get_window_extent().intervalx
462+
463+
fig.savefig(io.BytesIO(), format='png')
464+
# Backends should apply hinting_factor consistently (within 10%).
465+
np.testing.assert_allclose(t.get_window_extent().intervalx, expected,
466+
rtol=0.1)

src/ft2font.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
515515
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
516516
}
517517

518-
static FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
518+
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
519519
FT_Set_Transform(face, &transform, 0);
520520
}
521521

@@ -548,7 +548,7 @@ void FT2Font::set_size(double ptsize, double dpi)
548548
{
549549
int error = FT_Set_Char_Size(
550550
face, (long)(ptsize * 64), 0, (unsigned int)(dpi * hinting_factor), (unsigned int)dpi);
551-
static FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
551+
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
552552
FT_Set_Transform(face, &transform, 0);
553553

554554
if (error) {

0 commit comments

Comments
 (0)