Skip to content

fix(w2d): read AutoCAD-plotted DWF sheets to their end - #3

Merged
wybaby168 merged 3 commits into
flyfish-dev:mainfrom
AmirExpo:fix/w2d-real-plots
Nov 26, 2026
Merged

wybaby168 merged 3 commits into
flyfish-dev:mainfrom
AmirExpo:fix/w2d-real-plots

Conversation

@AmirExpo

Copy link
Copy Markdown
Contributor

We use dwf-viewer on a server to turn plotted DWF and DWFx drawings into PDF (its parser and Canvas2D renderers drawing into a PDF-backed canvas). On a set of 13 real AutoCAD-plotted drawings (8 DWF, 5 DWFx), 7 of the DWF sheets stopped early: the reader got through 0% to 48% of the W2D stream (one 99.8%) and the rest of the sheet was missing. These changes read all of them to the end, with no unsupported-opcode diagnostics. The drawings cannot be shared, so the regression tests build synthetic byte sequences.

What was wrong, and the fix

  1. Extended binary objects were skipped 2 bytes too far. The 4-byte size after { counts the 2-byte opcode, the payload and the closing } (WHIP! specification, "The Extended binary opcode"), so the object ends size - 2 bytes after the opcode. Skipping size bytes landed inside the next instruction, and the parser stopped at a meaningless "unknown opcode" (0xba, 0xa7, 0xfa in our files).
  2. Single-byte opcodes the parser did not know. Since an unknown single-byte opcode cannot be skipped, the first one stopped the parser (0x8d in four of our files). Added, with operand layouts from the WHIP! specification and the opcode table of Autodesk's DWF Toolkit (facts only, no Toolkit code):
    • contour set 0x0b (16-bit) / k (32-bit): one filled path primitive with fillRule: 'evenodd', so inner contours are holes;
    • Gouraud polyline 0x11 / q: segments in their vertex colours;
    • object node, auto-increment 0x0e (no operand);
    • macro draw / polymarker 0x8d / m / M, macro index G, macro scale S / s. Placements are read but not drawn; W2D_MACRO_NOT_DRAWN reports them, as info when the stream defines no symbol ((Macro ...)), as warning when it does. This keeps the parser fail-closed, as CONTRIBUTING.md asks.
  3. Call stack overflow on large sheets. primitives.push(...parsed.primitives) overflowed on Node 20 for sheets of about a million primitives (two of our files), and the page fell back to its thumbnail. It's a loop now.
  4. Absolute XPS part names. ImageSource (and FontUri) values like /Resources/scan.tif had their leading slash removed and were resolved against the page's folder, so raster images, such as a scanned sheet, were "ZIP entry not found". They now resolve from the package root, falling back to the old page-relative lookup.
  5. flatGouraud render option (new, off by default): fill each Gouraud triangle with the average of its vertex colours instead of rasterising it through ownerDocument.createElement('canvas'). It's for contexts that have no document, such as vector exports; the raster pixel budget doesn't apply to it. drawPrimitiveCanvas honours the path fillRule.

Tests: scripts/w2d-real-plots.test.mjs (7 tests), added to test:w2d and to the publish gate. node --test scripts/w2d-gouraud.test.mjs scripts/w2d-real-plots.test.mjs passes (18), and so does npm run validate:production.

Contributed under AGPL-3.0-only.

🤖 Generated with Claude Code

AmirExpo and others added 3 commits November 23, 2026 18:58
- Extended binary objects are skipped by size - 2: the size counts the
  2-byte opcode, the payload and the closing brace (WHIP! specification).
  Skipping the full size desynchronised the parser after the first one.
- Decode contour sets (0x0b, k), Gouraud polylines (0x11, q), automatic
  object nodes (0x0e) and symbol records (0x8d, m, M, G, S, s); symbol
  placements are reported, not drawn.
- Loop instead of push(...primitives): a million-item sheet overflowed
  the call stack on Node 20.
- Resolve absolute XPS ImageSource and FontUri part names from the
  package root.
- flatGouraud render option for contexts without a raster canvas, and
  an even-odd fill rule for paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Count only visible placements. Before revision 6.01 a placement draws a
built-in marker at the marker size (S/s; a single pixel at the default
0), so a sized marker is a visible loss (warning), as is any stream
that defines symbols ((Macro ...) or (Glyph ...)).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wybaby168
wybaby168 merged commit 6550099 into flyfish-dev:main Nov 26, 2026
2 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.

2 participants