【问题标题】:Change font-family for Dyslexic in every website (bookmark)在每个网站(书签)中更改 Dyslexic 的字体系列
【发布时间】:2013-08-20 21:04:03
【问题描述】:

我的目标很简单(我认为): 正如标题所示,我想要一个书签来更改 Open-dyslexic web-font 中每个网站的字体。

因为字体在我的服务器上,所以我需要在页面的 CSS 中添加一个新的 font-face 定义。

在那之后,我能想到的最简单的事情就是为标签设置新的字体系列。

但是在这里我遇到了第一个问题。什么都没有发生。

!function ()
{
    var newStyle = document.createElement( 'style' );
    newStyle.appendChild( document.createTextNode( "@font-face { font-family: 'OpenDyslexicRegular'; src: url('http://www.andrearastelli.net/font/OpenDyslexic-Regular.otf') format('opentype'); }" ) );
    document.head.appendChild( newStyle );
    console.debug( document.body.style.fontFamily );
    document.body.style.fontFamily = 'OpenDyslexicRegular, tahoma, verdana, arial, sans-serif';
    console.debug( document.body.style.fontFamily );
}();

这是我的代码,如果我删除“OpenDyslexicRegular”部分,字体实际上会改变(例如,如果尝试设置 font-family: serif,一切正常)。

我想问题是我添加到页面中的新字体。看起来新字体并没有真正加载。

或者也许是扩展名? (但CANIUSE/ttf 表明 otf 是一种非常有效的字体格式)


好的,这是脚本中的第一个更改(只是为每个 DOM 元素设置我想要的字体系列)

!function ()
{
    var newStyle = document.createElement( 'style' );
    newStyle.appendChild( document.createTextNode( "" +
        "@font-face { " +
        "font-family: 'OpenDyslexicRegular'; " +
        "src: url('http://www.andrearastelli.net/font/OpenDyslexic-Regular.otf') format('opentype'); " +
        "}" ) );
    document.head.appendChild( newStyle );
    var allDomElement = document.getElementsByTagName('*');
    for (var i=0; i<allDomElement.length; i++){
        allDomElement[i].style.fontFamily = 'OpenDyslexicRegular';
    }
    // document.body.style.fontFamily = 'OpenDyslexicRegular, tahoma, verdana, arial, sans-serif';
}();

我之前描述的问题只发生在 Firefox 上(我暂时没有测试过 IE 或 Safari)。


这是一个更高级的版本,在 CSS 中具有不同的字体格式。

!function ()
{
    var newStyle = document.createElement( 'style' );
    newStyle.appendChild( document.createTextNode( "" +
        "@font-face { " +
        "font-family: 'OpenDyslexicRegular'; " +
        "src: url('http://www.andrearastelli.net/font/OpenDyslexic-Regular.otf') format('opentype'); " +
        "src:   url('http://www.andrearastelli.net/font/opendyslexic-regular-webfont.woff') format('woff')," +
        "       url('http://www.andrearastelli.net/font/opendyslexic-regular-webfont.ttf') format('truetype')," +
        "       url('http://www.andrearastelli.net/font/opendyslexic-regular-webfont.svg#opendyslexicregular') format('svg');"+
        "}" ) );
    document.head.appendChild( newStyle );
    var allDomElement = document.getElementsByTagName('*');
    for (var i=0; i<allDomElement.length; i++){
        allDomElement[i].style.fontFamily = 'OpenDyslexicRegular';
    }
    // document.body.style.fontFamily = 'OpenDyslexicRegular, tahoma, verdana, arial, sans-serif';
}();

因为使用 firefox 这也不起作用。我正在考虑放弃对 FF 的支持。

【问题讨论】:

  • 我正在使用 chrome 并且效果很好,您是如何在希望使用此字体的页面上触发它的?
  • 我在 FireFox 上看到了同样的问题,但它在 Chrome 上运行。
  • 这里需要说明的是,OpenType 不是 WOFF 规范的一部分,被 Mozilla 视为“实验性”,请参阅 developer.mozilla.org/en-US/docs/Web/CSS/@font-face
  • @ars265 谢谢,我没有搜索(甚至没有想过)OTF 的 Mozilla 规范。
  • 如果你要发布它,我可以建议阅读这个并更改字体文件吗? paulirish.com/2009/bulletproof-font-face-implementation-syntax

标签: javascript css font-face


【解决方案1】:

正如我们在 cmets 中讨论的那样,如果您要将其发布给其他人,我建议您使用 Paul Irish's post,以便全面获得正确的功能。同样使用 Chrome,我注意到您的字体文件以application/x-font-otf 的形式返回,但this post 建议您应该使用font/opentypeapplication/x-font-opentype。如果 Firefox 有这个问题,我不会感到惊讶。

【讨论】:

  • 如何将 x-font-otf MIME 更改为正确的?我可以在字体规则中执行此操作.. 或者我需要构建“某些东西”以返回具有正确 MIME 类型的文件?
  • 这将是一个服务器端配置,它取决于您运行的服务器类型。我猜你的网站是 WordPress,你运行的是 debian/Apache 2?
  • 嗯..是的。我想我需要某种 .htaccess 在“font/”文件夹中让浏览器以正确的方式解码字体格式。感谢您的帮助;)
  • 根据你想读多少,这里是长版:httpd.apache.org/docs/2.2/mod/mod_mime.html,或者这里是缩短版:stackoverflow.com/questions/13847234/apache2-server-mime-types
  • 理论上是的,你也可以自己设置类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2012-11-16
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
相关资源
最近更新 更多