import 'dart:typed_data'; import 'font_file_info.dart'; /// One file the user picked, with the bytes the picker read. class PickedFontFile { const PickedFontFile({required this.name, required this.bytes}); final String name; final Uint8List bytes; } /// Why picked files cannot be added; [message] is shown as is. class CustomFontRejected implements Exception { const CustomFontRejected(this.message); final String message; @override String toString() => message; } /// The limits that keep added fonts within what a device and a browser's /// storage hold comfortably. abstract final class CustomFontLimits { static const int maxFileBytes = 10 * 1024 * 1024; static const int maxTotalBytes = 40 * 1024 * 1024; static const int maxFilesPerFamily = 16; static const int maxNameLength = 64; static const List extensions = ['ttf', 'otf']; /// Why [name] cannot name a font, or null when it can. static String? nameProblem(String name) { final trimmed = name.trim(); if (trimmed.isEmpty) return 'Enter a name.'; if (trimmed.length > maxNameLength) { return 'Use at most $maxNameLength characters.'; } return null; } } /// A face that passed inspection but is not stored yet. class DraftFontFace { const DraftFontFace({ required this.fileName, required this.bytes, required this.info, }); final String fileName; final Uint8List bytes; final FontFileInfo info; String get styleName => fontStyleName(info.weight, italic: info.italic); } /// Picked files grouped into one family, ready for the user to name. class CustomFontDraft { const CustomFontDraft({required this.suggestedName, required this.faces}); /// The family the files name most often, else the first file's name /// without its extension. final String suggestedName; final List faces; int get size => faces.fold(0, (total, face) => total + face.bytes.length); /// Checks [files] and reads their names and weights. [storedBytes] is what /// added fonts already occupy. Throws [CustomFontRejected] naming the /// first problem; nothing is stored either way. static CustomFontDraft inspect( List files, { int storedBytes = 0, }) { if (files.isEmpty) throw const CustomFontRejected('No font file chosen.'); if (files.length > CustomFontLimits.maxFilesPerFamily) { throw const CustomFontRejected( 'Choose at most ${CustomFontLimits.maxFilesPerFamily} files at once.', ); } final faces = []; for (final file in files) { final name = file.name; final dot = name.lastIndexOf('.'); final extension = dot < 0 ? '' : name.substring(dot + 1).toLowerCase(); if (!CustomFontLimits.extensions.contains(extension)) { throw CustomFontRejected('“$name” is not a .ttf or .otf font file.'); } if (file.bytes.length > CustomFontLimits.maxFileBytes) { throw CustomFontRejected( '“$name” is larger than ${_mb(CustomFontLimits.maxFileBytes)}.', ); } final FontFileInfo info; try { info = FontFileInfo.parse(file.bytes); } on FormatException catch (error) { throw CustomFontRejected('“$name” cannot be used: ${error.message}'); } final face = DraftFontFace(fileName: name, bytes: file.bytes, info: info); final twin = faces.where( (f) => f.info.weight == info.weight && f.info.italic == info.italic, ); if (twin.isNotEmpty) { throw CustomFontRejected( '“${twin.first.fileName}” and “$name” are both ${face.styleName}; ' 'add them as separate fonts.', ); } faces.add(face); } final draft = CustomFontDraft( suggestedName: _suggestName(faces), faces: faces, ); if (storedBytes + draft.size > CustomFontLimits.maxTotalBytes) { throw CustomFontRejected( 'Added fonts can use up to ${_mb(CustomFontLimits.maxTotalBytes)} ' 'on this device; remove one first.', ); } return draft; } static String _suggestName(List faces) { final counts = {}; for (final face in faces) { final family = face.info.family; if (family != null) counts[family] = (counts[family] ?? 0) + 1; } if (counts.isNotEmpty) { return counts.entries.reduce((a, b) => b.value > a.value ? b : a).key; } final name = faces.first.fileName; final dot = name.lastIndexOf('.'); final stem = (dot > 0 ? name.substring(0, dot) : name).trim(); return stem.length > CustomFontLimits.maxNameLength ? stem.substring(0, CustomFontLimits.maxNameLength) : stem; } static String _mb(int bytes) => '${bytes ~/ (1024 * 1024)} MB'; }