okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3616148bb
main
1import { execFile } from 'node:child_process' ; 2import { promisify } from 'node:util' ; 3import { utf8Script } from './powershell-utf8.mjs' ; 4const execute = promisify ( execFile ); 5let cached = null ; 6export async function systemFonts ({ platform= process . platform , run= execute } = {}){ 7if ( cached && platform === process . platform && run === execute ) return cached ; 8const read = ( cmd , args ) => run ( cmd , args ,{ timeout :20000 , maxBuffer :16 * 1024 * 1024 , windowsHide :true }); 9try { 10let names = []; 11if ( platform === 'darwin' ){ 12const { stdout} = await read ( '/usr/sbin/system_profiler' ,[ 'SPFontsDataType' , '-json' , '-detailLevel' , 'mini' ]); 13const collect = value => { if ( ! value || typeof value !== 'object' ) return ; if ( typeof value . family === 'string' ) names . push ( value . family ); for ( const nested of Object . values ( value )) if ( typeof nested === 'object' ) collect ( nested );}; collect ( JSON . parse ( stdout )); 14} else if ( platform === 'win32' ){ 15const { stdout} = await read ( 'powershell.exe' ,[ '-NoProfile' , '-NonInteractive' , '-Command' , utf8Script ( 'Add-Type -AssemblyName System.Drawing; (New-Object System.Drawing.Text.InstalledFontCollection).Families.Name | ConvertTo-Json -Compress' )]); 16const result = JSON . parse ( stdout . replace ( / ^\uFEFF / , '' )); names = Array . isArray ( result ) ?result :[ result ]; 17} else { 18const { stdout} = await read ( 'fc-list' ,[ '-f' , '%{family}\n' ]); names = stdout . split ( / [ \n , ] / ); 19} 20const fonts = [ ...new Set ( names . filter ( name => typeof name === 'string' ). map ( name => name . trim ()). filter ( Boolean ))]. sort (( a , b ) => a . localeCompare ( b )); 21const result = { fonts, available :fonts . length > 0 }; if ( platform === process . platform && run === execute ) cached = result ; return result ; 22} catch { return { fonts :[], available :false };} 23}