I am a full-stack engineer and independent developer living in beautiful Hangzhou.
- 🐦 Twitter - @oldj
- 📝 Blog - https://oldj.net
- ✉️ Email - [email protected]
Get the list of fonts installed in the system.
License: MIT License
I am a full-stack engineer and independent developer living in beautiful Hangzhou.
Hi,
Thanks for clarifying on the README how the strings should appear in the array.
However, when nesting on RunKit, I see that even fonts with spaces do not have quotes around them:
Good afternoon! I was wondering if you knew how compatible this package was in an Angular Application. I love the idea of how "getFonts" works, however it had issues loading in "child_process" and "fs". Once I followed some recommended fixes to get those working, I got "process is undefined" error when trying to run the function. Thanks in advance for any insights you can give me! Here are some details about what I am running locally with the package:
For example, if the font named Lato:Regular
has installed, when running demo.js from this repository by the following command in Bash:
node demo.js | grep 'Lato'
and the output will be:
Lato\
The expected output should be:
Lato:Regular
windows 7 上无法正常获取字体列表,windows 10 上正常
__dirname
has different meaning in electron-builder, in font-list/libs/darwin/index.js
const bin = path.join(__dirname, 'fontlist')
can't get the correct path of the fontlist
file.
Is there any solution?
目前Flutter的desktop版本还没有类似的库。
正常情况下,electron-builder
默认是不会将 font-list
打包的,所以导致无法使用,通过 extraResources
执行处理即可,下面为使用步骤:
package.json
中添加 "extraResources": [
"./extraResources/**"
],
extraResources/fontlist
文件夹,将 node_modules/font-list
下的文件源码复制到 extraResources
下extraResources
└─fontlist
├─getSystemFonts.js
├─index.d.ts
├─index.js
└─libs
├─darwin
├─linux
└─win32
getSystemFonts.js
中为const { getFonts } = require('./index');
function getSystemFonts() {
return new Promise((resolve, reject) => {
getFonts({ disableQuoting: true })
.then((fonts) => {
fonts = [...new Set(fonts)];
resolve(fonts || []);
})
.catch((err) => {
resolve([]);
});
});
}
(async () => {
const fonts = await getSystemFonts();
// process 处理
process.send(fonts);
// console.log('fonts', fonts);
})();
main.ts
中添加触发事件 getSystemFonts
import path from 'path';
import cp from 'child_process';
const EXTRARESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'extraResources')
: path.join(__dirname, '../../extraResources');
const getExtraResourcesPath = (...paths: string[]): string => {
return path.join(EXTRARESOURCES_PATH, ...paths);
};
// ...
ipcMain.on('getSystemFonts', async () => {
const systemFontsScriptPath = getExtraResourcesPath('fontlist/getSystemFonts.js')
let fonts: string[] = [];
const forked = cp.fork(systemFontsScriptPath);
forked.on('message', function (message: string[]) {
fonts = message;
mainWindow?.webContents.send('getSystemFontsCb', fonts);
});
});
utils/common.ts
为例子触发事件import { ipcRenderer } from "electron";
export const systemFonts: string[] = [];
// 获取系统字体列表
ipcRenderer.send('getSystemFonts');
ipcRenderer.on('getSystemFontsCb', (e, fonts: string[] = []) => {
// console.log('fonts', fonts);
systemFonts = fonts;
});
import { systemFonts } from "utils/common";
console.log('systemFonts', systemFonts)
// [ 'Arial', 'Bahnschrift', 'Book Antiqua']
具体commit源码 Beats0/bilive-danmaku@0f3f450
您好,非常感谢。这里报告一个问题,electron 打包在windows下无法获取字体, 报错信息:
EPERM: operation not permitted, copyfile 'C:\Program Files\BlinkMind\resources\app.asar.unpacked\node_modules\font-list\libs\win32\fonts.vbs' -> 'C:\Users\zhou\AppData\Local\Temp\node-font-list-fonts.vbs'
现在获取的字体无法直接在css里面用
我改了一下 不知道是否可以提交pr
建议直接在运行时判断文件是否存在,如果不存在则通过fs库将vbs内容写出到这个位置就好了
when build my electron project by
electron-packager . test --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/image/logo.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName="test"
font-list not work but when remove --asar worked
show message
error: Command failed: cscript "C:\Users\Your\release-builds\rc-win32-ia32\resources\app.asar\node_modules\font-list\libs\win32\fonts.vbs"
at ChildProcess.exithandler (child_process.js:304)
at ChildProcess.emit (events.js:200)
at maybeClose (internal/child_process.js:1021)
at Process.ChildProcess._handle.onexit (internal/child_process.js:283)
不能正常装进 app.asar
For example, if the font named Lato:Regular
has installed, when running demo.js from this repository by the following command in PowerShell:
PS> node demo.js | Select-String 'Lato:'
Nothing will be print out. The Lato:Regular
font is missing from the font list.
有木有linux版本支持哇,类似用fontconfig实现
Hi,
The code at https://github.com/oldj/node-font-list/blob/master/libs/win32/index.js#L69-L71 is being flagged by https://lgtm.com/projects/g/oldj/node-font-list/?mode=list with an explanation for the concern at https://lgtm.com/rules/1510773276307/ . It appears that this concern could be minimized by using:
let cmd = 'cscript'
execFile(cmd, [fn], (err, stdout, stderr) => {
Btw, I find LGTM.com to be a useful site which picks up issues like this which ESLint does not (and across multiple languages), and they have an integration for your site's PRs in case you might be interested: https://lgtm.com/help/lgtm/github-apps-integration
Hi, the linux implementation executes binary fc-list2
, but on archlinux fontconfig this binary is called fc-list
, without 2
. Maybe it would be better to test for existence of one of these two binaries with something like which
first?
When I packaged an "electron.exe", I found that the system font library could not be obtained. Later, through Debug, I found that the "font.vbs" file could not be found.
当前版本:1.2.11
当前系统:win10
当前nodejs:12.13.0
#13 中提到的强制使用utf8代码页chcp 65001
并不是一个万能的解决方案。在我的电脑中这并不能按照预期工作,还是有乱码:
215: ""Yu Gothic UI Semilight""
216: ""Yu Gothic UI""
217: ""Yu Gothic""
218: "ܛ�����w"
219: ""ܛ�����w Light""
220: "���ź�"
221: ""���ź� Light""
222: "�¼����w-ExtB"
223: "��Բ"
224: "���IJ���"
225: "���ķ���"
在其他项目中也有类似的情况,例如wmic,但都不清楚问题的原因是什么,这里提到的可能是由于控制台使用的是矢量字体的原因,我尝试过切换字体,但一样不能工作。(也许是因为vs code的原因?因为就算我切换字体vs code里终端的字体也没变化)
目前我发现的一个较为可行的解决方案是使用iconv-lite
进行编码转换:
const iconv = require("iconv-lite");
exec(cmd, { maxBuffer: 1024 * 1024 * 10, encoding: "binary" }, (err, stdout, stderr) => {
let t = iconv.decode(Buffer.from(stdout, "binary"), "utf8");
// if not include ‘�’
if (!t.includes("\uFFFD")) stdout = t;
else stdout = iconv.decode(Buffer.from(stdout, "binary"), "cp936");
if (err) return reject(err);
resolve(parse(stdout));
});
只判断�
是因为Buffer
会将无法解析为utf8的字符变成�,参考。
由此得出一个通用的解决方案是通过使用chcp
来获取当前活动页码,使用binary方式读取结果,最后使用iconv根据活动页码转换编码。
此外,通过vbs获取字体信息时,也存在乱码现象。但在处理返回结果的时候,把objFont.Name
的结果给过滤掉了:
// libs/win32/getByVBS.js : line 21
a = a.map(i => {
i = i
.split('\t')[0]
.split(path.sep)
i = i[i.length - 1]
getByVBS.js这里后续的处理中处理的都是字体路径而非字体名,不知是否符合作者你的预期。
Ps. 通过powershell获取的结果中有空字符串,希望过滤一下。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.