@nextgensoftwares/folio-metafile
WMF and EMF pictures (the Windows metafiles Office files embed: MathType equations, OLE object previews, clip art, charts pasted from older apps) as SVG, without a DOM. Depends on fflate (to encode bitmaps as PNG).
import { metafileToSvg } from '@nextgensoftwares/folio-metafile';
const out = metafileToSvg(bytes); // { svg, width, height, elements } | null
if (out) asset = { name: 'image1.svg', type: 'image/svg+xml', bytes: new TextEncoder().encode(out.svg) };@nextgensoftwares/folio-docx and @nextgensoftwares/folio-pptx call it for image/wmf / image/emf parts the host's converter didn't turn into something drawable (the host's converter, when it has one, comes first). @nextgensoftwares/folio-export-pdf recognises the SVG it writes (data-folio="metafile") and draws it as PDF vectors and text, not a raster.
Converting
| Export | Description |
|---|---|
metafileToSvg(bytes) | a WMF or EMF picture as MetafileSvg, picked by metafileKind; null when the bytes are neither, it draws nothing (elements 0) or reading fails. Never throws |
metafileKind(bytes) | 'emf', 'wmf' or null, from the header (EMF signature at byte 40; WMF placeable key 0x9AC6CDD7 or a bare WMF header) |
emfToSvg(bytes) | an Enhanced Metafile ([MS-EMF]) through its GDI records; an EMF+ dual file draws its GDI fallback, an EMF+-only file gives elements: 0. Null when it isn't an EMF |
wmfToSvg(bytes) | a Windows Metafile ([MS-WMF]), Aldus-placeable or not; null when it isn't one |
type MetafileSvg | { svg, width, height, elements }: the SVG document (UTF-8 text), the picture's size in CSS px (EMF frame, or the placeable box at its units per inch, else the window), and how many things it draws |
The SVG is <svg data-folio="metafile" width height viewBox preserveAspectRatio="none"> in the picture's own device space: absolute M/L/C/Z paths with fill, fill rule, stroke width and dashes; <text> with per-character x positions (ExtTextOut advances), family, size, bold, italic, decorations, anchor and rotation (right-to-left and combining text as one run from its left edge, textLength = the width its advances give; right-to-left reading as direction="rtl", its anchor swapped so the left edge stays at x); <image> data URLs for bitmaps; clipPaths of one path each (clip-rule even-odd or nonzero), nested one <g clip-path> per layer of the clip region. One metafile draws at most 200 000 elements; the rest is left out.
Coverage
- Pens (widths, dashes), brushes, polygons, poly-polygons, polylines, Béziers, paths (fill / stroke), rectangles, rounded rectangles, ellipses, arcs, chords and pies (also ArcTo and AngleArc, in either arc direction), the world transform and every mapping mode.
- Clipping: IntersectClipRect, ExcludeClipRect, WMF region objects (SelectClipRegion), EMF region data (ExtSelectClipRgn) and clip paths (SelectClipPath), moved by OffsetClipRgn, combined with AND, DIFF, COPY (exact) and OR, XOR (exact for one layer; with several nested layers their union, a little more than GDI shows). The clip is part of the device context: RestoreDC brings the previous one back.
- Text at its exact advances, with font, size, weight, italic, rotation and alignment. 8-bit text (WMF, ExtTextOutA) and WMF face names are read in the font's charset through the platform's
TextDecoder: Greek, Cyrillic, Central European, Turkish, Baltic, Vietnamese, Hebrew, Arabic, Thai, Shift-JIS, GBK, Big5, Korean (Windows-1252 otherwise, or without aTextDecoder). Advances are per byte, so a double-byte character moves by both. Right-to-left and combining text is written as one run, not placed character by character, so it is shaped; the run is fitted to the box its advances give (spaces at its ends trimmed on the side they sit), so neighbouring runs don't overlap whatever font substitutes. Text drawn with right-to-left reading (ETO_RTLREADING) keeps it: brackets mirror. The Symbol font's letters become the Greek and maths characters they show. - Bitmaps: DIB records (1–32 bit, bit fields) as PNG images; embedded JPEG and PNG bits are kept as they are.
Not drawn: EMF+-only records (elements is 0 then), raster operations other than copy.