loadMissingFonts method

Future<PdfFontLoadResult> loadMissingFonts(
  1. Iterable<PdfFontQuery> queries, {
  2. PdfFontResolveContext context = const PdfFontResolveContext(),
  3. bool reloadFonts = true,
  4. PdfFontLoadProgressCallback? onProgress,
})

Resolves and registers queries.

Concurrent requests with the same query parameters and context share font resolution and loading. Every waiting call reports the newly available font in its result so each caller can refresh its own document.

If reloadFonts is true and fonts become available, refreshes the backend font mapper. Concurrent calls share a refresh that covers their registered fonts. Callers still need to reopen or reload already opened documents.

The backend registers one font per target face. Resolvers that return different fonts must use different target faces; conflicting registrations are reported in PdfFontLoadResult.failed.

onProgress receives progress for this call, including shared loads. An error in its callback fails only this call, not other waiting callers.

Implementation

Future<PdfFontLoadResult> loadMissingFonts(
  Iterable<PdfFontQuery> queries, {
  PdfFontResolveContext context = const PdfFontResolveContext(),
  bool reloadFonts = true,
  PdfFontLoadProgressCallback? onProgress,
}) async {
  final initialGeneration = _generation;
  final initialRevision = initialGeneration?.registrationRevision ?? 0;
  final backend = PdfrxEntryFunctions.instance;
  await prepare();
  await waitForFontEnvironmentChanges();
  if (!identical(backend, PdfrxEntryFunctions.instance)) {
    throw StateError('The PDF backend changed while preparing fonts. Retry the font load.');
  }
  var generation = _generation;
  if (generation == null || !generation.isCurrent) {
    final revision = fontEnvironmentRevision;
    await withFontEnvironmentAccess(backend.init);
    final current = _generation;
    generation = current != null && current.isCurrent
        ? current
        : _generation = (_FontLoadGeneration(backend, revision)..checkCurrent());
  }
  final revisionAtStart = identical(initialGeneration, generation) ? initialRevision : 0;
  final loaded = <PdfLoadedFont>[];
  final loadedTargets = <String>{};
  final unresolved = <PdfFontQuery>[];
  final failed = <PdfFontLoadFailure>[];
  var registrationRevision = 0;
  final seen = <_FontQueryKey>{};
  for (final query in queries) {
    final key = _queryKey(query, context);
    if (!seen.add(key)) continue;
    try {
      generation.checkCurrent();
      final existing = generation.completedQueries[key];
      if (existing != null &&
          existing.revision <= revisionAtStart &&
          existing.revision <= generation.reloadedRevision) {
        continue;
      }
      final observer = _FontProgressObserver(query, onProgress);
      var pending = generation.pendingQueries[key];
      if (pending == null) {
        pending = _PendingFontLoad();
        generation.pendingQueries[key] = pending;
        pending.future = _resolveAndRegister(generation, query, context, key, pending);
      }
      pending.observers.add(observer);
      final _RegisteredFont? font;
      try {
        font = await pending.future;
      } finally {
        pending.observers.remove(observer);
      }
      observer.rethrowError();
      generation.checkCurrent();
      if (font == null) {
        unresolved.add(query);
      } else {
        if (loadedTargets.add(font.targetFace)) loaded.add(font.forQuery(query));
        if (registrationRevision < font.revision) registrationRevision = font.revision;
      }
    } catch (error, stackTrace) {
      failed.add(PdfFontLoadFailure(query: query, error: error, stackTrace: stackTrace));
    }
  }
  if (reloadFonts && loaded.isNotEmpty) await generation.reloadThrough(registrationRevision);
  if (loaded.isNotEmpty) generation.checkCurrent();
  return PdfFontLoadResult(
    loaded: loaded,
    unresolved: unresolved,
    failed: failed,
    fontsReloaded: reloadFonts && loaded.isNotEmpty,
  );
}