summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/lib/jquery.webfonts/src/jquery.webfonts.js
blob: a28ca39dd3dcf0daf320e2d84a67e2606b80efcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
 * 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 <input>, <textarea> and <button> must be changed explicitly.
			// Add class webfonts-changed when webfonts are applied.
			$element.find( 'textarea, input, button' ).not( this.options.exclude )
				.css( 'font-family', fontStack.join() )
				.addClass( 'webfonts-changed' );
		},

		/**
		 * Load given font families if not loaded already. Creates the CSS rules
		 * and appends them to document.
		 *
		 * @param {Array|String} fontFamilies List of font families
		 */
		load: function( fontFamilies ) {
			var css, fontFamily, i,
				fontFaceRule = '';

			// Convert to array if string given (old signature)
			if ( typeof fontFamilies === 'string' ) {
				fontFamilies = [fontFamilies];
			}

			for ( i = 0; i < fontFamilies.length; i++ ) {
				fontFamily = fontFamilies[i];
				if ( $.inArray( fontFamily, this.fonts ) >= 0 ) {
					continue;
				}

				css = this.getCSS( fontFamily, 'normal' );
				if ( css !== false ) {
					fontFaceRule += css;
					this.fonts.push( fontFamily );
				}
			}

			// In case the list contained only fonts that are already loaded
			// or non-existing fonts.
			if ( fontFaceRule !== '' ) {
				injectCSS( fontFaceRule );
			}

			return true;
		},

		/**
		 * Parse the element for custom font-family styles and for nodes with
		 * different language than what the element itself has.
		 */
		parse: function() {
			var webfonts = this,
				// Fonts can be added indirectly via classes, but also with
				// style attributes. For lang attributes we will use our font
				// if they don't have explicit font already.
				$elements = webfonts.$element.find( '*[lang], [style], [class]' ),
				// List of fonts to load in a batch
				fontQueue = [],
				// List of elements to apply a certain font family in a batch.
				// Object keys are the font family, values are list of plain elements.
				elementQueue = {};

			// Add to the font queue(no dupes)
			function addToFontQueue( value ) {
				if ( $.inArray( value, fontQueue ) < 0 ) {
					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 ) {
				var fontFamilyStyle, fontFamily,
					$element = $( element );

				if ( $element.is( webfonts.options.exclude ) ) {
					return;
				}

				// Note: it depends on the browser whether this returns font names
				// which don't exist. In Chrome it does, while in Opera it doesn't.
				fontFamilyStyle = $element.css( 'fontFamily' );
				// Note: It is unclear whether this can ever be falsy. Maybe also
				// browser specific.
				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];
						// Remove the ' and " characters if any.
						fontFamily = $.trim( fontFamily.replace( /["']/g, '' ) );
						addToFontQueue( fontFamily );
					}
				}

				// Load and apply fonts for other language tagged elements (batched)
				if ( element.lang && element.lang !== webfonts.language ) {
					// language differs. We may want to apply a different font.
					if ( webfonts.hasExplicitFontStyle ( $element ) &&
						!webfonts.isOverridable( fontFamilyStyle ) ) {
						// respect the explicit font family style. Do not override.
						// This style may be from css, inheritance, or even from
						// browser settings.
						return;
					} else {
						fontFamily = webfonts.getFont( element.lang, element.className.split(/\s+/) );
					}

					if ( !fontFamily ) {
						// No font preference for the language.
						// Check if we need to reset for this language.
						// If the font of the parent element, to which webfonts were applied,
						// remained the same, there is no need to reset.
						if ( webfonts.$element.css( 'fontFamily' ) !== webfonts.originalFontFamily ) {
							// The parent font changed.
							// Is there an inheritance?
							// Is the font for this element the same as parent's font?
							if ( fontFamilyStyle === webfonts.$element.css( 'fontFamily' ) ) {
								// Break inheritance of the font from the parent element
								// by applying the original font to this element
								fontFamily = webfonts.originalFontFamily;
							}
						}
					}

					// We do not have fonts for all languages
					if ( fontFamily ) {
						addToFontQueue( fontFamily );
						addToElementQueue( element, fontFamily );
					}
				}
			} );

			// Process in batch the accumulated fonts and elements
			this.load( fontQueue );
			$.each( elementQueue, function( fontFamily, elements ) {
				webfonts.apply( fontFamily, $( elements ) );
			} );
		},

		/**
		 * Find out whether an element has explicit non generic font family style
		 * For the practical purpose we check whether font is same as top element
		 * or having any of generic font family
		 * http://www.w3.org/TR/CSS2/fonts.html#generic-font-families
		 * @param {jQuery} $element
		 * @return {boolean}
		 */
		hasExplicitFontStyle: function ( $element ) {
			var elementFontFamily = $element.css( 'fontFamily' );

			// whether the font is inherited from top element to which plugin applied
			return this.$element.css( 'fontFamily' ) !== elementFontFamily
					// whether the element has generic font family
					&& ( $.inArray( elementFontFamily,
					[ 'monospace', 'serif', 'cursive', 'fantasy', 'sans-serif' ] ) < 0 );
		},

		/**
		 * Check whether the given font family is overridable or not. jquery.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, '' );

			return $.inArray( fontFamily, overridableFontFamilies ) >= 0;
		},

		/**
		 * List all fonts for the given language
		 *
		 * @param {String} [language] Language code. If undefined all fonts will be listed.
		 * @return {Array} List of font family names.
		 */
		list: function( language ) {
			var fontName,
				fontNames = [];

			if ( language ) {
				fontNames = this.repository.languages[language] || [];
			} else {
				for ( fontName in this.repository.fonts ) {
					if ( this.repository.fonts.hasOwnProperty( fontName ) ) {
						fontNames.push( fontName );
					}
				}
			}

			return fontNames;
		},

		/**
		 * List all languages supported by the repository
		 *
		 * @return {Array} List of language codes
		 */
		languages: function() {
			var language,
				languages = [];

			for ( language in this.repository.languages ) {
				if ( this.repository.languages.hasOwnProperty( language ) ) {
					languages.push( language );
				}
			}

			return languages;
		},

		/**
		 * Set the font repository
		 *
		 * @param {Object} repository The font repository.
		 */
		setRepository: function( repository ) {
			this.repository = $.extend( WebFonts.repository, repository );
		},

		/**
		 * Reset the font-family style.
		 */
		reset: function() {
			this.$element.find( '.webfonts-changed' )
				.removeClass( '.webfonts-changed' )
				.css( 'font-family', '' );
			this.apply( this.originalFontFamily );
		},

		/**
		 * Unbind the plugin
		 */
		unbind: function() {
			this.$element.data( 'webfonts', null );
		},

		/**
		 * Construct the CSS required for the font-family.
		 *
		 * @param {String} fontFamily The font-family name
		 * @param {String} [variant] The font variant, eg: bold, italic etc. Default is normal.
		 * @return {String} CSS
		 */
		getCSS: function( fontFamily, variant ) {
			var webfonts, base,
				fontFaceRule, userAgent, fontStyle, fontFormats, fullFontName,
				fontconfig = this.repository.get( fontFamily );

			variant = variant || 'normal';
			fullFontName = fontFamily;

			if ( variant !== 'normal' ) {
				if ( fontconfig.variants !== undefined && fontconfig.variants[variant] ) {
					fullFontName = fontconfig.variants[variant];
					fontconfig = this.repository.get( fontconfig.variants[variant] );
				}
			}

			if ( !fontconfig ) {
				return false;
			}

			base = this.repository.base;
			fontFaceRule = '@font-face { font-family: \'' + fontFamily + '\';\n';
			userAgent = window.navigator.userAgent;
			fontStyle = fontconfig.fontstyle || 'normal';
			fontFormats = [];

			if ( fontconfig.eot ) {
				fontFaceRule += '\tsrc: url(\'' + base + fontconfig.eot + '\');\n';
			}
			fontFaceRule += '\tsrc: ';

			// If the font is present locally, use it.
			if ( userAgent.match( /Android 2\.3/ ) === null ) {
				// Android 2.3.x does not respect local() syntax.
				// http://code.google.com/p/android/issues/detail?id=10609
				fontFaceRule += 'local(\'' + fullFontName + '\'),';
			}

			if ( fontconfig.woff2 ) {
				fontFormats.push( '\t\turl(\'' + base + fontconfig.woff2
					+ '\') format(\'woff2\')' );
			}

			if ( fontconfig.woff ) {
				fontFormats.push( '\t\turl(\'' + base + fontconfig.woff
					+ '\') format(\'woff\')' );
			}

			if ( fontconfig.svg ) {
				fontFormats.push( '\t\turl(\'' + base + fontconfig.svg + '#'
					+ fontFamily + '\') format(\'svg\')' );
			}

			if ( fontconfig.ttf ) {
				fontFormats.push( '\t\turl(\'' + base + fontconfig.ttf
					+ '\') format(\'truetype\')' );
			}

			fontFaceRule += fontFormats.join() + ';\n';

			if ( fontconfig.fontweight ) {
				fontFaceRule += '\tfont-weight:' + fontconfig.fontweight + ';';
			}

			if ( fontconfig.fontstyle !== undefined ) {
				fontFaceRule += '\tfont-style:' + fontconfig.fontstyle + ';';
			} else {
				fontFaceRule += '\tfont-style: normal;';
			}

			fontFaceRule += '}\n';

			webfonts = this;
			if ( fontconfig.variants !== undefined ) {
				$.each( fontconfig.variants, function ( variant ) {
					fontFaceRule += webfonts.getCSS( fontFamily, variant );
				} );
			}

			return fontFaceRule;
		}
	};

	$.fn.webfonts = function( option ) {
		return this.each( function() {
			var $this = $( this ),
				data = $this.data( 'webfonts' ),
				options = typeof option === 'object' && option;

			if ( !data ) {
				$this.data( 'webfonts', ( data = new WebFonts( this, options ) ) );
			}

			if ( typeof option === 'string' ) {
				data[option]();
			}
		} );
	};

	$.fn.webfonts.defaults = {
		repository: WebFonts.repository, // Default font repository
		fontStack: [ 'Helvetica', 'Arial', 'sans-serif' ], // Default font fallback
		exclude: '', // jQuery selectors to exclude
		overridableFontFamilies: []
	};

	$.fn.webfonts.Constructor = WebFonts;

	// Private methods for the WebFonts prototype

	/**
	 * Create a new style tag and add it to the DOM.
	 *
	 * @param {String} css
	 */
	function injectCSS( css ) {
		var s = document.createElement( 'style' );

		// Insert into document before setting cssText
		document.getElementsByTagName( 'head' )[0].appendChild( s );

		if ( s.styleSheet ) {
			s.styleSheet.cssText = css;
			// IE
		} else {
			// Safari sometimes borks on null
			s.appendChild( document.createTextNode( String( css ) ) );
		}
	}
} )( jQuery, window, document );