-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement Font-Fallback in Matplotlib #20740
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
********************** | ||
``matplotlib.ft2font`` | ||
********************** | ||
|
||
.. automodule:: matplotlib.ft2font | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Font Fallback in Agg | ||
-------------------- | ||
|
||
It is now possible to specify a list of fonts families and the Agg renderer | ||
will try them in order to locate a required glyph. | ||
|
||
.. plot:: | ||
:caption: Demonstration of mixed English and Chinese text with font fallback. | ||
:alt: The phrase "There are 几个汉字 in between!" rendered in various fonts. | ||
:include-source: True | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
text = "There are 几个汉字 in between!" | ||
|
||
plt.rcParams["font.size"] = 20 | ||
fig = plt.figure(figsize=(4.75, 1.85)) | ||
fig.text(0.05, 0.85, text, family=["WenQuanYi Zen Hei"]) | ||
fig.text(0.05, 0.65, text, family=["Noto Sans CJK JP"]) | ||
fig.text(0.05, 0.45, text, family=["DejaVu Sans", "Noto Sans CJK JP"]) | ||
fig.text(0.05, 0.25, text, family=["DejaVu Sans", "WenQuanYi Zen Hei"]) | ||
|
||
plt.show() | ||
|
||
|
||
This currently only works with the Agg backend, but support for the vector | ||
backends is planned for Matplotlib 3.7. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from pathlib import Path | ||
import io | ||
|
||
import pytest | ||
|
||
from matplotlib import ft2font | ||
from matplotlib.testing.decorators import check_figures_equal | ||
import matplotlib.font_manager as fm | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def test_fallback_errors(): | ||
file_name = fm.findfont('DejaVu Sans') | ||
|
||
with pytest.raises(TypeError, match="Fallback list must be a list"): | ||
# failing to be a list will fail before the 0 | ||
ft2font.FT2Font(file_name, _fallback_list=(0,)) | ||
|
||
with pytest.raises( | ||
TypeError, match="Fallback fonts must be FT2Font objects." | ||
): | ||
ft2font.FT2Font(file_name, _fallback_list=[0]) | ||
|
||
|
||
def test_ft2font_positive_hinting_factor(): | ||
file_name = fm.findfont('DejaVu Sans') | ||
with pytest.raises( | ||
ValueError, match="hinting_factor must be greater than 0" | ||
): | ||
ft2font.FT2Font(file_name, 0) | ||
|
||
|
||
def test_fallback_smoke(): | ||
fp = fm.FontProperties(family=["WenQuanYi Zen Hei"]) | ||
if Path(fm.findfont(fp)).name != "wqy-zenhei.ttc": | ||
pytest.skip("Font wqy-zenhei.ttc may be missing") | ||
|
||
fp = fm.FontProperties(family=["Noto Sans CJK JP"]) | ||
if Path(fm.findfont(fp)).name != "NotoSansCJK-Regular.ttc": | ||
pytest.skip("Noto Sans CJK JP font may be missing.") | ||
|
||
plt.rcParams['font.size'] = 20 | ||
fig = plt.figure(figsize=(4.75, 1.85)) | ||
fig.text(0.05, 0.45, "There are 几个汉字 in between!", | ||
family=['DejaVu Sans', "Noto Sans CJK JP"]) | ||
fig.text(0.05, 0.25, "There are 几个汉字 in between!", | ||
family=['DejaVu Sans', "WenQuanYi Zen Hei"]) | ||
fig.text(0.05, 0.65, "There are 几个汉字 in between!", | ||
family=["Noto Sans CJK JP"]) | ||
fig.text(0.05, 0.85, "There are 几个汉字 in between!", | ||
family=["WenQuanYi Zen Hei"]) | ||
|
||
# TODO enable fallback for other backends! | ||
for fmt in ['png', 'raw']: # ["svg", "pdf", "ps"]: | ||
fig.savefig(io.BytesIO(), format=fmt) | ||
|
||
|
||
@pytest.mark.parametrize('family_name, file_name', | ||
[("WenQuanYi Zen Hei", "wqy-zenhei.ttc"), | ||
("Noto Sans CJK JP", "NotoSansCJK-Regular.ttc")] | ||
) | ||
@check_figures_equal(extensions=["png"]) | ||
def test_font_fallback_chinese(fig_test, fig_ref, family_name, file_name): | ||
fp = fm.FontProperties(family=[family_name]) | ||
if Path(fm.findfont(fp)).name != file_name: | ||
pytest.skip(f"Font {family_name} ({file_name}) is missing") | ||
|
||
text = ["There are", "几个汉字", "in between!"] | ||
|
||
plt.rcParams["font.size"] = 20 | ||
test_fonts = [["DejaVu Sans", family_name]] * 3 | ||
ref_fonts = [["DejaVu Sans"], [family_name], ["DejaVu Sans"]] | ||
|
||
for j, (txt, test_font, ref_font) in enumerate( | ||
zip(text, test_fonts, ref_fonts) | ||
): | ||
fig_ref.text(0.05, .85 - 0.15*j, txt, family=ref_font) | ||
fig_test.text(0.05, .85 - 0.15*j, txt, family=test_font) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.