Skip to content
forked from SheetJS/js-wmf

Windows MetaFile (wmf) processor for Javascript and Typescript

License

Notifications You must be signed in to change notification settings

Mictronics/js-wmf

 
 

Repository files navigation

js-wmf

Processor for Windows MetaFile (WMF) files in JS (for the browser and nodejs).

Installation

With npm:

$ npm install wmf

In the browser:

<script src="wmf.js"></script>

The browser exposes a variable WMF.

Usage

The data argument is expected to be an ArrayBuffer, Uint8Array or Buffer

  • WMF.image_size(data) extracts the image offset and extents, returns an Array [width, height] where both metrics are measured in pixels.

  • WMF.draw_canvas(data, canvas) parses the WMF and draws to a Canvas.

Notes

  • The library assumes the global ImageData is available. For nodejs-powered canvas implementations, a shim must be exposed as a global. Using the canvas npm package:
const { createImageData } = require('canvas');
global.ImageData = createImageData;
  • OffscreenCanvas in Chrome and some other Canvas implementations require the dimensions in the constructor:
const size = WMF.image_size(data);
const canvas = new OffscreenCanvas(size[0], size[1]);

Examples

Browser Fetch into canvas (click to show)
// assume `canvas` is a DOM element
(async () => {
  const res = await fetch('url/for/image.wmf');
  const ab = await res.arrayBuffer();
  WMF.draw_canvas(ab, document.getElementById('canvas'));
})();
NodeJS (using `canvas` npm module) (click to show)
const { createCanvas, createImageData } = require('canvas');
global.ImageData = createImageData;

const size = WMF.image_size(data);
const canvas = createCanvas(size[0], size[1]);
WMF.draw_canvas(data, canvas);
NodeJS Convert base64 WMF and save as PNG (click to show)
base64Data = el.attribs.src.replace(/^data:image\/wmf;base64,/, '');
base64Data = Buffer.from(base64Data, 'base64');
util.prep_blob(base64Data, 0);
const size = wmf.image_size_prepped_bytes(base64Data);
const canvas = createCanvas(size[0], size[1]);
wmfCanvas.draw_canvas(base64Data, canvas);
if (!fs.existsSync(pDest)) {
  fs.writeFileSync(pDest, canvas.toBuffer('image/png'));
}

License

Please consult the attached LICENSE file for details. All rights not explicitly granted by the Apache 2.0 License are reserved by the Original Author.

References

  • MS-WMF: Windows Metafile Format

About

Windows MetaFile (wmf) processor for Javascript and Typescript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 86.8%
  • HTML 6.3%
  • JavaScript 6.1%
  • Makefile 0.8%