FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Log a warning if selected font weight differs from requested by QuLogic · Pull Request #30272 · matplotlib/matplotlib · GitHub

Repository navigation

Log a warning if selected font weight differs from requested - #30272

Merged
dstansby merged 1 commit into
matplotlib:mainfrom
QuLogic:font-weight-warning
Jul 15, 2025
Merged

dstansby merged 1 commit into
matplotlib:mainfrom
QuLogic:font-weight-warning

Conversation

QuLogic commented Jul 8, 2025

Copy link
Copy Markdown
Member

PR summary

This is a rebase of the orphaned #15615, but with @anntzer's comments handled.

PR checklist

QuLogic force-pushed the font-weight-warning branch from 96be1df to 8d7f02e Compare July 8, 2025 02:08
Comment thread lib/matplotlib/font_manager.pyi Outdated
X11FontDirectories: list[str]
OSXFontDirectories: list[str]

def _normalize_weight(weight: str | Number) -> Number: ...

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Typical font weights are multiples of 100. Can / should this be any Number or can we specialize to int?

Suggested change
def _normalize_weight(weight: str | Number) -> Number: ...
def _normalize_weight(weight: str | int) -> int: ...

If we want a general Number, there should be a test case for it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looking at the OpenType spec, it does appear to be integral only: https://learn.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Also, I think the original PR just used Number because it was already imported in font_manager.py.

Comment on lines +413 to +415
def test_map_weights():
assert _normalize_weight('ultralight') == 100

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's reframe this to test the helper instead of the mapping, so that we can also test the passthrough behavior

Suggested change
def test_map_weights():
assert _normalize_weight('ultralight') == 100
def test_normalize_weight():
assert _normalize_weight(300) == 300 # passthough
assert _normalize_weight('ultralight') == 100

Maybe also add an error case e.g. "italic"

Comment thread lib/matplotlib/font_manager.py Outdated
Comment on lines +1487 to +1489
if best_font is not None:
if (_normalize_weight(prop.get_weight()) !=
_normalize_weight(best_font.weight)):

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why are these nested ifs instead of if A and B ?

QuLogic force-pushed the font-weight-warning branch from 8d7f02e to 86983e4 Compare July 10, 2025 00:02
QuLogic added this to the v3.11.0 milestone Jul 10, 2025

dstansby left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

One minor suggestion to improve the changelog - take it or leave it, and feel free to self merge.

Comment on lines +1 to +2
``font_manager.findfont`` logs if selected font weight does not match requested
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
``font_manager.findfont`` logs if selected font weight does not match requested
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``font_manager.findfont`` warns if selected the font weight does not match requested
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

No, it's not a warning.

def test_font_match_warning(caplog):
findfont(FontProperties(family=["DejaVu Sans"], weight=750))
logs = [rec.message for rec in caplog.records]
assert 'findfont: Failed to find font weight 750, now using 700.' in logs

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Just for my own interest, is there a reason to check the logs, instead of using a pytest.warns context?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Same reason as above.

dstansby merged commit d2050cb into matplotlib:main Jul 15, 2025
QuLogic deleted the font-weight-warning branch July 15, 2025 10:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Chinese font can`t change the weight

3 participants


Back | FazBrowse Home | New Git URL