emf-converter 4.18.2 → 4.20.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 +42 -10
- package/dist/browser.js +849 -24
- package/dist/browser.mjs +849 -25
- package/dist/index.d.mts +57 -6
- package/dist/index.d.ts +57 -6
- package/dist/index.js +849 -24
- package/dist/index.mjs +849 -25
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://github.com/ChristopherVR/emf-converter/actions/workflows/ci.yml)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
|
|
7
|
-
A zero-dependency TypeScript library that converts **EMF** (Enhanced Metafile, including embedded **EMF+** / GDI+ records) and **WMF** (Windows Metafile) files into **PNG** or **SVG** (markup, a base64 data URL, React elements, or a generated JSX/TSX component).
|
|
7
|
+
A zero-dependency TypeScript library that converts **EMF** (Enhanced Metafile, including embedded **EMF+** / GDI+ records) and **WMF** (Windows Metafile) files into **PNG**, **JPEG** or **SVG** (markup, a base64 data URL, React elements, or a generated JSX/TSX component).
|
|
8
8
|
|
|
9
9
|
Windows metafiles are recorded GDI and GDI+ drawing calls, commonly embedded in Office documents and on the Windows clipboard. This library replays those calls the way Windows does: the PNG output is checked pixel for pixel against images painted by Windows itself (hundreds of ground-truth fixtures under `src/__fixtures__/gdi`, generated by `scripts/gdi-fixtures`), and the SVG output keeps vectors, text and gradients resolution-independent.
|
|
10
10
|
|
|
@@ -20,7 +20,7 @@ Windows metafiles are recorded GDI and GDI+ drawing calls, commonly embedded in
|
|
|
20
20
|
|
|
21
21
|
## Documentation and demo
|
|
22
22
|
|
|
23
|
-
The documentation site at **https://christophervr.github.io/emf-converter/** includes a live demo: drop an `.emf` or `.wmf` file to see the PNG or SVG output, download it, or copy it as a TSX component.
|
|
23
|
+
The documentation site at **https://christophervr.github.io/emf-converter/** includes a live demo: drop an `.emf` or `.wmf` file to see the PNG, JPEG or SVG output, download it, or copy it as a TSX component.
|
|
24
24
|
|
|
25
25
|
The site is built with VitePress from the `docs/` directory. Run it locally with `bun run docs:dev`.
|
|
26
26
|
|
|
@@ -32,14 +32,14 @@ npm install emf-converter
|
|
|
32
32
|
|
|
33
33
|
No required dependencies:
|
|
34
34
|
|
|
35
|
-
- **Browser / Web Worker**: `OffscreenCanvas` or `HTMLCanvasElement` is used automatically. Bundlers select the dedicated browser entry through conditional exports; it contains no native canvas or Node filesystem imports. Installing `@napi-rs/canvas` is only needed for the optional Node backend.
|
|
36
|
-
- **Node.js**: SVG output, and PNG output for drawings without text, work out of the box through the built-in rasteriser. For PNG output of drawings with text, either pass `fonts` (see [Exact text](#exact-text)) or install the optional [`@napi-rs/canvas`](https://www.npmjs.com/package/@napi-rs/canvas) (prebuilt, no `node-gyp`):
|
|
35
|
+
- **Browser / Web Worker**: `OffscreenCanvas` or `HTMLCanvasElement` is used automatically for PNG and JPEG. Bundlers select the dedicated browser entry through conditional exports; it contains no native canvas or Node filesystem imports. Installing `@napi-rs/canvas` is only needed for the optional Node backend.
|
|
36
|
+
- **Node.js**: SVG output, and PNG/JPEG output for drawings without text, work out of the box through the built-in rasteriser. For PNG/JPEG output of drawings with text, either pass `fonts` (see [Exact text](#exact-text)) or install the optional [`@napi-rs/canvas`](https://www.npmjs.com/package/@napi-rs/canvas) (prebuilt, no `node-gyp`):
|
|
37
37
|
|
|
38
38
|
```bash
|
|
39
39
|
npm install @napi-rs/canvas
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
Without either, PNG conversion of a drawing that contains text returns `null` rather than an image missing its text.
|
|
42
|
+
Without either, PNG/JPEG conversion of a drawing that contains text returns `null` rather than an image missing its text.
|
|
43
43
|
|
|
44
44
|
## Quick start
|
|
45
45
|
|
|
@@ -60,6 +60,21 @@ const smooth = await convertMetafileToDataUrl(buffer, { gdiAntialias: true });
|
|
|
60
60
|
|
|
61
61
|
Returns `Promise<string | null>`; `null` when the buffer is not a valid metafile (or, in plain Node.js, when it has text and neither `fonts` nor `@napi-rs/canvas` is available).
|
|
62
62
|
|
|
63
|
+
### JPEG output
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { convertMetafileToJpegDataUrl } from 'emf-converter';
|
|
67
|
+
|
|
68
|
+
const jpeg = await convertMetafileToJpegDataUrl(buffer);
|
|
69
|
+
// => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ..."
|
|
70
|
+
|
|
71
|
+
// Quality 0-1 (default 0.92), the colour under transparent areas (default white),
|
|
72
|
+
// plus every PNG option (maxWidth, dpiScale, fonts, ...).
|
|
73
|
+
const preview = await convertMetafileToJpegDataUrl(buffer, { quality: 0.7, background: '#f4f4f4', maxWidth: 800 });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The same rendering as the PNG, encoded by the bundled baseline JPEG encoder, so the output is identical in browsers and Node.js. JPEG has no transparency: the metafile's transparent background is filled with `background`. Colour is kept at full resolution (4:4:4) so coloured lines and text stay crisp; `chromaSubsampling: true` halves it (4:2:0) for a smaller file.
|
|
77
|
+
|
|
63
78
|
### SVG output
|
|
64
79
|
|
|
65
80
|
```typescript
|
|
@@ -144,14 +159,19 @@ Without `fonts`, text is drawn by the host's canvas font engine (supply `fontFam
|
|
|
144
159
|
|
|
145
160
|
#### `EmfConvertOptions`
|
|
146
161
|
|
|
147
|
-
`
|
|
148
|
-
(for example, `936` from Windows `GetACP()`
|
|
149
|
-
|
|
150
|
-
|
|
162
|
+
`ansiCodePage` and `oemCodePage` set the known code pages of the playback
|
|
163
|
+
device (for example, `936` from Windows `GetACP()` and `866` from `GetOEMCP()`).
|
|
164
|
+
Windows decodes ANSI text records (EMF and WMF) in ANSI/DEFAULT fonts with the
|
|
165
|
+
ANSI code page, WMF font face names too, and text in OEM fonts with the OEM code
|
|
166
|
+
page. The defaults are `1252` and `437`; explicit text charsets are unchanged.
|
|
167
|
+
`wmfAnsiCodePage` overrides `ansiCodePage` for WMF input only. See the
|
|
168
|
+
[API reference](docs/api.md) for supported values.
|
|
151
169
|
|
|
152
170
|
| Field | Type | Default | Description |
|
|
153
171
|
| -------------------- | ------------------------------------- | ----------------- | ----------- |
|
|
154
172
|
| `maxWidth` | `number` | None | Maximum output width in pixels (aspect ratio preserved) |
|
|
173
|
+
| `ansiCodePage` | `number` | `1252` | Playback-device ANSI code page (`GetACP()`) for ANSI/DEFAULT text |
|
|
174
|
+
| `oemCodePage` | `number` | `437` | Playback-device OEM code page (`GetOEMCP()`) for OEM text |
|
|
155
175
|
| `maxHeight` | `number` | None | Maximum output height in pixels |
|
|
156
176
|
| `dpiScale` | `number` | `1` | Resolution multiplier; clamped to `4` |
|
|
157
177
|
| `maxCanvasDimension` | `number` | `8192` | Hard cap on output width/height in pixels |
|
|
@@ -161,6 +181,18 @@ EMF text are unchanged. See the [API reference](docs/api.md) for supported value
|
|
|
161
181
|
| `fontSmoothing` | `'cleartype' \| 'gray' \| 'mono'` | `'cleartype'` | What `DEFAULT_QUALITY` / `DRAFT_QUALITY` / `PROOF_QUALITY` fonts render as (Windows' system setting) |
|
|
162
182
|
| `fontFamilyMap` | `Record<string, string>` | None | Without `fonts`: maps Windows face names (case-insensitive) to locally available fonts, e.g. `{ calibri: 'Carlito' }` |
|
|
163
183
|
|
|
184
|
+
### `convertMetafileToJpegDataUrl(buffer, options?)`
|
|
185
|
+
|
|
186
|
+
Returns `Promise<string | null>`, a `data:image/jpeg;base64,...` URL (`null` in the same cases as the PNG function).
|
|
187
|
+
|
|
188
|
+
#### `JpegConvertOptions` (extends `EmfConvertOptions`)
|
|
189
|
+
|
|
190
|
+
| Field | Type | Default | Description |
|
|
191
|
+
| --- | --- | --- | --- |
|
|
192
|
+
| `quality` | `number` | `0.92` | JPEG quality from `0` to `1`, as for `canvas.toDataURL('image/jpeg', quality)` |
|
|
193
|
+
| `background` | `string` | `'#ffffff'` | Colour under transparent pixels, `#rgb` or `#rrggbb` (other values fall back to white) |
|
|
194
|
+
| `chromaSubsampling` | `boolean` | `false` | `true` stores colour at half resolution (4:2:0) for a smaller file |
|
|
195
|
+
|
|
164
196
|
### SVG functions
|
|
165
197
|
|
|
166
198
|
| Function | Returns |
|
|
@@ -188,7 +220,7 @@ Node.js only (returns `[]` elsewhere; the package stays browser-safe). Reads the
|
|
|
188
220
|
|
|
189
221
|
## How it works
|
|
190
222
|
|
|
191
|
-
A three-phase pipeline: **parse → replay → export**. Records are replayed by the GDI, EMF+ or WMF handlers onto a Canvas (or the built-in pure-JavaScript rasteriser) for PNG, or onto a recording context for SVG. GDI and GDI+ shapes, raster operations, brushes, clipping, text and WMF playback are all fitted to output painted by Windows itself.
|
|
223
|
+
A three-phase pipeline: **parse → replay → export**. Records are replayed by the GDI, EMF+ or WMF handlers onto a Canvas (or the built-in pure-JavaScript rasteriser) for PNG and JPEG, or onto a recording context for SVG. GDI and GDI+ shapes, raster operations, brushes, clipping, text and WMF playback are all fitted to output painted by Windows itself.
|
|
192
224
|
|
|
193
225
|
See [How it works](docs/how-it-works.md) for the pipeline, the Windows-parity details and the full list of supported records.
|
|
194
226
|
|