Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to programmatically get a list of characters a .ttf file supports using Ruby and/or Bash. I am trying to pipe the supported character codes into a text file for later processing.

(I would prefer not to use Font Forge.)

like image 973
briangonzalez Avatar asked Dec 26 '22 19:12

briangonzalez


1 Answers

Found a Ruby gem called ttfunk which can be found here.

After a gem install ttfunk, you can get all unicode characters by running the following script:

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

Which will output something like:

- Found 2815 characters in this font 
["20", "21", "22", "23", ... , "fef8", "fef9", "fefa", "fefb", "fefc", "fffc", "ffff"]
like image 169
briangonzalez Avatar answered Dec 29 '22 12:12

briangonzalez