The Metafont language is an
interpreted language for programs that are
essentially declarative rather than imperative.
Variables and equations Variables in Metafont can be of eight different types: • Numeric:
fixed-point signed numbers with an
epsilon of 2^{-16}, capped to be less than 4096 (
Q12.16). This is the default for variables not declared to be of another type. • Pair: a pair of numerics, used primarily for representing points in the plane. • Path: as in
PostScript/
PDF/
SVG, a
parametric curve in the plane whose coordinate functions are
piecewise cubic polynomials. As in those other systems, path segments are encoded as
Bézier curves in terms of knots and control points. • Transform: an
affine transformation of the plane, equivalent to a "(transformation) matrix" in PostScript/PDF. • Pen: a convex polygon, representing the shape of a "pen" used for drawing. • Picture: a
raster image with a signed integer value for each pixel. •
Boolean • String (of 8-bit characters)
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. 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==