gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:01 27 4
gpt4 key购买 nike

有没有一种方法可以使用 Ruby 和/或 Bash 以编程方式获取 .ttf 文件支持的字符列表。我正在尝试将支持的字符代码通过管道传输到文本文件中以供以后处理。

(我不想使用 Font Forge。)

最佳答案

here 找到了一个名为ttfunk 的Ruby gem .

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

这将输出如下内容:

- Found 2815 characters in this font 
["20", "21", "22", "23", ... , "fef8", "fef9", "fefa", "fefb", "fefc", "fffc", "ffff"]

关于ruby - 以编程方式获取某个 .ttf 字体文件支持的字符列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11895641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com