check method

Wait for the font face represented by this FontFaceObserver to become available in the browser. It will asynchronously return a FontLoadResult with the result of whether or not the font is loaded or the timeout was reached while waiting for it to become available.

Implementation

Future<FontLoadResult> check() async {
  Timer t;
  if (_result.isCompleted) {
    return _result.future;
  }

  final record = _loadedFonts[key];
  if (record == null) {
    return FontLoadResult(isLoaded: false, didTimeout: false);
  }

  // Since browsers may not load a font until it is actually used (lazily loaded)
  // We add this span to trigger the browser to load the font when used
  final String _key = '_ffo_dummy_${key}';
  var dummy = document.getElementById(_key);
  if (dummy == null) {
    dummy = SpanElement()
      ..className = '$fontFaceObserverTempClassname _ffo_dummy'
      ..id = _key
      ..setAttribute('style', 'font-family: "${family}"; visibility: hidden;')
      ..text = testString;
    document.body!.append(dummy);
  }

  // Assume the browser supports FontFace API
  // set up an interval to check if the font is loaded
  t = Timer.periodic(
      const Duration(milliseconds: _nativeFontLoadingCheckInterval),
      _periodicallyCheckDocumentFonts);

  // Start a timeout timer that will cancel everything and complete
  // our _loaded future with false if it isn't already completed
  Timer(Duration(milliseconds: timeout), () => _onTimeout(t));

  return _result.future.then((FontLoadResult flr) {
    if (t.isActive) {
      t.cancel();
    }
    dummy?.remove();
    return flr;
  });
}