After CHARS (see Header and
properties) come that many glyph records, each from STARTCHAR to
ENDCHAR, then ENDFONT. A record gives the glyph’s name and
code, how far it moves the pen, the box its pixels fill, and the pixels themselves.
Coordinates
All metrics are relative to the glyph’s origin, the point on the baseline where it is drawn. x grows to the right and y grows upwards, so a negative y offset reaches below the baseline. Bitmap rows, however, are listed from the top of the bounding box down.
Glyph keywords
In this order:
| Keyword | Values | Meaning |
|---|---|---|
STARTCHAR | name | Required. The glyph name, with no spaces, such as A or quoteright: the name in the PostScript font’s encoding, or in a composite (Type 0) font a number such as a glyph ID. Version 2.1 limits it to 14 characters; 2.2 allows up to 65,535 (section 3.2). |
ENCODING | code, or -1, or -1 code | Required. See below. |
SWIDTH | x y | Scalable width: the pen movement for writing direction 0, in thousandths of the point size (the widths of the outline font’s AFM file). y is 0 for ordinary horizontal fonts. Mandatory for direction-0 fonts. |
DWIDTH | x y | Device width: the same movement in whole pixels, which is what is actually used. y is 0 for ordinary fonts. Mandatory for direction-0 fonts. |
SWIDTH1 | x y | Version 2.2: scalable width for writing direction 1 (vertical), as a vector to the next glyph’s origin. |
DWIDTH1 | x y | Version 2.2: device width for writing direction 1, in whole pixels. With METRICSSET 1 or 2 both SWIDTH1 and DWIDTH1 must be present; with 0 they should be absent (section 3.2). |
VVECTOR | x y | Version 2.2: the vector from origin 0 (horizontal writing) to origin 1 (vertical writing). With METRICSSET 1 or 2 it must be given either globally or in every glyph; a glyph’s value overrides the global one. The bitmap’s offset from origin 1 is the BBX offset minus VVECTOR. |
BBX | width height x-offset y-offset | Required. The bitmap’s size in pixels and the position of its lower-left corner relative to origin 0. |
ATTRIBUTES | four hex digits | Optional, only in the X Consortium’s 2.1 text, which does not define its meaning. Adobe’s 2.2 document does not have it. |
BITMAP | none | Required. Followed by one line per pixel row, height lines in all. |
ENDCHAR | none | Required. Ends the glyph. |
In version 2.1 SWIDTH and DWIDTH are required in
every glyph. In version 2.2 the five metric keywords may instead be given once in the
global section for the whole font, and in a font with METRICSSET 1
(vertical only) SWIDTH and DWIDTH are optional.
ENCODING
ENCODING 65: a positive code. For a font with an XLFD name it is a character in the set named byCHARSET_REGISTRYandCHARSET_ENCODING(forISO10646-1, a Unicode code point); otherwise its meaning depends on the font. Adobe’s 2.2 text defines it as the code in Adobe StandardEncoding.ENCODING -1: the glyph has no code. Software may drop it, or map it to characters by its name.ENCODING -1 65: in Adobe’s 2.2 text, a glyph not in StandardEncoding, with its index in the font’s own encoding; the X Consortium’s text treats it as the same asENCODING 65, an old form not to be used in new fonts.- Two glyphs with the same code: neither text forbids it, but a reader that fills a code table (the X loader does) lets the second replace the first. The first stays in the font and can be reached only by position, not by code.
Scalable and device widths
SWIDTH is the width the designer intended, independent of the device;
DWIDTH is the width rounded to pixels for this size and resolution.
Converting one to the other:
pixels = SWIDTH × (point size ÷ 1000) × (resolution ÷ 72)
For the 24-point, 75 dpi sample font, an SWIDTH of 355 gives
355 × 0.024 × 75 ÷ 72 = 8.875, and its DWIDTH is 8.
BITMAP rows
- Each row is written in hexadecimal, most significant bit first, with the leftmost pixel in the top bit. A set bit is a black (inked) pixel.
- A row is padded on the right with zero bits to a whole number of bytes, so it always has an even number of hex digits: a glyph 9 pixels wide uses four digits per row, with the last 7 bits 0.
- There are exactly as many rows as the
BBXheight, the first being the top of the box. - The box’s top row is at y = y-offset + height − 1, and its leftmost column at x = x-offset.
Number types and units
Adobe’s 2.2 text (sections 3.1 and 3.2) fixes the type of each glyph value:
| Keyword | Type and unit |
|---|---|
SWIDTH | Type number (real or integer), in units of 1/1000 of the glyph’s size, the widths found in AFM files. The width in printer’s points is the value times p/1000, p being the size in points. |
SWIDTH1 | Type number, and the widths are in glyph space coordinates (the 2.2 text’s own wording, with no further definition). |
DWIDTH | Integer, in device pixels. The actual device width must be a whole number of pixels. |
DWIDTH1 | Integer pixel width of the glyph in x and y. |
BBX | Width, height and the x and y offset of the lower left corner from origin 0: all integer numbers of pixels. |
Section 3.1 says FONTBOUNDINGBOX is also in integer pixel values.
Validity rules
The specification asks a reader to check these. Two of them it states as errors (ENDFONT too early, and the ENDCHAR check); for the CHARS count it only recommends error checking:
- Glyph count.
CHARSis followed by nglyphs, the number of glyphs that follow. “To make sure that the correct number of glyphs were actually read and processed, error checking is recommended at the end of the file” (2.2, section 3.2). The 2.1 text only saysCHARSgives the number of character segments that follow. - ENDFONT too early. The file is terminated with
ENDFONT; “if this is encountered before all of the glyphs have been read, it is an error condition” (2.2, section 3.2). - ENDCHAR after the bitmap. The
BBXwidth and height give exactly how much bitmap data to read. Section 4 recommends predicting that amount and usingENDCHARas the check: once the expected rows are consumed, the next line must beENDCHAR. If it is not, “either the parser is in error, the file is not complete, or it is incorrect”. - Metrics keywords. With
METRICSSET1 or 2 bothSWIDTH1andDWIDTH1must be present; with 0 both should be absent.VVECTORmust then be given globally or in every glyph.SWIDTHandDWIDTHare mandatory for every writing-direction-0 font.
Sources: Adobe Technical Note #5005 version 2.2 (22 March 1993), read in this session as text extracted from the PDF.
What the X loader accepts: glyphs
From libXfont’s bdfread.c and bdfutils.c (master branch at
gitlab.freedesktop.org, fetched raw in this session; the NetBSD xsrc copy read in an earlier session
differs only in allocation calls and, in libXfont, in two header details, none described here). How it reads lines and the header is on the
Header page: carriage returns dropped, blank and
COMMENT lines skipped, lines cut at 1023 characters.
Order and counts
CHARScomes right afterENDPROPERTIESand must hold an integer of at least 1.- Each glyph is exactly these lines in this order:
STARTCHAR(a name of at least one character; only its first 99 characters up to a space are kept),ENCODING,SWIDTH,DWIDTH,BBX, an optionalATTRIBUTES, thenBITMAP. A missing line, or one of them out of place, fails the whole font. That includes the 2.2 keywords: a glyph withSWIDTH1,DWIDTH1orVVECTORbetweenDWIDTHandBBXis rejected (“bad BBX”). ATTRIBUTESis read as four hexadecimal digits (after blanks) into a 16-bit value; a glyph without it has 0.- The number of glyphs read must equal
CHARSin both directions. Fewer is an error (“n too few characters”); aSTARTCHARwhereENDFONTis expected, afterCHARSglyphs, is an error (“more characters than specified”). The line after the last glyph must beENDFONT, or the font fails (“missing ENDFONT”). A font in which no glyph has a valid encoding loads, with a warning.
Value checks
SWIDTHandDWIDTHeach need two integers, and the y value of both must be 0 (a vertical-writing width is an error here). TheDWIDTHx must fit a signed 16-bit integer (−32768 to 32767).BBXneeds four integers. The width and the height must not be negative. The x and y offsets must each fit a signed 16-bit integer, and the offset plus the size must too (width ≤ 32767 − x offset, height ≤ 32767 − y offset); otherwise “out of range metrics”.ENCODINGtakes one or two integers. Any value below −1 is an error. With two values and a first value of −1, the second is used as the code (-1 65is read as65). A single-1means no code: the loader as called bybdfReadFontkeeps such a glyph, without an encoding slot. A code above 65535 (0xFFFF) is reported as an error message but is not fatal: the glyph is read and kept, but has no code. Codes 0 to 65535 are split into a row (high byte) and a column (low byte).
Reading the bitmap rows
- Exactly height rows are read, one per line. The number of bytes a row needs is the width in pixels divided by 8, rounded up.
- A row whose length in characters (all characters of the line, hex digits or not:
the test is
strlen) is odd gets a0added on the right (and an error message is printed, but the read goes on), so1G1is padded too. A row longer than the width needs has its extra bytes ignored. A row that is shorter is filled with zero bytes. When a row is full length or longer, any bits to the right of the glyph width in the last byte that is kept are cleared (the mask branch ofbdfReadBitmapruns whenever the row is not shorter than needed). A character that is not a hex digit is reported and then skipped without shifting the byte (bdfHexByte):1Greads as 0x01, not 0x10. - The loader reads exactly height lines as rows, then the next line must begin
ENDCHAR; otherwise the font fails (“missing ENDCHAR”). Because the rows are counted, anENDCHARthat comes early is taken as a data row. A glyph that is exactly one row short is therefore accepted: the earlyENDCHARline is read as the last row and, being the last line read, passes theENDCHARtest (bdfread.clines 130 to 182). Its row bytes are those of the textENDCHAR0(the odd length gets a 0) read with the rule above: E (N is skipped) is 0x0E, D and C are 0xDC, H is skipped and A is 0x0A, R is skipped and 0 is 0x00, cut to the row width. A glyph two or more rows short (the loader reads on into the next glyph’s lines) or with any extra row fails with “missing ENDCHAR”. - A glyph with a width of 0 has no bitmap bytes. The loader still reads at most height lines,
stopping early at a line that begins
ENDCHAR; if none of those lines wasENDCHAR, the next line must be, or the font fails (“missing ENDCHAR”). It does not read “untilENDCHAR”.
The last step: padding a constant-metric font to a terminal cell
After the glyphs are read, bdfReadFont (bdfread.c, lines 865 to 905) does more than store
them. It sets the default glyph from DEFAULT_CHAR, computes the font’s bounds
(bitmapComputeFontBounds, bitmaputil.c) and then asks
FontCouldBeTerminal (fontaccel.c). When that is true it calls
bdfPadToTerminal (bdfread.c, from line 954) and computes the bounds again. So for these fonts
the glyphs the X server holds are not the glyphs of the file.
The bounds are taken over the encoded glyphs (those with a code from 0 to 65535 that the font’s code table
reaches; a later glyph with the same code replaces an earlier one), and a glyph whose ascent, descent, left bearing,
right bearing and width are all 0 is left out of them. For each glyph the loader’s metrics are: left bearing = the
BBX x offset, right bearing = x offset + width, ascent = height + y offset, descent = −y offset, character
width = the DWIDTH x (bdfread.c lines 479 to 483). FontCouldBeTerminal is true when
all of these hold:
- the smallest left bearing is 0 or more, and the largest right bearing is no more than the largest character width;
- the smallest and the largest character width are equal (a constant-width font);
- the largest ascent is no more than
FONT_ASCENTand the largest descent no more thanFONT_DESCENT; - and the font is not already a terminal font: the largest left bearing is not 0, or the smallest right bearing is not
the width, or the smallest ascent is not
FONT_ASCENT, or the smallest descent is notFONT_DESCENT; - and it is not “a font with nothing but a SPACE”: a font whose largest ascent and largest descent are both 0 is not padded.
bdfPadToTerminal then makes one new glyph shape and gives it to every glyph of the font, encoded
or not: ascent = FONT_ASCENT, descent = FONT_DESCENT, left bearing 0, right bearing = the
smallest character width, character width = the same. Each glyph’s bitmap is reshaped into that cell by
FontCharReshape (fontink.c): it clears the new bitmap and copies every set pixel that lies inside
the overlap of the old and the new box to the same place relative to the origin and the baseline, so a pixel outside the new
box would be dropped. The old bitmap is freed; the glyph’s attributes are kept; the stored bitmap sizes are recomputed for the
new cell. Because the conditions put every glyph’s ink inside the cell (left bearing ≥ 0, right bearing ≤ the width,
ascent ≤ FONT_ASCENT, descent ≤ FONT_DESCENT), nothing is dropped: the glyphs gain blank rows and
columns. What changes is that every glyph
then has the same metrics, so the server treats the font as a constant-metrics terminal font
(FontComputeInfoAccelerators sets constantMetrics and terminalFont), and for a
constant-metrics font bitmapAddInkMetrics adds the ink bounds of each glyph afterwards.
Fonts whose glyphs already all have the full cell as their BBX are untouched: the X.Org fixed fonts
6x13, 7x13, 9x15, 10x20, 6x10, 8x13B, 12x13ja and 18x18ko are of that kind. A font drawn tight to each glyph in a constant
advance, such as a typical converted terminal font, is padded. The viewer shows the padding in the Font tab’s “X server
behaviour” section when it applies, with the cell and the number of glyphs whose BBX is not already the
cell, and draws the cell as an orange frame around every glyph on the glyph sheets. Each bitmap is copied to the
same place relative to the origin and the baseline, so the padded glyph is the glyph’s own ink inside that
frame.
Example
The two glyphs of the sample font in the BDF 2.1 specification (its header is on the Header and properties page):
STARTCHAR j
ENCODING 106
SWIDTH 355 0
DWIDTH 8 0
BBX 9 22 -2 -6
BITMAP
0380
0380
0380
0380
0000
0700
0700
0700
0700
0E00
0E00
0E00
0E00
0E00
1C00
1C00
1C00
1C00
3C00
7800
F000
E000
ENDCHAR
STARTCHAR quoteright
ENCODING 39
SWIDTH 223 0
DWIDTH 5 0
BBX 4 6 2 12
ATTRIBUTES 01C0
BITMAP
70
70
70
60
E0
C0
ENDCHAR
ENDFONT
| Line | Meaning |
|---|---|
ENCODING 106 | Code 106 in ISO 8859-1: the letter j. |
DWIDTH 8 0 | After drawing, the pen moves 8 pixels right. |
BBX 9 22 -2 -6 | A box 9 × 22 pixels, starting 2 pixels left of the origin and 6 below the baseline, so it reaches up to y = 15. |
0380 | 0000 0011 1000 0000: of the 9 pixels, columns 6, 7 and 8 are set (the dot). |
0000 | The gap between the dot and the stem. |
| rows 17 to 22 | The last six rows (1C00 1C00 3C00 7800 F000 E000) lie below the baseline and draw the tail, which reaches left of the origin into the space of the glyph before. |
BBX 4 6 2 12 | The apostrophe is a 4 × 6 box from x = 2, sitting 12 to 17 pixels above the baseline. |
Drawn out, with # for a set bit, the first 9 bits of each row of
j give:
......### 0380 × 4
......... 0000
.....###. 0700 × 4
....###.. 0E00 × 5
...###... 1C00 × 4 (the last two of these below the baseline)
..####... 3C00
.####.... 7800
####..... F000
###...... E000
The 2.2 specification’s example of a glyph with metrics for both directions (figure 4):
STARTCHAR c133
ENCODING 39
SWIDTH 1000 0
DWIDTH 22 0
BBX 17 17 2 -5
SWIDTH1 0 -1000
DWIDTH1 0 -22
VVECTOR 10 14
Written horizontally it advances 22 pixels to the right; written vertically it advances 22 pixels down, measured from origin 1, which is 10 pixels right of and 14 above origin 0. Seen from origin 1, the bitmap’s lower-left corner is at (2 − 10, −5 − 14) = (−8, −19).