loadSdkRobotoFonts function

Future<void> loadSdkRobotoFonts()

Loads the Roboto family from the Flutter SDK's material fonts (present in every SDK's bin/cache/artifacts/material_fonts), so golden renders use real glyphs without vendoring fonts into the repo. contract card ensures the fonts exist before running the render; FLUTTER_ROOT is set by the flutter tool for test processes.

Implementation

Future<void> loadSdkRobotoFonts() async {
  final root = Platform.environment['FLUTTER_ROOT'];
  final dir = root == null || root.isEmpty
      ? null
      : '$root${Platform.pathSeparator}bin'
          '${Platform.pathSeparator}cache${Platform.pathSeparator}artifacts'
          '${Platform.pathSeparator}material_fonts';
  if (dir == null) {
    throw StateError('FLUTTER_ROOT is not set — cannot load SDK fonts for '
        'the metrics-card golden render');
  }
  // Names differ by platform: the flutter tool lowercases them locally, the
  // storage.zip ships capitalized — match case-insensitively.
  String? findFont(String name) {
    final wanted = name.toLowerCase();
    for (final entry in Directory(dir).listSync()) {
      if (entry is File &&
          entry.path.split(Platform.pathSeparator).last.toLowerCase() ==
              wanted) {
        return entry.path;
      }
    }
    return null;
  }

  Future<ByteData> read(String name) async {
    final path = findFont(name);
    if (path == null) {
      throw StateError('SDK font missing for the metrics-card golden: '
          '$name in $dir (FLUTTER_ROOT=$root).');
    }
    return ByteData.view(File(path).readAsBytesSync().buffer);
  }

  await Future.wait<void>([
    (FontLoader('Roboto')..addFont(read('roboto-regular.ttf'))).load(),
    (FontLoader('RobotoMedium')..addFont(read('roboto-medium.ttf'))).load(),
    (FontLoader('RobotoBold')..addFont(read('roboto-bold.ttf'))).load(),
  ]);
}