Skip to content

Instantly share code, notes, and snippets.

@niw
Created June 16, 2019 03:25
Show Gist options
  • Select an option

  • Save niw/3010ab06271e28630463e124cdc044fb to your computer and use it in GitHub Desktop.

Select an option

Save niw/3010ab06271e28630463e124cdc044fb to your computer and use it in GitHub Desktop.
Extract each font file from TTC font collection file
# See <https://docs.microsoft.com/en-us/typography/opentype/spec/otff#font-collections>
ttc = ARGF
unless ttc.read(4) == "ttcf"
STDERR.puts "Not TTC file."
exit 1
end
major_version, minor_version = ttc.read(4).unpack("nn")
puts "Version: #{major_version}.#{minor_version}"
num_fonts = ttc.read(4).unpack("N").first
puts "Number of fonts: #{num_fonts}"
offsets = num_fonts.times.map do
ttc.read(4).unpack("N").first
end
num_fonts.times do |i|
offset = offsets[i]
puts "Extract font at offset: #{offset}"
ttc.seek(offset)
content = if i + 1 < num_fonts
size = offsets[i + 1] - offset
ttc.read(size)
else
# Read towards the end of file.
ttc.read
end
File.write("#{i}.ttf", content)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment