forked from adam7/delugia-code
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build-hdmx-for-sarasa.py
49 lines (33 loc) · 1.15 KB
/
build-hdmx-for-sarasa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#! /usr/bin/python
# credit: https://github.com/be5invis/Sarasa-Gothic/issues/108#issuecomment-517240248
# usage:
# python build-hdmx-for-sarasa.py your-sarasa-font.ttf
import sys
import math
from fontTools.ttLib import TTFont, newTable
def main():
headFlagInstructionsMayAlterAdvanceWidth = 0x0010
sarasaHintPpemMin = 11
sarasaHintPpemMax = 48
filename = sys.argv[1]
font = TTFont(filename, recalcBBoxes=False)
originalFontHead = font["head"]
originalFontHmtx = font["hmtx"]
originalFontHead.flags |= headFlagInstructionsMayAlterAdvanceWidth
hdmxTable = newTable("hdmx")
hdmxTable.hdmx = {}
# build hdmx table for odd and hinted ppems only.
for ppem in range(
math.floor(sarasaHintPpemMin / 2) * 2 + 1, sarasaHintPpemMax + 1, 2
):
halfUpm = originalFontHead.unitsPerEm / 2
halfPpem = math.ceil(ppem / 2)
hdmxTable.hdmx[ppem] = {
name: math.ceil(width / halfUpm) * halfPpem
for name, (width, _) in originalFontHmtx.metrics.items()
}
font["hdmx"] = hdmxTable
font.save(filename)
font.close()
if __name__ == "__main__":
main()