我的应用程序中有一个文本“name”,我希望使用安装在用户PC(本地)上的字体来设置样式,我将使用像filesystemobject这样的函数来获取。这样的事情(这只是一个指示性代码,只能获取文件名。)
<html>
<head>
<script>
function ShowFolderFileList()
{
var fso, f, f1, fc, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder("My Folder");
fc = new Enumerator(f.files);
s = "";
for (; !fc.atEnd(); fc.moveNext())
{
s += fc.item();
s += "<br>";
}
document.write(s);
}
</script>
</head>
<body>
<form name="myForm">
<input type="Button" value="Get File" onClick='ShowFolderFileList'>
</form>
</body>
</html>
我尽可能地尝试并解释了我的问题。如果需要更多细节,请发表评论我将提供有关您的查询的更多信息。
答案 0 :(得分:0)
您可以轻松访问用户导航器中嵌入的字体,称为“Web安全字体”。这些经典字体应该是广泛可用的跨操作系统。完整的“网络安全字体”列表可通过谷歌获得:)
要使用其中一个,只需使用CSS规则,例如: p {font-family:“Times New Roman”;}
我确定您期望更多特定字体,据我所知,您无法通过javascript列出用户PC上安装的字体。可能需要Flash。但是,我发现这段代码可以说是否在用户计算机上安装了字体,这可能符合您的目标。
/**
* JavaScript code to detect available availability of a
* particular font in a browser using JavaScript and CSS.
*
* Author : Lalit Patel
* Website: http://www.lalit.org/lab/javascript-css-font-detect/
* License: Apache Software License 2.0
* http://www.apache.org/licenses/LICENSE-2.0
* Version: 0.15 (21 Sep 2009)
* Changed comparision font to default from sans-default-default,
* as in FF3.0 font of child element didn't fallback
* to parent element if the font is missing.
* Version: 0.2 (04 Mar 2012)
* Comparing font against all the 3 generic font families ie,
* 'monospace', 'sans-serif' and 'sans'. If it doesn't match all 3
* then that font is 100% not available in the system
* Version: 0.3 (24 Mar 2012)
* Replaced sans with serif in the list of baseFonts
*/
/**
* Usage: d = new Detector();
* d.detect('font name');
*/
var Detector = function() {
// a font will be compared against all the three default fonts.
// and if it doesn't match all 3 then that font is not available.
var baseFonts = ['monospace', 'sans-serif', 'serif'];
//we use m or w because these two characters take up the maximum width.
// And we use a LLi so that the same matching fonts can get separated
var testString = "mmmmmmmmmmlli";
//we test using 72px font size, we may use any size. I guess larger the better.
var testSize = '72px';
var h = document.getElementsByTagName("body")[0];
// create a SPAN in the document to get the width of the text we use to test
var s = document.createElement("span");
s.style.fontSize = testSize;
s.innerHTML = testString;
var defaultWidth = {};
var defaultHeight = {};
for (var index in baseFonts) {
//get the default width for the three base fonts
s.style.fontFamily = baseFonts[index];
h.appendChild(s);
defaultWidth[baseFonts[index]] = s.offsetWidth; //width for the default font
defaultHeight[baseFonts[index]] = s.offsetHeight; //height for the defualt font
h.removeChild(s);
}
function detect(font) {
var detected = false;
for (var index in baseFonts) {
s.style.fontFamily = font + ',' + baseFonts[index]; // name of the font along with the base font for fallback.
h.appendChild(s);
var matched = (s.offsetWidth != defaultWidth[baseFonts[index]] || s.offsetHeight != defaultHeight[baseFonts[index]]);
h.removeChild(s);
detected = detected || matched;
}
return detected;
}
this.detect = detect;
};