Skip to content

Commit

Permalink
Share code between strategies.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiele committed Nov 29, 2011
1 parent 6408605 commit 3c63438
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 58 deletions.
38 changes: 38 additions & 0 deletions src/core/basecheckstrategy.js
@@ -0,0 +1,38 @@
/**
* @constructor
*/
webfont.BaseCheckStrategy = function(sizingElementCreator, activeCallback,
inactiveCallback, fontSizer, fontFamily, fontDescription, opt_fontTestString) {
this.sizingElementCreator_ = sizingElementCreator;
this.activeCallback_ = activeCallback;
this.inactiveCallback_ = inactiveCallback;
this.fontSizer_ = fontSizer;
this.fontFamily_ = fontFamily;
this.fontDescription_ = fontDescription;
this.fontTestString_ = opt_fontTestString
|| webfont.FontWatchRunner.DEFAULT_TEST_STRING;
};

webfont.BaseCheckStrategy.prototype.setUp = function() {
this.originalSizeA_ = this.getStackSize_(webfont.FontWatchRunner.SANS_STACK);
this.originalSizeB_ = this.getStackSize_(webfont.FontWatchRunner.SERIF_STACK);
this.requestedFontA_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SANS_STACK,
this.fontDescription_, this.fontTestString_);
this.requestedFontB_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SERIF_STACK,
this.fontDescription_, this.fontTestString_);
};

webfont.BaseCheckStrategy.prototype.tearDown = function() {
this.sizingElementCreator_.deleteSizingElement(this.requestedFontA_);
this.sizingElementCreator_.deleteSizingElement(this.requestedFontB_);
};

webfont.BaseCheckStrategy.prototype.getStackSize_ = function(stack) {
var stackElement = this.sizingElementCreator_.createSizingElement('',
stack, this.fontDescription_, this.fontTestString_);
var size = this.fontSizer_.getWidth(stackElement);
this.sizingElementCreator_.deleteSizingElement(stackElement);
return size;
};
34 changes: 5 additions & 29 deletions src/core/defaultcheckstrategy.js
Expand Up @@ -3,32 +3,16 @@
*/
webfont.DefaultCheckStrategy = function(sizingElementCreator, activeCallback,
inactiveCallback, fontSizer, fontFamily, fontDescription, opt_fontTestString) {
this.sizingElementCreator_ = sizingElementCreator;
this.activeCallback_ = activeCallback;
this.inactiveCallback_ = inactiveCallback;
this.fontSizer_ = fontSizer;
this.fontFamily_ = fontFamily;
this.fontDescription_ = fontDescription;
this.fontTestString_ = opt_fontTestString
|| webfont.FontWatchRunner.DEFAULT_TEST_STRING;
webfont.DefaultCheckStrategy.superCtor_.call(this, sizingElementCreator,
activeCallback, inactiveCallback, fontSizer, fontFamily, fontDescription,
opt_fontTestString);
};
webfont.extendsClass(webfont.BaseCheckStrategy, webfont.DefaultCheckStrategy);

webfont.DefaultCheckStrategy.prototype.setUp = function() {
this.originalSizeA_ = this.getStackSize_(webfont.FontWatchRunner.SANS_STACK);
this.originalSizeB_ = this.getStackSize_(webfont.FontWatchRunner.SERIF_STACK);
webfont.DefaultCheckStrategy.super_.setUp.call(this);
this.lastObservedSizeA_ = this.originalSizeA_;
this.lastObservedSizeB_ = this.originalSizeB_;
this.requestedFontA_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SANS_STACK,
this.fontDescription_, this.fontTestString_);
this.requestedFontB_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SERIF_STACK,
this.fontDescription_, this.fontTestString_);
};

webfont.DefaultCheckStrategy.prototype.tearDown = function() {
this.sizingElementCreator_.deleteSizingElement(this.requestedFontA_);
this.sizingElementCreator_.deleteSizingElement(this.requestedFontB_);
};

webfont.DefaultCheckStrategy.prototype.isLoaded = function() {
Expand All @@ -55,11 +39,3 @@ webfont.DefaultCheckStrategy.prototype.getTimeoutCallback = function() {
this.inactiveCallback_(this.fontFamily_, this.fontDescription_);
});
};

