1. 介绍
CSS Fonts Level 4 规范([CSS-FONTS-4])描述了 CSS 在文档中选择和使用字体方面提供的控制, 包括对可变字体和彩色字体的支持。 此处的想法是对 CSS Fonts Level 4 中定义的属性和 规则的补充或修改。
本规范当前作为对 CSS Fonts Level 4 规范的增量(delta)。 不要假定如果某些内容未出现在此处,则已被删除。
1.1. 值定义
本规范遵循来自 CSS 属性定义约定(来自 [CSS2]), 并使用来自 [CSS-VALUES-3] 的 值定义语法。 本规范中未定义的值类型在 CSS 值与单位([CSS-VALUES-3])中定义。 与其他 CSS 模块的组合可能会扩展这些值类型的定义。
除了在各自定义中列出的特定属性值之外, 本规范中定义的所有属性 也接受作为其属性值的 CSS 通用关键字。 为了可读性,这些关键字未被重复列出。
2.
Text-Scale
meta
元素
用户代理必须通过将 16px 乘以 文本缩放因子 来计算 medium 的计算值。 用户代理在确定其他 <absolute-size> 关键字的计算大小时也必须应用该因子。 然而,用户代理应对这些其他关键字进行非线性缩放 以保留可读性。
注:对所有关键字应用线性缩放会使大字号变得过大。参见 关于最小和最大关键字体大小的 WCAG 讨论。
含有以下
meta
标签且其
name
属性与 做 ASCII 不区分大小写匹配的文档,
被识别为控制 文本缩放因子。
该 content
属性的值必须与若干被识别的关键字之一做 ASCII 不区分大小写匹配,否则该标签被忽略。
未包含该
meta
标签的文档将假定默认值为 legacy。
每个文档中不得存在多于一个
meta
元素,其
name
属性值与 ASCII 不区分大小写 匹配的 text-scale。
2.1. 关键字
在 text-scale
meta
元素中被识别的关键字为:
- legacy
-
preferred-text-scale 环境变量在移动设备上返回用户操作系统级别的字体因子,
但在桌面设备上返回 1。
文本缩放因子
包含 UA 级别的字体偏好,但忽略操作系统级别的字体偏好。
注: 等价于省略 text-scale
meta标签或包含一个无法识别的 content 属性关键字。 - scale
- preferred-text-scale 环境变量返回 用户在操作系统中设置的字体偏好。 文本缩放因子 同时包含 UA 级别和操作系统级别的字体偏好。
2.2. “legacy” 关键字
当 text-scale content 属性的值为 legacy 时, 文本缩放因子 为用户仅通过用户代理(user agent)提供的任何设置所选择的字体缩放因子。 preferred-text-scale 环境变量的值在桌面平台上必须为 1。
在移动平台上:
-
如果操作系统提供文本缩放设置并且
UA 尚未将该因子应用到 medium 字号,
则 preferred-text-scale 环境变量返回
用户在操作系统文本缩放设置中选择的乘数。
注:在发布时的情况是,Android、iOS、Firefox、Safari 和 Chrome 的所有组合都满足此第一条件。
- 否则 preferred-text-scale 环境变量返回 1。
2.3. 当使用 scale 关键字时
当 text-scale content 属性的值为 scale 时, 文本缩放因子 等于用户偏好的段落文本大小(由操作系统和 UA 偏好组合确定)除以 16px。
preferred-text-scale 环境变量必须返回 文本缩放因子。
此外,当 text-scale content 属性的值为 scale 时,用户代理 应跳过其为自动尊重用户偏好而可能实施的所有字体大小干预(例如移动设备上的文本自动调整(参见 CSS Size Adjustment 1 § 1 引言)和全应用缩放(例如 Windows 上的流行浏览器会这样做))。
注:
作者预期在样式表中使用
<meta name=,
以便 medium 字号能反映用户的字体偏好(无论该偏好是在操作系统层面还是 UA 层面指定)。
作者之后就可以在页面中使用 rem 来尊重用户的字体偏好。
<!DOCTYPE html> < html > <!-- leave this element’s font-size as the default --> < head > < meta name = "text-scale" content = "scale" /> </ head > < body > < div style = "font-size: 1rem;" > This font size obeys the user’s font preferences, **whether those preferences are specified at the operating system level or the user agent level**</ div > < div style = "font-size: 20px;" > This font size does NOT respect the user’s font preferences.< div style = "font-size: 1rem;" > But this font size does!</ div > </ div > </ body > </ html >
3. 基础字体属性
3.1. 字体家族:font-family 属性
3.1.1. 通用字体家族
除了 CSS Fonts Level 4 中的 CSS Fonts 4 § 2.1.5 通用字体家族 之外,还定义了下列新的通用字体家族。
- xxx
- xxx 通用字体家族的占位文本。
3.2. 字体粗细:font-weight 属性
3.3. 字体样式:font-style 属性
3.4. 相对缩放:font-size-adjust 属性
| Name: | font-size-adjust |
|---|---|
| Value: | none | [ ex-height | cap-height | ch-width | ic-width | ic-height ]? [ from-font | <number [0,∞]> ] |
| Initial: | none |
| Applies to: | 所有元素和文本 |
| Inherited: | 是 |
| Percentages: | 不适用 |
| Computed value: | 关键字 none,或由一个度量关键字与一个 <number> 组成的对 |
| Canonical order: | 按语法 |
| Animation type: | 若关键字不同则为离散,否则按计算值类型 |
对于任意给定字体大小,文本的表观大小和有效可读性会随字体的设计而变化。 例如, 对于区分大小写的双系脚本(如拉丁文或西里尔文), 小写字母相对于大写字母的相对高度 是可读性的决定性因素。 在发生字体回退的情况下, 回退字体的关键排版度量可能与所需字体家族不一致, 因此看起来会是不同的大小, 并且可能不那么易读。
font-size-adjust 属性提供了一种在发生字体回退时 保持文本可读性和表观大小的方法。 它通过调整被使用的字体大小来实现,使得所指定的度量在所用字体之间保持一致。
各值含义如下:
- none
- 不应用特殊的 font-size 调整。
- ex-height | cap-height | ch-width | ic-width | ic-height
-
指定要规范化的字体度量,
默认为 ex-height:
- ex-height
- 规范化字体的 纵横比值, 使用 x-height 除以字体大小。
- cap-height
- 规范化字体的 cap-height, 使用 cap-height 除以字体大小。
- ch-width
- 规范化字体的水平窄间距, 使用“0”(数字零,U+0030)的 advance width 除以字体大小。
- ic-width
- 规范化字体的水平宽间距, 使用“水”(CJK 水表意符,U+6C34)的 advance width 除以字体大小。
- ic-height
- 规范化字体的垂直宽间距, 使用“水”(CJK 水表意符,U+6C34)的 advance height 除以字体大小。
- <number [0,∞]>
-
每个字体的 used 大小被规范化,
使所选择的度量与该 computed
font-size 的指定比例相匹配。
换言之,
对于每个字形,
要使用的调整后字体大小 u
计算为:
u =
( m / m′) s其中:
s = computed
'font-size!!property' value m = metric as specified by the'font-size-adjust' property m′ = metric as specified in the actual font u = adjusted font-size to use负值为无效。
- from-font
- 计算为对应于 <number> 的指定度量,取自 首个可用字体(若存在)。 否则,与 none 相同。
Tests
- font-size-adjust-composition.html (live test) (source)
- font-size-adjust-interpolation.html (live test) (source)
- font-size-adjust-013.html (live test) (source)
- font-size-adjust-014.html (live test) (source)
- font-size-adjust-metrics-override.html (live test) (source)
- font-size-adjust-text-orientation.html (live test) (source)
- font-size-adjust-units-001.html (live test) (source)
- font-size-adjust-computed.html (live test) (source)
- font-size-adjust-invalid.html (live test) (source)
- font-size-adjust-valid.html (live test) (source)
p{ font-family : Verdana, Futura, Times; } p.adj{ font-size-adjust : 0.545 ; } <p>Lorem ipsum dolor sit amet, ...</p> <p class="adj" >Lorem ipsum dolor sit amet, ...</p>
Verdana 的纵横比值相对较高,为 0.545, 意味着小写字母相对于大写字母比较高, 因此在小字号下文本看起来仍然可读。 Times 的纵横比值较低为 0.447, 因此如果发生回退, 文本在小字号下的可读性会比 Verdana 差, 除非也指定了 font-size-adjust。
注:对于使用变音符号的文本, 过大的 x-height 实际上会降低可读性, 因为变音符号会变得拥挤。
下面展示了这些字体各自渲染的对比,列分别显示 Verdana、Futura 和 Times 渲染的文本。 每行中单元格使用相同的 font-size 值,并用红线显示 x-height 的差异。 在上半部分,每行使用相同的 font-size 值。 在下半部分亦然, 但在下半部分 font-size-adjust 也设置为 0.545, 因此实际字体大小被调整以在每行中保持 Verdana 的 x-height。 注意下半部分的小字号在每行中仍相对可读。
font-size-adjust 的值影响 used 值的 font-size,
但不影响 computed 值。
因此它可以影响基于字体度量的相对单位的大小,如 ex 和 ch,
但不影响 em 单位的大小。
由于数值形式的 line-height
指的是 computed 的 font-size,
font-size-adjust 也不会影响
used 的 line-height 的值。
注:由于 font-size-adjust 不参与 line-height 的计算, 指定过紧的行高可能导致文本行重叠。 例如,当回退字体具有较低的 纵横比值 被规范化以匹配具有较高 纵横比值 的字体时, 其上升部和下降部可能会在 line-height: 1 的情况下延伸出行框。
font-size-adjust 调整适用于所选择的任意字体,
但在典型用法中它通常基于 font-family 列表中第一个(最期望的)字体的相应度量值。
如果该值被准确指定,
则调整公式中的 项
对于第一种字体将解析为 而不会发生调整;
其余字体将被调整以匹配。
如果该值指定不准确,
在不支持 font-size-adjust 的旧用户代理中,
使用字体族列表中第一个字体渲染的文本将显示不同的效果。
使用带边框的两个 span 来确定字体的 纵横比值。 两个 span 的 font-size 相同, 但 font-size-adjust 仅在右侧 span 中指定。 从 0.5 开始, 可以调整纵横比值直到两个字母周围的边框对齐。
p{ font-family : Futura; font-size : 500 px ; } span{ border : solid1 px red; } .adjust{ font-size-adjust : 0.5 ; } <p><span>b</span><span class="adjust" >b</span></p>
右侧的框比左侧稍大,因此该字体的 纵横比值 小于 0.5。 调整该值直到框对齐。
注:如果在 @font-face 中覆盖了指定的度量,例如通过 size-adjust, 那么在 font-size-adjust 的计算中将使用被覆盖的度量。 因此,同时应用 font-size-adjust 与 size-adjust 时, 看起来 size-adjust 无效。
4. 字体资源
4.1. @font-face 规则
注: 描述符按每个字体应用而非按每个元素。 单个元素中可以使用多个字体, 例如用于不被 首个可用字体 支持的字符。
4.2. 字体引用:src 描述符
4.2.1. 解析 src 描述符
src 描述符的值必须按照 CSS Syntax 3 § 5.3.11 解析以逗号分隔的组件值列表 来解析。 然后按下列语法解析每个组件值:
<url>[ format ( <font-format>)] ?[ tech ( <font-tech>#)] ? |local ( <family-name>)
<font-format>=[ <string> | collection | embedded-opentype | opentype | svg | truetype | woff | woff2]
<font-tech>=[ <font-features-tech> | <color-font-tech> | variations | palettes | incremental]
<font-features-tech>=[ features-opentype | features-aat | features-graphite]
<color-font-tech>=[ color-COLRv0 | color-COLRv1 | color-SVG | color-sbix | color-CBDT]
如果组件值被正确解析且是受支持的 CSS Fonts 4 § 11.2 字体格式 或 CSS Fonts 4 § 11.1 字体技术, 将其加入受支持来源列表。 如果解析组件值时产生解析错误或其 format 或 tech 不被支持, 则不要将其加入受支持来源列表。
如果在此过程结束时没有受支持的条目, 则 src 描述符的值为解析错误。
这些解析规则允许对不支持特定字体技术或字体格式的用户代理进行平滑回退。
@font-face { font-family : "MyIncrementallyLoadedWebFont" ; src : url ( "FallbackURLForBrowsersWhichDontSupportIncrementalLoading.woff2" ) format ( "woff2" ); src : url ( "MyIncrementallyLoadedWebFont.otf" ) format ( opentype) tech ( incremental); }
4.3. 字体属性描述符:font-size
| Name: | font-size |
|---|---|
| For: | @font-face |
| Value: | auto | [<number>]{1,2} |
| Initial: | auto |
- auto
- 该字体匹配任意字体大小
- <number>
- 如果只给出单个 <number>, 则字体只匹配该特定字体大小。 如果给出两个 <number>, 则它们指定一个字体大小范围,该范围内的字体将匹配。
4.4. 字形尺寸乘数: size-adjust 描述符
| Name: | size-adjust |
|---|---|
| For: | @font-face |
| Value: | <percentage [0,∞]> |
| Initial: | 100% |
size-adjust 描述符定义了一个用于该字体的字形轮廓和度量的乘数, 以允许作者在以相同 font-size 渲染时协调不同字体的设计。
与该字体相关的所有度量——包括字形 advance、基线表,以及由 @font-face 描述符提供的覆盖——以及渲染的字形图像, 都按所给百分比缩放。 因此,任何来自该字体并由字体度量导出的值 (例如 ex 和 ch 单位, 或 text-decoration-thickness 的 from-font 值) 在从该字体获取时也会受到影响。 然而,computed 的 font-size (从而任何由其派生的值,例如 em 单位、text-underline-offset 中的百分比等) 保持不变。
注: size-adjust 描述符的功能类似于 font-size-adjust 属性, 后者实质上通过匹配 ex 高度来为每个字体计算调整,但同样不会影响 computed 的 font-size。
Tests
4.5. 行高字体度量覆盖: ascent-override、descent-override 和 line-gap-override 描述符
| Name: | ascent-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | <percentage [0,∞]> ]{1,2} |
| Initial: | normal |
| Name: | descent-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | <percentage [0,∞]> ]{1,2} |
| Initial: | normal |
| Name: | line-gap-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | <percentage [0,∞]> ]{1,2} |
| Initial: | normal |
ascent-override、descent-override 与 line-gap-override 描述符分别指定字体的 上行度量(ascent metric)、 下行度量(descent metric) 和 行间距度量(line gap metric)。 第一个值为 x 轴的值, 第二个值为 y 轴的值 (若省略则默认为 normal)。
- normal
-
相应的度量值按通常方式从字体中获取,
就好像此描述符不存在于
块中一样。@font-face 注: 由于某些字体格式中此类度量可能有多个来源, 这可能导致不同用户代理/平台之间的文本布局不一致。
- <percentage>
- 相应的度量被替换为给定百分比乘以调整后的有效字体大小。
Tests
font-size-adjust 属性在 size-adjust 描述符之后应用。
注: 在 font-size-adjust 在 size-adjust 之后应用的结果是 size-adjust 看起来没有效果。
注: 这些描述符都不影响 font-size、line-height 或 font-relative lengths 的计算(computation)。 然而,它们可以影响 line-height: normal 的行为, 以及更一般地影响内联级(inline-level)内容的基线对齐。
注: 由于这些度量仅适用于 block axis, y 轴的值仅在垂直排版的 typesetting upright 情况下使用(即垂直排版的排印模式中)。
@font-face { font-family : overridden-font; ascent-override : 50 % ; ...} <span style="font-family: overridden-font; font-size: 20px;" > Outer span content <span style="font-size: 150%;" >Inner span content</span> </span>
外层 span 使用的 ascent 值为 10px,而内层 span 使用的是 15px。
@font-face { font-family : cool-web-font; src : url ( "https://example.com/font.woff" ); } @font-face { font-family : fallback-to-local; src : local ( Some Local Font); /* Override metric values to match cool-web-font */ ascent-override:125 % ; descent-override : 25 % ; line-gap-override : 0 % ; size-adjust : 96 % ; } <div style="font-family: cool-web-font, fallback-to-local" >Title goes here</div> <img src="https://example.com/largeimage" alt="A large image that you don’t want to shift" >
当用户代理加载完成并切换为使用 web 字体时(假设覆盖值与 web 字体的自然度量相似),图像的位移将不会那么明显。
4.6. 上标和下标度量覆盖: superscript-position-override, subscript-position-override,superscript-size-override 和 subscript-size-override 描述符
| Name: | superscript-position-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | from-font | <percentage> ]{1,2} |
| Initial: | normal |
| Name: | subscript-position-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | from-font | <percentage> ]{1,2} |
| Initial: | normal |
| Name: | superscript-size-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | from-font | <percentage [0,∞]> ]{1,2} |
| Initial: | normal |
| Name: | subscript-size-override |
|---|---|
| For: | @font-face |
| Value: | [ normal | from-font | <percentage [0,∞]> ]{1,2} |
| Initial: | normal |
superscript-position-override, subscript-position-override, superscript-size-override 与 subscript-size-override 描述符分别指定字体的上标偏移、下标偏移、上标大小和下标大小度量, 这些度量用于在 font-variant-position 需要时合成字形。 第一个值为 x 轴的值, 第二个值为 y 轴的值 (若省略则默认为第一个值)。
- normal
- 用户代理决定使用哪个度量值, 无论是从字体获取还是通过某种启发式确定。
- from-font
- 使用字体数据中相应的度量(如果存在)。 (若该度量缺失,则与 normal 相同。)
- <percentage>
- 相应的度量被替换为给定百分比乘以 used 字体大小。
注: 由于这些度量仅适用于 块轴, y 轴的值仅在 直立排印 的 垂直排印模式 中使用。
5. 字体特性属性
5.1. 字体语言重写:font-language-override 属性
6. 字体特性和变体解析
7. 字体变体属性
7.1. 光学字号控制:font-optical-sizing 属性
8. 字体技术与格式
8.1. 字体技术(Font tech)
本节扩展了 CSS Fonts 4 § 11.1 字体技术(Font tech) 的内容。
avar2 技术指对 OpenType 轴变体版本 2 的支持 [avar2], 用于更灵活的变体轴重映射。
@font-face { font-family : 'Roboto Flex' ; src : url ( 'RobotoFlex-VF-avar2.woff2' ) format ( woff2) tech ( variations, avar2); src : url ( 'RobotoFlex-VF.woff2' ) format ( woff2) tech ( variations); font-weight : 100 1000 ; font-stretch : 25 % 151 % ; }
9. 对象模型
本节扩展了 CSS Fonts 4 § 12. 对象模型 的内容。
@font-face 与 @font-feature-values 规则的内容 可以通过对 CSS 对象模型的下列扩展来访问。
9.1.
CSSFontFaceRule 接口
CSSFontFaceRule 接口表示一个 <@font-face> 规则。
[Exposed =Window ]interface :CSSFontFaceDescriptors CSSStyleDeclaration {attribute [LegacyNullToEmptyString ]CSSOMString ;src attribute [LegacyNullToEmptyString ]CSSOMString ;fontFamily attribute [LegacyNullToEmptyString ]CSSOMString ;font-family attribute [LegacyNullToEmptyString ]CSSOMString ;fontStyle attribute [LegacyNullToEmptyString ]CSSOMString ;font-style attribute [LegacyNullToEmptyString ]CSSOMString ;fontWeight attribute [LegacyNullToEmptyString ]CSSOMString ;font-weight attribute [LegacyNullToEmptyString ]CSSOMString ;fontStretch attribute [LegacyNullToEmptyString ]CSSOMString ;font-stretch attribute [LegacyNullToEmptyString ]CSSOMString ;fontWidth attribute [LegacyNullToEmptyString ]CSSOMString ;font-width attribute [LegacyNullToEmptyString ]CSSOMString ;fontSize attribute [LegacyNullToEmptyString ]CSSOMString ;font-size attribute [LegacyNullToEmptyString ]CSSOMString ;sizeAdjust attribute [LegacyNullToEmptyString ]CSSOMString ;size-adjust attribute [LegacyNullToEmptyString ]CSSOMString ;unicodeRange attribute [LegacyNullToEmptyString ]CSSOMString ;unicode-range attribute [LegacyNullToEmptyString ]CSSOMString ;fontFeatureSettings attribute [LegacyNullToEmptyString ]CSSOMString ;font-feature-settings attribute [LegacyNullToEmptyString ]CSSOMString ;fontVariationSettings attribute [LegacyNullToEmptyString ]CSSOMString ;font-variation-settings attribute [LegacyNullToEmptyString ]CSSOMString ;fontNamedInstance attribute [LegacyNullToEmptyString ]CSSOMString ;font-named-instance attribute [LegacyNullToEmptyString ]CSSOMString ;fontDisplay attribute [LegacyNullToEmptyString ]CSSOMString ;font-display attribute [LegacyNullToEmptyString ]CSSOMString ;fontLanguageOverride attribute [LegacyNullToEmptyString ]CSSOMString ;font-language-override attribute [LegacyNullToEmptyString ]CSSOMString ;ascentOverride attribute [LegacyNullToEmptyString ]CSSOMString ;ascent-override attribute [LegacyNullToEmptyString ]CSSOMString ;descentOverride attribute [LegacyNullToEmptyString ]CSSOMString ;descent-override attribute [LegacyNullToEmptyString ]CSSOMString ;lineGapOverride attribute [LegacyNullToEmptyString ]CSSOMString ;line-gap-override attribute [LegacyNullToEmptyString ]CSSOMString ;superscriptPositionOverride attribute [LegacyNullToEmptyString ]CSSOMString ;superscript-position-override attribute [LegacyNullToEmptyString ]CSSOMString ;subscriptPositionOverride attribute [LegacyNullToEmptyString ]CSSOMString ;subscript-position-override attribute [LegacyNullToEmptyString ]CSSOMString ;superscriptSizeOverride attribute [LegacyNullToEmptyString ]CSSOMString ;superscript-size-override attribute [LegacyNullToEmptyString ]CSSOMString ;subscriptSizeOverride attribute [LegacyNullToEmptyString ]CSSOMString ; }; [subscript-size-override Exposed =Window ]interface :CSSFontFaceRule CSSRule { [SameObject ,PutForwards =cssText ]readonly attribute CSSFontFaceDescriptors ; };style
10. 安全性注意事项
在本规范中未报告新的安全性注意事项。
11. 隐私注意事项
在本规范中未报告新的隐私注意事项。
12. 致谢
首先,编辑们要感谢所有对本模块先前版本作出贡献的人,见 以前级别的贡献者名单。
其次,我们要感谢以下人士对第 5 级改进所做的贡献: Dave Crossland(Google), Bernhard Fey(RealObjects), Xiaocheng Hu(Google), Jonathan Kew(Mozilla), 以及 Laurence Penney。
13. 修订记录
13.1. 自 2024 年 2 月 6 日 工作草案 以来的变化
- 添加了 text-scale meta 标签和 preferred-text-scale 环境变量支持,附示例 (Issue 12380), (Issue 12475)
- 添加了缺失的 CSSOM superscript-position-override、subscript-position-override、 superscript-size-override 和 subscript-size-override 描述符
- 添加了缺失的 CSSOM size-adjust 和 font-size 描述符
- 添加了 avar2 字体技术 (Issue 10599)
- 将增量示例更新为最新的 IFT 规范 (Issue 6063)
- 明确指出 font-size-adjust 在字体度量覆盖之前应用 (Issue 8967)
13.2. 自 2021 年 12 月 21 日 工作草案 以来的变化
- 修正了 font-size 描述符的初始值
- 为 font-size-adjust 数字的语法设置了范围
- 将 local() 的语法改为使用 family-name 代替 font-face-name
- 更新为当前的增量字体下载关键字
- 将 feature-* 设为复数以与 CSS Conditional 5 和 11.1 Font tech 保持一致
- 在范围表示中将 infinity 规范为 ∞
- 修正了 tech() 函数的笔误
13.3. 自 2021-07-29 工作草案 以来的变化
- 将 technology 重命名为 tech
- 安全性与隐私现在为独立章节
- 修正了 font-technology 语法
- 从 CSS Fonts 4 复制了解析 src 描述符的章节
13.4. 自 2021-06-29 候选发布草案 以来的变化
- 移除了令人担忧的警告通知
- 将增量字体技术添加到 supports 中