| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /**
- * 字体面注入 —— 规格 11 授权门禁在浏览器里的落点。
- *
- * 为什么是运行时注入而不是静态 @font-face 写进 tokens.css:
- * 静态声明只能表达「有一个字体名字」,无法承载授权门禁。这里逐条向
- * `resolveFont(key)` 要结果,**门禁不通过的字体根本不会生成 @font-face 规则**,
- * 于是「无证据字体不可被解析为运行时字体」不是靠约定,而是结构上做不到——
- * 没有 @font-face 就没有这个字族可用,浏览器只能落到系统回退链。
- *
- * 路径由统一资产解析层拼(含 BASE_URL / qiankun 注入路径),本模块不拼路径。
- */
- import { resolveFont } from './runtimeAssets.js'
- /** 需要注入的字体条目(语义 key)。与 runtimeAssets.FONT_SLOTS 的消费点一一对应。 */
- const FONT_FACE_KEYS = [
- 'font.body.regular',
- 'font.body.bold',
- 'font.num.regular',
- 'font.num.bold',
- 'font.num.mono',
- // 票 17 第七轮:'font.title' 已撤销(无界黑无消费点,见 runtimeAssets.FONT_SLOTS 注释)。
- ]
- const STYLE_ID = 'cockpit-font-faces'
- /** 生成 @font-face 规则文本。纯函数:输入已解析的字体描述,输出 CSS 文本,便于自查脚本复算。 */
- export function buildFontFaceCss(descriptors) {
- return descriptors
- .filter((d) => d.available && d.url)
- .map((d) => [
- '@font-face {',
- ` font-family: "${d.family}";`,
- ` font-style: ${d.style};`,
- ` font-weight: ${d.weight};`,
- ` font-display: swap;`, // 首屏不被字体阻塞:先用回退字体渲染,字体到位再换
- ` src: url("${d.url}") format("${d.format || 'woff2'}");`,
- '}',
- ].join('\n'))
- .join('\n')
- }
- /** 收集门禁通过的字体描述(不可用的被过滤掉——这正是门禁的可见后果)。 */
- export function fontFaceDescriptors(options = {}) {
- return FONT_FACE_KEYS.map((key) => resolveFont(key, options))
- }
- /**
- * 注入字体面。幂等:同一文档重复调用只保留一份 style 节点。
- * 返回注入的描述列表,供宿主 data-* 读数与走查断言。
- */
- export function installFontFaces(options = {}) {
- const descriptors = fontFaceDescriptors(options)
- const css = buildFontFaceCss(descriptors)
- if (typeof document === 'undefined') return descriptors
- let node = document.getElementById(STYLE_ID)
- if (!node) {
- node = document.createElement('style')
- node.id = STYLE_ID
- document.head.appendChild(node)
- }
- node.textContent = css
- node.dataset.fontFaces = String(descriptors.filter((d) => d.available).length)
- return descriptors
- }
|