@@ -33,15 +33,32 @@ export async function renderTxt2Img(
3333 onCreated ?: ( el : HTMLElement ) => void ;
3434 } = { } ,
3535) : Promise < HTMLImageElement > {
36- const div = createEl ( 'pre' ) ;
37- div . style . cssText = `margin: 0; ${ cssText } ; position: fixed;` ;
38- div . textContent = txt ;
39- document . body . appendChild ( div ) ;
40- opts . onCreated ?.( div ) ;
36+ const preEl = createEl ( 'pre' ) ;
37+ preEl . style . cssText = `margin: 0; ${ cssText } ; position: fixed;` ;
38+ preEl . textContent = txt ;
39+ document . body . appendChild ( preEl ) ;
40+ opts . onCreated ?.( preEl ) ;
4141
42- const { width, height } = div . getBoundingClientRect ( ) ;
42+ // 避免重复覆盖其他字体他
43+ const tmpFontName = 'TMP_FONT_NAME_' + crypto . randomUUID ( ) ;
44+ let fontFace : FontFace | null = null ;
45+ // 等待字体加载完成后再计算尺寸
46+ if ( opts . font != null ) {
47+ preEl . style . fontFamily = tmpFontName ;
48+ fontFace = new FontFace ( tmpFontName , `url(${ opts . font . url } )` ) ;
49+ await fontFace . load ( ) ;
50+ // @ts -expect-error https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/add
51+ document . fonts . add ( fontFace ) ;
52+ await document . fonts . ready ;
53+ }
54+
55+ const { width, height } = preEl . getBoundingClientRect ( ) ;
4356 // 计算出 rect,立即从dom移除
44- div . remove ( ) ;
57+ preEl . remove ( ) ;
58+ if ( fontFace != null ) {
59+ // @ts -expect-error https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/delete
60+ document . fonts . delete ( fontFace ) ;
61+ }
4562
4663 const img = new Image ( ) ;
4764 img . width = width ;
@@ -51,7 +68,7 @@ export async function renderTxt2Img(
5168 ? ''
5269 : `
5370 @font-face {
54- font-family: '${ opts . font . name } ';
71+ font-family: '${ tmpFontName } ';
5572 src: url('data:font/woff2;base64,${ arrayBufferToBase64 ( await ( await fetch ( opts . font . url ) ) . arrayBuffer ( ) ) } ') format('woff2');
5673 }
5774 ` ;
@@ -61,7 +78,7 @@ export async function renderTxt2Img(
6178 ${ fontFaceStr }
6279 </style>
6380 <foreignObject width="100%" height="100%">
64- <div xmlns="http://www.w3.org/1999/xhtml">${ div . outerHTML } </div>
81+ <div xmlns="http://www.w3.org/1999/xhtml">${ preEl . outerHTML } </div>
6582 </foreignObject>
6683 </svg>
6784 `
0 commit comments