本文目录导读:

Windows (PowerShell)
# 方法1:获取系统字体缓存中的字体
Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" |
Select-Object -ExpandProperty PSObject.Properties |
Where-Object {$_.Name -notlike 'PS*'} |
Format-Table Name, Value -AutoSize
# 方法2:获取当前用户安装的字体
Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\Windows\Fonts" |
Select-Object Name, Length, LastWriteTime
macOS (Bash/Zsh)
#!/bin/bash
# 获取系统字体缓存列表
# 方法1:使用系统字体注册
atsutil fonts -list
# 方法2:获取所有已安装的字体文件
find /System/Library/Fonts /Library/Fonts ~/Library/Fonts -name "*.ttf" -o -name "*.otf" | while read font; do
echo "Font: $(basename "$font")"
echo "Path: $font"
echo "---"
done
# 方法3:使用Python检查字体缓存
python3 -c "
import subprocess
result = subprocess.run(['atsutil', 'databases', '-list'], capture_output=True, text=True)
print(result.stdout)
"
Linux (Bash)
#!/bin/bash
# 获取字体缓存列表
# 方法1:使用fc-list命令
fc-list | awk -F: '{print $1}' | sort -u
# 方法2:查看字体缓存目录
ls -la /var/cache/fontconfig/
# 方法3:使用fc-cache查看缓存状态
fc-cache -v 2>&1 | grep "caching"
# 方法4:获取详细字体信息
fc-list :lang=zh | head -20 # 仅显示中文字体
Python (跨平台)
#!/usr/bin/env python3
import subprocess
import json
import os
import sys
def get_font_cache_windows():
"""Windows字体缓存"""
try:
import winreg
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts")
fonts = []
i = 0
while True:
try:
name, value, _ = winreg.EnumValue(key, i)
fonts.append({"name": name, "path": value})
i += 1
except WindowsError:
break
return fonts
except:
return []
def get_font_cache_mac():
"""macOS字体缓存"""
try:
result = subprocess.run(['atsutil', 'fonts', '-list'],
capture_output=True, text=True)
return result.stdout
except:
return ""
def get_font_cache_linux():
"""Linux字体缓存"""
try:
result = subprocess.run(['fc-list'], capture_output=True, text=True)
fonts = []
for line in result.stdout.split('\n'):
if line.strip():
parts = line.split(':')
if len(parts) >= 2:
fonts.append({
"file": parts[0].strip(),
"name": parts[1].strip() if len(parts) > 1 else ""
})
return fonts
except:
return []
def main():
if sys.platform == "win32":
fonts = get_font_cache_windows()
for font in fonts[:20]: # 显示前20个
print(f"{font['name']}: {font['path']}")
elif sys.platform == "darwin":
print(get_font_cache_mac())
else:
fonts = get_font_cache_linux()
for font in fonts[:20]:
print(f"File: {font['file']}")
print(f"Name: {font['name']}")
if __name__ == "__main__":
main()
Node.js (跨平台)
// font-cache.js
const { exec } = require('child_process');
const os = require('os');
function getFontCache() {
const platform = os.platform();
if (platform === 'win32') {
// Windows: 读取注册表
exec('reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"',
(error, stdout) => {
if (error) {
console.error('Error:', error);
return;
}
console.log('Font cache entries:');
console.log(stdout);
});
} else if (platform === 'darwin') {
// macOS
exec('atsutil fonts -list', (error, stdout) => {
if (error) {
console.error('Error:', error);
return;
}
console.log('Font cache:');
console.log(stdout);
});
} else {
// Linux
exec('fc-list', (error, stdout) => {
if (error) {
console.error('Error:', error);
return;
}
console.log('Font cache:');
console.log(stdout);
});
}
}
getFontCache();
使用注意事项
-
Windows: 字体缓存位于注册表
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts -
macOS: 使用
atsutil命令管理字体缓存 -
Linux: 使用
fontconfig库,缓存文件在/var/cache/fontconfig/ -
权限问题: 某些系统目录需要管理员权限
-
缓存文件: Linux 的字体缓存文件是
.cache-*格式的二进制文件
选择适合你操作系统的脚本,根据需要调整输出格式。