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 {
  try {
    // إذا كان الخط لهذه الصفحة محملًا، لا تعِد التحميل
    // If font for this page is already loaded, skip
    if (state.loadedFontPages.contains(pageIndex)) {
      return;
    }
    final fontLoader = FontLoader(getFontPath(pageIndex));
    if (kIsWeb) {
      log('Loading font for page ${pageIndex + 1} from web...',
          name: 'FontsLoad');
      fontLoader.addFont(_getWebFontBytes(pageIndex));
    }
    // else {

    //   // التحميل من التخزين المحلي (سواء كانت isFontsLocal true أم false)
    //   final fontFile = File(getFontFullPath(_dir, pageIndex));
    //   if (!await fontFile.exists()) {
    //     throw Exception(
    //         "Font file not exists for page: ${(pageIndex + 1).toString().padLeft(3, '0')}");
    //   }
    //   fontLoader.addFont(_getFontLoaderBytes(fontFile));
    // }
    await fontLoader.load();
    state.loadedFontPages.add(pageIndex);
    update();
    // حفظ القائمة لتسريع الجلسات اللاحقة
    GetStorage().write(
        _StorageConstants().loadedFontPages, state.loadedFontPages.toList());
    // على الويب بشكل خاص: أعد بناء الواجهة لتفعيل الخط فورًا دون قلب الصفحة
    if (kIsWeb && !isClosed) {
      try {
        update();
      } catch (_) {}
    }
  } catch (e) {
    throw Exception(
        "Failed to load font for page ${(pageIndex + 1).toString().padLeft(3, '0')}: $e");
  }
}