c24e7f40b4
Add font… picks one or more files, checks them with a bounded sfnt reader (name table family, OS/2 weight), registers them with FontLoader before storing, and chooses the new family. Added fonts list below the bundled ones with Rename and Remove; removing the chosen one falls back to System default. Bytes live in the app support directory natively and in IndexedDB on the web (localStorage is too small); a shared_preferences index names them. Limits: 10 MB per file, 40 MB total. The capture kit gains a files= step.
142 lines
3.9 KiB
Dart
142 lines
3.9 KiB
Dart
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<String, Object> toJson() => <String, Object>{
|
|
'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<CustomFontFace> faces;
|
|
|
|
@override
|
|
String get label => name;
|
|
|
|
@override
|
|
String get family => 'Pipelines $id';
|
|
|
|
@override
|
|
List<String> get sources => <String>[
|
|
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<String, Object> toJson() => <String, Object>{
|
|
'id': id,
|
|
'name': name,
|
|
'faces': <Object>[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 = <CustomFontFace>[];
|
|
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;
|
|
}
|