| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ********************** | ||
| ``matplotlib.ft2font`` | ||
| ********************** | ||
|
|
||
| .. automodule:: matplotlib.ft2font | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: |
| 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. |
| 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) |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.