8000 Use the --format argument to fontconfig so we don't have to parse · matplotlib/matplotlib@8e52475 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e52475

Browse files
committed
Use the --format argument to fontconfig so we don't have to parse
anything complex ourselves.
1 parent 895ed32 commit 8e52475

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

lib/matplotlib/font_manager.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,24 +268,22 @@ def get_fontconfig_fonts(fontext='ttf'):
268268

269269
fontfiles = {}
270270
try:
271-
pipe = subprocess.Popen(['fc-list', '', 'file'], stdout=subprocess.PIPE)
271+
pipe = subprocess.Popen(['fc-list', '--format=%{file}\\n'], stdout=subprocess.PIPE)
272272
output = pipe.communicate()[0]
273273
except (OSError, IOError):
274274
# Calling fc-list did not work, so we'll just return nothing
275275
return fontfiles
276276

277277
if pipe.returncode == 0:
278-
# The bulk of the output from fc-list is ascii, so we keep the
279-
# result in bytes and parse it as bytes, until we extract the
280-
# filename, which is in sys.filesystemencoding().
281-
for line in output.split(b'\n'):
282-
fname = line.split(b':')[0]
278+
# The line breaks between results are in ascii, but each entry
279+
# is in in sys.filesystemencoding().
280+
for fname in output.split(b'\n'):
281+
try:
282+
fname = six.text_type(fname, sys.getfilesystemencoding())
283+
except UnicodeDecodeError:
284+
continue
283285
if (os.path.splitext(fname)[1][1:] in fontext and
284286
os.path.exists(fname)):
285-
try:
286-
fname = six.text_type(fname, sys.getfilesystemencoding())
287-
except UnicodeDecodeError:
288-
continue
289287
fontfiles[fname] = 1
290288

291289
return fontfiles
@@ -1297,7 +1295,9 @@ def fc_match(pattern, fontext):
12971295
fontexts = get_fontext_synonyms(fontext)
12981296
ext = "." + fontext
12991297
try:
1300-
pipe = subprocess.Popen(['fc-match', '-sv', pattern], stdout=subprocess.PIPE)
1298+
pipe = subprocess.Popen(
1299+
['fc-match', '-s', '--format=%{file}\\n', pattern],
1300+
stdout=subprocess.PIPE)
13011301
output = pipe.communicate()[0]
13021302
except (OSError, IOError):
13031303
return None
@@ -1306,17 +1306,15 @@ def fc_match(pattern, fontext):
13061306
# result in bytes and parse it as bytes, until we extract the
13071307
# filename, which is in sys.filesystemencoding().
13081308
if pipe.returncode == 0:
1309-
for match in _fc_match_regex.finditer(output):
1310-
file = match.group(1)
1309+
for fname in output.split(b'\n'):
13111310
try:
1312-
file = six.text_type(file, sys.getfilesystemencoding())
1311+
fname = six.text_type(fname, sys.getfilesystemencoding())
13131312
except UnicodeDecodeError:
13141313
continue
1315-
if os.path.splitext(file)[1][1:] in fontexts:
1316-
return file
1314+
if os.path.splitext(fname)[1][1:] in fontexts:
1315+
return fname
13171316
return None
13181317

1319-
_fc_match_regex = re.compile(br'\sfile:\s+"(.*?)"')
13201318
_fc_match_cache = {}
13211319

13221320
def findfont(prop, fontext='ttf'):

0 commit comments

Comments
 (0)
0