@@ -625,20 +625,6 @@ class FontProperties(object):
625
625
fontconfig.
626
626
"""
627
627
628
- class FontPropertiesSet (object ):
629
- """This class contains all of the default properties at the
630
- class level, which are then overridden (only if provided) at
631
- the instance level."""
632
- family = rcParams ['font.' + rcParams ['font.family' ]]
633
- if is_string_like (family ):
634
- family = [family ]
635
- slant = [rcParams ['font.style' ]]
636
- variant = [rcParams ['font.variant' ]]
637
- weight = [rcParams ['font.weight' ]]
638
- stretch = [rcParams ['font.stretch' ]]
639
- size = [rcParams ['font.size' ]]
640
- file = None
641
-
642
628
def __init__ (self ,
643
629
family = None ,
644
630
style = None ,
@@ -649,12 +635,17 @@ def __init__(self,
649
635
fname = None , # if this is set, it's a hardcoded filename to use
650
636
_init = None # used only by copy()
651
637
):
652
-
653
- self .__props = self .FontPropertiesSet ()
638
+ self ._family = None
639
+ self ._slant = None
640
+ self ._variant = None
641
+ self ._weight = None
642
+ self ._stretch = None
643
+ self ._size = None
644
+ self ._file = None
654
645
655
646
# This is used only by copy()
656
647
if _init is not None :
657
- self .__props . __dict__ .update (_init )
648
+ self .__dict__ .update (_init . __dict__ )
658
649
return
659
650
660
651
if is_string_like (family ):
@@ -666,9 +657,8 @@ def __init__(self,
666
657
stretch is None and
667
658
size is None and
668
659
fname is None ):
669
- self .__props . __dict__ = self . _parse_fontconfig_pattern (family )
660
+ self .set_fontconfig_pattern (family )
670
661
return
671
- family = [family ]
672
662
673
663
self .set_family (family )
674
664
self .set_style (style )
@@ -682,54 +672,66 @@ def _parse_fontconfig_pattern(self, pattern):
682
672
return parse_fontconfig_pattern (pattern )
683
673
684
674
def __hash__ (self ):
685
- return hash (repr (self .__props . __dict__ ))
675
+ return hash (repr (self .__dict__ ))
686
676
687
677
def __str__ (self ):
688
678
return self .get_fontconfig_pattern ()
689
679
690
680
def get_family (self ):
691
681
"""Return a list of font names that comprise the font family.
692
682
"""
693
- return self .__props .family
683
+ if self ._family is None :
684
+ family = rcParams ['font.family' ]
685
+ if is_string_like (family ):
686
+ return [family ]
687
+ return family
688
+ return self ._family
694
689
695
690
def get_name (self ):
696
691
"""Return the name of the font that best matches the font properties."""
697
692
return ft2font .FT2Font (str (findfont (self ))).family_name
698
693
699
694
def get_style (self ):
700
695
"""Return the font style. Values are: normal, italic or oblique."""
701
- return self .__props .slant [0 ]
696
+ if self ._slant is None :
697
+ return rcParams ['font.style' ]
698
+ return self ._slant
702
699
703
700
def get_variant (self ):
704
701
"""Return the font variant. Values are: normal or small-caps."""
705
- return self .__props .variant [0 ]
702
+ if self ._variant is None :
703
+ return rcParams ['font.variant' ]
704
+ return self ._variant
706
705
707
706
def get_weight (self ):
708
707
"""
709
708
Return the font weight. See the FontProperties class for a
710
709
a list of possible values.
711
710
"""
712
- return self .__props .weight [0 ]
711
+ if self ._weight is None :
712
+ return rcParams ['font.weight' ]
713
+ return self ._weight
713
714
714
715
def get_stretch (self ):
715
716
"""
716
717
Return the font stretch or width. Options are: normal,
717
718
narrow, condensed, or wide.
718
719
"""
719
- return self .__props .stretch [0 ]
720
+ if self ._stretch is None :
721
+ return rcParams ['font.stretch' ]
722
+ return self ._stretch
720
723
721
724
def get_size (self ):
722
725
"""Return the font size."""
723
- return float (self .__props .size [0 ])
726
+ if self ._size is None :
727
+ return rcParams ['font.size' ]
728
+ return float (self ._size )
724
729
725
730
def get_file (self ):
726
- if self .__props .file is not None :
727
- return self .__props .file [0 ]
728
- else :
729
- return None
731
+ return self ._file
730
732
731
733
def get_fontconfig_pattern (self ):
732
- return generate_fontconfig_pattern (self . __props . __dict__ )
734
+ return generate_fontconfig_pattern (self )
733
735
734
736
def set_family (self , family ):
735
737
"""
@@ -738,87 +740,75 @@ def set_family(self, family):
738
740
fantasy, or monospace, or a real font name.
739
741
"""
740
742
if family is None :
741
- self .__props . __dict__ . pop ( 'family' , None )
743
+ self ._family = None
742
744
else :
743
745
if is_string_like (family ):
744
746
family = [family ]
745
- self .__props . family = family
747
+ self ._family = family
746
748
set_name = set_family
747
749
748
750
def set_style (self , style ):
749
751
"""Set the font style. Values are: normal, italic or oblique."""
750
- if style is None :
751
- self .__props .__dict__ .pop ('style' , None )
752
- else :
753
- if style not in ('normal' , 'italic' , 'oblique' ):
754
- raise ValueError ("style must be normal, italic or oblique" )
755
- self .__props .slant = [style ]
752
+ if style not in ('normal' , 'italic' , 'oblique' , None ):
753
+ raise ValueError ("style must be normal, italic or oblique" )
754
+ self ._slant = style
756
755
757
756
def set_variant (self , variant ):
758
757
"""Set the font variant. Values are: normal or small-caps."""
759
- if variant is None :
760
- self .__props .__dict__ .pop ('variant' , None )
761
- else :
762
- if variant not in ('normal' , 'small-caps' ):
763
- raise ValueError ("variant must be normal or small-caps" )
764
- self .__props .variant = [variant ]
758
+ if variant not in ('normal' , 'small-caps' , None ):
759
+ raise ValueError ("variant must be normal or small-caps" )
760
+ self ._variant = variant
765
761
766
762
def set_weight (self , weight ):
767
763
"""
768
764
Set the font weight. See the FontProperties class for a
769
765
a list of possible values.
770
766
"""
771
- if weight is None :
772
- self .__props .__dict__ .pop ('weight' , None )
773
- else :
774
- if (weight not in weight_dict and
775
- weight not in weight_dict .keys ()):
776
- raise ValueError ("weight is invalid" )
777
- self .__props .weight = [weight ]
767
+ if (weight is not None and
768
+ weight not in weight_dict and
769
+ weight not in weight_dict .keys ()):
770
+ raise ValueError ("weight is invalid" )
771
+ self ._weight = weight
778
772
779
773
def set_stretch (self , stretch ):
780
774
"""
781
775
Set the font stretch or width. Options are: normal, narrow,
782
776
condensed, or wide.
783
777
"""
784
- if stretch is None :
785
- self .__props .__dict__ .pop ('stretch' , None )
786
- else :
787
- self .__props .stretch = [stretch ]
778
+ if stretch not in ('normal' , 'narrow' , 'condensed' , 'wide' , None ):
779
+ raise ValueError ("stretch is invalid" )
780
+ self ._stretch = stretch
788
781
789
782
def set_size (self , size ):
790
783
"""Set the font size."""
791
784
if size is None :
792
- self .__props . __dict__ . pop ( 'size' , None )
785
+ self ._size = None
793
786
else :
794
787
if is_string_like (size ):
795
788
parent_size = fontManager .get_default_size ()
796
789
scaling = font_scalings .get (size )
797
790
if scaling is not None :
798
791
size = parent_size * scaling
799
792
else :
800
- size = parent_size
801
- if isinstance (size , (int , float )):
802
- size = [size ]
803
- self .__props .size = size
793
+ try :
794
+ size = float (size )
795
+ except ValueError :
796
+ size = parent_size
797
+ assert (type (size ) in (int , float ))
798
+ self ._size = size
804
799
805
800
def set_file (self , file ):
806
- if file is None :
807
- self .__props .__dict__ .pop ('file' , None )
808
- else :
809
- self .__props .file = [file ]
801
+ self ._file = file
810
802
811
803
get_size_in_points = get_size
812
804
813
805
def set_fontconfig_pattern (self , pattern ):
814
- self .__props .__dict__ = self ._parse_fontconfig_pattern (pattern )
815
-
816
- def add_property_pair (self , key , val ):
817
- self .__props .setdefault (key , []).append (val )
806
+ for key , val in self ._parse_fontconfig_pattern (pattern ).items ():
807
+ getattr (self , "set_" + key )(val )
818
808
819
809
def copy (self ):
820
810
"""Return a deep copy of self"""
821
- return FontProperties (_init = self . __props . __dict__ )
811
+ return FontProperties (_init = self )
822
812
823
813
def ttfdict_to_fnames (d ):
824
814
'flatten a ttfdict to all the filenames it contains'
0 commit comments