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 開発では、多くの場合、カスタマイズ可能なブラウジング エクスペリエンスをユーザーに提供することが望まれます。これには、ブラウザが表示できるフォントのリストから好みのフォントを選択できるようにすることが含まれます。これを促進するには、利用可能なすべてのフォントのリストをプログラムで取得する方法が必要です。

この問題を解決する 1 つのアプローチは、フォントのリストをハードコードするか、サーバーから取得することです。ただし、このアプローチは面倒な場合があり、ユーザーがブラウザに追加のフォントをインストールしている状況には対応できない可能性があります。

幸いなことに、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>
ログイン後にコピー

このコードは、「detect」メソッドを使用して「Detector」オブジェクトを作成します。 「detect」メソッドは引数としてフォント名を受け取り、そのフォントがブラウザでレンダリングできるかどうかを示すブール値を返します。

このコードは、 を作成することで機能します。要素を削除し、その font-family を指定されたフォントに設定します。この要素はドキュメント本文に追加されます。次に、要素の幅と高さが測定され、「等幅」、「サンセリフ」、「セリフ」の 3 つのフォントのデフォルト値と比較されます。測定値がデフォルト値と異なる場合は、指定されたフォントが使用可能であることを示します。

使用可能なフォントをすべて列挙するには、フォント名の配列を反復処理し、各フォントの「detect」メソッドを呼び出すだけです。 。結果は、ブラウザがレンダリングできるすべてのフォントのリストになります。

以上がJavaScript を使用して Web ブラウザのレンダリング エンジンで利用可能なフォントを列挙する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!