| 123 | getRendererModeFromParams(getInitialSearchParams()); |
| 124 | |
| 125 | const createPreviewSkiaFontResolver = ( |
| 126 | fontManager: PreviewSkiaFontManager, |
| 127 | ): PreviewSkiaFontResolver => { |
| 128 | const skiaWithFontMatching = SkiaModule as typeof SkiaModule & { |
| 129 | matchFont?: ( |
| 130 | style?: { |
| 131 | fontFamily?: string; |
| 132 | fontSize?: number; |
| 133 | fontStyle?: "normal"; |
| 134 | fontWeight?: "normal"; |
| 135 | }, |
| 136 | fontManager?: PreviewSkiaFontManager, |
| 137 | ) => unknown; |
| 138 | }; |
| 139 | const fontsBySize = new Map<number, PreviewSkiaFont | undefined>(); |
| 140 | const fontFamilies = previewSkiaFontFamilies; |
| 141 | const isUsableFont = (font: PreviewSkiaFont | undefined) => { |
| 142 | return Boolean(font); |
| 143 | }; |
| 144 | |
| 145 | return (options = {}) => { |
| 146 | const fontSize = options.fontSize ?? 12; |
| 147 | |
| 148 | const loadedFont = fontsBySize.get(fontSize); |
| 149 | |
| 150 | if (loadedFont) { |
| 151 | return loadedFont; |
| 152 | } |
| 153 | |
| 154 | for (const fontFamily of fontFamilies) { |
| 155 | try { |
| 156 | const matchedFont = skiaWithFontMatching.matchFont?.( |
| 157 | { |
| 158 | fontFamily, |
| 159 | fontSize, |
| 160 | fontStyle: "normal", |
| 161 | fontWeight: "normal", |
| 162 | }, |
| 163 | fontManager, |
| 164 | ) as PreviewSkiaFont | undefined; |
| 165 | |
| 166 | if (isUsableFont(matchedFont)) { |
| 167 | fontsBySize.set(fontSize, matchedFont); |
| 168 | |
| 169 | return fontsBySize.get(fontSize); |
| 170 | } |
| 171 | } catch { |
| 172 | // Try the next native family. |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | try { |
| 177 | const font = SkiaModule.Skia.Font(undefined, fontSize) as PreviewSkiaFont; |
| 178 | |
| 179 | fontsBySize.set(fontSize, isUsableFont(font) ? font : undefined); |
| 180 | |
| 181 | return fontsBySize.get(fontSize); |
| 182 | } catch { |