Add the ability to extract metrics from ttfs

Summary:
Upon switching over from ttf metrics to TeX metrics, we lost metrics for a
couple of the characters that are dynamically generated by TeX. Thus TeX doesn't
have metrics for them, but our fonts do have the characters because the MathJax
scripts also dynamically build them. This adds the ability to extract metrics
from the generated font files so that we can use the generated characters
correctly. A better solution would be to dynamically generate the characters
ourselves, but that is much harder, and will be left to a future time.

Test Plan:
 - Make sure typing "\neq \cong \text{ }" produces no warnings in the console.
 - Make sure huxley screenshots look the same

Reviewers: alpert

Reviewed By: alpert

Differential Revision: http://phabricator.khanacademy.org/D13107
This commit is contained in:
Emily Eisenberg 2014-09-12 14:59:28 -07:00
parent f52c84c187
commit 71da6aa50f
4 changed files with 56 additions and 6 deletions

View File

@ -31,7 +31,7 @@ test:
./node_modules/.bin/jasmine-node test/katex-spec.js
metrics:
cd metrics && ./mapping.pl | ./extract_tfms.py | ./replace_line.py
cd metrics && ./mapping.pl | ./extract_tfms.py | ./extract_ttfs.py | ./replace_line.py
clean:
rm -rf build/*

File diff suppressed because one or more lines are too long

View File

@ -3,12 +3,15 @@
There are several requirements for generating the metrics used by KaTeX.
- You need to have an installation of TeX which supports kpathsea (you can check
this by running `tex --version`, and seeing if it has a line that looks like >
kpathsea version 6.2.0
- You need to have an installation of TeX which supports kpathsea. You can check
this by running `tex --version`, and seeing if it has a line that looks like
> kpathsea version 6.2.0
- You need the JSON module for perl. You can install this either from CPAN or with
your package manager.
your package manager.
- You need the python fontforge module. This is probably either installed with
fontforge or can be installed from your package manager.
Once you have these things, run

47
metrics/extract_ttfs.py Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
import fontforge
import sys
import json
metrics_to_extract = {
"Main-Regular": [
u"\u2260",
u"\u2245",
u"\u0020",
u"\u00a0",
]
}
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()