@@ -477,24 +477,6 @@ def createFontDict(fontfiles, fontext='ttf'):
477477 except : continue
478478
479479 add_filename (fontdict , prop , fpath )
480-
481- # !!!! Default font algorithm needs improvement
482- if prop .name .lower () in ['bitstream vera serif' , 'times' ]:
483- prop .name = 'serif'
484- add_filename (fontdict , prop , fpath )
485- elif prop .name .lower () in ['bitstream vera sans' , 'helvetica' ]:
486- prop .name = 'sans-serif'
487- add_filename (fontdict , prop , fpath )
488- elif prop .name .lower () in ['zapf chancery' , 'itc zapf chancery' ]:
489- prop .name = 'cursive'
490- add_filename (fontdict , prop , fpath )
491- elif prop .name .lower () in ['western' , 'itc avant garde gothic' ]:
492- prop .name = 'fantasy'
493- add_filename (fontdict , prop , fpath )
494- elif prop .name .lower () in ['bitstream vera sans mono' , 'courier' ]:
495- prop .name = 'monospace'
496- add_filename (fontdict , prop , fpath )
497-
498480 return fontdict
499481
500482def setWeights (font ):
@@ -868,6 +850,13 @@ def rebuild():
868850 break
869851 verbose .report ('loaded ttfcache file %s' % ttfcache )
870852
853+ def flatten (d , path ):
854+ if isinstance (d , dict ):
855+ for key , val in d .items ():
856+ flatten (val , path + [key ])
857+ elif isinstance (d , str ):
858+ print path , os .path .basename (d )
859+ flatten (self .ttfdict , [])
871860 #self.ttfdict = createFontDict(self.ttffiles)
872861
873862 # Load AFM fonts for PostScript
@@ -928,74 +917,99 @@ def findfont(self, prop, fontext='ttf'):
928917 else :
929918 fontdict = self .ttfdict
930919
931- name = prop .get_family ()[0 ]
932- style = prop .get_style ()
933- variant = prop .get_variant ()
934- weight = weight_as_number (prop .get_weight ())
935- stretch = prop .get_stretch ()
936- size = str (prop .get_size_in_points ())
937-
938- try :
939- fname = fontdict [name ][style ][variant ][weight ][stretch ][size ]
940- verbose .report ('\t findfont cached %(name)s, %(style)s, %(variant)s, %(weight)s, %(stretch)s, %(size)s' % locals (), 'debug' )
941- verbose .report ('findfont returning %s' % fname , 'debug' )
942- return fname
943- except KeyError :
944- pass
920+ original_name = prop .get_family ()[0 ]
921+ style = prop .get_style ()
922+ variant = prop .get_variant ()
923+ weight = weight_as_number (prop .get_weight ())
924+ stretch = prop .get_stretch ()
925+ size = str (prop .get_size_in_points ())
945926
946- for name in prop .get_family ():
927+ def lookup_name (name ):
928+ try :
929+ fname = fontdict [name ][style ][variant ][weight ][stretch ][size ]
930+ verbose .report ('\t findfont cached %(name)s, %(style)s, %(variant)s, %(weight)s, %(stretch)s, %(size)s' % locals (), 'debug' )
931+ verbose .report ('findfont returning %s' % fname , 'debug' )
932+ return fname
933+ except KeyError :
934+ pass
935+
936+ fname = None
947937 font = fontdict
938+ print font .keys ()
948939 if font .has_key (name ):
949940 font = font [name ]
950941 else :
951942 verbose .report ('\t findfont failed %(name)s' % locals (), 'debug' )
952- continue
943+ return None
953944
945+ print font .keys ()
954946 if font .has_key (style ):
955947 font = font [style ]
956- elif style == 'italics ' and font .has_key ('oblique' ):
948+ elif style == 'italic ' and font .has_key ('oblique' ):
957949 font = font ['oblique' ]
950+ elif style == 'oblique' and font .has_key ('italic' ):
951+ font = font ['italic' ]
958952 else :
959953 verbose .report ('\t findfont failed %(name)s, %(style)s' % locals (), 'debug' )
960- continue
954+ return None
961955
962956 if font .has_key (variant ):
963957 font = font [variant ]
964958 else :
965959 verbose .report ('\t findfont failed %(name)s, %(style)s, %(variant)s' % locals (), 'debug' )
966- continue
960+ return None
967961
968962 if not font .has_key (weight ):
969963 setWeights (font )
970964 font = font [weight ]
971965
972- # !!!! need improvement
973966 if font .has_key (stretch ):
974- font = font [stretch ]
975- else :
967+ stretch_font = font [stretch ]
968+ if stretch_font .has_key ('scalable' ):
969+ fname = stretch_font ['scalable' ]
970+ elif stretch_font .has_key (size ):
971+ fname = stretch_font [size ]
972+
973+ if fname is None :
974+ for val in font .values ():
975+ if val .has_key ('scalable' ):
976+ fname = val ['scalable' ]
977+ break
978+
979+ if fname is None :
980+ for val in font .values ():
981+ if val .has_key (size ):
982+ fname = val [size ]
983+ break
984+
985+ if fname is None :
976986 verbose .report ('\t findfont failed %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s' % locals (), 'debug' )
977- continue
978-
979- if font .has_key ('scalable' ):
980- fname = font ['scalable' ]
981- elif font .has_key (size ):
982- fname = font [size ]
983987 else :
984- verbose .report ('\t findfont failed %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s' % locals (), 'debug' )
985- continue
986-
987- fontkey = FontKey (name , style , variant , weight , stretch , size )
988- add_filename (fontdict , fontkey , fname )
989- verbose .report ('\t findfont found %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s' % locals (), 'debug' )
990- verbose .report ('findfont returning %s' % fname , 'debug' )
991-
988+ fontkey = FontKey (original_name , style , variant , weight , stretch , size )
989+ add_filename (fontdict , fontkey , fname )
990+ verbose .report ('\t findfont found %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s' % locals (), 'debug' )
991+ verbose .report ('findfont returning %s' % fname , 'debug' )
992992 return fname
993993
994- fontkey = FontKey (name , style , variant , weight , stretch , size )
995- add_filename (fontdict , fontkey , self .defaultFont )
996- verbose .report ('Could not match %s, %s, %s. Returning %s' % (name , style , variant , self .defaultFont ))
994+ font_family_aliases = ['serif' , 'sans-serif' , 'cursive' , 'fantasy' , 'monospace' ]
995+
996+ for name in prop .get_family ():
997+ if name in font_family_aliases :
998+ for name2 in rcParams ['font.' + name ]:
999+ fname = lookup_name (name2 )
1000+ if fname :
1001+ break
1002+ else :
1003+ fname = lookup_name (name )
1004+ if fname :
1005+ break
9971006
998- return self .defaultFont
1007+ if not fname :
1008+ fontkey = FontKey (original_name , style , variant , weight , stretch , size )
1009+ add_filename (fontdict , fontkey , self .defaultFont )
1010+ verbose .report ('Could not match %s, %s, %s. Returning %s' % (name , style , variant , self .defaultFont ))
1011+ return self .defaultFont
1012+ return fname
9991013
10001014 def _get_afm_font_dict (self ):
10011015 cache_message = "Saving AFM font cache for PS and PDF backends to %s.\n " \
0 commit comments