Metafont

Last updated
Metafont
Developer(s) Donald Knuth
Stable release
2.71828182 / January 2021;2 years ago (2021-01)
Operating system Cross-platform
Type Computer language
License Permissive

Metafont is a description language used to define raster fonts. It is also the name of the interpreter that executes Metafont code, generating the bitmap fonts that can be embedded into e.g. PostScript. Metafont was devised by Donald Knuth as a companion to his TeX typesetting system.

Contents

One of the characteristics of Metafont is that the points defining the shapes of the glyphs—for example top of a stem, or intersection of a stem and crossbar—are defined with geometrical equations; the intent that the three stems of an ‘m’ are equally spaced horizontally might be expressed as if points 1, 2, and 3 are at the bottom ends of the three stems, whereas the intent that they all end on the same vertical position would be .

Metafont is a macro language, where operations such as "draw a lower case top of stem serif at point 4" might appear as one macro instruction (with the point as argument) in the program for a letter. For describing shapes, Metafont has a rich set of path construction operations that mostly relieves the user of having to calculate control points.

Many families of Metafont fonts are set up so that the main source file for a font only defines a small number of design parameters (x-height, em width, slant, vertical stroke width, etc.), then calling a separate source file common for a whole range of fonts to actually draw the individual glyphs; this is the meta aspect of the system.

Modes of operation

Metafont is most often run as a helper to output device (printer, screen) drivers; in those cases, its job is to generate bitmaps for a font for a specific combination of output device (called a mode in Metafont) and resolution (visible in the name of the output file, see below). These bitmaps are typically stored for later reuse, so that Metafont does not have to be run every time a document is displayed, but on the other hand TeX distributions with a Metafont component have typically not included any prebuilt bitmap fonts, since they would be rather large in comparison to the sources from which they could be generated. Since Metafont fonts were traditionally the TeX default from which other font formats were exceptions, an incomplete installation of a non-Metafont font can sometimes result in Metafont being called and emitting a confusing "somefont.mf not found" error message.

Equally important, but not as common, is running Metafont to generate a font metric (TFM) file; a TFM file is only generated if the fontmaking variable is positive. Traditionally TeX distributions have often come with all TFM files pregenerated (since they are pretty small), but someone installing a Metafont font from sources will have to generate its TFM file before TeX can use it.

A third way of operating Metafont is proof mode: if the proofing variable is positive then the bitmap font file also contains additional information provided via special commands, in particular the positions and names of points the font designer considered important for the design. If using the separate gftodvi utility to generate enlarged images of the font glyphs, this information from specials is included; point positions are not limited to pixel resolution.

Metafont can also be run interactively, and has commands for displaying on the screen the images it produces. Knuth has said that he uses Metafont as a kind of desk calculator for solving complicated equations, though he now uses MetaPost for mathematical illustrations.

Metafont can render any kind of graphical output, not just glyphs. However, MetaPost and Asymptote are preferred for mathematical illustrations. Metafont is most commonly invoked without a direct request from the user. DVI files can only contain references to typefaces, rather than the sets of raster or vector glyphs that other formats like PostScript allow. Consequently, the glyphs in the typefaces need to be accessed whenever a request is made to view, print or convert a DVI file.

Output files

Metafont outputs several kinds of files: for a file called NAME.mf, it can output:

After running Metafont, typically one uses the gftopk program to convert the NNNNgf files to pk (packed) format (.NNNNpk). The pk format was primarily introduced to reduce file size (to about half), but expected to also speed up processing since less data would have to be input/output. The GF and PK formats both employ run-length encoding of bitmaps, but make different binary encodings of the run-lengths. The PK format also does some preprocessing of the bitmaps (bounding box, eliding repeated rows) and encodes all rows of a character as one long bit-sequence. [1]

In the TeX Directory Structure standard, filenames are limited to 8+3 characters, so GF and PK files would only have extensions .gf and .pk. Files for different resolutions are kept apart by placing them in separate directories, named dpiNNNN, e.g. dpi300/cmr10.pk.

Language

The Metafont language is an interpreted language for programs that are essentially declarative rather than imperative . [2]

Variables and equations

