Home

Awesome

Google Font Installer

Google Font Installer is a NodeJS module/CLI that lets you Search, Download and Install fonts offered by Google Web Fonts.

You can use it in two ways:

how gfi works

Table of content

<br> ###### Font installation footnote In Linux and OSX, the font will be installed in the user's font directory (~/.fonts for Linux, ~/Library/Fonts for OSX). In Windows, due to the fact that font installation require some register modifications, I prefered to create a little WScript (a windows script that use ActiveX windows interface) and spawn a `cscript` process to install the font in a _'windows native way'_.

CLI

First of all you have to install google-font-installer in your system

$ npm install -g google-font-installer

Now, from your terminal emulator, you can use the command gfi

Search a font

$ gfi search [family_name]

The search is really permissive, so you can specify only few characters and view all the font families that contains these characters. Words order is also not important. For instance, search for Source Sans or Sans Source will produce the same result.

Download a font

$ gfi download [family_name] [-d|--dest destination_folder] [-v|--variants comma_separeted_variants]

If family_name will match more than one family, nothing will be downloaded: a list of alternatives will help you better specify the font family name.

Download command accepts two options:

Install a font

$ gfi install [family_name] [-v|--variants comma_separeted_variants]

If family_name will match more than one family, nothing will be installed: a list of alternatives will help you better specify the font family name.

Install command accepts only one option:

Copy font CSS url

$ gfi copy [family_name] [-v|--variants comma_separeted_variants]

If family_name will match more than one family, nothing will be copied: a list of alternatives will help you better specify the font family name.

<a id="cli-examples"></a>

Examples

Search the source keyword

$ gfi search source

Search results for: "source"

 * Source Code Pro
    Category: monospace
    Variants: 200, 300, regular, 500, 600, 700, 900
    CSS Url: https://fonts.googleapis.com/css?family=Source+Code+Pro
 * Source Sans Pro
    Category: sans-serif
    Variants: 200, 200italic, 300, 300italic, regular, italic, 600, 600italic, 700, 700italic, 900, 900italic
    CSS Url: https://fonts.googleapis.com/css?family=Source+Sans+Pro
 * Source Serif Pro
    Category: serif
    Variants: regular, 600, 700
    CSS Url: https://fonts.googleapis.com/css?family=Source+Serif+Pro

Download Source Sans Pro 600 and 700italic

$ gfi download source sans pro -v 600,700italic

Source Sans Pro variant 600 downloaded in /home/user/someFolder/SourceSansPro-600.ttf
Source Sans Pro variant 700italic downloaded in /home/user/someFolder/SourceSansPro-700italic.ttf

Install Lato 100

$ gfi install lato -v 100

Lato variant 100 downloaded in /home/user/.fonts/Lato-100.ttf

Copy font url for Work Sans variants 200, normal and 600

$ gfi copy work sans -v 200,400,600

"work sans" font url has been copied to your clipboard.

(https://fonts.googleapis.com/css?family=Work+Sans:200,400,600 will be available in your clipboard)

APIs

First of all you have to install the module in you NodeJS project:

$ npm install google-font-installer --save

And then you can require it in your code:

var GoogleFontList = require('google-font-installer');

To use this API is necessary to obtain a Browser Key enabled to Google Font API from the Google Developer Dashboard.

GoogleFontList

GoogleFontList is a class that can be instanced with the Google API Key as only argument.

var fontList = new GoogleFontList('API_KEY');

If you prefer you can also create an instance without the apiKey and call the methods .setApiKey('API_KEY') and .downloadList() in a second moment.

var fontList = new GoogleFontList();
fontList.setApiKey('API_KEY');
fontList.downloadList();

<a id="google-font-list-events"></a>

Events

'success'

Emitted when the Font List is downloaded from Google and the data are converted, stored and processed. Callback contains only one argument for convenience: the object itself

'error'

Emitter if something go wrong in downloading, coverting or processing data. The Callback argument is the error object.

<a id="google-font-list-properties"></a>

Public properties

data [Array]

Array of GoogleFont instances, a class that extends the data provided by the Google APIs. Empty until 'success' event.

apiKey [String]

The Google APIs Key setted with the constructor or with setApiKey().

<a id="google-font-list-methods"></a>

Public methods

setApiKey('API_KEY')

Used to set the apiKey to download data from Google Web Font APIs.

downloadList()

Download the list from Google Web Font APIs.

parseRawData(rawData)

Parse raw data in valid JSON format. Used internally, but public for convenience if someone prefers to use the object without downloading the list (ex. cached data).

populate(jsonData)

Populate the object data property with an array of GoogleFont instances, based on the data provided by Google Apis. Used internally, but public for convenience if someone prefers to use the object without downloading and parsing the list (ex. cached data).

clone()

Return a new instance of the object, with the same data and apiKey properties.

searchFont(term, field, callback)

Function with callback used to search a font inside the object data property: it's case insensitive, words order insensitive and test if the field CONTAIN that words (ex. source sans, source Sans and sans source will produce the same result).

searchFontByName(term, callback)

Same as searchFont, but specific for the Family property. (the font name)

searchFontByType(term, callback)

Same as searchFont, but specific for the Category property. (for instance serif, sans-serif, display, etc)

getFont(term, field, callback)

Function with callback used to get a specific font where a field and the term exactly match (case insensitive)

getFontByName(term, callback)

Same as getFont, but specific for the Family property. (the font name)

getFontByType(term, callback)

Same as getFont, but specific for the Category property. (for instance serif, sans-serif, display, etc)

getFirst()
isSingle()
forEachFont(fn, callback)

Execture fn foreach GoogleFOnt in GoogleFontList data property. It's async.

GoogleFont

Class that extends data structure provided by Google APIs.

It's instanced by the populate method of GoogleFontList, called internally by the constructor or by parseRawData method.

Every object inside the data property of GoogleFontList is an instance of this class.

<a id="google-font-properties"></a>

Public properties

family category version lastModified [String]

Inherits by Google Font API structure, respectively the font name, the font type (serif, sans-serif, etc), the version and the string rappresenting the last modification data.

subsets [Array]

Inherits by Google Font API structure, a list of the available subsest for that font.

variants [Array]

Inherits by Google Font API structure, a list of all available variants (weights) for that font.

files [Object]

Inherits by Google Font API structure, an object with key-value of variant and ttf remote file.

cssUrl [String]

Url of the css file containing the ready-to-use imports for the web about that specific font.

<a id="google-font-methods"></a>

Public methods

getFamily() getCategory() getVariants() getSubsets() getVersion() getLastMod() getFileList() getCssUrl()

Method to access to the public properties: it's a better idea use them insteed the properties for a future-proof reason. Maybe someday google will decide to changhe his properties names. All returns the type of the property, except GetLastMod() that return a new Date instance.

hasVariant(variant)
getFiles(variants)
saveAt(variants, destFolder, callback)

Download specified variants of the font in the destination folder directory.

download(variants, callback)

Download specified variants of the font in the current directory (where the script is called).

install(variants, callback)

Install specified variants of the font. The destination folder depends on the platform used:

<a id="api-examples"></a>

Examples

var GoogleFontlist = require('google-font-installer');

var fontList = new GoogleFontlist('VALID_API_KEY');

fontList.on('success', function(){
	this.searchFontByName('Source Sans Pro', function(err, filteredList) {
		if (err)
			throw err;
		filteredList.getFirst().download(['300', '400'], function(err, result){
			if (err)
				throw err;
			result.forEach(function(el, index){
				console.log('Variant %s of %s downloaded in %s', el.variant, el.family, el.path);
			})
		});
	})
})

fontList.on('error', function(err){
	throw err;
})