Skip to content

Fix rendering bug for small Chinese characters by adding FT_LOAD_NO_B… - #32072

Closed
mohit-sharma-001 wants to merge 3 commits into
matplotlib:mainfrom
mohit-sharma-001:main
Closed

mohit-sharma-001 wants to merge 3 commits into
matplotlib:mainfrom
mohit-sharma-001:main

Conversation

@mohit-sharma-001

@mohit-sharma-001 mohit-sharma-001 commented Jul 19, 2026 •

Copy link
Copy Markdown

PR summary

  • Why is this change necessary? / What problem does it solve?
    Fixes a rendering issue where small Chinese characters were loaded as pixelated embedded bitmaps instead of smooth vector outlines.
  • What is the reasoning for this implementation?
    Adding the FT_LOAD_NO_BITMAP flag to FT_Load_Glyph forces the FreeType engine to ignore embedded bitmaps and use vector outlines, ensuring high-quality rendering at small font sizes.
  • Closes [Bug]: Font fallback bugs #32063

AI Disclosure

Yes, generative AI was used to assist in troubleshooting the FreeType flag and setting up the local build environment.

PR checklist

  • "closes [Bug]: Font fallback bugs #32063" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • Documentation complies with general and docstring guidelines

@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@scottshambaugh

scottshambaugh commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the PR. Please add a test, change the language of your comment to English, and update your PR description to use our PR template.

@mohit-sharma-001

Copy link
Copy Markdown
Author

Thanks for the PR. Please add a test, change the language of your comment to English, and update your PR description to use our PR template.

@scottshambaugh
I have updated the PR description using the template, changed the code comment to English, and added a test for small character rendering. Please let me know if any further changes are needed. Thank you!

@scottshambaugh

scottshambaugh commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Please iterate on your code until the automated checks pass. There seems to be real linting (formatting) and test failures on your newly added test.

def test_small_chinese_character_rendering():
fig, ax = plt.subplots()
ax.text(0.5, 0.5, '字体', fontsize=6)
# Smoke test to ensure it renders without crashing

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.

The initial bug report rendered without error but did not show the glyphs. We may need to gain an image test here to be sure that the glyphs actually render.

@tacaswell

Copy link
Copy Markdown
Member

Hypothesis: The issue seems to be in the FreeType rendering phase. SimSun contains embedded bitmap strikes for smaller sizes (<= 12). It appears that the font fallback mechanism in Matplotlib fails when trying to render a mix of vector glyphs (Times New Roman) and embedded bitmap glyphs (SimSun at small sizes), causing the fallback text to disappear completely. When size >12, SimSun uses vector outlines, and the fallback works fine.

This seems like a reasonably theory as to what is wrong, but preventing bitmaps seems like the wrong fix, instead we should fix what ever is causing the bitmap glyphs to fail to render.

@mohit-sharma-001

Copy link
Copy Markdown
Author

Hypothesis: The issue seems to be in the FreeType rendering phase. SimSun contains embedded bitmap strikes for smaller sizes (<= 12). It appears that the font fallback mechanism in Matplotlib fails when trying to render a mix of vector glyphs (Times New Roman) and embedded bitmap glyphs (SimSun at small sizes), causing the fallback text to disappear completely. When size >12, SimSun uses vector outlines, and the fallback works fine.

This seems like a reasonably theory as to what is wrong, but preventing bitmaps seems like the wrong fix, instead we should fix what ever is causing the bitmap glyphs to fail to render.

Thank you for the insight! That makes a lot of sense regarding the embedded bitmaps in SimSun at smaller sizes. Since forcing FT_LOAD_NO_BITMAP might not be the ideal approach, could you point me in the right direction or give some hints on where the font fallback or bitmap rendering failure might be happening in the codebase? I'd be happy to look into a more proper fix and add an image test as requested.

@QuLogic

QuLogic commented Jul 24, 2026

Copy link
Copy Markdown
Member

