Comparing version 0.2.2 to 0.3.0
@@ -1,3 +0,4 @@ | ||
var Subset = module.exports = function(font) { | ||
var Subset = module.exports = function(font, opts) { | ||
this.font = font.clone() | ||
this.opts = opts || {} | ||
this.subset = { '32': 32 } | ||
@@ -11,5 +12,13 @@ this.mapping = { '32': 32 } | ||
var code = chars.charCodeAt(i) | ||
if (code in this.mapping || code < 33) continue | ||
this.subset[this.pos] = code | ||
this.mapping[code] = this.pos++ | ||
if (code in this.mapping || code < 33) { | ||
continue | ||
} | ||
if (this.opts.remap === false) { | ||
this.subset[code] = code | ||
this.mapping[code] = code | ||
} else { | ||
this.subset[this.pos] = code | ||
this.mapping[code] = this.pos++ | ||
} | ||
} | ||
@@ -45,3 +54,3 @@ } | ||
ids.sort() | ||
// collect the actual glyphs | ||
@@ -62,3 +71,3 @@ function collect(ids) { | ||
} | ||
return collect(ids) | ||
@@ -70,5 +79,5 @@ } | ||
, glyphs = this.glyphs() | ||
this.font.tables.cmap.subtables = [cmap.subtable] | ||
var old2new = { 0: 0 } | ||
@@ -79,3 +88,3 @@ for (var code in cmap.charMap) { | ||
} | ||
var nextGlyphID = cmap.maxGlyphID | ||
@@ -86,3 +95,3 @@ for (var oldID in glyphs) { | ||
} | ||
var new2old = {} | ||
@@ -98,3 +107,3 @@ for (var id in old2new) { | ||
}) | ||
// encode the font tables | ||
@@ -106,3 +115,3 @@ var offsets = this.font.tables.glyf.embed(glyphs, oldIDs, old2new) | ||
this.font.tables.maxp.embed(oldIDs) | ||
this.font.tables.name.embed(cmap.charMap) | ||
this.font.tables.name.embed(cmap.charMap, this.opts.trimNames) | ||
} | ||
@@ -109,0 +118,0 @@ |
@@ -32,37 +32,32 @@ var Struct = require('structjs') | ||
Name.prototype.embed = function(charMap) { | ||
Name.prototype.embed = function(charMap, trimNames) { | ||
var postscriptName = null | ||
for (var i = 0; i < this.records.length; ++i) { | ||
var record = this.records[i] | ||
if (record.nameID === 6) { | ||
if (postscriptName === null) postscriptName = record.string | ||
if (trimNames && record.platformID !== 0) { // unicode | ||
this.records.splice(i--, 1) | ||
continue | ||
} | ||
switch (record.nameID) { | ||
case 0: // copyright | ||
case 2: // font subfamily name | ||
case 5: // version string | ||
case 7: // trademark | ||
case 13: // license description | ||
case 14: // license info URL | ||
// preserve | ||
break | ||
case 1: // font family name | ||
case 3: // unique font identifier | ||
case 4: // full font name | ||
case 6: // postscript name | ||
record.string = "TTFJS+" + record.string | ||
break | ||
default: | ||
this.records.splice(i--, 1) | ||
break | ||
} | ||
} | ||
this.records.push(new Record({ | ||
platformID: 1, | ||
encodingID: 0, | ||
languageID: 0, | ||
nameID: 6, | ||
length: 0, | ||
offset: 0, | ||
string: "MARKUS+" + postscriptName | ||
})) | ||
// sample text | ||
// var sample = this.records.filter(function(record) { | ||
// return record.nameID === 19 | ||
// })[0] | ||
// if (!sample) { | ||
// sample = new Record({ | ||
// platformID: 0, | ||
// encodingID: 3, | ||
// languageID: 0, | ||
// nameID: 19, | ||
// length: 0, | ||
// offset: 0 | ||
// }) | ||
// this.records.push(sample) | ||
// } | ||
// sample.string = String.fromCharCode.apply(String, Object.keys(charMap)) | ||
} |
@@ -129,4 +129,4 @@ var Directory = require('./directory') | ||
TTFFont.prototype.subset = function() { | ||
return new Subset(this) | ||
TTFFont.prototype.subset = function(opts) { | ||
return new Subset(this, opts) | ||
} | ||
@@ -133,0 +133,0 @@ |
@@ -8,3 +8,3 @@ { | ||
"description": "TTFjs is a TrueType font parser entirely written in JavaScript and compatible to both Node.js and the Browser.", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"main": "./lib/ttf", | ||
@@ -11,0 +11,0 @@ "homepage": "https://github.com/rkusa/ttfjs", |
@@ -16,4 +16,86 @@ # ttfjs | ||
Coming soon ... | ||
```js | ||
var TTFFont = require('ttfjs') | ||
``` | ||
### new TTFFont(buffer) | ||
Create a new font instance from the provided buffer (can be a Node `Buffer` or an `ArrayBuffer`). | ||
#### .stringWidth(str, size) | ||
Returns the width for the given string in the given font size. | ||
#### .lineHeight(size, [includeGap]) | ||
Returns the line height for the given font size with line gap or without. | ||
#### .lineDescent(size) | ||
Return the line descent for the given font size. | ||
#### .clone() | ||
Returns a clone of the font object. | ||
#### .save() | ||
Encodes the font into an `ArrayBuffer`. | ||
#### .subset([opts]) | ||
Creates a `Subset` for the current font. | ||
### .subset([opts]) | new TTFFont.Subset(font, [opts]) | ||
Creates a `Subset` for the current font. | ||
**Options:** | ||
- **remap** - (default: true) whether to remap char codes | ||
- **trimNames** - (default: false) whether trim localized names (reduces file size) | ||
#### .use(chars) | ||
Add the given characters to the subset. | ||
#### .encode(str) | ||
Encode the given string using the current remapping. | ||
#### .embed() | ||
Embed the current used characters. | ||
#### .save() | ||
Encodes the subset into an `ArrayBuffer`. | ||
## Example | ||
```js | ||
var fs = require('fs') | ||
var TTFFont = require('ttfjs') | ||
var font = new TTFFont(fs.readFileSync(__dirname + '/Corbel.ttf')) | ||
var subset = font.subset() | ||
subset.use('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890') | ||
console.log(subset.encode('abcdefghijklmnopqrstuvwxyz')) | ||
subset.embed() | ||
fs.writeFileSync('./test.ttf', toBuffer(subset.save()), { encoding: 'binary' }) | ||
function toBuffer(ab) { | ||
var buffer = new Buffer(ab.byteLength) | ||
var view = new Uint8Array(ab) | ||
for (var i = 0; i < buffer.length; ++i) { | ||
buffer[i] = view[i] | ||
} | ||
return buffer | ||
} | ||
``` | ||
## MIT License | ||
@@ -20,0 +102,0 @@ Copyright (c) 2013-2015 Markus Ast |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32631
815
110