loadFont method

Future<void> loadFont(
  1. int pageIndex, {
  2. bool isFontsLocal = false,
})

Loads the font for the specified page index.

This method asynchronously loads the font for the given pageIndex. The font is then available for use within the application.

pageIndex - The index of the page for which the font should be loaded.

Returns a Future that completes when the font has been successfully loaded.

Implementation

Future<void> loadFont(int pageIndex, {bool isFontsLocal = false}) async {
  if (isFontsLocal) {
    return;
  } else {
    try {
      // إذا كان الخط لهذه الصفحة محملًا، لا تعِد التحميل
      // If font for this page is already loaded, skip
      if (state.loadedFontPages.contains(pageIndex)) {
        return;
      }
      // تعديل المسار ليشمل المجلد الإضافي
      final fontFile = File(
          '${_dir.path}/quran_fonts/quran_fonts/p${(pageIndex + 2001)}.ttf');
      if (!await fontFile.exists()) {
        throw Exception("Font file not found for page: ${pageIndex + 2001}");
      }
      final fontLoader = FontLoader('p${(pageIndex + 2001)}');
      fontLoader.addFont(_getFontLoaderBytes(fontFile));
      await fontLoader.load();
      state.loadedFontPages.add(pageIndex);
    } catch (e) {
      throw Exception("Failed to load font for page ${pageIndex + 1}: $e");
    }
  }
}