PixelFontBuilder
Description
Builds pixel fonts that can be loaded with PixelFontInfo.For and Graphics.LoadFontFrom.
To build a pixel font, the following steps are necessary:
-
Create a PixelFontBuilder object.
-
Use Baseline or AutoBaseline to specify the font baseline.
-
Use Compact and/or AutoCompact to specify the font compact height.
-
Use KerningPairs and Spacing to control the horizontal spacing between glyph pairs.
-
Use PowerOfTwo to control the dimensions of the pixel font image.
-
Use Trim to specify the trimming behaviour for glyph pixels.
-
Use Whitespace to specify the whitespace width to use for the font.
-
Use FilterCategories, FilterHeight and FilterIgnore to configure the filter that decides which characters will be included in the pixel font.
-
Use Build to build a pixel font image, which will contain all font glyphs as well as the corresponding metadata (see below for details).
-
Write the pixel font image to a
.pxb
file (or any other image format that supports 8-bit grayscale images), for example by using ColorBuffer.WritePXB1 -
Use Graphics.LoadFontFrom or PixelFont.HandleFile to load a pixel font from its image file.
A pixel font image holds the actual font glyphs as well as additional metadata, which is encoded as 8-bit grayscale values. The following logic may be used to iterate over all font glyphs and to decode the metadata:
-
Define
V(x,y)
as…-
the 8-bit pixel value at
(x,y)
, if inside of the font image. -
255, if
(x,y)
lies outside of the font image.
-
-
Let
X
be 0.
LetY
be 0.
LetDy
be 0. -
If
V(X,Y)
is not 255, goto 6. -
Let
X
be 0.
LetY
be the sum ofDy
andY
.
LetDy
be 0. -
If
V(X,Y)
is 255, goto 9. -
The pixel at
(X,Y)
represents the top-left corner of a font glyph specification rectangle, which encodes glyph metadata in the top-most pixel row:Rw := 255 - V(X+0,Y) Rh := 255 - V(X+1,Y) W := 255 - V(X+2,Y) Ox := 255 - V(X+3,Y) Oy := 255 - V(X+4,Y) Ch := 255 - V(X+5,Y) Cl := 255 - V(X+6,Y)
These metadata values correlate with PixelFontGlyph attributes as follows:
X
becomes Box2I.X1 of PixelFontGlyph.Bounds.
Y+1
becomes Box2I.Y1 of PixelFontGlyph.Bounds.
Rw
becomes Box2I.Width of PixelFontGlyph.Bounds.
Rh
becomes Box2I.Height of PixelFontGlyph.Bounds.
W
becomes PixelFontGlyph.Width.
Ox
becomes Vec2I.X of PixelFontGlyph.Offset.
Oy
becomes Vec2I.Y of PixelFontGlyph.Offset.
Ch*256+Cl
becomes PixelFontGlyph.Character. -
Let
Dx
be the maximum ofRw
and7
.
LetDy
be the maximum ofRh+1
andDy
.
LetX
be the sum ofDx
andX
. -
Continue with step 3.
-
Let
B
be a empty sequence of unsigned 8-bit integer values. -
Start at
(X+1,Y)
to iterate in row-major order over all remaining pixels of the font image and append255 - V(x,y)
toB
for each pixel. -
Consume the byte stream
B
according to this structure:Block | Count --------+------ VERSION | 1 HEADER | 1 KERNING | 0..n FOOTER | 1 VERSION | Type | Meaning --------+-----------+------------------------------- 0 | uint8 | block ID (always 1) 1 | uint8 | data stream version (always 1) HEADER | Type | Meaning --------+-----------+------------------------------- 0 | uint8 | block ID (always 2) 1 | uint8 | font height 2 | uint8 | font ascent 3 | uint8 | compact height 4 | uint8 | whitespace width 5 | int8 | glyph spacing KERNING | Type | Meaning --------+-----------+------------------------------- 0 | uint8 | block ID (always 3) 1 | uint16_be | first character 3 | uint16_be | second character 5 | int8 | kerning width FOOTER | Type | Meaning --------+-----------+------------------------------- 0 | uint8 | block ID (always 255)
- See also
Public / Methods
Glyph
Adds a glyph to the font.
This method returns false
in these cases:
-
IsIgnored returns
true
for character in. -
The glyph pixels in are empty, i.e. all pixels are white.
-
The trimmed height of the glyph pixels in is not contained in FilterHeight.
GlyphAll
Helper method that iterates over all characters in IPixelFontGlyphs.Glyphs, calls IPixelFontGlyphs.Glyph for each one and then delegates to Glyph.
GlyphRange
Helper method that iterates over all characters between a in and b in, calls IPixelFontGlyphs.Glyph for each one and then delegates to Glyph.
GlyphString
Helper method that iterates over all unique characters in value in, calls IPixelFontGlyphs.Glyph for each one and then delegates to Glyph.
IsIgnored
Checks if the given character is ignored and will thus never produce a font glyph.
A character is ignored if any of the following holds true:
-
It is contained in the set of ignored characters, see FilterIgnore.
-
The set of character categories of character in is disjoint with the set of allowed categories, see FilterCategories.
Public / Attributes
AutoBaseline
Specifies a character that will be used to set Baseline automatically when a matching glyph is being added by subsequent calls to Glyph.
Defaults to '\0'
.
AutoCompact
Specifies a set of characters that will be used to set Compact to a value greater than its current value, automatically when a matching glyph is being added by subsequent calls to Glyph.
Contains all characters of AutoCompactDefault by default.
Baseline
The font baseline, in pixels.
The value of this property will be taken into account when Build is called.
Defaults to 0
.
- See also
Compact
Specifies the compact font height.
The value of this property will be taken into account when Build is called.
Defaults to 0
.
- See also
FilterCategories
Specifies the unicode character categories to include.
Defaults to CharacterCategory.All.
FilterHeight
Specifies the allowed range of pixel font height values.
This property can be useful to quickly remove unwanted glyphs when building a font, just by having a look at the generated pixel font image, because there the font glyphs are sorted in ascending order of their trimmed pixel font glyph height.
Defaults to RangeI.Max.
KerningPairs
Returns the glyph pairs for which custom kerning width are defined.
The value of this property will be taken into account when Build is called.
The values in this map are added to the initial kerning widths that result from applying Spacing.
PowerOfTwo
Generate pixel font images that have power-of-two dimensions?
The value of this property will be taken into account when Build is called.
Defaults to true
.
Spacing
Specifies the amount of omnidirectional spacing around the pixels of each font glyph to use for computing the initial kerning width of each glyph pair.
The value of this property will be taken into account when Build is called.
The initial kerning width may be adjusted by adding custom kerning width offsets to KerningPairs, for specific glyph pairs.
Defaults to 1
.
Trim
Enables or disables horizontal trimming of glyphs for subsequent calls to Glyph.
Defaults to true
.
Whitespace
Specifies the whitespace width to use for the pixel font.
The value of this property will be taken into account when Build is called.
Defaults to 0
.