Update version from upstream

* Adds exclude option

Change-Id: Iaa220abf0bc11c26b13a481e1fbf3b307a0e9d0b
This commit is contained in:
Santhosh Thottingal
2012-09-05 13:46:26 +05:30
parent dd3d214cb5
commit 62a8d4f8fd

View File

@@ -15,58 +15,58 @@
* @licence MIT License * @licence MIT License
*/ */
(function($, window, document, undefined) { ( function( $, window, document, undefined ) {
"use strict"; "use strict";
var WebFonts = function(element, options) { var WebFonts = function( element, options ) {
// Load defaults // Load defaults
this.options = $.extend({}, $.fn.webfonts.defaults, options); this.options = $.extend( {}, $.fn.webfonts.defaults, options );
this.$element = $(element); this.$element = $( element );
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.language = this.$element.attr( 'lang' ) || $( 'html' ).attr( 'lang' );
this.init(); this.init();
}; };
WebFonts.repository = { WebFonts.repository = {
base : 'fonts', // Relative or absolute path to the font repository. base: 'fonts', // Relative or absolute path to the font repository.
languages : {}, // languages to font mappings languages: {}, // languages to font mappings
fonts : {}, // Font name to font configuration mapping fonts: {}, // Font name to font configuration mapping
// Utility methods to work on the repository. // Utility methods to work on the repository.
defaultFont : function(language) { defaultFont: function( language ) {
var defaultFont = null; var defaultFont = null;
if (this.languages[language]) { if ( this.languages[language] ) {
defaultFont = this.languages[language][0]; defaultFont = this.languages[language][0];
} }
return defaultFont; return defaultFont;
}, },
get : function(fontFamily) { get: function( fontFamily ) {
return this.fonts[fontFamily]; return this.fonts[fontFamily];
} }
}; };
WebFonts.prototype = { WebFonts.prototype = {
constructor : WebFonts, constructor: WebFonts,
getFont: function(language){ getFont: function( language ) {
if (this.options.fontSelector) { if ( this.options.fontSelector ) {
return this.options.fontSelector(this.repository, language || this.language); return this.options.fontSelector( this.repository, language || this.language );
} else { } else {
return this.repository.defaultFont(language); return this.repository.defaultFont( language );
} }
}, },
/** /**
* Initialize. * Initialize.
*/ */
init : function() { init: function() {
var fontFamily; var fontFamily;
if (this.language) { if ( this.language ) {
fontFamily = this.getFont(this.language); fontFamily = this.getFont( this.language );
this.apply(fontFamily); this.apply( fontFamily );
} }
this.parse(); this.parse();
}, },
@@ -78,86 +78,91 @@
/** /**
* Apply a font for the element. * Apply a font for the element.
*
* @param fontFamily String: font family name * @param fontFamily String: font family name
*/ */
apply : function(fontFamily, $element) { apply: function( fontFamily, $element ) {
$element = $element || this.$element; $element = $element || this.$element;
var fontStack = this.options.fontStack.slice(0); var fontStack = this.options.fontStack.slice( 0 );
// Loading an empty string is pointless. // Loading an empty string is pointless.
// Putting an empty string into a font-family list doesn't work with jQuery.css(). // Putting an empty string into a font-family list doesn't work with
// jQuery.css().
if ( fontFamily ) { if ( fontFamily ) {
this.load(fontFamily); this.load( fontFamily );
fontStack.unshift(fontFamily); fontStack.unshift( fontFamily );
} }
$element.css('font-family', fontStack.join() ); $element.css( 'font-family', fontStack.join() );
$element.find('textarea, input').css('font-family', fontStack.join()); $element.find( 'textarea, input' ).not( this.options.exclude ).css( 'font-family',
fontStack.join() );
}, },
/** /**
* Load a given fontFamily if not loaded already * Load a given fontFamily if not loaded already
*
* @param fontFamily String font family name * @param fontFamily String font family name
*/ */
load : function(fontFamily) { load: function( fontFamily ) {
if ($.inArray(fontFamily, this.fonts) >= 0) { if ( $.inArray( fontFamily, this.fonts ) >= 0 ) {
return true; return true;
} }
var styleString = this.getCSS(fontFamily); var styleString = this.getCSS( fontFamily );
if (styleString) { if ( styleString ) {
injectCSS(styleString); injectCSS( styleString );
} else { } else {
// Font not found // Font not found
return false; return false;
} }
this.fonts.push(fontFamily); this.fonts.push( fontFamily );
return true; return true;
}, },
/** /**
* Parse the element for custom font-family styles and * Parse the element for custom font-family styles and for nodes with
* for nodes with different language than element * different language than element
*/ */
parse : function() { parse: function() {
var that = this; var that = this;
that.$element.find('*[lang], [style], [class]').each(function(i, element) { that.$element.find( '*[lang], [style], [class]' ).not( that.options.exclude ).each(
var fontFamilyStyle, fontFamily, function( i, element ) {
$element = $(element); var fontFamilyStyle, fontFamily, $element = $( element );
fontFamilyStyle = $element.css('fontFamily'); fontFamilyStyle = $element.css( 'fontFamily' );
if (fontFamilyStyle) { if ( fontFamilyStyle ) {
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, '' ) );
if (that.load(fontFamily)){ if ( that.load( fontFamily ) ) {
// Font family overrides lang attribute // Font family overrides lang attribute
// But was it the fontfamily allocated for the current language? // But was it the fontfamily allocated for the current
if (fontFamily === that.getFont(element.lang)) { // language?
if ( fontFamily === that.getFont( element.lang ) ) {
return true; return true;
} }
} }
} }
if (element.lang && element.lang !== that.$element.attr('lang')) { if ( element.lang && element.lang !== that.$element.attr( 'lang' ) ) {
fontFamily = that.getFont(element.lang); fontFamily = that.getFont( element.lang );
that.apply(fontFamily, $(element)); that.apply( fontFamily, $( element ) );
} }
}); } );
}, },
/** /**
* List all fonts for the given language * List all fonts for the given language
* @param language mixed: [optional] language code. If undefined all fonts will *
* be listed * @param language mixed: [optional] language code. If undefined all
* fonts will be listed
* @return Array font names array * @return Array font names array
*/ */
list : function(language) { list: function( language ) {
var fontName = null, var fontName = null, fontNames = [];
fontNames = [];
if (language) { if ( language ) {
fontNames = this.repository.languages[language]; fontNames = this.repository.languages[language];
} else { } else {
for (fontName in this.repository.fonts) { for (fontName in this.repository.fonts) {
if (this.repository.fonts.hasOwnProperty(fontName)) { if ( this.repository.fonts.hasOwnProperty( fontName ) ) {
fontNames.push(fontName); fontNames.push( fontName );
} }
} }
} }
@@ -166,14 +171,14 @@
/** /**
* List all languages supported by the repository * List all languages supported by the repository
*
* @return Array language codes * @return Array language codes
*/ */
languages : function() { languages: function() {
var language = null, 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)) { languages.push( language );
languages.push(language);
} }
} }
return languages; return languages;
@@ -181,44 +186,47 @@
/** /**
* Set the font repository * Set the font repository
*
* @param {Object} repository The font repository. * @param {Object} repository The font repository.
*/ */
setRepository : function(repository) { setRepository: function( repository ) {
this.repository = $.extend(WebFonts.repository, repository); this.repository = $.extend( WebFonts.repository, repository );
}, },
/** /**
* Reset the font-family style. * Reset the font-family style.
*/ */
reset : function() { reset: function() {
this.apply(this.originalFontFamily); this.apply( this.originalFontFamily );
}, },
/** /**
* unbind the plugin * unbind the plugin
*/ */
unbind : function() { unbind: function() {
this.$element.data('webfonts', null); this.$element.data( 'webfonts', null );
}, },
/** /**
* Construct the CSS required for the font-family, inject it to the head of the * Construct the CSS required for the font-family, inject it to the head
* body so that it gets loaded. * of the body so that it gets loaded.
*
* @param fontFamily The font-family name * @param fontFamily The font-family name
* @param variant The font variant, eg: bold, italic etc. Default is normal. * @param variant The font variant, eg: bold, italic etc. Default is
* normal.
*/ */
getCSS : function(fontFamily, variant) { getCSS: function( fontFamily, variant ) {
var fontconfig, base, version, versionSuffix, styleString, userAgent, fontStyle, fontFormats; var fontconfig, base, version, versionSuffix, styleString, userAgent, fontStyle, fontFormats;
variant = variant || 'normal'; variant = variant || 'normal';
fontconfig = this.repository.get(fontFamily); fontconfig = this.repository.get( fontFamily );
if (variant !== 'normal') { if ( variant !== 'normal' ) {
if (fontconfig.variants !== undefined && fontconfig.variants[variant]) { if ( fontconfig.variants !== undefined && fontconfig.variants[variant] ) {
fontFamily = fontconfig.variants[variant]; fontFamily = fontconfig.variants[variant];
fontconfig = this.repository.get(fontFamily); fontconfig = this.repository.get( fontFamily );
} }
} }
if (!fontconfig) { if ( !fontconfig ) {
return false; return false;
} }
@@ -230,27 +238,30 @@
fontStyle = fontconfig.fontstyle || 'normal'; fontStyle = fontconfig.fontstyle || 'normal';
fontFormats = []; fontFormats = [];
if (fontconfig.eot) { if ( fontconfig.eot ) {
styleString += "\tsrc: url('" + base + fontconfig.eot + versionSuffix + "');\n"; styleString += "\tsrc: url('" + base + fontconfig.eot + versionSuffix + "');\n";
} }
styleString += "\tsrc: "; styleString += "\tsrc: ";
// If the font is present locally, use it. // If the font is present locally, use it.
if (userAgent.match(/Android 2\.3/) === null) { if ( userAgent.match( /Android 2\.3/ ) === null ) {
// Android 2.3.x does not respect local() syntax. // Android 2.3.x does not respect local() syntax.
// http://code.google.com/p/android/issues/detail?id=10609 // http://code.google.com/p/android/issues/detail?id=10609
styleString += "local('" + fontFamily + "'),"; styleString += "local('" + fontFamily + "'),";
} }
if (fontconfig.woff) { if ( fontconfig.woff ) {
fontFormats.push("\t\turl('" + base + fontconfig.woff + versionSuffix + "') format('woff')"); fontFormats.push( "\t\turl('" + base + fontconfig.woff + versionSuffix
+ "') format('woff')" );
} }
if (fontconfig.svg) { if ( fontconfig.svg ) {
fontFormats.push("\t\turl('" + base + fontconfig.svg + versionSuffix + "#" + fontFamily + "') format('svg')"); fontFormats.push( "\t\turl('" + base + fontconfig.svg + versionSuffix + "#"
+ fontFamily + "') format('svg')" );
} }
if (fontconfig.ttf) { if ( fontconfig.ttf ) {
fontFormats.push("\t\turl('" + base + fontconfig.ttf + versionSuffix + "') format('truetype')"); fontFormats.push( "\t\turl('" + base + fontconfig.ttf + versionSuffix
+ "') format('truetype')" );
} }
styleString += fontFormats.join() + ";\n"; styleString += fontFormats.join() + ";\n";
if (fontconfig.fontweight) { if ( fontconfig.fontweight ) {
styleString += "\tfont-weight:" + fontconfig.fontweight + ";"; styleString += "\tfont-weight:" + fontconfig.fontweight + ";";
} }
styleString += "\tfont-style:" + fontStyle + ";"; styleString += "\tfont-style:" + fontStyle + ";";
@@ -259,30 +270,31 @@
} }
}; };
$.fn.webfonts = function(option) { $.fn.webfonts = function( option ) {
return this.each(function() { return this.each( function() {
var $this, data, options; var $this, data, options;
$this = $(this); $this = $( this );
data = $this.data('webfonts'); data = $this.data( 'webfonts' );
options = typeof option === 'object' && option; options = typeof option === 'object' && option;
if (!data) { if ( !data ) {
$this.data('webfonts', (data = new WebFonts(this, options))); $this.data( 'webfonts', ( data = new WebFonts( this, options ) ) );
} }
if (typeof option === 'string') { if ( typeof option === 'string' ) {
data[option](); data[option]();
} }
}); } );
}; };
$.fn.webfonts.defaults = { $.fn.webfonts.defaults = {
repository : WebFonts.repository, // Default font repository repository: WebFonts.repository, // Default font repository
fontStack : ['Helvetica', 'Arial', 'sans-serif'] // Default font fall back series fontStack: [ 'Helvetica', 'Arial', 'sans-serif' ], // Default font fallback
exclude: '' // jQuery selectors to exclude
}; };
$.fn.webfonts.Constructor = WebFonts; $.fn.webfonts.Constructor = WebFonts;
// Private methods for the WebFonts prototype // Private methods for the WebFonts prototype
//================================= // =================================
/** /**
* Create a new style tag and add it to the DOM. * Create a new style tag and add it to the DOM.
@@ -290,20 +302,20 @@
* @param text String: CSS text * @param text String: CSS text
* @return HTMLStyleElement * @return HTMLStyleElement
*/ */
function injectCSS(css) { function injectCSS( css ) {
var s = document.createElement('style'); var s = document.createElement( 'style' );
s.type = 'text/css'; s.type = 'text/css';
s.rel = 'stylesheet'; s.rel = 'stylesheet';
// Insert into document before setting cssText // Insert into document before setting cssText
document.getElementsByTagName('head')[0].appendChild(s); document.getElementsByTagName( 'head' )[0].appendChild( s );
if (s.styleSheet) { if ( s.styleSheet ) {
s.styleSheet.cssText = css; s.styleSheet.cssText = css;
// IE // IE
} else { } else {
// Safari sometimes borks on null // Safari sometimes borks on null
s.appendChild(document.createTextNode(String(css))); s.appendChild( document.createTextNode( String( css ) ) );
} }
return s; return s;
} }
} )(jQuery, window, document); } )( jQuery, window, document );