Skip to content

Instantly share code, notes, and snippets.

@poi519
Last active December 21, 2015 12:40
Show Gist options
  • Save poi519/a24a1097d5592c2b2f3e to your computer and use it in GitHub Desktop.
Save poi519/a24a1097d5592c2b2f3e to your computer and use it in GitHub Desktop.
Ruby script to break svg font into individual glyphs.
require 'nokogiri'
input_file_name = ARGV[0] || "./font.svg"
input_file = File.open input_file_name
doc = Nokogiri::XML input_file
input_file.close
font_face = doc.css("font-face")[0]
units_per_em = font_face["units-per-em"]
ascent = font_face["ascent"]
doc.css("glyph").each do |glyph|
output_doc = Nokogiri::XML::Document.new
root = Nokogiri::XML::Node.new "svg", output_doc
root.add_namespace_definition nil, "http://www.w3.org/2000/svg"
root.add_namespace_definition "xlink", "http://www.w3.org/1999/xlink"
root["width"] = root["height"] = units_per_em
path = Nokogiri::XML::Node.new "path", output_doc
path["d"] = glyph["d"]
path["transform"] = "scale(1, -1) translate(0,-#{ascent})"
output_doc << (root << path)
output_file_name = glyph["unicode"][0].ord.to_i.to_s(16) + ".svg"
puts output_file_name
output_file = File.new(output_file_name, "w")
output_file.write(output_doc.to_xml)
output_file.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment