浏览器有时会使用错误的字体渲染图标,导致文本混乱。

7

我在使用Chrome浏览器时遇到了一个间歇性的问题,即无法正确呈现图标所需的字体。

我正在使用icomoon.io创建自定义图标集作为字体,并使用以下CSS(由icomoon自动生成)显示这些自定义图标。

@font-face {
    font-family: 'eIconFont';
    src: url('fonts/eIconFontV3.eot?kmqo7q');
    src: url('fonts/eIconFontV3.eot?kmqo7q#iefix') format('embedded-opentype'), url('fonts/eIconFontV3.woff2?kmqo7q') format('woff2'), url('fonts/eIconFontV3.ttf?kmqo7q') format('truetype'), url('fonts/eIconFontV3.woff?kmqo7q') format('woff'), url('fonts/eIconFontV3.svg?kmqo7q#eIconFontV3') format('svg');
    font-weight: normal;
    font-style: normal;
}
[class^="eIcon-"], [class*=" eIcon-"] {
    /* use !important to prevent issues with browser extensions that change fonts */
    font-family: 'eIconFont' !important;
    speak: none;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    /* Enable Ligatures ================ */
    letter-spacing: 0;
    -webkit-font-feature-settings: "liga";
    -moz-font-feature-settings: "liga=1";
    -moz-font-feature-settings: "liga";
    -ms-font-feature-settings: "liga" 1;
    font-feature-settings: "liga";
    -webkit-font-variant-ligatures: discretionary-ligatures;
    font-variant-ligatures: discretionary-ligatures;
    /* Better Font Rendering =========== */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
.eIcon-support:before {
    content: "\e93b";
}
.eIcon-collapse:before {
    content: "\e935";
}
.eIcon-expand:before {
    content: "\e936";
}

HTML代码如下:
<span class="eIcon-support" title="Support">
    ::before
</span>

结果应该长这样:

结果看起来是这样的:

在此输入图片描述

通过查看 Chrome 开发者工具,我可以看到字体文件自身从我们的 CDN 加载得很好,但我注意到在 元素 > 计算 > 渲染字体 下,它说 Times New Roman,而通常情况下该值为 eIconFontV3。很奇怪浏览器尝试使用 Times New Roman,因为在 body 上设置的字体族名称是 'Gotham Light'、Arial、sans-serif

这个 bug 很少发生,而且很难重现。


@font-face 应该有两个 src 值吗?这样似乎会互相覆盖。 - showdev
@showdev 我不确定为什么有两个“src”,这只是icomoon.io生成的。可能是为了额外的谨慎回退? - BobSacamano
可能是这样。我找到了这个,但我不确定它是否相关。你看到类似的错误吗? - showdev
@showdev 不好意思,控制台中没有显示任何错误。 - BobSacamano
1个回答

3
我也遇到了这个问题。如果你使用sass并且导致了问题,也许下面的细节可以帮助你。
我使用sass将SCSS文件编译为CSS文件,并发现sass将转义字符串(例如\E91E,十六进制5C45393145)转换为实际的utf-8值(,十六进制EEA49E),但是node-sass不会这样做。当使用Chrome时,这种转换有时会导致文本乱码。这里有更多关于这个问题的细节。
我的临时解决方案是使用node-sass代替sass来编译SCSS文件。

虽然我理解使用sass和node-sass之间的CSS差异,但我不明白为什么Chrome(或任何Webkit浏览器)有时会使用Times New Roman而不是指定的图标字体。这让我非常头疼。 - Mat
@lhtin非常感谢您在sass存储库中链接问题上所做的工作。您的解释和示例对我帮助很大,让我能够追踪到为什么在我所工作的应用程序中会出现这种情况。 - Hooray Im Helping

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接