Skip to content

Instantly share code, notes, and snippets.

@phoenixenero
Created December 6, 2015 10:22
Show Gist options
  • Save phoenixenero/ccc7ade8ae63f9894847 to your computer and use it in GitHub Desktop.
Save phoenixenero/ccc7ade8ae63f9894847 to your computer and use it in GitHub Desktop.
[RUBY] Get all available characters in a .WOFF font
#!/usr/bin/env ruby
require 'optparse'
# Get available characters
#
# Written by Phoenix Enero.
#
# It requires:
# - `woff2sfnt` command-line tool. It can be installed through
# `apt-get install woff-tools` in Ubuntu/Debian.
# - `ttfunk` Ruby gem. Include this in your `Gemfile`
#
# It works by:
# - Converting the WOFF files into a temporary OTF file
# - Accessing the `cmap` table and getting the unicode values
#
# The values get outputted to a file with the same name as the input file that
# generated it. For example, inputting "charter-regular.woff" will output
# "charter-regular.unc"
#
# You can also choose the file extension. By default it is `.unc`
## Lets start!
temp_name = "__#{Time.now.to_i.to_s}.font"
# Option handling
options = {:extension => '.unc', :file => ''}
OptionParser.new do |o|
o.banner = "Usage: get-available-chars.rb [options]"
o.on("-f", "--font [FONT]",
"The source font. Must be in WOFF format (NOT WOFF2)") do |font|
options[:file] = font
end
o.on("-x", "--extension [EXTENSION]",
"The extension for the output file(s).") do |ext|
options[:extension] = ext || '.unc'
# Ensure extension begins with dot `.`
options[:extension].sub!(/\A\.?(?=.)/, ".")
end
end.parse!
# Error handling
at_exit do
if $! # If the program exits due to an exception
# delete temp font if it exists
if File.file?( temp_name )
File.delete( temp_name )
end
puts 'Exiting'
end
end
# Check if font file exists
if ! File.file?( options[:file] )
puts 'Font does not exist'
exit
end
#######################################
# Convert WOFF to OTF/TTF
#######################################
`woff2sfnt "#{options[:file]}" > #{temp_name}`
if ! File.file?( temp_name )
raise 'Failed to generate temporary font'
exit
end
#######################################
# Get available characters
#######################################
require 'ttfunk'
file = TTFunk::File.open( temp_name )
puts "Font name: #{file.name.font_name.join(', ')}"
characters = file.cmap.unicode.first.code_map.keys
# Remove `temp` font
File.delete( temp_name )
#######################################
# Output to file
#######################################
basename = File.basename( options[:file], File.extname( options[:file] ) )
output = File.open( basename + options[:extension],"w" );
# Output characters as a hexadecimal comma-delimited list
output << characters.map{|character| character.to_s(16)}.join(",")
output.close
# And we're done!!!
#!/usr/bin/env ruby
require 'optparse'
# A wrapper of the get-available-chars.rb script for multiple files.
# Option handling
options = {:extension => '.unc', :pattern => '*.woff'}
OptionParser.new do |o|
o.banner = "Usage: get-available-chars.rb [options]"
o.on("-p", "--pattern [PATTERN]", "The pattern used") do |pattern|
options[:pattern] = pattern
end
o.on("-x", "--extension [EXTENSION]", "The extension for the output file(s).") do |ext|
options[:extension] = ext || '.unc'
# Ensure extension begins with dot `.`
options[:extension].sub!(/\A\.?(?=.)/, ".")
end
end.parse!
files = Dir.glob( options[:pattern] )
files.each do |file|
system( "ruby get-available-chars.rb -f \"#{file}\" -x \"#{options[:extension]}\"" )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment