Update jquery.webfonts to 372aad1

Introduce overridable font family option
Font stacks given in overridableFontFamilies will be overridden
as as exception to the general policy of not altering elements with
explicit font family styles

Change-Id: I579c3ccb6e0c1ad39d434651a4689e8c595e5a84
This commit is contained in:
Santhosh Thottingal
2014-04-14 15:54:41 +05:30
parent 53a4d8b087
commit db750d1a1e

View File

@@ -174,8 +174,7 @@
* different language than what the element itself has. * different language than what the element itself has.
*/ */
parse: function() { parse: function() {
var append, var webfonts = this,
webfonts = this,
// Fonts can be added indirectly via classes, but also with // Fonts can be added indirectly via classes, but also with
// style attributes. For lang attributes we will use our font // style attributes. For lang attributes we will use our font
// if they don't have explicit font already. // if they don't have explicit font already.
@@ -186,12 +185,18 @@
// Object keys are the font family, values are list of plain elements. // Object keys are the font family, values are list of plain elements.
elementQueue = {}; elementQueue = {};
// Append function that keeps the array as a set (no dupes) // Add to the font queue(no dupes)
append = function( array, value ) { function addToFontQueue( value ) {
if ( $.inArray( value, array ) < 0 ) { if ( $.inArray( value, fontQueue ) < 0 ) {
array.push( value ); fontQueue.push( value );
}
}
// Add to the font queue
function addToElementQueue( element, fontFamily ) {
elementQueue[fontFamily] = elementQueue[fontFamily] || [];
elementQueue[fontFamily].push( element );
} }
};
$elements.each( function( i, element ) { $elements.each( function( i, element ) {
var fontFamilyStyle, fontFamily, var fontFamilyStyle, fontFamily,
@@ -207,18 +212,28 @@
// Note: It is unclear whether this can ever be falsy. Maybe also // Note: It is unclear whether this can ever be falsy. Maybe also
// browser specific. // browser specific.
if ( fontFamilyStyle ) { if ( fontFamilyStyle ) {
// if it is overridable, override. always.
if ( webfonts.isOverridable( fontFamilyStyle ) ) {
fontFamily = webfonts.getFont( element.lang || webfonts.language );
// We do not have fonts for all languages
if ( fontFamily ) {
addToFontQueue( fontFamily );
addToElementQueue( element, fontFamily );
}
return;
} else {
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, '' ) );
addToFontQueue( fontFamily );
append( fontQueue, fontFamily ); }
} }
// Load and apply fonts for other language tagged elements (batched) // Load and apply fonts for other language tagged elements (batched)
if ( element.lang && element.lang !== webfonts.language ) { if ( element.lang && element.lang !== webfonts.language ) {
// language differs. We may want to apply a different font. // language differs. We may want to apply a different font.
if ( webfonts.hasExplicitFontStyle ( $element ) ) { if ( webfonts.hasExplicitFontStyle ( $element ) &&
!webfonts.isOverridable( fontFamilyStyle ) ) {
// respect the explicit font family style. Do not override. // respect the explicit font family style. Do not override.
// This style may be from css, inheritance, or even from // This style may be from css, inheritance, or even from
// browser settings. // browser settings.
@@ -246,9 +261,8 @@
// We do not have fonts for all languages // We do not have fonts for all languages
if ( fontFamily ) { if ( fontFamily ) {
append( fontQueue, fontFamily ); addToFontQueue( fontFamily );
elementQueue[fontFamily] = elementQueue[fontFamily] || []; addToElementQueue( element, fontFamily );
elementQueue[fontFamily].push( element );
} }
} }
} ); } );
@@ -278,6 +292,26 @@
[ 'monospace', 'serif', 'cursive', 'fantasy', 'sans-serif' ] ) < 0 ); [ 'monospace', 'serif', 'cursive', 'fantasy', 'sans-serif' ] ) < 0 );
}, },
/**
* Check whether the give font family is overridable or not. jquey.webfonts
* by default does not override any font-family styles other than generic
* font family styles(See hasExplicitFontStyle method)
* @param {string} fontFamily
* @return {boolean} Whether the given fontFamily is overridable or not.
*/
isOverridable: function( fontFamily ) {
var overridableFontFamilies = [ 'monospace', 'serif', 'cursive', 'fantasy', 'sans-serif' ];
$.merge( overridableFontFamilies, this.options.overridableFontFamilies );
// Browsers like FF put space after comma in font stack. Chrome does not.
// Normalise it by removing the spaces and quotes
overridableFontFamilies = $.map( overridableFontFamilies, function( item ) {
return item.replace( /[\s'"]/g, '' );
} );
fontFamily = fontFamily.replace( /[\s'"]/g, '' );
console.log(fontFamily+':'+overridableFontFamilies);
return $.inArray( fontFamily, overridableFontFamilies ) >= 0;
},
/** /**
* List all fonts for the given language * List all fonts for the given language
* *
@@ -448,7 +482,8 @@
$.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 fallback fontStack: [ 'Helvetica', 'Arial', 'sans-serif' ], // Default font fallback
exclude: '' // jQuery selectors to exclude exclude: '', // jQuery selectors to exclude
overridableFontFamilies: []
}; };
$.fn.webfonts.Constructor = WebFonts; $.fn.webfonts.Constructor = WebFonts;