50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
# Adds the Turkish letters the menu font lacks, built from its own letters and accents.
|
|
# Usage: python tools/font-turkish.py web/shell/BowlbyOne.ttf
|
|
import sys
|
|
from fontTools.ttLib import TTFont
|
|
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
|
from fontTools.pens.boundsPen import BoundsPen
|
|
|
|
path = sys.argv[1]
|
|
font = TTFont(path)
|
|
glyphs = font.getGlyphSet()
|
|
table = font['glyf']
|
|
cmap = font.getBestCmap()
|
|
cap_height = font['OS/2'].sCapHeight or 1400
|
|
x_height = font['OS/2'].sxHeight or 1000
|
|
|
|
|
|
def bounds(name):
|
|
pen = BoundsPen(glyphs)
|
|
glyphs[name].draw(pen)
|
|
return pen.bounds
|
|
|
|
|
|
def add(code, name, base, accent, above):
|
|
if code in cmap:
|
|
return
|
|
b, a = bounds(base), bounds(accent)
|
|
dx = (b[0] + b[2]) / 2 - (a[0] + a[2]) / 2
|
|
gap = (b[3] - b[1]) * 0.06
|
|
dy = b[3] + gap - a[1] if above else b[1] + gap - a[3]
|
|
pen = TTGlyphPen(glyphs)
|
|
pen.addComponent(base, (1, 0, 0, 1, 0, 0))
|
|
pen.addComponent(accent, (1, 0, 0, 1, round(dx), round(dy)))
|
|
glyph = pen.glyph()
|
|
table[name] = glyph
|
|
font['hmtx'][name] = font['hmtx'][base]
|
|
font.setGlyphOrder(font.getGlyphOrder() if name in font.getGlyphOrder() else font.getGlyphOrder() + [name])
|
|
for t in font['cmap'].tables:
|
|
if t.isUnicode():
|
|
t.cmap[code] = name
|
|
|
|
|
|
add(0x11E, 'Gbreve', 'G', 'breve', True)
|
|
add(0x11F, 'gbreve', 'g', 'breve', True)
|
|
add(0x15E, 'Scedilla', 'S', 'cedilla', False)
|
|
add(0x15F, 'scedilla', 's', 'cedilla', False)
|
|
add(0x130, 'Idotaccent', 'I', 'dotaccent', True)
|
|
font['maxp'].numGlyphs = len(font.getGlyphOrder())
|
|
font.save(path)
|
|
print('ok', len(font.getGlyphOrder()))
|