Socket
Socket
Sign inDemoInstall

opentype.js

Package Overview
Dependencies
1
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.6.8 to 0.6.9

test/font.js

2

bower.json
{
"name": "opentype.js",
"version": "0.6.8",
"version": "0.6.9",
"main": "dist/opentype.js",

@@ -5,0 +5,0 @@ "keywords": [

@@ -351,3 +351,3 @@ /**

*/
opentype.Layout = function() {};
opentype.Layout = function(font, tableName) {};

@@ -371,2 +371,9 @@ /**

/**
* Get or create the Layout table (GSUB, GPOS etc).
* @param {boolean} create - Whether to create a new one.
* @return {Object} The GSUB or GPOS table.
*/
opentype.Layout.prototype.getTable = function(create) {};
/**
* Returns all scripts in the substitution table.

@@ -391,3 +398,3 @@ * @instance

* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} language - Use 'dlft' for default language
* @param {boolean} create - forces the creation of this langSysTable if it doesn't exist.

@@ -402,3 +409,3 @@ * @return {Object} An object with tag and script properties.

* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} language - Use 'dlft' for default language
* @param {string} feature - One of the codes listed at https://www.microsoft.com/typography/OTSPEC/featurelist.htm

@@ -411,12 +418,12 @@ * @param {boolean} create - forces the creation of the feature table if it doesn't exist.

/**
* Get the first lookup table of a given type for a script/language/feature.
* Get the lookup tables of a given type for a script/language/feature.
* @instance
* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} [script='DFLT']
* @param {string} [language='dlft']
* @param {string} feature - 4-letter feature code
* @param {number} lookupType - 1 to 8
* @param {boolean} create - forces the creation of the lookup table if it doesn't exist, with no subtables.
* @return {Object}
* @return {Object[]}
*/
opentype.Layout.prototype.getLookupTable = function(script, language, feature, lookupType, create) {};
opentype.Layout.prototype.getLookupTables = function(script, language, feature, lookupType, create) {};

@@ -441,7 +448,6 @@ /**

/**
* Get or create the GSUB table.
* @param {Boolean} create
* @return {Object}
* Create a default GSUB table.
* @return {Object} gsub - The GSUB table.
*/
opentype.Substitution.prototype.getGsubTable = function(create) {};
Substitution.prototype.createDefaultTable = function() {};

@@ -482,3 +488,3 @@ /**

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/

@@ -492,3 +498,3 @@ opentype.Substitution.prototype.addSingle = function(feature, substitution, script, language) {};

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/

@@ -503,3 +509,3 @@ opentype.Substitution.prototype.addAlternate = function(feature, substitution, script, language) {};

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/

@@ -512,3 +518,3 @@ opentype.Substitution.prototype.addLigature = function(feature, ligature, script, language) {};

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
* @return {[type]} [description]

@@ -524,3 +530,3 @@ * @return {Array} substitutions - The list of substitutions.

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/

@@ -527,0 +533,0 @@ opentype.Substitution.prototype.add = function(feature, sub, script, language) {};

{
"name": "opentype.js",
"description": "OpenType font parser",
"version": "0.6.8",
"version": "0.6.9",
"author": {

@@ -6,0 +6,0 @@ "name": "Frederik De Bleser",

@@ -37,2 +37,3 @@ opentype.js

* Support for kerning (Using GPOS or the kern table).
* Support for ligatures.
* Very efficient.

@@ -148,2 +149,4 @@ * Runs in the browser and node.js.

* `kerning`: if true takes kerning information into account (default: true)
* `features`: an object with [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) as keys, and a boolean value to enable each feature.
Currently only ligature features "liga" and "rlig" are supported (default: true).

@@ -257,3 +260,3 @@ _Note: there is also `Font.getPaths` with the same arguments which returns a list of Paths._

=======
* Support for ligatures and contextual alternates.
* Support for contextual alternates.

@@ -260,0 +263,0 @@ Thanks

@@ -0,1 +1,5 @@

0.6.9 (Jan 17, 2017)
====================
* Add ligature rendering (thanks @fpirsch!)
0.6.8 (Jan 9, 2017)

@@ -2,0 +6,0 @@ =========================

@@ -139,11 +139,43 @@ // The Font object

* @param {string}
* @param {GlyphRenderOptions} [options]
* @return {opentype.Glyph[]}
*/
Font.prototype.stringToGlyphs = function(s) {
var glyphs = [];
for (var i = 0; i < s.length; i += 1) {
Font.prototype.stringToGlyphs = function(s, options) {
options = options || this.defaultRenderOptions;
var i;
// Get glyph indexes
var indexes = [];
for (i = 0; i < s.length; i += 1) {
var c = s[i];
glyphs.push(this.charToGlyph(c));
indexes.push(this.charToGlyphIndex(c));
}
var length = indexes.length;
// Apply substitutions on glyph indexes
if (options.features) {
var script = options.script || this.substitution.getDefaultScriptName();
var manyToOne = [];
if (options.features.liga) manyToOne = manyToOne.concat(this.substitution.getFeature('liga', script, options.language));
if (options.features.rlig) manyToOne = manyToOne.concat(this.substitution.getFeature('rlig', script, options.language));
for (i = 0; i < length; i += 1) {
for (var j = 0; j < manyToOne.length; j++) {
var ligature = manyToOne[j];
var components = ligature.sub;
var compCount = components.length;
var k = 0;
while (k < compCount && components[k] === indexes[i + k]) k++;
if (k === compCount) {
indexes.splice(i, compCount, ligature.by);
length = length - compCount + 1;
}
}
}
}
// convert glyph indexes to glyph objects
var glyphs = new Array(length);
var notdef = this.glyphs.get(0);
for (i = 0; i < length; i += 1) {
glyphs[i] = this.glyphs.get(indexes[i]) || notdef;
}
return glyphs;

@@ -207,4 +239,17 @@ };

* @type Object
* @property {boolean} [kerning] - whether to include kerning values
* @property {string} [script] - script used to determine which features to apply. By default, 'DFLT' or 'latn' is used.
* See https://www.microsoft.com/typography/otspec/scripttags.htm
* @property {string} [language='dflt'] - language system used to determine which features to apply.
* See https://www.microsoft.com/typography/developers/opentype/languagetags.aspx
* @property {boolean} [kerning=true] - whether to include kerning values
* @property {object} [features] - OpenType Layout feature tags. Used to enable or disable the features of the given script/language system.
* See https://www.microsoft.com/typography/otspec/featuretags.htm
*/
Font.prototype.defaultRenderOptions = {
kerning: true,
features: {
liga: true,
rlig: true
}
};

@@ -225,6 +270,5 @@ /**

fontSize = fontSize !== undefined ? fontSize : 72;
options = options || {};
var kerning = options.kerning === undefined ? true : options.kerning;
options = options || this.defaultRenderOptions;
var fontScale = 1 / this.unitsPerEm * fontSize;
var glyphs = this.stringToGlyphs(text);
var glyphs = this.stringToGlyphs(text, options);
for (var i = 0; i < glyphs.length; i += 1) {

@@ -237,3 +281,3 @@ var glyph = glyphs[i];

if (kerning && i < glyphs.length - 1) {
if (options.kerning && i < glyphs.length - 1) {
var kerningValue = this.getKerningValue(glyph, glyphs[i + 1]);

@@ -240,0 +284,0 @@ x += kerningValue * fontScale;

@@ -46,4 +46,9 @@ // The Layout object is the prototype of Substition objects, and provides utility methods to manipulate

*/
var Layout = {
function Layout(font, tableName) {
this.font = font;
this.tableName = tableName;
}
Layout.prototype = {
/**

@@ -59,2 +64,3 @@ * Binary search an object by "tag" property

searchTag: searchTag,
/**

@@ -72,2 +78,15 @@ * Binary search in a list of numbers

/**
* Get or create the Layout table (GSUB, GPOS etc).
* @param {boolean} create - Whether to create a new one.
* @return {Object} The GSUB or GPOS table.
*/
getTable: function(create) {
var layout = this.font.tables[this.tableName];
if (!layout && create) {
layout = this.font.tables[this.tableName] = this.createDefaultTable();
}
return layout;
},
/**
* Returns all scripts in the substitution table.

@@ -78,5 +97,5 @@ * @instance

getScriptNames: function() {
var gsub = this.getGsubTable();
if (!gsub) { return []; }
return gsub.scripts.map(function(script) {
var layout = this.getTable();
if (!layout) { return []; }
return layout.scripts.map(function(script) {
return script.tag;

@@ -87,5 +106,23 @@ });

/**
* Returns the best bet for a script name.
* Returns 'DFLT' if it exists.
* If not, returns 'latn' if it exists.
* If neither exist, returns undefined.
*/
getDefaultScriptName: function() {
var layout = this.getTable();
if (!layout) { return; }
var hasLatn = false;
for (var i = 0; i < layout.scripts.length; i++) {
var name = layout.scripts[i].tag;
if (name === 'DFLT') return name;
if (name === 'latn') hasLatn = true;
}
if (hasLatn) return 'latn';
},
/**
* Returns all LangSysRecords in the given script.
* @instance
* @param {string} script - Use 'DFLT' for default script
* @param {string} [script='DFLT']
* @param {boolean} create - forces the creation of this script table if it doesn't exist.

@@ -95,9 +132,10 @@ * @return {Object} An object with tag and script properties.

getScriptTable: function(script, create) {
var gsub = this.getGsubTable(create);
if (gsub) {
var scripts = gsub.scripts;
var pos = searchTag(gsub.scripts, script);
var layout = this.getTable(create);
if (layout) {
script = script || 'DFLT';
var scripts = layout.scripts;
var pos = searchTag(layout.scripts, script);
if (pos >= 0) {
return scripts[pos].script;
} else {
} else if (create) {
var scr = {

@@ -110,4 +148,4 @@ tag: script,

};
scripts.splice(-1 - pos, 0, scr.script);
return scr;
scripts.splice(-1 - pos, 0, scr);
return scr.script;
}

@@ -120,4 +158,4 @@ }

* @instance
* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} [script='DFLT']
* @param {string} [language='dlft']
* @param {boolean} create - forces the creation of this langSysTable if it doesn't exist.

@@ -129,3 +167,3 @@ * @return {Object}

if (scriptTable) {
if (language === 'DFLT') {
if (!language || language === 'dflt' || language === 'DFLT') {
return scriptTable.defaultLangSys;

@@ -150,4 +188,4 @@ }

* @instance
* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} [script='DFLT']
* @param {string} [language='dlft']
* @param {string} feature - One of the codes listed at https://www.microsoft.com/typography/OTSPEC/featurelist.htm

@@ -162,3 +200,3 @@ * @param {boolean} create - forces the creation of the feature table if it doesn't exist.

var featIndexes = langSysTable.featureIndexes;
var allFeatures = this.font.tables.gsub.features;
var allFeatures = this.font.tables[this.tableName].features;
// The FeatureIndex array of indices is in arbitrary order,

@@ -188,17 +226,18 @@ // even if allFeatures is sorted alphabetically by feature tag.

/**
* Get the first lookup table of a given type for a script/language/feature.
* Get the lookup tables of a given type for a script/language/feature.
* @instance
* @param {string} script - Use 'DFLT' for default script
* @param {string} language - Use 'DFLT' for default language
* @param {string} [script='DFLT']
* @param {string} [language='dlft']
* @param {string} feature - 4-letter feature code
* @param {number} lookupType - 1 to 8
* @param {boolean} create - forces the creation of the lookup table if it doesn't exist, with no subtables.
* @return {Object}
* @return {Object[]}
*/
getLookupTable: function(script, language, feature, lookupType, create) {
getLookupTables: function(script, language, feature, lookupType, create) {
var featureTable = this.getFeatureTable(script, language, feature, create);
var tables = [];
if (featureTable) {
var lookupTable;
var lookupListIndexes = featureTable.lookupListIndexes;
var allLookups = this.font.tables.gsub.lookups;
var allLookups = this.font.tables[this.tableName].lookups;
// lookupListIndexes are in no particular order, so use naïve search.

@@ -208,6 +247,6 @@ for (var i = 0; i < lookupListIndexes.length; i++) {

if (lookupTable.lookupType === lookupType) {
return lookupTable;
tables.push(lookupTable);
}
}
if (create) {
if (tables.length === 0 && create) {
lookupTable = {

@@ -222,5 +261,6 @@ lookupType: lookupType,

lookupListIndexes.push(index);
return lookupTable;
return [lookupTable];
}
}
return tables;
},

@@ -227,0 +267,0 @@

@@ -17,3 +17,3 @@ // The Substitution object provides utility methods to manipulate

var Substitution = function(font) {
this.font = font;
Layout.call(this, font, 'gsub');
};

@@ -46,27 +46,22 @@

Substitution.prototype = Layout;
Substitution.prototype = Layout.prototype;
/**
* Get or create the GSUB table.
* @param {boolean} create - Whether to create a new one.
* Create a default GSUB table.
* @return {Object} gsub - The GSUB table.
*/
Substitution.prototype.getGsubTable = function(create) {
var gsub = this.font.tables.gsub;
if (!gsub && create) {
// Generate a default empty GSUB table with just a DFLT script and dflt lang sys.
this.font.tables.gsub = gsub = {
version: 1,
scripts: [{
tag: 'DFLT',
script: {
defaultLangSys: { reserved: 0, reqFeatureIndex: 0xffff, featureIndexes: [] },
langSysRecords: []
}
}],
features: [],
lookups: []
};
}
return gsub;
Substitution.prototype.createDefaultTable = function() {
// Generate a default empty GSUB table with just a DFLT script and dflt lang sys.
return {
version: 1,
scripts: [{
tag: 'DFLT',
script: {
defaultLangSys: { reserved: 0, reqFeatureIndex: 0xffff, featureIndexes: [] },
langSysRecords: []
}
}],
features: [],
lookups: []
};
};

@@ -76,4 +71,4 @@

* List all single substitutions (lookup type 1) for a given script, language, and feature.
* @param {string} script
* @param {string} language
* @param {string} [script='DFLT']
* @param {string} [language='dflt']
* @param {string} feature - 4-character feature name ('aalt', 'salt', 'ss01'...)

@@ -84,20 +79,21 @@ * @return {Array} substitutions - The list of substitutions.

var substitutions = [];
var lookupTable = this.getLookupTable(script, language, feature, 1);
if (!lookupTable) { return substitutions; }
var subtables = lookupTable.subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var j;
if (subtable.substFormat === 1) {
var delta = subtable.deltaGlyphId;
for (j = 0; j < glyphs.length; j++) {
var glyph = glyphs[j];
substitutions.push({ sub: glyph, by: glyph + delta });
var lookupTables = this.getLookupTables(script, language, feature, 1);
for (var idx = 0; idx < lookupTables.length; idx++) {
var subtables = lookupTables[idx].subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var j;
if (subtable.substFormat === 1) {
var delta = subtable.deltaGlyphId;
for (j = 0; j < glyphs.length; j++) {
var glyph = glyphs[j];
substitutions.push({ sub: glyph, by: glyph + delta });
}
} else {
var substitute = subtable.substitute;
for (j = 0; j < glyphs.length; j++) {
substitutions.push({ sub: glyphs[j], by: substitute[j] });
}
}
} else {
var substitute = subtable.substitute;
for (j = 0; j < glyphs.length; j++) {
substitutions.push({ sub: glyphs[j], by: substitute[j] });
}
}

@@ -110,4 +106,4 @@ }

* List all alternates (lookup type 3) for a given script, language, and feature.
* @param {string} script
* @param {string} language
* @param {string} [script='DFLT']
* @param {string} [language='dflt']
* @param {string} feature - 4-character feature name ('aalt', 'salt'...)

@@ -118,11 +114,12 @@ * @return {Array} alternates - The list of alternates

var alternates = [];
var lookupTable = this.getLookupTable(script, language, feature, 3);
if (!lookupTable) { return alternates; }
var subtables = lookupTable.subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var alternateSets = subtable.alternateSets;
for (var j = 0; j < glyphs.length; j++) {
alternates.push({ sub: glyphs[j], by: alternateSets[j] });
var lookupTables = this.getLookupTables(script, language, feature, 3);
for (var idx = 0; idx < lookupTables.length; idx++) {
var subtables = lookupTables[idx].subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var alternateSets = subtable.alternateSets;
for (var j = 0; j < glyphs.length; j++) {
alternates.push({ sub: glyphs[j], by: alternateSets[j] });
}
}

@@ -137,4 +134,4 @@ }

* @param {string} feature - 4-letter feature name ('liga', 'rlig', 'dlig'...)
* @param {string} script
* @param {string} language
* @param {string} [script='DFLT']
* @param {string} [language='dflt']
* @return {Array} ligatures - The list of ligatures.

@@ -144,18 +141,19 @@ */

var ligatures = [];
var lookupTable = this.getLookupTable(script, language, feature, 4);
if (!lookupTable) { return []; }
var subtables = lookupTable.subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var ligatureSets = subtable.ligatureSets;
for (var j = 0; j < glyphs.length; j++) {
var startGlyph = glyphs[j];
var ligSet = ligatureSets[j];
for (var k = 0; k < ligSet.length; k++) {
var lig = ligSet[k];
ligatures.push({
sub: [startGlyph].concat(lig.components),
by: lig.ligGlyph
});
var lookupTables = this.getLookupTables(script, language, feature, 4);
for (var idx = 0; idx < lookupTables.length; idx++) {
var subtables = lookupTables[idx].subtables;
for (var i = 0; i < subtables.length; i++) {
var subtable = subtables[i];
var glyphs = this.expandCoverage(subtable.coverage);
var ligatureSets = subtable.ligatureSets;
for (var j = 0; j < glyphs.length; j++) {
var startGlyph = glyphs[j];
var ligSet = ligatureSets[j];
for (var k = 0; k < ligSet.length; k++) {
var lig = ligSet[k];
ligatures.push({
sub: [startGlyph].concat(lig.components),
by: lig.ligGlyph
});
}
}

@@ -173,6 +171,6 @@ }

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/
Substitution.prototype.addSingle = function(feature, substitution, script, language) {
var lookupTable = this.getLookupTable(script, language, feature, 1, true);
var lookupTable = this.getLookupTables(script, language, feature, 1, true)[0];
var subtable = getSubstFormat(lookupTable, 2, { // lookup type 1 subtable, format 2, coverage format 1

@@ -199,6 +197,6 @@ substFormat: 2,

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/
Substitution.prototype.addAlternate = function(feature, substitution, script, language) {
var lookupTable = this.getLookupTable(script, language, feature, 3, true);
var lookupTable = this.getLookupTables(script, language, feature, 3, true)[0];
var subtable = getSubstFormat(lookupTable, 1, { // lookup type 3 subtable, format 1, coverage format 1

@@ -226,8 +224,6 @@ substFormat: 1,

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/
Substitution.prototype.addLigature = function(feature, ligature, script, language) {
script = script || 'DFLT';
language = language || 'DFLT';
var lookupTable = this.getLookupTable(script, language, feature, 4, true);
var lookupTable = this.getLookupTables(script, language, feature, 4, true)[0];
var subtable = lookupTable.subtables[0];

@@ -273,8 +269,6 @@ if (!subtable) {

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
* @return {Array} substitutions - The list of substitutions.
*/
Substitution.prototype.getFeature = function(feature, script, language) {
script = script || 'DFLT';
language = language || 'DFLT';
if (/ss\d\d/.test(feature)) { // ss01 - ss20

@@ -299,7 +293,5 @@ return this.getSingle(feature, script, language);

* @param {string} [script='DFLT']
* @param {string} [language='DFLT']
* @param {string} [language='dflt']
*/
Substitution.prototype.add = function(feature, sub, script, language) {
script = script || 'DFLT';
language = language || 'DFLT';
if (/ss\d\d/.test(feature)) { // ss01 - ss20

@@ -306,0 +298,0 @@ return this.addSingle(feature, sub, script, language);

@@ -196,3 +196,4 @@ // The `GPOS` table contains kerning pairs, among other things.

for (var i = 0; i < subTableCount; i++) {
subtables.push(parsePairPosSubTable(data, start + subTableOffsets[i]));
var pairPosSubTable = parsePairPosSubTable(data, start + subTableOffsets[i]);
if (pairPosSubTable) subtables.push(pairPosSubTable);
}

@@ -199,0 +200,0 @@ // Return a function which finds the kerning values in the subtables.

@@ -47,10 +47,5 @@ /* jshint mocha: true */

describe('getGsubTable', function() {
it('must not create an empty default GSUB table', function() {
assert.equal(substitution.getGsubTable(), undefined);
assert.equal(substitution.getGsubTable(false), undefined);
});
it('can create an empty default GSUB table', function() {
assert.deepEqual(substitution.getGsubTable(true), {
describe('createDefaultTable', function() {
it('must return an empty default GSUB table', function() {
assert.deepEqual(substitution.createDefaultTable(), {
version: 1,

@@ -57,0 +52,0 @@ scripts: [{

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc