jQuery Fontpicker Plugin Example

A component to quickly choose fonts from Google Web Fonts, custom fonts you (the web developer) provide, as well as system fonts.
View project on GitHub.

Example 1: Fully default

Default configuration. Dropdown shows a list of default system fonts, as well as all available Google fonts. Users can select both a font-family and a font-variant (font weight- and style).


<input id="font1" type="text">

<script>
$('#font1')
.fontpicker()
.on('change', function() {
	applyFont('#sample1', this.value);
});
</script>
		



API: -

Example 2: Default local fonts, custom Google fonts, disabled variant-selection

The dropdown shows the default list of local fonts. The list of Google fonts the user can choose from is customized. Users can only select a font-family, font-variant selection is disabled (variants: false).
This Fontpicker will not remember last picked fonts (nrRecents: 0).
This Fontpicker allows the user to clear/deselect a chosen font (showClear: true).


<input id="font2" type="text" value="Indie Flower">

<script>
$('#font2').fontpicker({
	variants: false,
	nrRecents: 0,
	showClear: true,
	googleFonts: [
		'Abel', 'Advent Pro', 'Changa', 'Eczar', 'Gloria Hallelujah',
		'Indie Flower', 'Press Start 2P', 'Slackey', 'Yeon Sung'
	],
	localFonts: false
})
.on('change', function() {
	applyFont('#sample2', this.value);
});
</script>
		

Example 3: Custom local fonts, no Google fonts

The dropdown shows a list of system fonts (Arial, Georgia, Times New Roman and Verdana), as well as three custom fonts (Action Man, Bauer, Bubble).
Google fonts are disabled (googleFonts: false).
The local font files have to be in TTF, Woff, Woff2 or OTF format, and they should all be put in a single folder, under the document root folder of your site. Something like /fonts makes sense. Provide the path to this folder as the localFontsUrl configuration parameter.
All fonts should be in the same format (woff, woff2, ttf, otf). Define your format via the localFontsType parameter.
In this example, there are 3 files in the htdocs/fonts folder: Action Man.woff, Bauer.woff, Bubble.woff.
Make sure the font files are named the same as the font names you provide in the localFonts object.


<input id="font3" type="text">

<script>
var localFonts = {
	"Arial": {
		"category": "sans-serif",
		"variants": "400,400i,600,600i"
	},
	"Georgia": {
		"category": "serif",
		"variants": "400,400i,600,600i"
	},
	"Times New Roman": {
		"category": "serif",
		"variants": "400,400i,600,600i"
	},
	"Verdana": {
		"category": "sans-serif",
		"variants": "400,400i,600,600i",
	},

	"Action Man": {},
	"Bauer": {
		"category": "display",
		"variants": "400,400i,600,600i",
		"subsets": "latin-ext,latin"
	},
	"Bubble": {
		"category": "display",
		"variants": "400,400i,600,600i",
		"subsets": "latin-ext,latin"
	}
};

$('#font3').fontpicker({
	localFontsUrl: 'fonts/', // End with a slash!
	localFonts: localFonts,
	localFontsType: 'woff', // Either 'ttf', 'woff', 'woff2' or 'otf'
	googleFonts: false
})
.on('change', function() {
	applyFont('#sample3', this.value);
});
</script>
		

Example 4: Some local fonts, custom Google fonts, onSelect callback

The dropdown shows a mix of local, custom and Google fonts. Instead of on('change', fn), the onSelect callback is used to be notified about the font the user picked. The onSelect option is called with a single argument: an object containing fontType (local or google), fontFamily, fontStyle, fontWeight and fontSpec members.
obj.fontSpec contains the same value that on('change', fn) would be called with.


<input id="font4" type="text" value="Cookie">

<script>
var localFonts = {
	"Arial": {
		"category": "sans-serif",
		"variants": "400,400i,600,600i"
	},
	"Georgia": {
		"category": "serif",
		"variants": "400,400i,600,600i"
	},
	"Times New Roman": {
		"category": "serif",
		"variants": "400,400i,600,600i"
	},
	"Verdana": {
		"category": "sans-serif",
		"variants": "400,400i,600,600i",
	},

	"Action Man": {},
	"Bauer": {
		"category": "display",
		"variants": "400,400i,600,600i",
		"subsets": "latin-ext,latin"
	},
	"Bubble": {
		"category": "display",
		"variants": "400,400i,600,600i",
		"subsets": "latin-ext,latin"
	}
};

<script>
$('#font4').fontpicker({
	localFontsUrl: 'fonts/',
	localFonts: localFonts,
	googleFonts: [
		'Mali', 'Aladin', 'Alex Brush', 'Amita', 'Annie Use Your Telescope', 'Architects Daughter',
		'Bad Script', 'Bilbo', 'Bonbon', 'Caveat', 'Chilanka', 'Cookie', 'Crafty Girls'
	],
	onSelect: function(obj) {
		console.log('onSelect', obj);
		applyFont('#sample4', obj.fontSpec);
	}
});
</script>