import '../font_choice.dart'; import 'font_file_info.dart'; /// One file of a [CustomFont]: where its bytes are stored and the weight /// and style it declares. class CustomFontFace { const CustomFontFace({ required this.key, required this.fileName, required this.weight, required this.italic, required this.size, }); /// The bytes' key in the custom font store. final String key; /// The name the file had when it was added (shown, never used as a path). final String fileName; final int weight; final bool italic; final int size; String get styleName => fontStyleName(weight, italic: italic); Map toJson() => { 'key': key, 'file': fileName, 'weight': weight, 'italic': italic, 'size': size, }; static CustomFontFace? fromJson(Object? json) { if (json is! Map) return null; final key = json['key']; final file = json['file']; final weight = json['weight']; final italic = json['italic']; final size = json['size']; if (key is! String || key.isEmpty || file is! String) return null; if (weight is! int || italic is! bool || size is! int) return null; return CustomFontFace( key: key, fileName: file, weight: weight, italic: italic, size: size, ); } } /// A family the user added from their own font files. Its registered /// [family] is derived from the stable [id], so a rename changes only the /// [label] and never re-registers the files. class CustomFont implements FontChoice { const CustomFont({required this.id, required this.name, required this.faces}); static const String idPrefix = 'custom-'; @override final String id; /// The user's name for the family. final String name; /// At least one face, in the order they were added. final List faces; @override String get label => name; @override String get family => 'Pipelines $id'; @override List get sources => [ for (final face in faces) customFontSource(face.key), ]; /// The upright face nearest to 400, else the face nearest to 400. @override String? get previewSource { CustomFontFace? best; int score(CustomFontFace face) => (face.weight - 400).abs() + (face.italic ? 2000 : 0); for (final face in faces) { if (best == null || score(face) < score(best)) best = face; } return best == null ? null : customFontSource(best.key); } /// The styles it has, lightest first: "Regular, Bold". String get styles { final sorted = [...faces] ..sort( (a, b) => a.weight != b.weight ? a.weight.compareTo(b.weight) : (a.italic ? 1 : 0).compareTo(b.italic ? 1 : 0), ); return sorted.map((face) => face.styleName).join(', '); } int get size => faces.fold(0, (total, face) => total + face.size); CustomFont renamed(String name) => CustomFont(id: id, name: name, faces: faces); Map toJson() => { 'id': id, 'name': name, 'faces': [for (final face in faces) face.toJson()], }; /// A stored entry, or null when it is not one this version can use. static CustomFont? fromJson(Object? json) { if (json is! Map) return null; final id = json['id']; final name = json['name']; final faces = json['faces']; if (id is! String || !id.startsWith(idPrefix) || name is! String) { return null; } if (faces is! List) return null; final parsed = []; for (final face in faces) { final value = CustomFontFace.fromJson(face); if (value == null) return null; parsed.add(value); } if (parsed.isEmpty || name.trim().isEmpty) return null; return CustomFont(id: id, name: name, faces: parsed); } /// The same option whatever its current name. @override bool operator ==(Object other) => other is CustomFont && other.id == id; @override int get hashCode => id.hashCode; }