Add og:image
[proportionaltomonofont] / generate.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 """
5 Generates the Comic Mono font files based on Comic Shanns font.
6
7 Required files:
8 - vendor/comic-shanns.otf
9 - vendor/Cousine-Regular.ttf
10
11 Based on:
12 - monospacifier: https://github.com/cpitclaudel/monospacifier/blob/master/monospacifier.py
13 - YosemiteAndElCapitanSystemFontPatcher: https://github.com/dtinth/YosemiteAndElCapitanSystemFontPatcher/blob/master/bin/patch
14 """
15
16 import os
17 import re
18 import sys
19
20 reload(sys)
21 sys.setdefaultencoding('UTF8')
22
23 import fontforge
24 import psMat
25 import unicodedata
26
27 def height(font):
28     return float(font.capHeight)
29
30 def adjust_height(source, template, scale):
31     source.selection.all()
32     source.transform(psMat.scale(height(template) / height(source)))
33     for attr in ['ascent', 'descent',
34                 'hhea_ascent', 'hhea_ascent_add',
35                 'hhea_linegap',
36                 'hhea_descent', 'hhea_descent_add',
37                 'os2_winascent', 'os2_winascent_add',
38                 'os2_windescent', 'os2_windescent_add',
39                 'os2_typoascent', 'os2_typoascent_add',
40                 'os2_typodescent', 'os2_typodescent_add',
41                 ]:
42         setattr(source, attr, getattr(template, attr))
43     source.transform(psMat.scale(scale))
44
45 font = fontforge.open('vendor/comic-shanns.otf')
46 ref = fontforge.open('vendor/Cousine-Regular.ttf')
47 for g in font.glyphs():
48     uni = g.unicode
49     category = unicodedata.category(unichr(uni)) if 0 <= uni <= sys.maxunicode else None
50     if g.width > 0 and category not in ['Mn', 'Mc', 'Me']:
51         target_width = 510
52         if g.width != target_width:
53             delta = target_width - g.width
54             g.left_side_bearing += delta / 2
55             g.right_side_bearing += delta - g.left_side_bearing
56             g.width = target_width
57
58 font.familyname = 'Comic Mono'
59 font.version = '0.1.1'
60 font.comment = 'https://github.com/dtinth/comic-mono-font'
61 font.copyright = 'https://github.com/dtinth/comic-mono-font/blob/master/LICENSE'
62
63 adjust_height(font, ref, 0.875)
64 font.sfnt_names = [] # Get rid of 'Prefered Name' etc.
65 font.fontname = 'ComicMono'
66 font.fullname = 'Comic Mono'
67 font.generate('ComicMono.ttf')
68
69 font.selection.all()
70 font.fontname = 'ComicMono-Bold'
71 font.fullname = 'Comic Mono Bold'
72 font.weight = 'Bold'
73 font.changeWeight(32, "LCG", 0, 0, "squish")
74 font.generate('ComicMono-Bold.ttf')