Skip to main content

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

Common release commands are captured in justfile:

just test          # cargo test + Python pytest
just build         # build manylinux wheel and sdist into /tmp/easy_font_dist_publish
just publish       # run checks, rebuild artifacts, upload with pypi_token
just publish-built # upload existing artifacts from /tmp/easy_font_dist_publish

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")
bbox = font.text_bbox(text, height=12, tracking=0.1)
advance = font.text_advance(text, height=12, tracking=0.1)

# 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(), text_advance(), text_bbox(), 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

Metadata

Release files for cad-easy-font 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cad-easy-font 0.1.3
File Size Uploaded Provenance Actions
cad_easy_font-0.1.3.tar.gz 59.4 kB Without provenance Details for cad_easy_font-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cad-easy-font 0.1.3
File Interpreter ABI Platform Actions
cad_easy_font-0.1.3-cp39-abi3-manylinux_2_34_x86_64.whl CPython 3.9 abi3 Linux glibc 2.34+ x86-64 Details for cad_easy_font-0.1.3-cp39-abi3-manylinux_2_34_x86_64.whl

Total release size: 706.9 kB

Release files / cad_easy_font-0.1.3.tar.gz

Download URL cad_easy_font-0.1.3.tar.gz
Size 59.4 kB
Tags Source
SHA-256 checksum
How to use checksums
12f9bc697022e6f7004818a546ef6f59f87689d89b1cc68c1e793106106af1bb
BLAKE2b-256 checksum
How to use checksums
990c7d0d094da728415b9ce3f2dab811d8cc50152e7b62ca0c62203a3eb3af07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Release files / cad_easy_font-0.1.3-cp39-abi3-manylinux_2_34_x86_64.whl

Download URL cad_easy_font-0.1.3-cp39-abi3-manylinux_2_34_x86_64.whl
Size 647.5 kB
Tags CPython 3.9 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
132ae79ff483b2049492dfc64b91c929105be95e63b7f7e5cce428f502623aaa
BLAKE2b-256 checksum
How to use checksums
115b06e40244ffbc22555c3ab273d42351c9d55e999b637a57f91146d02013ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.12.6

Security for cad-easy-font 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page