Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Oct 14, 2014
0 parents commit 46b16a1
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
10 changes: 10 additions & 0 deletions .eslintrc
@@ -0,0 +1,10 @@
env:
browser: false
node: true
rules:
no-extra-parens: 2
eqeqeq: 2
block-scoped-var: 2
quotes:
- 2
- single
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
coverage
node_modules
5 changes: 5 additions & 0 deletions .jscs.json
@@ -0,0 +1,5 @@
{
"preset": "google",
"maximumLineLength": 90,
"excludeFiles": ["{coverage,node_modules}/**"]
}
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
language: node_js
node_js:
- '0.10'
- '0.11'
after_script:
- npm run-script coveralls
notifications:
email: false
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Shinnosuke Watanabe

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
@@ -0,0 +1,78 @@
# read-font-cmap

[![Build Status](https://travis-ci.org/shinnn/read-font-cmap.svg?branch=master)](https://travis-ci.org/shinnn/read-font-cmap)
[![Build status](https://ci.appveyor.com/api/projects/status/2a35xhls3avqvpoo?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/node-font-cmap-259)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/read-font-cmap.svg)](https://coveralls.io/r/shinnn/read-font-cmap)
[![Dependency Status](https://david-dm.org/shinnn/read-font-cmap.svg)](https://david-dm.org/shinnn/read-font-cmap)
[![devDependency Status](https://david-dm.org/shinnn/read-font-cmap/dev-status.svg)](https://david-dm.org/shinnn/read-font-cmap#info=devDependencies)

A [Node](http://nodejs.org/) module to parse [CMap](http://www.microsoft.com/typography/otspec/cmap.htm) of a TrueType/OpenType font file

```javascript
var readFontCmap = require('read-font-cmap');

readFontCmap('bower_components/font-awesome/fonts/FontAwesome.otf', function(err, map) {
if (err) {
throw err;
}

console.log(map); // yields: '{"32": 1, "168": 6, "169": 12, "174": 10, ... }'
});
```

## Installation

[![NPM version](https://badge.fury.io/js/read-font-cmap.svg)](https://www.npmjs.org/package/read-font-cmap)

[Use npm](https://www.npmjs.org/doc/cli/npm-install.html).

```sh
npm install read-font-cmap
```

## API

```javascript
var readFontCmap = require('read-font-cmap');
```

### readFontCmap(*filePath*, *callback*)

*filePath*: `String` (font file path)
*callback*: `Function`

It reads and parses a TrueType/OpenType font file asynchronously, then runs callback function.

#### callback(*error*, *cmap*)

*error*: `Object` (an error if it fails to parse the font, otherwise `null`)
*cmap*: `Object`

The second argument represents CMap table in the form:

```json
{
"Unicode value (integer)": "Glyph ID (integer)"
}
```

[Here](https://raw.githubusercontent.com/shinnn/read-font-cmap/master/test/fixture.json) is a real-life example, the result of parsing [Font Awesome](http://fortawesome.github.io/Font-Awesome/) CMap table.

### readFontCmap.sync(*filePath*)

*filePath*: `String` (font file path)
Return: `Object` (CMap table)

Synchronous version of [`readFontCmap`](#readfontcmapfilepath-callback).

```javascript
var readFontCmap = require('read-font-cmap');
readFontCmap.sync('bower_components/font-awesome/fonts/FontAwesome.otf');
//=> {"32": 1, "168": 6, "169": 12, "174": 10, ... }
```

## License

Copyright (c) 2014 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
19 changes: 19 additions & 0 deletions appveyor.yml
@@ -0,0 +1,19 @@
init:
- git config --global core.autocrlf input

version: '{build}'

environment:
matrix:
- nodejs_version: '0.10'
- nodejs_version: '0.11'

install:
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
- npm install

build: off

test_script:
- ps: 'node test.js # PowerShell'
- cmd: node test.js
36 changes: 36 additions & 0 deletions index.js
@@ -0,0 +1,36 @@
/*!
* read-font-cmap | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/read-font-cmap
*/
'use strict';

var fs = require('fs');

var fontCmap = require('font-cmap');

module.exports = function readFontCmap(filePath, cb) {
if (typeof cb !== 'function') {
throw new TypeError('Expecting a callback function as a second argument.');
}

fs.readFile(filePath, function(err, buf) {
if (err) {
cb(err);
return;
}

var map;

try {
map = fontCmap(buf);
} catch (e) {
err = e;
}

cb(err, map);
});
};

module.exports.sync = function readFontCmap(filePath) {
return fontCmap(fs.readFileSync(filePath));
};
53 changes: 53 additions & 0 deletions package.json
@@ -0,0 +1,53 @@
{
"name": "read-font-cmap",
"version": "0.0.0",
"description": "parse CMap of a TrueType/OpenType font file",
"repository": "shinnn/read-font-cmap",
"author": {
"name": "Shinnosuke Watanabe",
"url": "https://github.com/shinnn"
},
"scripts": {
"pretest": "eslint *.js & jscs *.js",
"test": "node test.js | tap-spec",
"coverage": "istanbul cover test.js",
"coveralls": "${npm_package_scripts_coverage} && istanbul-coveralls"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/shinnn/read-font-cmap/blob/master/LICENSE"
}
],
"files": [
"index.js",
"LICENSE"
],
"keywords": [
"read",
"file",
"font",
"glyph",
"code-point",
"unicode",
"parse",
"cmap",
"table",
"data",
"metadata"
],
"dependencies": {
"font-cmap": "^1.0.0"
},
"devDependencies": {
"eslint": "^0.8.2",
"font-awesome": "^4.2.0",
"istanbul": "^0.3.2",
"istanbul-coveralls": "^1.0.0",
"jscs": "^1.6.2",
"lodash": "^2.4.1",
"nop": "0.0.0",
"tap-spec": "^1.0.0",
"tape": "^3.0.0"
}
}
78 changes: 78 additions & 0 deletions test.js
@@ -0,0 +1,78 @@
'use strict';

var isPlainObject = require('lodash').isPlainObject;
var noop = require('nop');
var readFontCmap = require('./');
var test = require('tape');

var fontPath = 'node_modules/font-awesome/fonts/FontAwesome.otf';

test('readFontCmap()', function(t) {
t.plan(8);

readFontCmap(fontPath, function(err, map) {
t.error(err, 'should read a font file.');
t.ok(isPlainObject(map), 'should create glyph map object.');
});

t.throws(
readFontCmap.bind(null), /function/,
'should throw an error when it takes no arguments.'
);

t.throws(
readFontCmap.bind(null, 1, noop), /path/,
'should throw an error when it takes non-string argument.'
);

t.throws(
readFontCmap.bind(null, fontPath), /function/,
'should throw an error when it doesn\'t take the second argument.'
);

t.throws(
readFontCmap.bind(null, fontPath, {foo: true}),
/function/,
'should throw an error when the second argument is not a function.'
);

readFontCmap('foo', function(err) {
t.equal(err.code, 'ENOENT', 'should pass an error when the file doesn\'t exist.');
});

readFontCmap('package.json', function(err) {
t.ok(
/Unsupported OpenType version/.test(err),
'should pass an error when the file is not a valid font file.'
);
});
});

test('readFontCmap.sync()', function(t) {
t.plan(5);

t.ok(
isPlainObject(readFontCmap.sync(fontPath)),
'should should create glyph map object.'
);

t.throws(
readFontCmap.sync.bind(null), /path/,
'should throw an error when it takes no arguments.'
);

t.throws(
readFontCmap.sync.bind(null, [fontPath]), /path/,
'should throw an error when it takes non-string argument.'
);

t.throws(
readFontCmap.sync.bind(null, 'foo'), /ENOENT/,
'should throw an error when the file doesn\'t exist.'
);

t.throws(
readFontCmap.sync.bind(null, __filename), /Unsupported OpenType version/,
'should pass an error when the file is not a valid font file.'
);
});

0 comments on commit 46b16a1

Please sign in to comment.