Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Apply hinting factor rcParam in all cases. #9481

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 3 commits into from
Nov 4, 2017
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
4 changes: 1 addition & 3 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ def _get_agg_font(self, prop):
Get the font for text instance t, cacheing for efficiency
"""
fname = findfont(prop)
font = get_font(
fname,
hinting_factor=rcParams['text.hinting_factor'])
font = get_font(fname)

font.clear()
size = prop.get_size_in_points()
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,12 @@ def is_opentype_cff_font(filename):
_fmcache = None


get_font = lru_cache(64)(ft2font.FT2Font)
_get_font = lru_cache(64)(ft2font.FT2Font)

def get_font(filename, hinting_factor=None):
if hinting_factor is None:
hinting_factor = rcParams['text.hinting_factor']
return _get_font(filename, hinting_factor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not it equals to?

@lru_cache(64)
def get_font(filename, hinting_factor=None):
    if hinting_factor is None:
        hinting_factor = rcParams['text.hinting_factor']
    return ft2font.FT2Font(filename, hinting_factor)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would cache hinting_factor=None with whatever the value of the rcParam was at first call.



# The experimental fontconfig-based backend.
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tempfile
import warnings

import numpy as np
import pytest

from matplotlib.font_manager import (
Expand Down Expand Up @@ -86,3 +87,22 @@ def test_otf():
@pytest.mark.skipif(not has_fclist, reason='no fontconfig installed')
def test_get_fontconfig_fonts():
assert len(get_fontconfig_fonts()) > 1


@pytest.mark.parametrize('factor', [2, 4, 6, 8])
def test_hinting_factor(factor):
font = findfont(FontProperties(family=["sans-serif"]))

font1 = get_font(font, hinting_factor=1)
font1.clear()
font1.set_size(12, 100)
font1.set_text('abc')
expected = font1.get_width_height()

hinted_font = get_font(font, hinting_factor=factor)
hinted_font.clear()
hinted_font.set_size(12, 100)
hinted_font.set_text('abc')
# Check that hinting only changes text layout by a small (10%) amount.
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
rtol=0.1)
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
unicode_literals)

import six

import io
import warnings

import numpy as np
Expand Down Expand Up @@ -448,3 +450,17 @@ def test_nonfinite_pos():
ax.text(0, np.nan, 'nan')
ax.text(np.inf, 0, 'inf')
fig.canvas.draw()


def test_hinting_factor_backends():
plt.rcParams['text.hinting_factor'] = 1
fig = plt.figure()
t = fig.text(0.5, 0.5, 'some text')

fig.savefig(io.BytesIO(), format='svg')
expected = t.get_window_extent().intervalx

fig.savefig(io.BytesIO(), format='png')
# Backends should apply hinting_factor consistently (within 10%).
np.testing.assert_allclose(t.get_window_extent().intervalx, expected,
rtol=0.1)
4 changes: 2 additions & 2 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ FT2Font::FT2Font(FT_Open_Args &open_args, long hinting_factor_) : image(), face(
face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM;
}

static FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
FT_Set_Transform(face, &transform, 0);
}

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

if (error) {
Expand Down