Skip to content

feat(writer): write the drawing's code page, not UTF-8 or bare escapes (§3.2.5, #375) - #382

Merged
DrunkOnJava merged 5 commits into
mainfrom
feat/code-page-writers
Oct 27, 2026
Merged

DrunkOnJava merged 5 commits into
mainfrom
feat/code-page-writers

Conversation

@DrunkOnJava

Copy link
Copy Markdown
Owner

Summary

The inverse of #374 (#375, the writers' item): dwg-to-dxf and the DWG writer now write a pre-R2007 drawing's text in the drawing's own code page instead of UTF-8 or bare escapes. CodePage::encode_char and encode turn a character the page holds into its byte (two on a double-byte page) and any other into AutoCAD's \U+XXXX; convert_dwg_to_dxf_bytes (and convert_file_to_dxf_bytes, and the binary) write R12 to R2004 in that page; classes::write_tv encodes through the page in force; file_writer::assemble_dwg_bytes_with_page writes the page into the header (the short at 0x13) and DwgFile::to_bytes passes the drawing's own, so a rewritten file no longer reads its own text as an unnamed page (it wrote 0). convert_dwg_to_dxf still returns a String, so it keeps escapes only.

Spec reference

ODA spec v5.4.1 §3.2.5 (the code page short, via #374); ARCHITECTURE.md §7ar (provisional letter; the integrator renumbers).

Corpus measurement (tools/corpus/measure.py check, 52 pinned drawings): no decoded, skipped or errored count moves, so no baseline is regenerated; the DXF oracle matches tools/oracle/baseline.tsv.

Evidence (cargo run --release --example probe_code_page -- ../dwg-corpus, 2026-10-27; the 24 pre-R2007 drawings of the 52):

What Result
encode against decode, every byte above 0x7F of each named page 128 of 128 for ANSI_1251 and for ANSI_1252
Escapes: U+2205 and U+0439 under 1252, U+00E9 under 1251 and under an unnamed page, MTEXT's own 94\U+00B0 each as AutoCAD writes it
239 strings in 25 kinds of record, read under the drawing's own page, 29, 30 and 31, the R2004 drawings re-assembled by the DWG writer with that short as the header page 956 of 956 reads as the code page charts say
The 85 table-entry names written by dwg-to-dxf (R2000 target, bytes) 340 of 340 checks
DwgFile::to_bytes on the R2004 family 23 of 23 keep the header's page; the 8 pre-R2007 ones decode to the same entities (5 more are refused by the experimental writer for their section map, as before)
R2007 and later texts encoded in the R2000 save's page against the R2000 save's bytes (sample_AC1021, 1024, 1027, 1032) 59 of 59 each (one holds the diameter sign, written \U+220545,6 as AutoCAD wrote it)
convert_dwg_to_dxf_bytes against convert_dwg_to_dxf (R2000 and R2004 targets) 104 of 104 write the same bytes

What AutoCAD does is measured for two cases only, said so in ARCHITECTURE.md: an in-page character is its byte in a pre-R2007 DXF (one raw 0xD8 in a TEXT of the R12 export sample_AC1009_ascii.dxf; none in an R2000 or R2004 export), and a character the page lacks is \U+XXXX (the diameter sign U+2205: the R2007+ saves hold the character, the R2000 and R2004 saves hold the escape, in an MTEXT and an XRECORD string). Still owed: TEXT, ATTRIB or name text outside its page, MTEXT in an R2000 or R2004 DXF target (the ENTITIES writer does not write it yet), characters above the BMP, and a strings encoder for entities (the DWG writer writes no entity strings).

The first run of the bytes-against-String check read 0 of 104 and found a real defect: four 999 comments the writer itself emits in dxf_sections.rs held an em dash or a ≈, which a pre-R2007 DXF reads as bytes of the code page, not UTF-8. They are ASCII now, and a character above U+00FF that still reaches the bytes conversion becomes an escape, never ?.

README.md and STATUS.md are not refreshed here: tools/maintainer/readme_status.py is not on main yet (#376 adds it), so the integrator regenerates them after this lands.

Test plan

  • cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features, MSRV cargo +1.85 check --all-features, wasm32 cargo check --lib, cargo deny check advisories bans licenses sources pass locally on this head; cargo test --release --all-features ran green on the pre-rebase head and runs in CI on this one
  • Shown working on real files: probe_code_page over the 52 pinned drawings (numbers above); the DXF probes call convert_dwg_to_dxf unchanged and read 0 differences
  • python3 tools/ci/spec_coverage.py --check and python3 tools/maintainer/train/arch.py check pass
  • No new unit tests. Tests touched: none.

Source-provenance declaration

By submitting this PR I certify that my contribution does not incorporate executable code from any source whose license is incompatible with Apache-2.0. Specifically:

  • No Autodesk DWG SDK source (RealDWG, ObjectARX, ObjectDBX) or NDA-protected Autodesk documentation was consulted or imported.
  • No Open Design Alliance Teigha / Drawings SDK source was consulted or imported. The ODA's public Open Design Specification for .dwg files PDF is explicitly allowed (see CLEANROOM.md).
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported. This matters because dwg-rs is Apache-2.0 and must remain compatible.

My contribution is based on:

  • The ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1);
  • Public reverse-engineering notes, research papers, or blog posts cited inline;
  • Reading algorithm-description comments (not executable code) in permissively-licensed projects (MIT / Apache / BSD) to resolve spec ambiguities — disclose any such reading in this PR body so it can be recorded in CLEANROOM.md.

See CLEANROOM.md for the full allowed/forbidden source list and the honest scope of what "clean-room" means for this project.

For this PR specifically: the behaviours come from AutoCAD's own output alone (AutoCAD-written DWG and DXF files of the pinned public corpus). That a DXF before R2007 is read in $DWGCODEPAGE is the Autodesk public DXF Reference (recalled; dated entry in CLEANROOM.md). No code of any other DWG implementation was read.

Developer Certificate of Origin

By submitting this PR, I certify that the contribution complies with the
Developer Certificate of Origin 1.1.
Specifically, I affirm that the contribution is my own work (or rightfully
licensed to me) and that it can be distributed under the project's Apache-2.0
license.

I will sign off commits with git commit -s when asked, which adds a
Signed-off-by trailer to the commit message asserting the DCO.

…s (§3.2.5, #375)

CodePage::encode is decode's inverse (a character the page holds is its byte, any other the \U+XXXX escape AutoCAD's own R2000 and R2004 saves hold). convert_dwg_to_dxf_bytes and dwg-to-dxf write R12 to R2004 in the drawing's page, the class table writer encodes through the page in force, and the R2004-family header carries the page again, so DwgFile::to_bytes keeps the text readable.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T17:13:25.825064Z c685027 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

… it (#375)

The WHATWG encoders map a few extra code points onto bytes the decoder reads as another character (U+00A5 to 0x5C and U+203E to 0x7E on Shift_JIS, U+0080 on Shift_JIS, U+20AC on GBK). encode_char now returns the bytes only when decode gives the character back, so those characters take the escape and a written string reads as the text it was. The 128 of 128 round trip of each named page in probe_code_page is the same check over the whole page.
# Conflicts:
#	ARCHITECTURE.md
#	examples/probe_code_page.rs
…and extended data strings included (#375)

With #380 on main the probe also patches XRECORD and extended data strings, and the R2004 drawings through the DWG writer: 266 strings in 27 kinds, 1,064 of 1,064 reads, 348 of 348 DXF name checks; XRECORD values 24 strings 96 of 96, extended data 3 strings 12 of 12.
@DrunkOnJava
DrunkOnJava enabled auto-merge (squash) October 27, 2026 17:15
@DrunkOnJava
DrunkOnJava merged commit c1a58c9 into main Oct 27, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the feat/code-page-writers branch October 27, 2026 17:26
DrunkOnJava added a commit that referenced this pull request Oct 28, 2026
…10-27 (#416)

* docs: bring README, ROADMAP and CHANGELOG up to the landings of 2026-10-27

README's code-page and DXF writer text named work in review that has landed
(#371, #380, #382, #384); jev_review.py prose ranked those sentences 0.72 to
0.96. ROADMAP's "where it stands" table was a 2026-10-21 snapshot whose
numbers, CI row and release row were all out of date; it now points to the
rendered numbers instead of repeating them. CHANGELOG gains #371, #380, #382,
#384, #385 and #404.

* docs(changelog): record the CI-only merge train (#408)

* docs: OBJECTS landed (#372): README, ROADMAP and CHANGELOG

* docs: LAYOUT, GROUP and MLINESTYLE (#373), the class map writer (#405) and the double-byte round trip (#414) landed

* docs: VISUALSTYLE landed (#413)

* docs(readme): name the skipped types still in review without per-type counts; MATERIAL landed (#325)

* docs(readme): point to the open decoder PRs instead of a list that each landing makes stale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant