Text Size Tools

class ezdxf.tools.text_size.TextSize

A frozen dataclass as return type for the text_size() function.

width

The text width in drawing units (float).

cap_height

The font cap-height in drawing units (float).

total_height

The font total-height = cap-height + descender-height in drawing units (float).

ezdxf.tools.text_size.text_size(text: ezdxf.entities.text.Text) → ezdxf.tools.text_size.TextSize

Returns the measured text width, the font cap-height and the font total-height for a Text entity. This function uses the optional Matplotlib package if available to measure the final rendering width and font-height for the Text entity as close as possible. This function does not measure the real char height! Without access to the Matplotlib package the MonospaceFont is used and the measurements are very inaccurate.

See the text2path add-on for more tools to work with the text path objects created by the Matplotlib package.

class ezdxf.tools.text_size.MTextSize

A frozen dataclass as return type for the mtext_size() function.

total_width

The total width in drawing units (float)

total_height

The total height in drawing units (float), same as max(column_heights).

column_width

The width of a single column in drawing units (float)

gutter_width

The space between columns in drawing units (float)

column_heights

A tuple of columns heights (float) in drawing units. Contains at least one column height and the the column height is 0 for an empty column.

column_count

The count of columns (int).

ezdxf.tools.text_size.mtext_size(mtext: ezdxf.entities.mtext.MText, tool: Optional[ezdxf.tools.text_size.MTextSizeDetector] = None) → ezdxf.tools.text_size.MTextSize

Returns the total-width, -height and columns information for a MText entity.

This function uses the optional Matplotlib package if available to do font measurements and the internal text layout engine to determine the final rendering size for the MText entity as close as possible. Without access to the Matplotlib package the MonospaceFont is used and the measurements are very inaccurate.

Attention: The required full layout calculation is slow!

The first call to this function with Matplotlib support is very slow, because Matplotlib lookup all available fonts on the system. To speedup the calculation and accepting inaccurate results you can disable the Matplotlib support manually:

ezdxf.option.use_matplotlib = False
ezdxf.tools.text_size.estimate_mtext_extents(mtext: MText) → Tuple[float, float]

Estimate the width and height of a single column MText entity.

This function is faster than the mtext_size() function, but the result is very inaccurate if inline codes are used or line wrapping at the column border is involved!

This function uses the optional Matplotlib package if available.

Returns:Tuple[width, height]