We should definitely support bitmap glyphs, but that's probably a lot more work rather than sticking with scalable glyphs if the font supports it. So I think it's fine to avoid the bitmaps for now.

However, if we do that, we should a) reject fonts at load time that only have bitmaps and b) I don't think this is the normal rendering path now; are you sure this PR actually fixes the problem?

@mohit-sharma-001

Copy link
Copy Markdown
Author

We should definitely support bitmap glyphs, but that's probably a lot more work rather than sticking with scalable glyphs if the font supports it. So I think it's fine to avoid the bitmaps for now.

However, if we do that, we should a) reject fonts at load time that only have bitmaps and b) I don't think this is the normal rendering path now; are you sure this PR actually fixes the problem?

@QuLogic
Thanks for the feedback! I'm glad avoiding bitmaps is acceptable for now. To answer your question: yes, adding FT_LOAD_NO_BITMAP forced it to bypass the embedded bitmaps and use scalable outlines, which successfully made the characters render correctly instead of disappearing.

Regarding rejecting fonts at load time that only have bitmaps: could you guide me on where in the codebase font loading checks happen so I can implement that check?

@scottshambaugh

scottshambaugh commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

I looked into this more and found the root cause(s), and believe that blocking bitmaps is the wrong direction for the fix. See my comment here for more details: #32063 (comment)

So I'm going to close this PR as won't-do, but @mohit-sharma-001 please feel free to open another one that addresses the root causes I describe there.

@scottshambaugh

scottshambaugh commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Actually, I was overzealous on closing this! It looks like this flag is ignored for bitmap-only fonts, so it's safe to set by default. The fixes I mention in that issue should still be made, but will apply to bitmap-only fonts. So this PR will partially but not fully address that issue.

That said, there are a few other spots this flag will need to be set.

https://github.com/freetype/freetype/blob/VER-2-14-3/include/freetype/freetype.h#L3349-L3351

@mohit-sharma-001

Copy link
Copy Markdown
Author

@scottshambaugh Thanks for reopening and clarifying! That makes sense. Could you please point out or specify the other spots/files in the codebase where this flag (FT_LOAD_NO_BITMAP) needs to be set? I'd be glad to update those as well to make this fix complete.

@scottshambaugh

scottshambaugh commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

There are several places in the code that use the same exact pattern as in this PR, so that's a good starting point (but not a finishing point).

Part of the process of submitting PRs is searching and understanding the codebase, so that you yourself are confident that the changes you are making are correct and comprehensive. Please take some time to do that, and ensure that the changes are tested and passing all automated checks. We will be happy to review once that's done.

Marking for autoclose for now, will remove that tag on successful revision.

@scottshambaugh scottshambaugh added the status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks label Jul 28, 2026
@github-actions

Copy link
Copy Markdown

⏰ This pull request might be automatically closed in two weeks from now.

Thank you for your contribution to Matplotlib and for the effort you have put into this PR. This pull request does not yet meet the quality and clarity standards needed for an effective review. Project maintainers have limited time for code reviews, and our goal is to prioritize well-prepared contributions to keep Matplotlib maintainable.

Matplotlib maintainers cannot provide one-to-one guidance on this PR. However, if you ask focused, well-researched questions, a community member may be willing to help. 💬

To increase the chance of a productive review:

As the author, you are responsible for driving this PR, which entails doing necessary background research as well as presenting its context and your thought process. If you are a new contributor, or do not know how to fulfill these requirements, we recommend that you familiarize yourself with Matplotlib's development conventions or engage with the community via our Discourse or one of our meetings before submitting code.

If you substantially improve this PR within two weeks, leave a comment and a team member may remove the status: autoclose candidate label and the PR stays open. Cosmetic changes or incomplete fixes will not be sufficient. Maintainers will assess improvements on their own schedule. Please do not ping (@) maintainers.

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

Labels

first-contribution status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks topic: text/fonts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Font fallback bugs

4 participants