【发布时间】:2018-09-12 03:01:26
【问题描述】:
我在我的工作 PC (Windows 7) 上没有管理员权限,因此我无法将自定义字体 (Fira Code) 安装到我的系统中。有没有办法在不安装 VS Code 的情况下使用这种字体?
【问题讨论】:
标签: fonts visual-studio-code vscode-settings
我在我的工作 PC (Windows 7) 上没有管理员权限,因此我无法将自定义字体 (Fira Code) 安装到我的系统中。有没有办法在不安装 VS Code 的情况下使用这种字体?
【问题讨论】:
标签: fonts visual-studio-code vscode-settings
为这个问题找到一个丑陋的解决方法:使用 webfont。
var styleNode = document.createElement('style');
styleNode.type = "text/css";
var styleText = document.createTextNode(`
@font-face{
font-family: 'Fira Code';
src: url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/eot/FiraCode-Regular.eot') format('embedded-opentype'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/woff2/FiraCode-Regular.woff2') format('woff2'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/woff/FiraCode-Regular.woff') format('woff'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/ttf/FiraCode-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}`);
styleNode.appendChild(styleText);
document.getElementsByTagName('head')[0].appendChild(styleNode);
【讨论】:
我找到了一种方法来做到这一点,而不必每次启动 VS Code 时都运行 sn-p。
File > Open Folder
导航到您的 VS Code 安装,然后转到:
resources > app > out > vs > code > electron-browser > workbench
打开那个文件夹。
使用 VS Code 编辑 workbench.js 并将 Tai 的 sn-p 添加到它的末尾。
Ctrl+R 重新加载窗口,你应该完成了!同样,请确保您在 VS Code 的字体系列设置中将 Fira Code 作为第一个选项,并确保启用了字体连字。
【讨论】:
Tai Zhang 的 great little hack 可以通过 Monkey Patch 扩展名自动化。
安装Monkey Patch后,将Tai的代码粘贴到一个新文件中,例如%USERPROFILE%\vscode-monkeypatch-modules\custom-fonts.js.
现在将文件添加到您的settings.json,例如:-
"monkeyPatch.folderMap": {
"my-custom-modules" : "~/vscode-monkeypatch-modules",
},
"monkeyPatch.browserModules": [
"my-custom-modules/custom-fonts"
]
【讨论】:
您实际上并不需要破解 VS Code - 您可以使用免费的 RegisterFont 应用程序在没有管理员权限的情况下将每个会话字体添加到窗口,或者编写您自己的小应用程序以在每种字体上调用 AddFontResource。
我创建了一个 C# 应用程序,将 .exe 放在我的用户字体目录中,然后将 paste shortcut 放入我的用户启动文件夹(您可以通过在 Run... 对话框中键入 shell:startup 来找到它):-
using System;
using System.IO;
using System.Runtime.InteropServices;
class Program
{
[DllImport("gdi32.dll")]
static extern int AddFontResource(string lpFilename);
[DllImport("gdi32.dll")]
static extern bool RemoveFontResource(string lpFilename);
[DllImport("user32.dll")]
static extern int SendMessage(int hWnd, uint message, int wParam, int lParam);
static void Main()
{
var exePath = Environment.GetCommandLineArgs()[0];
var fontFolder = Path.GetDirectoryName(exePath);
var fontCache = Path.Combine(Path.GetTempPath(), "UserSessionFontCache");
Directory.CreateDirectory(fontCache);
foreach (var fontPath in Directory.EnumerateFiles(fontFolder, "*.ttf", SearchOption.AllDirectories))
{
var targetPath = Path.Combine(fontCache, Path.GetFileName(fontPath));
try
{
if (File.Exists(targetPath)) RemoveFontResource(targetPath);
File.Copy(fontPath, targetPath, true);
}
catch { /* font in use */ }
if (File.Exists(targetPath)) AddFontResource(targetPath);
}
SendMessage(0xFFFF /* HWND_BROADCAST */, 0x001D /* WM_FONTCHANGE */, 0, 0);
}
}
【讨论】: