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

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Oct 19, 2017

This is only explicitly used in Agg, so any other users get the default in the C++ code. Thus if you modify this setting and use some other backend, you get very weird behaviour. Positioning sometimes breaks and glyphs are shrunk or cropped.

@anntzer this is why just changing a random hinting_factor produced super-weird results yesterday.

Don't know if it's worth backporting; I guess no-one ever tried modifying this setting or they would have complained. Also, trying to see if a simple test of some sort can be done.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • [N/A] New features are documented, with examples if plot related
  • [N/A] Documentation is sphinx and numpydoc compliant
  • [N/A] Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • [?] Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

This is only explicitly used in Agg, so any other users get the default
in the C++ code. Thus if you modify this setting and use some other
backend, you get very weird behaviour. Positioning sometimes breaks and
glyphs are shrunk or cropped.
anntzer
anntzer previously approved these changes Oct 19, 2017
Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

conditional on CI

@QuLogic
Copy link
Member Author

QuLogic commented Oct 19, 2017

Hmm, something might still be cached somewhere as changing the hinting factor in the middle of a script causes all text to be the wrong width.

for hinting_factor in map(int, sys.argv[1:]):
    plt.rcParams['text.hinting_factor'] = hinting_factor
    fig, ax = plt.subplots()
    ax.text(0.5, 0.5, 'text')
    plt.show()

Opens figures with progressively smaller text (or wider depending on what you specify), but it's fine if you specify anything other than 8 in a separate run.

As an example of something this PR does fix, if you add a fig.savefig('test.svg') before show (to switch backends), you get broken text for anything other than 8 on master.

@anntzer
Copy link
Contributor

anntzer commented Oct 19, 2017

@QuLogic but I guess that caching was already present before, in which case this PR does not really make things worse, does it?

@QuLogic
Copy link
Member Author

QuLogic commented Oct 19, 2017

@anntzer True, it just makes it more difficult to write a test.

@anntzer anntzer dismissed their stale review October 19, 2017 22:25

let me investigate the caching first...

Static initializers are only applied the first time, so any subsequent
calls with alternate hinting_factor would use the first value. FreeType
copies the matrix values into an internal structure, so these do not
really need to be static.
@QuLogic
Copy link
Member Author

QuLogic commented Oct 20, 2017

I think that should fix the caching issue, and it even fixes the SVG+Agg save from above, without the first change. I guess the first change can still go in for consistency.

But thinking about it, SVG and PDF render text externally, so it may be better to have those use hinting_factor=1 since that's the most likely method to be used by external renderers. Maybe can discuss that in a separate issue where we also might decide to turn it off entirely.

@anntzer
Copy link
Contributor

anntzer commented Oct 20, 2017

Ah, the static was tricky...
I would favor moving the discussion of entirely dropping hinting_factor (which I agree with) to a separate issue, not clear there's much gain of doing so in a piecemeal fashion.

@anntzer
Copy link
Contributor

anntzer commented Oct 20, 2017

test failures look real though

@QuLogic QuLogic force-pushed the fix-hinting-factor branch 3 times, most recently from b61fbd7 to df5558c Compare October 21, 2017 08:54
@QuLogic QuLogic force-pushed the fix-hinting-factor branch from df5558c to 6a685d2 Compare October 21, 2017 09:23
@QuLogic
Copy link
Member Author

QuLogic commented Oct 22, 2017

Not sure why I needed the clear and set_size as I thought the same stuff is done in the constructor, but it matches what's done in normal rendering code and it works, so...

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.

@tacaswell tacaswell added this to the v2.2 milestone Oct 29, 2017
@anntzer
Copy link
Contributor

anntzer commented Nov 4, 2017

thanks

@anntzer anntzer merged commit b2bbbfd into matplotlib:master Nov 4, 2017
@QuLogic QuLogic deleted the fix-hinting-factor branch November 4, 2017 23:26
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants