Skip to content

Instantly share code, notes, and snippets.

@xymostech
Created August 8, 2014 21:03
Show Gist options
  • Save xymostech/6189efae9a9998105f14 to your computer and use it in GitHub Desktop.
Save xymostech/6189efae9a9998105f14 to your computer and use it in GitHub Desktop.
require 'json'
require 'rubygems'
require 'ttfunk'
def metrics_for_file(filename)
file = TTFunk::File.open(filename)
max_height = 0
max_depth = 0
file.cmap.unicode[0].code_map.sort.each do |u, g|
glyph = file.glyph_outlines.for(g)
if glyph
max_height = [max_height, glyph.y_max].max
max_depth = [-glyph.y_min, max_depth].max
end
end
return [max_height, max_depth]
end
font_dir = File.join(File.dirname(__FILE__), 'static/fonts/')
%w[Main-Regular Math-Italic AMS-Regular
Size1-Regular Size2-Regular Size3-Regular Size4-Regular].each do |face|
file_name = File.join(font_dir, 'KaTeX_%s.ttf' % face)
ascent, descent = metrics_for_file(file_name)
`./modify_ascent_descent.py #{file_name} #{ascent} #{descent}`
end
#!/usr/bin/env python2
import fontforge
import sys
if len(sys.argv) < 4:
print "Usage: %s <font file> <ascent> <descent>" % sys.argv[0]
sys.exit(1)
font_file = sys.argv[1]
ascent = int(sys.argv[2])
descent = int(sys.argv[3])
font = fontforge.open(font_file)
font.os2_winascent = ascent
font.os2_windescent = descent
font.hhea_ascent = ascent
font.hhea_descent = -descent
font.generate(font_file, flags=("TeX-table"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment