import 'dart:typed_data'; import 'custom_font_draft.dart'; export 'font_file_picker_native.dart' if (dart.library.js_interop) 'font_file_picker_web.dart' show pickFontFiles; /// Opens the host's file picker for font files; null when the user cancels. /// Throws [CustomFontRejected] when a chosen file cannot be used as picked /// (too large, unreadable); any other error means the picker itself failed. typedef FontFilePicker = Future?> Function(); /// One file the host picker returned, before its bytes are read. class FontFileSource { const FontFileSource({required this.name, required this.read, this.size}); final String name; /// The size the host reports, if any; checked before [read]. final int? size; /// Reads the file; null when the host could not provide its content. final Future Function() read; } /// Turns a picker's selection into [PickedFontFile]s. An empty selection is /// a cancel (null). A file over [CustomFontLimits.maxFileBytes] is refused /// before it is read; a file whose bytes cannot be read is refused by name. /// Whether each file is a font is [CustomFontDraft.inspect]'s job. Future?> readFontSelection( List selection, ) async { if (selection.isEmpty) return null; final files = []; for (final source in selection) { final size = source.size; if (size != null && size > CustomFontLimits.maxFileBytes) { throw CustomFontRejected( '“${source.name}” is larger than ' '${CustomFontLimits.maxFileBytes ~/ (1024 * 1024)} MB.', ); } final Uint8List? bytes; try { bytes = await source.read(); } catch (_) { throw CustomFontRejected('“${source.name}” could not be read.'); } if (bytes == null) { throw CustomFontRejected('“${source.name}” could not be read.'); } files.add(PickedFontFile(name: source.name, bytes: bytes)); } return files; }