Skip to content

Support (first font of) TTC files. #9787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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).
  • Loading branch information
anntzer committed Jan 2, 2019
commit ae618f34f367cf5cd3cfba004271071eb2b6cae9
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ addons:
- texlive-latex-recommended
- texlive-xetex
- texlive-luatex
- ttf-wqy-zenhei

env:
global:
Expand Down Expand Up @@ -110,7 +111,7 @@ before_install: |
ci/silence brew update
brew uninstall numpy gdal postgis
brew upgrade python
brew install ffmpeg imagemagick mplayer ccache
brew install ffmpeg imagemagick mplayer ccache font-wenquanyi-zen-hei
hash -r
which python
python --version
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,13 @@ def get_char_width(charcode):

# Make the charprocs array (using ttconv to generate the
# actual outlines)
rawcharprocs = ttconv.get_pdf_charprocs(
os.fsencode(filename), glyph_ids)
try:
rawcharprocs = ttconv.get_pdf_charprocs(
os.fsencode(filename), glyph_ids)
except RuntimeError:
_log.warning("The PDF backend does not currently support the "
"selected font.")
raise
charprocs = {}
for charname in sorted(rawcharprocs):
stream = rawcharprocs[charname]
Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,14 @@ def print_figure_impl(fh):
"time; consider using the Cairo backend")
else:
fh.flush()
convert_ttf_to_ps(os.fsencode(font_filename),
fh, fonttype, glyph_ids)
try:
convert_ttf_to_ps(os.fsencode(font_filename),
fh, fonttype, glyph_ids)
except RuntimeError:
_log.warning("The PostScript backend does not "
"currently support the selected "
"font.")
raise
print("end", file=fh)
print("%%EndProlog", file=fh)

Expand Down
9 changes: 6 additions & 3 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ def get_fontext_synonyms(fontext):
Return a list of file extensions extensions that are synonyms for
the given file extension *fileext*.
"""
return {'ttf': ('ttf', 'otf'),
'otf': ('ttf', 'otf'),
'afm': ('afm',)}[fontext]
return {
'afm': ['afm'],
'otf': ['otf', 'ttc', 'ttf'],
'ttc': ['otf', 'ttc', 'ttf'],
'ttf': ['otf', 'ttc', 'ttf'],
}[fontext]


def list_fonts(directory, extensions):
Expand Down
23 changes: 21 additions & 2 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from io import BytesIO
import os
from pathlib import Path
import shutil
import sys
Expand All @@ -9,7 +11,7 @@
from matplotlib.font_manager import (
findfont, FontProperties, fontManager, json_dump, json_load, get_font,
get_fontconfig_fonts, is_opentype_cff_font)
from matplotlib import rc_context
from matplotlib import pyplot as plt, rc_context

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

Expand Down Expand Up @@ -91,7 +93,7 @@ def test_hinting_factor(factor):


@pytest.mark.skipif(sys.platform != "win32",
reason="Need Windows font to test against")
reason="Need Windows font to test against")
def test_utf16m_sfnt():
segoe_ui_semibold = None
for f in fontManager.ttflist:
Expand All @@ -105,3 +107,20 @@ def test_utf16m_sfnt():
# Check that we successfully read the "semibold" from the font's
# sfnt table and set its weight accordingly
assert segoe_ui_semibold.weight == "semibold"


@pytest.mark.xfail(not (os.environ.get("TRAVIS") and sys.platform == "linux"),
reason="Font may be missing.")
def test_find_ttc():
fp = FontProperties(family=["WenQuanYi Zen Hei"])
font = findfont(fp)
assert Path(font).name == "wqy-zenhei.ttc"

fig, ax = plt.subplots()
ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp)
fig.savefig(BytesIO(), format="raw")
fig.savefig(BytesIO(), format="svg")
with pytest.raises(RuntimeError):
fig.savefig(BytesIO(), format="pdf")
with pytest.raises(RuntimeError):
fig.savefig(BytesIO(), format="ps")