Skip to content

feat(objects): derive the LAYOUT + PLOTSETTINGS field list and dispatch it on R2004 and R2010+ - #52

Merged
DrunkOnJava merged 6 commits into
mainfrom
feat/layout-field-list
Aug 30, 2026
Merged

DrunkOnJava merged 6 commits into
mainfrom
feat/layout-field-list

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

LAYOUT was the largest undispatched non-entity class after #47 — 31 records across the corpus, every one Unhandled. Unlike VISUALSTYLE, the ODA spec does prescribe it, in one place: §20.4.84 LAYOUT opens with the complete plot-settings block (every row glossed plotsettings …) and continues into LAYOUT's own fields. There is no separate §20.4 PLOTSETTINGS entry, and no §19.6 chapter at all.

What this tree carried was neither. acad_layout cited a "§19.6.12 (L6-12)" that does not exist and listed a field order missing the six margin/paper BDs, the paper-size TV and the shade-plot triple; acad_plot_settings cited "§19.6.6 (L6-14)" and modelled three of the five TVs. Both citations and both field lists are withdrawn, replaced by one shared implementation (acad_plot_settings::read_fields) that LAYOUT and a standalone PLOTSETTINGS record both go through, so the two cannot drift apart.

Read straight and measured against the boundary, §20.4.84 closes every record on four release bands at once — no band declines.

The derived field list

<plot-settings block, §20.4.84>
TV   page_setup_name             (1)      -- string stream on R2007+
TV   printer_config_name         (2)      --   "
BS   plot_layout_flags           (70)
BD   margin_left                 (40)   mm
BD   margin_bottom               (41)   mm
BD   margin_right                (42)   mm
BD   margin_top                  (43)   mm
BD   paper_width                 (44)   mm
BD   paper_height                (45)   mm
TV   paper_size                  (4)      -- string stream on R2007+
2BD  plot_origin                 (46,47)
BS   paper_units                 (72)
BS   plot_rotation               (73)
BS   plot_type                   (74)
2BD  window_min                  (48,49)
2BD  window_max                  (140,141)
BD   real_world_units            (142)
BD   drawing_units               (143)
TV   current_style_sheet         (7)      -- string stream on R2007+
BS   standard_scale_type         (75)
BD   scale_factor                (147)
2BD  paper_image_origin          (148,149)
BS   shade_plot_mode             (76)   -- R2004+
BS   shade_plot_resolution_level (77)   -- R2004+
BS   shade_plot_custom_dpi       (78)   -- R2004+
<LAYOUT's own fields>
TV   layout_name                 (1)      -- string stream on R2007+
BL   tab_order                   (71)
BS   flags                       (70)
3BD  ucs_origin                  (13)
2RD  limmin                      (10)
2RD  limmax                      (11)
3BD  insertion_point             (12)
3BD  ucs_x_axis                  (16)
3BD  ucs_y_axis                  (17)
BD   elevation                   (146)
BS   ucs_ortho_view_type         (76)
3BD  extent_min                  (14)
3BD  extent_max                  (15)
BL   viewport_count                       -- R2004+; spec prints RL

The handle references §20.4.84 lists after viewport_count live in the object's handle stream, past the data-stream boundary, so they are not fields of the data record and the five Handle members AcadLayout used to carry are gone.

Bit-budget evidence per version

Every record's data fields must end exactly on the first bit of its string stream (R2010+) or on the RL object-data-size (R2004). examples/probe_field_list.rs prints the delta; delta 0 is the only passing result.

Band Records Boundary Result
R2004 (AC1018) × 3 9 RL obj-size, inline TVs delta 0 on all 9
R2010 (AC1024) × 3 9 string-stream start bit delta 0 on all 9
R2013 (AC1027) × 3 9 string-stream start bit delta 0 on all 9
R2018 (AC1032) × 1 4 string-stream start bit delta 0 on all 4

Per-record budgets, from the end of the common object prefix to the boundary:

File Handle Layout Budget (bits) viewport_count
arc_2004.dwg 111 Layout1 1090 0
arc_2004.dwg 115 Model 666 0
arc_2004.dwg 119 Layout2 706 0
arc_2010.dwg 111 / 115 / 119 Layout1 / Model / Layout2 808 / 400 / 424 0 / 0 / 0
arc_2013.dwg 111 / 115 / 119 Layout1 / Model / Layout2 808 / 400 / 424 0 / 0 / 0
sample_AC1032.dwg 34 Model 912 0
sample_AC1032.dwg 89 / 94 Layout1 / Layout2 1072 / 1072 2 / 2
sample_AC1032.dwg 602 MyLayout 1072 2

