Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge with upstream repo (and fix subsetting!) #5

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1a2a572
Use hard coded offSize in CFF encoder
devongovett Jan 28, 2019
2d1b0eb
1.7.8
devongovett Jan 28, 2019
e8ad1df
Added `defaultLanguage` to font objects (#194)
scottrippey Mar 7, 2019
b2a7529
Fix issue with undefined private dict in CFF
devongovett Mar 11, 2019
7b4f75d
Allow deleting a glyph via a multiple substitution in GSUB
devongovett Mar 11, 2019
4ca73fe
Merge branch 'master' of github.com:devongovett/fontkit
devongovett Mar 11, 2019
abbe9c2
Set a global default language (#195)
scottrippey Mar 11, 2019
436bd5e
Fix saving loca index format/version when subsetting (#191)
blikblum Mar 11, 2019
408f67c
Add missing font
devongovett Mar 11, 2019
d6a46e3
Formatting
devongovett Mar 11, 2019
374f1c4
1.8.0
devongovett Mar 11, 2019
1a53baa
Adding repository entry to packages json so that it appears on npm (#…
luanpotter Mar 11, 2019
9d41540
Augment AATMorxProcessor to allow for features to be disabled (instea…
issacgerges Oct 6, 2019
f2aa3d6
Use Promise instead of process.nextTick (#209)
blikblum Nov 17, 2019
cebf94e
Fix getting the fd index for OTF/CFF CID fonts (#207)
blikblum Nov 17, 2019
0e04e01
Update brf to ^2.0.0 so that it does not pull static-eval versions lo…
musculman Nov 17, 2019
15ef9d9
Fixes getting path from woff files with CFF (#200)
blikblum Nov 17, 2019
881d1b6
Bump lodash from 4.17.4 to 4.17.15 (#210)
dependabot[bot] Nov 17, 2019
f3f55a0
Fix for FDArray parsing in CFF (#224)
liborm85 Apr 25, 2020
fe37449
Fix for new Function call (#223)
liborm85 Apr 25, 2020
417af0c
1.8.1
devongovett Apr 25, 2020
006d0da
Merge with upstream repo
joewestcott Mar 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ Fontkit includes several methods for accessing glyph metrics and performing layo

Returns the advance width (described above) for a single glyph id.

#### `font.layout(string, features = [])`
#### `font.layout(string, features = [] | {})`

This method returns a `GlyphRun` object, which includes an array of `Glyph`s and `GlyphPosition`s for the given string.
`Glyph` objects are described below. `GlyphPosition` objects include 4 properties: `xAdvance`, `yAdvance`, `xOffset`,
and `yOffset`.

The `features` parameter is an array of [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) to be applied
in addition to the default set. If this is an AAT font, the OpenType feature tags are mapped to AAT features.
The `features` parameter is either an array of [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) to be applied
in addition to the default set, or an object mapping OpenType features to a boolean enabling or disabling each. If this is an AAT font, the OpenType feature tags are mapped to AAT features.

### Variation fonts

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"babel-plugin-istanbul": "^4.1.3",
"base64-arraybuffer": "^0.1.5",
"buffer": "^5.6.0",
"clone": "^1.0.1",
"clone": "^1.0.4",
"codepoints": "^1.2.0",
"concat-stream": "^1.4.6",
"concat-stream": "^1.6.2",
"deep-equal": "^1.0.0",
"dfa": "^1.0.0",
"dfa": "^1.2.0",
"esdoc": "^0.4.8",
"esdoc-es7-plugin": "0.0.3",
"iconv-lite": "^0.4.13",
"iconv-lite": "^0.4.24",
"mocha": "^2.0.1",
"nyc": "^10.3.2",
"rollup": "^2.10.2",
Expand Down
41 changes: 24 additions & 17 deletions src/TTFFont.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class TTFFont {
}

constructor(stream, variationCoords = null) {
this.defaultLanguage = null;
this.stream = stream;
this.variationCoords = variationCoords;

Expand All @@ -44,6 +45,10 @@ export default class TTFFont {
}
}

setDefaultLanguage(lang = null) {
this.defaultLanguage = lang;
}

_getTable(table) {
if (!(table.tag in this._tables)) {
try {
Expand Down Expand Up @@ -83,34 +88,36 @@ export default class TTFFont {
return result;
}

/**
* The unique PostScript name for this font
* @type {string}
*/
get postscriptName() {
let name = this.name.records.postscriptName;
if (name) {
let lang = Object.keys(name)[0];
return name[lang];
}

return null;
}

/**
* Gets a string from the font's `name` table
* `lang` is a BCP-47 language code.
* @return {string}
*/
getName(key, lang = 'en') {
let record = this.name.records[key];
getName(key, lang = this.defaultLanguage || fontkit.defaultLanguage) {
let record = this.name && this.name.records[key];
if (record) {
return record[lang];
// Attempt to retrieve the entry, depending on which translation is available:
return (
record[lang]
|| record[this.defaultLanguage]
|| record[fontkit.defaultLanguage]
|| record['en']
|| record[Object.keys(record)[0]] // Seriously, ANY language would be fine
|| null
);
}

return null;
}

/**
* The unique PostScript name for this font, e.g. "Helvetica-Bold"
* @type {string}
*/
get postscriptName() {
return this.getName('postscriptName');
}

/**
* The font's full name, e.g. "Helvetica Bold"
* @type {string}
Expand Down
13 changes: 9 additions & 4 deletions src/aat/AATMorxProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ export default class AATMorxProcessor {
}

// Processes an array of glyphs and applies the specified features
// Features should be in the form of {featureType:{featureSetting:true}}
// Features should be in the form of {featureType:{featureSetting:boolean}}
process(glyphs, features = {}) {
for (let chain of this.morx.chains) {
let flags = chain.defaultFlags;

// enable/disable the requested features
for (let feature of chain.features) {
let f;
if ((f = features[feature.featureType]) && f[feature.featureSetting]) {
flags &= feature.disableFlags;
flags |= feature.enableFlags;
if (f = features[feature.featureType]) {
if (f[feature.featureSetting]) {
flags &= feature.disableFlags;
flags |= feature.enableFlags;
} else if (f[feature.featureSetting] === false) {
flags |= ~feature.disableFlags;
flags &= ~feature.enableFlags;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ const fontkit = {
},
};

fontkit.defaultLanguage = 'en';
fontkit.setDefaultLanguage = function(lang = 'en') {
fontkit.defaultLanguage = lang;
};

export default fontkit;

2 changes: 1 addition & 1 deletion src/cff/CFFFont.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class CFFFont {

if (gid < ranges[mid].first) {
high = mid - 1;
} else if (mid < high && gid > ranges[mid + 1].first) {
} else if (mid < high && gid >= ranges[mid + 1].first) {
low = mid + 1;
} else {
return ranges[mid].fd;
Expand Down
4 changes: 3 additions & 1 deletion src/cff/CFFTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class CFFPrivateOp {
let FontDict = new CFFDict([
// key name type(s) default
[18, 'Private', new CFFPrivateOp, null],
[[12, 38], 'FontName', 'sid', null]
[[12, 38], 'FontName', 'sid', null],
[[12, 7], 'FontMatrix', 'array', [0.001, 0, 0, 0.001, 0, 0]],
[[12, 5], 'PaintType', 'number', 0],
]);

let CFFTopDict = new CFFDict([
Expand Down
6 changes: 2 additions & 4 deletions src/glyph/CFFGlyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ export default class CFFGlyph extends Glyph {
}

_getPath() {
let { stream } = this._font;
let { pos } = stream;

let cff = this._font.CFF2 || this._font['CFF '];
let { stream } = cff;
let str = cff.topDict.CharStrings[this.id];
let end = str.offset + str.length;
stream.pos = str.offset;
Expand All @@ -49,7 +47,7 @@ export default class CFFGlyph extends Glyph {
let gsubrs = cff.globalSubrIndex || [];
let gsubrsBias = this.bias(gsubrs);

let privateDict = cff.privateDictForGlyph(this.id);
let privateDict = cff.privateDictForGlyph(this.id) || {};
let subrs = privateDict.Subrs || [];
let subrsBias = this.bias(subrs);

Expand Down
6 changes: 5 additions & 1 deletion src/glyph/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export default class Path {
* @return {string}
*/
toFunction() {
return (ctx) => this.commands.forEach((c) => ctx[c.command].apply(ctx, c.args));
return ctx => {
this.commands.forEach(c => {
return ctx[c.command].apply(ctx, c.args)
})
};
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/opentype/GSUBProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export default class GSUBProcessor extends OTProcessor {
let index = this.coverageIndex(table.coverage);
if (index !== -1) {
let sequence = table.sequences.get(index);

if (sequence.length === 0) {
// If the sequence length is zero, delete the glyph.
// The OpenType spec disallows this, but seems like Harfbuzz and Uniscribe allow it.
this.glyphs.splice(this.glyphIterator.index, 1);
return true;
}

this.glyphIterator.cur.id = sequence[0];
this.glyphIterator.cur.ligatureComponent = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/subset/CFFSubset.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default class CFFSubset extends Subset {
let top = {
version: 1,
hdrSize: this.cff.hdrSize,
offSize: this.cff.length,
offSize: 4,
header: this.cff.header,
nameIndex: [this.cff.postscriptName],
topDictIndex: [topDict],
Expand Down
4 changes: 3 additions & 1 deletion src/subset/Subset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import r from '@pdf-lib/restructure';

const resolved = Promise.resolve();

export default class Subset {
constructor(font) {
this.font = font;
Expand All @@ -26,7 +28,7 @@ export default class Subset {
encodeStream() {
let s = new r.EncodeStream();

process.nextTick(() => {
resolved.then(() => {
this.encode(s);
return s.end();
});
Expand Down
4 changes: 2 additions & 2 deletions src/subset/TTFSubset.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export default class TTFSubset extends Subset {
this.glyf = [];
this.offset = 0;
this.loca = {
offsets: []
offsets: [],
version: this.font.loca.version
};

this.hmtx = {
Expand All @@ -77,7 +78,6 @@ export default class TTFSubset extends Subset {
maxp.numGlyphs = this.glyf.length;

this.loca.offsets.push(this.offset);
Tables.loca.preEncode.call(this.loca);

let head = cloneDeep(this.font.head);
head.indexToLocFormat = this.loca.version;
Expand Down
5 changes: 0 additions & 5 deletions src/tables/loca.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ loca.process = function() {
};

loca.preEncode = function() {
if (this.version != null) return;

// assume this.offsets is a sorted array
this.version = this.offsets[this.offsets.length - 1] > 0xffff ? 1 : 0;

if (this.version === 0) {
for (let i = 0; i < this.offsets.length; i++) {
this.offsets[i] >>>= 1;
Expand Down
Binary file added test/data/FiraSans/FiraSans-Regular.ttf
Binary file not shown.
93 changes: 93 additions & 0 deletions test/data/FiraSans/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright (c) 2012-2015, The Mozilla Foundation and Telefonica S.A.

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions test/glyph_mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ describe('character to glyph mapping', function() {
return assert.deepEqual(glyphs.map(g => g.codePoints), [[102, 102, 105], [32], [49], [8260], [50]]);
});

it('should allow for disabling of default AAT morx features', function() {
let {glyphs} = font.layout('ffi 1⁄2', { 'liga': false });
assert.equal(glyphs.length, 7);
assert.deepEqual(glyphs.map(g => g.id), [73, 73, 76, 3, 20, 645, 21]);
return assert.deepEqual(glyphs.map(g => g.codePoints), [[102], [102], [105], [32], [49], [8260], [50]]);
});

it('should apply user specified features', function() {
let {glyphs} = font.layout('ffi 1⁄2', [ 'numr' ]);
assert.equal(glyphs.length, 3);
Expand Down
Loading