scribble-math/metric_parse.rb
Emily Eisenberg 100798847b Add delimiter sizing
Summary:
Make delimiter sizing work. This involved
 - Adding the symbols for the remaining delimiters (like `\lfloor` and `\{`)
 - Adding metrics for the size1, size2, size3, and size4 fonts
 - Parsing delimiter sizing functions
 - Using the big fonts when possible, otherwise building large copies of the
   delimiters from scratch

Test Plan:
 - See that
   `\bigl\uparrow\Bigl\downarrow\biggl\updownarrow\Biggl\Uparrow
    \Biggr\Downarrow\biggr\Updownarrow\bigm/\Bigm\backslash\biggm|
    \Biggm|\big\lceil\Big\rceil\bigg\langle\Bigg\rangle\bigl(\Bigl)
    \biggl[\Biggl]\Biggr\{\biggr\}\Bigr\lfloor\bigr\rfloor`
   parses correctly (this contains all of the delimiters, and all of the sizing
   modes)
 - See that the huxley tests didn't change, and the new one looks good
 - See the normal tests work

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D7844
2014-08-05 16:43:43 -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