webfont.DefaultCheckStrategy.prototype.getStackSize_ = function(stack) {
var stackElement = this.sizingElementCreator_.createSizingElement('',
stack, this.fontDescription_, this.fontTestString_);
var size = this.fontSizer_.getWidth(stackElement);
this.sizingElementCreator_.deleteSizingElement(stackElement);
return size;
};
35 changes: 6 additions & 29 deletions src/core/lastresortwebkitcheckstrategy.js
Expand Up @@ -3,31 +3,16 @@
*/
webfont.LastResortWebKitCheckStrategy = function(sizingElementCreator, activeCallback,
inactiveCallback, fontSizer, fontFamily, fontDescription, opt_fontTestString) {
this.sizingElementCreator_ = sizingElementCreator;
this.activeCallback_ = activeCallback;
this.inactiveCallback_ = inactiveCallback;
this.fontSizer_ = fontSizer;
this.fontFamily_ = fontFamily;
this.fontDescription_ = fontDescription;
this.fontTestString_ = opt_fontTestString
|| webfont.FontWatchRunner.DEFAULT_TEST_STRING;
webfont.LastResortWebKitCheckStrategy.superCtor_.call(this,
sizingElementCreator, activeCallback, inactiveCallback, fontSizer,
fontFamily, fontDescription, opt_fontTestString);
};
webfont.extendsClass(webfont.BaseCheckStrategy,
webfont.LastResortWebKitCheckStrategy);

webfont.LastResortWebKitCheckStrategy.prototype.setUp = function() {
this.originalSizeA_ = this.getStackSize_(webfont.FontWatchRunner.SANS_STACK);
this.originalSizeB_ = this.getStackSize_(webfont.FontWatchRunner.SERIF_STACK);
webfont.LastResortWebKitCheckStrategy.super_.setUp.call(this);
this.webKitLastResortFontSizes_ = this.setUpWebKitLastResortFontSizes_();
this.requestedFontA_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SANS_STACK,
this.fontDescription_, this.fontTestString_);
this.requestedFontB_ = this.sizingElementCreator_.createSizingElement(
this.fontFamily_, webfont.FontWatchRunner.SERIF_STACK,
this.fontDescription_, this.fontTestString_);
};

webfont.LastResortWebKitCheckStrategy.prototype.tearDown = function() {
this.sizingElementCreator_.deleteSizingElement(this.requestedFontA_);
this.sizingElementCreator_.deleteSizingElement(this.requestedFontB_);
};

webfont.LastResortWebKitCheckStrategy.prototype.isLoaded = function() {
Expand All @@ -48,14 +33,6 @@ webfont.LastResortWebKitCheckStrategy.prototype.getTimeoutCallback = function()
return this.getActiveCallback();
};

webfont.LastResortWebKitCheckStrategy.prototype.getStackSize_ = function(stack) {
var stackElement = this.sizingElementCreator_.createSizingElement('',
stack, this.fontDescription_, this.fontTestString_);
var size = this.fontSizer_.getWidth(stackElement);
this.sizingElementCreator_.deleteSizingElement(stackElement);
return size;
};

/**
* While loading a web font webkit applies a last resort fallback font to the
* element on which the web font is applied.
Expand Down
14 changes: 14 additions & 0 deletions src/core/namespace.js
Expand Up @@ -14,3 +14,17 @@ webfont.bind = function(context, func, opt_args) {
return func.apply(context, args);
};
};

webfont.extendsClass = function(baseClass, subClass) {

// Avoid polluting the baseClass prototype object with methods from the
// subClass
/** @constructor */
function baseExtendClass() {};
baseExtendClass.prototype = baseClass.prototype;
subClass.prototype = new baseExtendClass();

subClass.prototype.constructor = subClass;
subClass.superCtor_ = baseClass;
subClass.super_ = baseClass.prototype;
};
1 change: 1 addition & 0 deletions src/modules.yml
Expand Up @@ -6,6 +6,7 @@ core:
- core/eventdispatcher.js
- core/fontmoduleloader.js
- core/sizingelementcreator.js
- core/basecheckstrategy.js
- core/defaultcheckstrategy.js
- core/lastresortwebkitcheckstrategy.js
- core/fontwatcher.js
Expand Down

0 comments on commit 3c63438

Please sign in to comment.