javascript 有没有办法获取浏览器可以显示的所有字体(或字体系列)的名称? (我想为用户提供一个包含所有可用字体列表的下拉菜单,并允许用户选择一种字体。)
我不想提前对此列表进行硬编码或从服务器发送下来。 (直观上,浏览器似乎应该知道它有什么字体,并且应该以某种方式暴露给 JavaScript。)
Is there a way in javascript to obtain the names of all fonts (or font-families) that the browser can show? (I want to give the user a dropdown with a list of all available fonts, and allow the user to choose a font.)
I'd prefer not to have to hardcode this list ahead of time or send it down from the server. (Intuitively, it seems like the browser should know what fonts it has and this should be exposed to javascript somehow.)
发布评论
评论(12)
2024 更新
下面详细介绍的
FontFaceSet.check()
现在似乎在 Chrome 上已“修复”,因此不再有效。Windows 10 字体列表
macOS/iOS 字体列表
FontFaceSet.check() 解决方案FontFaceSet.check()
2024 Update
The
FontFaceSet.check()
detailed below appears to now be "fixed" on Chrome now so no longer works.Windows 10 Font List
macOS/iOS Font List
FontFaceSet.check() solutionFontFaceSet.check()
有一种方法可以使用
document.fonts 来做到这一点
。返回值是文档的FontFaceSet接口。 FontFaceSet 接口对于加载新字体、检查以前加载的字体的状态等很有用。
仅返回我测试过的 ,没有链接 HTML 中的任何字体,然后链接Roboto 字体,再次测试,它被添加到结果中。
There is a way to do this using
document.fonts
.The returned value is the FontFaceSet interface of the document. The FontFaceSet interface is useful for loading new fonts, checking the status of previously loaded fonts, etc.
I have tested it without linking any fonts in the HTML, then linked Roboto font, tested again and it got added to the result.
JavaScript 版本有点不稳定。它通过迭代已知字体和测试来获取字体。
最准确的方法(尽管必须使用专有插件)是 使用 Flash。在这里,您可以获得字体列表,而无需使用尺寸单独测试它们。
您必须决定是否要有一个准确的列表,但代价是无法在某些设备上工作(iDevices、没有 Flash 插件的浏览器等),或者 仅通过 JavaScript 提供更好支持的部分列表。
The JavaScript version is a bit flaky. It gets fonts by iterating through known fonts and testing.
The most accurate way (albeit having to use a proprietary plugin) is to use Flash. Here you can get the list of fonts without having to test for them individually using dimensions.
You are going have to decide whether to have an exact list at the expense of not working on some devices (iDevices, browsers without Flash plugin, etc), or a partial list with better support via JavaScript only.
您可以使用新的本地字体访问 API 枚举所有字体:
您还可以检查是否用户已授予权限:
You can use the new Local Font Access API to enumerate through all of the fonts:
You can also check whether the user has granted permission:
简短的回答是。 2020 年浏览器中的字体检测方面没有太大变化,除了使用 Flash 现在已成为更糟糕的想法。
目前没有浏览器本机系统可以“列出”所有可用字体。这样做是为了避免浏览器指纹识别及其安全隐患。
但是,浏览器会让您使用 FontFaceSet API。它在现代浏览器中得到了很好的支持。
这是为了显示网络字体是否已完全下载,但它也适用于系统字体。问题是您必须提供要检查的字体列表。
因此,结合
用户代理
测试(并不总是准确) ,您可以为每种设备类型生成常用系统字体列表。然后针对这些字体和您加载的任何网络字体进行测试。注意:这不会为您提供可用字体的完整列表,但您可以检查 MS Office 或 Adobe 产品通常安装的字体。
The short answer is. Not much has changed regarding fonts detection in browsers in 2020 except that using Flash is now an even worse idea.
There's currently no browser native system to "list" all available fonts. This is intentional to avoid browser fingerprinting and its security implications.
However, browsers will let you check if a font is loaded/ready using the FontFaceSet API. It's pretty well supported in modern browsers.
This is intended to show if a web font is completely downloaded BUT it will work for system fonts as well. The catch is you have to provide a list of fonts to check.
So in conjunction with a
user agent
test (not always accurate), you could produce a list of common system fonts for each device type. Then test against those and any web fonts you load.NOTE: This will NOT give you a full list of available fonts, but you can check for fonts commonly installed by MS Office or Adobe products.
Chrome 87 中提供了字体访问 API:
详细信息这里
Font access API is available in Chrome 87:
More info here
我在上面的 Lalit Patel 检测器中添加了两个方法:
您可以这样做:
代码:
I added two methods to Lalit Patel's Detector above :
With this you can do:
code:
在我的搜索中,我还发现了 Font.js,它添加了一个与 Image 非常相似的 Font 对象,因此可以检查字体何时真正可以使用。也适用于已安装/系统字体。缺点是 IE9+ 只是因为需要
Object.defineProperty
(其他浏览器也有),但如果您正在做现代网络,这似乎是一个更好的选择。 (遗憾的是,我必须接受上面的答案,投票并继续前进。:))In my search for this, I also found Font.js, which adds a Font object much like Image, so it's possible to check when a font is actually ready to use. Also works on installed/system fonts. Downside is IE9+ only due to needing
Object.defineProperty
(other browsers have it), but if you are doing modern web, this seems like an even better option. (I will, sadly, have to go with the answer above, upvoted and moving on for now. :))也许这可以通过完全不同的方式来完成,使用带有特定字符的已知字体图像的精灵表,并将其与画布元素的快照进行比较,在画布元素上绘制相同的字符,并使用浏览器报告为相同的字体。可以使用 resemble.js 之类的内容进行比较。
这比较慢,但也应该允许我们检测浏览器何时撒谎。
Maybe this could be done in a completely different way, using a spritesheet with known font images for a specific character and comparing that with snapshots of a canvas element on which the same character is drawn with what the browser reports as the same font. The comparison may be done with something like resemble.js.
This is slower, but should also allow us to detect when the browser is lying.
我最近注意到,如果我将 HTML5 画布的 context.font 值设置为无效的值,例如“垃圾”,则画布会忽略更改。我不知道这是否是特定于浏览器的,但它似乎在 Chrome 上以这种方式工作。我还看到其他帖子(HTML 5 canvas 字体被忽略)表明它发生在其他浏览器中。
然后可以用默认值写出一个字符串,我相信它是“10px sans serif”(https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/font),将字体设置为您正在测试的字体并再次写入字符串。如果与第一张图相同,则该字体不可用。
I have recently noticed that if I set the context.font value for an HTML5 canvas, to something invalid, such as "junk", the change is ignored by the canvas. I do not know if this is browser specific, but it seems to work this way on Chrome. I have also seen other posts (HTML 5 canvas font being ignored) that indicate it happens in other browsers.
One could then write a string out with the default value, which I believe is "10px sans serif" (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/font), set the font to one you are testing and write the string again. If it is the same as the first drawing, then the font is not available.
是的,有!我很高兴你问这个问题,因为我现在也想使用这个。
http://www.lalit.org/lab/javascript-css-font-检测
代码来自http://www.lalit.org/wordpress/wp-content/uploads/2008/05/fontdetect.js?ver=0.3
摘要
Yes there is! I'm so glad you asked this question because I now want to use this too.
http://www.lalit.org/lab/javascript-css-font-detect
Code from http://www.lalit.org/wordpress/wp-content/uploads/2008/05/fontdetect.js?ver=0.3
Summary