# Browser dependency

WASM/glue rebuilt from npm @mlightcad/libredwg-web 0.8.2 sources with local patches.
Upstream source tag v0.8.2, commit 45a2a217f4916daddd7bd598c900d6c698d141b6.
Corresponding C/C++/TypeScript sources and upstream build scripts are in
source-v0.8.2.tar.gz. See its bindings/javascript/package.json and README.md.
The local patches/ and scripts/build_dwg_wasm.sh that turn it into the shipped
wasm/ files are also in ../../viewer-source.tar.gz.
License: GPL-3.0, COPYING. Upstream copyright notices remain in that archive.

The viewer-specific entry bundles the upstream modules without changing their
behavior (esbuild 0.25.12). Rebuild from the repository root:

npm exec --yes --package=esbuild@0.25.12 -- esbuild apps/dwg/static/vendor/libredwg/viewer-entry.js --bundle --format=esm --platform=neutral '--external:node:*' --outfile=apps/dwg/static/vendor/libredwg/viewer.js

Patched rebuild: `bash scripts/build_dwg_wasm.sh` downloads the v0.8.2 source
(SHA-256 checked), applies `patches/*.patch`, builds with the upstream
bindings/javascript flags except a 64 MiB initial heap, and re-bundles viewer.js.
The shipped wasm/ files come from this script (Emscripten 6.0.13; the bindings
are linked with em++). `01-eed-linear-memory.patch` stops XDATA-heavy drawings from needing GBs of memory;
`02-eed-drop-after-decode.patch` (enabled with `-DDWG_DECODE_DROP_EED`) frees EED the viewer never reads.
`03-read-from-memory.patch` adds `dwg_read_data()`, which the worker fills via `_malloc`/`HEAPU8`
instead of writing the file to MEMFS.
`04-compact-simple-entities.patch` and `05-presize-object-vector.patch` (enabled with `-DDWG_DECODE_COMPACT`)
reduce LINE/CIRCLE/ARC/ELLIPSE/SPLINE/POINT to 12-double records while decoding r2004+ files and free the
entities; the worker reads them with `dwg_compact_record()`.
`06-compact-spline-data.patch` keeps SPLINE knots, control and fit points in a pool so the worker can draw them.
After a rebuild, bump `ENGINE` in dwg-worker.js so cached engines are not mixed.
