浏览器有时会使用错误的字体来渲染图标,导致出现乱码

2024-01-07

我遇到了 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'Gotham Light', Arial, sans-serif.

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


我也遇到了这个问题。如果你使用sass https://github.com/sass/dart-sass并导致问题,也许以下详细信息会对您有所帮助。

我使用 sass 将 SCSS 文件编译为 CSS 文件,我发现 sass 转换了转义字符串(例如\E91E, hex 5C45393145 ) 到真正的 utf-8 值(, hex EEA49E),但是节点 sass https://github.com/sass/node-sass不这样做。并且转换后有时会导致使用Chrome时文本出现乱码。Here https://github.com/sass/dart-sass/issues/1219有关于这个问题的更多细节。

我的临时解决方案是使用node-sass而不是sass来编译SCSS文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

浏览器有时会使用错误的字体来渲染图标,导致出现乱码 的相关文章