okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-iE' into f-021-int3616148bb

main
1.6 KiB23 linesraw
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}={}){
7 if(cached&&platform===process.platform&&run===execute)return cached;
8 const read=(cmd,args)=>run(cmd,args,{timeout:20000,maxBuffer:16*1024*1024,windowsHide:true});
9 try{
10  let names=[];
11  if(platform==='darwin'){
12   const {stdout}=await read('/usr/sbin/system_profiler',['SPFontsDataType','-json','-detailLevel','mini']);
13   const 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'){
15   const {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')]);
16   const result=JSON.parse(stdout.replace(/^\uFEFF/,''));names=Array.isArray(result)?result:[result];
17  }else{
18   const {stdout}=await read('fc-list',['-f','%{family}\n']);names=stdout.split(/[\n,]/);
19  }
20  const fonts=[...new Set(names.filter(name=>typeof name==='string').map(name=>name.trim()).filter(Boolean))].sort((a,b)=>a.localeCompare(b));
21  const result={fonts,available:fonts.length>0};if(platform===process.platform&&run===execute)cached=result;return result;
22 }catch{return{fonts:[],available:false};}
23}