(circle_* and line_* are bit-identical to their arc_* siblings.)

Two places the spec and the bytes disagree

1. viewport_count is a BL, not the RL §20.4.84 prints. On sample_AC1032.dwg handle 89 the field starts 10 bits before the string-stream start bit. A 32-bit RL cannot fit; a BL in its 8-bit form is exactly 10 bits and decodes 2 — the number of viewports that layout owns. The 28 records with no viewport spend 2 bits there, where an RL would overrun the boundary by 30. A test builds a body with an RL in that slot and asserts the boundary check rejects it.

2. The R2013+ has_ds_binary_data flag carries 16 further bits. The spec's common-object-data table gives only the B. objects::modern consumed a single RC after it — an unvalidated guess that no test or corpus record had ever reached, because until now nothing that set the bit was dispatched. Exactly four non-entity records in the corpus set it — the four LAYOUTs of sample_AC1032.dwg, against 327 that clear it across the R2013 and R2018 files — and on all four the field list closes only with 16 bits consumed, the first byte 0x2C on every one (the second is 0x0B on Model, 0x00 on the three paper layouts). 8 bits leaves each record 8 short of its boundary; 0 bits leaves it 16 short.

Two readings fit the corpus: the marker is 16 bits wide, or it is absent and LAYOUT gained two leading bytes in R2018. This PR takes the first — the extra bits appear exactly when the flag is set and not on the R2013 records that clear it, the constant leading 0x2C reads as data-store bookkeeping rather than page-setup state, and the second reading would require §20.4.84 to be wrong at its very first field. Nothing in the corpus separates them outright; a non-LAYOUT record with the bit set would. Both readings are written down in src/objects/modern.rs rather than one being quietly assumed.

Value corroboration

A field list off by a single bit reproduces none of this:

  • paper_width × paper_height matches each record's own paper_size string: 215.9 × 279.4 mm for ANSI_A_(8.50_x_11.00_Inches) and Letter_(8.50_x_11.00_Inches) (8.5 × 11 in), 210 × 297 for A4;
  • all four margins are 6.35 mm (0.25 in) on the none_device records, ≈4.23 mm on the MON-MUR-09-PRINT (HP LaserJet Pro M148fdw) record;
  • the paper layouts' limmin/limmax are the sheet less those margins, in inches: (-0.25, -0.25) … (10.75, 8.25) on Letter, (-0.1667, -0.1667) … (11.526, 8.101) on A4;
  • every Model record carries AutoCAD's default (0, 0) … (12, 9); the A3 Layout1 of the R2004/R2010/R2013 files carries (0, 0) … (420, 297);
  • ucs_x_axis is (1,0,0) and ucs_y_axis (0,1,0) on all 31 records; ucs_origin is (0,0,0);
  • extent_min/extent_max hold AutoCAD's +1e20 / -1e20 uninitialised sentinel on the empty Layout1 records;
  • tab_order is 0 on each Model and 1/2/3 on the paper tabs, matching the layout_name strings;
  • scale_factor equals real_world_units / drawing_units on every record — 0.38686283977 = 1 / 2.5848954647 on the Model of sample_AC1032.dwg, 1 / 1 on its paper layouts;
  • shade_plot_custom_dpi is 300 and shade_plot_resolution_level 2 on all 31.

Versions declined

