Converts a .ttf
font file into multichannel signed distance fields, then outputs packed spritesheets and a xml(.fnt} or json representation of an AngelCode BMfont.
Bitmap Font Generator About. FOSS and cross-platform alternative to Glyph Designer made with Qt. Renders specified characters with a font to one (or multiple) textures, applying several neat effects in the process and creates a lookup file containing each character's position and dimension in the textures. In the File-menu, select Import assets-Bitmap fonts. This will bring up the bitmap font import wizard. Click Load font.and navigate to the FNT-file you just generated. Once that's done, you have the option to change the name and add a filter.
Signed distance fields are a method of reproducing vector shapes from a texture representation, popularized in this paper by Valve.This tool uses Chlumsky/msdfgen to generate multichannel signed distance fields to preserve corners. The distance fields are created from vector fonts, then rendered into texture pages. A BMFont object is provided for character layout.
Install as CLI
Then you just need to call msdf-bmfont
from console to generate font file.Type in msdf-bmfont --help
for more detail usage.
Usage
CLI Examples
Generate a multi-channel signed distance field font atlas with ASCII charset, font size 42, spread 3, maximum texture size 512x256, padding 1, and save out config file:
We will get three file: atlas.0.png
atlas.0.cfg
& font.fnt
and this is the generated atlas in the minimum pot size (256x256):
Then we want to use the old setting except a different font and use monochrome signed distance field atlas, and output an extra .svg
version of atlas:
This time we get a modified atlas.0.png
with new bitmap font appended:
Not satisfied with the style? Remember we got a svg
atlas!
How about fire up some graphic editor and add some neat effect and lay on the output atlas?
Install as Module
Module usage Examples
Writing the distance fields and font data to disk:
Generating a single channel signed distance field with a custom character set:
API
generateBMFont(fontPath | fontBuffer, [opt], callback)
Renders a bitmap font from the font specified by fontPath
or fontBuffer
, with optional opt
settings, triggering callback
on complete.
Options:
outputType
(String)- type of output font file. Defaults to
xml
xml
a BMFont standard .fnt file which is wildly supported.json
a JSON file compatible with Hiero
- type of output font file. Defaults to
filename
(String)- filename of both font file and font atlas. If omited, font face name is used. Required if font is provided as a Buffer.
charset
(StringArray) - the characters to include in the bitmap font. Defaults to all ASCII printable characters.
fontSize
(Number)- the font size at which to generate the distance field. Defaults to
42
- the font size at which to generate the distance field. Defaults to
textureSize
(Array[2])- the dimensions of an output texture sheet, normally power-of-2 for GPU usage. Both dimensions default to
[512, 512]
- the dimensions of an output texture sheet, normally power-of-2 for GPU usage. Both dimensions default to
texturePadding
(Number)- pixels between each glyph in the texture. Defaults to
2
- pixels between each glyph in the texture. Defaults to
border
(Number)- space between glyphs textures & edge. Defaults to
0
- space between glyphs textures & edge. Defaults to
fieldType
(String)- what kind of distance field to generate. Defaults to
msdf
. Must be one of:msdf
Multi-channel signed distance fieldsdf
Monochrome signed distance fieldpsdf
monochrome signed pseudo-distance field
- what kind of distance field to generate. Defaults to
distanceRange
(Number)- the width of the range around the shape between the minimum and maximum representable signed distance in pixels, defaults to
3
- the width of the range around the shape between the minimum and maximum representable signed distance in pixels, defaults to
roundDecimal
(Number)- rounded digits of the output font metics. For
xml
output,roundDecimal: 0
recommended.
- rounded digits of the output font metics. For
vector
(Boolean)- output a SVG Vector file for debugging. Defauts to
false
- output a SVG Vector file for debugging. Defauts to
smart-size
(Boolean)- shrink atlas to the smallest possible square. Default:
false
- shrink atlas to the smallest possible square. Default:
pot
(Boolean)- output atlas size shall be power of 2. Default:
false
- output atlas size shall be power of 2. Default:
square
(Boolean)- output atlas size shall be square. Default:
false
- output atlas size shall be square. Default:
rot
(Boolean)- allow 90-degree rotation while packing. Default:
false
- allow 90-degree rotation while packing. Default:
rtl
(Boolean)- use RTL(Arabic/Persian) charators fix. Default:
false
- use RTL(Arabic/Persian) charators fix. Default:
The callback
is called with the arguments (error, textures, font)
error
on success will be null/undefinedtextures
an array of js objects of texture spritesheet.textures[index].filename
Spritesheet filenametextures[index].texture
Image Buffers, containing the PNG data of one texture sheet
font
an object containing the BMFont data, to be used to render the fontfont.filename
font filenamefont.data
stringified xmljson data to be written to disk
Since opt
is optional, you can specify callback
as the second argument.
License
MIT, see LICENSE.md for details.
Summary
The following table summarizes the font capabilities of the various Apacheā¢ FOP renderers:
Renderer | Base-14 | AWT/OS | Custom | Custom Embedding |
---|---|---|---|---|
yes | no | yes | yes | |
PostScript | yes | no | yes | yes |
PCL | yes (modified) | yes (painted as bitmaps) | yes (painted as bitmaps) | no |
AFP | no | no | yes | yes |
Java2D/AWT/Bitmap | if available from OS | yes | yes | n/a (display only) |
if available from OS | yes | yes | controlled by OS printer driver | |
RTF | n/a (font metrics not needed) | n/a | n/a | n/a |
TXT | yes (used for layout but not for output) | no | yes (used for layout but not for output) | no |
XML | yes | no | yes | n/a |
Note that Java2D based renderers (Java2D, AWT, Print, TIFF, PNG) support both system (AWT/OS) and custom fonts.
Base-14 Fonts
The Adobe PostScript and PDF Specification specify a set of 14 fonts that must be available to every PostScript interpreter and PDF reader: Helvetica (normal, bold, italic, bold italic), Times (normal, bold, italic, bold italic), Courier (normal, bold, italic, bold italic), Symbol and ZapfDingbats.
The following font family names are hard-coded into FOP for the Base-14 font set:
Base-14 font | font families |
---|---|
Helvetica | Helvetica, sans-serif, SansSerif |
Times | Times, Times Roman, Times-Roman, serif, any |
Courier | Courier, monospace, Monospaced |
Symbol | Symbol |
ZapfDingbats | ZapfDingbats |
Please note that recent versions of Adobe Acrobat Reader replace 'Helvetica' with 'Arial' and 'Times' with 'Times New Roman' internally. GhostScript replaces 'Helvetica' with 'Nimbus Sans L' and 'Times' with 'Nimbus Roman No9 L'. Other document viewers may do similar font substitutions. If you need to make sure that there are no such substitutions, you need to specify an explicit font and embed it in the target document.
Missing Fonts
When FOP does not have a specific font at its disposal (because it's not installed in the operating system or set up in FOP's configuration), the font is replaced with 'any'. 'any' is internally mapped to the Base-14 font 'Times' (see above).
Missing Glyphs
Every font contains a particular set of glyphs. If no glyph can be found for a given character, FOP will issue a warning and use the glpyh for '#' (if available) instead. Before it does that, it consults a (currently hard-coded) registry of glyph substitution groups (see Glyphs.java in Apache XML Graphics Commons). This registry can supply alternative glyphs in some cases (like using space when a no-break space is requested). But there's no guarantee that the result will be as expected (for example, in the case of hyphens and similar glyphs). A better way is to use a font that has all the necessary glyphs. This glyph substitution is only a last resort.
System Fonts
Support for system fonts relies on the Java AWT subsystem for font metric information. Through operating system registration, the AWT subsystem knows what fonts are available on the system, and the font metrics for each one.
When working with renderers that supports system fonts (see above table) and you're missing a font, you can just install it in your operating system and it should be available for these renderers. Please note that this is not true for output formats, such as PDF or PostScript, that only support custom fonts.
Custom Fonts
Renderer | TTF | TTC | Type1 | OTF | AFP Fonts |
---|---|---|---|---|---|
yes | yes | yes | yes | no | |
Postscript | yes | yes | yes | yes | no |
AFP | yes | no | no | no | yes |
PCL | yes | yes | bitmap | bitmap | no |
TIFF | bitmap | bitmap | bitmap | bitmap | no |
Support for custom fonts is highly output format dependent (see above table). This section shows how to add Type 1, TrueType (TTF) and OpenType (OTF) fonts to the PDF, PostScript and Java2D-based renderers. Other renderers (like AFP) support other font formats. Details in this case can be found on the page about output formats.
In earlier FOP versions, it was always necessary to create an XML font metrics file if you wanted to add a custom font. This inconvenient step has been removed and in addition to that, FOP supports auto-registration of fonts, i.e. FOP can find fonts installed in your operating system or can scan user-specified directories for fonts. Font registration via XML font metrics has been deprecated and is not recommended although it is still supported by the current code.
Create Bitmap Fonts
More information about fonts can be found at Adobe Font Technical Notes.
OpenType Advanced Font Features
OpenType fonts support advanced layout features such as ligatures, small caps, swashes, alternates, old style figures and more. Please see Advanced Typographic Extensions - OpenType Layout. These features are currently missing within FOP due to the implementation opting to favor a greater number of formats.
While FOP may support embedding OpenType with advanced features in the future, the current method extracts the Compact Font Format (CFF) data containing among other things the character definitions, optionally subsets and embeds the result as a Type1C font. This allows the font to be used by both Postscript and older PDF versions while losing the features mentioned above. This is because embedding Open-Type in it's original state is only supported by PDF 1.6 and above.
Bulk Font Configuration
If you want FOP to use custom fonts, you need to tell it where to find them. This is done in the configuration file and once per renderer (because each output format is a little different). For convenience, FOP allows bulk registering of fonts; you can either tell FOP to find your operating system fonts or you can specify directories that it will search for support fonts. These fonts will then automatically be registered.
Register Fonts with FOP
You must tell FOP how to find and use the font files by registering them in the FOP Configuration. Add entries for your custom fonts, regardless of font type, to the configuration file in a manner similar to the following:
The 'embed-url' attribute is used to specify the font file. Relative URLs are resolved relative to the font-base property (or base) if available. See FOP: Configuration for more information.
The 'embedding-mode' attribute is optional and can take two values: subset (the default) and full. If not specified the font is subset embedded for TTF and OTF or full embedded for Type 1, unless it is explicitly referenced (see below).
The font 'kerning' attribute is optional. Default is 'true'.
The 'embed-as-type1' attribute is optional, setting to 'true' converts OTF fonts to Type 1 for postscript output.
The 'simulate-style' attribute is optional, setting to 'true' generates bold and oblique looking glyphs for PDF output.
When setting the 'embed-url' attribute for Type 1 fonts, be sure to specify the PFB (actual font data), not the PFM (font metrics) file. If the PFM (or AFM) file is in a different location (i.e., not in the same directory) then you need to specify an 'embed-url-pfm' (or 'embed-url-afm') attribute next to the 'embed-url' one.
The attribute 'encoding-mode' is optional an may have the following values:
auto: default font encoding mode ('cid' for Truetype, 'single-byte' for Type 1)
single-byte: use single-byte encodings in the target format (if applicable)
cid: encode as CID-keyed font (currently only supported for PDF output with TrueType fonts)
The fonts 'directory' tag can be used to register fonts contained within a single or list of directory paths. The 'recursive' attribute can be specified to recursively add fonts from all sub directories.
The fonts 'auto-detect' tag can be used to automatically register fonts that are found to be installed on the native operating system.
Fonts registered with 'font' tag configurations override fonts found by means of 'directory' tag definitions.
Fonts found as a result of a 'directory' tag configuration override fonts found as a result of the 'auto-detect' tag being specified.
If relative URLs are specified, they are evaluated relative to the value of the 'font-base' setting. If there is no 'font-base' setting, the fonts are evaluated relative to the base directory.
If a fop.xconf is not used, or the 'embed-url' attribute is missing, the fonts are referenced (and the default Base-14 is used in this case).
TrueType Collections
TrueType collections (.ttc files) contain more than one font. The individual sub-fonts of a TrueType Collection can be selected using the 'sub-font' attribute on the 'font' element. Example:
Auto-Detect and auto-embed feature
When the 'auto-detect' flag is set in the configuration, FOP will automatically search for fonts in the default paths for your operating system.
FOP will also auto-detect fonts which are available in the classpath, if they are described as 'application/x-font' in the MANIFEST.MF file. For example, if your .jar file contains font/myfont.ttf:
Bitmap Font Generator Xml Generator
This feature allows you to create JAR files containing fonts. The JAR files can be added to fop by providem them in the classpath, e.g. copying them into the lib/ directory.
The font cache
Bitmap Font Creator
Apache FOP maintains a cache file that is used to speed up auto-detection. This file is usually found in the '.fop' directory under the user's home directory. It's called 'fop-fonts.cache'. When the user's home directory is not writable, the font cache file is put in the directory for temporary files.
If there was a problem loading a particular font, it is flagged in the cache file so it is not loaded anymore. So, if a font is actually around but is still not found by Apache FOP, it's worth a try to delete the font cache file which forces Apache FOP to reparse all fonts.
Referencing Fonts
By default, all fonts are embedded if an output format supports font embedding. In some cases, however, it is preferred that some fonts are only referenced. When working with referenced fonts it is important to be in control of the target environment where the produced document is consumed, i.e. the necessary fonts have to be installed there.
There are two different ways how you can specify that a font should be referenced:
When explicitly configuring a font, font referencing is controlled by the embed-url attribute. If you don't specify the embed-url attribute the font will not be embedded, but will only be referenced.
For automatically configured fonts there's a different mechanism to specify which fonts should be referenced rather than embedded. This is done in the 'referenced-fonts' element in the configuration. Here's an example:
At the moment, you can only match fonts against their font-family. It is possible to use regular expressions as is shown in the second example above ('DejaVu.*'). The syntax for the regular expressions used here are the one used by the package. So, in the above snippet 'Helvetica' and all variants of the 'DejaVu' font family are referenced. If you want to reference all fonts, just specify font-family='.*'
.
The referenced-fonts
element can be placed either inside the general fonts
element (right under the root) or in the fonts
element under the renderer configuration. In the first case, matches apply to all renderers. In the second case, matches only apply to the renderer where the element was specified. Both cases can be used at the same time.
Embedding Fonts
Some notes related to embedded fonts:
When FOP embeds a font in PDF, it adds a prefix to the fontname to ensure that the name will not match the fontname of an installed font. This is helpful with older versions of Acrobat Reader that preferred installed fonts over embedded fonts.
When embedding PostScript fonts, the entire font is always embedded.
When embedding TrueType (ttf) or TrueType Collections (ttc), a subset of the original font, containing only the glyphs used, is embedded in the output document. That's the default, but if you specify encoding-mode='single-byte' (see above), the complete font is embedded.
Font Substitution
When a <substitutions/>
section is defined in the configuration, FOP will re-map any font-family references found in your FO input to a given substitution font.
If a
<substitution/>
is declared, it is mandatory that both aand child element is declared with a font-family attribute. Both font-weight and font-style are optional attributes, if they are provided then a value of 'normal' is assumed.
For example you could make all FO font-family references to 'Arial' with weights between 700 and 900 reference the normal 'Arial Black' font.
Font Selection Strategies
There are two font selection strategies: character-by-character or auto. The default is auto.
Auto selected the first font from the list which is able to display the most characters in a given word. This means (assume font A has characters for abclmn, font B for lnmxyz, fontlist is A,B):
aaa lll xxx would be displayed in fonts A A B
aaaxx would be displayed in font A
aaaxxx would be displayed in font A
aaaxxxx would be displayed in font B
Character-by-Character is NOT yet supported!
Font List Command-Line Tool
FOP contains a small command-line tool that lets you generate a list of all configured fonts. Its class name is: org.apache.fop.tools.fontlist.FontListMain
. Run it with the '-?' parameter to get help for the various options.