简体   繁体   English

随后如何加载和覆盖Google Web字体?

[英]How to load and override Google Web Fonts subsequently?

After loading one set of Google Web Fonts, how can you load a new set to override the previously loaded set (assuming the elements making use of the old fonts are going to point to the newer font-families). 加载一组Google Web字体后,如何加载新的集以覆盖以前加载的集(假设利用旧字体的元素将指向较新的字体家族)。

The Goal is to provide a way for a designer to choose any Google Web Fonts by copying one or more identifiers ( Name:Size:Charset ) in a Game Configuration popup (this interacts with the game at runtime, letting the user tweak various parameters). 目标是通过复制“游戏配置”弹出窗口中的一个或多个标识符( Name:Size:Charset ),为设计人员提供一种选择任何Google Web字体的方式(此标识符在运行时与游戏交互,让用户调整各种参数) 。

Example: 例:

  1. Load Lato:900:latin . 加载Lato:900:latin
  2. Wait for user to type in new font to load (in a text input). 等待用户键入要加载的新字体(在文本输入中)。
  3. When the user clicks "OK", load the new Web Font(s) + trigger a new onComplete callback. 当用户单击“确定”时,加载新的Web字体+触发新的onComplete回调。

I've tried reusing the Google JS snippet, but any calls after the 1st doesn't seem to trigger the "fontloading" callback (unsure if it fails). 我尝试重用Google JS代码段,但是1号之后的任何调用似乎都不会触发“字体加载”回调(不确定是否失败)。

var loadWebFont = function(fontFamilies, onComplete) {
    //Ensure 1st argument is of Array[String] type:
    if(!_.isArray(fontFamilies)) fontFamilies = [fontFamilies];
    else fontFamilies = fontFamilies.concat();

    //Get rid of blank/null fonts:
    for(var f=fontFamilies.length; --f>=0;) {
        var theFont = fontFamilies[f];
        if(!theFont || $.trim(theFont)==="") {
            fontFamilies.splice(f, 1);
        }
    }

    //Setup the font families Object and "fontloading" callback on window object.
    var loadCount = fontFamilies.length;
    window.WebFontConfig = {
        "fontloading": function(a, b) {
            if((--loadCount)<=0) onComplete();
        },
        google: { families: fontFamilies }
    };

    //Remove previously attached WebFonts <script> tag:
    if(window._WF_SCRIPT) {
        console.log("Removing previous Google Web Fonts node: " + window._WF_SCRIPT);
        window._WF_SCRIPT.parentNode.removeChild(window._WF_SCRIPT);
        window._WF_SCRIPT = null;
    }

    console.log("Loading fonts: " + fontFamilies);

    var wf = window._WF_SCRIPT = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
};

The first time the WebFont API gets loaded, the webfont.js file defines a top-level object on window.WebFont . 第一次web字体API被加载,则webfont.js文件定义上的顶层对象window.WebFont This holds the methods for loading the fonts, DOM manipulation to add a stylesheet, and ( by the looks of it ) some cross-browser compatibility check. 它包含用于加载字体的方法,用于添加样式表的DOM操作以及( 通过其外观 )一些跨浏览器兼容性检查的方法。

If you attempt reloading this script again, it seems to reuse the 1st window.WebFont object and therefore disregards any new fonts you pass to it. 如果尝试再次重新加载此脚本,它似乎会重用第一个window.WebFont对象,因此会忽略传递给它的任何新字体。

Solution: 解:

Nullify the window.WebFont and its generated <script> + <link rel="stylesheet"> tags 使window.WebFont及其生成的<script> + <link rel =“ stylesheet”>标签window.WebFont

Here's the corrections made to the above script: 这是对以上脚本所做的更正:

window.loadWebFont = function(fontFamilies, onComplete) {
    if(window._WEBFONT_SCRIPT) {
        console.log("Removing previous Google Web Fonts elements.");
        window._WEBFONT_SCRIPT.parentNode.removeChild(window._WEBFONT_SCRIPT);
        window._WEBFONT_SCRIPT = null;

        if(window._WEBFONT_CSS) {
            window._WEBFONT_CSS.parentNode.removeChild(window._WEBFONT_CSS);
            window._WEBFONT_CSS = null;
        }
        window.WebFont = null;
    }

    if(!_.isArray(fontFamilies)) fontFamilies = [fontFamilies];
    else fontFamilies = fontFamilies.concat();
    for(var f=fontFamilies.length; --f>=0;) {
        var theFont = fontFamilies[f];
        if(!theFont || $.trim(theFont)==="") {
            fontFamilies.splice(f, 1);
        }
    }
    var loadCount = fontFamilies.length;
    window.WebFontConfig = {
        "fontloading": function(a, b) {
            if((--loadCount)<=0) {
                var css = $('link[href^="http://fonts.googleapis.com/"]');
                //Store the <link> element, not the jQuery/Zepto result
                window._WEBFONT_CSS = css[0];
                onComplete();
            }
        },
        google: { families: fontFamilies }
    };

    var wf = window._WEBFONT_SCRIPT = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM