Windows Metafile Explained

Windows Metafile (WMF) is an image file format originally designed for Microsoft Windows in the 1990s. The original Windows Metafile format was not device-independent (though could be made more so with placement headers) and may contain both vector graphics and bitmap components. It acts in a similar manner to SVG files. WMF files were later superseded by Enhanced Metafiles (EMF files) which did provide for device-independence. EMF files were then themselves enhanced via EMF+ files.

Essentially, a metafile stores a list of records consisting of drawing commands, property definitions and graphics objects to display an image on screen. The drawing commands used are closely related to the commands of the Graphics Device Interface (GDI) API used for drawing in Microsoft Windows.

There are three major types of metafiles – a WMF is a 16-bit format introduced in Windows 3.0. It is the native vector format for Microsoft Office applications such as Word, PowerPoint, and Publisher., revision 17 of the Windows Metafile Format specification is available.[1]

History

The original 16 bit WMF file format was fully specified in volume 4 of the 1992 Windows 3.1 SDK documentation[2] (at least if combined with the descriptions of the individual functions and structures in the other volumes), but that specification was vague about a few details. These manuals were published as printed books available in bookstores with no click through EULA or other unusual licensing restrictions (just a general warning that if purchased as part of a software bundle, the software would be subject to one).

Over time the existence of that historic specification was largely forgotten and some alternative implementations resorted to reverse engineering to figure out the file format from existing WMF files, which was difficult and error prone.[3] In September 2006, Microsoft again published the WMF file format specification in a more complete form[4]

Records other than control records can be largely grouped into bitmap records, drawing records, object records, state records and escape records.

Bitmap records

Bitmap records manage and output bitmap images.

NameDescription
META_BITBLTSpecifies how to do a bit block transfer. These records can specify a bitmap to use as the source, or a region.
META_DIBSTRETCHBITBLT Specifies how to do a bit block transfer of a device-independent bitmap image, but allows for expansion or contraction of the image. These records can specify a bitmap to use as the source, or a region.
META_STRETCHBLT Specifies how to do a bit block transfer, but allows for expansion or contraction of the image.
|-| META_CHORD | Draws a chord.|-| META_EXTFLOODFILL | Fills an area with a brush.|-| META_FRAMEREGION | Draws a border around a region of the page with a brush.|-| META_LINETO | Draws a half-open line from the drawing position defined in the playback device context to a specified point.|-| META_POLYGON | Paints a polygon consisting of two or more vertices connected by straight lines. The polygon is outlined by using the pen and filled by using the brush and polygon fill mode that are defined in the playback device context.|-| META_RECTANGLE | Paints a rectangle, which is outlined by using the pen and filled by using the brush and is filled by using the brush that is defined in the playback device context.|-| META_SETPIXEL | Sets the pixel at the specified coordinates to the specified color.|}

Object records

Object records create and manage graphics objects. In WMF files there are two broad categories of objects – graphics objects and structure objects. Structure objects are not explicitly created or deleted in a WMF, they are instead of complex structures. For example, the BitmapCoreHeader contains information about the dimensions and color format of a device-independent bitmap, A graphics object, however, specifies parameters for graphics output and during playback of the WMF it sets up the playback device context. |-| META_CREATEFONTINDIRECT | Creates a brush object from a font object. |-| META_CREATEPENINDIRECT| Creates a pen object. |-| META_DELETEOBJECT| Delete an object.|-| META_DIBCREATEPATTERNBRUSH| Creates a brush object from a device-independent bitmap. |-| META_SELECTOBJECT | Selects the object that will be the current object for the playback device context, which works on all graphics objects except palette objects, which must be set with META_SELECTPALETTE.

