figlet = function (txt, options, next) { me.text(txt, options, next); }
n/a
defaults = function (opts) { if (typeof opts === 'object' && opts !== null) { for (var prop in opts) { if (opts.hasOwnProperty(prop)) { figDefaults[prop] = opts[prop]; } } } return JSON.parse(JSON.stringify(figDefaults)); }
...
### textSync
The browser API supports a synchronous mode so long as fonts used are preloaded.
Example:
```js
figlet.defaults({fontPath: "assets/fonts"});
figlet.preloadFonts(["Standard", "Ghost"], ready);
function ready(){
console.log(figlet.textSync("ASCII"));
console.log(figlet.textSync("Art", "Ghost"));
}
...
fonts = function (next) { var fontList = []; fs.readdir(fontDir, function (err, files) { // '/' denotes the root folder if (err) { return next(err); } files.forEach( function (file) { if ( /\.flf$/.test(file) ) { fontList.push( file.replace(/\.flf$/,'') ); } }); next(null, fontList); }); }
...
```
### fonts
The fonts function allows you to get a list of all of the available fonts. Example usage:
```js
figlet.fonts(function(err, fonts) {
if (err) {
console.log('something went wrong...');
console.dir(err);
return;
}
console.dir(fonts);
});
...
fontsSync = function () { var fontList = []; fs.readdirSync(fontDir).forEach(function(file) { if ( /\.flf$/.test(file) ) { fontList.push( file.replace(/\.flf$/,'') ); } }); return fontList; }
...
```
### fontsSync
The synchronous version of the fonts method
```js
console.log(figlet.fontsSync());
```
Getting Started - The Browser
-------------------------
The browser API is the same as the Node API with the exception of the "fonts" method not being available. The browser
version also requires [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (or a [shim](https://github.com/github
/fetch)) for its loadFont function.
...
loadFont = function (name, next) { fs.readFile(fontDir + name + '.flf', {encoding: 'utf-8'}, function(err, fontData) { if (err) { return next(err); } fontData = fontData + ''; try { next(null, figlet.parseFont(name, fontData)); } catch(error) { next(error); } }); }
n/a
loadFontSync = function (name) { var fontData = fs.readFileSync(fontDir + name + '.flf', {encoding: 'utf-8'}); fontData = fontData + ''; return figlet.parseFont(name, fontData); }
n/a
metadata = function (fontName, next) {
fontName = fontName + '';
/*
Load the font. If it loads, it's data will be contained in the figFonts object.
The callback will recieve a fontsOpts object, which contains the default
options of the font (its fitting rules, etc etc).
*/
me.loadFont(fontName, function(err, fontOpts) {
if (err) {
next(err);
return;
}
next(null, fontOpts, figFonts[fontName].comment);
});
}
...
In most cases you'll either use the default setting or the "fitted" setting. Most fonts don't support vertical
kerning, but a hand full fo them do (like the "Standard" font).
### metadata
The metadata function allows you to retrieve a font's default options and header comment. Example usage:
```js
figlet.metadata('Standard', function(err, options, headerComment) {
if (err) {
console.log('something went wrong...');
console.dir(err);
return;
}
console.dir(options);
console.log(headerComment);
...
parseFont = function (fontName, data) {
data = data.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
figFonts[fontName] = {};
var lines = data.split("\n");
var headerData = lines.splice(0,1)[0].split(" ");
var figFont = figFonts[fontName];
var opts = {};
opts.hardBlank = headerData[0].substr(5,1);
opts.height = parseInt(headerData[1], 10);
opts.baseline = parseInt(headerData[2], 10);
opts.maxLength = parseInt(headerData[3], 10);
opts.oldLayout = parseInt(headerData[4], 10);
opts.numCommentLines = parseInt(headerData[5], 10);
opts.printDirection = (headerData.length >= 6) ? parseInt(headerData[6], 10) : 0;
opts.fullLayout = (headerData.length >= 7) ? parseInt(headerData[7], 10) : null;
opts.codeTagCount = (headerData.length >= 8) ? parseInt(headerData[8], 10) : null;
opts.fittingRules = getSmushingRules(opts.oldLayout, opts.fullLayout);
figFont.options = opts;
// error check
if (opts.hardBlank.length !== 1 ||
isNaN(opts.height) ||
isNaN(opts.baseline) ||
isNaN(opts.maxLength) ||
isNaN(opts.oldLayout) ||
isNaN(opts.numCommentLines) )
{
throw new Error('FIGlet header contains invalid values.');
}
/*
All FIGlet fonts must contain chars 32-126, 196, 214, 220, 228, 246, 252, 223
*/
var charNums = [], ii;
for (ii = 32; ii <= 126; ii++) {
charNums.push(ii);
}
charNums = charNums.concat(196, 214, 220, 228, 246, 252, 223);
// error check - validate that there are enough lines in the file
if (lines.length < (opts.numCommentLines + (opts.height * charNums.length)) ) {
throw new Error('FIGlet file is missing data.');
}
/*
Parse out the context of the file and put it into our figFont object
*/
var cNum, endCharRegEx, parseError = false;
figFont.comment = lines.splice(0,opts.numCommentLines).join("\n");
figFont.numChars = 0;
while (lines.length > 0 && figFont.numChars < charNums.length) {
cNum = charNums[figFont.numChars];
figFont[cNum] = lines.splice(0,opts.height);
// remove end sub-chars
for (ii = 0; ii < opts.height; ii++) {
if (typeof figFont[cNum][ii] === "undefined") {
figFont[cNum][ii] = "";
} else {
endCharRegEx = new RegExp("\\"+figFont[cNum][ii].substr(figFont[cNum][ii].length-1,1)+"+$");
figFont[cNum][ii] = figFont[cNum][ii].replace(endCharRegEx,"");
}
}
figFont.numChars++;
}
/*
Now we check to see if any additional characters are present
*/
while (lines.length > 0) {
cNum = lines.splice(0,1)[0].split(" ")[0];
if ( /^0[xX][0-9a-fA-F]+$/.test(cNum) ) {
cNum = parseInt(cNum, 16);
} else if ( /^0[0-7]+$/.test(cNum) ) {
cNum = parseInt(cNum, 8);
} else if ( /^[0-9]+$/.test(cNum) ) {
cNum = parseInt(cNum, 10);
} else if ( /^-0[xX][0-9a-fA-F]+$/.test(cNum) ) {
cNum = parseInt(cNum, 16);
} else {
if (cNum === "") {break;}
// something's wrong
console.log("Invalid data:"+cNum);
parseError = true;
break;
}
figFont[cNum] = lines.splice(0,opts.height);
// remove end sub-chars
for (ii = 0; ii < opts.height; ii++) {
if (typeof figFont[cNum][ii] === "undefined") {
figFont[cNum][ii] = "";
} else {
endCharRegEx = new RegExp("\\"+figFont[cNum][ii].substr(figFont[cNum][ii].length-1,1)+"+$");
figFont[cNum][ii] = figFont[cNum][ii].replace(endCharRegEx,"");
}
}
figFont.numChars++;
}
// error check
if (parseError === true) {
throw new Error('Error parsing data.');
}
return opts;
}
n/a
preloadFonts = function (fonts, next) {
if (typeof jQuery === 'undefined') { /* TODO: create common function for jQuery checks */
throw new Error('jQuery is required for ajax method to work.');
}
jQuery.when.apply(this, fonts.map(function(name){
return jQuery.get(figDefaults.fontPath + '/' + name + '.flf')
})).then(function() {
var args = fonts.length > 1 ? arguments : [arguments];
for(var i in fonts){
me.parseFont(fonts[i], args[i][0]);
}
if(next)next();
});
}
...
The browser API supports a synchronous mode so long as fonts used are preloaded.
Example:
```js
figlet.defaults({fontPath: "assets/fonts"});
figlet.preloadFonts(["Standard", "Ghost"], ready);
function ready(){
console.log(figlet.textSync("ASCII"));
console.log(figlet.textSync("Art", "Ghost"));
}
```
...
text = function (txt, options, next) {
var fontName = '';
// Validate inputs
txt = txt + '';
if (typeof arguments[1] === 'function') {
next = options;
options = {};
options.font = figDefaults.font; // default font
}
if (typeof options === 'string') {
fontName = options;
options = {};
} else {
options = options || {};
fontName = options.font || figDefaults.font;
}
/*
Load the font. If it loads, it's data will be contained in the figFonts object.
The callback will recieve a fontsOpts object, which contains the default
options of the font (its fitting rules, etc etc).
*/
me.loadFont(fontName, function(err, fontOpts) {
if (err) {
return next(err);
}
next(null, generateText(fontName, _reworkFontOpts(fontOpts, options), txt));
});
}
...
* Input Text - A string of text to turn into ASCII Art.
* Font Options - Either a string indicating the font name or an options object (description below).
* Callback - A function to execute with the generated ASCII Art.
Example:
```js
figlet.text('Boo!', {
font: 'Ghost',
horizontalLayout: 'default',
verticalLayout: 'default'
}, function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
...
textSync = function (txt, options) { var fontName = ''; // Validate inputs txt = txt + ''; if (typeof options === 'string') { fontName = options; options = {}; } else { options = options || {}; fontName = options.font || figDefaults.font; } var fontOpts = _reworkFontOpts(me.loadFontSync(fontName), options); return generateText(fontName, fontOpts, txt); }
...
* Input Text - A string of text to turn into ASCII Art.
* Font Options - Either a string indicating the font name or an options object (description below).
Example:
```js
console.log(figlet.textSync('Boo!', {
font: 'Ghost',
horizontalLayout: 'default',
verticalLayout: 'default'
}));
```
That will print out:
...
figlet = function (txt, options, next) { me.text(txt, options, next); }
n/a
defaults = function (opts) { if (typeof opts === 'object' && opts !== null) { for (var prop in opts) { if (opts.hasOwnProperty(prop)) { figDefaults[prop] = opts[prop]; } } } return JSON.parse(JSON.stringify(figDefaults)); }
...
### textSync
The browser API supports a synchronous mode so long as fonts used are preloaded.
Example:
```js
figlet.defaults({fontPath: "assets/fonts"});
figlet.preloadFonts(["Standard", "Ghost"], ready);
function ready(){
console.log(figlet.textSync("ASCII"));
console.log(figlet.textSync("Art", "Ghost"));
}
...
fonts = function (next) { var fontList = []; fs.readdir(fontDir, function (err, files) { // '/' denotes the root folder if (err) { return next(err); } files.forEach( function (file) { if ( /\.flf$/.test(file) ) { fontList.push( file.replace(/\.flf$/,'') ); } }); next(null, fontList); }); }
...
```
### fonts
The fonts function allows you to get a list of all of the available fonts. Example usage:
```js
figlet.fonts(function(err, fonts) {
if (err) {
console.log('something went wrong...');
console.dir(err);
return;
}
console.dir(fonts);
});
...
fontsSync = function () { var fontList = []; fs.readdirSync(fontDir).forEach(function(file) { if ( /\.flf$/.test(file) ) { fontList.push( file.replace(/\.flf$/,'') ); } }); return fontList; }
...
```
### fontsSync
The synchronous version of the fonts method
```js
console.log(figlet.fontsSync());
```
Getting Started - The Browser
-------------------------
The browser API is the same as the Node API with the exception of the "fonts" method not being available. The browser
version also requires [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (or a [shim](https://github.com/github
/fetch)) for its loadFont function.
...
loadFont = function (name, next) { fs.readFile(fontDir + name + '.flf', {encoding: 'utf-8'}, function(err, fontData) { if (err) { return next(err); } fontData = fontData + ''; try { next(null, figlet.parseFont(name, fontData)); } catch(error) { next(error); } }); }
n/a
loadFontSync = function (name) { var fontData = fs.readFileSync(fontDir + name + '.flf', {encoding: 'utf-8'}); fontData = fontData + ''; return figlet.parseFont(name, fontData); }
n/a
metadata = function (fontName, next) {
fontName = fontName + '';
/*
Load the font. If it loads, it's data will be contained in the figFonts object.
The callback will recieve a fontsOpts object, which contains the default
options of the font (its fitting rules, etc etc).
*/
me.loadFont(fontName, function(err, fontOpts) {
if (err) {
next(err);
return;
}
next(null, fontOpts, figFonts[fontName].comment);
});
}
...
In most cases you'll either use the default setting or the "fitted" setting. Most fonts don't support vertical
kerning, but a hand full fo them do (like the "Standard" font).
### metadata
The metadata function allows you to retrieve a font's default options and header comment. Example usage:
```js
figlet.metadata('Standard', function(err, options, headerComment) {
if (err) {
console.log('something went wrong...');
console.dir(err);
return;
}
console.dir(options);
console.log(headerComment);
...
parseFont = function (fontName, data) {
data = data.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
figFonts[fontName] = {};
var lines = data.split("\n");
var headerData = lines.splice(0,1)[0].split(" ");
var figFont = figFonts[fontName];
var opts = {};
opts.hardBlank = headerData[0].substr(5,1);
opts.height = parseInt(headerData[1], 10);
opts.baseline = parseInt(headerData[2], 10);
opts.maxLength = parseInt(headerData[3], 10);
opts.oldLayout = parseInt(headerData[4], 10);
opts.numCommentLines = parseInt(headerData[5], 10);
opts.printDirection = (headerData.length >= 6) ? parseInt(headerData[6], 10) : 0;
opts.fullLayout = (headerData.length >= 7) ? parseInt(headerData[7], 10) : null;
opts.codeTagCount = (headerData.length >= 8) ? parseInt(headerData[8], 10) : null;
opts.fittingRules = getSmushingRules(opts.oldLayout, opts.fullLayout);
figFont.options = opts;
// error check
if (opts.hardBlank.length !== 1 ||
isNaN(opts.height) ||
isNaN(opts.baseline) ||
isNaN(opts.maxLength) ||
isNaN(opts.oldLayout) ||
isNaN(opts.numCommentLines) )
{
throw new Error('FIGlet header contains invalid values.');
}
/*
All FIGlet fonts must contain chars 32-126, 196, 214, 220, 228, 246, 252, 223
*/
var charNums = [], ii;
for (ii = 32; ii <= 126; ii++) {
charNums.push(ii);
}
charNums = charNums.concat(196, 214, 220, 228, 246, 252, 223);
// error check - validate that there are enough lines in the file
if (lines.length < (opts.numCommentLines + (opts.height * charNums.length)) ) {
throw new Error('FIGlet file is missing data.');
}
/*
Parse out the context of the file and put it into our figFont object
*/
var cNum, endCharRegEx, parseError = false;
figFont.comment = lines.splice(0,opts.numCommentLines).join("\n");
figFont.numChars = 0;
while (lines.length > 0 && figFont.numChars < charNums.length) {
cNum = charNums[figFont.numChars];
figFont[cNum] = lines.splice(0,opts.height);
// remove end sub-chars
for (ii = 0; ii < opts.height; ii++) {
if (typeof figFont[cNum][ii] === "undefined") {
figFont[cNum][ii] = "";
} else {
endCharRegEx = new RegExp("\\"+figFont[cNum][ii].substr(figFont[cNum][ii].length-1,1)+"+$");
figFont[cNum][ii] = figFont[cNum][ii].replace(endCharRegEx,"");
}
}
figFont.numChars++;
}
/*
Now we check to see if any additional characters are present
*/
while (lines.length > 0) {
cNum = lines.splice(0,1)[0].split(" ")[0];
if ( /^0[xX][0-9a-fA-F]+$/.test(cNum) ) {
cNum = parseInt(cNum, 16);
} else if ( /^0[0-7]+$/.test(cNum) ) {
cNum = parseInt(cNum, 8);
} else if ( /^[0-9]+$/.test(cNum) ) {
cNum = parseInt(cNum, 10);
} else if ( /^-0[xX][0-9a-fA-F]+$/.test(cNum) ) {
cNum = parseInt(cNum, 16);
} else {
if (cNum === "") {break;}
// something's wrong
console.log("Invalid data:"+cNum);
parseError = true;
break;
}
figFont[cNum] = lines.splice(0,opts.height);
// remove end sub-chars
for (ii = 0; ii < opts.height; ii++) {
if (typeof figFont[cNum][ii] === "undefined") {
figFont[cNum][ii] = "";
} else {
endCharRegEx = new RegExp("\\"+figFont[cNum][ii].substr(figFont[cNum][ii].length-1,1)+"+$");
figFont[cNum][ii] = figFont[cNum][ii].replace(endCharRegEx,"");
}
}
figFont.numChars++;
}
// error check
if (parseError === true) {
throw new Error('Error parsing data.');
}
return opts;
}
n/a
preloadFonts = function (fonts, next) {
if (typeof jQuery === 'undefined') { /* TODO: create common function for jQuery checks */
throw new Error('jQuery is required for ajax method to work.');
}
jQuery.when.apply(this, fonts.map(function(name){
return jQuery.get(figDefaults.fontPath + '/' + name + '.flf')
})).then(function() {
var args = fonts.length > 1 ? arguments : [arguments];
for(var i in fonts){
me.parseFont(fonts[i], args[i][0]);
}
if(next)next();
});
}
...
The browser API supports a synchronous mode so long as fonts used are preloaded.
Example:
```js
figlet.defaults({fontPath: "assets/fonts"});
figlet.preloadFonts(["Standard", "Ghost"], ready);
function ready(){
console.log(figlet.textSync("ASCII"));
console.log(figlet.textSync("Art", "Ghost"));
}
```
...
text = function (txt, options, next) {
var fontName = '';
// Validate inputs
txt = txt + '';
if (typeof arguments[1] === 'function') {
next = options;
options = {};
options.font = figDefaults.font; // default font
}
if (typeof options === 'string') {
fontName = options;
options = {};
} else {
options = options || {};
fontName = options.font || figDefaults.font;
}
/*
Load the font. If it loads, it's data will be contained in the figFonts object.
The callback will recieve a fontsOpts object, which contains the default
options of the font (its fitting rules, etc etc).
*/
me.loadFont(fontName, function(err, fontOpts) {
if (err) {
return next(err);
}
next(null, generateText(fontName, _reworkFontOpts(fontOpts, options), txt));
});
}
...
* Input Text - A string of text to turn into ASCII Art.
* Font Options - Either a string indicating the font name or an options object (description below).
* Callback - A function to execute with the generated ASCII Art.
Example:
```js
figlet.text('Boo!', {
font: 'Ghost',
horizontalLayout: 'default',
verticalLayout: 'default'
}, function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
...
textSync = function (txt, options) { var fontName = ''; // Validate inputs txt = txt + ''; if (typeof options === 'string') { fontName = options; options = {}; } else { options = options || {}; fontName = options.font || figDefaults.font; } var fontOpts = _reworkFontOpts(me.loadFontSync(fontName), options); return generateText(fontName, fontOpts, txt); }
...
* Input Text - A string of text to turn into ASCII Art.
* Font Options - Either a string indicating the font name or an options object (description below).
Example:
```js
console.log(figlet.textSync('Boo!', {
font: 'Ghost',
horizontalLayout: 'default',
verticalLayout: 'default'
}));
```
That will print out:
...