/** * 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 ); 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, getFont: function( language ) { if ( this.options.fontSelector ) { return this.options.fontSelector( this.repository, language || this.language ); } else { return this.repository.defaultFont( language ); } }, /** * Initialize. */ init: function() { var fontFamily; if ( this.language ) { fontFamily = this.getFont( this.language ); this.apply( fontFamily ); } this.parse(); }, refresh: function() { this.reset(); this.init(); }, /** * Apply a font for the element. * * @param fontFamily String: font family name * @param $element */ 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 ); fontStack.unshift( fontFamily ); } // Set the font of this element if it's not excluded if ( !$element.is( this.options.exclude ) ) { $element.css( 'font-family', fontStack.join() ); } if ( !fontFamily || fontFamily === this.originalFontFamily ) { // We are resetting the font to original font. // Do not explicitly set fontFamily for child nodes. Just remove // the previous setting. $element.find( 'textarea, input, button' ) .not( this.options.exclude ) .css( 'font-family', '' ); return; } // Set the font of this element's children if they are not excluded. // font-family of ,