Update README.md
[proportionaltomonofont] / generate.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 """
5 Generates the Mono font files based on a proportional font
6
7 Required files:
8 - vendor/fonttobechanged.ttf
9 - vendor/referencefont.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 - https://github.com/dtinth/comic-mono-font
15 - Comic Mono: https://github.com/dtinth/comic-mono-font
16 """
17
18 import os
19 import re
20 import sys
21
22 reload(sys)
23 sys.setdefaultencoding('UTF8')
24
25 import fontforge
26 import psMat
27 import unicodedata
28
29 def height(font):
30     return float(font.capHeight)
31
32 def adjust_height(source, template, scale):
33     source.selection.all()
34     source.transform(psMat.scale(height(template) / height(source)))
35     for attr in ['ascent', 'descent',
36                 'hhea_ascent', 'hhea_ascent_add',
37                 'hhea_linegap',
38                 'hhea_descent', 'hhea_descent_add',
39                 'os2_winascent', 'os2_winascent_add',
40                 'os2_windescent', 'os2_windescent_add',
41                 'os2_typoascent', 'os2_typoascent_add',
42                 'os2_typodescent', 'os2_typodescent_add',
43                 ]:
44         setattr(source, attr, getattr(template, attr))
45     source.transform(psMat.scale(scale))
46 #change the next to lines to the font you want to convert and the reference font
47 # make sure those two fonts are in the vendor directory
48 font = fontforge.open('vendor/comic-shanns.otf')
49 ref = fontforge.open('vendor/Cousine-Regular.ttf')
50 for g in font.glyphs():
51     uni = g.unicode
52     category = unicodedata.category(unichr(uni)) if 0 <= uni <= sys.maxunicode else None
53     if g.width > 0 and category not in ['Mn', 'Mc', 'Me']:
54         # change target width to either make characters norrow or wide
55         target_width = 510
56         if g.width != target_width:
57             delta = target_width - g.width
58             g.left_side_bearing += delta / 2
59             g.right_side_bearing += delta - g.left_side_bearing
60             g.width = target_width
61 # change next 4 lines to desired information
62 font.familyname = 'Comic Mono'
63 font.version = '0.1.1'
64 font.comment = 'https://github.com/dtinth/comic-mono-font'
65 font.copyright = 'https://github.com/dtinth/comic-mono-font/blob/master/LICENSE'
66 #change number in adjust_height to make font shorter or taller
67 adjust_height(font, ref, 0.875)
68 font.sfnt_names = [] # Get rid of 'Prefered Name' etc.
69 #change to desired font name
70 font.fontname = 'ComicMono'
71 #change to desired full name
72 font.fullname = 'Comic Mono'
73 #change to desired file name
74 font.generate('ComicMono.ttf')
75
76 font.selection.all()
77 #Bold section
78 #change to desired font name
79 font.fontname = 'ComicMono-Bold'
80 #change to desired full name
81 font.fullname = 'Comic Mono Bold'
82 font.weight = 'Bold'
83 font.changeWeight(32, "LCG", 0, 0, "squish")
84 #change to desired file name
85 font.generate('ComicMono-Bold.ttf')