KaTeX/metrics/extract_ttfs.py
Emily Eisenberg 79ab06d88d Get rid of the lookahead-y dots
Summary:
The `\dots` and `\dots{c,o,b,i,m}` commands do more than just show characters,
they add extra space and `\dots` also looks ahead to determine what kind of
environment it is in (see [this math.sx post]
(http://tex.stackexchange.com/questions/122491/difference-of-the-dots)). We
can't support that yet, so remove them.

Also, add comments to the extract_ttf script to show what caracters we are
extracting metrics for.

Test Plan: - Make sure tests work

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D13151
2014-09-12 17:29:49 -07:00

52 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
import fontforge
import sys
import json
metrics_to_extract = {
"Main-Regular": [
u"\u2260", # \neq
u"\u2245", # \cong
u"\u0020", # space
u"\u00a0", # nbsp
u"\u2026", # \ldots
u"\u22ef", # \cdots
u"\u22f1", # \ddots
u"\u22ee", # \vdots
]
}
def main():
start_json = json.load(sys.stdin)
for font, chars in metrics_to_extract.iteritems():
fontInfo = fontforge.open("../static/fonts/KaTeX_" + font + ".ttf")
for glyph in fontInfo.glyphs():
try:
char = unichr(glyph.unicode)
except ValueError:
continue
if char in chars:
_, depth, _, height = glyph.boundingBox()
depth = -depth
# TODO(emily): Figure out a real way to calculate this
italic = 0
start_json[font][ord(char)] = {
height: height / fontInfo.em,
depth: depth / fontInfo.em,
italic: italic / fontInfo.em,
}
sys.stdout.write(
json.dumps(start_json, separators=(',', ':'), sort_keys=True))
if __name__ == "__main__":
main()