emf-converter 1.4.3 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,29 +51,53 @@ Both functions return `Promise<string | null>` — `null` if the buffer is inval
51
51
 
52
52
  ## API
53
53
 
54
- ### `convertEmfToDataUrl(buffer, maxWidth?, maxHeight?)` · `convertWmfToDataUrl(buffer, maxWidth?, maxHeight?)`
55
-
56
- | Parameter | Type | Description |
57
- | ----------- | ------------------------- | --------------------------------- |
58
- | `buffer` | `ArrayBuffer` | Raw EMF/WMF file bytes |
59
- | `maxWidth` | `number` (optional) | Maximum output width in pixels |
60
- | `maxHeight` | `number` (optional) | Maximum output height in pixels |
61
- | **Returns** | `Promise<string \| null>` | PNG data URL or `null` on failure |
54
+ ### `convertEmfToDataUrl(buffer, maxWidth?, maxHeight?, options?)` · `convertWmfToDataUrl(buffer, maxWidth?, maxHeight?, options?)`
55
+
56
+ | Parameter | Type | Description |
57
+ | ----------- | ------------------------------------- | ---------------------------------------------------- |
58
+ | `buffer` | `ArrayBuffer` | Raw EMF/WMF file bytes |
59
+ | `maxWidth` | `number` (optional) | Maximum output width in pixels |
60
+ | `maxHeight` | `number` (optional) | Maximum output height in pixels |
61
+ | `options` | `EmfConvertOptions \| number` (opt.) | Options object, or a numeric `dpiScale` (legacy) |
62
+ | **Returns** | `Promise<string \| null>` | PNG data URL or `null` on failure |
63
+
64
+ #### `EmfConvertOptions`
65
+
66
+ | Field | Type | Default | Description |
67
+ | -------------------- | -------------------------- | -------------- | --------------------------------------------------------------------------- |
68
+ | `maxWidth` | `number` | — | Maximum output width in pixels (aspect ratio preserved) |
69
+ | `maxHeight` | `number` | — | Maximum output height in pixels |
70
+ | `dpiScale` | `number` | `1` | Resolution multiplier for sharper output; clamped to `4` |
71
+ | `maxCanvasDimension` | `number` | `8192` | Hard cap on canvas width/height in pixels |
72
+ | `maxRecords` | `number` | `200000`/`500000` | Cap on records processed per stream before replay stops (EMF+ uses the higher default unless overridden) |
73
+ | `fontFamilyMap` | `Record<string, string>` | — | Maps Windows face names (case-insensitive) to fonts available locally, e.g. `{ calibri: 'Carlito' }` |
74
+
75
+ ```ts
76
+ const png = await convertEmfToDataUrl(buffer, undefined, undefined, {
77
+ dpiScale: 2,
78
+ fontFamilyMap: { calibri: 'Carlito', 'ms shell dlg': 'Tahoma' },
79
+ });
80
+ ```
62
81
 
63
82
  ## How it works
64
83
 
65
- A three-phase pipeline: **parse → replay → export**. The header parser extracts the drawing bounds, a Canvas is created and clamped to 4096×4096, then records are scanned sequentially and dispatched to GDI, EMF+, or WMF handlers that drive the Canvas 2D context. Embedded bitmaps (DIB and GDI+ pixel formats) and recursively embedded metafiles are resolved asynchronously after the synchronous replay completes.
84
+ A three-phase pipeline: **parse → replay → export**. The header parser extracts the drawing bounds, a Canvas is created and clamped to 8192×8192 (configurable via `maxCanvasDimension`), then records are scanned sequentially and dispatched to GDI, EMF+, or WMF handlers that drive the Canvas 2D context. Embedded bitmaps (DIB and GDI+ pixel formats) and recursively embedded metafiles are resolved asynchronously after the synchronous replay completes.
85
+
86
+ It supports 300+ EMF GDI record types, the EMF+ (GDI+) record set, and legacy WMF records, including state, transforms, objects, shapes, poly/path operations, text, bitmaps, gradients, raster operations, and clipping:
66
87
 
67
- It supports 300+ EMF GDI record types, the EMF+ (GDI+) record set, and legacy WMF records, including state, transforms, objects, shapes, poly/path operations, text, bitmaps, and clipping.
88
+ - **Clip regions with full boolean combine modes** — the converter tracks the active clip as a list of path shapes, so `Intersect`, `Union`, `Xor`, `Exclude`, `Complement`, and `Replace` combine modes work for `EMR_INTERSECTCLIPRECT` / `EMR_EXCLUDECLIPRECT` / `EMR_EXTSELECTCLIPRGN` (all `RGN_*` modes), the EMF+ `SetClipRect` / `SetClipPath` / `SetClipRegion` records (all `CombineMode` values, including nested region-node trees), and clip translation via `EMR_OFFSETCLIPRGN` / EMF+ `OffsetClip`. Subtraction and symmetric difference are expressed through even-odd fill-rule clipping, which Canvas 2D cannot do with plain `clip()` stacking.
89
+ - **Gradient brushes** — GDI+ linear gradients render as Canvas linear gradients with their full colour-stop list (preset blend colours and blend factors are expanded into stops, and the optional brush transform rotates the gradient axis). Path gradients render as radial gradients from the centre colour to the surrounding colour across the boundary radius.
90
+ - **Raster operations (ROP2)** — every `SetROP2` mode is mapped: `R2_BLACK`, `R2_WHITE`, `R2_NOP`, `R2_COPYPEN`, `R2_NOTCOPYPEN`, and `R2_NOT` are emulated exactly (via colour inversion and `difference` compositing); the remaining bitwise AND/OR/XOR-family modes are approximated with the nearest arithmetic composite (`difference` / `darken` / `lighten`), combined with pen-colour inversion for the NOT variants.
91
+ - **GDI world transforms** — the scale and translation set by `EMR_SETWORLDTRANSFORM` / `EMR_MODIFYWORLDTRANSFORM` are applied to all GDI drawing, which is required for GDI+-exported EMF files (they record coordinates at 16× sub-pixel precision with a compensating transform). EMF+ records support the full affine transform set.
68
92
 