Per the honesty rule, bands whose layout is not measured return Error::Unsupported, which the dispatcher maps to Unhandled (never Error):

  • R13 / R14 / R2000 — §20.4.84 inserts a T plot_view_name (6) into the block for those releases and omits the shade-plot triple and viewport_count. Those files have no walkable object stream in this crate (Epic: Trace every ODA spec section #104), so the branch cannot be measured.
  • R2007 — the field list is presumably the R2004 one, but this crate cannot locate an R2007 object's string stream yet (Epic: Trace every ODA spec section #104).

Measured coverage

examples/coverage_report.rs over the 19-file local corpus, before = f706937 (main, after #46 + #47), measured in a clean worktree at that commit:

Corpus slice Before After
R2004 (AC1018) × 3 489 / 108 / 0 / 81.9 % 498 / 99 / 0 / 83.4 %
R2010 (AC1024) × 3 510 / 33 / 0 / 93.9 % 519 / 24 / 0 / 95.6 %
R2013 (AC1027) × 3 363 / 33 / 0 / 91.7 % 372 / 24 / 0 / 93.9 %
R2018 (AC1032) × 1 561 / 145 / 39 / 75.3 % 565 / 141 / 39 / 75.8 %
Aggregate 1923 / 319 / 39 / 84.3 % 1954 / 288 / 39 / 85.7 %

All 31 LAYOUT records decode; LAYOUT drops out of the unhandled histogram entirely. The error count is unchanged at 39 — the objects::modern prefix change is a no-op for every record that clears the AcDs bit, which is all of them outside these four.

API changes

  • DecodedEntity::Layout(Box<AcadLayout>) and DecodedEntity::PlotSettings(AcadPlotSettings) are new variants (the enum is #[non_exhaustive]; Layout is boxed to keep large_enum_variant quiet).
  • AcadLayout embeds an AcadPlotSettings and drops its five Handle fields. paper_min_limits / paper_max_limits / min_extents / max_extents are renamed to the spec's limmin / limmax / extent_min / extent_max; paper_width() / paper_height() become limits_width() / limits_height() (paper size proper now lives on the embedded plot settings); has_uninitialised_extents() is new and extents_diagonal() returns 0.0 for the sentinel.
  • AcadPlotSettings gains the twelve fields the old list was missing and printable_width() / printable_height() / custom_scale().
  • acad_layout::decode and acad_plot_settings::decode are replaced by decode_object(payload, body_start, inline_data_end, version), which takes the raw payload so it can reach the string stream.
  • examples/dump_decoded_entities.rs prints LAYOUT and PLOTSETTINGS values.

Tests

BitWriter-built R2018 fixtures in both modules: a full split-stream LAYOUT that closes on its string stream (name, tab order, viewport count, UCS axes, limits, and the embedded plot-settings values), a Model variant, a standalone PLOTSETTINGS record, plus four negative tests — an RL-sized viewport_count, a body missing the plot-settings block, a body one field long, and the declined release bands. Dispatch-level tests cover empty-payload → Error and undetermined-release → Unhandled. tests/code_table_agreement.rs and tests/dispatch_roundtrip.rs move 0x52 from the "no decoder" list to the routed list.

Source provenance

Clean-room declaration. I certify that:

  1. All code and tests in this PR were written from scratch by me.
  2. I consulted only sources listed as allowed in CLEANROOM.md — specifically the freely-redistributable ODA Open Design Specification for .dwg files v5.4.1 (§20.4.84 LAYOUT, and the common-object-data table for the R2013+ AcDs flag) and first-party byte inspection of publicly-available DWG sample files. I did not consult Autodesk SDK source, ODA SDK (Drawings SDK / Teigha) source, LibreDWG source, decompilations of Autodesk or ODA binaries, or any copyleft-licensed DWG implementation code.
  3. Every field name, type and DXF group code in the two modules comes from §20.4.84's own table. No Autodesk DXF Reference vocabulary was consulted or needed for this object — unlike the VISUALSTYLE slot names disclosed in CLEANROOM.md for feat(objects): derive the ACAD_VISUALSTYLE field list from the data-stream boundary and dispatch it on R2010+ #47, which had no spec prescription to draw on. The two departures from the spec (viewport_count as BL, the 16-bit AcDs trailer) are derived from the corpus's own data-stream boundary and are documented as measurements in the modules.
  4. CLEANROOM.md carries a dated entry recording all of the above and withdrawing the two non-existent §19.6.x citations.

Closes #50
Closes #39


Note

Medium Risk
Touches shared R2013+ common-object prefix parsing and breaks AcadLayout/AcadPlotSettings APIs, but behavior is gated by boundary checks, corpus validation, and explicit unsupported release bands.

Overview
Implements LAYOUT and standalone PLOTSETTINGS using ODA §20.4.84 via a shared acad_plot_settings::read_fields, with boundary-checked decode_object paths through objects::modern. All 31 corpus LAYOUT records decode on R2004/R2010/R2013/R2018; aggregate coverage rises from 84.3% to 85.7%.

Spec vs bytes: viewport_count is read as BL (not spec RL); when R2013+ has_ds_binary_data is set, objects::modern now consumes 16 bits (was one RC) so R2018 LAYOUTs close on the string-stream boundary.

Dispatch/API: DecodedEntity::Layout / PlotSettings; LAYOUT 0x52 routed in dispatch_object; custom PLOTSETTINGS by class name. AcadLayout embeds AcadPlotSettings, drops data-stream handles, renames limits/extents fields; pre-R2004 and R2007 return Unsupported → Unhandled.

Docs, CLEANROOM, coverage tables, dump_decoded_entities, and integration tests updated accordingly.

Reviewed by Cursor Bugbot for commit 2555331. Configure here.

…tream boundary and dispatch it on R2010+

The ODA v5.4.1 object-prescription chapter §20.4 stops at XRECORD and
carries no entry for VISUALSTYLE or MATERIAL; both modules cited
sections (§19.6.10, §19.6.9) that do not exist. The field list here was
derived instead from the boundary the format itself provides: searching
for one token sequence over B/BS/BL/BD/CMC that lands all 24
VISUALSTYLE records of a file exactly on its string-stream start bit
returns exactly one answer per release band — a fixed head plus 28
(value, flag) pairs on R2010, the same 28 plus 30 more on R2013, which
also closes on all 24 R2018 records.

R2004 and R2007 store the same styles without the per-property flags
and with a different property count; no sequence lands their records on
the RL object-data-size boundary, so those bands return
Error::Unsupported and the dispatcher now maps an Unsupported from an
object-class decoder to Unhandled rather than Error.

ACAD_MATERIAL withdraws its previous field list, which was wrong from
its first field, and keeps only what the bytes prove: the strings (now
read from the R2007+ string stream instead of inline) and the measured
data-field budget. It stays undispatched.

Measured on the 19-file corpus: 1751 / 484 / 46 / 76.8 % → 1919 / 316 /
46 / 84.1 %.

Refs #38
# Conflicts:
#	CHANGELOG.md
#	README.md
#	STATUS.md
#	examples/dump_decoded_entities.rs
#	src/entities/dispatch.rs
#	src/objects/mod.rs
…ch it on R2004 and R2010+

LAYOUT was the largest undispatched non-entity class after #47 — 31
records across the corpus, all Unhandled. Unlike VISUALSTYLE, the spec
does prescribe it: §20.4.84 opens with the whole plot-settings block
(every row glossed "plotsettings ...") and continues into LAYOUT's own
fields. What the crate carried was neither — acad_layout cited a
"§19.6.12 (L6-12)" that does not exist (there is no §19.6 chapter) and
listed a field order missing the six margin/paper BDs, the paper-size
TV and the shade-plot triple; acad_plot_settings cited "§19.6.6
(L6-14)" and modelled three of the five TVs. Both citations and both
field lists are withdrawn and replaced by one shared implementation.

Read straight and measured against the data-stream boundary, §20.4.84
closes every record on four release bands at once: 9 on the R2004
files (inline TVs, RL boundary), 9 on R2010, 9 on R2013 and 4 on
sample_AC1032.dwg, delta 0 with no bits to spare. The boundary then
catches the two places the prescription and the bytes disagree:

- viewport_count is a BL, not the RL §20.4.84 prints. On handle 89 of
  sample_AC1032.dwg it starts 10 bits before the string-stream start —
  an RL cannot fit, a BL in its 8-bit form is exactly 10 bits and
  decodes 2. The 28 records with no viewport spend 2 bits there.
- The R2013+ has_ds_binary_data flag carries 16 further bits. The spec
  gives only the B; objects::modern consumed a single RC after it, an
  unvalidated guess no record had reached. Four corpus records set the
  bit — the four LAYOUTs of sample_AC1032.dwg, against 327 that clear
  it — and all four close only with 16 bits consumed, first byte 0x2C
  on every one. The alternative reading is recorded, not hidden.

Corroboration from the values: paper size matches its own name string
(215.9x279.4 mm for ANSI_A, 210x297 for A4), margins 6.35 mm on the
none_device records, paper limits equal to the sheet less those margins
in inches, (1,0,0)/(0,1,0) UCS axes on all 31, AutoCAD's +/-1e20
uninitialised-extents sentinel on the empty layouts, and scale_factor =
real_world_units / drawing_units everywhere.

Pre-R2004 and R2007 decline with Error::Unsupported rather than guess.

Measured on the 19-file corpus: 1923 / 319 / 39 / 84.3 % → 1954 / 288 /
39 / 85.7 %.

Closes #50
Closes #39
@cursor

cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot 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_6ec3c906-2211-4c21-a0a7-67fc227bfd8f)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant