Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial translation for Local font access api #17161

Merged
merged 36 commits into from
Nov 30, 2023
Merged
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b2fa2ef
Create index.md
skyclouds2001 May 24, 2023
466f5ab
Merge branch 'mdn:main' into main
skyclouds2001 May 24, 2023
60d3d7e
Update index.md
skyclouds2001 May 24, 2023
8c7f938
Rename index.md to index.md
skyclouds2001 May 24, 2023
3ddfec1
Merge branch 'main' into main
skyclouds2001 May 24, 2023
83420ff
Merge branch 'main' into main
skyclouds2001 May 25, 2023
4b29750
Merge branch 'mdn:main' into main
skyclouds2001 May 26, 2023
1982318
Merge branch 'main' into main
skyclouds2001 May 28, 2023
9d67f3a
Merge branch 'mdn:main' into main
skyclouds2001 May 28, 2023
c9f9274
Merge branch 'main' into main
skyclouds2001 May 28, 2023
24d32a2
Update files/zh-cn/web/api/notification/maxactions_static/index.md
skyclouds2001 May 29, 2023
21b0517
Update files/zh-cn/web/api/notification/maxactions_static/index.md
skyclouds2001 May 29, 2023
45b4d51
Update files/zh-cn/web/api/notification/maxactions_static/index.md
skyclouds2001 May 29, 2023
86eb8fb
Update files/zh-cn/web/api/notification/maxactions_static/index.md
skyclouds2001 May 29, 2023
807f36b
Update index.md
yin1999 May 29, 2023
02a2cb2
Merge branch 'mdn:main' into main
skyclouds2001 Jun 2, 2023
167ff4e
Merge branch 'mdn:main' into main
skyclouds2001 Jun 10, 2023
42262cd
Merge branch 'mdn:main' into main
skyclouds2001 Jun 15, 2023
81d1ca2
Merge branch 'mdn:main' into main
skyclouds2001 Jul 5, 2023
c0a28c7
Merge branch 'mdn:main' into main
skyclouds2001 Aug 30, 2023
93c3f13
Merge branch 'mdn:main' into main
skyclouds2001 Sep 21, 2023
9679641
Merge branch 'mdn:main' into main
skyclouds2001 Oct 9, 2023
ff89172
Merge branch 'mdn:main' into main
skyclouds2001 Nov 2, 2023
b413003
Merge branch 'mdn:main' into main
skyclouds2001 Nov 3, 2023
5bdfab6
Merge branch 'mdn:main' into main
skyclouds2001 Nov 8, 2023
9327d38
Merge branch 'mdn:main' into main
skyclouds2001 Nov 10, 2023
26600c5
Merge branch 'mdn:main' into main
skyclouds2001 Nov 14, 2023
db4da4e
Merge branch 'mdn:main' into main
skyclouds2001 Nov 16, 2023
079b2a4
Merge branch 'mdn:main' into main
skyclouds2001 Nov 17, 2023
3ac6885
Merge branch 'mdn:main' into main
skyclouds2001 Nov 25, 2023
f80c6d3
initial
skyclouds2001 Nov 28, 2023
e72de41
Merge branch 'main' into Local-Font-Access-API
skyclouds2001 Nov 28, 2023
ca234b2
small update
skyclouds2001 Nov 29, 2023
031d74d
Update files/zh-cn/web/api/local_font_access_api/index.md
skyclouds2001 Nov 29, 2023
2353373
Apply suggestions from code review
skyclouds2001 Nov 30, 2023
3a6299c
Update files/zh-cn/web/api/local_font_access_api/index.md
yin1999 Nov 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions files/zh-cn/web/api/local_font_access_api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: Local Font Access API
slug: Web/API/Local_Font_Access_API
l10n:
sourceCommit: a3c8e936784a6dc4a5c7884ec97164e4e055a5ea
---

{{SeeCompatTable}}{{DefaultAPISidebar("Local Font Access API")}}

**Local Font Access API** 提供了一种访问用户本地安装的字体数据的机制——这包括更高级别的详细信息,例如名称、样式和系列,以及底层字体文件的原始字节内容。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

## 概念和用法

[Web fonts](/zh-CN/docs/Learn/CSS/Styling_text/Web_fonts) 通过允许 Web 设计人员提供在网页文档上使用的自定义字体,在实现 Web 排版方面具有革命性意义。通过 {{cssxref("@font-face")}} at-规则,网络字体可以通过 `url()` 函数中提供的 URL 加载。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

