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.
35 lines
1.4 KiB
Dart
35 lines
1.4 KiB
Dart
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'custom_font_draft.dart';
|
|
|
|
/// Opens the host's file picker for font files; null when the user cancels.
|
|
/// Throws [CustomFontRejected] for a file too large to read.
|
|
typedef FontFilePicker = Future<List<PickedFontFile>?> Function();
|
|
|
|
/// The host picker through `file_picker`. The web and Apple pickers filter
|
|
/// to .ttf/.otf; Android's document picker filters by MIME type, which many
|
|
/// providers do not report for fonts, so there every file is offered and
|
|
/// [CustomFontDraft.inspect] rejects the rest by name.
|
|
Future<List<PickedFontFile>?> pickFontFiles() async {
|
|
final android = !kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
|
final picked = await FilePicker.pickFiles(
|
|
type: android ? FileType.any : FileType.custom,
|
|
allowedExtensions: android ? null : CustomFontLimits.extensions,
|
|
);
|
|
if (picked.isEmpty) return null;
|
|
final files = <PickedFontFile>[];
|
|
for (final file in picked) {
|
|
final length = file.lengthSync() ?? await file.length();
|
|
if (length != null && length > CustomFontLimits.maxFileBytes) {
|
|
// Refuse before reading a huge file into memory.
|
|
throw CustomFontRejected(
|
|
'“${file.name}” is larger than '
|
|
'${CustomFontLimits.maxFileBytes ~/ (1024 * 1024)} MB.',
|
|
);
|
|
}
|
|
files.add(PickedFontFile(name: file.name, bytes: await file.readAsBytes()));
|
|
}
|
|
return files;
|
|
}
|