monospacify, adjusted metrics, update readme, added comic-shanns-dtinth
[proportionaltomonofont] / dtinth.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 """Update the glyph metrics so that they all really have the same size."""
5
6 # Based on
7 # - monospacifier: https://github.com/cpitclaudel/monospacifier/blob/master/monospacifier.py
8 # - YosemiteAndElCapitanSystemFontPatcher: https://github.com/dtinth/YosemiteAndElCapitanSystemFontPatcher/blob/master/bin/patch
9
10 import os
11 import re
12 import sys
13
14 reload(sys)
15 sys.setdefaultencoding('UTF8')
16
17 import fontforge
18 import psMat
19 import unicodedata
20
21 def height(font):
22     return float(font.capHeight)
23
24 def adjust_height(source, template):
25     source.selection.all()
26     source.transform(psMat.scale(height(template) / height(source)))
27     for attr in ['ascent', 'descent',
28                 'hhea_ascent', 'hhea_ascent_add',
29                 'hhea_linegap',
30                 'hhea_descent', 'hhea_descent_add',
31                 'os2_winascent', 'os2_winascent_add',
32                 'os2_windescent', 'os2_windescent_add',
33                 'os2_typoascent', 'os2_typoascent_add',
34                 'os2_typodescent', 'os2_typodescent_add',
35                 ]:
36         setattr(source, attr, getattr(template, attr))
37     source.transform(psMat.scale(0.9))
38
39 font = fontforge.open('comic-shanns.otf')
40 ref = fontforge.open('vendor/Menlo.ttc')
41 for g in font.glyphs():
42     uni = g.unicode
43     category = unicodedata.category(unichr(uni)) if 0 <= uni <= sys.maxunicode else None
44     if g.width > 0 and category not in ['Mn', 'Mc', 'Me']:
45         target_width = 510
46         if g.width != target_width:
47             delta = target_width - g.width
48             g.left_side_bearing += delta / 2
49             g.right_side_bearing += delta - g.left_side_bearing
50             g.width = target_width
51
52 adjust_height(font, ref)
53 font.sfnt_names = [] # Get rid of 'Prefered Name' etc.
54 font.fontname = 'Comic Shanns dtinth'
55 font.familyname = 'Comic Shanns dtinth'
56 font.fullname = 'Comic Shanns dtinth'
57 font.generate('comic-shanns-dtinth.otf')