Home

Awesome

Build Status

font-manager

A C++ module for Node.js providing access to the system font catalog.

Features

Platforms

Installation

Installation of the font-manager module is via npm:

npm install font-manager

On Linux, you also may need to install the libfontconfig-dev package, for example:

sudo apt-get install libfontconfig-dev

API

You load the font-manager module using require as with all Node modules:

var fontManager = require('font-manager');

All of the methods exported by font-manager have both synchronous and asynchronous versions available. You should generally prefer the asynchronous version as it will allow your program to continue doing other processing while a request for fonts is processing in the background, which may be expensive depending on the platform APIs that are available.

getAvailableFonts()

Returns an array of all font descriptors available on the system.

// asynchronous API
fontManager.getAvailableFonts(function(fonts) { ... });

// synchronous API
var fonts = fontManager.getAvailableFontsSync();

// output
[ { path: '/Library/Fonts/Arial.ttf',
    postscriptName: 'ArialMT',
    family: 'Arial',
    style: 'Regular',
    weight: 400,
    width: 5,
    italic: false,
    monospace: false },
  ... ]

findFonts(fontDescriptor)

Returns an array of font descriptors matching a query font descriptor. The returned array may be empty if no fonts match the font descriptor.

// asynchronous API
fontManager.findFonts({ family: 'Arial' }, function(fonts) { ... });

// synchronous API
var fonts = fontManager.findFontsSync({ family: 'Arial' });

// output
[ { path: '/Library/Fonts/Arial.ttf',
    postscriptName: 'ArialMT',
    family: 'Arial',
    style: 'Regular',
    weight: 400,
    width: 5,
    italic: false,
    monospace: false },
  { path: '/Library/Fonts/Arial Bold.ttf',
    postscriptName: 'Arial-BoldMT',
    family: 'Arial',
    style: 'Bold',
    weight: 700,
    width: 5,
    italic: false,
    monospace: false } ]

findFont(fontDescriptor)

Returns a single font descriptors matching a query font descriptors as well as possible. This method always returns a result (never null), so sometimes the output will not exactly match the input font descriptor if not all input parameters could be met.

// asynchronous API
fontManager.findFont({ family: 'Arial', weight: 700 }, function(font) { ... });

// synchronous API
var font = fontManager.findFontSync({ family: 'Arial', weight: 700 });

// output
{ path: '/Library/Fonts/Arial Bold.ttf',
  postscriptName: 'Arial-BoldMT',
  family: 'Arial',
  style: 'Bold',
  weight: 700,
  width: 5,
  italic: false,
  monospace: false }

substituteFont(postscriptName, text)

Substitutes the font with the given postscriptName with another font that contains the characters in text. If a font matching postscriptName is not found, a font containing the given characters is still returned. If a font matching postscriptName is found, its characteristics (bold, italic, etc.) are used to find a suitable replacement. If the font already contains the characters in text, it is not replaced and the font descriptor for the original font is returned.

// asynchronous API
fontManager.substituteFont('TimesNewRomanPSMT', '汉字', function(font) { ... });

// synchronous API
var font = fontManager.substituteFontSync('TimesNewRomanPSMT', '汉字');

// output
{ path: '/Library/Fonts/Songti.ttc',
  postscriptName: 'STSongti-SC-Regular',
  family: 'Songti SC',
  style: 'Regular',
  weight: 400,
  width: 5,
  italic: false,
  monospace: false }

Font Descriptor

Font descriptors are normal JavaScript objects that describe characteristics of a font. They are passed to the findFonts and findFont methods and returned by all of the methods. Any combination of the fields documented below can be used to find fonts, but all methods return full font descriptors.

NameTypeDescription
pathstringThe path to the font file in the filesystem. (not applicable for queries, only for results)
postscriptNamestringThe PostScript name of the font (e.g 'Arial-BoldMT'). This uniquely identities a font in most cases.
familystringThe font family name (e.g 'Arial')
stylestringThe font style name (e.g. 'Bold')
weightnumberThe font weight (e.g. 400 for normal weight). Should be a multiple of 100, between 100 and 900. See below for weight documentation.
widthnumberThe font width (e.g. 5 for normal width). Should be an integer between 1 and 9. See below for width documentation.
italicbooleanWhether the font is italic or not.
monospacebooleanWhether the font is monospace or not.

Weights

ValueName
100Thin
200Ultra Light
300Light
400Normal
500Medium
600Semi Bold
700Bold
800Ultra Bold
900Heavy

Widths

ValueName
1Ultra Condensed
2Extra Condensed
3Condensed
4Semi Condensed
5Normal
6Semi Expanded
7Expanded
8Extra Expanded
9Ultra Expanded

License

MIT