Skip to content

fix(dwg): preserve MTEXT caret-adjacent whitespace (#2250) - #2314

Merged
tanish2k09 merged 2 commits into
storytold:mainfrom
Zaxaerith:fix/2250-dwg-mtext-caret-space
Nov 2, 2026
Merged

tanish2k09 merged 2 commits into
storytold:mainfrom
Zaxaerith:fix/2250-dwg-mtext-caret-space

Conversation

@Zaxaerith

@Zaxaerith Zaxaerith commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An authored MTEXT string ^ C reopens from DWG as ^C (#2238 A12).

Root cause

Source tracing of the pinned acadrust 0.6.6 bridge identifies an asymmetric return conversion, rather than an extra caret decode in the native DWG reader:

Checkpoint Text for the minimal witness
Authored MText.contents ^ C
CADCraft bridge DXF group 1 ^ C (escape separator plus authored space)
acadrust DXF model ^ C
Native DWG value written/read ^ C
Previous acadrust ASCII DXF rendition ^ C
CADCraft decoding that rendition ^C

These checkpoints were traced in source, not executed locally. The new tests assert the native values and both conversion directions.

acadrust's ASCII reader decodes the bridge's caret escapes. Its ASCII writer treats a caret already followed by a space as escaped, so it does not add an independent separator for that literal space. CADCraft's existing MTEXT decoder correctly consumes the separator, exposing the missing escape. The return MTEXT writer also converts actual line feeds into paragraph codes.

Conversion boundary and changes

  • In cadcraft-dwg's DWG-to-DXF return path, serialize affected MTEXT group 1/3 contents from the native model by handle, using acadrust's existing DxfTextWriter::write_xrecord_string API for caret, tab and line-feed escaping.
  • Keep escape pairs together when splitting contents, retain the selected legacy code page or UTF-8, and apply the existing capped writer and panic boundary to the conversion.
  • Restrict replacement to the MTEXT record's own contents before group 101. Other records, embedded-object data and non-content groups are copied unchanged.
  • Leave CADCraft's ordinary DXF reader/writer and all third-party sources and dependencies unchanged. No trimming, first-save normalization, comparison tolerance, proprietary assets or new format markers.

Regression coverage

Added default-enabled synthetic tests, not executed locally:

  • 14 authored strings: ^ C, A ^ C B, repeated/trailing spaces, consecutive carets, literal ^I/^J, real tabs and line feeds, adjacent \\P, stacked text and chunked contents.
  • Exact contents and handle checks against the original Drawing after each of two saves across all six supported DWG versions (2000–2018).
  • Native DWG-text inspection before the return DXF conversion, plus explicit bridge-DXF and final read() checks. Native inspection uses the same acadrust codec; this is not independent-codec interoperability evidence.
  • DXF ASCII/binary controls across all six versions and two saves.
  • A focused bridge unit test for unchanged embedded-object/TEXT groups, legacy/UTF-8 text and preservation of the output cap.

Compatibility

The repair is limited to MTEXT content serialization on the DWG return bridge. It preserves literal carets separately from their following spaces and leaves ordinary DXF ^ , ^I and ^J decoding in its existing layer. Actual tabs/line feeds and existing paragraph formatting remain distinct.

Excluded: #2251 Unicode/MIF double decoding, #2247 group-48 scoping (PR #2276 is not included), #2248 static-column loss, broader #532 work, and other #2238 findings.

Verification

  • Passed: git diff --check, staged diff check, git show --check, strict UTF-8 text inspection and full diff review.
  • Reviewed statically: public API signatures, MTEXT/embedded-object scope, code-page selection, output limits, default test registration, and absence of new panic/unsafe/ignored-test/tolerance patterns.
  • No local Rust/Cargo compilation, tests, rustfmt, Clippy, xtask, or WASM builds were performed. Regression tests were added but not executed locally. CI and the full preservation matrix remain the verification gates.
  • This does not claim runtime success, formatting success, external-app rendering parity or completion of the Issue's unexecuted acceptance gates.

Focused checks for CI/maintainers (not run here):

cargo test -p cadcraft-io --test it mtext_caret_spaces
cargo test -p cadcraft-dwg mtext_contents_keeps_other_groups_and_the_output_limit

Related

Fixes #2250. Related: #2238 A12 and #2237.

At review time #2237 is still open, at ed37a1c8f83d7d995afacc4ace6dae32dd1f9483. Its roundtrip::dwg::extended::mtext_carets remains ignored and is absent from this branch's test tree. This PR neither cherry-picks its matrix nor changes that PR. Once the matrix is available with this fix, review/remove the corresponding ignore and run that family plus neighboring preservation families across their applicable save-version/encoding domains. Only in that environment is the following exact command applicable:

cargo test -p cadcraft-io --test it 'roundtrip::dwg::extended::mtext_carets' -- --ignored --exact

Prepared with AI assistance for @Zaxaerith.

Summary by CodeRabbit

  • Bug Fixes
    • MTEXT content now better preserves caret characters, surrounding spaces, tabs, and line breaks when saving and converting between DWG and DXF.
    • Text remains intact across repeated save-and-read cycles, including around long-text chunk boundaries and in supported DXF versions and formats.
    • DWG-to-DXF conversions preserve other drawing data and respect output size limits.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0ff1b03 and dfcbcf0.

Files selected for processing: 3
  • crates/dwg/src/lib.rs
  • crates/io/tests/it/main.rs
  • crates/io/tests/it/mtext_caret_spaces.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

DWG-to-DXF conversion now rewrites selected MTEXT group 1/3 content from native values. The rewrite preserves other DXF groups and uses code-page encoding for legacy versions. Integration tests cover caret and whitespace preservation through DXF and DWG save and read paths.

Changes: MTEXT content preservation

Layer / File(s) Summary
DWG-to-DXF MTEXT rewrite
crates/dwg/src/lib.rs
The conversion output rewrites affected MTEXT content while preserving other DXF groups and embedded-object data. Text is encoded for legacy versions and chunked within group limits. Tests cover serialization, preservation of surrounding records, and output-limit errors.
DXF round-trip coverage
crates/io/tests/it/main.rs, crates/io/tests/it/mtext_caret_spaces.rs
Fixtures and DXF tests check exact MTEXT contents across supported versions, text and binary formats, and repeated save/read passes.
DWG round-trip and bridge coverage
crates/io/tests/it/mtext_caret_spaces.rs
DWG tests check native stored text, DWG-to-DXF conversion, and DWG read-back. Bridge tests check caret-adjacent whitespace in DXF wire text and parsed output.

Priority: Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: vangel76

Merge Risk: Minimal · up to dfcbc

The MTEXT preservation change is mergeable after normal build and test checks; no specific regression was established.

Security architecture risk: Moderate · up to dfcbc

Large DWG files can now require both the original and rewritten DXF output in memory at once. That could exhaust a process even when each output stays within its size limit. The change does not add a public endpoint or grant new permissions.

Retained concerns

  • Medium · security · inferred: A DWG containing selected MTEXT can make the conversion retain its original, near-limit DXF buffer while constructing another near-limit buffer, increasing process-exhaustion risk on the existing file-read path.
Security review details

Security Blast Radius

  • inferred — A caller that opens an attacker-supplied DWG through the existing IO read function can reach the new allocation path. The evidence establishes process-level resource exposure, not tenant or service-wide reachability.

Security Findings and Attack Paths

  • inferred — A sufficiently large accepted drawing with at least one selected MTEXT value can cause the bridge to retain the first DXF buffer while allocating a replacement. Whether that exhausts a particular process depends on its available memory.

Trust Boundaries and Controls

  • observed — DWG bytes enter a failsafe third-party reader, then the rewritten DXF enters the repository's DXF parser. The size checks and error return limit individual outputs; the rewrite does not publish partial output after an error.

Resilience and Maintainability Implications

  • inferred — The rewrite uses conversion-local state and returns an error on incomplete DXF pairs or capped writes; there is no new persistent state to recover. Those error paths do not address exhaustion while both buffers are live.

Hardening Proposals

  • proposed — Bound aggregate conversion memory, for example by avoiding a simultaneous full-size source and destination DXF buffer or by applying a process memory budget where untrusted drawings are opened.
Pre-merge checks: Passed 3 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Linked Issues check Warning The implementation and default-enabled tests target #2250. mtext_contents replaces only MTEXT group 1/3 contents before group 101 and uses DxfTextWriter::write_xrecord_string; the tests cover lite… Run the listed focused tests and the applicable neighboring preservation families across the required save-version and encoding domains. Enable and run the corresponding mtext_carets regression when its test source is available. Record th…
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 3 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving whitespace adjacent to MTEXT caret characters in DWG handling.
Out of Scope Changes check Passed The changed DWG bridge code, test registration, and regression tests support #2250. The bridge limits replacement to MTEXT content groups and preserves embedded-object and unrelated records. No unrela…
Full details: Out of Scope Changes check

Explanation

The changed DWG bridge code, test registration, and regression tests support #2250. The bridge limits replacement to MTEXT content groups and preserves embedded-object and unrelated records. No unrelated feature, dependency, tolerance, normalization, or format-marker change is shown.

Full details: Linked Issues check

Explanation

The implementation and default-enabled tests target #2250. mtext_contents replaces only MTEXT group 1/3 contents before group 101 and uses DxfTextWriter::write_xrecord_string; the tests cover literal carets, whitespace, controls, chunking, versions, and two passes. However, #2250 requires the listed automated checks and the applicable neighboring preservation matrix to pass. The PR states that no Cargo tests were run, and the roundtrip::dwg::extended::mtext_carets regression from #2237 is absent from this branch. The required runtime verification is therefore incomplete.

Resolution

Run the listed focused tests and the applicable neighboring preservation families across the required save-version and encoding domains. Enable and run the corresponding mtext_carets regression when its test source is available. Record the passing results and link the resolution to #2238 A12.

Full details: Docstring Coverage

Explanation

Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 8 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@austin62jake austin62jake left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Windows 10 Pro x64 (Rust 1.99.1, cargo-nextest 0.9.150), with this PR merged onto current main (336243e). It merges cleanly, and it works. The description says some checkpoints were traced in source, not run, so here they are run:

  • dwg_mtext_caret_spaces_round_trip fails on main and passes with the fix. bridge_caret_space_is_escaped_once_in_each_direction, dxf_mtext_caret_spaces_round_trip and the existing paragraph_codes_tabs_and_carets_round_trip pass.
  • cargo xtask ci passes all 7 steps (2621 tests) with this PR merged onto main together with #2294, #2287, #2301 and #2335, which I tested at the same time, and #2198 so the gate runs on Windows.

With cadcraft-cli, I saved two MTEXTs as DWG 2004 and DWG 2018 and reopened them:

Text main This PR
^ C, a^b, x ^ y ^C, a^b, x ^ y unchanged
80 × é^ C (480 characters, so several group 3 chunks, and a code page in 2004) 400 characters (every ^ lost its space) unchanged

So the chunking keeps escape pairs together and the legacy code-page path works. The rewrite is limited to MTEXT records whose contents hold ^, a tab or a line feed, and it re-writes through the same CappedWriter, so the size cap still applies.

@ATARYS-LAB

Copy link
Copy Markdown
Contributor

Tested with synthetic drawings made by CADCraft, checked in AutoCAD 2023. A DWG opened and saved twice by 0.14.0 really loses the spaces in AutoCAD (^C, 10^3, 2 ^8, ^suite, ^^); the same file saved twice with this PR keeps ^ C, 10^ 3, 2 ^ 8, ^ suite, ^ ^, ^ deux. All other files read correctly. A DWG typed in AutoCAD 2023 (^ C, x^ 2) loses the space when read by 0.14.0, and saving it once makes the loss permanent; with this PR it is kept.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReviewerA — source-read COMMENT (not an approval)

I read the first hunk: the DWG to DXF conversion runs the DXF writer and then a new mtext_contents(&doc, &mut out) step inside the same catch_unwind, and per the title (#2250) MTEXT whitespace next to a caret is preserved (a ^ followed by a space is the DXF escape for a literal caret-space, and a trimmed space changed the text). Preserving whitespace is right, since text content shouldn't be altered by a converter. Two points: the closure now returns a Result from write_to_writer(...)? so check that a writer error is reported as before (not as a panic message), and that the second pass can't double-write the text. I did not read mtext_contents. Add a round trip for A^ B, trailing spaces, tabs and a caret at the end. Nothing was run.

I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.

Status: waiting_author.

@tanish2k09
tanish2k09 merged commit 0bc0259 into storytold:main Nov 2, 2026
6 checks passed
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.

DWG round trip consumes literal whitespace following an MTEXT caret

5 participants