/**
* jQuery Webfonts.
*
* Copyright (C) 2012 Santhosh Thottingal
*
* UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
* have to do anything special to choose one license or the other and you don't
* have to notify anyone which license you are using. You are free to use
* UniversalLanguageSelector in commercial projects as long as the copyright
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
*
* @file
* @ingroup Extensions
* @licence GNU General Public Licence 2.0 or later
* @licence MIT License
*/
( function( $, window, document, undefined ) {
'use strict';
var WebFonts = function( element, options ) {
// Load defaults
this.options = $.extend( {}, $.fn.webfonts.defaults, options );
this.$element = $( element );
this.repository = $.extend( WebFonts.repository, this.options.repository );
// List of loaded fonts
this.fonts = [];
this.originalFontFamily = this.$element.css( 'font-family' );
this.language = this.$element.attr( 'lang' ) || $( 'html' ).attr( 'lang' );
this.init();
};
WebFonts.repository = {
base: 'fonts', // Relative or absolute path to the font repository.
languages: {}, // languages to font mappings
fonts: {}, // Font name to font configuration mapping
// Utility methods to work on the repository.
defaultFont: function( language ) {
var defaultFont = null;
if ( this.languages[language] ) {
defaultFont = this.languages[language][0];
}
return defaultFont;
},
get: function( fontFamily ) {
return this.fonts[fontFamily];
}
};
WebFonts.prototype = {
constructor: WebFonts,
/**
* Get the default font family for given language.
* @param {String|undefined} language Language code.
* @param {array} classes
* @return {String} Font family name
*/
getFont: function( language, classes ) {
language = ( language || this.language || '' ).toLowerCase();
if ( this.options.fontSelector && language ) {
return this.options.fontSelector( this.repository, language, classes );
} else {
return this.repository.defaultFont( language );
}
},
/**
* Initialize.
*/
init: function() {
if ( this.language ) {
this.apply( this.getFont( this.language ) );
}
this.parse();
},
/**
* TODO: document
*/
refresh: function() {
this.reset();
this.init();
},
/**
* Apply a font for given elements.
*
* @param {String} fontFamily Font family name
* @param {jQuery} $element One or more jQuery elements
*/
apply: function( fontFamily, $element ) {
var fontStack = this.options.fontStack.slice( 0 );
$element = $element || this.$element;
// Loading an empty string is pointless.
// Putting an empty string into a font-family list doesn't work with
// jQuery.css().
if ( fontFamily ) {
this.load( fontFamily );
// Avoid duplicates
if ( $.inArray( fontFamily, fontStack ) < 0 ) {
fontStack.unshift( fontFamily );
}
}
if ( !fontFamily ) {
// We are resetting the font to original font.
fontStack = [];
// This will cause removing inline fontFamily style.
}
// Set the font of this element if it's not excluded.
// Add class webfonts-changed when webfonts are applied.
$element.not( this.options.exclude )
.css( 'font-family', fontStack.join() )
.addClass( 'webfonts-changed' );
// Set the font of this element's children if they are not excluded.
// font-family of ,