NameDescription
META_ANIMATEPALETTERedefines entries in the logical palette that is defined in the playback device context with a specified Palette Object.
META_INTERSECTCLIPRECTSets the clipping region that is defined in the playback device context to the intersection of the existing clipping region and a specified rectangle.
META_OFFSETVIEWPORTORGMoves the viewport origin in the playback device context by specified horizontal and vertical offsets.
META_SAVEDCSaves the playback device context for later retrieval.
META_SETBKCOLORSets the background color in the playback device context to a specified color.
META_SETLAYOUTDefines the layout orientation in the playback device context.
META_SETMAPPERFLAGSDefines the algorithm that the font mapper uses when it maps logical fonts to physical fonts.
META_SETPOLYFILLMODEDefines polygon fill mode in the playback device context for graphics operations that fill polygons.
META_SETROP2Defines the foreground raster operation mixing mode in the playback device context.
META_SETTEXTJUSTIFICATIONDefines the amount of space to add to break characters in a string of justified text.
META_SETWINDOWEXTDefines the horizontal and vertical extents of the output window in the playback device context.
BEGIN_PATHOpens a path.
CHECK_PNGFORMATDetermines whether it can handle the given PNG image.
CLOSE_CHANNELSame as ENDDOC.
ENDDOCNotifies the printer driver that a new print job is ending.
EXTTEXTOUTDraws text using the currently selected font, background color, and text color.
GET_DEVICEUNITSGets the device units currently configured on the output device.
GET_PAIRKERNTABLEGets the font kern table currently defined on the output device.
GET_PRINTINGOFFSETRetrieves the offset from the upper-left corner of the physical page where the actual printing or drawing begins.
META_ESCAPE_ENHANCED_METAFILEUsed to embed an EMF metafile within a WMF metafile.
NEWFRAMENotifies the printer driver that the application has finished writing to a page.
PASSTHROUGHPasses through arbitrary data to the printer driver.
POSTSCRIPT_IDENTIFYSets the printer driver to either PostScript-centric or GDI-centric mode.
QUERY_ESCSUPPORTQueries the printer driver to determine whether a specific WMF escape function is supported on the output device.
SET_MITERLIMITSets the limit for the length of miter joins to use in drawing to the output device.

Each EMF header starts with an EMR_HEADER record, and records the relevant properties of the device on which the metafile image was recorded. The original EMF header has an 80 byte header and an optional variable length description string. EmfMetafileHeaderExtension2 is a record that is inserted directly after the EmfMetafileHeaderExtension1 record, and it contains two fields with the X and Y values to measure the device surface in micrometers.

Notes and References

  1. Web site: 2021-04-26 . [MS-WMF]

    Windows Metafile Format |url=http://msdn.microsoft.com/en-us/library/cc250370.aspx |access-date=2023-09-13 |publisher=MSDN}} EMF files, which replaced WMF files, work on the same principle only it is a 32-bit file format that also allows for the embedding of private data within "comment" records. EMF+ is an extension to EMF files and embedded in these comment records, allowing for images and text using commands, objects and properties that are similar to Windows GDI+.

    . Microsoft. 19.
  2. Microsoft Windows 3.1 Programmers Reference, Volume 4 Resources, Microsoft Press 1992,, chapter 3 pp. 21-45
  3. Web site: Window Metafile (wmf) Reference . These opcodes are unimplemented, for the reason that i dont know what they are, no known documentation . Caolan McNamara . 2008-06-01.
  4. Web site: Microsoft Open Specification Promise . . 2008-06-01 .
  5. Windows Image Media Types. 7903. Windows Metafiles. 1. Leonard. Sean. September 2016. IETF. February 8, 2020. 2070-1721. 10.17487/RFC7903.
  6. Book: Graphics Programming with GDI+ & DirectX. A-List Publishing. 68. 2005. Alex. Polyakov. Vitaly. Brusentsev. 1-931769-39-7.
  7. Web site: [MS-WMF]

    Windows Metafile Format Specification | access-date=2008-06-01 }} in the context of the Microsoft Open Specification Promise, promising to not assert patent rights to file format implementors.[4]

    Microsoft later deprecated WMF files in favour of 32-bit EMF files as WMF files had real issues with device independence, despite the use of a "placeable" file header which provided basic device independence. Microsoft found that developers who use the format were "[embedding] application, location, or scaling comments in the metafiles... Others added headers to the metafile that provided various application-specific information", causing major compatibility issues. Thus, in 1992 with Windows NT 3.1, Microsoft introduced the Enhanced Metafile format (EMF)[5] - a format which was based on the Win32 API and with which they built-in device independence. - these were also known as NT metafiles. With the release of Windows XP and GDI+, the set of records had to be significantly increased and so Microsoft released EMF+ as an extension to the existing EMF file format.[6]

    Metafile structure

    WMF, EMF and EMF+ files all consist of a series of records that are played back to produce graphical output. Some records define objects which can specify graphical objects used to determine how graphics should be drawn (e.g. pens specify the color and width of lines). Each of these objects are stored in metafiles and are placed into an object table, which tracks the usage of graphic objects while processing the metafile. The object table is an associative array of indexes to graphical object structures defined within the metafile.

    WMF and EMF files handle object processing differently to EMF+ records in EMF files. As a WMF and EMF file is being processed, the records are read into an object table once an object is defined. If an object is deleted then the object is released from the table and the identifier can be reused. Notably an object will not be used until it is specifically selected during record playback.

    . Microsoft. 187. 3.1.4.1 WMF Object Table.