Skip to content

fix(entities): read pre-R2007 text in the code page the header names (§3.2.5, #370) - #374

Merged
DrunkOnJava merged 6 commits into
mainfrom
fix/legacy-codepage-decode
Oct 27, 2026
Merged

DrunkOnJava merged 6 commits into
mainfrom
fix/legacy-codepage-decode

Conversation

@DrunkOnJava

Copy link
Copy Markdown
Owner

Summary

Pre-R2007 text is decoded in the code page the file header names (§3.2.5) through one shared path, CodePage::decode, instead of as lossy UTF-8 at each reader. Closes #370.

Spec reference

  • §3.2.5 DWGCODEPAGE: "Bytes at 0x13 and 0x14 are a raw short indicating the value of the code page." v5.4.1 prints no table of its values, so the table is measured, not transcribed: header short against the $DWGCODEPAGE of AutoCAD's own export of the same drawing (examples/probe_code_page.rs). It names two rows, 29 = ANSI_1251 (14 drawings) and 30 = ANSI_1252 (2 drawings). Any other number is CodePage::Unknown(n) and its text is read exactly as before. Windows-1250, 1253 to 1258, 874, 932, 936, 949 and 950 are not covered: no pinned drawing gives a number for them, and none is taken from memory or from a third-party implementation (CLEANROOM.md entry added).
  • What changed: src/code_page.rs (new, encoding_rs 0.8.35: MIT or Apache-2.0, BSD-3-Clause index data; cargo deny passes; builds on MSRV 1.85 and wasm32). RawObject::code_page is stamped by the walker from DwgFile::code_page(); decode_from_raw* put it in force for the decode; the shared TV readers (tables, TEXT, ATTRIB, ATTDEF, MTEXT, TOLERANCE, IMAGEDEF, BLOCK, every object through objects::modern::read_tv), the class table, the header variables (Value::RawText only under an unnamed page) and the metadata sections read it back. dxf_header's $DWGCODEPAGE name now comes from the same table.
  • The page travels as a scoped thread-local and not as a parameter because about forty object decoders take only (payload, body_start, inline_end, version); a parameter would rewrite all of them and every open PR that adds one.

Evidence (real drawings, no unit tests)

The pinned corpus holds no pre-R2007 string with a byte above 0x7F: 3,418 / 2,846 / 2,812 strings decoded in the 8 AC1014 / 8 AC1015 / 8 AC1018 drawings, 0 with a high byte. The 94\U+00B0 of sample_AC1015's export is not one: the DWG's own MTEXT holds those nine ASCII characters (\U+00B0 is MTEXT's escape for a character outside the page), so old and new decode agree with the export. Issue #370's premise that the DWG holds a 0xB0 byte there does not hold.

So the path is proved on real drawings altered in memory (nothing written): 153 strings in 24 kinds of record of the 16 uncompressed drawings (AC1014 and AC1015) had their first four characters overwritten with E9 B0 C0 FC at the bit they sit at, and were read back under the drawing's own header short and under 29, 30 and 31. 612 of 612 reads are what the Windows-1251 (29: й°Аь), Windows-1252 (30: é°Àü) and unknown-page (31: kept as before, counted) rules say, using the code page charts' characters and not the decoder's; 51 of 51 table-entry names reach dwg-to-dxf as the matching \U+XXXX escapes (they were \U+FFFD). The probe's first run found one reader that takes a RawObject around the dispatcher (tables::dimstyle::decode_with_vars, DIMSTYLE in DXF TABLES, 0 of 8); it now puts the record's page in force itself (8 of 8).

Measured on the merged tree (main at 03e9e71 plus this branch), 2026-10-27, drawings under the pinned corpus (tools/corpus/pins.tsv: 52 drawings, 18 acadsharp and 84 nextgis files):

Gate Command Result
format, lint, docs cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features clean
tests cargo test --release --all-features 921 passed, 0 failed (the existing suites; none added)
MSRV, wasm cargo +1.85 check --all-features, cargo check --lib --target wasm32-unknown-unknown both build
supply chain cargo deny check advisories bans licenses sources (0.19.4), cargo metadata --locked for the root, wasm/ and fuzz/ pass
corpus counts measure.py run then check all 52 rows match the baseline: 15,527 decoded, 547 skipped, 0 errored; no baseline regenerated
oracle dxf_oracle <corpus> --baseline tools/oracle/baseline.tsv no agreeing count fell; the regenerated table is identical to the baseline (105,690 equal, 11,142 default, 283 different, 6,722 DWG-only)
value probes probe_header_vars, probe_dxf_header, probe_dxf_tables, probe_dxf_entities, probe_entity_colour, probe_text, probe_mtext 0 disagreements, exit 0
docs gates spec_coverage.py --check, arch.py check, tools/streaming/check_invariants.py pass
this probe probe_code_page <corpus> exit 0: 14 drawings 29 = ANSI_1251, 2 drawings 30 = ANSI_1252, 0 of 9,076 strings with a byte above 0x7F, 612 of 612 patched reads

Decoded, skipped and errored counts did not move, as expected: text content changes no field width.

Reproduce: python3 -I tools/corpus/fetch.py --dest ../dwg-corpus && cargo run --release --example probe_code_page -- ../dwg-corpus; the Corpus job runs it (a CI step is added). Evidence section: ARCHITECTURE.md 7ao.

Not in this change

  • The writers still emit strings as UTF-8; a page-aware encoder is the owed inverse (ARCHITECTURE 7an).
  • dxf_tables::dash_text (landed with feat(dxf): write the ENTITIES section of 15 kinds, paired with AutoCAD's export (#126) #358, merged here) takes the record's page (ltype_groups gets RawObject::code_page); the corpus's complex linetypes have ASCII text, so no data exercises that line.
  • XRECORD and extended entity data strings carry a code page of their own and are not decoded by the crate.

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 was read for §3.2.5.
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported.

My contribution is based on the ODA specification v5.4.1, AutoCAD's own DWG and DXF output in the pinned corpus, the Windows code page charts (for the expected characters of the probe), and the encoding_rs crate as a dependency. No ACadSharp or ezdxf code, comments or data were read for the code page numbers (CLEANROOM.md, 2026-10-27).

Developer Certificate of Origin

By submitting this PR, I certify that the contribution complies with the Developer Certificate of Origin 1.1.

… not as lossy UTF-8 (§3.2.5, #370)

One decode path, CodePage::decode, for every pre-R2007 string: the TV readers of tables, TEXT, ATTRIB, ATTDEF, MTEXT, TOLERANCE, IMAGEDEF, BLOCK and every object, the class table, the header variables and the metadata sections. The page is the header short at 0x13, stamped on each RawObject by the walker and put in force by decode_from_raw*. Two rows are measured against AutoCAD's exports (29 ANSI_1251, 30 ANSI_1252); any other number reads as before.
…ecode

# Conflicts:
#	.github/workflows/ci.yml
#	ARCHITECTURE.md
… on patched copies (#370)

The corpus holds no pre-R2007 string above 0x7F (0 of 9,076 strings in 24 drawings), so the decode is proved on 153 strings of 24 record kinds rewritten in memory: 612 of 612 reads match the Windows-1251 and Windows-1252 charts, and 51 table-entry names reach dwg-to-dxf as the matching escapes. Counts, oracle and probes did not move.
@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 🔄 Running since 2026-10-27T09:44:36.674298Z 4ccb983 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.

@DrunkOnJava

Copy link
Copy Markdown
Owner Author

Follow-up tracked in #375: code pages other than 1251/1252, a real-drawing proof (the pinned corpus has no pre-R2007 byte above 0x7F), R14, the writers' inverse, and XRECORD/XDATA strings. This PR stays scoped to the decode path and the two pages AutoCAD's exports name.

@DrunkOnJava
DrunkOnJava enabled auto-merge (squash) October 27, 2026 09:48
@DrunkOnJava
DrunkOnJava merged commit 0ec01f2 into main Oct 27, 2026
25 checks passed
@DrunkOnJava
DrunkOnJava deleted the fix/legacy-codepage-decode branch October 27, 2026 09:59
DrunkOnJava added a commit that referenced this pull request Oct 27, 2026
DrunkOnJava added a commit that referenced this pull request Oct 27, 2026
…red table from the baseline (#376)

* docs: bring README.md and RELEASE.md up to main, and render the measured table from the baseline

README.md still showed the 2026-10-26 measurement: 13,781 decoded, 308
errored, 85.7 %, with the errors tracked in four issues that are now
closed. main measures 15,527 decoded, 547 skipped and 0 errored on the
52 pinned drawings (96.6 %). The README now says that, lists the 30
skipped types with the open PR for each, gives the DXF value oracle's
totals, and brings the capability matrix, the streaming section, the
CLI list and the "does not do" list up to what has landed.

The table and the totals are rendered from tools/corpus/baseline.tsv by
tools/maintainer/readme_status.py (--write, --check). audit.py runs
the check, and docs/maintaining.md adds housekeeping item 10: refresh
the README after any landing that moves the baseline or ships a
capability. STATUS.md's coverage line uses the same blocks.

RELEASE.md cited a "#418" and "Q-07" that do not exist, listed five CLI
tools where release.yml builds seven, and promised "4 binaries on 3
platforms" where it ships seven on five targets. It now names the
twelve required checks and the README refresh as release steps.

* docs: the README names #374 as landed and #375 as what remains of code pages

* docs: render the DXF value oracle's totals from tools/oracle/baseline.tsv too

The README's oracle line was typed by hand and went stale when #351 landed:
105,690 equal fields became 105,756. readme_status.py now renders it from
the oracle baseline that CI pins, and --check covers it.

* fix(tools): readme_status requires every marker block in both documents

A STATUS.md edit that dropped or damaged its corpus-summary markers made
--check pass with stale numbers, and --write left them untouched. Each
document now lists the blocks it must carry. --check reports a block that
is missing or doubled, and --write refuses to run until it is fixed.

* docs: record #379 and #351, and re-render the numbers they moved

#379 decoded the last two skipped types that had no decoder in progress,
so every skipped type now has one in review, and #351 changed the
oracle's totals. readme_status.py re-renders both, and the CHANGELOG gets
the three 2026-10-27 entries the catch-up (#378) predates.
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.

Pre-R2007 strings are decoded as lossy UTF-8, not in the drawing's $DWGCODEPAGE

1 participant