Skip to content

Fix positioning of mathtext glyphs#15339

Closed
anntzer wants to merge 1 commit into
matplotlib:mainfrom
anntzer:mathtext-glyphs
Closed

Fix positioning of mathtext glyphs#15339
anntzer wants to merge 1 commit into
matplotlib:mainfrom
anntzer:mathtext-glyphs

Conversation

@anntzer

@anntzer anntzer commented Sep 25, 2019

Copy link
Copy Markdown
Contributor

PR Summary

Fixes #15313 by porting mplcairo's mathtext backend to matplotlib. Also breaks all mathtext image comparison tests due to subpixel positioning issues :( Some of them should be fixed on this PR, but others are issues with the old mathtext code.

Will do some more detailed writeup at some later time...

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • 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

@QuLogic

QuLogic commented Sep 25, 2019

Copy link
Copy Markdown
Member

So would this fix the weird uneven baseline that seems to happen in some (all?) mathtext test images?

@anntzer

anntzer commented Sep 25, 2019

Copy link
Copy Markdown
Contributor Author

Not right now, but I have a plan...

Basically the origin of the uneven baselines, I think, is that draw_glyph_to_bitmap specifies the top right corner of the glyph, but that can be a fractional value (FreeType gives glyph sizes in (1/64)px) and then there is some roundoff when actually copying the glyph outline to the FT2Image which leads to the baseline being unaligned.

I think best would be to fix the mathtext logic to position glyphs by baseline/right position and adjust draw_glyph_to_bitmap accordingly; alternatively we can "reverse-engineer" the values that need to be sent to draw_glyph_to_bitmap to fix that roundoff.

for x1, y1, x2, y2 in self._rects:
x1 += dxmin
x2 += dymin
y1 += dxmin

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this correct?

@jklymak

jklymak commented Sep 25, 2019

Copy link
Copy Markdown
Member

I was just digging through this code in an attempt to understand #15313. If you are thinking about it all and understand it, can you add a few comments for posterity?

@anntzer

anntzer commented Sep 25, 2019

Copy link
Copy Markdown
Contributor Author

Yes, I need to write up something...

@anntzer

anntzer commented Nov 5, 2020

Copy link
Copy Markdown
Contributor Author

This also mostly fixes #18899, but the example at #18899 actually shows that the glyphs are also cut in the horizontal direction:
hdpi
The bottom left corner of the x and the right edges of the digits (using mathtext) are clearly cut with this PR :/

I think another problem is that draw_text_to_bitmap doesn't support subpixel positioning, there seems to be some story related to sub_offset at the C level and old commits from @mdboom mentioning links with the wiggly baseline problem...

@github-actions

Copy link
Copy Markdown

Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it.

@github-actions github-actions Bot added the status: inactive Marked by the “Stale” Github Action label Jun 26, 2023
@anntzer

anntzer commented Jun 26, 2023

Copy link
Copy Markdown
Contributor Author

Should likely be kept and put in in some form once the baseline image machinery is revamped.

@anntzer anntzer added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Jun 26, 2023
@anntzer anntzer mentioned this pull request Aug 7, 2023
5 tasks
@anntzer anntzer mentioned this pull request Apr 27, 2025
5 tasks
@QuLogic

QuLogic commented Mar 11, 2026

Copy link
Copy Markdown
Member

Is this still relevant after all the refactors and changes in the text-overhaul branch?

@anntzer

anntzer commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

Indeed, this has been fixed by the direct-rendering mechanism.

@anntzer anntzer closed this Mar 11, 2026
@QuLogic QuLogic added status: superseded and removed keep Items to be ignored by the “Stale” Github Action labels Apr 11, 2026
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.

star (*) symbol in text box cuts off bottom of text when saved

3 participants