为什么 CSS 'ex' 单位使用 'first available font' 定义?

Why is CSS 'ex' unit defined using the 'first available font'?

为什么 em 单位根据实际用于呈现文本的字体定义,而 ex 单位使用 第一个可用字体?

在我看来,用于计算 ex 高度的字体可能与实际用于呈现文本的字体不同。

引用规范:

The first available font, used for example in the definition of font-relative lengths such as ‘ex’ and ‘ch’ or in the definition of the ‘line-height’ property, is defined to be the first available font that would match the U+0020 (space) character given font families in the ‘font-family’ list (or a user agent's default font if none are available).

为什么算法会寻找space来计算字母'x'的高度?非常感谢通俗易懂的解释。

Why is the em unit defined in terms of the font actually used to render the text, and the ex unit using the first available font?

这不应该是这样的:两个单位都是相对于当前字体的。您提供的定义中提到“font-relative lengths 例如‘ex’”,其中还包括“em”单位。

也就是说,作者似乎同意应该澄清“第一个可用字体”的定义:https://github.com/w3c/csswg-drafts/issues/4796

您引用的部分似乎暗示如果 font-family 列表中的第一个字体存在,但 U+0020 (space) 字符不在字体中,则下一个应使用字体。实际上,听起来浏览器并没有这样做,而且这可能不是最初的意图。

您可以在此处看到对定义所做的更改,如该期中的总结:https://github.com/w3c/csswg-drafts/commit/7c2108c1764f328e0b60fffed47d3885a3dc7c11?diff=split

Why does the algorithm look for the space to compute the height of the letter 'x'? An explanation in layman terms would be very appreciated.

出于收集和计算字体指标的目的,U+0020 space 很可能是最早和最常见的代码点,它可以包含该信息并且可以进行检查。然后计算许多指标,例如行高和 em 单位,而不仅仅是 ex 单位。

除此之外,CSS ex unit section 提供了有关如何确定该值的更多详细信息:

The x-height is so called because it is often equal to the height of the lowercase "x". However, an ex is defined even for fonts that do not contain an "x". The x-height of a font can be found in different ways. Some fonts contain reliable metrics for the x-height. If reliable font metrics are not available, UAs may determine the x-height from the height of a lowercase glyph. One possible heuristic is to look at how far the glyph for the lowercase "o" extends below the baseline, and subtract that value from the top of its bounding box. In the cases where it is impossible or impractical to determine the x-height, a value of 0.5em must be assumed.