const package = require('../package');
const fs = require('fs');
const opentype = require('opentype.js');
function findUnsupportedChars(fontPath, text) {
const font = opentype.loadSync(fontPath);
const unsupportedChars = [];
for (let i = 0; i < text.length; i++) {
let char = text[i];
// 处理多字节字符(如emoji)
if (char.codePointAt(0) > 0xFFFF) {
char = text.slice(i, i + 2); // 假设是四字节字符,取两个JavaScript字符
i++; // 跳过下一个字符,因为它已经是当前字符的一部分了
}
const glyphs = font.stringToGlyphs(char);
// 检查是否找到了对应的glyph,并且该glyph有有效的Unicode属性
let isSupported = false;
for (const glyph of glyphs) {
if (glyph.unicode !== undefined || glyph.unicodes.length > 0) {
isSupported = true;
break;
}
}
// 如果没有找到支持的glyph
if (!isSupported) {
unsupportedChars.push(char);
}
}
return unsupportedChars;
}
// const fontPath = '/usr/share/fonts/yimeng3-fonts/碳纤维正大黑简体.ttf';
// const text = '蕸紦的一堂缔约良缘永结匹配同称十指相扣终于白首王小姐';
// 从命令行参数获取字体文件路径和文本
const [nodePath, scriptPath, fontPath, ...textArgs] = process.argv;
const text = textArgs.join(' '); // 将所有文本参数连接成一个字符串
// 验证命令行参数
if (!fontPath || text.length === 0) {
console.error('Usage: node script.js <fontPath> <text>');
process.exit(1);
}
const unsupportedChars = findUnsupportedChars(fontPath, text);
console.log(JSON.stringify(unsupportedChars));
执行示例:fontparseNew 碳纤维正大黑简体.ttf "蕸紦的一堂缔约良缘永结匹配同称十指相扣终于白首王小姐"
命令行执行返回结果:["蕸","紦"]
标签:包不,字符,const,text,fontPath,字体,unsupportedChars,glyph From: https://www.cnblogs.com/anniu1122/p/18460098