loadSystemFontsImpl function

Future<FontFallbackChain> loadSystemFontsImpl({
  1. List<String>? preferredFonts,
  2. int maxFonts = 5,
  3. GraphicsFileSystem? filesystem,
})

Implementation

Future<FontFallbackChain> loadSystemFontsImpl({
  List<String>? preferredFonts,
  int maxFonts = 5,
  GraphicsFileSystem? filesystem,
}) async {
  final chain = FontFallbackChain();
  final system = filesystem ?? const LocalGraphicsFileSystem();
  final platform = Platform.operatingSystem;
  final directories = _directories[platform] ?? [];

  final names = preferredFonts ?? _defaults;
  var loaded = 0;

  for (final name in names) {
    if (loaded >= maxFonts) break;

    // The first file that loads, through the file system, wins the name;
    // a missing or unreadable file moves on to the next candidate
    search:
    for (final base in directories) {
      final directory = base.replaceFirst(
        '~',
        Platform.environment['HOME'] ?? '',
      );

      for (final extension in ['.ttf', '.otf', '.TTF', '.OTF']) {
        final path = '$directory$name$extension';
        final result = await chain.load(path, filesystem: system);
        if (result is Ok) {
          loaded++;
          logger.info('Loaded system font: $path');
          break search;
        }
      }
    }
  }

  if (loaded == 0) {
    logger.warning('No system fonts could be loaded');
  }

  return chain;
}