Skip to content

Commit 77c0e34

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 2ac048b commit 77c0e34

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ addons:
4040
- texlive-latex-recommended
4141
- texlive-xetex
4242
- texlive-luatex
43+
- graphviz
44+
- libgeos-dev
45+
- otf-freefont
46+
- ttf-wqy-zenhei
4347

4448
env:
4549
global:

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

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

149149

150150
def list_fonts(directory, extensions):

lib/matplotlib/tests/test_font_manager.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from matplotlib.font_manager import (
1010
findfont, FontProperties, fontManager, json_dump, json_load, get_font,
1111
get_fontconfig_fonts, is_opentype_cff_font)
12-
from matplotlib import rc_context
12+
from matplotlib import pyplot as plt, rc_context
1313

1414
has_fclist = shutil.which('fc-list') is not None
1515

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

0 commit comments

Comments
 (0)