使用Fontmin生成WebFont压缩字体

2021-09-09 11:44:54 浏览数 (1)

我上一篇文章(记一次改字体的辛酸史,解决CDN跨域问题)讲的是我改字体的一个过程,用了@font-face的方式,引用了一整个特殊字体,导致网站加载了整个字体文件,访问变慢,所有内容加起来已经有5MB。于是我在找前端如何优化字体加速访问,然而找到的都是Font-Spider(字蛛),看了网上各种教程,无一例外全是一模一样的。 昨天我测试字体的时候用过有字库,有字库就是一个典型的webfont,我就在想有没有跟有字库差不多的webfont引用方式,不直接引用整个字体,而是引用需要用到字,终于,我在GitHub上找到了这个15年的项目Fontmin,我直接下载了他的客户端,Fontmin提供了Mac os & Windows客户端。只需要将ttf原字体拖入客户端,输入需要用到的字,即可一键生成字体和css配置。

生成之后,会创建以你原字体名开头的eot,ttf,woff,css文件

由于我源站国内访问并不是很快,我修改了css内容,上传字体到我的腾讯云cos并链接,有两个css,一个是name.css,一个是name-embed.css,以下是我的配置

代码语言:javascript复制
@font-face {
        font-family: "f";
        src: url("cdn.elstec.cn/test/f.eot"); /* IE9 */
        src: url("cdn.elstec.cn/test/f.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */

        url("cdn.elstec.cn/test/f.woff") format("woff"), /* chrome、firefox */
        url("cdn.elstec.cn/test/f.ttf") format("truetype"), /* chrome、firefox、opera、Safari, Android, iOS 4.2  */

        url("cdn.elstec.cn/test/f.svg#f") format("svg"); /* iOS 4.1- */
        font-style: normal;
        font-weight: normal;
    }

以下是embed文件

代码语言:javascript复制
@font-face {
        font-family: "f";
        src: url("cdn.elstec.cn/test/f.eot"); /* IE9 */
        src: url("cdn.elstec.cn/test/f.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */

/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMmAtawkAAACsAAAAYGNtYXCvY9BaAAABDAAAAYpnbHlmEnhSBAAAApgAABF0aGVhZAr97/0AABQMAAAANm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T1MxLjAwRlpDVUpJTkxGVy0tR0IxLTBCeSBCZWlqaW5nIEZvdW5kZXIgRWxlY3Ryb25pY3MgQ28uLEx0ZC4gUxdOwqxTF1knZcK5a2N1NVtQZwnCllBRbFPDuABDAG8AcAB5AHIAaQBnAGgAdAAoAGMAKQAgAEIAZQBpAGoAaQBuAGcAIABGAG8AdQBuAGQAZQByACAARQBsAGUAYwB0AHIAbwBuAGkAYwBzACAAQwBvAC4ALABMAHQAZAAuADIAMAAxADUAZQC5AGsAYwB8AJcAkQDRAJYAdQB AEEATwBTAFIAZQBnAHUAbABhAHIARgBvAHUAbgBkAGUAcgA6AGUAuQBrAGMAfACXAJEA0QCWAHUAfgBBAE8AUwAJAFIAZQBnAHUAbABhAHIAZQC5AGsAYwB8AJcAkQDRAJYAdQB AEEATwBTADEALgAwADAARgBaAEMAVQBKAEkATgBMAEYAVwAtAC0ARwBCADEALQAwAEIAeQAgAEIAZQBpAGoAaQBuAGcAIABGAG8AdQBuAGQAZQByACAARQBsAGUAYwB0AHIAbwBuAGkAYwBzACAAQwBvAC4ALABMAHQAZAAuACAAUwAXAE4ArABTABcAWQAnAGUAuQBrAGMAdQA1AFsAUABnAAkAlgBQAFEAbABTAPgAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALAAsAAAAmAC0ANgA7ADwAXwECAQMBBAEFB3VuaTRFM0EHdW5pNEY1Qwd1bmk2NUUwB3VuaTZCRUI=) format("truetype"), /* chrome、firefox、opera、Safari, Android, iOS 4.2  */
*/

        url("cdn.elstec.cn/test/f.svg#f") format("svg"); /* iOS 4.1- */
        font-style: normal;
        font-weight: normal;
    }

我又上传这两个css到cos,用这两句代码引用

代码语言:javascript复制
<link rel="stylesheet" href="https://cdn.elstec.cn/test/f.css" />
    <link rel="stylesheet" href="https://cdn.elstec.cn/test/f-embed.css" />

刷新就看到效果啦。(跨域报错的话,请看我上一篇文章)

0 人点赞