Skip to content

Text antialiasing for mathtext (reopen) #26376

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 6 commits into from
Aug 2, 2023

Conversation

stevezhang1999
Copy link
Contributor

@stevezhang1999 stevezhang1999 commented Jul 22, 2023

Accidentally closed this by force pushing.

As mentioned in the previous PR, if the text contains math text, antialiasing will use rcParams["text.antialiased"] and the parameter antialiased for text() will have no effect. Now this is fixed - antialiased works for mathtext. Note that only AGG backend support this. Using Cairo we cannot customize antialiasing for math text.

@jklymak jklymak requested review from anntzer and oscargus July 27, 2023 19:57
@ksunden ksunden added this to the v3.8.0 milestone Jul 27, 2023
Comment on lines +967 to +980
@check_figures_equal()
def test_text_math_antialiased_on_default_vs_manual(fig_test, fig_ref):
fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=True)

mpl.rcParams['text.antialiased'] = True
fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$")


@check_figures_equal()
def test_text_math_antialiased_off_default_vs_manual(fig_test, fig_ref):
fig_test.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$", antialiased=False)

mpl.rcParams['text.antialiased'] = False
fig_ref.text(0.5, 0.5, r"OutsideMath $I\'m \sqrt{2}$")
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, neither of these tests fail without this PR, so I'm not sure if they are testing what you want?

Copy link
Contributor Author

@stevezhang1999 stevezhang1999 Jul 30, 2023

Choose a reason for hiding this comment

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

Yes.
Before this: For math text, whether we are using antialiasing is resolved when rendering (_mathtext.py::Output.to_raster()). Two test figures are saved after we executing theses codes, resulting two figures resolve to the same rcParams.
After this: When creating the text, antialiasing states are set by True or False if the user specifies the keyword argument. Otherwise, the state resolves to rcParams also at the creation time. That is, we no longer query rcParams when rendering.

@stevezhang1999
Copy link
Contributor Author

flake 8 tests failing seems unrelated.

@timhoffm
Copy link
Member

flake8 issues are fixed in #26414. You can rebase onto main to get rid of them.


@functools.lru_cache(50)
def _parse_cached(self, s, dpi, prop):
def _parse_cached(self, s, dpi, prop, antialiased):
Copy link
Member

Choose a reason for hiding this comment

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

This also fixes a latent bug where we would incorrectly hit the cache if the rcparam was changed between the first and subsequent calls!

@tacaswell tacaswell merged commit 98007a5 into matplotlib:main Aug 2, 2023
@tacaswell
Copy link
Member

I squash-merged this to make the history a little cleaner.

Thank you @stevezhang1999 for following up and getting aliasing fully sorted! I hope we continue to hear from you.

@stevezhang1999
Copy link
Contributor Author

I squash-merged this to make the history a little cleaner.

Thank you @stevezhang1999 for following up and getting aliasing fully sorted! I hope we continue to hear from you.

Thanks! I'm really happy to be part of the community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants