mirror of
https://github.com/ecomfe/fontmin.git
synced 2026-01-25 14:42:33 +00:00
getUniqueText() process passed string as a regular one, it doesn't handle unicode properly and will remove code points parts when several code points share the same numeric parts. Ultimately, it will result in wrong code points being pushed to output. The glyphs count will match, not the codes themselves. Fix subset of UTF-16 glyphs (and probably others). Keeping getUniqText() method for backward compatibility / third party packages relying on it.
129 lines
2.4 KiB
JavaScript
129 lines
2.4 KiB
JavaScript
/**
|
|
* @file util
|
|
* @author junmer
|
|
*/
|
|
|
|
/* eslint-env node */
|
|
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
var _ = require('lodash');
|
|
var codePoints = require('code-points');
|
|
|
|
/**
|
|
* getFontFolder
|
|
*
|
|
* @return {string} fontFolder
|
|
*/
|
|
function getFontFolder() {
|
|
return path.resolve({
|
|
win32: '/Windows/fonts',
|
|
darwin: '/Library/Fonts',
|
|
linux: '/usr/share/fonts/truetype'
|
|
}[process.platform]);
|
|
}
|
|
|
|
/**
|
|
* getFonts
|
|
*
|
|
* @param {string} path path
|
|
* @return {Array} fonts
|
|
*/
|
|
function getFonts() {
|
|
return fs.readdirSync(getFontFolder());
|
|
}
|
|
|
|
/**
|
|
* getPureText
|
|
*
|
|
* @see https://msdn.microsoft.com/zh-cn/library/ie/2yfce773
|
|
* @see http://www.unicode.org/charts/
|
|
*
|
|
* @param {string} str target text
|
|
* @return {string} pure text
|
|
*/
|
|
function getPureText(str) {
|
|
|
|
// fix space
|
|
var emptyTextMap = {};
|
|
|
|
function replaceEmpty (word) {
|
|
emptyTextMap[word] = 1;
|
|
return '';
|
|
}
|
|
|
|
var pureText = String(str)
|
|
.trim()
|
|
.replace(/[\s]/g, replaceEmpty)
|
|
// .replace(/[\f]/g, '')
|
|
// .replace(/[\b]/g, '')
|
|
// .replace(/[\n]/g, '')
|
|
// .replace(/[\t]/g, '')
|
|
// .replace(/[\r]/g, '')
|
|
.replace(/[\u2028]/g, '')
|
|
.replace(/[\u2029]/g, '');
|
|
|
|
var emptyText = Object.keys(emptyTextMap).join('');
|
|
|
|
return pureText + emptyText;
|
|
|
|
}
|
|
|
|
/**
|
|
* getUniqText
|
|
*
|
|
* @param {string} str target text
|
|
* @return {string} uniq text
|
|
*/
|
|
function getUniqText(str) {
|
|
return _.uniq(
|
|
str.split('')
|
|
).join('');
|
|
}
|
|
|
|
|
|
/**
|
|
* basic chars
|
|
*
|
|
* "!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}"
|
|
*
|
|
* @type {string}
|
|
*/
|
|
var basicText = String.fromCharCode.apply(this, _.range(33, 126));
|
|
|
|
/**
|
|
* get subset text
|
|
*
|
|
* @param {Object} opts opts
|
|
* @return {string} subset text
|
|
*/
|
|
function getSubsetText(opts) {
|
|
|
|
var text = opts.text || '';
|
|
|
|
// trim
|
|
text && opts.trim && (text = getPureText(text));
|
|
|
|
// basicText
|
|
opts.basicText && (text += basicText);
|
|
|
|
return text;
|
|
}
|
|
|
|
/**
|
|
* string to unicodes
|
|
*
|
|
* @param {string} str string
|
|
* @return {Array} unicodes
|
|
*/
|
|
function string2unicodes(str) {
|
|
return _.uniq(codePoints(str));
|
|
}
|
|
|
|
exports.getFontFolder = getFontFolder;
|
|
exports.getFonts = getFonts;
|
|
exports.getPureText = getPureText;
|
|
exports.getUniqText = getUniqText;
|
|
exports.getSubsetText = getSubsetText;
|
|
exports.string2unicodes = string2unicodes;
|