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