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.
44 lines
1.6 KiB
Dart
44 lines
1.6 KiB
Dart
import 'package:flutter/services.dart';
|
|
|
|
import 'font_choice.dart';
|
|
|
|
/// Registers [sources] under [family]. Production reads them from the bundle
|
|
/// or the custom font store; tests substitute a recorder.
|
|
typedef AppFontAssetLoader =
|
|
Future<void> Function(String family, List<String> sources);
|
|
|
|
/// Registers font files already in memory under [family]; used to check an
|
|
/// added font before it is stored.
|
|
typedef AppFontBytesLoader =
|
|
Future<void> Function(String family, List<Uint8List> faces);
|
|
|
|
/// Reads the bytes of an added face by its store key.
|
|
typedef CustomFontReader = Future<Uint8List> Function(String key);
|
|
|
|
/// Loads font files on demand with a [FontLoader]: bundle assets (on the web
|
|
/// each is fetched only now, when its family is first previewed or chosen)
|
|
/// and [customFontSource] keys, read through [readCustom].
|
|
AppFontAssetLoader appFontSourceLoader(CustomFontReader readCustom) =>
|
|
(family, sources) async {
|
|
final loader = FontLoader(family);
|
|
for (final source in sources) {
|
|
loader.addFont(
|
|
source.startsWith(customFontSourcePrefix)
|
|
? readCustom(
|
|
source.substring(customFontSourcePrefix.length),
|
|
).then(ByteData.sublistView)
|
|
: rootBundle.load(source),
|
|
);
|
|
}
|
|
await loader.load();
|
|
};
|
|
|
|
/// Registers in-memory font files with a [FontLoader].
|
|
Future<void> loadFontBytes(String family, List<Uint8List> faces) async {
|
|
final loader = FontLoader(family);
|
|
for (final face in faces) {
|
|
loader.addFont(Future<ByteData>.value(ByteData.sublistView(face)));
|
|
}
|
|
await loader.load();
|
|
}
|