def text_size(text: Text) -> TextSize: """Returns the measured text width, the font cap-height and the font total-height for a :class:`~ezdxf.entities.Text` entity. This function uses the optional `Matplotlib` package if available to measure the final rendering width and font-height for the :class:`Text` entity as close as possible. This function does not measure the real char height! Without access to the `Matplotlib` package the :class:`~ezdxf.tools.fonts.MonospaceFont` is used and the measurements are very inaccurate. See the :mod:`~ezdxf.addons.text2path` add-on for more tools to work with the text path objects created by the `Matplotlib` package. """ width_factor: float = text.dxf.get_default("width") text_width: float = 0.0 cap_height: float = text.dxf.get_default("height") font: fonts.AbstractFont = fonts.MonospaceFont(cap_height, width_factor) if ezdxf.options.use_matplotlib and text.doc is not None: style = text.doc.styles.get(text.dxf.get_default("style")) font_name = get_font_name(style) font = fonts.make_font(font_name, cap_height, width_factor) total_height = font.measurements.total_height content = text.plain_text() if content: text_width = font.text_width(content) return TextSize(text_width, cap_height, total_height)
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 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 _convert_entity(self): """ Calculates the rough border path for a single line text. Calculation is based on a mono-spaced font and therefore the border path is just an educated guess. Vertical text generation and oblique angle is ignored. """ def get_text_rotation() -> float: if alignment in ('FIT', 'ALIGNED') and not p1.isclose(p2): return (p2 - p1).angle else: return math.degrees(text.dxf.rotation) def get_insert() -> Vec3: if alignment == 'LEFT': return p1 elif alignment in ('FIT', 'ALIGNED'): return p1.lerp(p2, factor=0.5) else: return p2 text = cast('Text', self.entity) if text.dxftype() == 'ATTDEF': # ATTDEF outside of a BLOCK renders the tag rather than the value content = text.dxf.tag else: content = text.dxf.text content = plain_text(content) if len(content) == 0: # empty path - does not render any vertices! self._path = Path() return p1: Vec3 = text.dxf.insert p2: Vec3 = text.dxf.align_point font = fonts.make_font(get_font_name(text), text.dxf.height, text.dxf.width) text_line = TextLine(content, font) alignment: str = text.get_align() if text.dxf.halign > 2: # ALIGNED=3, MIDDLE=4, FIT=5 text_line.stretch(alignment, p1, p2) halign, valign = unified_alignment(text) corner_vertices = text_line.corner_vertices(get_insert(), halign, valign, get_text_rotation()) ocs = text.ocs() self._path = Path.from_vertices( ocs.points_to_wcs(corner_vertices), close=True, )
def _convert_entity(self): """ Calculates the rough border path for a MTEXT entity. Calculation is based on a mono-spaced font and therefore the border path is just an educated guess. Most special features of MTEXT is not supported. """ def get_content() -> List[str]: text = mtext.plain_text(split=False) return text_wrap(text, box_width, font.text_width) def get_max_str() -> str: return max(content, key=lambda s: len(s)) def get_rect_width() -> float: if box_width: return box_width s = get_max_str() if len(s) == 0: s = " " return font.text_width(s) def get_rect_height() -> float: line_height = font.measurements.total_height cap_height = font.measurements.cap_height # Line spacing factor: Percentage of default (3-on-5) line # spacing to be applied. # thx to mbway: multiple of cap_height between the baseline of the # previous line and the baseline of the next line # 3-on-5 line spacing = 5/3 = 1.67 line_spacing = cap_height * mtext.dxf.line_spacing_factor * 1.67 spacing = line_spacing - line_height line_count = len(content) return line_height * line_count + spacing * (line_count - 1) def get_ucs() -> UCS: """ Create local coordinate system: origin = insertion point z-axis = extrusion vector x-axis = text_direction or text rotation, text rotation requires extrusion vector == (0, 0, 1) or treatment like an OCS? """ origin = mtext.dxf.insert z_axis = mtext.dxf.extrusion # default is Z_AXIS x_axis = X_AXIS if mtext.dxf.hasattr('text_direction'): x_axis = mtext.dxf.text_direction elif mtext.dxf.hasattr('rotation'): # TODO: what if extrusion vector is not (0, 0, 1) x_axis = Vec3.from_deg_angle(mtext.dxf.rotation) z_axis = Z_AXIS return UCS(origin=origin, ux=x_axis, uz=z_axis) def get_shift_factors(): halign, valign = unified_alignment(mtext) shift_x = 0 shift_y = 0 if halign == const.CENTER: shift_x = -0.5 elif halign == const.RIGHT: shift_x = -1.0 if valign == const.MIDDLE: shift_y = 0.5 elif valign == const.BOTTOM: shift_y = 1.0 return shift_x, shift_y def get_corner_vertices() -> Iterable[Vec3]: """ Create corner vertices in the local working plan, where the insertion point is the origin. """ rect_width = mtext.dxf.get('rect_width', get_rect_width()) rect_height = mtext.dxf.get('rect_height', get_rect_height()) # TOP LEFT alignment: vertices = [ Vec3(0, 0), Vec3(rect_width, 0), Vec3(rect_width, -rect_height), Vec3(0, -rect_height) ] sx, sy = get_shift_factors() shift = Vec3(sx * rect_width, sy * rect_height) return (v + shift for v in vertices) mtext: "MText" = cast("MText", self.entity) box_width = mtext.dxf.get('width', 0) font = fonts.make_font(get_font_name(mtext), mtext.dxf.char_height, 1.0) content: List[str] = get_content() if len(content) == 0: # empty path - does not render any vertices! self._path = Path() return ucs = get_ucs() corner_vertices = get_corner_vertices() self._path = Path.from_vertices( ucs.points_to_wcs(corner_vertices), close=True, )
def _convert_entity(self): """Calculates the rough border path for a single line text. Calculation is based on a mono-spaced font and therefore the border path is just an educated guess. Vertical text generation and oblique angle is ignored. """ def text_rotation(): if fit_or_aligned and not p1.isclose(p2): return (p2 - p1).angle else: return math.radians(text.dxf.rotation) def location(): if fit_or_aligned: return p1.lerp(p2, factor=0.5) return p1 text = cast("Text", self.entity) if text.dxftype() == "ATTDEF": # ATTDEF outside of a BLOCK renders the tag rather than the value content = text.dxf.tag else: content = text.dxf.text content = plain_text(content) if len(content) == 0: # empty path - does not render any vertices! self._path = Path() return font = fonts.make_font(get_font_name(text), text.dxf.height, text.dxf.width) text_line = TextLine(content, font) alignment, p1, p2 = text.get_placement() if p2 is None: p2 = p1 fit_or_aligned = (alignment == TextEntityAlignment.FIT or alignment == TextEntityAlignment.ALIGNED) if text.dxf.halign > 2: # ALIGNED=3, MIDDLE=4, FIT=5 text_line.stretch(alignment, p1, p2) halign, valign = unified_alignment(text) mirror_x = -1 if text.is_backward else 1 mirror_y = -1 if text.is_upside_down else 1 oblique: float = math.radians(text.dxf.oblique) corner_vertices = text_line.corner_vertices( location(), halign, valign, angle=text_rotation(), scale=(mirror_x, mirror_y), oblique=oblique, ) ocs = text.ocs() self._path = from_vertices( ocs.points_to_wcs(corner_vertices), close=True, )
def __init__(self, height: float): self.height = float(height) self.font = fonts.make_font(FONT, self.height) self.space = measure_space(self.font)