Skip to content

Commit d653bc1

Browse files
committed
Support (first font of) TTC files.
TTC is a TrueType Collection file (a collection of TTFs). Currently, ft2font only supports getting the first font from the collection, and neither the pdf nor ps backends are able to use them (due to limitations of ttconv). Still, it seems better than nothing to support them for Agg and SVG (that comes for free via FreeType).
1 parent 13629a4 commit d653bc1

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ addons:
4040
- texlive-latex-recommended
4141
- texlive-xetex
4242
- texlive-luatex
43+
- ttf-wqy-zenhei
4344

4445
env:
4546
global:
@@ -104,7 +105,7 @@ before_install: |
104105
else
105106
ci/silence brew update
106107
brew upgrade python
107-
brew install ffmpeg imagemagick mplayer ccache
108+
brew install ffmpeg imagemagick mplayer ccache font-wenquanyi-zen-hei
108109
hash -r
109110
which python
110111
python --version

lib/matplotlib/backends/backend_pdf.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,13 @@ def get_char_width(charcode):
968968

969969
# Make the charprocs array (using ttconv to generate the
970970
# actual outlines)
971-
rawcharprocs = ttconv.get_pdf_charprocs(
972-
os.fsencode(filename), glyph_ids)
971+
try:
972+
rawcharprocs = ttconv.get_pdf_charprocs(
973+
os.fsencode(filename), glyph_ids)
974+
except RuntimeError:
975+
_log.warning("The PDF backend does not currently support the "
976+
"selected font.")
977+
raise
973978
charprocs = {}
974979
for charname in sorted(rawcharprocs):
975980
stream = rawcharprocs[charname]

lib/matplotlib/backends/backend_ps.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,14 @@ def print_figure_impl(fh):
11411141
"time; consider using the Cairo backend")
11421142
else:
11431143
fh.flush()
1144-
convert_ttf_to_ps(os.fsencode(font_filename),
1145-
fh, fonttype, glyph_ids)
1144+
try:
1145+
convert_ttf_to_ps(os.fsencode(font_filename),
1146+
fh, fonttype, glyph_ids)
1147+
except RuntimeError:
1148+
_log.warning("The PostScript backend does not "
1149+
"currently support the selected "
1150+
"font.")
1151+
raise
11461152
print("end", file=fh)
11471153
print("%%EndProlog", file=fh)
11481154

lib/matplotlib/font_manager.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,12 @@ def get_fontext_synonyms(fontext):
141141
Return a list of file extensions extensions that are synonyms for
142142
the given file extension *fileext*.
143143
"""
144-
return {'ttf': ('ttf', 'otf'),
145-
'otf': ('ttf', 'otf'),
146-
'afm': ('afm',)}[fontext]
144+
return {
145+
'afm': ['afm'],
146+
'otf': ['otf', 'ttc', 'ttf'],
147+
'ttc': ['otf', 'ttc', 'ttf'],
148+
'ttf': ['otf', 'ttc', 'ttf'],
149+
}[fontext]
147150

148151

149152
def list_fonts(directory, extensions):

lib/matplotlib/tests/test_font_manager.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import BytesIO
12
import os
23
import shutil
34
import tempfile
@@ -9,7 +10,7 @@
910
from matplotlib.font_manager import (
1011
findfont, FontProperties, fontManager, json_dump, json_load, get_font,
1112
get_fontconfig_fonts, is_opentype_cff_font)
12-
from matplotlib import rc_context
13+
from matplotlib import pyplot as plt, rc_context
1314

1415
has_fclist = shutil.which('fc-list') is not None
1516

@@ -96,3 +97,19 @@ def test_hinting_factor(factor):
9697
# Check that hinting only changes text layout by a small (10%) amount.
9798
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
9899
rtol=0.1)
100+
101+
102+
@pytest.mark.xfail(not os.environ.get("TRAVIS"), reason="Font may be missing.")
103+
def test_find_ttc():
104+
fp = FontProperties(family=["WenQuanYi Zen Hei"])
105+
font = findfont(fp)
106+
assert os.path.basename(font) == "wqy-zenhei.ttc"
107+
108+
fig, ax = plt.subplots()
109+
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
110+
fig.savefig(BytesIO(), format="raw")
111+
fig.savefig(BytesIO(), format="svg")
112+
with pytest.raises(RuntimeError):
113+
fig.savefig(BytesIO(), format="pdf")
114+
with pytest.raises(RuntimeError):
115+
fig.savefig(BytesIO(), format="ps")

0 commit comments

Comments
 (0)