`@font-face` 还有其他几个有用的功能可用。特别是,你还可以在 `local()` 函数中指定字体的完整名称或 Postscript 名称,以告诉浏览器在用户计算机上安装了该字体时使用本地副本。这并非没有问题——`local()` 作为[指纹向量](https://developer.chrome.com/articles/local-fonts/#local-fonts-as-fingerprint-vector)已经变得臭名昭著。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

此外,由于准确的字体枚举和访问低级字体数据(例如,应用过滤器和转换)方面的挑战,高端设计工具历来难以在网络上提供。当前的应用程序通常依赖于解决方法,例如要求用户将字体上传到服务器,在服务器上处理字体以获取原始字节数据,或者安装单独的本地程序以提供附加功能。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

Local Font Access API 的创建就是为了解决这些问题。

{{domxref("Window.queryLocalFonts()")}} 方法提供对本地安装的字体的数组的访问,每个字体都由一个 {{domxref("FontData")}} 对象实例表示。{{domxref("FontData")}} 有多个属性,提供对名称、样式和字体族的访问,并且它还有一个 {{domxref("FontData.blob", "blob()")}} 方法,提供对包含底层字体文件的原始字节内容的 {{domxref("Blob")}} 的访问。

在隐私和安全方面:

- 本地字体访问 API 旨在仅提供对解决上述问题所需的数据的访问。它既不要求浏览器提供可用本地字体的完整列表,也不要求按照磁盘上显示的顺序提供数据。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
- 当调用 {{domxref("Window.queryLocalFonts()")}} 时,系统会请求用户授予访问其本地字体的权限。此权限的状态可以通过 {{domxref("Permissions API")}}(`local-fonts` 权限)查询。
- 你可以使用 {{httpheader("Permissions-Policy/local-fonts", "local-fonts")}} [权限策略](/zh-CN/docs/Web/HTTP/Permissions_Policy) 控制对此功能的访问。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

## 接口

- {{domxref("FontData")}}
- : 代表单个本地字体。

## 其他接口的扩展

- {{domxref("Window.queryLocalFonts()")}}
- : 返回一个 {{jsxref("Promise")}},它成功完成时返回表示本地可用字体的 {{domxref("FontData")}} 对象的数组。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

## 示例

有关实时演示,请参阅[字体选择演示](https://local-font-access.glitch.me/demo/)。

### 特征检测
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

```js
if ("queryLocalFonts" in window) {
// 支持本地字体访问 API
}
```

### 字体枚举

以下代码片段将查询所有可用字体并打印元数据。例如,这可以用于填充字体选择器控件。

```js
async function logFontData() {
try {
const availableFonts = await window.queryLocalFonts();
for (const fontData of availableFonts) {
console.log(fontData.postscriptName);
console.log(fontData.fullName);
console.log(fontData.family);
console.log(fontData.style);
}
} catch (err) {
console.error(err.name, err.message);
}
}
```

### 访问低级数据
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

{{domxref("FontData.blob", "blob()")}} 方法提供对低级 [SFNT](https://en.wikipedia.org/wiki/SFNT) 数据的访问——这是一种可以包含其他字体格式的字体文件格式,例如 PostScript、TrueType、OpenType 或 Web 开放字体格式(WOFF)。
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

```js
async function computeOutlineFormat() {
try {
const availableFonts = await window.queryLocalFonts({
postscriptNames: ["ComicSansMS"],
});
for (const fontData of availableFonts) {
// `blob()` 方法返回一个包含有效且完整的 SFNT 包装字体数据的 Blob。
const sfnt = await fontData.blob();
// 仅裁剪出我们需要的字节部分:前 4 个字节是 SFNT 版本信息。
// 标准:https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved
const sfntVersion = await sfnt.slice(0, 4).text();

let outlineFormat = "UNKNOWN";
switch (sfntVersion) {
case "\x00\x01\x00\x00":
case "true":
case "typ1":
outlineFormat = "truetype";
break;
case "OTTO":
outlineFormat = "cff";
break;
}
console.log("Outline format:", outlineFormat);
}
} catch (err) {
console.error(err.name, err.message);
}
}
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [使用带有本地字体的高级排版](https://developer.chrome.com/articles/local-fonts/)
- {{cssxref("@font-face")}}
- {{httpheader("Permissions-Policy/local-fonts", "local-fonts")}} [权限策略](/zh-CN/docs/Web/HTTP/Permissions_Policy) 指令
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved