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

Skip to content

Commit cc5c188

Browse files
authored
Merge pull request #9787 from anntzer/ttc
Support (first font of) TTC files.
2 parents 2bb68f6 + d3e6a91 commit cc5c188

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ addons:
4343
- texlive-latex-recommended
4444
- texlive-xetex
4545
- texlive-luatex
46+
- ttf-wqy-zenhei
4647

4748
env:
4849
global:
@@ -85,8 +86,12 @@ matrix:
8586
- EXTRAREQS='-r requirements/testing/travis36.txt'
8687
- python: 3.7
8788
sudo: true
89+
env:
90+
- DELETE_FONT_CACHE=1
8891
- python: "nightly"
89-
env: PRE=--pre
92+
env:
93+
- PRE=--pre
94+
- DELETE_FONT_CACHE=1
9095
- os: osx
9196
language: generic # https://github.com/travis-ci/travis-ci/issues/2312
9297
only: master
@@ -110,7 +115,7 @@ before_install: |
110115
ci/silence brew update
111116
brew uninstall numpy gdal postgis
112117
brew upgrade python
113-
brew install ffmpeg imagemagick mplayer ccache
118+
brew install ffmpeg imagemagick mplayer ccache font-wenquanyi-zen-hei
114119
hash -r
115120
which python
116121
python --version

lib/matplotlib/backends/backend_pdf.py

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

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

lib/matplotlib/backends/backend_ps.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,14 @@ def print_figure_impl(fh):
11171117
"time; consider using the Cairo backend")
11181118
else:
11191119
fh.flush()
1120-
convert_ttf_to_ps(os.fsencode(font_filename),
1121-
fh, fonttype, glyph_ids)
1120+
try:
1121+
convert_ttf_to_ps(os.fsencode(font_filename),
1122+
fh, fonttype, glyph_ids)
1123+
except RuntimeError:
1124+
_log.warning("The PostScript backend does not "
1125+
"currently support the selected "
1126+
"font.")
1127+
raise
11221128
print("end", file=fh)
11231129
print("%%EndProlog", file=fh)
11241130

lib/matplotlib/font_manager.py

Lines changed: 6 additions & 3 deletions
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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from io import BytesIO
2+
import os
13
from pathlib import Path
24
import shutil
35
import sys
@@ -9,7 +11,7 @@
911
from matplotlib.font_manager import (
1012
findfont, FontProperties, fontManager, json_dump, json_load, get_font,
1113
get_fontconfig_fonts, is_opentype_cff_font)
12-
from matplotlib import rc_context
14+
from matplotlib import pyplot as plt, rc_context
1315

1416
has_fclist = shutil.which('fc-list') is not None
1517

@@ -91,7 +93,7 @@ def test_hinting_factor(factor):
9193

9294

9395
@pytest.mark.skipif(sys.platform != "win32",
94-
reason="Need Windows font to test against")
96+
reason="Need Windows font to test against")
9597
def test_utf16m_sfnt():
9698
segoe_ui_semibold = None
9799
for f in fontManager.ttflist:
@@ -105,3 +107,20 @@ def test_utf16m_sfnt():
105107
# Check that we successfully read the "semibold" from the font's
106108
# sfnt table and set its weight accordingly
107109
assert segoe_ui_semibold.weight == "semibold"
110+
111+
112+
@pytest.mark.xfail(not (os.environ.get("TRAVIS") and sys.platform == "linux"),
113+
reason="Font may be missing.")
114+
def test_find_ttc():
115+
fp = FontProperties(family=["WenQuanYi Zen Hei"])
116+
font = findfont(fp)
117+
assert Path(font).name == "wqy-zenhei.ttc"
118+
119+
fig, ax = plt.subplots()
120+
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
121+
fig.savefig(BytesIO(), format="raw")
122+
fig.savefig(BytesIO(), format="svg")
123+
with pytest.raises(RuntimeError):
124+
fig.savefig(BytesIO(), format="pdf")
125+
with pytest.raises(RuntimeError):
126+
fig.savefig(BytesIO(), format="ps")

0 commit comments

Comments
 (0)