import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; /// Paper's reading serif (fonts/README.md): Source Serif 4. const String paperSerifFamily = 'Source Serif 4'; const String _directory = 'fonts/source-serif-4'; /// The licence shipped beside the files, registered with the app's other /// font licences. const String paperSerifLicenseAsset = '$_directory/OFL.txt'; /// Every face of [paperSerifFamily]: regular, italic, semibold and semibold /// italic. The loader reads each file's own weight and style. const List paperSerifAssets = [ '$_directory/SourceSerif4-Regular.ttf', '$_directory/SourceSerif4-It.ttf', '$_directory/SourceSerif4-Semibold.ttf', '$_directory/SourceSerif4-SemiboldIt.ttf', ]; Future? _loading; /// Registers [paperSerifFamily] the first time Paper's theme is built. The /// files are plain assets, not a `fonts:` family, so the web build fetches /// them only for a device that chooses Paper. Once registered the engine /// reports a font change and text lays out again in the serif; until then /// it reads in the interface face (the reading style's fallback). A failed /// load is logged and the interface face stays. Future ensurePaperFonts() => _loading ??= _load(); Future _load() async { try { final loader = FontLoader(paperSerifFamily); for (final asset in paperSerifAssets) { loader.addFont(rootBundle.load(asset)); } await loader.load(); } catch (error) { debugPrint('Paper serif font did not load: $error'); } }