Settings: add your own fonts (.ttf/.otf) per device
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.
This commit is contained in:
@@ -1,28 +1,21 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'app_font.dart';
|
||||
import 'app_font_loaders.dart';
|
||||
import 'app_font_store.dart';
|
||||
import 'custom_fonts/custom_font.dart';
|
||||
import 'custom_fonts/custom_font_draft.dart';
|
||||
import 'custom_fonts/custom_font_library.dart';
|
||||
import 'font_choice.dart';
|
||||
|
||||
/// Registers [assets] under [family]. Production reads them from the bundle;
|
||||
/// tests substitute a recorder.
|
||||
typedef AppFontAssetLoader =
|
||||
Future<void> Function(String family, List<String> assets);
|
||||
|
||||
/// Loads bundled font files on demand with a [FontLoader]. On the web each
|
||||
/// asset is fetched only now, when its family is first previewed or chosen.
|
||||
Future<void> loadBundledFontAssets(String family, List<String> assets) async {
|
||||
final loader = FontLoader(family);
|
||||
for (final asset in assets) {
|
||||
loader.addFont(rootBundle.load(asset));
|
||||
}
|
||||
await loader.load();
|
||||
}
|
||||
export 'app_font_loaders.dart';
|
||||
|
||||
/// The device's interface font: the stored choice, the family the theme
|
||||
/// actually uses, and the on-demand loading between the two.
|
||||
/// actually uses, the on-demand loading between the two, and the fonts the
|
||||
/// user added.
|
||||
///
|
||||
/// The theme keeps the previous family (at first the platform's) until the
|
||||
/// chosen files are registered, then switches once; text never lays out in a
|
||||
@@ -30,51 +23,69 @@ Future<void> loadBundledFontAssets(String family, List<String> assets) async {
|
||||
class AppFontController extends ChangeNotifier {
|
||||
AppFontController({
|
||||
this._store = const SharedPreferencesAppFontStore(),
|
||||
this._loader = loadBundledFontAssets,
|
||||
});
|
||||
AppFontAssetLoader? loader,
|
||||
this._bytesLoader = loadFontBytes,
|
||||
CustomFontLibrary? library,
|
||||
}) : _library = library ?? CustomFontLibrary() {
|
||||
_loader = loader ?? appFontSourceLoader(_library.read);
|
||||
}
|
||||
|
||||
/// The app's own controller: one per running app, shared by the theme
|
||||
/// hosts and the Settings picker.
|
||||
static final AppFontController instance = AppFontController();
|
||||
|
||||
final AppFontStore _store;
|
||||
final AppFontAssetLoader _loader;
|
||||
late final AppFontAssetLoader _loader;
|
||||
final AppFontBytesLoader _bytesLoader;
|
||||
final CustomFontLibrary _library;
|
||||
final Map<String, Future<void>> _assetLoads = <String, Future<void>>{};
|
||||
final Set<String> _loadedAssets = <String>{};
|
||||
Future<void>? _libraryLoad;
|
||||
|
||||
AppFont _selected = AppFont.system;
|
||||
AppFont _applied = AppFont.system;
|
||||
FontChoice _selected = AppFont.system;
|
||||
FontChoice _applied = AppFont.system;
|
||||
bool _saveFailed = false;
|
||||
AppFont? _loadFailed;
|
||||
FontChoice? _loadFailed;
|
||||
|
||||
/// The option the picker shows as chosen.
|
||||
AppFont get selected => _selected;
|
||||
FontChoice get selected => _selected;
|
||||
|
||||
/// The option the theme uses now.
|
||||
AppFont get applied => _applied;
|
||||
FontChoice get applied => _applied;
|
||||
|
||||
/// The theme's family; null keeps the platform typography.
|
||||
String? get family => _applied.family;
|
||||
|
||||
/// The chosen family's files are still downloading.
|
||||
/// The fonts the user added on this device, in the order added.
|
||||
List<CustomFont> get customFonts => _library.fonts;
|
||||
|
||||
/// The chosen family's files are still loading.
|
||||
bool get loading => _selected != _applied && _loadFailed != _selected;
|
||||
|
||||
/// The last choice could not be stored on this device.
|
||||
bool get saveFailed => _saveFailed;
|
||||
|
||||
/// The chosen family's files could not be loaded; the theme kept [applied].
|
||||
AppFont? get loadFailed => _loadFailed;
|
||||
FontChoice? get loadFailed => _loadFailed;
|
||||
|
||||
/// Whether [font]'s regular weight is registered, so its name can be drawn
|
||||
/// in its own face.
|
||||
bool canPreview(AppFont font) =>
|
||||
font.previewAsset == null || _loadedAssets.contains(font.previewAsset);
|
||||
bool canPreview(FontChoice font) =>
|
||||
font.previewSource == null || _loadedAssets.contains(font.previewSource);
|
||||
|
||||
/// Reads the added fonts' index once.
|
||||
Future<void> loadCustomFonts() => _libraryLoad ??= () async {
|
||||
await _library.load();
|
||||
notifyListeners();
|
||||
}();
|
||||
|
||||
/// Reads the stored choice and starts loading it. With [wait], returns once
|
||||
/// it is applied or after [wait], whichever is first; a slower load still
|
||||
/// applies when it finishes.
|
||||
Future<void> restore({Duration? wait}) async {
|
||||
final font = await _store.load();
|
||||
final id = await _store.load();
|
||||
if (id == null || id == AppFont.system.id) return;
|
||||
final font = await _resolve(id);
|
||||
if (font == AppFont.system || font == _selected) return;
|
||||
_selected = font;
|
||||
notifyListeners();
|
||||
@@ -86,13 +97,25 @@ class AppFontController extends ChangeNotifier {
|
||||
]);
|
||||
}
|
||||
|
||||
Future<FontChoice> _resolve(String id) async {
|
||||
for (final font in AppFont.values) {
|
||||
if (font.id == id) return font;
|
||||
}
|
||||
if (!id.startsWith(CustomFont.idPrefix)) return AppFont.system;
|
||||
await loadCustomFonts();
|
||||
for (final font in customFonts) {
|
||||
if (font.id == id) return font;
|
||||
}
|
||||
return AppFont.system;
|
||||
}
|
||||
|
||||
/// Chooses [font], stores it, and applies it once its files are loaded.
|
||||
Future<void> select(AppFont font) async {
|
||||
Future<void> select(FontChoice font) async {
|
||||
if (font == _selected && _loadFailed != font) return;
|
||||
_selected = font;
|
||||
_loadFailed = null;
|
||||
notifyListeners();
|
||||
final saved = await _store.save(font);
|
||||
final saved = await _store.save(font.id);
|
||||
if (saved == _saveFailed) {
|
||||
_saveFailed = !saved;
|
||||
notifyListeners();
|
||||
@@ -102,25 +125,78 @@ class AppFontController extends ChangeNotifier {
|
||||
|
||||
/// Loads [font]'s regular weight for its picker row. Failures are quiet: the
|
||||
/// row keeps the current face and choosing it reports the problem.
|
||||
Future<void> preview(AppFont font) async {
|
||||
final asset = font.previewAsset;
|
||||
Future<void> preview(FontChoice font) async {
|
||||
final source = font.previewSource;
|
||||
final family = font.family;
|
||||
if (asset == null || family == null || _loadedAssets.contains(asset)) {
|
||||
if (source == null || family == null || _loadedAssets.contains(source)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await _ensure(family, <String>[asset]);
|
||||
await _ensure(family, <String>[source]);
|
||||
} catch (_) {
|
||||
return;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> _apply(AppFont font) async {
|
||||
/// Checks picked files before they are named; see
|
||||
/// [CustomFontDraft.inspect]. Throws [CustomFontRejected].
|
||||
Future<CustomFontDraft> inspect(List<PickedFontFile> files) async {
|
||||
await loadCustomFonts();
|
||||
return CustomFontDraft.inspect(files, storedBytes: _library.storedBytes);
|
||||
}
|
||||
|
||||
/// Registers [draft]'s files with the engine, and only if that works
|
||||
/// stores them as [name] and chooses the new font. Throws
|
||||
/// [CustomFontRejected]; a rejected font is never stored.
|
||||
Future<CustomFont> addCustom(CustomFontDraft draft, String name) async {
|
||||
final problem = CustomFontLimits.nameProblem(name);
|
||||
if (problem != null) throw CustomFontRejected(problem);
|
||||
await loadCustomFonts();
|
||||
final font = _library.prepare(draft, name.trim());
|
||||
try {
|
||||
await _bytesLoader(font.family, <Uint8List>[
|
||||
for (final face in draft.faces) face.bytes,
|
||||
]);
|
||||
} catch (_) {
|
||||
throw const CustomFontRejected('This font could not be loaded.');
|
||||
}
|
||||
await _library.store(font, draft);
|
||||
for (final source in font.sources) {
|
||||
_assetLoads[source] = Future<void>.value();
|
||||
_loadedAssets.add(source);
|
||||
}
|
||||
notifyListeners();
|
||||
await select(font);
|
||||
return font;
|
||||
}
|
||||
|
||||
/// Renames an added font; the choice and the theme keep it.
|
||||
Future<void> renameCustom(CustomFont font, String name) async {
|
||||
final problem = CustomFontLimits.nameProblem(name);
|
||||
if (problem != null) throw CustomFontRejected(problem);
|
||||
final renamed = await _library.rename(font, name.trim());
|
||||
if (_selected == font) _selected = renamed;
|
||||
if (_applied == font) _applied = renamed;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Deletes an added font. When it is chosen (or still shown), the device
|
||||
/// returns to the system default.
|
||||
Future<void> removeCustom(CustomFont font) async {
|
||||
await _library.remove(font);
|
||||
if (_selected == font || _applied == font) {
|
||||
_applied = AppFont.system;
|
||||
await select(AppFont.system);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> _apply(FontChoice font) async {
|
||||
final family = font.family;
|
||||
if (family != null) {
|
||||
try {
|
||||
await _ensure(family, font.assets);
|
||||
await _ensure(family, font.sources);
|
||||
} catch (_) {
|
||||
if (_selected == font) {
|
||||
_loadFailed = font;
|
||||
@@ -135,22 +211,22 @@ class AppFontController extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Registers the assets not yet loaded or loading, in one [FontLoader], and
|
||||
/// waits for all of [assets]. A failed asset can be retried.
|
||||
Future<void> _ensure(String family, List<String> assets) {
|
||||
final missing = assets.where((a) => !_assetLoads.containsKey(a)).toList();
|
||||
/// Registers the sources not yet loaded or loading, in one [FontLoader],
|
||||
/// and waits for all of [sources]. A failed source can be retried.
|
||||
Future<void> _ensure(String family, List<String> sources) {
|
||||
final missing = sources.where((s) => !_assetLoads.containsKey(s)).toList();
|
||||
if (missing.isNotEmpty) {
|
||||
final load = _loader(family, missing);
|
||||
for (final asset in missing) {
|
||||
_assetLoads[asset] = load.then(
|
||||
(_) => _loadedAssets.add(asset),
|
||||
for (final source in missing) {
|
||||
_assetLoads[source] = load.then(
|
||||
(_) => _loadedAssets.add(source),
|
||||
onError: (Object error, StackTrace stack) {
|
||||
_assetLoads.remove(asset);
|
||||
_assetLoads.remove(source);
|
||||
Error.throwWithStackTrace(error, stack);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
return Future.wait(assets.map((a) => _assetLoads[a]!));
|
||||
return Future.wait(sources.map((s) => _assetLoads[s]!));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user