text2path

Tools to convert text strings and text based DXF entities into outer- and inner linear paths as Path objects. At the moment only the TEXT and the ATTRIB entity can be converted into paths and hatches.

Added in version 1.1: Text rendering is done by the fontTools package, which is a hard dependency of ezdxf. Support for stroke fonts, these are the basic vector fonts included in CAD applications, like .shx, .shp or .lff fonts was added but these fonts cannot be rendered as HATCH entities.

The required font files are not included with ezdxf as they are copyrighted or, in the case of the LibreCAD font format, licensed under the “GPL v2 and later”. Set the paths to such stroke fonts in the config file, see option ezdxf.options.support_dirs:

[core]
support_dirs =
    "C:\Program Files\Bricsys\BricsCAD V23 en_US\Fonts",
    ~/shx_fonts,
    ~/shp_fonts,
    ~/lff_fonts,

Don’t expect a 100% match compared to CAD applications but the results with fontTools are better than the previous Matplotlib renderings.

Text Alignments

The text alignments are enums of type ezdxf.enums.TextEntityAlignment

Vertical

Left

Center

Right

Top

TOP_LEFT

TOP_CENTER

TOP_RIGHT

Middle

MIDDLE_LEFT

MIDDLE_CENTER

MIDDLE_RIGHT

Bottom

BOTTOM_LEFT

BOTTOM_CENTER

BOTTOM_RIGHT

Baseline

LEFT

CENTER

RIGHT

The vertical middle alignments (MIDDLE_XXX), center the text vertically in the middle of the uppercase letter “X” (cap height).

Special alignments, where the horizontal alignment is always in the center of the text:

  • ALIGNED: text is scaled to match the given length, scales x- and y-direction by the same factor.

  • FIT: text is scaled to match the given length, but scales only in x-direction.

  • MIDDLE: insertion point is the center of the total height (cap height + descender height) without scaling, the length argument is ignored.

Font Face Definition

A font face is defined by the Matplotlib compatible FontFace object by font-family, font-style, font-stretch and font-weight.

String Functions

ezdxf.addons.text2path.make_path_from_str(s: str, font: FontFace, size: float = 1.0, align=TextEntityAlignment.LEFT, length: float = 0, m: Matrix44 = None) Path

Convert a single line string s into a Multi-Path object. The text size is the height of the uppercase letter “X” (cap height). The paths are aligned about the insertion point at (0, 0). BASELINE means the bottom of the letter “X”.

Parameters:
  • s – text to convert

  • font – font face definition as FontFace object

  • size – text size (cap height) in drawing units

  • align – alignment as ezdxf.enums.TextEntityAlignment, default is LEFT

  • length – target length for the ALIGNED and FIT alignments

  • m – transformation Matrix44

ezdxf.addons.text2path.make_paths_from_str(s: str, font: FontFace, size: float = 1.0, align=TextEntityAlignment.LEFT, length: float = 0, m: Matrix44 = None) list[Path]

Convert a single line string s into a list of Path objects. All paths are returned as a list of Single-Path objects. The text size is the height of the uppercase letter “X” (cap height). The paths are aligned about the insertion point at (0, 0). BASELINE means the bottom of the letter “X”.

Parameters:
  • s – text to convert

  • font – font face definition as FontFace object

  • size – text size (cap height) in drawing units

  • align – alignment as ezdxf.enums.TextEntityAlignment, default is LEFT

  • length – target length for the ALIGNED and FIT alignments

  • m – transformation Matrix44

ezdxf.addons.text2path.make_hatches_from_str(s: str, font: FontFace, size: float = 1.0, align=TextEntityAlignment.LEFT, length: float = 0, dxfattribs=None, m: Matrix44 = None) list[Hatch]

Convert a single line string s into a list of virtual Hatch entities. The text size is the height of the uppercase letter “X” (cap height). The paths are aligned about the insertion point at (0, 0). The HATCH entities are aligned to this insertion point. BASELINE means the bottom of the letter “X”.

Important

Returns an empty list for .shx, .shp and .lff fonts a.k.a. stroke fonts.

Parameters:
  • s – text to convert

  • font – font face definition as FontFace object

  • size – text size (cap height) in drawing units

  • align – alignment as ezdxf.enums.TextEntityAlignment, default is LEFT

  • length – target length for the ALIGNED and FIT alignments

  • dxfattribs – additional DXF attributes

  • m – transformation Matrix44

Entity Functions

class ezdxf.addons.text2path.Kind(value, names=_not_given, *values, module=None, qualname=None, type=None, start=1, boundary=None)

The Kind enum defines the DXF types to create as bit flags, e.g. 1+2 to get HATCHES as filling and SPLINES and POLYLINES as outline:

Int

Enum

Description

1

HATCHES

Hatch entities as filling

2

SPLINES

Spline and 3D Polyline entities as outline

4

LWPOLYLINES

LWPolyline entities as approximated (flattened) outline

ezdxf.addons.text2path.virtual_entities(entity: Text | Attrib, kind: int = Kind.HATCHES) EntityQuery

Convert the text content of DXF entities TEXT and ATTRIB into virtual SPLINE and 3D POLYLINE entities or approximated LWPOLYLINE entities as outlines, or as HATCH entities as fillings.

Returns the virtual DXF entities as an EntityQuery object.

Parameters:
  • entity – TEXT or ATTRIB entity

  • kind – kind of entities to create as bit flags, see enum Kind

ezdxf.addons.text2path.explode(entity: Text | Attrib, kind: int = Kind.HATCHES, target=None) EntityQuery

Explode the text entity into virtual entities, see virtual_entities(). The source entity will be destroyed.

The target layout is given by the target argument, if target is None, the target layout is the source layout of the text entity.

Returns the created DXF entities as an EntityQuery object.

Parameters:
  • entity – TEXT or ATTRIB entity to explode

  • kind – kind of entities to create as bit flags, see enum Kind

  • target – target layout for new created DXF entities, None for the same layout as the source entity.

ezdxf.addons.text2path.make_path_from_entity(entity: Text | Attrib) Path

Convert text content from DXF entities TEXT and ATTRIB into a Multi-Path object. The paths are located at the location of the source entity.

ezdxf.addons.text2path.make_paths_from_entity(entity: Text | Attrib) list[Path]

Convert text content from DXF entities TEXT and ATTRIB into a list of Path objects. All paths are returned as a list of Single-Path objects. The paths are located at the location of the source entity.