pythonfontforge.py
2.16 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import os
import fontforge #Load the module
import psMat
import centerglyph
def center_glyph(glyph, vcenter=0, lbearing=15, rbearing=None):
'''Center a glyph.'''
# Shift width
if not rbearing:
rbearing = lbearing
bounding = glyph.boundingBox() # xmin,ymin,xmax,ymax
width = (bounding[2] - bounding[0])
width_new = width+(lbearing+rbearing)
glyph.width = width_new
glyph.left_side_bearing = lbearing
glyph.right_side_bearing = rbearing
# Shift Height
center_glyph_height(glyph, vcenter, bounding)
def fit_glyph_height(glyph, em, percent=0.95):
'''Scale a glyph verticaly to fit the width.'''
bounding = glyph.boundingBox() # xmin, ymin, xmax, ymax
gwidth = (bounding[2] - bounding[0])
gheight = (bounding[3] - bounding[1])
if not gwidth or not gheight:
return
rate = em*percent/gheight
#print gwidth,gheight, rate, em, percent
# Don't excess the height
#if rate*gwidth > em:
# rate = em*percent/gwidth
matrix = psMat.scale(rate,rate)
#print matrix
glyph.transform(matrix)
def center_glyph_height(glyph, vcenter, abound=None):
bounding = abound or glyph.boundingBox() # xmin,ymin,xmax,ymax
glyphcenter = (bounding[3] - bounding[1])/2+bounding[1]
deltay = vcenter - glyphcenter
matrix = psMat.translate(0, deltay)
glyph.transform(matrix)
print ('start')
font = fontforge.font()
fontfile = sys.argv[1]
fontpath = sys.argv[2]
fontchar = sys.argv[3]
glyph = font.createMappedChar(fontchar)
glyph.importOutlines(fontpath+fontfile+'.svg')
##
# Aufruf um Glyph hoehe auf Maximal zu skalieren und zu positionieren (hoehe und breite werden gleich skaliert)
#
fit_glyph_height(glyph, 1000, 0.95)
center_glyph(glyph, 0, 15, None)
print( font )
font.fullname = fontfile+' Font Medium'
font.familyname = fontfile+' Font'
font.fontname = fontfile+'FontMedium'
font.weight = 'medium'
font.round() # this one is needed to make simplify more reliable
font.removeOverlap()
font.round()
font.autoHint()
glyph.getPosSub('*')
font.generate(fontpath+fontfile+'.ttf')
#font.generate(fontpath+fontfile+'.otf', flags=("round", "opentype"))