def make_font(self, cap_height: float = None, width_factor: float = None) -> "AbstractFont": """Returns a font abstraction :class:`~ezdxf.tools.fonts.AbstractFont` for this text style. Returns a font for a cap height of 1, if the text style has auto height (:attr:`Textstyle.dxf.height` is 0) and the given `cap_height` is ``None`` or 0. Uses the :attr:`Textstyle.dxf.width` attribute if the given `width_factor` is ``None`` or 0, the default value is 1. The attribute :attr:`Textstyle.dxf.big_font` is ignored. """ from ezdxf import options from ezdxf.tools import fonts ttf = "" if options.use_matplotlib and self.has_extended_font_data: family, italic, bold = self.get_extended_font_data() if family: text_style = "italic" if italic else "normal" text_weight = "bold" if bold else "normal" font_face = fonts.FontFace(family=family, style=text_style, weight=text_weight) ttf = fonts.find_ttf_path(font_face) else: ttf = self.dxf.get("font", const.DEFAULT_TTF) if ttf == "": ttf = const.DEFAULT_TTF if cap_height is None or cap_height == 0.0: cap_height = self.dxf.height if cap_height == 0.0: cap_height = 1.0 if width_factor is None or width_factor == 0.0: width_factor = self.dxf.width return fonts.make_font(ttf, cap_height, width_factor) # type: ignore
def test_font_ttf_path_from_font_face(): # low level support path = mpl_fs.find_filename(family="Arial", weight=900) assert path.name == "ariblk.ttf" # high level support, see also test_resolve_font_ttf_path() ff = fonts.get_font_face(path.name) assert fonts.find_ttf_path(ff) == "ariblk.ttf"
def get_font(self, ctx: MTextContext) -> fonts.AbstractFont: ttf = fonts.find_ttf_path(ctx.font_face) key = (ttf, ctx.cap_height, ctx.width_factor) font = self._font_cache.get(key) if font is None: font = fonts.make_font(ttf, ctx.cap_height, ctx.width_factor) self._font_cache[key] = font return font
def ttf_path(font_face: fonts.FontFace) -> str: ttf = font_face.ttf if not ttf: ttf = fonts.find_ttf_path(font_face) else: # remapping SHX replacement fonts to SHX fonts, # like "txt_____.ttf" to "TXT.SHX": shx = fonts.map_ttf_to_shx(ttf) if shx: ttf = shx return ttf
def ttf_path(font_face: fonts.FontFace) -> str: ttf = font_face.ttf if not ttf: ttf = fonts.find_ttf_path(font_face) return ttf