Repository navigation
fix(entities): MTEXT and friends read their TV fields from the R2007+ string stream - #28
Merged
Merged
Conversation
…y common object data ODA v5.4.1 §19.1 puts an `RL` "object data size in bits" between the object type and the object handle for the whole R2000..R2007 band; R2010+ replaces it with the leading `MC` handle-stream size. The reader consumed it for R2000 alone, leaving every AC1018 record 32 bits out of phase from the handle onwards — ~20 `Bit cursor exhausted` errors per R2004 sample. Also read the §19.4.2 common object data (EED chain, `BL` reactor count, xdictionary flag) before the pre-R2007 symbol-table decoders, which started at the entry name with the cursor still on the EED chain; fix the legacy STYLE decoder's `RC`-for-two-`B`-bits overread; and give the legacy VPORT decoder the same field table as the already-verified R2007+ split-stream decoder. samples/ corpus: R2004 15 decoded / 60 errored / 2.5% -> 75 / 0 / 12.6%; aggregate 527 / 94 / 23.1% -> 587 / 34 / 25.7%. All remaining errors are in the single R2018 sample. Closes #24
…he R2007+ string stream MTEXT decoded silently wrong on R2007+ — it read its TV from the data cursor, which at that position holds the next data field, so it returned a one-character garbage string for every record and never errored. It now reads through the object's string stream. Its only TV is the text, so the stream must hold exactly one string and nothing else; that makes "the reader is exhausted after one read_tv" a self-validating check, measured exact across all 22 MTEXT records of sample_AC1032.dwg. TOLERANCE gets the same port and is fully self-validating (its text is the last data field, so the fields must end exactly on the string-stream start bit); the measurement also shows its BS unknown_short / BD height / BD dimgap fields are absent on R2018. DIMENSION and HATCH take their TV fields from the string stream so every field after them keeps alignment. StringReader::empty covers records whose "strings present" bit is clear. Layer::is_locked tested 0x04, which is frozen-in-new-viewports in the DWG values word; locked is 0x08. Adds is_frozen_in_new_viewports. samples/ corpus: R2018 374 decoded / 34 errored / 50.2% -> 384 / 24 / 51.5%; aggregate 587 / 34 / 25.7% -> 597 / 24 / 26.2%. Closes #25 Closes #26
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_ff900666-66c5-4924-a6f3-736b6d0ca46f) |
This was referenced Aug 30, 2026
# Conflicts: # ARCHITECTURE.md # CHANGELOG.md # README.md # STATUS.md
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_92cd039e-51e8-49f2-9aa6-61073a250558) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MTEXT(0x2C) read itsTVfrom the data cursor. On R2007+ that position holds the next data field, so MTEXT never errored — it returned"ѕ", a single UTF-16 code unit built from whatever two bytes followedextents_width, for every record in the file. Silent wrong is worse than an error: the coverage report counted all 33 records as decoded.This ports MTEXT to the split-stream layout, audits every other
TV-bearing decoder for the same failure mode, fixes the cheap ones (TOLERANCE, HATCH, the DIMENSION family), and fixes #26 in the same area.Spec reference
§19.1 split streams / §19.4.44 MTEXT.
src/entities/mtext.rsgainsdecode_modern_split_stream, wired intodispatch_split_stream_entityalongside the #23 ports.Self-validating: MTEXT's string stream holds exactly one
TVtextis MTEXT's only variable-text field, so its string stream must contain one string and nothing else. Measured across all 22 MTEXT records insample_AC1032.dwg's main object stream, the stream length equals that string's encoded length to the bit, with no padding:"Text""Table sample""Sample annotation""this is a Mtext\nwith multiple lines in it"So the decoder requires (a) the data fields not to run past the string-stream start bit and (b) the reader to be exhausted after one
read_tv. Either failing is an error, not a plausible-looking string.Measured:
background_scale_factoris aBD, not aBLHandle
0x6D8(text"\A1;7.0711") is the one record in the corpus with the background bit set. Reading the field as aBLleaves the followingCMCon a 16-bit code yielding colour index 768 and then a reserved11pattern; reading aBDrecovers1.25(bytes00 00 00 00 00 00 F4 3Fat bit 559) and the record decodes.Not decoded: the R2018 MTEXT tail (documented, not guessed)
The fields this decoder understands end 567 bits short of the string stream on the first MTEXT of
sample_AC1032.dwg(data fields end at bit 480, string stream starts at 1047). Scanning that gap for the doubles already read shows the record repeats them, each as aBDwith the two-bit00full-double prefix:insertion.xinsertion.yrect_widthextents_widthextents_heightrect_widthagainThat is consistent with the R2018 annotative / column block restating the geometry, but the field boundaries between the repeats are not established from bytes, so per
CONTRIBUTING.mdthe offsets are documented and the decoder asserts the weaker<= string_startbound instead of guessing.examples/probe_mtext_fields.rsreproduces it.Verification on
sample_AC1032.dwg— a sample of what MTEXT now returns (was"ѕ"for all of them):The
TV-field auditexamples/audit_string_streams.rs(new) walks every object in a file, locates its string stream and prints the strings it holds. A type whose records carry strings there but whose decoder readsTVfrom the data cursor is silently wrong on that file, and the printed strings are what it should have returned. Census onsample_AC1032.dwg:"Hello this is a single line text"tag="ATTINFO" value="17"tag="ATTINFO" prompt="Enter number:"TVread lands on the string stream. Names decode as*Model_Space,_ArchTick,*D3. Not a counter-example to the split layout; noted inARCHITECTURE.md§7c.{\Fgdt;j}%%v{\Fgdt;n}%%v…,99\na,{\Fgdt;p}user_textfrom the stream keeps every later field aligned; 10 errors → 2TVat all; the block name comes through a handleTVTVfieldsTV; also never dispatchedmtext_default_textread inlineMLeader text, hello!,MULTILEADER TEST,MULTILEADER\PTEST\P123. Not cheap: MLEADER also reads threeHhandles from the data stream, which on R2007+ live in the handle stream, so the record is misaligned before theTV.file_pathread inline.\image.JPG. Not reachable fromdecode_from_raw(custom classes dispatch IMAGE, not IMAGEDEF), so it is only wrong through the direct public API.ACAD_COLOR,ACAD_CIP_PREVIOUS_PRODUCT_INFO, … Not reachable fromdecode_from_raw(non-entity, non-table →Unhandled); wrong only throughcrate::objects::dictionary::decode.ANSI_A_(8.50_x_11.00_Inches),none_deviceSTANDARD,1:1,Flat,strokes_ogs.tifread_tvignores the version entirely and always reads 8-bit bytes, so it is wrong on R2007+ even before the split-stream questionsrc/entities/light.rs:139TVreadsFixed in this PR: MTEXT, TOLERANCE, DIMENSION, HATCH. Left for follow-ups, listed with evidence above: MLEADER, IMAGEDEF, LIGHT, GEODATA, and the non-entity object decoders (DICTIONARY, LAYOUT, GROUP, MLINESTYLE, MATERIAL, VISUALSTYLE, SCALE, PLOTSETTINGS, PROPERTYSET).
Structural finding: a record with no string stream still has its
TVslotsSome R2007+ records have the strings present trailer bit clear. Their
TVslots still exist in the field table and still consume no data-stream bits, so falling back to an inline read there shifts every following field.StringReader::emptygives those records a reader that yields"", andmodern::open_entityreturns it with the handle-stream start as the bound. That alone removed two errors (one HATCH, one DIMENSION).#26 —
Layer::is_lockedtested the wrong bitThe DWG
valuesword (§20.4.53) is not the DXF group-70 flag word. DXF puts locked at0x04; DWG puts frozen in new viewports there and locked at0x08. Sois_lockedreported every layer frozen-in-new-viewports as locked, and every genuinely locked layer as unlocked.is_lockednow tests0x08(LAYER_VALUE_LOCKED).is_frozen_in_new_viewportstests0x04(LAYER_VALUE_FROZEN_IN_NEW) so the bit is not lost.is_frozenandis_lockednow use the named constants rather than repeating literals.decode_modern_split_streamdoc comment that recorded the bug is updated.layer_with_flagstest now writes0x0D(frozen + frozen-in-new-viewports + locked) and asserts all three; a newfrozen_in_new_viewports_is_not_lockedtest pins that0x04alone is not locked.Measured coverage — before / after
cargo run --release --example coverage_report -- <samples>over the 19-file corpus. "Before" is the state after #27 (this branch's base).Error histogram on
sample_AC1032.dwg, before → after:Note the MTEXT count is unchanged at 1 because the previously "decoded" records were silently wrong, not erroring — the win is 33 records of real text, not a count change.
Test plan
cargo fmt --all -- --checkcargo clippy --all-targets --all-features -- -D warningscargo test --release— 669 lib tests (was 666; +3) + all integration suitescargo deny check allRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-featurescargo +1.85.0 test --no-run(MSRV, no let-chains)New tests:
entities::mtext::tests::r2018_split_stream_mtext_reads_text_from_string_stream— aBitWriter-built R2018 payload (common entity preamble + §19.4.44 field body + string stream + trailer) read back throughdecode_modern_split_stream, asserting the text and every geometry field.entities::mtext::tests::r2018_split_stream_mtext_rejects_a_short_body— a truncated field list must error rather than return a plausible string.tables::layer::tests::frozen_in_new_viewports_is_not_locked— Layer::is_locked tests bit 0x04, but the LAYER values word puts locked at 0x08 (0x04 is frozen-in-new-viewports) #26's regression pin.tests/canonical_corpus.rsvalue pins and the synthetic fixtures are untouched by this branch.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:
This contribution is based on the ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1) and on first-party byte inspection of publicly-available sample DWG files, reproduced by the two probes under
examples/. No permissively-licensed third-party comments were consulted, so noCLEANROOM.mddisclosure entry is needed.Closes #25
Closes #26
Note
Medium Risk
Changes bit-level entity decoding and public
Layerflag semantics on real R2007+ files; misalignment checks reduce silent garbage but wrong field layouts could still surface as new errors rather than wrong values.Overview
Fixes silent wrong text on R2007+ where decoders still read
TVfrom the data stream instead of the per-object string stream (ODA §19.1). MTEXT previously returned a one-character garbage string for every record without erroring; it now usesdecode_modern_split_stream, validates a single exhaustedTVin the string stream, and treatsbackground_scale_factorasBD(notBL).TOLERANCE, HATCH (pattern + gradient names), and the DIMENSION family (
user_text) follow the same split-stream path viamodern::read_tv_fieldand newdecode_modern_split_streamentry points, wired fromdispatch_split_stream_entity. Shared plumbing addsStringReader::emptyandopen_entityhandling when the strings-present bit is clear (TV slots exist but consume no data bits).Also fixes #26:
Layer::is_lockednow tests DWG bit0x08(not DXF-aligned0x04); addsis_frozen_in_new_viewportsfor0x04.Adds
examples/audit_string_streams.rsandprobe_mtext_fields.rs; docs and coverage stats move to 597/24/26.2% aggregate (384/24/51.5% on R2018).Reviewed by Cursor Bugbot for commit eac228d. Configure here.