Variables in Metafont can be of eight different types:

Metapost adds color (a triple of numerics) as a ninth type and has a completely different (non-raster) model for pictures; the latter is the main point of divergence between the two programs. Metafont vardef macros also live in the same namespace as variables and may in some ways be regarded as a ninth type of variable, although macros do not exist as first-class values in Metafont.

Unusually, the names of variables are not simple tokens, but sequences of symbolic tokens and numeric indices; the variable name x2r is thus not one alphanumeric token, but a sequence of the three tokens x (symbolic), 2 (numeric), and r (symbolic). Record and array types may be simulated through collections of variables that share a common name prefix, an idiom supported by the type declaration system giving all variables whose names which differ only in numeric indices the same type (as expected for arrays) while keeping variables whole name differ in some symbolic token separate (as expected for records).

A very distinctive feature of Metafont is the use of equations to define variables. A numeric variable (or component of a pair or transformation variable) may be in the three states known (set), unknown independent (not set), and unknown dependent (not set, but given by a linear expression of one or several independents). When Metafont executes an equation statement, it turns one of the independents involved into a dependent and eliminates it from the expressions for all other dependents; when no independents remain in the expression for a dependent variable, that variable becomes known. Solving linear equation systems thus is a built-in feature of the Metafont language, and the recommended method of assigning most variables (especially those whose values have geometric significance) is to state equations determining their values. Equation systems frequently mix numeric (scalar) equations with pair (vector) equations.

An exception to the above is the class of internal quantity variables. These have names consisting of just one symbolic token, are always numeric, and are always known. They have a more direct internal representation than ordinary variables, making it convenient for primitive operations in Metafont (or extensions thereof) to use them implicitly.

Syntax

Metafont has numeric and string constant tokens with mainstream syntaxes; strings are delimited by " quotes, numeric constants can have decimals but not an exponent part. All other tokens are classified as symbolic, and can be redefined arbitrarily; there is no restriction that tokens with certain meanings must have names consisting of certain characters. At runtime, there can additionally be capsule tokens, which are effectively constant value tokens of arbitrary type; in the source code those appear as symbolic tokens.

