Updated version from upstream

Introduces refresh method
Introduces fontSelector hook

Change-Id: I255c213a87cd588f6dd17b1ec48ac3c294413a81
This commit is contained in:
Santhosh Thottingal
2012-08-21 10:06:58 +05:30
parent a07dc90ddf
commit 92ed1f656d

View File

@@ -25,6 +25,7 @@
this.repository = $.extend(WebFonts.repository, this.options.repository); this.repository = $.extend(WebFonts.repository, this.options.repository);
this.fonts = []; this.fonts = [];
this.originalFontFamily = this.$element.css('font-family'); this.originalFontFamily = this.$element.css('font-family');
this.language = this.$element.attr('lang') || $('html').attr('lang');
this.init(); this.init();
}; };
@@ -35,9 +36,9 @@
// Utility methods to work on the repository. // Utility methods to work on the repository.
defaultFont : function(language) { defaultFont : function(language) {
var defaultFont; var defaultFont = null;
if (this.languages[language]) { if (this.languages[language]) {
return this.languages[language][0]; defaultFont = this.languages[language][0];
} }
return defaultFont; return defaultFont;
}, },
@@ -50,19 +51,31 @@
WebFonts.prototype = { WebFonts.prototype = {
constructor : WebFonts, constructor : WebFonts,
getFont: function(language){
if (this.options.fontSelector) {
return this.options.fontSelector(this.repository, language || this.language);
} else {
return this.repository.defaultFont(language);
}
},
/** /**
* Initialize. * Initialize.
*/ */
init : function() { init : function() {
var language, fontFamily; var fontFamily;
language = this.$element.attr('lang') || $('html').attr('lang'); if (this.language) {
if (language) { fontFamily = this.getFont(this.language);
fontFamily = this.repository.defaultFont(language);
this.apply(fontFamily); this.apply(fontFamily);
} }
this.parse(); this.parse();
}, },
refresh: function() {
this.reset();
this.init();
},
/** /**
* Apply a font for the element. * Apply a font for the element.
* @param fontFamily String: font family name * @param fontFamily String: font family name
@@ -86,13 +99,17 @@
*/ */
load : function(fontFamily) { load : function(fontFamily) {
if ($.inArray(fontFamily, this.fonts) >= 0) { if ($.inArray(fontFamily, this.fonts) >= 0) {
return; return true;
} }
var styleString = this.getCSS(fontFamily); var styleString = this.getCSS(fontFamily);
if (styleString) { if (styleString) {
injectCSS(styleString); injectCSS(styleString);
} else {
// Font not found
return false;
} }
this.fonts.push(fontFamily); this.fonts.push(fontFamily);
return true;
}, },
/** /**
@@ -110,12 +127,16 @@
fontFamily = fontFamilyStyle.split(',')[0]; fontFamily = fontFamilyStyle.split(',')[0];
// Remove the ' and " characters if any. // Remove the ' and " characters if any.
fontFamily = $.trim(fontFamily.replace(/["']/g, '')); fontFamily = $.trim(fontFamily.replace(/["']/g, ''));
that.load(fontFamily); if (that.load(fontFamily)){
// Font family overrides lang attribute // Font family overrides lang attribute
return; // But was it the fontfamily allocated for the current language?
if (fontFamily === that.getFont(element.lang)) {
return true;
}
}
} }
if (element.lang && element.lang !== that.$element.attr('lang')) { if (element.lang && element.lang !== that.$element.attr('lang')) {
fontFamily = this.repository.defaultFont(element.lang); fontFamily = that.getFont(element.lang);
that.apply(fontFamily, $(element)); that.apply(fontFamily, $(element));
} }
}); });
@@ -128,7 +149,7 @@
* @return Array font names array * @return Array font names array
*/ */
list : function(language) { list : function(language) {
var fontName, var fontName = null,
fontNames = []; fontNames = [];
if (language) { if (language) {
@@ -148,7 +169,7 @@
* @return Array language codes * @return Array language codes
*/ */
languages : function() { languages : function() {
var language, var language = null,
languages = []; languages = [];
for (language in this.repository.languages ) { for (language in this.repository.languages ) {
if (this.repository.languages.hasOwnProperty(language)) { if (this.repository.languages.hasOwnProperty(language)) {