簡體   English   中英

以編程方式獲取某個.ttf字體文件支持的字符列表

[英]Programmatically get a list of characters a certain .ttf font file supports

有沒有辦法以編程方式獲取.ttf文件支持使用Ruby和/或Bash的字符列表。 我試圖將支持的字符代碼傳輸到文​​本文件中以供以后處理。

(我不想使用Font Forge。)

找到一個名為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"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM