等宽字体字符不固定宽度

Monospace font characters are not fixed width

我正在尝试对齐一些字符以在 html 中绘制一个框。我选择了一种等宽字体,以便字符对齐,并为每行绘制了相同数量的字符的框。

pre {
  font-family: 'Roboto Mono';
  white-space: pre;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
<pre>
    ╭─────────────────────────────────────────────────────────────────────────╮
    │.........................................................................│
    │-------------------------------------------------------------------------│
    │                                                                         │
    ╰─────────────────────────────────────────────────────────────────────────╯                         
</pre>

当我在浏览器中渲染它时,最右边的边缘有时可能会错位,具体取决于使用的等宽字体。

当我使用 Roboto Mono 时:

当我使用 Space 单声道时:

出于某种原因,我的等宽字符不是等宽的。为什么会发生这种情况,如何强制等宽以对齐字符?

转到Google 字体并写下您用于边框的字符。将显示空字形,这意味着字体没有边框(顶部和底部)的字形并且浏览器使用后备字体。

所以你有几个选择:

  1. 仅使用monospace字体
  2. 查找支持所有字符的字体
  3. 使用 CSS 实际绘制元素而不是“ASCII-art”

pre {
  font-family: monospace;
  white-space: pre;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
<pre>
    ╭─────────────────────────────────────────────────────────────────────────╮
    │.........................................................................│
    │-------------------------------------------------------------------------│
    │                                                                         │
    ╰─────────────────────────────────────────────────────────────────────────╯                         
</pre>

tl;dr: Google 的字体 CDN 没有提供完整的字体。下载他们提供的 zip 并自己托管字体。


Google 的字体 CDN 似乎没有提供某些字体的完整字符范围。例如,Google的@import是:

@import url('https://fonts.googleapis.com/css2?family=Fira+Code&display=swap');

看着里面的CSS:

/* cyrillic-ext */
@font-face {
  font-family: 'Fira Code';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/firacode/v14/uU9eCBsR6Z2vfE9aq3bL0fxyUs4tcw4W_D1sJV37Nv7g.woff2) format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
@font-face {
  font-family: 'Fira Code';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/firacode/v14/uU9eCBsR6Z2vfE9aq3bL0fxyUs4tcw4W_D1sJVT7Nv7g.woff2) format('woff2');
  unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* more chunks like this omitted */

所以字体被分解成几个文件,他们使用 unicode-range 将字形映射到特定文件。

这些是他们在该文件中提供的所有范围:

unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
unicode-range: U+1F00-1FFF;
unicode-range: U+0370-03FF;
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;

方框绘图字符的范围是U+2500-257F,上面的列表中没有。

我尝试添加它们,但据我所知,提供的文件不包括该范围。

但是,您可以从 Google 下载原始字体文件(作为 .ttf 文件的 zip)。将这些字体文件之一转换为数据 url 并使用它可以正确呈现字形:

pre {
  font-family: 'Fira Code stripped';
}
@font-face {
  font-family: 'Fira Code stripped';
  src: url('data:application/octet-stream;base64,d09GMgABAAAAAA8oABEAAAAAW8gAAA7JAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP0ZGVE0cGjQbcByBmX4GYACCYggEEQgKgzyCcwsaAAE2AiQDIAQgBZAxB2oMBxs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qD5TpLonHy1G3BeT3LZ274P58CeYeRyrJBid/0DZ3GH67XOcf0U9hM5ZNx4fzbTCY0U2j8lFyWOy76Bdatp1UsNrKT0pWIOD9BwjliAUeCB6CYGKBJSNWc+KeaXFDw5+RwPBBOUPZXKPMWs6qdyuQ9UGeamPVhNjVYXkFMVXJa9lHIBgFeapdHapiBdal1icQ2ALMsN62RKH5IO/QZcp/6NBIJ18pSoOJAxpa5KrNfMbSkkkkZ2Uzhyw1fS1nDjXf9DX3oAsXJPkWWjrGRi7h4ql5R0sY4jhPNxUnJTEF5eFTyyg/hdtIZ1b88nUfdyBPEqJUm/+9oLKNtc6Ii4z1XIhMB89R475JNhXfJv6MtIHsuaPFRTDcrJE0IcjCkYSJskEXLXgZ1bI27FaaSBht3HMz7qjOgyOTpArPqZmbOhmPg1WkGms+1uqK4FdUUOSfhztnC0+5QjJidNCtB5aeT+ru8dsU49ktkL7N4HYkMghNzL6xtwoT2CnyC/WZpFIRFDS9BIIEFSHEldFS1wOWhyc6f4Z1aJX9Ded6nn/sN666WHyhaBF56Jj/uR6UQ6gcsEhDnwKTBz2GT5PjLzePc68DrvnqbgMtSboVtpwdqku1xKkPMHtjkgLEH2WTNgHc4Ayd+a7fLCp1v7mSLsYsSKEy8hnHBkjqkFDM/lmTa0dDc88jxntIJR2WxncG4BAA==');
}
<pre>
╭────────────────────────────────────────────╮
│............................................│
│--------------------------------------------│
│                                            │
╰────────────────────────────────────────────╯
</pre>

注意: 为了使这个示例符合答案,我将字体缩减为呈现示例所需的 9 个字符并将其转换为 woff2。使用完整的 ttf 文件进行的外部测试产生相同的结果。

我的结论是 Google 根本没有通过他们的 CDN 提供完整的字体。