scribble-math/metric_parse.rb
Emily Eisenberg ad97dab19c Update fonts from MathJax
Summary:
Also, rename all of our uses of fonts to use the uppercased versions. We want to
use the uppercase versions because it makes updating and modifying the fonts
much easier (since the font names inside the actual font files are uppercased).

Test Plan:
  - Make sure the huxley screenshots look good (You can compare a diff of them
    on github at
    f90d093361
    By my eye, it seems like some things have moved up ~1/2 pixel, and some of
    the fonts have maybe slightly changed shape, like the large `b` in
    SizingBaseline)

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D11979
2014-08-06 17:52:26 -07:00

48 lines
1.0 KiB
Ruby

require 'json'
require 'rubygems'
require 'ttfunk'
def metrics_for_file(filename)
file = TTFunk::File.open(filename)
per_em = 1.0 * file.header.units_per_em
chars = {}
file.cmap.unicode[0].code_map.sort.each do |u, g|
horiz = file.horizontal_metrics.for(g)
# width = (horiz.advance_width / per_em).round(3)
height = 0
depth = 0
italic = 0
glyph = file.glyph_outlines.for(g)
if glyph
height = (glyph.y_max / per_em).round(3)
depth = (-glyph.y_min / per_em).round(3)
italic = [0, (glyph.x_max - horiz.advance_width) / per_em].max.round(3)
end
chars[u] = {
# :width => width,
:height => height,
:depth => depth,
:italic => italic,
}
end
chars
end
font_dir = File.join(File.dirname(__FILE__), 'static/fonts/')
metrics = {}
%w[Main-Regular Math-Italic AMS-Regular
Size1-Regular Size2-Regular Size3-Regular Size4-Regular].each do |face|
metrics[face] = metrics_for_file(File.join(font_dir, 'KaTeX_%s.ttf' % face))
end
puts "var metricMap = %s;" % metrics.to_json