【问题标题】:How to use custom font in VS Code(Windows) without admin privilege to install the font?如何在没有管理员权限的情况下在 VS Code(Windows) 中使用自定义字体来安装字体?
【发布时间】:2018-09-12 03:01:26
【问题描述】:

我在我的工作 PC (Windows 7) 上没有管理员权限,因此我无法将自定义字体 (Fira Code) 安装到我的系统中。有没有办法在不安装 VS Code 的情况下使用这种字体?

【问题讨论】:

    标签: fonts visual-studio-code vscode-settings


    【解决方案1】:

    为这个问题找到一个丑陋的解决方法:使用 webfont。

    1. 打开帮助 -> 在菜单中切换开发者工具
    2. 粘贴下面的 js 代码并在 DevTools 的控制台中执行以设置“Fira Code”字体。
    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. 确保“Fira 代码”是字体系列设置中的第一个参数。
    2. 启用字体连字设置,否则 ">=" 不会转移到 "≥"
    3. (可选)在 DevTools 的 Sources 选项卡中,使用代码创建一个新的 sn-p,然后右键单击新创建的文件以运行。它可以在重新启动应用程序后一直保留。

    【讨论】:

    • 谢谢!我注意到重新启动后保留了 sn-p,但每次启动应用程序时都必须运行它。它仍然比使用默认字体更好!我想知道这是否可以自动作为 vs 代码扩展运行,也许?
    【解决方案2】:

    我找到了一种方法来做到这一点,而不必每次启动 VS Code 时都运行 sn-p。

    1. 转到File > Open Folder
    2. 导航到您的 VS Code 安装,然后转到:

      resources > app > out > vs > code > electron-browser > workbench 打开那个文件夹。

    3. 使用 VS Code 编辑 workbench.js 并将 Tai 的 sn-p 添加到它的末尾。

    4. 保存
    5. Ctrl+R 重新加载窗口,你应该完成了!

    同样,请确保您在 VS Code 的字体系列设置中将 Fira Code 作为第一个选项,并确保启用了字体连字​​。

    【讨论】:

    • 我似乎在我的 Appdata 安装中找不到这个“资源”文件夹
    • 谢谢,这对我很有用!但是,它确实会导致 VS 代码中出现一条消息(“安装似乎已损坏 [不支持]”),参考此链接:code.visualstudio.com/docs/supporting/…,这对于许多用户来说可能是不受欢迎的。
    【解决方案3】:

    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"
    ]
    

    【讨论】:

    • 如何添加多种字体(用于快速切换),这可能吗?
    • 张太的代码是制作一个样式节点并附加到头部。您应该能够创建任意数量的节点,每种字体一个,并将它们全部附加到头部。
    • 这种 hack 应该越来越没必要了 - 较新版本的 Windows 10 允许您在没有管理员权限的情况下右键单击安装字体,而旧版本的 Windows 10 允许您使用 RegisterFont 来执行相同的操作(请参阅我的另一个答案)。
    【解决方案4】:

    您实际上并不需要破解 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多