55
55
from collections import Iterable
56
56
import matplotlib
57
57
from matplotlib import afm
58
- from matplotlib import ft2font
59
58
from matplotlib import rcParams , get_cachedir
60
59
from matplotlib .cbook import is_string_like
61
60
import matplotlib .cbook as cbook
68
67
except ImportError :
69
68
from functools32 import lru_cache
70
69
70
+ import freetypy as ft
71
+
71
72
72
73
USE_FONTCONFIG = False
73
74
verbose = matplotlib .verbose
@@ -392,35 +393,27 @@ def ttfFontProperty(font):
392
393
A function for populating the :class:`FontKey` by extracting
393
394
information from the TrueType font file.
394
395
395
- *font* is a :class:`FT2Font ` instance.
396
+ *font* is a :class:`freetypy.Face ` instance.
396
397
"""
397
398
name = font .family_name
398
399
399
400
# Styles are: italic, oblique, and normal (default)
400
401
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 :
413
407
style = 'oblique'
414
- elif sfnt4 .find ('italic' ) >= 0 :
408
+ elif full_name .find ('italic' ) >= 0 :
415
409
style = 'italic'
416
- elif sfnt2 .find ('regular' ) >= 0 :
410
+ elif subfamily .find ('regular' ) >= 0 :
417
411
style = 'normal'
418
- elif font .style_flags & ft2font .ITALIC :
412
+ elif font .style_flags & ft . STYLE_FLAG .ITALIC :
419
413
style = 'italic'
420
414
else :
421
415
style = 'normal'
422
416
423
-
8000
span>
424
417
# Variants are: small-caps and normal (default)
425
418
426
419
# !!!! Untested
@@ -435,11 +428,11 @@ def ttfFontProperty(font):
435
428
436
429
weight = None
437
430
for w in six .iterkeys (weight_dict ):
438
- if sfnt4 .find (w ) >= 0 :
431
+ if full_name .find (w ) >= 0 :
439
432
weight = w
440
433
break
441
434
if not weight :
442
- if font .style_flags & ft2font .BOLD :
435
+ if font .style_flags & ft . STYLE_FLAG .BOLD :
443
436
weight = 700
444
437
else :
445
438
weight = 400
@@ -452,12 +445,12 @@ def ttfFontProperty(font):
452
445
# Relative stretches are: wider, narrower
453
446
# Child value is: inherit
454
447
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 ) :
457
450
stretch = 'condensed'
458
- elif sfnt4 .find ('demi cond' ) >= 0 :
451
+ elif full_name .find ('demi cond' ) >= 0 :
459
452
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 :
461
454
stretch = 'expanded'
462
455
else :
463
456
stretch = 'normal'
@@ -470,15 +463,15 @@ def ttfFontProperty(font):
470
463
# Percentage values are in 'em's. Most robust specification.
471
464
472
465
# !!!! Incomplete
473
- if font .scalable :
466
+ if font .is_scalable :
474
467
size = 'scalable'
475
468
else :
476
- size = str (float (font .get_fontsize () ))
469
+ size = str (float (font .size ))
477
470
478
471
# !!!! Incomplete
479
472
size_adjust = None
480
473
481
- return FontEntry (font .fname , name , style , variant , weight , stretch , size )
474
+ return FontEntry (font .filename , name , style , variant , weight , stretch , size )
482
475
483
476
484
477
def afmFontProperty (fontpath , font ):
@@ -558,7 +551,7 @@ def createFontList(fontfiles, fontext='ttf'):
558
551
fontlist = []
559
552
# Add fonts from list of known font files.
560
553
seen = {}
561
- for fpath in fontfiles :
554
+ for i , fpath in enumerate ( fontfiles ) :
562
555
verbose .report ('createFontDict: %s' % (fpath ), 'debug' )
563
556
fname = os .path .split (fpath )[1 ]
564
557
if fname in seen : continue
@@ -583,7 +576,7 @@ def createFontList(fontfiles, fontext='ttf'):
583
576
continue
584
577
else :
585
578
try :
586
- font = ft2font . FT2Font (fpath )
579
+ font = ft . Face (fpath )
587
580
except RuntimeError :
588
581
verbose .report ("Could not open font file %s" % fpath )
589
582
continue
@@ -1349,7 +1342,7 @@ def is_opentype_cff_font(filename):
1349
1342
_fmcache = None
1350
1343
1351
1344
1352
- get_font = lru_cache (64 )(ft2font . FT2Font )
1345
+ get_font = lru_cache (64 )(ft . Face )
1353
1346
1354
1347
1355
1348
# The experimental fontconfig-based backend.
0 commit comments