Skip to content

Commit b3cadbe

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 2698288 commit b3cadbe

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:
@@ -111,7 +112,7 @@ before_install: |
111112
ci/silence brew update
112113
brew uninstall numpy gdal postgis
113114
brew upgrade python
114-
brew install ffmpeg imagemagick mplayer ccache
115+
brew install ffmpeg imagemagick mplayer ccache font-wenquanyi-zen-hei
115116
hash -r
116117
which python
117118
python --version

lib/matplotlib/backends/backend_pdf.py

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

974974
# Make the charprocs array (using ttconv to generate the
975975
# actual outlines)
976-
rawcharprocs = ttconv.get_pdf_charprocs(
977-
os.fsencode(filename), glyph_ids)
976+
try:
977+
rawcharprocs = ttconv.get_pdf_charprocs(
978+
os.fsencode(filename), glyph_ids)
979+
except RuntimeError:
980+
_log.warning("The PDF backend does not currently support the "
981+
"selected font.")
982+
raise
978983
charprocs = {}
979984
for charname in sorted(rawcharprocs):
980985
stream = rawcharprocs[charname]

lib/matplotlib/backends/backend_ps.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,14 @@ def print_figure_impl(fh):
11201120
"time; consider using the Cairo backend")
11211121
else:
11221122
fh.flush()
1123-
convert_ttf_to_ps(os.fsencode(font_filename),
1124-
fh, fonttype, glyph_ids)
1123+
try:
1124+
convert_ttf_to_ps(os.fsencode(font_filename),
1125+
fh, fonttype, glyph_ids)
1126+
except RuntimeError:
1127+
_log.warning("The PostScript backend does not "
1128+
"currently support the selected "
1129+
"font.")
1130+
raise
11251131
print("end", file=fh)
11261132
print("%%EndProlog", file=fh)
11271133

lib/matplotlib/font_manager.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ def get_fontext_synonyms(fontext):
129129
Return a list of file extensions extensions that are synonyms for
130130
the given file extension *fileext*.
131131
"""
132-
return {'ttf': ('ttf', 'otf'),
133-
'otf': ('ttf', 'otf'),
134-
'afm': ('afm',)}[fontext]
132+
return {
133+
'afm': ['afm'],
134+
'otf': ['otf', 'ttc', 'ttf'],
135+
'ttc': ['otf', 'ttc', 'ttf'],
136+
'ttf': ['otf', 'ttc', 'ttf'],
137+
}[fontext]
135138

136139

137140
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)