Files
plezy/lib/mpv/font_loader.dart
T

51 lines
1.7 KiB
Dart
Raw Normal View History

2025-12-04 16:39:07 +01:00
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
2026-04-25 03:16:11 +02:00
import '../utils/app_logger.dart';
2026-05-04 22:39:17 +02:00
/// Extracts font files from Flutter assets to the cache directory for
/// comprehensive Unicode coverage (including CJK characters) for libass subtitles.
2025-12-19 13:19:57 +01:00
class SubtitleFontLoader {
2025-12-04 16:39:07 +01:00
static const String _fontAssetPath = 'assets/go-noto-current-regular.ttf';
static const String _fontName = 'Go Noto Current-Regular';
2026-04-24 23:20:31 +02:00
/// In-memory cache of the resolved font directory. The filesystem work
/// (temp dir lookup, existence checks, asset extraction) is idempotent per
/// process — caching the result skips ~20ms on every subsequent Player
/// instantiation.
static Future<String?>? _cachedFontDir;
static Future<String?> loadSubtitleFont() {
return _cachedFontDir ??= _loadSubtitleFontOnce();
}
static Future<String?> _loadSubtitleFontOnce() async {
2025-12-04 16:39:07 +01:00
try {
final cacheDir = await getTemporaryDirectory();
final fontDir = Directory(path.join(cacheDir.path, 'subtitle_fonts'));
if (!await fontDir.exists()) {
await fontDir.create(recursive: true);
}
2025-12-15 02:29:03 +01:00
final fontFile = File(path.join(fontDir.path, 'go-noto-current-regular.ttf'));
2025-12-04 16:39:07 +01:00
if (!await fontFile.exists()) {
final fontData = await rootBundle.load(_fontAssetPath);
await fontFile.writeAsBytes(fontData.buffer.asUint8List());
}
return fontDir.path;
2026-04-25 03:16:11 +02:00
} catch (e, st) {
appLogger.w('Failed to load subtitle font', error: e, stackTrace: st);
2025-12-04 16:39:07 +01:00
return null;
}
}
static String get fontName => _fontName;
static String get fontAssetPath => _fontAssetPath;
}