dnm-font-manager
Advanced tools
Comparing version 0.4.7 to 0.4.8
298
dist/main.js
@@ -15,2 +15,6 @@ 'use strict'; | ||
var _os = require('os'); | ||
var _os2 = _interopRequireDefault(_os); | ||
var _path = require('path'); | ||
@@ -26,2 +30,10 @@ | ||
var _readChunk = require('read-chunk'); | ||
var _readChunk2 = _interopRequireDefault(_readChunk); | ||
var _fileType = require('file-type'); | ||
var _fileType2 = _interopRequireDefault(_fileType); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -42,3 +54,2 @@ | ||
} catch (e) { | ||
// console.error(e); | ||
return []; | ||
@@ -52,4 +63,6 @@ } | ||
console.error(e); | ||
return []; | ||
} | ||
return files.reduce(function (arr, f) { | ||
if (f.toLowerCase() === 'deleted') return []; | ||
return arr.concat(recGetFile(_path2.default.join(target, f))); | ||
@@ -61,2 +74,15 @@ }, []); | ||
return [target]; | ||
} else if (ext === '') { | ||
// NOTE: Check files without extension, TypeKit on Windows does that. | ||
var fontFileHeader = _readChunk2.default.sync(target, 0, _fileType2.default.minimumBytes); | ||
var fileType = (0, _fileType2.default)(fontFileHeader); | ||
if (!fileType) { | ||
return []; | ||
} | ||
if (fileType.ext === 'ttf' || fileType.ext === 'otf' || fileType.ext === 'ttc') { | ||
return [target]; | ||
} | ||
return []; | ||
} else { | ||
@@ -71,7 +97,21 @@ return []; | ||
var extension = _path2.default.extname(f).toLowerCase(); | ||
return extension === '.ttf' || extension === '.otf'; | ||
if (extension === '.ttf' || extension === '.otf' || extension === '.ttc') { | ||
return true; | ||
} | ||
var fontFileHeader = _readChunk2.default.sync(f, 0, _fileType2.default.minimumBytes); | ||
var fileType = (0, _fileType2.default)(fontFileHeader); | ||
if (!fileType) { | ||
return false; | ||
} | ||
if (fileType.ext === 'ttf' || fileType.ext === 'otf') { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
}; | ||
var filterFontInfos = function filterFontInfos(obj) { | ||
var filterFontTtfInfos = function filterFontTtfInfos(obj) { | ||
return { | ||
@@ -81,9 +121,9 @@ family: obj['16'] ? obj['16'] : obj['1'], | ||
postscript: obj['6'], | ||
alternativeFamily: obj['16'], | ||
alternativeSubFamily: obj['17'] | ||
alternativeFamilies: [], | ||
alternativeSubFamilies: [] | ||
}; | ||
}; | ||
var tableToObj = function tableToObj(obj, file, systemFont) { | ||
var infos = filterFontInfos(obj); | ||
var ttfInfoTableToObj = function ttfInfoTableToObj(obj, file, systemFont) { | ||
var infos = filterFontTtfInfos(obj); | ||
return _extends({}, infos, { | ||
@@ -95,2 +135,39 @@ file: file, | ||
var fontkitTableToObj = function fontkitTableToObj(obj, file, systemFont) { | ||
var familyName = obj.familyName, | ||
subfamilyName = obj.subfamilyName, | ||
postscriptName = obj.postscriptName, | ||
name = obj.name; | ||
var alternativeFamilies = []; | ||
var alternativeSubFamilies = []; | ||
if (name && name.records) { | ||
var _name$records = name.records, | ||
preferredFamily = _name$records.preferredFamily, | ||
preferredSubfamily = _name$records.preferredSubfamily; | ||
if (preferredFamily) { | ||
for (var key in preferredFamily) { | ||
alternativeFamilies.push(preferredFamily[key]); | ||
} | ||
} | ||
if (preferredSubfamily) { | ||
for (var _key in preferredSubfamily) { | ||
alternativeSubFamilies.push(preferredSubfamily[_key]); | ||
} | ||
} | ||
} | ||
var infos = { | ||
family: familyName, | ||
subFamily: subfamilyName, | ||
postscript: postscriptName, | ||
alternativeFamilies: alternativeFamilies, | ||
alternativeSubFamilies: alternativeSubFamilies | ||
}; | ||
return _extends({}, infos, { | ||
file: file, | ||
systemFont: systemFont | ||
}); | ||
}; | ||
var extendedReducer = function extendedReducer(m, _ref) { | ||
@@ -101,3 +178,5 @@ var family = _ref.family, | ||
postscript = _ref.postscript, | ||
systemFont = _ref.systemFont; | ||
systemFont = _ref.systemFont, | ||
alternativeFamilies = _ref.alternativeFamilies, | ||
alternativeSubFamilies = _ref.alternativeSubFamilies; | ||
@@ -110,3 +189,5 @@ if (m.has(family)) { | ||
files: _extends({}, origFont.files, _defineProperty({}, subFamily, file)), | ||
postscriptNames: _extends({}, origFont.postscriptNames, _defineProperty({}, subFamily, postscript)) | ||
postscriptNames: _extends({}, origFont.postscriptNames, _defineProperty({}, subFamily, postscript)), | ||
alternativeFamilies: _extends({}, origFont.alternativeFamilies, _defineProperty({}, subFamily, alternativeFamilies)), | ||
alternativeSubFamilies: _extends({}, origFont.alternativeSubFamilies, _defineProperty({}, subFamily, alternativeSubFamilies)) | ||
})); | ||
@@ -119,3 +200,5 @@ } else { | ||
files: _defineProperty({}, subFamily, file), | ||
postscriptNames: _defineProperty({}, subFamily, postscript) | ||
postscriptNames: _defineProperty({}, subFamily, postscript), | ||
alternativeFamilies: _defineProperty({}, subFamily, alternativeFamilies), | ||
alternativeSubFamilies: _defineProperty({}, subFamily, alternativeSubFamilies) | ||
}); | ||
@@ -125,2 +208,52 @@ } | ||
var reorderWithAlt = function reorderWithAlt(fonts) { | ||
fonts.forEach(function (name) { | ||
var alternativeFamilies = name.alternativeFamilies, | ||
alternativeSubFamilies = name.alternativeSubFamilies, | ||
family = name.family; | ||
if (alternativeFamilies && alternativeSubFamilies) { | ||
var _loop = function _loop(type) { | ||
var altFamilies = alternativeFamilies[type]; | ||
altFamilies.forEach(function (altFamily, index) { | ||
if (altFamily && family !== altFamily) { | ||
var file = name.files[type]; | ||
var postscriptName = name.postscriptNames[type]; | ||
var altSubFamily = alternativeSubFamilies[type][index]; | ||
var existingFamily = null; | ||
for (var i = 0; i < fonts.length; i++) { | ||
if (fonts[i].family === altFamily) { | ||
existingFamily = fonts[i]; | ||
break; | ||
} | ||
} | ||
if (existingFamily) { | ||
if (existingFamily.subFamilies.indexOf(altSubFamily) === -1) { | ||
existingFamily.subFamilies.push(altSubFamily); | ||
existingFamily.files[altSubFamily] = file; | ||
existingFamily.postscriptNames[altSubFamily] = postscriptName; | ||
} | ||
} else { | ||
fonts.push({ | ||
family: altFamily, | ||
systemFont: name.systemFont, | ||
subFamilies: [altSubFamily], | ||
files: _defineProperty({}, altSubFamily, file), | ||
postscriptNames: _defineProperty({}, altSubFamily, postscriptName), | ||
alternativeFamilies: [], | ||
alternativeSubFamilies: [] | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
for (var type in alternativeFamilies) { | ||
_loop(type); | ||
} | ||
} | ||
}); | ||
return fonts; | ||
}; | ||
var SystemFonts = function SystemFonts() { | ||
@@ -140,2 +273,8 @@ var _this = this; | ||
var debug = function debug(title, msg) { | ||
if (options.debug) { | ||
console.log(title, typeof msg !== 'function' ? msg : msg()); | ||
} | ||
}; | ||
if (!Array.isArray(customDirs)) { | ||
@@ -159,6 +298,8 @@ throw new Error('customDirs must be an array of folder path strings'); | ||
var home = process.env.HOME; | ||
directories = [].concat(_toConsumableArray(directories), [_path2.default.join(home, 'Library', 'Fonts'), _path2.default.join('/', 'Library', 'Fonts'), _path2.default.join('/', 'System', 'Library', 'Fonts')]); | ||
directories = [].concat(_toConsumableArray(directories), [_path2.default.join(home, 'Library', 'Fonts'), _path2.default.join('/', 'Library', 'Fonts'), _path2.default.join('/', 'System', 'Library', 'Fonts'), _path2.default.join(_os2.default.homedir(), 'Library', 'Application Support', 'Adobe', 'CoreSync', 'plugins', 'livetype', '.r')]); | ||
} else if (platform === 'windows') { | ||
var winDir = process.env.windir || process.env.WINDIR; | ||
directories = [].concat(_toConsumableArray(directories), [_path2.default.join(_path2.default.resolve(process.env.APPDATA, '..'), 'Local', 'Microsoft', 'Windows', 'Fonts'), _path2.default.join(winDir, 'Fonts')]); | ||
directories = [].concat(_toConsumableArray(directories), [_path2.default.join(_path2.default.resolve(process.env.APPDATA, '..'), 'Local', 'Microsoft', 'Windows', 'Fonts'), _path2.default.join(winDir, 'Fonts') | ||
//path.join(os.homedir(), 'AppData', 'Roaming', 'Adobe', 'CoreSync', 'plugins', 'livetype', 'r') | ||
]); | ||
} else { | ||
@@ -170,2 +311,4 @@ // some flavor of Linux, most likely | ||
debug('Directories', directories); | ||
return directories.reduce(function (arr, d) { | ||
@@ -190,2 +333,5 @@ var files = recGetFile(d); | ||
fontFiles = filterReadableFonts(allFontFiles); | ||
// debug('All fonts after filter', () => { | ||
// fontFiles.forEach(font => console.log(font)); | ||
// }); | ||
}; | ||
@@ -195,2 +341,54 @@ | ||
this.getFontInfo = function (file) { | ||
return new Promise(function (resolve1) { | ||
_ttfinfo2.default.get(file, function (err, fontMeta) { | ||
if (!fontMeta) { | ||
if (_this.options.fontkit) { | ||
_this.options.fontkit.open(file, null, function (err2, fontMeta2) { | ||
if (!fontMeta2) { | ||
debug('Error reading font ' + file, err2); | ||
resolve1(null); | ||
} else { | ||
var fonts = fontMeta2.fonts || fontMeta2; | ||
var fontInfos = fonts.map(function (font) { | ||
return fontkitTableToObj(font, file, !customFontFiles.has(file)); | ||
}); | ||
resolve1(fontInfos.length === 0 ? null : fontInfos.length === 1 ? fontInfos[0] : fontInfos); | ||
} | ||
}); | ||
} else { | ||
debug('Error reading font ' + file, err); | ||
resolve1(null); | ||
} | ||
} else { | ||
var fontInfos = ttfInfoTableToObj(fontMeta.tables.name, file, !customFontFiles.has(file)); | ||
resolve1(fontInfos); | ||
} | ||
}); | ||
}); | ||
}; | ||
this.getFontInfoSync = function (file) { | ||
try { | ||
var fontMeta = _ttfinfo2.default.getSync(file); | ||
return ttfInfoTableToObj(fontMeta.tables.name, file, !customFontFiles.has(file)); | ||
} catch (err) { | ||
try { | ||
if (_this.options.fontkit) { | ||
var fontMeta2 = _this.options.fontkit.openSync(file); | ||
var fonts = fontMeta2.fonts || fontMeta2; | ||
var fontInfos = fonts.map(function (font) { | ||
return fontkitTableToObj(font, file, !customFontFiles.has(file)); | ||
}); | ||
return fontInfos.length === 0 ? null : fontInfos.length === 1 ? fontInfos[0] : fontInfos; | ||
} else { | ||
debug('Error reading font ' + file, err); | ||
} | ||
} catch (err2) { | ||
debug('Error reading font ' + file, err2); | ||
} | ||
} | ||
return null; | ||
}; | ||
// this list includes all TTF and OTF files (these are the ones we parse in this lib) | ||
@@ -210,19 +408,17 @@ this.getFontFilesSync = function () { | ||
filteredFontFiles.forEach(function (file, i) { | ||
promiseList.push(new Promise(function (resolve1) { | ||
_ttfinfo2.default.get(file, function (err, fontMeta) { | ||
if (!fontMeta) { | ||
resolve1(null); | ||
} else { | ||
resolve1(tableToObj(fontMeta.tables.name, file, !customFontFiles.has(file))); | ||
} | ||
}); | ||
})); | ||
filteredFontFiles.forEach(function (file) { | ||
return promiseList.push(_this.getFontInfo(file)); | ||
}); | ||
Promise.all(promiseList).then(function (res) { | ||
var names = res.filter(function (data) { | ||
return data ? true : false; | ||
}).reduce(extendedReducer, new Map()); | ||
Promise.all(promiseList).then(function (_res) { | ||
var res = []; | ||
_res.forEach(function (fonts) { | ||
if (fonts) { | ||
if (fonts.length) { | ||
fonts.forEach(function (font) { | ||
return res.push(font); | ||
}); | ||
} else res.push(fonts); | ||
} | ||
}); | ||
var names = res.reduce(extendedReducer, new Map()); | ||
var namesArr = [].concat(_toConsumableArray(names.values())).sort(function (a, b) { | ||
@@ -232,3 +428,3 @@ return a.family.localeCompare(b.family); | ||
resolve(namesArr); | ||
resolve(reorderWithAlt(namesArr)); | ||
}, function (err) { | ||
@@ -246,18 +442,18 @@ return reject(err); | ||
var names = filteredFontFiles.reduce(function (arr, file) { | ||
var data = void 0; | ||
try { | ||
data = _ttfinfo2.default.getSync(file); | ||
} catch (e) { | ||
return arr; | ||
var res = []; | ||
filteredFontFiles.forEach(function (font) { | ||
var metas = _this.getFontInfoSync(font); | ||
if (metas) { | ||
if (metas.length) { | ||
metas.forEach(function (meta) { | ||
return res.push(meta); | ||
}); | ||
} else res.push(metas); | ||
} | ||
return arr.concat([tableToObj(data.tables.name, file, !customFontFiles.has(file))]); | ||
}, []).filter(function (data) { | ||
return data ? true : false; | ||
}).reduce(extendedReducer, new Map()); | ||
}); | ||
var names = res.reduce(extendedReducer, new Map()); | ||
var namesArr = [].concat(_toConsumableArray(names.values())).sort(function (a, b) { | ||
return a.family.localeCompare(b.family); | ||
}); | ||
return namesArr; | ||
return reorderWithAlt(namesArr); | ||
}; | ||
@@ -306,3 +502,3 @@ | ||
var _loop = function _loop(i) { | ||
var _loop2 = function _loop2(i) { | ||
if (search[i].family === family) { | ||
@@ -322,5 +518,5 @@ is_sorted = true; | ||
for (var i = 0; i < search.length; i++) { | ||
var _ret = _loop(i); | ||
var _ret2 = _loop2(i); | ||
if (_ret === 'break') break; | ||
if (_ret2 === 'break') break; | ||
} | ||
@@ -343,3 +539,3 @@ if (!is_sorted) { | ||
if (family === fonts[i].family) { | ||
var _ret2 = function () { | ||
var _ret3 = function () { | ||
found_font = true; | ||
@@ -371,3 +567,3 @@ var files = fonts[i].files; | ||
if (_ret2 === 'break') break; | ||
if (_ret3 === 'break') break; | ||
} | ||
@@ -439,7 +635,9 @@ } | ||
try { | ||
var _filterFontInfos = filterFontInfos(_ttfinfo2.default.getSync(font).tables.name), | ||
family = _filterFontInfos.family, | ||
subFamily = _filterFontInfos.subFamily; | ||
var fontInfos = _this.getFontInfoSync(font); | ||
if (fontInfos) { | ||
var family = fontInfos.family, | ||
subFamily = fontInfos.subFamily; | ||
searchFonts.push({ family: family, style: subFamily, path: font }); | ||
searchFonts.push({ family: family, style: subFamily, path: font }); | ||
} else fontsToInstall.push(font); | ||
} catch (e) { | ||
@@ -446,0 +644,0 @@ console.error(e); |
{ | ||
"name": "dnm-font-manager", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "Search system fonts with family and style using pure JavaScript, forked from rBurgett/system-font-families", | ||
@@ -25,4 +25,4 @@ "main": "dist/main.js", | ||
"eslint": "^3.14.1", | ||
"lodash": "^4.17.4", | ||
"mocha": "^2.4.5", | ||
"lodash": "^4.17.20", | ||
"mocha": "^8.1.3", | ||
"should": "^8.3.0" | ||
@@ -37,4 +37,6 @@ }, | ||
"babel-polyfill": "^6.23.0", | ||
"ttfinfo": "https://github.com/rBurgett/ttfinfo.git" | ||
"file-type": "^10.11.0", | ||
"read-chunk": "^3.2.0", | ||
"ttfinfo": "git+https://github.com/rBurgett/ttfinfo.git" | ||
} | ||
} |
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
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
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
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
46115
639
1
4
+ Addedfile-type@^10.11.0
+ Addedread-chunk@^3.2.0
+ Addedfile-type@10.11.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpify@4.0.1(transitive)
+ Addedread-chunk@3.2.0(transitive)
+ Addedwith-open-file@0.1.7(transitive)