How to use custom font in VS Code(Windows) without admin privilege to install the font?

Viewed 4334

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?

4 Answers

Find a ugly workaround for this issue: using webfont.

  1. Open Help -> Toggle Developer Tools in the menu
  2. Paste below js codes and execute in the Console of DevTools to setup 'Fira Code' font face.
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);
  1. Make sure 'Fira Code' is the first parameter in the Font Family settings.
  2. Enable Font Ligatures settings, otherwise ">=" won't be transferred to "≥"
  3. (Optional) in the Sources tab of DevTools, create a new snippet with the codes, and right click the new created file to run. It can be preserved ever after restart the application.

I found a way to do this without having to run the snippet every time you stat up VS Code.

  1. Go to File > Open Folder
  2. Navigate to your VS Code installation, and then go to:

    resources > app > out > vs > code > electron-browser > workbench Open that folder.

  3. Edit workbench.js using VS Code and add the Tai's snippet to the end of it.

  4. Save
  5. Do 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);
    }
}
Related