{"id":10388,"date":"2022-04-27T19:46:00","date_gmt":"2022-04-27T11:46:00","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10388"},"modified":"2024-11-01T16:03:35","modified_gmt":"2024-11-01T08:03:35","slug":"js-font-face-load","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2022\/04\/js-font-face-load\/","title":{"rendered":"JS FontFace API\u5b57\u4f53\u52a0\u8f7d\u5931\u8d25\u6216\u5b8c\u6bd5\u7684\u68c0\u6d4b"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10388\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10388<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202204-font-load.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202204\/your-story-cover.jpg\" width=\"480\" height=\"301\" alt=\"font \u5b57\u4f53\u5c01\u9762\u56fe\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5f00\u95e8\u89c1\u5c71<\/h3>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5b57\u4f53\uff0c\u5982\u679c\u8fd8\u6ca1\u6709\u52a0\u8f7d\u5b8c\u6bd5\uff0c\u5c31\u8ba9 Unicode \u5b57\u7b26\u663e\u793a\uff0c\u6709\u53ef\u80fd\u4f1a\u663e\u793a\u4e71\u4e03\u516b\u7cdf\u7684\u5b57\u7b26\uff0c\u8fc7\u4e00\u6bb5\u65f6\u95f4\u540e\u624d\u663e\u793a\u6620\u5c04\u540e\u7684\u6548\u679c\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u4f53\u9a8c\u5c31\u4e0d\u592a\u597d\u3002<\/p>\n<p>\u8fd8\u6709\u5c31\u662f\u5f00\u53d1\u4eba\u5458\u5e0c\u671b\u77e5\u9053\u5b57\u4f53\u6587\u4ef6\u52a0\u8f7d\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u9700\u8981\u5728\u52a0\u8f7d\u5f02\u5e38\u7684\u65f6\u5019\u4e0a\u62a5\u3002<\/p>\n<p>\u4e0a\u9762\u8fd9\u4e24\u79cd\u573a\u666f\u7684\u5b9e\u73b0\u53ef\u4ee5\u4f7f\u7528\u6d4f\u89c8\u5668\u539f\u751f\u7684 FontFace API\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ \u517c\u5bb9\u6027\u5224\u65ad\uff0c\u9632\u6b62 IE \u6d4f\u89c8\u5668\u4e0b\u62a5\u9519\r\nif (window.FontFace) {\r\n    var fontFile = new FontFace('Some Font Name', 'url(someFontUrl.woff2)');\r\n\r\n    fontFile.load().then(function () {\r\n        console.log('\u6210\u529f');\r\n    }, function (err) {\r\n        console.log('\u5931\u8d25\uff1a' + err);\r\n    });\r\n}<\/pre>\n<p>\u6211\u4e13\u95e8\u505a\u4e86\u4e2a\u4f53\u9a8cdemo\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202204\/font-face-load-demo.php\" rel=\"noopener\" target=\"_blank\">FontFace load()\u52a0\u8f7d\u6210\u529f\u5931\u8d25demo<\/a><\/p>\n<p>\u53ef\u4ee5\u5bf9\u8f93\u5165\u6846\u4e2d\u7684 URL \u5730\u5740\u8fdb\u884c\u5b57\u4f53\u52a0\u8f7d\u6210\u529f\u4e0e\u5426\u7684\u5224\u522b\u3002<\/p>\n<p>GIF \u5f55\u5c4f\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202204\/font-detect.gif\" width=\"319\" height=\"187\" alt=\"\u5b57\u4f53\u52a0\u8f7d\u68c0\u6d4b\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6253\u5f00\u7f51\u7edc\u76d1\u63a7\uff0c\u70b9\u51fb\u6309\u94ae\u540e\u53ef\u4ee5\u770b\u5230\u5b57\u4f53\u7684\u8bf7\u6c42\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202204\/font-load-request.gif\" width=\"714\" height=\"164\" alt=\"\u68c0\u6d4b\u4e0e\u8bf7\u6c42\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001\u4e86\u89e3 FontFace API<\/h3>\n<p>FontFace API \u76f8\u5bf9\u4e8e\u628a CSS <code>@font-face<\/code> \u4e00\u4e9b\u4e0d\u53ef\u63a7\u7684\u7b56\u7565\u642c\u5230\u4e86 JS \u4e2d\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u8bbe\u7f6e\u3002<\/p>\n<p>\u5176\u652f\u6301\u7684\u4e00\u4e9b API \u5c5e\u6027\u548c <code>@font-face<\/code> \u89c4\u5219\u652f\u6301\u7684 CSS \u5c5e\u6027\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>FontFace.display \u5bf9\u5e94 CSS <code>font-display<\/code> \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.family \u5bf9\u5e94 font-family \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.featureSettings \u5bf9\u5e94 font-feature-settings \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.stretch \u5bf9\u5e94 font-stretch \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.style \u5bf9\u5e94 font-style \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.unicodeRange \u5bf9\u5e94 unicode-range \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.variant \u5bf9\u5e94 font-variant \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.variationSettings \u5bf9\u5e94 font-variation-settings \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.weight \u5bf9\u5e94 font-weight \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.ascentOverride \u5bf9\u5e94 ascent-override \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.descentOverride \u5bf9\u5e94 descent-override \u5c5e\u6027\uff1b<\/li>\n<li>FontFace.lineGapOverride \u5bf9\u5e94 line-gap-override \u5c5e\u6027\uff1b<\/li>\n<\/ul>\n<p>\u5176\u5b9e\uff0c\u4e0a\u9762\u5217\u8868\u4e2d\u7684\u6700\u540e 3 \u4e2aCSS\u5c5e\u6027\u5927\u5bb6\u53ef\u80fd\u6ca1\u89c1\u8fc7\uff0c\u53ef\u4ee5\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2022\/03\/css-ascent-override-descent\/\">ascent-override descent-override line-gap-override\u4e00\u9505\u7aef<\/a>\u201d\u8fdb\u884c\u4e86\u89e3\u3002<\/p>\n<p>\u53e6\u5916\uff0cFontFace \u63a5\u53e3\u8fd8\u6709\u5982\u4e0b\u4e00\u4e9b\u53ea\u8bfb\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<dl>\n<dt><strong>FontFace.loaded<\/strong><\/dt>\n<dd>\u8fd4\u56de\u4e00\u4e2a Promise\uff0c\u81ea\u5b9a\u4e49\u5b57\u4f53\u52a0\u8f7d\u5b8c\u6bd5\u6216\u8005\u5931\u8d25\u4e4b\u540e\u89e6\u53d1\u3002\u611f\u89c9\u6709\u4e9b\u9e21\u808b\uff0c\u6709\u4e86 load() \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u5c5e\u6027\u6709\u4ec0\u4e48\u7528\u5462\uff1f\u800c\u4e14\u6b64\u65b9\u6cd5\u6839\u636e\u6211\u7684\u6d4b\u8bd5\uff0c\u8c8c\u4f3c\u8981\u6267\u884c load() \u624d\u6709\u6548\u3002<\/dd>\n<dt><strong>FontFace.status<\/strong><\/dt>\n<dd>\u8fd4\u56de\u5b57\u4f53\u7684\u72b6\u6001\uff0c\u503c\u53ef\u80fd\u662f\u540e\u9762\u5176\u4e2d\u4e4b\u4e00\uff1a<code>\"unloaded\"<\/code>, <code>\"loading\"<\/code>, <code>\"loaded\"<\/code> \u6216\u8005 <code>\"error\"<\/code>\u3002<\/dd>\n<dt><strong>FontFace.load()<\/strong><\/dt>\n<dd>\u89e6\u53d1\u81ea\u5b9a\u4e49\u5b57\u4f53\u52a0\u8f7d\uff0c\u53ef\u4ee5\u662f URL \u5730\u5740\uff0c\u4e5f\u53ef\u4ee5\u662f\u5b57\u4f53\u7684 buffer \u8d44\u6e90\uff0c\u8fd4\u56de\u503c\u662f\u4e2a Promise\uff0c\u6210\u529f\u540e\u8fd4\u56de\u5f53\u524d FontFace \u5bf9\u8c61\u3002<\/dd>\n<\/dl>\n<p>\u7b80\u5355\u7684\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>var fontFile = new FontFace('Some Font Name', 'url(someFontUrl.woff2)');\r\n\r\nfontFile.load().then(function (fontface) {\r\n    \/\/ \u6253\u5370\u503c\u662f 'Some Font Name'\r\n    console.log(fontface.family);\r\n});<\/pre>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>\u9664\u4e86\u4e2a\u522b\u6bd4\u8f83\u65b0\u7684\u5c5e\u6027\u503c\uff0cFontFace API \u7684\u517c\u5bb9\u6027\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6d4f\u89c8\u56682014\u5e74\u5c31\u5df2\u7ecf\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202204\/2022-04-27_181429.png\" width=\"562\" height=\"237\" alt=\"FontFace API \u63a5\u53e3\u7684\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001\u9e23\u91d1\u6536\u5175<\/h3>\n<p>\u770b\u4e86\u4e0b\uff0c\u8fd8\u6709\u4e2a\u540d\u53eb <code>FontFaceSet<\/code> \u7684 API\uff0c\u8fd9\u4e2a API \u4e00\u822c\u548c <code>document.fonts<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u4e3b\u8981\u5bf9\u6587\u6863\u91cc\u9762\u7684\u81ea\u5b9a\u4e49\u5b57\u4f53\u8fdb\u884c\u6279\u91cf\u7684\u5904\u7406<\/p>\n<p>\u4ee5\u540e\u9047\u5230\u76f8\u5173\u7684\u9700\u6c42\u6211\u518d\u505a\u76f8\u5173\u7684\u4ecb\u7ecd\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u60f3\u8981\u517c\u5bb9 IE \u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f20\u7edf\u7684 XMLHttpRequest GET \u8bf7\u6c42\u6a21\u62df\uff0conLoad \u4e4b\u540e\u6267\u884c\u76f8\u5173\u7684\u5904\u7406\uff0c\u793a\u610f\uff1a<\/p>\n<pre>\/\/ \u4f20\u7edf\u5b9e\u73b0\r\nvar xhr = new XMLHttpRequest();\r\nxhr.open('get', 'someFontUrl.woff2');\r\nxhr.onload = function () {\r\n    console.log('\u52a0\u8f7d\u6210\u529f\uff01');\r\n};\r\nxhr.onerror = function () {\r\n    console.log('\u52a0\u8f7d\u5931\u8d25\uff01');\r\n};\r\nxhr.send();<\/pre>\n<p>\u8fd8\u6709\u51e0\u5929\u5c31\u4e94\u4e00\u8282\u4e86\uff0c\u5df2\u7ecf\u9759\u6001\u5728\u5bb640\u591a\u5929\u4e86\u3002<\/p>\n<p>\u5c0f\u533a\u4f9d\u7136\u6bcf\u65e5\u6709\u9633\u6027\uff0c\u6211\u4f30\u8ba1\u6700\u65e9\u4e5f\u8981\u513f\u7ae5\u8282\u89c1\u4e86\uff01<\/p>\n<p>\u597d\u60f3\u51fa\u95e8\u53bb\u9493\u9c7c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f3a3.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10388\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10388<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202204\/your-story-cover.jpg\" width=\"160\" height=\"100\" alt=\"font \u5b57\u4f53\u5c01\u9762\u56fe\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u5982\u4f55\u5224\u65ad CSS @font-face \u89c4\u5219\u4e2d\u7684\u5b57\u4f53\u662f\u5426\u52a0\u8f7d\u7ed3\u675f\uff0c\u662f\u5426\u52a0\u8f7d\u6210\u529f\uff1f\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\u4f60\u4e00\u5b9a\u559c\u6b22\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[416,655,1826,1285,1286],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10388"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10388"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10388\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}