69
93
  ## Limitations
70
94
 
71
- - **EMF+ region objects** are not parsed (no Canvas 2D equivalent for boolean region clipping).
72
- - **Gradient brushes are simplified** — GDI+ linear/path gradients use the primary colour only.
73
- - **No raster operations (ROP)** — `SetROP2` blend modes are not applied.
74
- - **Limited clipping** — single rect/path clipping is supported; combined regions are not.
75
- - **Safety limits** — output is clamped to 4096×4096; processing stops after 50,000 records (EMF/WMF) or 100,000 (EMF+).
76
- - **Font rendering** uses the browser's font engine, so glyph metrics may differ from Windows GDI.
95
+ - **Approximated edge cases in region ops** — all six combine modes are exact while the tracked clip is at most one composable shape (the overwhelmingly common case). When the clip is already an intersection of several shapes, or was set from a live path bracket (`EMR_SELECTCLIPPATH`), `Union` / `Xor` / `Complement` degrade to the nearest conservative approximation (a console log notes when this happens).
96
+ - **Bitwise ROP2 modes are arithmetic approximations** — Canvas compositing cannot reproduce true bitwise AND/OR/XOR against the destination, so those modes use `darken` / `lighten` / `difference` stand-ins; only the modes listed above as exact are pixel-faithful.
97
+ - **Gradient details** — gradient wrap/tile modes clamp instead of tiling, path gradients are radial approximations of the true boundary-shaped falloff, and texture (image) brushes fall back to solid black.
98
+ - **GDI rotation/skew** — rotation and shear components of the *GDI* world transform are ignored (EMF+ transforms are unaffected); plain-GDI metafiles using rotated world transforms are rare.
99
+ - **Safety limits** — output is clamped to 8192×8192 and replay stops after 200,000 records (EMF/WMF) or 500,000 (EMF+). All three are overridable via `maxCanvasDimension` / `maxRecords`.
100
+ - **Font rendering** uses the host Canvas font engine, so glyph metrics may differ from Windows GDI. Weight, italic, underline, and strike-out are honoured; supply `fontFamilyMap` to remap Windows face names to fonts available in your environment.
77
101
 
78
102
  ## License
79
103
 
package/dist/index.d.mts CHANGED
@@ -25,6 +25,23 @@ interface EmfConvertOptions {
25
25
  * Values above 4 are clamped to 4 to prevent excessive memory usage.
26
26
  */
27
27
  dpiScale?: number;
28
+ /**
29
+ * Hard cap on the output canvas width/height in pixels. Guards against
30
+ * pathological metafiles. Defaults to {@link MAX_CANVAS_DIMENSION} (8192).
31
+ */
32
+ maxCanvasDimension?: number;
33
+ /**
34
+ * Maximum number of records processed per stream before replay stops.
35
+ * Defaults to 200,000 for GDI/WMF and 500,000 for the finer-grained EMF+
36
+ * stream. Supplying a value overrides both with the same cap.
37
+ */
38
+ maxRecords?: number;
39
+ /**
40
+ * Optional map from a (case-insensitive) Windows face name to a CSS font
41
+ * family available in the rendering environment, e.g. `{ calibri: 'Carlito',
42
+ * 'ms shell dlg': 'Tahoma' }`. Applied to GDI, WMF, and EMF+ text.
43
+ */
44
+ fontFamilyMap?: Record<string, string>;
28
45
  }
29
46
  /**
30
47
  * Converts an EMF (Enhanced Metafile) binary buffer to a PNG data-URL string
package/dist/index.d.ts CHANGED
@@ -25,6 +25,23 @@ interface EmfConvertOptions {
25
25
  * Values above 4 are clamped to 4 to prevent excessive memory usage.
26
26
  */
27
27
  dpiScale?: number;
28
+ /**
29
+ * Hard cap on the output canvas width/height in pixels. Guards against
30
+ * pathological metafiles. Defaults to {@link MAX_CANVAS_DIMENSION} (8192).
31
+ */
32
+ maxCanvasDimension?: number;
33
+ /**
34
+ * Maximum number of records processed per stream before replay stops.
35
+ * Defaults to 200,000 for GDI/WMF and 500,000 for the finer-grained EMF+
36
+ * stream. Supplying a value overrides both with the same cap.
37
+ */
38
+ maxRecords?: number;
39
+ /**
40
+ * Optional map from a (case-insensitive) Windows face name to a CSS font
41
+ * family available in the rendering environment, e.g. `{ calibri: 'Carlito',
42
+ * 'ms shell dlg': 'Tahoma' }`. Applied to GDI, WMF, and EMF+ text.
43
+ */
44
+ fontFamilyMap?: Record<string, string>;
28
45
  }
29
46
  /**
30
47
  * Converts an EMF (Enhanced Metafile) binary buffer to a PNG data-URL string