emf-converter 4.19.0 → 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 CHANGED
@@ -4,7 +4,7 @@
4
4
  [![CI](https://github.com/ChristopherVR/emf-converter/actions/workflows/ci.yml/badge.svg)](https://github.com/ChristopherVR/emf-converter/actions/workflows/ci.yml)
5
5
  [![license](https://img.shields.io/npm/l/emf-converter.svg)](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
@@ -166,6 +181,18 @@ page. The defaults are `1252` and `437`; explicit text charsets are unchanged.
166
181
  | `fontSmoothing` | `'cleartype' \| 'gray' \| 'mono'` | `'cleartype'` | What `DEFAULT_QUALITY` / `DRAFT_QUALITY` / `PROOF_QUALITY` fonts render as (Windows' system setting) |
167
182
  | `fontFamilyMap` | `Record<string, string>` | None | Without `fonts`: maps Windows face names (case-insensitive) to locally available fonts, e.g. `{ calibri: 'Carlito' }` |
168
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
+
169
196
  ### SVG functions
170
197
 
171
198
  | Function | Returns |
@@ -193,7 +220,7 @@ Node.js only (returns `[]` elsewhere; the package stays browser-safe). Reads the
193
220
 
194
221
  ## How it works
195
222
 
196
- 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.
197
224
 
198
225
  See [How it works](docs/how-it-works.md) for the pipeline, the Windows-parity details and the full list of supported records.
199
226