module:extras/meshes/text.TextFont

Back to documentation index.

### new module:extras/meshes/text.TextFont()

Represents a bitmap font, which supports drawing two-dimensional text. This class supports traditional bitmap fonts and signed distance field fonts.

Bitmap fonts consist of a font definition file and one or more textures containing the shape of each font glyph. The glyphs are packed so that the glyphs don’t overlap each other.

In a signed distance field font, each pixel’s alpha value depends on the distance from that location to the edge of the glyph. A pixel alpha less than 0.5 (127 in most image formats) means the pixel is outside the glyph, greater than 0.5 means the pixel is inside the glyph, and 0 (for outside the glyph) and 1 (for inside the glyph) means the pixel is outside a buffer zone formed by the glyph’s outline. Each glyph is usually given extra space to accommodate the signed distance field information.

The font definition file formats supported are text (“.fnt”), JSON (“.json”), binary (“.fnt” or “.bin”), and XML (“.xml”). The text and binary file formats are specified at this page. The XML format is very similar to the text file format. The JSON format is described at this page.

See this page for a list of bitmap font generation tools. (No one tool is recommended over any other, and the mention of this link is not an endorsement or sponsorship of any particular tool.)

NOTE: The constructor should not be called directly by applications. Use the TextFont.load method to get an TextFont object. This constructor’s parameters are undocumented and are subject to change.

This class is considered a supplementary class to the Public Domain HTML 3D Library and is not considered part of that library.

To use this class, you must include the script “extras/meshes/text.js”; the class is not included in the “h3du_min.js” file which makes up the HTML 3D Library. Example:

<script type="text/javascript" src="extras/meshes/text.js"></script>

Methods

### (static) module:extras/meshes/text~TextFont.loadData(data, fontFileName)

Loads a bitmap font definition from a file. Note that this method only loads the font data and not the bitmaps used to represent the font.

Parameters

Return Value

Text font data, or null if an error occurs. (Type: TextFont null)

### module:extras/meshes/text~TextFont#loadTextures(textureLoader)

Loads the texture files used by this font object.

Parameters

Return Value

A promise as described in getPromiseResultsAll. If the promise resolves, each item in the resulting array will be a loaded Texture object. (Type: Promise.<H3DU.Texture>)

### (static) module:extras/meshes/text~TextFont.loadWithTextures(fontFileName, textureLoader)

Loads a bitmap font definition from a file along with the textures used by that font.

Parameters

Return Value

A promise that is resolved when the font data and textures are loaded successfully, and is rejected when an error occurs. If the promise is resolved, the result will be an object with the following keys:<ul>

  • url - The URL of the font data file.
  • font - The font data in the form of an TextFont object.
  • textures - An array of Texture objects used by the font, in the order in which they are declared in the font data file. </ul> (Type: Promise) ### module:extras/meshes/text~TextFont#makeTextMeshes(str, params) Creates an array of meshes containing the primitives needed to draw text with this font. #### Parameters * `str` (Type: string)
    The text to draw. Line breaks ("\n", "\r", "\r\n") are recognized by this method. * `params` (Type: Object)
    An object whose keys have the possibilities given later, and whose values are those allowed for each key.