Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

electron使用font-list的正确方法,支持windows和mac #29

Open
Beats0 opened this issue Mar 22, 2022 · 1 comment
Open

electron使用font-list的正确方法,支持windows和mac #29

Beats0 opened this issue Mar 22, 2022 · 1 comment

Comments

@Beats0
Copy link

Beats0 commented Mar 22, 2022

正常情况下,electron-builder 默认是不会将 font-list 打包的,所以导致无法使用,通过 extraResources 执行处理即可,下面为使用步骤:

  1. package.json 中添加
   "extraResources": [
      "./extraResources/**"
    ],
  1. 在项目根目录创建 extraResources/fontlist 文件夹,将 node_modules/font-list 下的文件源码复制到 extraResources
extraResources
  └─fontlist
      ├─getSystemFonts.js
      ├─index.d.ts
      ├─index.js
      └─libs
          ├─darwin
          ├─linux
          └─win32
  1. 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);
})();
  1. 在主进程 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);
  });
});
  1. 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;
});
  1. 引用即可
import { systemFonts } from "utils/common";

console.log('systemFonts', systemFonts)
// [ 'Arial', 'Bahnschrift', 'Book Antiqua']

具体commit源码 Beats0/bilive-danmaku@0f3f450

@wxfred
Copy link

wxfred commented Mar 22, 2022

嗯,我也是手动拷贝了一份,放到一个文件夹里。但是,这只是个workaround

  extraResources: [
    'bin/*',
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants