lib/FontUtils.js
function loadFont (fontFace, callback) {
  var defaultNode;
  var testNode;
  var checkFont;

  // See if we've previously loaded it.
  if (_loadedFonts[fontFace.id]) {
    return callback(null);
  }

  // See if we've previously failed to load it.
  if (_failedFonts[fontFace.id]) {
    return callback(_failedFonts[fontFace.id]);
  }

  // System font: assume already loaded.
  if (!fontFace.url) {
    return callback(null);
  }

  // Font load is already in progress:
  if (_pendingFonts[fontFace.id]) {
    _pendingFonts[fontFace.id].callbacks.push(callback);
    return;
  }

  // Create the test <span>'s for measuring.
  defaultNode = createTestNode('Helvetica', fontFace.attributes);
  testNode = createTestNode(fontFace.family, fontFace.attributes);
  document.body.appendChild(testNode);
  document.body.appendChild(defaultNode);

  _pendingFonts[fontFace.id] = {
    startTime: Date.now(),
    defaultNode: defaultNode,
    testNode: testNode,
    callbacks: [callback]
  };

  // Font watcher
  checkFont = function () {
    var currWidth = testNode.getBoundingClientRect().width;
    var defaultWidth = defaultNode.getBoundingClientRect().width;
    var loaded = currWidth !== defaultWidth;

    if (loaded) {
      handleFontLoad(fontFace, null);
    } else {
      // Timeout?
      if (Date.now() - _pendingFonts[fontFace.id].startTime >= kFontLoadTimeout) {
        handleFontLoad(fontFace, true);
      } else {
        requestAnimationFrame(checkFont);
      }
    }
  };

  // Start watching
  checkFont();
}
function loadFont (fontFace, callback) {
  var defaultNode;
  var testNode;
  var checkFont;

  // See if we've previously loaded it.
  if (_loadedFonts[fontFace.id]) {
    return callback(null);
  }

  // See if we've previously failed to load it.
  if (_failedFonts[fontFace.id]) {
    return callback(_failedFonts[fontFace.id]);
  }

  // System font: assume already loaded.
  if (!fontFace.url) {
    return callback(null);
  }

  // Font load is already in progress:
  if (_pendingFonts[fontFace.id]) {
    _pendingFonts[fontFace.id].callbacks.push(callback);
    return;
  }

  // Create the test <span>'s for measuring.
  defaultNode = createTestNode('Helvetica', fontFace.attributes);
  testNode = createTestNode(fontFace.family, fontFace.attributes);
  document.body.appendChild(testNode);
  document.body.appendChild(defaultNode);

  _pendingFonts[fontFace.id] = {
    startTime: Date.now(),
    defaultNode: defaultNode,
    testNode: testNode,
    callbacks: [callback]
  };

  // Font watcher
  checkFont = function () {
    var currWidth = testNode.getBoundingClientRect().width;
    var defaultWidth = defaultNode.getBoundingClientRect().width;
    var loaded = currWidth !== defaultWidth;

    if (loaded) {
      handleFontLoad(fontFace, null);
    } else {
      // Timeout?
      if (Date.now() - _pendingFonts[fontFace.id].startTime >= kFontLoadTimeout) {
        handleFontLoad(fontFace, true);
      } else {
        requestAnimationFrame(checkFont);
      }
    }
  };

  // Start watching
  checkFont();
}
function loadFont ( fontFace , callback ) { var defaultNode ; var testNode ; var checkFont ; // See if we've previously loaded it. if ( _loadedFonts [ fontFace . id ] ) { return callback ( null ) ; } // See if we've previously failed to load it. if ( _failedFonts [ fontFace . id ] ) { return callback ( _failedFonts [ fontFace . id ] ) ; } // System font: assume already loaded. if ( ! fontFace . url ) { return callback ( null ) ; } // Font load is already in progress: if ( _pendingFonts [ fontFace . id ] ) { _pendingFonts [ fontFace . id ] . callbacks . push ( callback ) ; return ; } // Create the test <span>'s for measuring. defaultNode = createTestNode ( 'Helvetica' , fontFace . attributes ) ; testNode = createTestNode ( fontFace . family , fontFace . attributes ) ; document . body . appendChild ( testNode ) ; document . body . appendChild ( defaultNode ) ; _pendingFonts [ fontFace . id ] = { startTime : Date . now ( ) , defaultNode : defaultNode , testNode : testNode , callbacks : [ callback ] } ; // Font watcher checkFont = function ( ) { var currWidth = testNode . getBoundingClientRect ( ) . width ; var defaultWidth = defaultNode . getBoundingClientRect ( ) . width ; var loaded = currWidth !== defaultWidth ; if ( loaded ) { handleFontLoad ( fontFace , null ) ; } else { // Timeout? if ( Date . now ( ) - _pendingFonts [ fontFace . id ] . startTime >= kFontLoadTimeout ) { handleFontLoad ( fontFace , true ) ; } else { requestAnimationFrame ( checkFont ) ; } } } ; // Start watching checkFont ( ) ; }
Load a remote font and execute a callback.
@param {FontFace} fontFace The font to Load
@param {Function} callback Function executed upon font Load
Load a remote font and execute a callback .