Web Font API 快速入门指南

网络字体 API 快速入门指南

什么是字体API?

| Web字体服务 | 托管库(例如Google Fonts、Adobe Fonts),提供CSS <link> 或JavaScript代码片段,用于从CDN获取字体文件。 | | CSS@font-face | 底层浏览器API,用于告知用户代理从何处下载字体文件以及要向CSS公开哪个字体系列名称。 | | 可变字体API | 较新的语法(font-variation-settings、轴范围),允许单个文件公开多种样式(粗细、宽度、倾斜度等)。 | | 字体显示描述符 | 控制字体加载期间文本的渲染方式(autoblockswapfallbackoptional)。 | | JavaScript加载辅助函数 |用于动态加载或预加载字体的可选辅助函数(FontFace 构造函数、document.fonts.load())。 |

所有这些都属于“字体 API”家族,因为它们公开了请求和使用字体的“编程”方式。

核心 CSS 术语

属性/关键字含义
font-family字体加载后在 CSS 中使用的名称。
src实际字体文件的 URL(.woff2.woff.ttf 等)。
unicode-range给定文件涵盖的字符子集 – 非常适合子集化
font-weight / font-style / font-stretch经典的静态描述符。
font-variation-settings用于可变轴的键值对('wght' 400)。
font-display字体加载期间的渲染备用策略。

使用内置 CSS@font‑face(自托管)

 1<!-- 1️ Put the font files on your server (or CDN) -->
 2<link rel="preload" href="/fonts/Inter-Variable.woff2"
 3      as="font" type="font/woff2" crossorigin>
 4
 5<style>
 6  /* 2️⃣ Declare the family */
 7  @font-face {
 8    font-family: 'Inter Variable';
 9    src: url('/fonts/Inter-Variable.woff2') format('woff2');
10    /* Optional – load only Latin characters */
11    unicode-range: U+0000-00FF;
12    /* Show fallback text until the font is ready */
13    font-display: swap;
14  }
15
16  /* 3️⃣ Use it */
17  body {
18    font-family: 'Inter Variable', system-ui, sans-serif;
19  }
20</style>

优点:完全控制缓存、许可和子集设置;如果您捆绑文件,则可以离线工作。

Googlefonts CSSAPI(最常见的公共字体 API)

1<link href="https://fonts.googleapis.com/css2?
2family=Roboto:wght@400;700&
3family=Open+Sans:ital,wght@0,400;0,600;1,400&
4display=swap"
5rel="stylesheet">

说明

部分功能
family=Roboto:wght@400;700仅请求 Roboto 字体的常规和粗体字重。
family=Open+Sans:ital,wght@0,400;0,600;1,400加载 OpenSans 字体的常规/半粗体 Roman 字重和常规斜体。
display=swap在字体加载完成之前使用备用文本(推荐用于用户体验)。

然后在CSS中:

1h1 { font-family: 'Roboto', sans-serif; }
2p  { font-family: 'Open Sans', sans-serif; }

提示

可变字体示例 (Googlefonts)

 1<link href="https://fonts.googleapis.com/css2?
 2family=Inter:wght@200..800&display=swap"
 3rel="stylesheet">
 4
 5<style>
 6  .hero {
 7    font-family: 'Inter', system-ui;
 8    /* Animate weight from thin → bold */
 9    font-variation-settings: 'wght' 200;
10    transition: font-variation-settings .6s ease;
11  }
12  .hero:hover { font-variation-settings: 'wght' 800; }
13</style>

为什么重要:一个文件取代了数十个静态“.woff2”→ 有效负载大大减少。

JavaScript 字体加载 API (FontFace)

何时使用 – 当您需要在页面加载后加载字体(例如,在 SPA 路由中)或希望在显示 UI 之前等待字体准备就绪时。

 1// Create a FontFace object
 2const myFont = new FontFace(
 3  'Custom Serif',
 4  'url(/fonts/CustomSerif-VF.woff2) format("woff2")',
 5  { weight: '400 900', style: 'normal' }   // optional descriptors
 6);
 7
 8// Add it to the document’s FontFaceSet
 9document.fonts.add(myFont);
10
11// Start loading
12myFont.load().then(() => {
13  // Font ready → apply it
14  document.body.style.fontFamily = '"Custom Serif", serif';
15}).catch(err => console.error('Font failed', err));

您还可以查询加载了哪些字体:

1document.fonts.ready.then(() => console.log('All fonts ready'));

AdobeFonts (Typekit) – 另一个公共字体 API

1<link rel="stylesheet" href="https://use.typekit.net/xyz123.css">

然后参考套件中列出的完全一样的系列:

1h2 { font-family: "adobe-caslon-pro", serif; }

与 Googlefonts 的主要区别

性能最佳实践

  1. 仅需要字形子集 – 使用“unicode-range”或 Google Fonts“text=”参数。

  2. **利用关键字体的“预加载”:

1<link rel="preload" href="/fonts/Inter-Variable.woff2"
2      as="font" type="font/woff2" crossorigin>
  1. **将“font-display”设置为至少“swap”。
    避免“阻塞”文本渲染,这会损害 CLS(累积布局偏移)。

  2. 使用 GoogleFonts 或 AdobeFonts 时,将系列合并到一个请求中。

  3. 永久缓存 – 大多数 CDN 都会发送 woff2 文件的远期“Cache-Control”标头。

快速参考备忘单

API如何请求典型用例
CSS @font-face<style>@font-face{…}</style> + 可选的 <link rel="preload">自托管字体,完全控制
Google Fonts CSS API<link href="https://fonts.googleapis.com/css2?...">带有简单 URL 参数的免费字体
Adobe Fonts (Typekit)<link href="https://use.typekit.net/xxxx.css">付费字体库,集成设计系统
FontFace JS APInew FontFace(name, src[, descriptors])在 SPA/运行时包中动态加载
可变字体轴范围family=Inter:wght@200..800 或 CSS font-variation-settings减少文件数量并实现平滑的粗细过渡

TL;DR 代码示例 – 适用于大多数项目的单行代码

1<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap"
2      rel="stylesheet">
3<style>
4  body {font-family:'Montserrat',Arial,sans-serif;}
5</style>

✅ 仅加载常规和粗体字重 → 下载量极小。 ✅ display=swap 保证快速切换回原始文本 → 良好的用户体验。 ✅ 无需额外 JavaScript 即可在所有现代浏览器上运行。


阿斯珀怎么样?

如果您需要在服务器端操作或转换字体文件(例如,生成子集、在 TTF/WOFF2 之间转换或提取字形度量),Aspose.Font 库为 .NET、Java 和 Python 提供编程 API。它可以集成到构建管道或后端服务中,以在通过上述任何字体 API 提供字体资源之前准备好字体资源。


准备好嵌入了吗?

将上面的任何片段复制到您的 HTML/CSS/JS 中,您将在几秒钟内获得可用于生产的 Font API 集成。快乐造型!

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.