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
This commit is contained in:
Ben Alpert
2013-08-14 10:57:00 -07:00
parent 9fc357b8a1
commit 7f25fd7dc9
8 changed files with 53 additions and 5067 deletions

46
metric_parse.rb Normal file
View File

@@ -0,0 +1,46 @@
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