|
|
|
|
|
from fontTools.ttLib import TTFont |
|
import sys |
|
import json |
|
|
|
|
|
metrics_to_extract = { |
|
|
|
"AMS-Regular": { |
|
u"\u21e2": None, |
|
u"\u21e0": None, |
|
}, |
|
"Main-Regular": { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u"\u2245": None, |
|
u"\u2026": None, |
|
u"\u22ef": None, |
|
u"\u22f1": None, |
|
u"\u22ee": None, |
|
u"\u22ee": None, |
|
u"\u22a8": None, |
|
u"\u22c8": None, |
|
u"\u2250": None, |
|
u"\u23b0": None, |
|
u"\u23b1": None, |
|
u"\u27ee": None, |
|
u"\u27ef": None, |
|
u"\u27f5": None, |
|
u"\u27f8": None, |
|
u"\u27f6": None, |
|
u"\u27f9": None, |
|
u"\u27f7": None, |
|
u"\u27fa": None, |
|
u"\u21a6": None, |
|
u"\u27fc": None, |
|
u"\u21a9": None, |
|
u"\u21aa": None, |
|
u"\u21cc": None, |
|
}, |
|
"Main-Bold": { |
|
u"\u2245": None, |
|
}, |
|
"Size1-Regular": { |
|
u"\u222c": u"\u222b", |
|
u"\u222d": u"\u222b", |
|
}, |
|
"Size2-Regular": { |
|
u"\u222c": u"\u222b", |
|
u"\u222d": u"\u222b", |
|
}, |
|
} |
|
|
|
|
|
def main(): |
|
start_json = json.load(sys.stdin) |
|
|
|
for font in start_json: |
|
fontInfo = TTFont("../../fonts/KaTeX_" + font + ".ttf") |
|
glyf = fontInfo["glyf"] |
|
widths = fontInfo.getGlyphSet() |
|
unitsPerEm = float(fontInfo["head"].unitsPerEm) |
|
|
|
|
|
|
|
|
|
|
|
cmap = [t.cmap for t in fontInfo["cmap"].tables |
|
if (t.platformID == 0) |
|
or (t.platformID == 3 and t.platEncID in (1, 10))] |
|
|
|
chars = metrics_to_extract.get(font, {}) |
|
chars[u"\u0020"] = None |
|
chars[u"\u00a0"] = None |
|
|
|
for char, base_char in chars.items(): |
|
code = ord(char) |
|
names = set(t.get(code) for t in cmap) |
|
if not names: |
|
sys.stderr.write( |
|
"Codepoint {} of font {} maps to no name\n" |
|
.format(code, font)) |
|
continue |
|
if len(names) != 1: |
|
sys.stderr.write( |
|
"Codepoint {} of font {} maps to multiple names: {}\n" |
|
.format(code, font, ", ".join(sorted(names)))) |
|
continue |
|
name = names.pop() |
|
|
|
height = depth = italic = skew = width = 0 |
|
glyph = glyf[name] |
|
if glyph.numberOfContours: |
|
height = glyph.yMax / unitsPerEm |
|
depth = -glyph.yMin / unitsPerEm |
|
width = widths[name].width / unitsPerEm |
|
if base_char: |
|
base_char_str = str(ord(base_char)) |
|
base_metrics = start_json[font][base_char_str] |
|
italic = base_metrics["italic"] |
|
skew = base_metrics["skew"] |
|
width = base_metrics["width"] |
|
|
|
start_json[font][str(code)] = { |
|
"height": height, |
|
"depth": depth, |
|
"italic": italic, |
|
"skew": skew, |
|
"width": width |
|
} |
|
|
|
sys.stdout.write( |
|
json.dumps(start_json, separators=(',', ':'), sort_keys=True)) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|