javascript 如何使用javascript迭代已安装的字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3597682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate the installed fonts using javascript?
提问by xport
How to iterate the installed fonts using javascript?
如何使用javascript迭代已安装的字体?
回答by mauris
To start off, you might want to check what fonts are installed on the client. Read up on http://www.lalit.org/lab/javascript-css-font-detect
首先,您可能需要检查客户端上安装了哪些字体。阅读http://www.lalit.org/lab/javascript-css-font-detect
You need to have your own list of fonts to check, then you have an array of installed fonts by checking each of the list to see which one is installed.
您需要有自己的字体列表来检查,然后通过检查每个列表来查看已安装的字体,从而获得一系列已安装的字体。
The difference in widths will tell you the availability of the fonts installed on the client's computers because the browser will fall back to its default font. So you probably need to do some invisible testing for text widths to determine if a font is installed.
宽度的差异将告诉您安装在客户端计算机上的字体的可用性,因为浏览器将回退到其默认字体。因此,您可能需要对文本宽度进行一些不可见的测试,以确定是否安装了字体。
回答by Kris van der Mast
Javascript's sandboxed inside the browser and doesn't have privileges to read from the clients disk for security reasons.
Javascript 在浏览器中被沙箱化,出于安全原因没有从客户端磁盘读取的权限。
However people tried making some workarounds like http://www.lalit.org/lab/javascript-css-font-detector http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/.
但是人们尝试了一些变通方法,例如http://www.lalit.org/lab/javascript-css-font-detect或http://remysharp.com/2008/07/08/how-to-detect-if-a -font-is-installed-only-using-javascript/。
回答by Kundan Atre
This code works for IE
此代码适用于 IE
<html>
<head>
<script type="text/javascript">
<!--
function getFonts() {
// get list of fonts, and sort alphabetically
var allFonts = [];
for (var loop = 1; loop < dlgHelper.fonts.count + 1; loop++) allFonts[loop - 1] = dlgHelper.fonts(loop);
allFonts.sort();
// create output list, and include samples of each font
var outputStr = '';
var fontTestString = 'ABC abc 123';
for (var loop = 0; loop < allFonts.length; loop++) {
outputStr += '<span style="font-family: ' + allFonts[loop] + ';">' + allFonts[loop] + '</span><br />\n';
}
document.getElementById('fontList').innerHTML = outputStr;
}
//-->
</script>
</head>
<body onload="getFonts();">
<object id="dlgHelper" classid="clsid:3050F819-98B5-11CF-BB82-00AA00BDCE0B" width="0px"
height="0px">
</object>
<div id="fontList">
</div>
</body>
</html>
回答by g.d.d.c
There's not a way that I'm aware of. There are system APIs in languages like C++ and Python that will return the installed fonts, and you can certainly write a backend in a higher level language that communicates with a JavaScript front end using get/post requests and (optionally) AJAX, but you're not going to get the installed fonts with only JavaScript.
没有我知道的方法。有 C++ 和 Python 等语言的系统 API 将返回已安装的字体,您当然可以使用更高级别的语言编写后端,该后端使用 get/post 请求和(可选)AJAX 与 JavaScript 前端通信,但是您不会仅使用 JavaScript 来获取已安装的字体。

