Skip to content

Commit

Permalink
add font store
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Apr 17, 2016
1 parent 964ab18 commit 344ab8f
Show file tree
Hide file tree
Showing 15 changed files with 874 additions and 31 deletions.
63 changes: 52 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fonteditor-core
# fonteditor-core

**font editor core functions**
**FontEditor core functions**

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
Expand All @@ -9,25 +9,29 @@
## Feature

- sfnt parse
- read, write, transform fonts (ttf, woff, eot, svg, otf)
- read, write, transform fonts (ttf, woff, eot, svg, otf)
- ttf glyph adjust
- svg to glyph

## Usage

```js
// read font file
var Font = require('fonteditor-core').Font;
var fs = require('fs');
var bufferToArrayBuffer = require('b3b').bufferToArrayBuffer;
var fontBuffer = fs.readFileSync('font.ttf');
var fontArrayBuffer = bufferToArrayBuffer(fontBuffer);
var buffer = fs.readFileSync('font.ttf');

// read font data
var TTFReader = require('fonteditor-core').TTFReader;
var ttfReader = new TTFReader({ hinting: true });
var fontData = ttfReader.read(fontArrayBuffer);

console.log(Object.keys(fontData));
var font = Font.create(buffer, {
type: 'ttf', // support ttf,woff,eot,otf,svg
subset: [65, 66], // only read `a`, `b` glyf
hinting: true, // save font hinting
compound2simple: true, // transform ttf compound glyf to simple
inflate: null, // inflate function for woff
combinePath: false, // for svg path
});
var fontObject = font.get();
console.log(Object.keys(fontObject));
/* => [ 'version',
'numTables',
'searchRenge',
Expand All @@ -47,6 +51,43 @@ console.log(Object.keys(fontData));
]
*/

// write font file
var buffer = font.write({
type: 'woff', // support ttf,woff,eot,otf,svg
hinting: true, // save font hinting
deflate: null, // deflate function for woff
});
// fs.writeFileSync('font.woff', buffer);

// to base64 str
font.toBase64({
type: 'ttf' // support ttf,woff,eot,svg
});

// optimize glyf
font.optimize()

// compound2simple
font.compound2simple()

// sort glyf
font.sort()

// find glyf
var result = font.find({
unicode: [65]
});
var result = font.find({
filter: function (glyf) {
return glyf.name == 'icon'
}
});

// merge another font object
font.merge(font1, {
scale: 1
});

```

## Demo
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fonteditor-core",
"version": "0.0.18",
"version": "0.0.20",
"description": "fonts (ttf, woff, eot, svg, otf) parse, write, transform. glyph adjust.",
"keywords": [
"sfnt",
Expand Down Expand Up @@ -34,7 +34,8 @@
],
"scripts": {
"npm_publish": "sh tool/publish-node.sh",
"test": "phantomjs test/run-jasmine.js test/SpecRunner.html"
"test": "phantomjs test/run-jasmine.js test/SpecRunner.html",
"test_node": "sh tool/test-node.sh"
},
"edp": {
"main": "src/main",
Expand All @@ -52,6 +53,7 @@
"graphics",
"math",
"ttf",
"nodejs",
"main.js"
],
"main": "main.js",
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
define(
function (require) {
return {
Font: require('./ttf/font'),
TTF: require('./ttf/ttf'),
TTFReader: require('./ttf/ttfreader'),
TTFWriter: require('./ttf/ttfwriter'),
Expand Down
60 changes: 60 additions & 0 deletions src/nodejs/buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file Buffer和ArrayBuffer转换
* @author mengke01([email protected])
*/

/**
* Buffer转换成ArrayBuffer
*
* @param {Buffer} buffer 缓冲数组
* @return {ArrayBuffer}
*/
function toArrayBuffer(buffer) {
var length = buffer.length;
var view = new DataView(new ArrayBuffer(length), 0, length);
for (var i = 0, l = length; i < l; i++) {
view.setUint8(i, buffer[i], false);
}
return view.buffer;
}

/**
* ArrayBuffer转换成Buffer
*
* @param {ArrayBuffer} arrayBuffer 缓冲数组
* @return {Buffer}
*/
function toBuffer(arrayBuffer) {
if (Array.isArray(arrayBuffer)) {
return new Buffer(arrayBuffer);
}
else {
var length = arrayBuffer.byteLength;
var view = new DataView(arrayBuffer, 0, length);
var buffer = new Buffer(length);
for (var i = 0, l = length; i < l; i++) {
buffer[i] = view.getUint8(i, false);
}
}

return buffer;
}

if (typeof exports !== 'undefined') {
module.exports = {
toArrayBuffer: toArrayBuffer,
toBuffer: toBuffer
};
}
else {
define(
function (require) {
return {
toArrayBuffer: toArrayBuffer,
toBuffer: toBuffer
};
}
);
}


Loading

0 comments on commit 344ab8f

Please sign in to comment.