I don't have Admin privilege on my working PC(Windows 7), so I can't install custom font(Fira Code) into my system. Is there a way to use such font without installing in VS Code?
I don't have Admin privilege on my working PC(Windows 7), so I can't install custom font(Fira Code) into my system. Is there a way to use such font without installing in VS Code?
Find a ugly workaround for this issue: using 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);
I found a way to do this without having to run the snippet every time you stat up VS Code.
File > Open FolderNavigate to your VS Code installation, and then go to:
resources > app > out > vs > code > electron-browser > workbench
Open that folder.
Edit workbench.js using VS Code and add the Tai's snippet to the end of it.
Ctrl+R to reload the window and you should be done!Again, make sure that you have Fira Code as the first option in your Font Family settings within VS Code, and make sure that font ligatures are enabled.
Tai Zhang's great little hack can be automated with the Monkey Patch extension.
After installing Monkey Patch, paste Tai's code into a new file, e.g. %USERPROFILE%\vscode-monkeypatch-modules\custom-fonts.js.
Now add the file to your settings.json, e.g.:-
"monkeyPatch.folderMap": {
"my-custom-modules" : "~/vscode-monkeypatch-modules",
},
"monkeyPatch.browserModules": [
"my-custom-modules/custom-fonts"
]
You don't actually need to hack VS Code - you can add per-session fonts to windows without admin privileges with the free RegisterFont app, or write your own tiny app to call AddFontResource on each font.
I whipped up a C# app, put the .exe in my user-fonts directory, then paste shortcut it into my user startup folder (which you can find by typing shell:startup into the Run... dialog):-
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);
}
}