import sys import simplejson def parse_input(): """ Used to get the information from the calling process """ data = sys.stdin.readline() return simplejson.loads(data) def complete(data): """ Prints a JSON dictionary to stdout to communicate status to the calling process. """ print simplejson.dumps(data) def original_metrics(original): # Calculate the tallest and widest glyph in ems glyphs = list(original.glyphs()) ascent = max([r.boundingBox()[3] for r in glyphs]) descent = -1 * min([r.boundingBox()[1] for r in glyphs]) results = {} results['os2_winascent'] = original.os2_winascent + ascent results['os2_windescent'] = original.os2_windescent + descent results['os2_typoascent'] = original.os2_typoascent + original.ascent results['os2_typodescent'] = original.os2_typodescent + (-1 * original.descent) results['hhea_ascent'] = original.hhea_ascent + ascent results['hhea_descent'] = original.hhea_descent + (-1 * descent) # It seems to adjust the underline position and thickness results['upos'] = float(original.upos) return results def lossless_save(original_metrics, font, output_filename): """ For some reason fontforge likes to modify things in it's output files, such as underline position. So we are undoing it to maintain the integrity of the font. """ font.os2_winascent_add = 0 # turns off the offset, stupid naming if you ask me. font.os2_winascent = original_metrics['os2_winascent'] font.os2_windescent_add = 0 font.os2_windescent = original_metrics['os2_windescent'] font.os2_typoascent_add = 0 font.os2_typoascent = original_metrics['os2_typoascent'] font.os2_typodescent_add = 0 font.os2_typodescent = original_metrics['os2_typodescent'] font.hhea_ascent_add = 0 font.hhea_ascent = original_metrics['hhea_ascent'] font.hhea_descent_add = 0 font.hhea_descent = original_metrics['hhea_descent'] # Force the font family name to be correct. font.familyname = font.fullname.split('-')[0] # It seems to adjust the underline position and thickness font.upos = original_metrics['upos'] font.generate(output_filename)