forked from oldj/node-font-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @author oldj
* @blog http://oldj.net
*/
'use strict'
const platform = process.platform
let getFontsFunc
switch (platform) {
case 'darwin':
getFontsFunc = require('./libs/darwin')
break
case 'win32':
getFontsFunc = require('./libs/win32')
break
case 'linux':
getFontsFunc = require('./libs/linux')
break
default:
throw new Error(`Error: font-list can not run on ${platform}.`)
}
const defaultOptions = {
disableQuoting: false
}
exports.getFonts = async (options) => {
options = Object.assign({}, defaultOptions, options)
let fonts = await getFontsFunc()
fonts = fonts.map(i => {
// parse unicode names, eg: '"\\U559c\\U9e4a\\U805a\\U73cd\\U4f53"' -> '"喜鹊聚珍体"'
try {
i = i.replace(/\\u([\da-f]{4})/ig, (m, s) => String.fromCharCode(parseInt(s, 16)))
} catch (e) {
console.log(e)
}
if (options && options.disableQuoting) {
if (i.startsWith('"') && i.endsWith('"')) {
i = `${i.substr(1, i.length - 2)}`
}
} else if (i.includes(' ') && !i.startsWith('"')) {
i = `"${i}"`
}
return i
})
fonts.sort((a, b) => {
return a.replace(/^['"]+/, '').toLocaleLowerCase() < b.replace(/^['"]+/, '').toLocaleLowerCase() ? -1 : 1
})
return fonts
}