def __init__(self, expr, fontset = 'bakoma', fontsize = 12, dpi = 100, default_style = 'it', cache=False): # Hash the arguments h = hash((expr, fontset, fontsize, dpi, default_style)) if is_string_like(fontset): fontset = self.fontset_mapping[fontset](default_style) # Check the cache first if cache and h in self._cache: self.boxmodel = self._cache[h] # Parse the expression else: self.boxmodel = MathtexParser().parse(expr, fontset, fontsize, dpi) if cache: self._cache[h] = self.boxmodel # Use ship to get a stream of glyphs and rectangles bbox = ship(0, 0, self.boxmodel)[2] self.rects, self.glyphs, bbox = ship(-bbox[0], 0, self.boxmodel) # Calculate the exact width, height and depth self.width = bbox[2] - bbox[0] self.height = self.boxmodel.height self.depth = self.boxmodel.depth self.fontset = fontset self.fontsize = fontsize self.dpi = dpi
def __init__(self, expr, fontset='bakoma', fontsize=12, dpi=100, default_style='it', cache=False): # Hash the arguments h = hash((expr, fontset, fontsize, dpi, default_style)) if is_string_like(fontset): fontset = self.fontset_mapping[fontset](default_style) # Check the cache first if cache and h in self._cache: self.boxmodel = self._cache[h] # Parse the expression else: self.boxmodel = MathtexParser().parse(expr, fontset, fontsize, dpi) if cache: self._cache[h] = self.boxmodel # Use ship to get a stream of glyphs and rectangles bbox = ship(0, 0, self.boxmodel)[2] self.rects, self.glyphs, bbox = ship(-bbox[0], 0, self.boxmodel) # Calculate the exact width, height and depth self.width = bbox[2] - bbox[0] self.height = self.boxmodel.height self.depth = self.boxmodel.depth self.fontset = fontset self.fontsize = fontsize self.dpi = dpi
def get_family(self): """ Return a list of font names that comprise the font family. """ if self._family is None: family = rcParams['font.family'] if is_string_like(family): return [family] return family return self._family
def set_family(self, family): """ Change the font family. May be either an alias (generic name is CSS parlance), such as: 'serif', 'sans-serif', 'cursive', 'fantasy', or 'monospace', or a real font name. """ if family is None: self._family = None else: if is_string_like(family): family = [family] self._family = family
def findfont(prop, fontext='ttf'): if not is_string_like(prop): prop = prop.get_fontconfig_pattern() cached = _fc_match_cache.get(prop) if cached is not None: return cached result = fc_match(prop, fontext) if result is None: result = fc_match(':', fontext) _fc_match_cache[prop] = result return result
def __init__(self, family = None, style = None, variant= None, weight = None, stretch= None, size = None, fname = None, # if this is set, it's a hardcoded filename to use _init = None # used only by copy() ): self._family = None self._slant = None self._variant = None self._weight = None self._stretch = None self._size = None self._file = None # This is used only by copy() if _init is not None: self.__dict__.update(_init.__dict__) return if is_string_like(family): # Treat family as a fontconfig pattern if it is the only # parameter provided. if (style is None and variant is None and weight is None and stretch is None and size is None and fname is None): self.set_fontconfig_pattern(family) return self.set_family(family) self.set_style(style) self.set_variant(variant) self.set_weight(weight) self.set_stretch(stretch) self.set_file(fname) self.set_size(size)
def __init__( self, family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, # if this is set, it's a hardcoded filename to use _init=None # used only by copy() ): self._family = None self._slant = None self._variant = None self._weight = None self._stretch = None self._size = None self._file = None # This is used only by copy() if _init is not None: self.__dict__.update(_init.__dict__) return if is_string_like(family): # Treat family as a fontconfig pattern if it is the only # parameter provided. if (style is None and variant is None and weight is None and stretch is None and size is None and fname is None): self.set_fontconfig_pattern(family) return self.set_family(family) self.set_style(style) self.set_variant(variant) self.set_weight(weight) self.set_stretch(stretch) self.set_file(fname) self.set_size(size)
def findfont(self, prop, fontext='ttf'): """ Search the font list for the font that most closely matches the :class:`FontProperties` *prop*. :meth:`findfont` performs a nearest neighbor search. Each font is given a similarity score to the target font properties. The first font with the highest score is returned. If no matches below a certain threshold are found, the default font (usually Vera Sans) is returned. The result is cached, so subsequent lookups don't have to perform the O(n) nearest neighbor search. See the `W3C Cascading Style Sheet, Level 1 <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation for a description of the font finding algorithm. """ debug = False if prop is None: prop = FontProperties() if is_string_like(prop): prop = FontProperties(prop) fname = prop.get_file() if fname is not None: return fname if fontext == 'afm': font_cache = self.afm_lookup_cache fontlist = self.afmlist else: font_cache = self.ttf_lookup_cache fontlist = self.ttflist cached = font_cache.get(hash(prop)) if cached: return cached best_score = 1e64 best_font = None for font in fontlist: # Matching family should have highest priority, so it is multiplied # by 10.0 score = \ self.score_family(prop.get_family(), font.name) * 10.0 + \ self.score_style(prop.get_style(), font.style) + \ self.score_variant(prop.get_variant(), font.variant) + \ self.score_weight(prop.get_weight(), font.weight) + \ self.score_stretch(prop.get_stretch(), font.stretch) + \ self.score_size(prop.get_size(), font.size) if score < best_score: best_score = score best_font = font if score == 0: break if best_font is None or best_score >= 10.0: result = self.defaultFont else: result = best_font.fname font_cache[hash(prop)] = result return result