Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 224bdb1

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.
1 parent c508c35 commit 224bdb1

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ addons:
3535
- graphviz
3636
- libgeos-dev
3737
- otf-freefont
38+
- ttf-wqy-zenhei
3839

3940
env:
4041
global:

lib/matplotlib/backends/backend_pdf.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,13 @@ def get_char_width(charcode):
984984

985985
# Make the charprocs array (using ttconv to generate the
986986
# actual outlines)
987-
rawcharprocs = ttconv.get_pdf_charprocs(
988-
filename.encode(sys.getfilesystemencoding()), glyph_ids)
987+
try:
988+
rawcharprocs = ttconv.get_pdf_charprocs(
989+
filename.encode(sys.getfilesystemencoding()), glyph_ids)
990+
except RuntimeError:
991+
_log.warning("The PDF backend does not currently support the "
992+
"selected font.")
993+
raise
989994
charprocs = {}
990995
for charname in sorted(rawcharprocs):
991996
stream = rawcharprocs[charname]

lib/matplotlib/backends/backend_ps.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,16 @@ def print_figure_impl(fh):
11111111
raise RuntimeError(msg)
11121112
else:
11131113
fh.flush()
1114-
convert_ttf_to_ps(
1115-
font_filename.encode(
1116-
sys.getfilesystemencoding()),
1117-
fh, fonttype, glyph_ids)
1114+
try:
1115+
convert_ttf_to_ps(
1116+
font_filename.encode(
1117+
sys.getfilesystemencoding()),
1118+
fh, fonttype, glyph_ids)
1119+
except RuntimeError:
1120+
_log.warning("The PostScript backend does not "
1121+
"currently support the selected "
1122+
"font.")
1123+
raise
11181124
print("end", file=fh)
11191125
print("%%EndProlog", file=fh)
11201126

lib/matplotlib/font_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ def get_fontext_synonyms(fontext):
164164
Return a list of file extensions extensions that are synonyms for
165165
the given file extension *fileext*.
166166
"""
167-
return {'ttf': ('ttf', 'otf'),
168-
'otf': ('ttf', 'otf'),
169-
'afm': ('afm',)}[fontext]
167+
return {'ttf': ['ttf', 'ttc', 'otf'],
168+
'otf': ['ttf', 'otf'],
169+
'afm': ['afm']}[fontext]
170170

171171

172172
def list_fonts(directory, extensions):

lib/matplotlib/tests/test_font_manager.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from matplotlib.font_manager import (
1414
findfont, FontProperties, fontManager, json_dump, json_load, get_font,
1515
get_fontconfig_fonts, is_opentype_cff_font, fontManager as fm)
16-
from matplotlib import rc_context
16+
from matplotlib import pyplot as plt, rc_context
1717

1818
if six.PY2:
1919
from distutils.spawn import find_executable
@@ -106,3 +106,19 @@ def test_hinting_factor(factor):
106106
# Check that hinting only changes text layout by a small (10%) amount.
107107
np.testing.assert_allclose(hinted_font.get_width_height(), expected,
108108
rtol=0.1)
109+
110+
111+
@pytest.mark.xfail(not os.environ.get("TRAVIS"), reason="Font may be missing.")
112+
def test_find_ttc():
113+
fp = FontProperties(family=["WenQuanYi Zen Hei"])
114+
font = findfont(fp)
115+
assert os.path.basename(font) == "wqy-zenhei.ttc"
116+
117+
fig, ax = plt.subplots()
118+
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
119+
fig.savefig(six.BytesIO(), format="raw")
120+
fig.savefig(six.BytesIO(), format="svg")
121+
with pytest.raises(RuntimeError):
122+
fig.savefig(six.BytesIO(), format="pdf")
123+
with pytest.raises(RuntimeError):
124+
fig.savefig(six.BytesIO(), format="ps")

0 commit comments

Comments
 (0)