pythonfontforge_1.py
6.27 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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 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)
def CenterGlyph(junk, afont):
'''Main function.'''
center = (afont.ascent - afont.descent)/2
# total 5% margin
em = afont.ascent + afont.descent
lbearing = int(em*0.05/2)
for glyph in afont.selection.byGlyphs:
center_glyph(glyph, center, lbearing)
def CenterHeight(junk, afont):
'''Main function.'''
center = (afont.ascent - afont.descent)/2
for glyph in afont.selection.byGlyphs:
center_glyph_height(glyph, center)
def fit_glyph(glyph, em, percent=0.95):
'''Scale a glyph horizontally 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/gwidth
#print gwidth, rate, em
# Don't excess the height
if rate*gheight > em:
rate = em*percent/gheight
matrix = psMat.scale(rate)
glyph.transform(matrix)
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 ScaleToEm(percent, afont):
'''Scale a font to fit 5% less than the em.'''
em = afont.ascent + afont.descent
center = (afont.ascent - afont.descent)/2
lbearing = int(em*0.05/2)
if not percent:
ret = fontforge.askString('Scale Percent?',
'Percent of Line Height (em)?', '90' )
if not ret:
return
try:
percent = float(ret)/100
if percent <= 0 or percent > 1:
raise ValueError
except ValueError:
fontforge.postError('Wrong Percent!', "Need a value <= 100.")
return
for glyph in afont.selection.byGlyphs:
fit_glyph(glyph, em, percent)
#center_glyph(glyph, center, lbearing) # Maybe not move glyph?
center_glyph_height(glyph, center)
def get_max_size(afont):
'''Get the max size of the selected glyphs.'''
wmax = 0
hmax = 0
for glyph in afont.selection.byGlyphs:
bbox = glyph.boundingBox()
wmax = max(wmax, bbox[2] - bbox[0])
hmax = max(hmax, bbox[3] - bbox[1])
em = afont.ascent + afont.descent
wpct = wmax and em/wmax*100
hpct = hmax and em/hmax*100
return wmax, hmax, wpct, hpct
def GetSelectedBound(junk, afont):
'''Show max height and width of the selected glyphs.'''
wmax, hmax, wpct, hpct = get_max_size(afont)
msg = ('The max width and height of the selected glyphs are:\n'
'Points:\t%d\t%d\nScale Factor to 100%%:\t%.3f\t %.3f%%')
msg = msg % (wmax, hmax, wpct, hpct)
fontforge.postError('Max Demension', msg)
print ('start')
font = fontforge.font()
#font_in = "/home/dev/git/Magento/AncImage/ancSetup/ancfont.sfd"
#font_in = "/home/dev/git/Magento/AncImage/ancSetup/movie.sfd"
#font = fontforge.open(font_in)
#ScaleToEm(10,font)
fontfile = sys.argv[1]
fontpath = sys.argv[2]
fontchar = sys.argv[3]
# Create a char in the unicode 41 pos (an "A")
#glyph = font.createChar(41, 'B')
glyph = font.createMappedChar(fontchar)
glyph.importOutlines(fontpath+fontfile+'.svg')
#print(glyph.boundingBox())
#print(glyph.activeLayer)
#glyph.removeoverlap()
#GetSelectedBound(glyph ,font)
#print (get_max_size(font))
##
#Keine Ahnung Warum aber so funzt es allerdings buggy
#
#fit_glyph(glyph, 1000, 1)
#fit_glyph(glyph, 1000,0.5);
fit_glyph_height(glyph, 1000, 0.95)
#CenterHeight(glyph, font)
#CenterMain(glyph, font)
center_glyph(glyph, 0, 15, None)
#matrix = psMat.scale(2,.5)
#glyph.transform(matrix)
#CenterHeight(glyph,font)
#matrix = psMat.translate(0,-1800)
#glyph.transform(matrix)
print(glyph.width)
#glyph.intersect()
#font['a'].unicode
glyph = font.createMappedChar('a')
glyph.importOutlines(fontpath+fontfile+'.svg')
#glyph.importOutlines()
#print( glyph.width )
print( font )
# Write the font from memory to a TTF file
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.simplify()
font.removeOverlap()
font.round()
font.autoHint()
#font.em =200
#font.addExtrema()
glyph.getPosSub('*')
font.generate(fontpath+fontfile+'.ttf')
#font.generate(fontpath+fontfile+'.otf', flags=("round", "opentype"))
#print fontpath
#print fontfile
#print (font)
#amb=fontforge.open("Ambrosia.sfd") #Open a font
#amb.selection.select(("ranges",None),"A","Z") #select A-Z
#amb.copy() #Copy those glyphs into the clipboard
#n=fontforge.font() #Create a new font
#n.selection.select(("ranges",None),"A","Z") #select A-Z of it
#n.paste() #paste the glyphs above in
#print n["A"].foreground #test to see that something
# actually got pasted
#n.fontname="NewFont" #Give the new font a name
#n.save("NewFont.sfd") #and save it.