首页 > web前端 > css教程 > 正文

如何使用 JavaScript 枚举 Web 浏览器渲染引擎可用的字体?

Mary-Kate Olsen
发布: 2024-10-23 13:44:01
原创
943 人浏览过

How to Enumerate Fonts Available to a Web Browser's Rendering Engine using JavaScript?

枚举可用于 Web 浏览器渲染引擎的字体

在 Web 开发中,通常希望为用户提供可自定义的浏览体验。这可以包括允许他们从浏览器可以呈现的字体列表中选择自己选择的字体。为了实现这一点,有必要有一种方法以编程方式获取所有可用字体的列表。

解决此问题的一种方法是硬编码字体列表或从服务器检索它。然而,这种方法可能很麻烦,并且可能无法满足浏览器安装了用户附加字体的情况。

幸运的是,存在一种使用 JavaScript 的更优雅和动态的解决方案。以下代码片段利用了一种称为字体检测的技术:

<code class="js">/**
 * 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 = &quot;mmmmmmmmmmlli&quot;;

    //we test using 72px font size, we may use any size. I guess larger the better.
    var testSize = '72px';

    var h = document.getElementsByTagName(&quot;body&quot;)[0];

    // create a SPAN in the document to get the width of the text we use to test
    var s = document.createElement(&quot;span&quot;);
    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;
};</code>
登录后复制

此代码使用“检测”方法创建一个“检测器”对象。 “检测”方法采用字体名称作为参数,并返回一个布尔值,指示该字体是否可用于浏览器渲染。

代码通过创建一个 来工作。元素并将其 font-family 设置为指定字体。然后将该元素附加到文档正文。然后测量元素的宽度和高度,并将其与三种“monospace”、“sans-serif”和“serif”字体的默认值进行比较。如果测量值与默认值不同,则表明指定的字体可用。

要枚举所有可用字体,只需迭代字体名称数组并为每种字体调用“检测”方法。结果将是浏览器可以呈现的所有字体的列表。

以上是如何使用 JavaScript 枚举 Web 浏览器渲染引擎可用的字体?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!