loadFonts static method

Future<void> loadFonts()

Loads the Uthmani font programmatically from assets. Call this once at app startup before using the font.

Example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await QuranFontLoader.loadFonts();
  runApp(MyApp());
}

Implementation

static Future<void> loadFonts() async {
  if (_isLoaded) return;

  try {
    // Load Uthmani font
    final uthmaniLoader = FontLoader(uthmaniFamily);

    ByteData? fontData;

    // Try package path first (when used as a dependency)
    try {
      fontData = await rootBundle.load(_packageFontPath);
      // ignore: avoid_print
      print('QuranFontLoader: Loading font from package path');
    } catch (e) {
      // Try direct path (when running example app or standalone)
      try {
        fontData = await rootBundle.load(_directFontPath);
        // ignore: avoid_print
        print('QuranFontLoader: Loading font from direct path');
      } catch (e2) {
        throw Exception(
            'Could not load font from either path. Package path error: $e, Direct path error: $e2');
      }
    }

    uthmaniLoader.addFont(Future.value(fontData));
    await uthmaniLoader.load();
    _isLoaded = true;

    // ignore: avoid_print
    print('QuranFontLoader: Uthmani font loaded successfully');
  } catch (e) {
    // ignore: avoid_print
    print('QuranFontLoader: Failed to load Uthmani font: $e');
    rethrow;
  }
}