8000 Use freetypy in font_manager.py · matplotlib/matplotlib@6cdf405 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cdf405

Browse files
committed
Use freetypy in font_manager.py
1 parent 4de8258 commit 6cdf405

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

lib/matplotlib/font_manager.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
from collections import Iterable
5656
import matplotlib
5757
from matplotlib import afm
58-
from matplotlib import ft2font
5958
from matplotlib import rcParams, get_cachedir
6059
from matplotlib.cbook import is_string_like
6160
import matplotlib.cbook as cbook
@@ -68,6 +67,8 @@
6867
except ImportError:
6968
from functools32 import lru_cache
7069

70+
import freetypy as ft
71+
7172

7273
USE_FONTCONFIG = False
7374
verbose = matplotlib.verbose
@@ -392,35 +393,27 @@ def ttfFontProperty(font):
392393
A function for populating the :class:`FontKey` by extracting
393394
information from the TrueType font file.
394395
395-
*font* is a :class:`FT2Font` instance.
396+
*font* is a :class:`freetypy.Face` instance.
396397
"""
397398
name = font.family_name
398399

399400
# Styles are: italic, oblique, and normal (default)
400401

401-
sfnt = font.get_sfnt()
402-
sfnt2 = sfnt.get((1,0,0,2))
403-
sfnt4 = sfnt.get((1,0,0,4))
404-
if sfnt2:
405-
sfnt2 = sfnt2.decode('macroman').lower()
406-
else:
407-
sfnt2 = ''
408-
if sfnt4:
409-
sfnt4 = sfnt4.decode('macroman').lower()
410-
else:
411-
sfnt4 = ''
412-
if sfnt4.find('oblique') >= 0:
402+
names = font.sfnt_names
403+
subfamily = names.get_name(ft.TT_NAME_ID.FONT_SUBFAMILY).string
404+
full_name = names.get_name(ft.TT_NAME_ID.FULL_NAME).string
405+
406+
if full_name.find('oblique') >= 0:
413407
style = 'oblique'
414-
elif sfnt4.find('italic') >= 0:
408+
elif full_name.find('italic') >= 0:
415409
style = 'italic'
416-
elif sfnt2.find('regular') >= 0:
410+
elif subfamily.find('regular') >= 0:
417411
style = 'normal'
418-
elif font.style_flags & ft2font.ITALIC:
412+
elif font.style_flags & ft.STYLE_FLAG.ITALIC:
419413
style = 'italic'
420414
else:
421415
style = 'normal'
422416

423-
424417
# Variants are: small-caps and normal (default)
425418

426419
# !!!! Untested
@@ -435,11 +428,11 @@ def ttfFontProperty(font):
435428

436429
weight = None
437430
for w in six.iterkeys(weight_dict):
438-
if sfnt4.find(w) >= 0:
431+
if full_name.find(w) >= 0:
439432
weight = w
440433
break
441434
if not weight:
442-
if font.style_flags & ft2font.BOLD:
435+
if font.style_flags & ft.STYLE_FLAG.BOLD:
443436
weight = 700
444437
else:
445438
weight = 400
@@ -452,12 +445,12 @@ def ttfFontProperty(font):
452445
# Relative stretches are: wider, narrower
453446
# Child value is: inherit
454447

455-
if sfnt4.find('narrow') >= 0 or sfnt4.find('condensed') >= 0 or \
456-
sfnt4.find('cond') >= 0:
448+
if (full_name.find('narrow') >= 0 or full_name.find('condensed') >= 0 or
449+
full_name.find('cond') >= 0):
457450
stretch = 'condensed'
458-
elif sfnt4.find('demi cond') >= 0:
451+
elif full_name.find('demi cond') >= 0:
459452
stretch = 'semi-condensed'
460-
elif sfnt4.find('wide') >= 0 or sfnt4.find('expanded') >= 0:
453+
elif full_name.find('wide') >= 0 or full_name.find('expanded') >= 0:
461454
stretch = 'expanded'
462455
else:
463456
stretch = 'normal'
@@ -470,15 +463,15 @@ def ttfFontProperty(font):
470463
# Percentage values are in 'em's. Most robust specification.
471464

472465
# !!!! Incomplete
473-
if font.scalable:
466+
if font.is_scalable:
474467
size = 'scalable'
475468
else:
476-
size = str(float(font.get_fontsize()))
469+
size = str(float(font.size))
477470

478471
# !!!! Incomplete
479472
size_adjust = None
480473

481-
return FontEntry(font.fname, name, style, variant, weight, stretch, size)
474+
return FontEntry(font.filename, name, style, variant, weight, stretch, size)
482475

483476

484477
def afmFontProperty(fontpath, font):
@@ -558,7 +551,7 @@ def createFontList(fontfiles, fontext='ttf'):
558551
fontlist = []
559552
# Add fonts from list of known font files.
560553
seen = {}
561-
for fpath in fontfiles:
554+
for i, fpath in enumerate(fontfiles):
562555
verbose.report('createFontDict: %s' % (fpath), 'debug')
563556
fname = os.path.split(fpath)[1]
564557
if fname in seen: continue
@@ -583,7 +576,7 @@ def createFontList(fontfiles, fontext='ttf'):
583576
continue
584577
else:
585578
try:
586-
font = ft2font.FT2Font(fpath)
579+
font = ft.Face(fpath)
587580
except RuntimeError:
588581
verbose.report("Could not open font file %s"%fpath)
589582
continue
@@ -1349,7 +1342,7 @@ def is_opentype_cff_font(filename):
13491342
_fmcache = None
13501343

13511344

1352-
get_font = lru_cache(64)(ft2font.FT2Font)
1345+
get_font = lru_cache(64)(ft.Face)
13531346

13541347

13551348
# The experimental fontconfig-based backend.

0 commit comments

Comments
 (0)
0