Logo Search packages:      
Sourcecode: matplotlib version File versions

def matplotlib::font_manager::FontManager::findfont (   self,
  prop,
  fontext = 'ttf' 
)

Search the font dictionary for a font that exactly or closely
matches the specified font properties.  See the FontProperties class
for a description.

The properties are searched in the following order: name, style,
variant, weight, stretch, and size.  The font weight always matches
returning the closest weight, and the font size always matches for
scalable fonts.  An oblique style font will be used inplace of a
missing italic style font if present.  See the W3C Cascading Style
Sheet, Level 1 (CSS1; http://www.w3.org/TR/1998/REC-CSS2-19980512/)
documentation for a description of the font finding algorithm.

Definition at line 941 of file font_manager.py.

00941                                            :
        """
        Search the font dictionary for a font that exactly or closely
        matches the specified font properties.  See the FontProperties class
        for a description.

        The properties are searched in the following order: name, style,
        variant, weight, stretch, and size.  The font weight always matches
        returning the closest weight, and the font size always matches for
        scalable fonts.  An oblique style font will be used inplace of a
        missing italic style font if present.  See the W3C Cascading Style
        Sheet, Level 1 (CSS1; http://www.w3.org/TR/1998/REC-CSS2-19980512/)
        documentation for a description of the font finding algorithm.
        """
        debug = False
        if is_string_like(prop):
            prop = FontProperties(prop)
        fname = prop.get_file()
        if fname is not None:
            verbose.report('findfont returning %s'%fname, 'debug')
            return fname

        if fontext == 'afm':
            fontdict = self.afmdict
        else:
            fontdict = self.ttfdict

        original_name = prop.get_family()[0]
        style         = prop.get_style()
        variant       = prop.get_variant()
        weight        = weight_as_number(prop.get_weight())
        stretch       = prop.get_stretch()
        size          = str(prop.get_size_in_points())

        def lookup_name(name):
            try:
                fname = fontdict[name][style][variant][weight][stretch][size]
                verbose.report('\tfindfont cached %(name)s, %(style)s, %(variant)s, %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
                verbose.report('findfont returning %s'%fname, 'debug')
                return fname
            except KeyError:
                pass

            fname = None
            font = fontdict
            if font.has_key(name):
                font = font[name]
            else:
                verbose.report('\tfindfont failed %(name)s'%locals(), 'debug')
                return None

            if font.has_key(style):
                font = font[style]
            elif style == 'italic' and font.has_key('oblique'):
                font = font['oblique']
            elif style == 'oblique' and font.has_key('italic'):
                font = font['italic']
            else:
                verbose.report('\tfindfont failed %(name)s, %(style)s'%locals(), 'debug')
                return None

            if font.has_key(variant):
                font = font[variant]
            else:
                verbose.report('\tfindfont failed %(name)s, %(style)s, %(variant)s'%locals(), 'debug')
                return None

            if not font.has_key(weight):
                setWeights(font)
            if not font.has_key(weight):
                return None
            font = font[weight]

            if font.has_key(stretch):
                stretch_font = font[stretch]
                if stretch_font.has_key('scalable'):
                    fname = stretch_font['scalable']
                elif stretch_font.has_key(size):
                    fname = stretch_font[size]

            if fname is None:
                for val in font.values():
                    if val.has_key('scalable'):
                        fname = val['scalable']
                        break

            if fname is None:
                for val in font.values():
                    if val.has_key(size):
                        fname = val[size]
                        break

            if fname is None:
                verbose.report('\tfindfont failed %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s'%locals(), 'debug')
            else:
                fontkey = FontKey(",".join(prop.get_family()), style, variant, weight, stretch, size)
                add_filename(fontdict, fontkey, fname)
                verbose.report('\tfindfont found %(name)s, %(style)s, %(variant)s %(weight)s, %(stretch)s, %(size)s'%locals(), 'debug')
                verbose.report('findfont returning %s'%fname, 'debug')
            return fname

        font_family_aliases = Set(['serif', 'sans-serif', 'cursive',
                                   'fantasy', 'monospace', 'sans'])

        for name in prop.get_family():
            if name in font_family_aliases:
                if name == 'sans':
                    name = 'sans-serif'
                for name2 in rcParams['font.' + name]:
                    fname = lookup_name(name2)
                    if fname:
                        break
            else:
                fname = lookup_name(name)
            if fname:
                break

        if not fname:
            fontkey = FontKey(",".join(prop.get_family()), style, variant, weight, stretch, size)
            add_filename(fontdict, fontkey, self.defaultFont)
            verbose.report('Could not match %s, %s, %s.  Returning %s' % (name, style, weight, self.defaultFont))
            return self.defaultFont
        return fname


_is_opentype_cff_font_cache = {}


Generated by  Doxygen 1.6.0   Back to index