检测是否安装了特定字体
如何仅使用 javascript 检测是否安装了特定字体。 (不管是否启用)。
谢谢
How to detect whether or not a particular font is installed using javascript only. (Disregard to whether it is enabled or not).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最后一个答案是11年前提供的。与此同时,对于仍在寻找解决方案的人来说,有新的解决方案可用:
您可以使用
FontFaceSet
浏览器提供的 API。目前它仍是一项实验性技术,但已在大多数浏览器(IE 除外)中可用。来自 MDN Web 文档
示例:
使用
时check()
方法,请确保使用ready
属性,加载完成后返回一个Promise
:The last answer was provided 11 years ago. In the meantime there are new solutions available for people who are still looking for a solution:
You could use the
FontFaceSet
API provided by browsers. It is currently still an experimental technology, but already available in most browsers (except IE).From MDN Web Docs
Example:
When using the
check()
method, make sure to check if font loading operations have been completed by using theready
property, which returns aPromise
when the loading has finished:对于最新的浏览器,window.queryLocalFonts()方法返回一个 Promise,该 Promise 满足表示本地可用字体的 FontData 对象数组。
对于较旧的浏览器,这里有一个解决方案来检查您的设备中的 html 页面是否安装了字体。
这个想法是:创建一个极窄的字体,并使用数据对象 url 在 css 中定义它,用该字体检查目标字体的宽度。
这是示例:
这是测试结果:
For latest browser , the window.queryLocalFonts() method returns a Promise that fulfills with an array of FontData objects representing the font faces available locally.
for older browser, here is a solution to check if font is installed in your device in a html page.
The idea is :create a extreme narrow font and define it in css with data object url, check target font's width with this font.
Here is the example:
Here is the test result:
将此代码放在 javascript 文件中的任意位置。
使用
来源
https://www.samclarke.com/javascript-is-font-available< /a>
Put this code anywhere in your javascript file.
Usage
Source
https://www.samclarke.com/javascript-is-font-available
@Matchu 正确地建议了您,但这就是您正在寻找的内容:
http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/< /a>
@Matchu suggested you rightly but here is what you are looking for:
http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/
如果您绝对需要特定字体,则所有访问者都必须拥有最新的浏览器。然后你就可以使用网络字体了。
如果这不是一个选项,您必须在服务器上渲染字体并提供图像。
If you absolutely need a specific font, all your visitors must have current browsers. Then you can use webfonts.
If that's not an option, you must render the font on your server and serve an image.
字体系列声明应该足以在您要使用的字体不存在时提供后备。
但是,如果您确实需要在网站中使用特殊字体,尽管这使用 JavaScript,但我建议您使用 Cufon< /a> 因为它是最跨浏览器的解决方案 - 有一个 CSS3 修复(字体声明),但它还不能在所有浏览器中工作。
Font family declarations should suffice to provide a fallback in case the font you want to use is not present.
But if you really need to use a special font in your site, although this uses JavaScript, I'd recommend Cufon since it's the most cross-browser solution out there - there is a CSS3 fix (font-face declarations), but it doesn't work in all browsers yet.
您是否可以像世界其他地方一样:使用 CSS 指定字体,并提供后备方案?任何应用程序都不应该需要知道可用的字体,也没有可靠的方法来做到这一点。 (您可能必须使用字体打印隐藏的 div 并测量它以查看尺寸是否符合您的预期,但这会非常脆弱。)
只需:
如果您想对除显示之外的字体进行任何操作如果可能的话,请考虑替代解决方案。
How about you just do what the rest of the world does: specify the font with CSS, and offer fallbacks? No app should ever need to know what fonts are available, nor is there a reliable way of doing so. (You would probably have to print hidden div with the font and measure it to see if the dimensions are what you expected, but that would be extremely brittle.)
Just go for:
If you want to be doing anything with the font other than display things in it if possible, consider an alternative solution.