Except where characters are involved in numeric or string constants, the extent of the token containing a particular character depends on to which class the character belongs; unlike TeX, Metafont has fixed character classes. The characters ,, ;, (, and ) are "loners" and only form single character tokens. For the character classes <=>:|, ‘’ (single quotes), +-, /*\, !?, #&@$, ^~, [, ], {}, and ., as well as the class of underscore together with upper and lower case A–Z, the token consists of the longest consecutive sequence of characters from the same class. Whitespace characters don't contribute tokens. % starts a comment lasting until end of line.

A notable application of these rules is that # is frequently appearing as part of variable names in Metafont code, e.g. em# and pt#.

Delimiters (such as parentheses) do not have built-in meanings, instead there is a command that turns two symbolic tokens into a pair of matching delimiters, but normally Metafont programs use only the ordinary parentheses. Besides to override priorities in expressions, delimiters are also required around certain kinds of macro arguments.

Graphics

Curves in Metafont are defined as cubic splines rather than quadratic, for greater versatility at the cost of more complex arithmetic. [3]

Unlike more common outline font formats (such as TrueType or PostScript Type 1), a Metafont font is primarily made up of strokes with finite-width "pens", along with filled regions. Thus, rather than describing the outline of the glyph directly, a Metafont file describes the pen paths. Some simpler Metafont fonts, such as the calligraphic mathematics fonts in the Computer Modern family, use a single pen stroke with a relatively large pen to define each visual "stroke" of the glyphs. More complex fonts such as the Roman text fonts in the Computer Modern family use a small pen to trace around the outline of the visual "strokes", which are then filled; the result is much like an outline font, but with slightly softened corners defined by the pen shape.

Since the font shapes are defined by equations rather than directly coded numbers, it is possible to treat parameters such as aspect ratio, font slant, stroke width, serif size, and so forth as input parameters in each glyph definition (which then define not a single font, but a meta-font). Thus, by changing the value of one of these parameters at one location in the Metafont file, one can produce a consistent change throughout the entire font. Computer Modern Roman illustrates many uses of this feature; a typical TeX installation includes a number of versions of the font in pitches from 5 to 17 cpi, with the stroke widths the same in all sizes (rather than increasing as the font is scaled up). In addition, the Computer Modern typewriter and sans-serif fonts are defined using essentially the same Metafont file as the Roman font, but with different global parameters.

Use

While well-known type designers, such as Hermann Zapf, have collaborated with Knuth to create new fonts using Metafont, the system has not been widely adopted by professional type designers. Knuth attributes this to the fact that "asking an artist to become enough of a mathematician to understand how to write a font with 60 parameters is too much." [4] Jonathan Hoefler commented that the Metafont system ultimately became "a technology behind zero of your favourite fonts...Knuth's idea that letters start with skeletal forms is flawed." [5] [ better source needed ]

The Metafont system allows fonts to be processed in unusual ways; in 1982 Knuth showed how it could be used to morph fonts, with a serif font slowly transitioning into a sans-serif design over the course of a text. [6]

History

Donald Knuth started work on font creation software in 1977, and produced the first version of Metafont in 1979. Due to shortcomings in the original Metafont language, Knuth developed an entirely new Metafont system in 1984, and it is this revised system that is used today; Metafont has a versioning system similar to that of TeX, where the number asymptotically approaches e with each revision. [7]

Example

The following example creates a closed beanlike shape for the character "B" of a font:

%file name: beta.mf%mode_setup;% Define a beanlike shape for the character Bbeginchar("B",11pt#,11pt#,0);% Setup coordinates as an equation systemy1=y2=y3=0;y4=y5=y6=h;x1=x4=0;x2=x5=w;x3=x6=2*w;% Define penpickuppencirclexscaled0.2wyscaled0.04wrotated45;% Draw the character curve% z1 is the same as (x1, y1)drawz1..z3..z6{z2-z6}..z5..{z4-z2}z4..cycle;endchar;end

This yields the following glyph:

Metafont-example-output.svg

The above example will be processed with a command line such as: [8]

mf '\mode=ljfour; mode_setup; input beta.mf'; gftopk beta.600gf beta.600pk 

Then it can be used in a LaTeX file such as [8] (all files should dwell in the same directory or the TeX system should be informed about them using appropriate methods):

\documentclass{article}\newfont{\letterbeta}{beta}\newcommand{\otherbeta}{{\letterbeta B}}\begin{document} Let's try having a strange \otherbeta\ here. \end{document}

The resulting PDF file should look like this: Tex mf beta.pdf

Producing PostScript Type 1 fonts

There are several tools for converting Metafont programs to PostScript Type 1 fonts. Most make use of MetaPost's ability to convert a subset of Metafont's language into EPS outlines, which can subsequently be converted to PostScript Type 1 fonts. Generating vector outlines of Metafont pen strokes is nontrivial, as the Metafont model of a glyph is a raster image and the exact outlines of most strokes are not Bézier curves. [note 1]

The common approach to generate Type 1 fonts with pen strokes remains to generate a high-resolution bitmap and then using an autotracer, implemented by packages such as mftrace [13] and TeXtrace [14]

See also

Notes

  1. Technically, Metafont does generate vector outlines as an intermediate step of rasterising a stroke, but those intermediate outlines are tailored to the current raster resolution and non-simple as curves, so they are very far from being usable as Type 1 outlines. An effect of the non-simplicity is spurious pixels on the concave side of a stroke that get a value of 2 rather than 1, however the automatic culling of pictures being shipped out as glyphs in a font normally prevents this from showing up in the final bitmap.

Related Research Articles

<span class="mw-page-title-main">PostScript</span> File format

PostScript (PS) is a page description language in the electronic publishing and desktop publishing realm. It is a dynamically typed, concatenative programming language. It was created at Adobe Systems by John Warnock, Charles Geschke, Doug Brotz, Ed Taft and Bill Paxton from 1982 to 1984.

TeX, stylized within the system as TeX, is a typesetting system which was designed and written by computer scientist and Stanford University professor Donald Knuth and first released in 1978. TeX is a popular means of typesetting complex mathematical formulae; it has been noted as one of the most sophisticated digital typographical systems.

TrueType is an outline font standard developed by Apple in the late 1980s as a competitor to Adobe's Type 1 fonts used in PostScript. It has become the most common format for fonts on the classic Mac OS, macOS, and Microsoft Windows operating systems.

<span class="mw-page-title-main">Typeface</span> Set of characters that share common design features

A typeface is the design of lettering that can include variations in size, weight, slope, width, and so on. Each of these variations of the typeface is a font.

OpenType is a format for scalable computer fonts. It was built on its predecessor TrueType, retaining TrueType's basic structure and adding many intricate data structures for prescribing typographic behavior. OpenType is a registered trademark of Microsoft Corporation.

<span class="mw-page-title-main">Device independent file format</span> Typesetting file format

The device independent file format (DVI) is the output file format of the TeX typesetting program, designed by David R. Fuchs and implemented by Donald E. Knuth in 1982. Unlike the TeX markup files used to generate them, DVI files are not intended to be human-readable; they consist of binary data describing the visual layout of a document in a manner not reliant on any specific image format, display hardware or printer. DVI files are typically used as input to a second program which translates DVI files to graphical data. For example, most TeX software packages include a program for previewing DVI files on a user's computer display; this program is a driver. Drivers are also used to convert from DVI to popular page description languages and for printing.

MetaPost refers to both a programming language and the interpreter of the MetaPost programming language. Both are derived from Donald Knuth's Metafont language and interpreter. MetaPost produces vector graphic diagrams from a geometric/algebraic description. The language shares Metafont's declarative syntax for manipulating lines, curves, points and geometric transformations. However,

<span class="mw-page-title-main">Computer Modern</span> Family of typefaces

Computer Modern is the original family of typefaces used by the typesetting program TeX. It was created by Donald Knuth with his Metafont program, and was most recently updated in 1992. Computer Modern, or variants of it, remains very widely used in scientific publishing, especially in disciplines that make frequent use of mathematical notation.

A computer font is implemented as a digital data file containing a set of graphically related glyphs. A computer font is designed and created using a font editor. A computer font specifically designed for the computer screen, and not for printing, is a screen font.

<span class="mw-page-title-main">Font rasterization</span> Process of converting text from vector to raster

Font rasterization is the process of converting text from a vector description to a raster or bitmap description. This often involves some anti-aliasing on screen text to make it smoother and easier to read. It may also involve hinting—information embedded in the font data that optimizes rendering details for particular character sizes.

<span class="mw-page-title-main">AMS Euler</span> Typeface

AMS Euler is an upright cursive typeface, commissioned by the American Mathematical Society (AMS) and designed and created by Hermann Zapf with the assistance of Donald Knuth and his Stanford graduate students. It tries to emulate a mathematician's style of handwriting mathematical entities on a blackboard, which is upright rather than italic. It blends very well with other typefaces made by Hermann Zapf, such as Palatino, Aldus and Melior, but very badly with the default TeX font Computer Modern. All the alphabets were implemented with the computer-assisted design system Metafont developed by Knuth. Zapf designed and drew the Euler alphabets in 1980–81 and provided critique and advice of digital proofs in 1983 and later. The typeface family is copyright by American Mathematical Society, 1983. Euler Metafont development was done by Stanford computer science and/or digital typography students; first Scott Kim, then Carol Twombly and Daniel Mills, and finally David Siegel, all assisted by John Hobby. Siegel finished the Metafont Euler digitization project as his M.S. thesis in 1985.

TeX font metric (TFM) is a font file format used by the TeX typesetting system. It is a font metric format, not an outline font format like TrueType, because it provides only the information necessary to typeset the font such as each character's width, height and depth. The actual glyphs are stored elsewhere. This is not unique to TeX; Adobe's AFM files and Windows' PFM files use the same technique.

<span class="mw-page-title-main">FontForge</span> Font editor created by George Williams

FontForge is a FOSS font editor which supports many common font formats. Developed primarily by George Williams until 2012, FontForge is free software and is distributed under a mix of the GNU General Public License Version 3 and the 3-clause BSD license. It is available for operating systems including Linux, Windows, and macOS, and is localized into 12 languages.

<span class="mw-page-title-main">Font</span> Particular size, weight and style of a typeface

In metal typesetting, a font is a particular size, weight and style of a typeface. Each font is a matched set of type, with a piece for each glyph. A typeface consists various fonts that share an overall design.

MetaType1, also stylized as METATYPE1, is a tool for creating Type 1 fonts using MetaPost, developed by the Polish JNS team.

A Unicode font is a computer font that maps glyphs to code points defined in the Unicode Standard. The vast majority of modern computer fonts use Unicode mappings, even those fonts which only include glyphs for a single writing system, or even only support the basic Latin alphabet. Fonts which support a wide range of Unicode scripts and Unicode symbols are sometimes referred to as "pan-Unicode fonts", although as the maximum number of glyphs that can be defined in a TrueType font is restricted to 65,535, it is not possible for a single font to provide individual glyphs for all defined Unicode characters. This article lists some widely used Unicode fonts that support a comparatively large number and broad range of Unicode characters.

Apple's Macintosh computer supports a wide variety of fonts. This support was one of the features that initially distinguished it from other systems.

<span class="mw-page-title-main">Microsoft Sans Serif</span> Vectorised TrueType font introduced with Microsoft Windows

Microsoft Sans Serif is a TrueType font introduced with early Windows versions. It is the successor of MS Sans Serif, a proportional bitmap font introduced in Windows 1.0. Both fonts are very similar in design to Arial and Helvetica. This font was made to match the MS Sans bitmap included in the early releases of Microsoft Windows.

PostScript fonts are font files encoded in outline font specifications developed by Adobe Systems for professional digital typesetting. This system uses PostScript file format to encode font information.

Kochi (東風フォント) was a font development project to build free replacements of proprietary fonts such as MS Gothic or MS Mincho, developed by Yasuyuki Furukawa. The project consisted of the Kochi Gothic and Kochi Mincho fonts. It was released in the public domain.

References

  1. Rokicki, Tomas (1985). "Packed (PK) Font File Format" (PDF). TUGboat. 6 (3): 115–120. Archived (PDF) from the original on 2011-11-06. Retrieved 2019-08-09.
  2. Knuth, Donald (1986). The METAFONTbook. Addison Wesley. ISBN   0-201-13445-4.
  3. Knuth, Donald (January 25, 2000). "Interview: Donald E. Knuth" (Interview). Interviewed by Advogato. Archived from the original on January 22, 2009. Retrieved January 13, 2016. ...the mathematics is really simple for a quadratic. The corresponding thing for a cubic is six times as complicated...
  4. CSTUG, Charles University, Prague, March 1996, Questions and Answers with Prof. Donald E. Knuth, reproduced in TUGboat 17 (4) (1996), 355–67. Citation is from page 361. Available online at
  5. Hoefler, Jonathan. "Knuth's idea that letters start with skeletal forms is flawed. But his work is important and had lasting impact". Twitter. Retrieved 18 August 2018.
  6. Knuth, Donald (1982). "The Concept of a Meta-Font" (PDF). Visible Language. 16 (1): 3–27. Archived from the original (PDF) on 2020-07-07.
  7. "Knuth: Computers and Typesetting".
  8. 1 2 Christophe Grandsir. "METAFONT Tutorial".
  9. "CTAN: tex-archive/fonts/utilities/metatype1".
  10. "CTAN: tex-archive/support/mf2pt1".
  11. "R. J. Kinch, "MetaFog: converting Metafont shapes to contours", TUGboat16(3), 233–43 (1995)" (PDF). Archived (PDF) from the original on 2000-10-05.
  12. K. Píška, 2004
  13. mftrace – a small Python program that lets you trace a TeX bitmap font into a PFA or PFB font (A PostScript Type1 Scalable Font) or TTF (TrueType) font. It is licensed under the GNU GPL. Created by Han-Wen Nienhuys. Versions prior to 1.0.5 were called 'pktrace'.
  14. TeXtrace – a collection of scripts for UNIX that convert any TeX font into a Type1 .pfb outline font immediately suitable for use with dvips, pdftex, acroread (and any many other programs). Created by Peter Szabo.

Sources