以编程方式获取某个.ttf字体文件支持的字符列表

bri*_*lez 4 ruby bash fonts file font-face

有没有办法以编程方式获取.ttf文件支持使用Ruby和/或Bash支持的字符列表.我试图将支持的字符代码传输到文​​本文件中以便以后处理.

(我不想使用Font Forge.)

bri*_*lez 10

找到一个ttfunk可以在这里找到的Ruby gem .

在a之后gem install ttfunk,您可以通过运行以下脚本来获取所有unicode字符:

require 'ttfunk'

file = TTFunk::File.open("path/to/font.ttf")
cmap = file.cmap

chars = {}
unicode_chars = []

cmap.tables.each do |subtable|
  next if !subtable.unicode?
  chars = chars.merge( subtable.code_map )
end

unicode_chars = chars.keys.map{ |dec| dec.to_s(16) }

puts "\n -- Found #{unicode_chars.length} characters in this font \n\n"
p unicode_chars
Run Code Online (Sandbox Code Playgroud)

这将输出如下内容:

- Found 2815 characters in this font 
["20", "21", "22", "23", ... , "fef8", "fef9", "fefa", "fefb", "fefc", "fffc", "ffff"]
Run Code Online (Sandbox Code Playgroud)