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 _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) # type: ignore 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. """ if columns: rect_width = columns.total_width rect_height = columns.total_height # TODO: this works only for reliable sources like AutoCAD, # BricsCAD and ezdxf! So far no known column support from # other DXF exporters. else: 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) columns = mtext.columns if columns is None: 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 = 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 test_get_font_name_for_mtext(msp): mtext = msp.add_mtext("abc", dxfattribs={"style": "ARIAL"}) assert get_font_name(mtext) == "arial.ttf"
def test_get_font_name_for_entity_without_font_usage(): e = DXFEntity() assert get_font_name(e) == "txt", "should return the default font"