loadFontFromZip method

Future<void> loadFontFromZip([
  1. int? pageIndex
])

Loads a font from a ZIP file for the specified page index.

This method asynchronously loads a font from a ZIP file based on 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> loadFontFromZip([int? pageIndex]) async {
  try {
    // مسار مجلد الخطوط بعد فك الضغط
    final fontsDir = Directory(
        isPhones ? '${_dir.path}/qcf4_woff' : '${_dir.path}/qcf4_ttf');

    final loadedSet = state.loadedFontPages; // في الذاكرة

    // حمّل جميع الخطوط المتاحة 001..604
    for (int i = 0; i < 604; i++) {
      try {
        // إذا كان محمّلًا في هذه الجلسة، تخطّه
        if (loadedSet.contains(i)) continue;

        final fontPath = getFontFullPath(fontsDir, i);
        final fontFile = File(fontPath);
        if (!await fontFile.exists()) {
          // قد تكون بعض الملفات غير موجودة في مجموعة معينة
          continue;
        }

        final loader = FontLoader(getFontPath(i));
        loader.addFont(_getFontLoaderBytes(fontFile));
        await loader.load();
        loadedSet.add(i);
      } catch (e) {
        // تجاهل فشل صفحة واحدة واستمر
        log('Failed to register font for page ${i + 1}: $e',
            name: 'FontsLoad');
        continue;
      }
    }

    // حفظ الصفحات التي تم تحميل خطوطها
    GetStorage()
        .write(_StorageConstants().loadedFontPages, loadedSet.toList());
  } catch (e) {
    throw Exception("Failed to load fonts from disk: $e");
  }
}