예제 #1
0
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)
예제 #2
0
 def test_monospace_font(self):
     font = fonts.MonospaceFont(2.5, 0.75)
     assert font.text_width("1234") == 7.5