Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit699f3c5

Browse files
committed
pgf: Support any font in a collection
Note, this only has an effect if set as the global font. Otherwise, justthe font name is recorded, and the TeX engine's normal lookup isperformed.
1 parentab11cef commit699f3c5

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

‎lib/matplotlib/backends/backend_pgf.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@
3838

3939
def_get_preamble():
4040
"""Prepare a LaTeX preamble based on the rcParams configuration."""
41-
font_size_pt=FontProperties(
42-
size=mpl.rcParams["font.size"]
43-
).get_size_in_points()
41+
def_to_fontspec():
42+
forcommand,familyin [("setmainfont","serif"),
43+
("setsansfont","sans\\-serif"),
44+
("setmonofont","monospace")]:
45+
font_path=fm.findfont(family)
46+
path=pathlib.Path(font_path)
47+
yieldr" \%s{%s}[Path=\detokenize{%s/}%s]"% (
48+
command,path.name,path.parent.as_posix(),
49+
f',FontIndex={font_path.face_index:d}'ifpath.suffix=='.ttc'else'')
50+
51+
font_size_pt=FontProperties(size=mpl.rcParams["font.size"]).get_size_in_points()
4452
return"\n".join([
4553
# Remove Matplotlib's custom command \mathdefault. (Not using
4654
# \mathnormal instead since this looks odd with Computer Modern.)
@@ -63,15 +71,8 @@ def _get_preamble():
6371
*([
6472
r"\ifdefined\pdftexversion\else % non-pdftex case.",
6573
r" \usepackage{fontspec}",
66-
]+ [
67-
r" \%s{%s}[Path=\detokenize{%s/}]"
68-
% (command,path.name,path.parent.as_posix())
69-
forcommand,pathinzip(
70-
["setmainfont","setsansfont","setmonofont"],
71-
[pathlib.Path(fm.findfont(family))
72-
forfamilyin ["serif","sans\\-serif","monospace"]]
73-
)
74-
]+ [r"\fi"]ifmpl.rcParams["pgf.rcfonts"]else []),
74+
*_to_fontspec(),
75+
r"\fi"]ifmpl.rcParams["pgf.rcfonts"]else []),
7576
# Documented as "must come last".
7677
mpl.texmanager._usepackage_if_not_loaded("underscore",option="strings"),
7778
])

‎lib/matplotlib/tests/test_backend_pgf.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
importdatetime
22
fromioimportBytesIO
33
importos
4+
frompathlibimportPath
45
importshutil
6+
importstring
57

68
importnumpyasnp
79
frompackaging.versionimportparseasparse_version
810
importpytest
911

1012
importmatplotlibasmpl
1113
importmatplotlib.pyplotasplt
14+
frommatplotlib.font_managerimportFontProperties,findfont
1215
frommatplotlib.testingimport_has_tex_package,_check_for_pgf
1316
frommatplotlib.testing.exceptionsimportImageComparisonFailure
1417
frommatplotlib.testing.compareimportcompare_images
@@ -330,6 +333,23 @@ def test_png_transparency(): # Actually, also just testing that png works.
330333
assert (t[...,3]==0).all()# fully transparent.
331334

332335

336+
@needs_pgf_xelatex
337+
@pytest.mark.backend('pgf')
338+
@image_comparison(['ttc_pgf.pdf'],style='mpl20')
339+
deftest_ttc_output():
340+
fp=FontProperties(family=['WenQuanYi Zen Hei'])
341+
ifPath(findfont(fp)).name!='wqy-zenhei.ttc':
342+
pytest.skip('Font wqy-zenhei.ttc may be missing')
343+
344+
fonts= {'sans-serif':'WenQuanYi Zen Hei','monospace':'WenQuanYi Zen Hei Mono'}
345+
plt.rc('font',size=16,**fonts)
346+
347+
figs=plt.figure(figsize=(7,len(fonts)/2)).subfigures(len(fonts))
348+
forfont,figinzip(fonts.values(),figs):
349+
fig.text(0.5,0.5,f'{font}:{string.ascii_uppercase}',font=font,
350+
horizontalalignment='center',verticalalignment='center')
351+
352+
333353
@needs_pgf_xelatex
334354
deftest_unknown_font(caplog):
335355
withcaplog.at_level("WARNING"):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp