File: text_lff_to_path.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (55 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (c) 2021-2023, Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf
from ezdxf import path, zoom
from ezdxf.fonts import fonts
from ezdxf.addons import text2path
from ezdxf.enums import TextEntityAlignment

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

# ------------------------------------------------------------------------------
# This example shows how to convert a text-string to stroke paths.
#
# docs: https://ezdxf.mozman.at/docs/addons/text2path.html
# ------------------------------------------------------------------------------

FONT = "standard.lff"


def main():
    doc = ezdxf.new()
    doc.layers.new("STROKE")
    doc.layers.new("TEXT")
    doc.styles.add("TXT", font="txt.shx")
    msp = doc.modelspace()

    attr = {"layer": "STROKE", "color": 1}
    ff = fonts.FontFace(filename=FONT)
    s = f'1234567890 AXxp written with stroke font "{FONT}"'

    lff_font = fonts.make_font(FONT, 1)
    text_path = lff_font.text_path(s)
    box = path.precise_bbox(text_path)
    msp.add_lwpolyline(
        box.rect_vertices(), close=True, dxfattribs={"color": 6}
    )
    align = TextEntityAlignment.LEFT
    path.render_splines_and_polylines(
        msp, text2path.make_paths_from_str(s, ff, align=align, size=1), dxfattribs=attr
    )
    msp.add_text(s, height=1, dxfattribs={"layer": "TEXT", "color": 2, "style": "TXT"})
    font_ = fonts.make_font(FONT, 1)
    bottom = font_.measurements.baseline
    msp.add_line((0, bottom), (30, bottom), dxfattribs={"color": 5})
    top = bottom + font_.measurements.cap_height
    msp.add_line((0, top), (30, top), dxfattribs={"color": 5})
    zoom.extents(msp)
    doc.saveas(CWD / "lff2path.dxf")


if __name__ == "__main__":
    main()