ArticleslgStudy

computer science

Metafont

Metafont is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Metafont rather than just read about it. In short: 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.

Metafont — main illustration
Metafont — illustration

Key takeaways

  • Metafont belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Metafont to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Metafont from memory before moving on to harder problems.

Reference excerpt

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. 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 x2−x1=x3−x2 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 y1=y2=y3. 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:

NAME.NNNNgf – File with raster output at resolution NNNN (2602 by default). NAME.tfm – File with TeX font metric information, which is the information TeX needs. Usually metafont has to be told to generate this file. NAME.log – Log file output from processing 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. 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.

Variables and equations Variables in Metafont can be of eight different types:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Metafont

Start with the simplest possible case. Write down what Metafont claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Metafont before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Metafont ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Metafont

In research
Metafont appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Metafont in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Metafont is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer-related introductions in 1977, Cross-platform software, Declarative programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Metafont outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Metafont” →

How to study Metafont in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Metafont means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Metafont out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Metafont in simple terms?

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.

Why does Metafont matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Metafont?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Metafont.

Tags

  • Computer-related introductions in 1977
  • Cross-platform software
  • Declarative programming languages
  • Donald Knuth
  • Font formats
  • Free TeX software

Keep exploring