Skip to main content

DXF text and AutoCAD SHX/BigFont support implemented with Rust and PyO3.

Project description

easy-font

easy-font is a PyO3/Rust Python extension focused on DXF text handling and AutoCAD SHX font compatibility. It intentionally starts smaller than ezdxf: the first milestone is reliable text/style extraction plus support for AutoCAD-86 SHX unifont and bigfont files found in CAD datasets.

Development

nix-shell
maturin develop
pytest

Python API

import easy_font as cad

font = cad.open_shx("cad-data/fonts/hztxt.shx", encoding="gbk")
print(font.kind, font.is_bigfont, font.glyph_count)
print(font.glyph_code_for_char("中"))  # GBK BigFont glyph code, e.g. 0xD6D0
print(font.missing_chars("中文ABC"))

doc = cad.readfile("cad-data/data/hatch.dxf")
for entity in doc.modelspace():
    if entity.dxftype == "TEXT":
        print(entity.text)

dwg = cad.readfile("cad-data/data/baseline-sample.dwg")
print(dwg.format, len(dwg.modelspace()))

# Resolve extracted text to local font files.
resolved_rows = cad.resolve_text_runs(dwg, ["cad-data/fonts"])
print(resolved_rows[0]["style"], resolved_rows[0]["font_path"])

Render one line of Chinese CAD text

CAD text styles often pair a primary ASCII SHX font with a BigFont for Chinese glyphs. FontStack keeps that split explicit, so ASCII such as -2# uses simplex.shx while Chinese text is encoded through the BigFont mapping.

from pathlib import Path

import easy_font as cad

fonts = Path("cad-data/fonts")
font = cad.FontStack(
    ascii_font=fonts / "simplex.shx",
    bigfont=fonts / "gbcbig.shx",
    encoding="gbk",
)

text = "明厚源悦府-2#"
print(font.glyph_code_for_char("明"))
print(font.encode(text))
print(font.missing_chars(text))

# Geometry in drawing units, already scaled to a 12-unit text height.
strokes = font.text_strokes(text, height=12, tracking=0.1, align="left")

# SVG path only, useful when embedding into a larger document.
path = font.text_svg_path("共1页,第1页", height=12, tracking=0.1, align="center")

# Complete SVG document and PNG bytes.
svg = font.render_text_svg(text, height=12, tracking=0.1, stroke_width=0.35, align="right")
Path("cad-text.svg").write_text(svg, encoding="utf-8")

png = font.render_text_png(text, height=120, stroke_width=3, align="left")
Path("cad-text.png").write_bytes(png)

For a single SHX font, the lower-level object also exposes glyph_code_for_char(), encode(), has_char(), missing_chars(), glyph_metrics(), text_strokes(), and text_svg_path().

Current scope

  • Reads ASCII DXF group-code pairs.
  • Reads DWG files using a handwritten Rust binary parser path.
  • Extracts STYLE table records, including primary SHX font and BigFont file.
  • Extracts common text entities from ENTITIES: TEXT, MTEXT, ATTRIB, and ATTDEF.
  • Recognizes AutoCAD-86 SHX unifont and bigfont headers.
  • Preserves raw SHX glyph records so BigFont files that ezdxf cannot load are still inspectable.
  • Maps Unicode characters back to encoded SHX glyph codes for BigFont files such as GBK/CP936.
  • Provides glyph metrics and high-level stroke/SVG/PNG helpers for one-line CAD text rendering.
  • Decodes DXF text escapes such as %%d, \P, and \U+4E2D.
  • Decodes encoded BigFont text bytes through Python codecs such as gbk, big5, and shift_jis.

Full DXF entity parity with ezdxf is out of scope for this initial implementation.

Font Resolution Workflow

python scripts/extract_and_resolve_fonts.py --input baseline-sample.dwg --limit 10

This script reads text entities from a drawing, resolves style fonts against cad-data/fonts, and prints style/text font coverage. For SHX fonts it verifies glyph coverage through open_shx; for TTF/OTF/TTC/WOFF it verifies cmap coverage when fontTools is available.

To validate real render output for SFNT fonts (TTF/OTF/TTC/WOFF/WOFF2) and save PNG previews:

python scripts/validate_render_output.py --input baseline-sample.dwg --max-rows 200 --preview-limit 20

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cad_easy_font-0.1.1.tar.gz (26.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cad_easy_font-0.1.1-cp39-abi3-manylinux_2_34_x86_64.whl (652.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

File details

Details for the file cad_easy_font-0.1.1.tar.gz.

File metadata

  • Download URL: cad_easy_font-0.1.1.tar.gz
  • Upload date:
  • Size: 26.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for cad_easy_font-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e78027f6efc7973fa27ef49b1d31e76b7d4ceea31c1ead746d5542777f82bcfa
MD5 385209df892abe6d7decff4dfdbf5e21
BLAKE2b-256 b35bd56d6fec12c5e88a29d5cd360c08267fc2c11f8a6f12c294df82a61d9228

See more details on using hashes here.

File details

Details for the file cad_easy_font-0.1.1-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cad_easy_font-0.1.1-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 458824af69dbc03623bff15641a08ee0bc2ab066de618b6deb8c396c86a48e5f
MD5 694c397018bb60bd2a18962c50c9f95b
BLAKE2b-256 f263ae239773ee62406acf1b24ee64a9ed6b3f9d3130433d4f803a75717b00cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page