Lessons from METAFONT
A good piece of automation is like a good font: when it is working well, it is hardly noticeable; on closer inspection, it reveals its beauty. Of course, when that vital piece of automation is over-engineered or clunky
At YLD we are confronted daily with daunting questions about what should be automated, how much of it should be automated, and how much time should be spent on automating it. When we make the right decisions, we can have a huge impact on a company and the world; when we don’t, we can spend days, weeks or months slaving over tooling that no one will use.
Enter METAFONT, the original font creation automation tool. If you have ever worked with the typesetting software LaTeX, you may have heard of Metafont. If you have heard of Metafont, odds are you have become obsessed with it. If you have become obsessed with Metafont, drop me an email and let’s drown our sorrows together.
On the other hand, if you’re just curious, read on. Metafont is a beautiful example of software engineering at its most ambitious, but also its most fragile. Everyone should know about it!
What is Metafont?
Metafont is a language for creating parametric fonts written by the Stanford computer scientist Donald Knuth. It is the first and by far the most powerful format for specifying parametric fonts. It is also, by the by, an engine for solving simultaneous equations. But more on that later. First…
What is a parametric font?
If you have been following recent developments in CSS, you may be aware that parametric fonts are coming to a stylesheet near you. The CSS Fonts Module Level 4 specification defines several properties designed to work with the parametric font format OpenType 1.8:
The current way of the web is to ship several font files explicitly containing all of the font variations that are required: bold, italics, small capitals etc. These variations are then scattered like marbles across the page. By contrast the new specification will allow us to treat fonts more like plasticine: we can ship a single font file and simply mutate the appearance of it through CSS. Why is that better?
- We we have only one file so reduce page loading time.
- We can animate fonts.
- We can adjust the look of fonts to take account of ambient lighting.
- We can adjust text to its container.
- We can scale fonts nonlinearly to take account of different design requirements at different scales.
- We can adjust fonts to improve accessibility.
With just the few parameters offered by OpenType 1.8, we can have a huge impact on the appearance of a font. Now consider: fonts designed with Metafont support an infinite number of parameters!
Metafont is unusual
In OpenType 1.8 and the variable font formats that preceded it (e.g. Adobe’s Multiple Masters), glyphs are defined as outline shapes that are filled. A parameter is implemented by defining two glyphs at the opposite end of an axis of variability: the computer then uses linear interpolation to determine the intermediate forms. Metafont is different. In Metafont the glyphs are defined as the strokes of a pen, where both the shape of the strokes and the shape of the pen can be described abstractly by a set of simultaneous equations. Here is an example of a simplified Metafont program:
The advantage of this form of description is that we do not need to know the absolute coordinates of the points of the letter. As long as we can identify the relationships between some anchor points (z1 to z7) and set them down mathematically, Metafont can work out where exactly those points fall and join them with a suitable curve. In this case, the strokes we are constructing are all linear and the only parameters are the dimensions of the letter. But it would be possible to write down mathematical constructions of arbitrary complexity, parametrised in any way you like. The language has been designed to make this task as easy as possible. It allows, for example, equations to be written with a syntax this is closer to ordinary mathematical notation than most languages.
Let us run Metafont in proof mode to check what letter we have drawn:
$ mf my-letter; gftodvi my-letter; open my-letter.dvi
We need a bit of history to understand how Metafont got started.
Metafont’s author, Donald Knuth, is primarily known for his multi-volume masterpiece, the Art of Computer Programming. All Knuth’s work on typesetting and font creation software is, on the face of it, nothing more than an effort to get his book printed to a reasonable standard.
The first edition of Knuth’s books appeared in 1969, printed using metal type in a fashion not fundamentally dissimilar from the techniques developed by Gutenberg. Hundreds of years of refinement of the use of metal type had produced excellent standards in fonts and typesetting. By the time of the second edition In 1976, however, the technology had changed. A new technique called phototypesetting eliminated the need for tonnes of metal by allowing fonts to be inscribed onto a small glass disc and typeset by projection. Unfortunately, the speed with which this technology was brought to market meant the quality was relatively poor. Seeing that digital printing was on the horizon, Knuth seized the opportunity to use computers to create a system that could match the quality of the old metal type.
Knuth laboured on the first version of Metafont, and a font written in the language (‘Computer Modern’), for 5 years. In 1982 he received the proofs of the first books printed with his system. He was, reportedly, “devastated”. The quality was higher than any book produced by phototypesetting, but still not up to the standards of the old metal type. Over the following three years Knuth rewrote Metafont from the ground up and collaborated with the famous type designer Hermann Zapf to produce MetaFont84 and the final version of Computer Modern. All told it was at least 8 years of hard work.
So why did he do it? Thankfully Knuth reflected a lot on his motivations, so much so that his reflections provide useful fodder for thinking about automation in general. We can identify five distinct motivations:
A font comprises far more than the 26 letters of the alphabet and a few mathematical symbols and punctuation marks. A typical font will exist in anywhere from 10 to 300 versions: bold, italics, small capitals, etc. When the switch was made to phototypesetting, the need to create all these versions individually had massively multiplied the labour involved in transferring the old designs to the new medium. Knuth speculated that if he could capture all of the information required in a single design and produce the variations automatically, then he would be able to create fonts to a high standard without sacrificing economy and speed. A familiar story when it comes to automation.
What specific proportions make a font appear harmonious are not always obvious, but writing a metafont obliges us to specify them exactly. As Knuth put it:
“The ability to explain a design in precise terms is highly instructive”
More than that, however, Metafont forces us to come up with a generalisation about those proportions and allows us to change the parameters to see if the generalisation holds. By observing the limits of any given generalisation, we can come up with a better one. Knuth even went so far as to claim:
“The art of letter design will not be fully understood until it can be explained to a computer”
Once we have a parameterised system, we can use it to discover new and unexpected results. With Metafont, we do not need to know in advance what our font should look like. We can come up with a rough estimate, vary the parameters, and pick the outcome that we prefer. Indeed:
“An almost endless series of interesting experiments can be performed, now that Metafont is available”
Knuth often raises variants of the following question:
“The ability to manipulate lots of parameters may be interesting and fun, but does anybody really need a 6.5-point font that is one fourth of the way between Baskerville and Helvetica”
In most cases he attempts to play down simple fun in favour of the other more politically correct motivations, but it is obvious that no one would work on a project for 8 years if they did not find it interesting and fun. Indeed, Knuth plays with Metafont throughout his papers, making, as he put it, “a travesty of mathematics”:
At the end of his paper on The Concept of a Meta-Font, Knuth comes back this question and reclaims fun as a legitimate reason for introducing parametric variation, because variety is a basic human need:
“The utility of parametric variations comes from mankind’s need for variety”
Fun is in the end one of the most important motivations for creating ever more powerful automation.
Knuth was very idealistic about what the aims of automation should be. He comes back again and again to the idea that automation should capture the “substance”, the “intelligence” or the “spirit” of its object, recognising both Aristotle’s Metaphysics and Ovid’s Metamorphoses as inspiration for the name “Metafont”:
“We should copy the substance rather than the form”
“The intelligence of that letter can be captured in a way that permits us to obtain an infinite variety”
“…Capture the spirit”
Knuth is not content to have the computer be a slave; he wants to teach it to be an artist. It is surely this desire that underlies the unusual decision to have the computer construct letters by pen strokes rather than outlines. If that is right then Knuth’s choice of model has been influenced more by “spiritual” considerations than practical ones. The term “spiritual” is not used lightly. Here is how Knuth demonstrates the continuous variation of parameters:
Knuth characterises the beginning of this variable font as “old-fashioned” and the end as “hyper-modern”, writing rather poetically:
“the words come out of the past to the present, as they approach the future”
It is as if with Metafont Knuth has summoned the divine out of the distant past and into the modern world!
What went wrong?
Despite its unparalleled power, Metafont was not widely used. Knuth’s font Computer Modern was the only really popular font created with Metafont, and then largely because of its integration with Tex and its unusual attention to mathematical symbols. Given the clear benefits of parametric fonts, why was Metafont not widely adopted?
1. Failure of the model
A clue lies in the following remark:
“I spent three days and nights without sleep, trying to make the S look right”
Knuth simply could not come up with a good mathematical description of the pen strokes underlying the letter S. The best he could do was describe the outline and then fill it in, which is similar to the process that the more mainstream font formats use. Not only that, but the formula for the outline was so complex that Knuth published it as a mathematical discovery in its own right. The model of describing letters as pen strokes simply does not always allow a designer to implement what the font requires: the model fails to capture reality.
2. Ignoring the users
Designers are not mathematicians. The main reason outline fonts have come to dominate is that they are more visual and tactile, less purely conceptual (see Why Metafont Didn’t Catch On). Curious testimony of the strength of this preference comes from the story of the Nara typeface first designed in 1988 and only completed recently. Nara’s designer chose Metafont because it was free and available behind the Iron Curtain. The implementation makes no use of Metafont’s parametric features, but simply transcribes the absolute coordinates of an outline that had been drawn in advance on graph paper:
3. Means obscuring ends
If a good piece of automation should pass unnoticed, like a good font, then devoting a decade of one’s life to the printing and typography of one’s books stands as an apt emblem of the dangers of an over-investment in tooling. Knuth himself implied as much when he wrote:
“Type design can be hazardous to your other interests. Once you get hooked, you will develop intense feelings about letterforms; the medium will intrude on the messages that you read”
Ultimately, devoting so much time to Metafont at the height of his career cost Knuth the ability to finish The Art of Computer Programming. So far, only four of the projected seven volumes have been published and Knuth is around 80 years old.
When we consider Knuth’s reasons for writing Metafont the way he did, and compare them to the reasons for Metafont’s failure, a couple of tradeoffs emerge.
The first tradeoff is between experimentation and fun on the one hand, and the tendency of tooling to obscure its purpose on the other. Building systems that are more powerful than they need to be is not only fun, but often a prerequisite for devising experiments that can take us in a new direction. The difficulty of the matter comes in deciding when and how much we should automate beyond immediate business requirements.
The second tradeoff is between ignoring the users and the failure of the model on the one hand, and Knuth’s prioritisation of analysis and idealism on the other. The insistence on capturing the “intelligence” of a letter in abstract formulae is what made his program so difficult to use. In other words, Knuth’s approach was the exact opposite of user-centric design. He did not want to conform his software to his users, he wanted to transform the way that his users thought about font creation. At YLD, as consultants, balancing idealism and pragmatism is one of the most difficult challenges we face. Clients will frequently produce a set of software requirements designed to optimise the way they work at the moment, when in fact what we want to do is transform the way that they work in order to make it better.
So Metafont failed to catch on, but Knuth was onto something important. Namely, in seeking to teach a task to computers, he wanted to make humans themselves more intelligent about the way they conducted their business. And that is surely something every programmer can aspire to.
Much of the history of Metafont is summarised in Knuth’s own Lessons Learned from Metafont. His most philosophical and entertaining paper on Metafont is The Concept of a Meta-Font. Mathematical Typography and The Letter S (paywalled) contain more technical information. For a full exposition of all of Metafont’s features (there are a lot), check out the Metafont book.
If you like Metafont, check out MetaPost, a tool for creating scientific diagrams based on the Metafont engine, with Postscript output. A browser-based GUI for visual editing of Metafont fonts has also been created: Metaflop.
Although Metafont never became mainstream, hundreds of fonts have been created with it: an incredible list has been compiled by Luc Devroye.
Finally, any Latourians among you may enjoy these meta-reflections by Dexter Sinister.