Skip to content

Commit

Permalink
Add more extensive type annotations so we get improved type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram Stein committed Mar 6, 2013
1 parent a60a461 commit 29309ab
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/ascender/ascender_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ goog.provide('webfont.AscenderScript');
* });
*
* @constructor
* @implements {webfont.FontModule}
*/
webfont.AscenderScript = function(domHelper, configuration) {
this.domHelper_ = domHelper;
Expand Down
50 changes: 45 additions & 5 deletions src/core/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ goog.scope(function () {
FontWatcher = webfont.FontWatcher,
Size = webfont.Size;

/**
* @param {string} name
* @param {webfont.FontModuleFactory} factory
*/
WebFont.prototype.addModule = function(name, factory) {
this.fontModuleLoader_.addModuleFactory(name, factory);
};

/**
* @param {Object} configuration
*/
WebFont.prototype.load = function(configuration) {
var context = configuration['context'] || this.mainWindow_;
this.domHelper_ = new DomHelper(this.mainWindow_, context);
Expand All @@ -45,10 +52,18 @@ goog.scope(function () {
}
};

/**
* @param {webfont.FontModule} module
* @param {webfont.EventDispatcher} eventDispatcher
* @param {webfont.FontWatcher} fontWatcher
* @param {boolean} support
*/
WebFont.prototype.isModuleSupportingUserAgent_ = function(module, eventDispatcher,
fontWatcher, support) {
var fontWatchRunnerCtor = module.getFontWatchRunnerCtor ?
module.getFontWatchRunnerCtor() : webfont.FontWatchRunner;
module.getFontWatchRunnerCtor() : webfont.FontWatchRunner,
that = this;

if (!support) {
var allModulesLoaded = --this.moduleLoading_ == 0;

Expand All @@ -63,21 +78,46 @@ goog.scope(function () {
fontWatcher.watch([], {}, {}, fontWatchRunnerCtor, allModulesLoaded);
return;
}
module.load(goog.bind(this.onModuleReady_, this, eventDispatcher,
fontWatcher, fontWatchRunnerCtor));

module.load(function (fontFamilies, fontVariations) {
that.onModuleReady_(eventDispatcher, fontWatcher, fontWatchRunnerCtor, fontFamilies, fontVariations);
});
};

/**
* @param {webfont.EventDispatcher} eventDispatcher
* @param {webfont.FontWatcher} fontWatcher
* @param {function(new:webfont.FontWatchRunner,
* function(string, string),
* function(string, string),
* webfont.DomHelper,
* string,
* string,
* webfont.BrowserInfo,
* number=,
* Object.<string, boolean>=,
* string=)} fontWatchRunnerCtor
* @param {webfont.FontFamilies} fontFamilies
* @param {webfont.FontVariations=} opt_fontVariations
* @param {webfont.FontTestStrings=} opt_fontTestStrings
*/
WebFont.prototype.onModuleReady_ = function(eventDispatcher, fontWatcher,
fontWatchRunnerCtor, fontFamilies, opt_fontDescriptions, opt_fontTestStrings) {
fontWatchRunnerCtor, fontFamilies, opt_fontVariations, opt_fontTestStrings) {
var allModulesLoaded = --this.moduleLoading_ == 0;

if (allModulesLoaded) {
eventDispatcher.dispatchLoading();
}

setTimeout(goog.bind(fontWatcher.watch, fontWatcher, fontFamilies, opt_fontDescriptions || {}, opt_fontTestStrings || {}, fontWatchRunnerCtor, allModulesLoaded), 0);
setTimeout(function () {
fontWatcher.watch(fontFamilies, opt_fontVariations || {}, opt_fontTestStrings || {}, fontWatchRunnerCtor, allModulesLoaded);
}, 0);
};

/**
* @param {webfont.EventDispatcher} eventDispatcher
* @param {Object} configuration
*/
WebFont.prototype.load_ = function(eventDispatcher, configuration) {
var modules = this.fontModuleLoader_.getModules(configuration, this.domHelper_),
timeout = configuration['timeout'],
Expand Down
37 changes: 37 additions & 0 deletions src/core/fontmoduleloader.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
goog.provide('webfont.FontModuleLoader');
goog.provide('webfont.FontModule');
goog.provide('webfont.FontModuleFactory');

/**
* @interface
*/
webfont.FontModule = function () {};

goog.scope(function () {
var FontModule = webfont.FontModule;

/**
* @param {webfont.UserAgent} userAgent
* @param {function(boolean)} support
*/
FontModule.prototype.supportUserAgent = function (userAgent, support) {};

/**
* @param {function(webfont.FontFamilies, webfont.FontVariations=, webfont.FontTestStrings=)} onReady
*/
FontModule.prototype.load = function (onReady) {};
});

/** @typedef {function(Object, webfont.DomHelper): webfont.FontModule} */
webfont.FontModuleFactory;

/**
* @constructor
*/
webfont.FontModuleLoader = function() {
/**
* @type {Object.<string, webfont.FontModuleFactory>}
*/
this.modules_ = {};
};

goog.scope(function () {
var FontModuleLoader = webfont.FontModuleLoader;

/**
* @param {string} name
* @param {webfont.FontModuleFactory} factory
*/
FontModuleLoader.prototype.addModuleFactory = function(name, factory) {
this.modules_[name] = factory;
};

/**
* @param {Object} configuration
* @param {webfont.DomHelper} domHelper
* @return {Array.<webfont.FontModule>}
*/
FontModuleLoader.prototype.getModules = function(configuration, domHelper) {
var modules = [];

Expand Down
13 changes: 10 additions & 3 deletions src/core/fontwatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ goog.scope(function () {
* of each family to watch. Described with FVD.
* @param {Object.<string, string>} fontTestStrings The font test strings for
* each family.
* @param {function(new:webfont.FontWatchRunner, function(string, string),
* function(string, string), webfont.DomHelper,
* string, string, webfont.BrowserInfo, number=, Object.<string, boolean>=, string=)} fontWatchRunnerCtor The font watch runner constructor.
* @param {function(new:webfont.FontWatchRunner,
* function(string, string),
* function(string, string),
* webfont.DomHelper,
* string,
* string,
* webfont.BrowserInfo,
* number=,
* Object.<string, boolean>=,
* string=)} fontWatchRunnerCtor The font watch runner constructor.
* @param {boolean} last True if this is the last set of families to watch.
*/
FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
Expand Down
15 changes: 15 additions & 0 deletions src/core/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ goog.require('webfont.UserAgentParser');
goog.require('webfont.FontModuleLoader');
goog.require('webfont.WebFont');

/**
* @typedef {Array.<string>}
*/
webfont.FontFamilies;

/**
* @typedef {Object.<string, Array.<string>>}
*/
webfont.FontVariations;

/**
* @typedef {Object.<string, Array.<string>>}
*/
webfont.FontTestStrings;

// Name of the global object.
var globalName = 'WebFont';

Expand Down
1 change: 1 addition & 0 deletions src/custom/customcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ goog.provide('webfont.CustomCss');
* });
*
* @constructor
* @implements {webfont.FontModule}
*/
webfont.CustomCss = function(domHelper, configuration) {
this.domHelper_ = domHelper;
Expand Down
1 change: 1 addition & 0 deletions src/fontdeck/fontdeck_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ goog.require('webfont.FontVariationDescription');

/**
* @constructor
* @implements {webfont.FontModule}
*/
webfont.FontdeckScript = function(domHelper, configuration) {
this.domHelper_ = domHelper;
Expand Down
1 change: 1 addition & 0 deletions src/google/googlefontapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ goog.require('webfont.LastResortWebKitFontWatchRunner');

/**
* @constructor
* @implements {webfont.FontModule}
*/
webfont.GoogleFontApi = function(userAgent, domHelper, configuration) {
this.userAgent_ = userAgent;
Expand Down
3 changes: 3 additions & 0 deletions src/google/lastresortwebkitfontwatchrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ goog.scope(function () {
return webKitLastResortFontSizes;
};

/**
* @override
*/
LastResortWebKitFontWatchRunner.prototype.check_ = function() {
var sizeA = this.fontRulerA_.getSize();
var sizeB = this.fontRulerB_.getSize();
Expand Down
1 change: 1 addition & 0 deletions src/monotype/monotype_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ projectId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'//this is your Fonts.com Web fo

/**
* @constructor
* @implements {webfont.FontModule}
*/
webfont.MonotypeScript = function (userAgent, domHelper, configuration) {
this.userAgent_ = userAgent;
Expand Down
1 change: 1 addition & 0 deletions src/typekit/typekit_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ goog.provide('webfont.TypekitScript');

/**
* @constructor
* @implements {webfont.FontModule}
*/
webfont.TypekitScript = function(domHelper, configuration) {
this.domHelper_ = domHelper;
Expand Down

0 comments on commit 29309ab

Please sign in to comment.