Files
KaTeX/metric_parse.rb
Ben Alpert 7f25fd7dc9 Parse metrics directly from .ttf files
Summary:
Fixes T1325.

This has the advantage of not having to do the mapping manually. Also it means that characters like \neq are included in the mapping, so this resolves T1323 as well.

I opted to group the metrics by character instead of by metric type because I think it'll gzip better and it makes getCharacterMetrics simpler anyway.

Test Plan: The test page looks identical to the pixel to my eye.

Reviewers: emily

Reviewed By: emily

Maniphest Tasks: T1323, T1325

Differential Revision: http://phabricator.khanacademy.org/D3535
2013-08-14 10:57:00 -07:00

47 lines
997 B
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].each do |face|
metrics[face] = metrics_for_file(File.join(font_dir, 'katex_%s.ttf' % face))
end
puts "var metricMap = %s;" % metrics.to_json