Skip to content

Instantly share code, notes, and snippets.

@jem-computer
Created August 5, 2015 14:21
Show Gist options
  • Save jem-computer/815e7d17ef0ebb989e65 to your computer and use it in GitHub Desktop.
Save jem-computer/815e7d17ef0ebb989e65 to your computer and use it in GitHub Desktop.
opentype = require 'opentype.js'
R = require 'ramda'
fonts =
openSans:
url: 'fonts/apache/opensans'
file: 'OpenSans-Regular.ttf'
roboto:
url: 'fonts/apache/roboto'
file: 'Roboto-Regular.ttf'
robotoCondensed:
url: 'fonts/apache/robotocondensed'
file: 'RobotoCondensed-Regular.ttf'
merriweather:
url: 'fonts/ofl/merriweather'
file: 'Merriweather-Regular.ttf'
inconsolata:
url: 'fonts/ofl/inconsolata'
file: 'Inconsolata-Regular.ttf'
joinPath = (font) ->
[font.url, font.file].join('/')
getWidth = (glyph) ->
{xMin, xMax} = glyph
xMax - xMin
getHeight = (glyph) ->
{yMin, yMax} = glyph
yMax - yMin
getMetrics = (glyph) ->
glyph.getMetrics()
getXHeight = R.pipe(
R.map getMetrics
R.map getHeight
R.reduce R.flip(R.divide), 1
)
averageHeight = R.pipe(
R.map getMetrics
R.map getHeight
R.mean
)
averageWidth = R.pipe(
R.map getMetrics
R.map getWidth
R.mean
)
characterWidthRatio = R.converge(
R.divide
averageWidth
averageHeight
)
loadFont = (f) ->
opentype.load joinPath(f), (err, font) ->
if err then console.error err
output = {
names: R.pick ['fullName', 'fontFamily'], font.names
xHeight: getXHeight(font.stringToGlyphs('Ax')).toFixed(4)
width: characterWidthRatio(font.stringToGlyphs('M')).toFixed(4)
}
console.log JSON.stringify output, null, 2
console.log '---------------'
R.map(loadFont, R.values(fonts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment