loadFont method

BitmapFont loadFont(
  1. String path,
  2. double size, {
  3. bool antiAlias = true,
  4. String containedAsciiCharacters = BitmapFont.extendedAscii,
})

Loads a TrueType Font and creates BitmapFont out of it.

  • path: The path to the font file (e.g., .ttf, .woff2).
  • size: The desired font size in pixels.
  • antiAlias: Whether to use anti-aliasing when rendering the font atlas. Defaults to true.
  • containedAsciiCharacters: A string of characters to include in the font atlas. Defaults to BitmapFont.extendedAscii.

Returns the loaded or cached BitmapFont.

Implementation

BitmapFont loadFont(
  String path,
  double size, {
  bool antiAlias = true,
  String containedAsciiCharacters = BitmapFont.extendedAscii,
}) {
  var font = BitmapFont(_renderer);

  var hash = "${path}_${size}_${antiAlias}_$containedAsciiCharacters".hashCode;

  if (_fontCache.containsKey(hash)) {
    return _fontCache[hash]!;
  } else {
    _fontCache[hash] = font;
    var loadingState = _loadingInfo.add(path);

    _fileLoader.loadBytes(path).then((bytes) {
      _fontRasterizer.rasterize(bytes, size, containedAsciiCharacters, antiAlias).then((rasterized) {
        font.generateAtlasFromRasterized(rasterized, antiAlias);
        loadingState.completedOrFailed = true;
      });
    }).catchError((e) {
      warn("Error loading font: $path", e);
      loadingState.completedOrFailed = true;
    });
  }

  return font;
}