@shuding/opentype.js
Advanced tools
Comparing version 1.3.4 to 1.3.5
{ | ||
"name": "@shuding/opentype.js", | ||
"description": "OpenType font parser", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"author": { | ||
@@ -69,3 +69,6 @@ "name": "Frederik De Bleser", | ||
"string.prototype.codepointat": "^0.2.1" | ||
}, | ||
"prettier": { | ||
"singleQuote": true | ||
} | ||
} |
@@ -1,4 +0,7 @@ | ||
# opentype.js | ||
An [opentype.js](https://github.com/opentypejs/opentype.js) fork which replaces `tiny-inflate` with `fflate` for better performance. | ||
An [opentype.js](https://github.com/opentypejs/opentype.js) fork that: | ||
- Replaces `tiny-inflate` with `fflate` for better performance. | ||
- Removes dynamic font loading and `fs`, `Buffer` deps. | ||
Published as `@shuding/opentype.js` on [npm](https://www.npmjs.com/package/@shuding/opentype.js). |
290
src/font.js
@@ -9,3 +9,3 @@ // The Font object | ||
import Substitution from './substitution'; | ||
import { isBrowser, checkArgument, arrayBufferToNodeBuffer } from './util'; | ||
import { checkArgument } from './util'; | ||
import HintingTrueType from './hintingtt'; | ||
@@ -56,25 +56,48 @@ import Bidi from './bidi'; | ||
// Check that we've provided the minimum set of names. | ||
checkArgument(options.familyName, 'When creating a new Font object, familyName is required.'); | ||
checkArgument(options.styleName, 'When creating a new Font object, styleName is required.'); | ||
checkArgument(options.unitsPerEm, 'When creating a new Font object, unitsPerEm is required.'); | ||
checkArgument(options.ascender, 'When creating a new Font object, ascender is required.'); | ||
checkArgument(options.descender <= 0, 'When creating a new Font object, negative descender value is required.'); | ||
checkArgument( | ||
options.familyName, | ||
'When creating a new Font object, familyName is required.' | ||
); | ||
checkArgument( | ||
options.styleName, | ||
'When creating a new Font object, styleName is required.' | ||
); | ||
checkArgument( | ||
options.unitsPerEm, | ||
'When creating a new Font object, unitsPerEm is required.' | ||
); | ||
checkArgument( | ||
options.ascender, | ||
'When creating a new Font object, ascender is required.' | ||
); | ||
checkArgument( | ||
options.descender <= 0, | ||
'When creating a new Font object, negative descender value is required.' | ||
); | ||
// OS X will complain if the names are empty, so we put a single space everywhere by default. | ||
this.names = { | ||
fontFamily: {en: options.familyName || ' '}, | ||
fontSubfamily: {en: options.styleName || ' '}, | ||
fullName: {en: options.fullName || options.familyName + ' ' + options.styleName}, | ||
fontFamily: { en: options.familyName || ' ' }, | ||
fontSubfamily: { en: options.styleName || ' ' }, | ||
fullName: { | ||
en: | ||
options.fullName || | ||
options.familyName + ' ' + options.styleName, | ||
}, | ||
// postScriptName may not contain any whitespace | ||
postScriptName: {en: options.postScriptName || (options.familyName + options.styleName).replace(/\s/g, '')}, | ||
designer: {en: options.designer || ' '}, | ||
designerURL: {en: options.designerURL || ' '}, | ||
manufacturer: {en: options.manufacturer || ' '}, | ||
manufacturerURL: {en: options.manufacturerURL || ' '}, | ||
license: {en: options.license || ' '}, | ||
licenseURL: {en: options.licenseURL || ' '}, | ||
version: {en: options.version || 'Version 0.1'}, | ||
description: {en: options.description || ' '}, | ||
copyright: {en: options.copyright || ' '}, | ||
trademark: {en: options.trademark || ' '} | ||
postScriptName: { | ||
en: | ||
options.postScriptName || | ||
(options.familyName + options.styleName).replace(/\s/g, ''), | ||
}, | ||
designer: { en: options.designer || ' ' }, | ||
designerURL: { en: options.designerURL || ' ' }, | ||
manufacturer: { en: options.manufacturer || ' ' }, | ||
manufacturerURL: { en: options.manufacturerURL || ' ' }, | ||
license: { en: options.license || ' ' }, | ||
licenseURL: { en: options.licenseURL || ' ' }, | ||
version: { en: options.version || 'Version 0.1' }, | ||
description: { en: options.description || ' ' }, | ||
copyright: { en: options.copyright || ' ' }, | ||
trademark: { en: options.trademark || ' ' }, | ||
}; | ||
@@ -86,7 +109,13 @@ this.unitsPerEm = options.unitsPerEm || 1000; | ||
this.tables = Object.assign(options.tables, { | ||
os2: Object.assign({ | ||
usWeightClass: options.weightClass || this.usWeightClasses.MEDIUM, | ||
usWidthClass: options.widthClass || this.usWidthClasses.MEDIUM, | ||
fsSelection: options.fsSelection || this.fsSelectionValues.REGULAR, | ||
}, options.tables.os2) | ||
os2: Object.assign( | ||
{ | ||
usWeightClass: | ||
options.weightClass || this.usWeightClasses.MEDIUM, | ||
usWidthClass: | ||
options.widthClass || this.usWidthClasses.MEDIUM, | ||
fsSelection: | ||
options.fsSelection || this.fsSelectionValues.REGULAR, | ||
}, | ||
options.tables.os2 | ||
), | ||
}); | ||
@@ -107,3 +136,3 @@ } | ||
Object.defineProperty(this, 'hinting', { | ||
get: function() { | ||
get: function () { | ||
if (this._hinting) return this._hinting; | ||
@@ -113,3 +142,3 @@ if (this.outlinesFormat === 'truetype') { | ||
} | ||
} | ||
}, | ||
}); | ||
@@ -123,3 +152,3 @@ } | ||
*/ | ||
Font.prototype.hasChar = function(c) { | ||
Font.prototype.hasChar = function (c) { | ||
return this.encoding.charToGlyphIndex(c) !== null; | ||
@@ -135,3 +164,3 @@ }; | ||
*/ | ||
Font.prototype.charToGlyphIndex = function(s) { | ||
Font.prototype.charToGlyphIndex = function (s) { | ||
return this.encoding.charToGlyphIndex(s); | ||
@@ -147,3 +176,3 @@ }; | ||
*/ | ||
Font.prototype.charToGlyph = function(c) { | ||
Font.prototype.charToGlyph = function (c) { | ||
const glyphIndex = this.charToGlyphIndex(c); | ||
@@ -165,7 +194,7 @@ let glyph = this.glyphs.get(glyphIndex); | ||
// TODO: update all features options not only 'latn'. | ||
return this.defaultRenderOptions.features.map(feature => { | ||
return this.defaultRenderOptions.features.map((feature) => { | ||
if (feature.script === 'latn') { | ||
return { | ||
script: 'latn', | ||
tags: feature.tags.filter(tag => options[tag]) | ||
tags: feature.tags.filter((tag) => options[tag]), | ||
}; | ||
@@ -187,8 +216,7 @@ } else { | ||
*/ | ||
Font.prototype.stringToGlyphs = function(s, options) { | ||
Font.prototype.stringToGlyphs = function (s, options) { | ||
const bidi = new Bidi(); | ||
// Create and register 'glyphIndex' state modifier | ||
const charToGlyphIndexMod = token => this.charToGlyphIndex(token.char); | ||
const charToGlyphIndexMod = (token) => this.charToGlyphIndex(token.char); | ||
bidi.registerModifier('glyphIndex', null, charToGlyphIndexMod); | ||
@@ -198,4 +226,4 @@ | ||
let features = options ? | ||
this.updateFeatures(options.features) : | ||
this.defaultRenderOptions.features; | ||
this.updateFeatures(options.features) : | ||
this.defaultRenderOptions.features; | ||
@@ -221,3 +249,3 @@ bidi.applyFeatures(this, features); | ||
*/ | ||
Font.prototype.nameToGlyphIndex = function(name) { | ||
Font.prototype.nameToGlyphIndex = function (name) { | ||
return this.glyphNames.nameToGlyphIndex(name); | ||
@@ -230,3 +258,3 @@ }; | ||
*/ | ||
Font.prototype.nameToGlyph = function(name) { | ||
Font.prototype.nameToGlyph = function (name) { | ||
const glyphIndex = this.nameToGlyphIndex(name); | ||
@@ -246,3 +274,3 @@ let glyph = this.glyphs.get(glyphIndex); | ||
*/ | ||
Font.prototype.glyphIndexToName = function(gid) { | ||
Font.prototype.glyphIndexToName = function (gid) { | ||
if (!this.glyphNames.glyphIndexToName) { | ||
@@ -266,3 +294,3 @@ return ''; | ||
*/ | ||
Font.prototype.getKerningValue = function(leftGlyph, rightGlyph) { | ||
Font.prototype.getKerningValue = function (leftGlyph, rightGlyph) { | ||
leftGlyph = leftGlyph.index || leftGlyph; | ||
@@ -272,3 +300,7 @@ rightGlyph = rightGlyph.index || rightGlyph; | ||
if (gposKerning) { | ||
return this.position.getKerningValue(gposKerning, leftGlyph, rightGlyph); | ||
return this.position.getKerningValue( | ||
gposKerning, | ||
leftGlyph, | ||
rightGlyph | ||
); | ||
} | ||
@@ -298,4 +330,4 @@ // "kern" table | ||
{ script: 'arab', tags: ['init', 'medi', 'fina', 'rlig'] }, | ||
{ script: 'latn', tags: ['liga', 'rlig'] } | ||
] | ||
{ script: 'latn', tags: ['liga', 'rlig'] }, | ||
], | ||
}; | ||
@@ -313,3 +345,10 @@ | ||
*/ | ||
Font.prototype.forEachGlyph = function(text, x, y, fontSize, options, callback) { | ||
Font.prototype.forEachGlyph = function ( | ||
text, | ||
x, | ||
y, | ||
fontSize, | ||
options, | ||
callback | ||
) { | ||
x = x !== undefined ? x : 0; | ||
@@ -319,3 +358,3 @@ y = y !== undefined ? y : 0; | ||
options = Object.assign({}, this.defaultRenderOptions, options); | ||
const fontScale = 1 / this.unitsPerEm * fontSize; | ||
const fontScale = (1 / this.unitsPerEm) * fontSize; | ||
const glyphs = this.stringToGlyphs(text, options); | ||
@@ -325,3 +364,6 @@ let kerningLookups; | ||
const script = options.script || this.position.getDefaultScriptName(); | ||
kerningLookups = this.position.getKerningTables(script, options.language); | ||
kerningLookups = this.position.getKerningTables( | ||
script, | ||
options.language | ||
); | ||
} | ||
@@ -339,4 +381,8 @@ for (let i = 0; i < glyphs.length; i += 1) { | ||
const kerningValue = kerningLookups ? | ||
this.position.getKerningValue(kerningLookups, glyph.index, glyphs[i + 1].index) : | ||
this.getKerningValue(glyph, glyphs[i + 1]); | ||
this.position.getKerningValue( | ||
kerningLookups, | ||
glyph.index, | ||
glyphs[i + 1].index | ||
) : | ||
this.getKerningValue(glyph, glyphs[i + 1]); | ||
x += kerningValue * fontScale; | ||
@@ -363,8 +409,15 @@ } | ||
*/ | ||
Font.prototype.getPath = function(text, x, y, fontSize, options) { | ||
Font.prototype.getPath = function (text, x, y, fontSize, options) { | ||
const fullPath = new Path(); | ||
this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) { | ||
const glyphPath = glyph.getPath(gX, gY, gFontSize, options, this); | ||
fullPath.extend(glyphPath); | ||
}); | ||
this.forEachGlyph( | ||
text, | ||
x, | ||
y, | ||
fontSize, | ||
options, | ||
function (glyph, gX, gY, gFontSize) { | ||
const glyphPath = glyph.getPath(gX, gY, gFontSize, options, this); | ||
fullPath.extend(glyphPath); | ||
} | ||
); | ||
return fullPath; | ||
@@ -382,8 +435,15 @@ }; | ||
*/ | ||
Font.prototype.getPaths = function(text, x, y, fontSize, options) { | ||
Font.prototype.getPaths = function (text, x, y, fontSize, options) { | ||
const glyphPaths = []; | ||
this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) { | ||
const glyphPath = glyph.getPath(gX, gY, gFontSize, options, this); | ||
glyphPaths.push(glyphPath); | ||
}); | ||
this.forEachGlyph( | ||
text, | ||
x, | ||
y, | ||
fontSize, | ||
options, | ||
function (glyph, gX, gY, gFontSize) { | ||
const glyphPath = glyph.getPath(gX, gY, gFontSize, options, this); | ||
glyphPaths.push(glyphPath); | ||
} | ||
); | ||
@@ -408,4 +468,4 @@ return glyphPaths; | ||
*/ | ||
Font.prototype.getAdvanceWidth = function(text, fontSize, options) { | ||
return this.forEachGlyph(text, 0, 0, fontSize, options, function() {}); | ||
Font.prototype.getAdvanceWidth = function (text, fontSize, options) { | ||
return this.forEachGlyph(text, 0, 0, fontSize, options, function () {}); | ||
}; | ||
@@ -422,3 +482,3 @@ | ||
*/ | ||
Font.prototype.draw = function(ctx, text, x, y, fontSize, options) { | ||
Font.prototype.draw = function (ctx, text, x, y, fontSize, options) { | ||
this.getPath(text, x, y, fontSize, options).draw(ctx); | ||
@@ -437,6 +497,13 @@ }; | ||
*/ | ||
Font.prototype.drawPoints = function(ctx, text, x, y, fontSize, options) { | ||
this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) { | ||
glyph.drawPoints(ctx, gX, gY, gFontSize); | ||
}); | ||
Font.prototype.drawPoints = function (ctx, text, x, y, fontSize, options) { | ||
this.forEachGlyph( | ||
text, | ||
x, | ||
y, | ||
fontSize, | ||
options, | ||
function (glyph, gX, gY, gFontSize) { | ||
glyph.drawPoints(ctx, gX, gY, gFontSize); | ||
} | ||
); | ||
}; | ||
@@ -456,6 +523,13 @@ | ||
*/ | ||
Font.prototype.drawMetrics = function(ctx, text, x, y, fontSize, options) { | ||
this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) { | ||
glyph.drawMetrics(ctx, gX, gY, gFontSize); | ||
}); | ||
Font.prototype.drawMetrics = function (ctx, text, x, y, fontSize, options) { | ||
this.forEachGlyph( | ||
text, | ||
x, | ||
y, | ||
fontSize, | ||
options, | ||
function (glyph, gX, gY, gFontSize) { | ||
glyph.drawMetrics(ctx, gX, gY, gFontSize); | ||
} | ||
); | ||
}; | ||
@@ -467,3 +541,3 @@ | ||
*/ | ||
Font.prototype.getEnglishName = function(name) { | ||
Font.prototype.getEnglishName = function (name) { | ||
const translations = this.names[name]; | ||
@@ -478,3 +552,3 @@ if (translations) { | ||
*/ | ||
Font.prototype.validate = function() { | ||
Font.prototype.validate = function () { | ||
const warnings = []; | ||
@@ -491,4 +565,6 @@ const _this = this; | ||
const englishName = _this.getEnglishName(name); | ||
assert(englishName && englishName.trim().length > 0, | ||
'No English ' + name + ' specified.'); | ||
assert( | ||
englishName && englishName.trim().length > 0, | ||
'No English ' + name + ' specified.' | ||
); | ||
} | ||
@@ -512,3 +588,3 @@ | ||
*/ | ||
Font.prototype.toTables = function() { | ||
Font.prototype.toTables = function () { | ||
return sfnt.fontToTable(this); | ||
@@ -519,4 +595,6 @@ }; | ||
*/ | ||
Font.prototype.toBuffer = function() { | ||
console.warn('Font.toBuffer is deprecated. Use Font.toArrayBuffer instead.'); | ||
Font.prototype.toBuffer = function () { | ||
console.warn( | ||
'Font.toBuffer is deprecated. Use Font.toArrayBuffer instead.' | ||
); | ||
return this.toArrayBuffer(); | ||
@@ -528,3 +606,3 @@ }; | ||
*/ | ||
Font.prototype.toArrayBuffer = function() { | ||
Font.prototype.toArrayBuffer = function () { | ||
const sfntTable = this.toTables(); | ||
@@ -542,47 +620,15 @@ const bytes = sfntTable.encode(); | ||
/** | ||
* Initiate a download of the OpenType font. | ||
*/ | ||
Font.prototype.download = function(fileName) { | ||
const familyName = this.getEnglishName('fontFamily'); | ||
const styleName = this.getEnglishName('fontSubfamily'); | ||
fileName = fileName || familyName.replace(/\s/g, '') + '-' + styleName + '.otf'; | ||
const arrayBuffer = this.toArrayBuffer(); | ||
if (isBrowser()) { | ||
window.URL = window.URL || window.webkitURL; | ||
if (window.URL) { | ||
const dataView = new DataView(arrayBuffer); | ||
const blob = new Blob([dataView], {type: 'font/opentype'}); | ||
let link = document.createElement('a'); | ||
link.href = window.URL.createObjectURL(blob); | ||
link.download = fileName; | ||
let event = document.createEvent('MouseEvents'); | ||
event.initEvent('click', true, false); | ||
link.dispatchEvent(event); | ||
} else { | ||
console.warn('Font file could not be downloaded. Try using a different browser.'); | ||
} | ||
} else { | ||
const fs = require('fs'); | ||
const buffer = arrayBufferToNodeBuffer(arrayBuffer); | ||
fs.writeFileSync(fileName, buffer); | ||
} | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
Font.prototype.fsSelectionValues = { | ||
ITALIC: 0x001, //1 | ||
UNDERSCORE: 0x002, //2 | ||
NEGATIVE: 0x004, //4 | ||
OUTLINED: 0x008, //8 | ||
STRIKEOUT: 0x010, //16 | ||
BOLD: 0x020, //32 | ||
REGULAR: 0x040, //64 | ||
USER_TYPO_METRICS: 0x080, //128 | ||
WWS: 0x100, //256 | ||
OBLIQUE: 0x200 //512 | ||
ITALIC: 0x001, //1 | ||
UNDERSCORE: 0x002, //2 | ||
NEGATIVE: 0x004, //4 | ||
OUTLINED: 0x008, //8 | ||
STRIKEOUT: 0x010, //16 | ||
BOLD: 0x020, //32 | ||
REGULAR: 0x040, //64 | ||
USER_TYPO_METRICS: 0x080, //128 | ||
WWS: 0x100, //256 | ||
OBLIQUE: 0x200, //512 | ||
}; | ||
@@ -602,3 +648,3 @@ | ||
EXTRA_EXPANDED: 8, | ||
ULTRA_EXPANDED: 9 | ||
ULTRA_EXPANDED: 9, | ||
}; | ||
@@ -618,5 +664,5 @@ | ||
EXTRA_BOLD: 800, | ||
BLACK: 900 | ||
BLACK: 900, | ||
}; | ||
export default Font; |
@@ -6,6 +6,6 @@ // opentype.js | ||
/* global DataView, Uint8Array, XMLHttpRequest */ | ||
/* global DataView, Uint8Array */ | ||
import 'string.prototype.codepointat'; | ||
import {inflateSync} from 'fflate'; | ||
import { inflateSync } from 'fflate'; | ||
import Font from './font'; | ||
@@ -17,3 +17,2 @@ import Glyph from './glyph'; | ||
import Path from './path'; | ||
import { nodeBufferToArrayBuffer } from './util'; | ||
import cmap from './tables/cmap'; | ||
@@ -43,44 +42,2 @@ import cff from './tables/cff'; | ||
// File loaders ///////////////////////////////////////////////////////// | ||
/** | ||
* Loads a font from a file. The callback throws an error message as the first parameter if it fails | ||
* and the font as an ArrayBuffer in the second parameter if it succeeds. | ||
* @param {string} path - The path of the file | ||
* @param {Function} callback - The function to call when the font load completes | ||
*/ | ||
function loadFromFile(path, callback) { | ||
const fs = require('fs'); | ||
fs.readFile(path, function(err, buffer) { | ||
if (err) { | ||
return callback(err.message); | ||
} | ||
callback(null, nodeBufferToArrayBuffer(buffer)); | ||
}); | ||
} | ||
/** | ||
* Loads a font from a URL. The callback throws an error message as the first parameter if it fails | ||
* and the font as an ArrayBuffer in the second parameter if it succeeds. | ||
* @param {string} url - The URL of the font file. | ||
* @param {Function} callback - The function to call when the font load completes | ||
*/ | ||
function loadFromUrl(url, callback) { | ||
const request = new XMLHttpRequest(); | ||
request.open('get', url, true); | ||
request.responseType = 'arraybuffer'; | ||
request.onload = function() { | ||
if (request.response) { | ||
return callback(null, request.response); | ||
} else { | ||
return callback('Font could not be loaded: ' + request.statusText); | ||
} | ||
}; | ||
request.onerror = function () { | ||
callback('Font could not be loaded'); | ||
}; | ||
request.send(); | ||
} | ||
// Table Directory Entries ////////////////////////////////////////////// | ||
@@ -101,3 +58,9 @@ /** | ||
const length = parse.getULong(data, p + 12); | ||
tableEntries.push({tag: tag, checksum: checksum, offset: offset, length: length, compression: false}); | ||
tableEntries.push({ | ||
tag: tag, | ||
checksum: checksum, | ||
offset: offset, | ||
length: length, | ||
compression: false, | ||
}); | ||
p += 16; | ||
@@ -130,4 +93,9 @@ } | ||
tableEntries.push({tag: tag, offset: offset, compression: compression, | ||
compressedLength: compLength, length: origLength}); | ||
tableEntries.push({ | ||
tag: tag, | ||
offset: offset, | ||
compression: compression, | ||
compressedLength: compLength, | ||
length: origLength, | ||
}); | ||
p += 20; | ||
@@ -153,13 +121,21 @@ } | ||
if (tableEntry.compression === 'WOFF') { | ||
const inBuffer = new Uint8Array(data.buffer, tableEntry.offset + 2, tableEntry.compressedLength - 2); | ||
const inBuffer = new Uint8Array( | ||
data.buffer, | ||
tableEntry.offset + 2, | ||
tableEntry.compressedLength - 2 | ||
); | ||
const outBuffer = new Uint8Array(tableEntry.length); | ||
inflateSync(inBuffer, outBuffer); | ||
if (outBuffer.byteLength !== tableEntry.length) { | ||
throw new Error('Decompression error: ' + tableEntry.tag + ' decompressed length doesn\'t match recorded length'); | ||
throw new Error( | ||
'Decompression error: ' + | ||
tableEntry.tag + | ||
` decompressed length doesn't match recorded length` | ||
); | ||
} | ||
const view = new DataView(outBuffer.buffer, 0); | ||
return {data: view, offset: 0}; | ||
return { data: view, offset: 0 }; | ||
} else { | ||
return {data: data, offset: tableEntry.offset}; | ||
return { data: data, offset: tableEntry.offset }; | ||
} | ||
@@ -178,3 +154,3 @@ } | ||
function parseBuffer(buffer, opt) { | ||
opt = (opt === undefined || opt === null) ? {} : opt; | ||
opt = opt === undefined || opt === null ? {} : opt; | ||
@@ -186,3 +162,3 @@ let indexToLocFormat; | ||
// should be an empty font that we'll fill with our own data. | ||
const font = new Font({empty: true}); | ||
const font = new Font({ empty: true }); | ||
@@ -196,3 +172,7 @@ // OpenType fonts use big endian byte ordering. | ||
const signature = parse.getTag(data, 0); | ||
if (signature === String.fromCharCode(0, 1, 0, 0) || signature === 'true' || signature === 'typ1') { | ||
if ( | ||
signature === String.fromCharCode(0, 1, 0, 0) || | ||
signature === 'true' || | ||
signature === 'typ1' | ||
) { | ||
font.outlinesFormat = 'truetype'; | ||
@@ -243,3 +223,3 @@ numTables = parse.getUShort(data, 4); | ||
break; | ||
case 'cvt ' : | ||
case 'cvt ': | ||
table = uncompressTable(data, tableEntry); | ||
@@ -252,3 +232,3 @@ p = new parse.Parser(table.data, table.offset); | ||
break; | ||
case 'fpgm' : | ||
case 'fpgm': | ||
table = uncompressTable(data, tableEntry); | ||
@@ -295,3 +275,3 @@ p = new parse.Parser(table.data, table.offset); | ||
break; | ||
case 'prep' : | ||
case 'prep': | ||
table = uncompressTable(data, tableEntry); | ||
@@ -335,5 +315,16 @@ p = new parse.Parser(table.data, table.offset); | ||
const locaTable = uncompressTable(data, locaTableEntry); | ||
const locaOffsets = loca.parse(locaTable.data, locaTable.offset, font.numGlyphs, shortVersion); | ||
const locaOffsets = loca.parse( | ||
locaTable.data, | ||
locaTable.offset, | ||
font.numGlyphs, | ||
shortVersion | ||
); | ||
const glyfTable = uncompressTable(data, glyfTableEntry); | ||
font.glyphs = glyf.parse(glyfTable.data, glyfTable.offset, locaOffsets, font, opt); | ||
font.glyphs = glyf.parse( | ||
glyfTable.data, | ||
glyfTable.offset, | ||
locaOffsets, | ||
font, | ||
opt | ||
); | ||
} else if (cffTableEntry) { | ||
@@ -343,7 +334,15 @@ const cffTable = uncompressTable(data, cffTableEntry); | ||
} else { | ||
throw new Error('Font doesn\'t contain TrueType or CFF outlines.'); | ||
throw new Error(`Font doesn't contain TrueType or CFF outlines.`); | ||
} | ||
const hmtxTable = uncompressTable(data, hmtxTableEntry); | ||
hmtx.parse(font, hmtxTable.data, hmtxTable.offset, font.numberOfHMetrics, font.numGlyphs, font.glyphs, opt); | ||
hmtx.parse( | ||
font, | ||
hmtxTable.data, | ||
hmtxTable.offset, | ||
font.numberOfHMetrics, | ||
font.numGlyphs, | ||
font.glyphs, | ||
opt | ||
); | ||
addGlyphNames(font, opt); | ||
@@ -376,3 +375,7 @@ | ||
const fvarTable = uncompressTable(data, fvarTableEntry); | ||
font.tables.fvar = fvar.parse(fvarTable.data, fvarTable.offset, font.names); | ||
font.tables.fvar = fvar.parse( | ||
fvarTable.data, | ||
fvarTable.offset, | ||
font.names | ||
); | ||
} | ||
@@ -389,59 +392,5 @@ | ||
/** | ||
* Asynchronously load the font from a URL or a filesystem. When done, call the callback | ||
* with two arguments `(err, font)`. The `err` will be null on success, | ||
* the `font` is a Font object. | ||
* We use the node.js callback convention so that | ||
* opentype.js can integrate with frameworks like async.js. | ||
* @alias opentype.load | ||
* @param {string} url - The URL of the font to load. | ||
* @param {Function} callback - The callback. | ||
*/ | ||
function load(url, callback, opt) { | ||
opt = (opt === undefined || opt === null) ? {} : opt; | ||
const isNode = typeof window === 'undefined'; | ||
const loadFn = isNode && !opt.isUrl ? loadFromFile : loadFromUrl; | ||
function load() {} | ||
function loadSync() {} | ||
return new Promise((resolve, reject) => { | ||
loadFn(url, function(err, arrayBuffer) { | ||
if (err) { | ||
if (callback) { | ||
return callback(err); | ||
} else { | ||
reject(err); | ||
} | ||
} | ||
let font; | ||
try { | ||
font = parseBuffer(arrayBuffer, opt); | ||
} catch (e) { | ||
if (callback) { | ||
return callback(e, null); | ||
} else { | ||
reject(e); | ||
} | ||
} | ||
if (callback) { | ||
return callback(null, font); | ||
} else { | ||
resolve(font); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Synchronously load the font from a URL or file. | ||
* When done, returns the font object or throws an error. | ||
* @alias opentype.loadSync | ||
* @param {string} url - The URL of the font to load. | ||
* @param {Object} opt - opt.lowMemory | ||
* @return {opentype.Font} | ||
*/ | ||
function loadSync(url, opt) { | ||
const fs = require('fs'); | ||
const buffer = fs.readFileSync(url); | ||
return parseBuffer(nodeBufferToArrayBuffer(buffer), opt); | ||
} | ||
export { | ||
@@ -455,3 +404,3 @@ Font, | ||
load, | ||
loadSync | ||
loadSync, | ||
}; |
@@ -1,9 +0,1 @@ | ||
function isBrowser() { | ||
return typeof window !== 'undefined'; | ||
} | ||
function isNode() { | ||
return typeof window === 'undefined'; | ||
} | ||
function nodeBufferToArrayBuffer(buffer) { | ||
@@ -19,12 +11,2 @@ const ab = new ArrayBuffer(buffer.length); | ||
function arrayBufferToNodeBuffer(ab) { | ||
const buffer = new Buffer(ab.byteLength); | ||
const view = new Uint8Array(ab); | ||
for (let i = 0; i < buffer.length; ++i) { | ||
buffer[i] = view[i]; | ||
} | ||
return buffer; | ||
} | ||
function checkArgument(expression, message) { | ||
@@ -36,2 +18,2 @@ if (!expression) { | ||
export { isBrowser, isNode, nodeBufferToArrayBuffer, arrayBufferToNodeBuffer, checkArgument }; | ||
export { nodeBufferToArrayBuffer, checkArgument }; |
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 too big to display
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 not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
8
1
3907806
38819