-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfont.py
371 lines (297 loc) · 11.6 KB
/
font.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# coding: utf-8
import freetype
from freetype import (FT_LOAD_DEFAULT, FT_LOAD_NO_BITMAP, FT_LOAD_NO_HINTING,
FT_LOAD_RENDER, FT_RENDER_MODE_NORMAL, Face, Vector)
from io import BytesIO
import codecs
import struct
from PIL import Image
BLANK_CHARS = [u'\r', u'\n']
def f26d6_to_int(val):
ret = (abs(val) & 0x7FFFFFC0) >> 6
if val < 0:
return -ret
else:
return ret
def f16d16_to_int(val):
ret = (abs(val) & 0x3FFFC0) >> 16
if val < 0:
return -ret
else:
return ret
class Font(object):
def __init__(self, ttfPath, pxWidth, pxHeight, fltr, padding, baseline, isAscii=False):
self._fileName = ttfPath
self.FtFont = freetype.Face(ttfPath)
if isAscii:
self.FtFont.set_pixel_sizes(pxHeight, pxHeight)
else:
self.FtFont.set_pixel_sizes(pxWidth, pxHeight)
self.FontSize = pxWidth
self.CellWidth = pxWidth+padding
self.CellHeight = pxHeight+padding
self.GlyphSize = (self.CellWidth, self.CellHeight)
self.BaseLine = pxHeight-baseline
self.Filter = fltr
self.Chars = {}
def addChars(self, chars):
for c in chars:
if not c in self.Chars.keys():
self.addChar(c)
def addChar(self, c):
flags = FT_LOAD_RENDER | FT_LOAD_NO_HINTING
self.FtFont.load_char(c, flags)
glyphslot = self.FtFont.glyph
bitmap = glyphslot.bitmap
buf = bytearray('')
if self._fileName[-3:] != 'bdf':
adv = f26d6_to_int(glyphslot.metrics.horiAdvance)
horiBearingX = f26d6_to_int(glyphslot.metrics.horiBearingX)
horiBearingY = f26d6_to_int(glyphslot.metrics.horiBearingY)
dxo = ((self.CellWidth - bitmap.width) / 2)
dy = self.BaseLine - horiBearingY
buf = bytearray('\x00'*(self.CellWidth*self.CellHeight))
if(dxo + bitmap.width) > self.CellWidth:
dxo -= dxo+bitmap.width-self.CellWidth
for y in range(bitmap.rows):
if(dy >= self.CellHeight):
break
dx = dxo
for x in range(bitmap.width):
if(dx >= self.CellWidth):
break
pos = y * bitmap.width + x
if(pos >= len(bitmap.buffer)):
break
a = ((bitmap.buffer[pos]) & 0xFF)
buf[self.CellWidth*dy+dx] = a
dx += 1
dy += 1
buf = compressGlyphBmp(buf, self.Filter)
else:
buf = [int('{:08b}'.format(btis)[::-1], 2)
for btis in bitmap.buffer]
self.Chars[c] = buf
self.GlyphDataLength = len(buf)
def saveHeader(self, path):
with open(path, 'w')as hdr:
chars_str = ''
glyphs_strs = []
for k in sorted(self.Chars.keys()):
val = self.Chars[k]
chars_str += '0x%04x, ' % ord(k)
s = '{'
for c in val:
s += '0x%02x, ' % (c)
s += '}'
glyphs_strs.append(s)
hdr.write('const uint8_t font_%d[%d][%d] = {\n' % (
self.FontSize, len(self.Chars), len(self.Chars.values()[0])))
hdr.write(',\n'.join(glyphs_strs))
hdr.write('};\n\n')
hdr.write('const uint16_t chars_%d[] = {\n' % self.FontSize)
hdr.write(chars_str)
hdr.write('};\n\n')
hdr.write('const size_t font_%d_char_count = %d;\n\n' %
(self.FontSize, len(self.Chars)))
hdr.write('const uint16_t font_%d_cell_width = %d;\n\n' %
(self.FontSize, self.CellWidth))
hdr.write('const uint16_t font_%d_cell_height = %d;\n\n' %
(self.FontSize, self.CellHeight))
def saveBin(self, path):
with open(path, 'wb') as fnt:
for k in sorted(self.Chars.keys()):
fnt.write(self.Chars[k])
def saveImage(self, path):
glyphsToImg(path, [self.Chars[k]
for k in sorted(self.Chars.keys())], self.GlyphSize, self.Filter)
def getFileEncoding(path):
with open(path, 'rb')as f:
magic = f.read(4)
# utf-8
if(magic[:3] == '\xef\xbb\xbf'):
return 'utf-8-sig'
elif (magic[:2] == '\xff\xfe'):
return 'utf-16le'
elif (magic[:2] == '\xfe\xff'):
return 'utf-16be'
elif (magic == '\x00\x00\xff\xfe'):
return 'utf-32le'
elif (magic == '\xfe\xff\x00\x00'):
return 'utf-32be'
else:
return 'utf-8'
def scanFiles(paths):
chars = []
for path in paths:
encoding = getFileEncoding(path)
with codecs.open(path, 'r', encoding) as f:
print path
text = f.read()
for c in text:
if c not in chars and c not in BLANK_CHARS:
chars.append(c)
return sorted(chars)
def generateFont(ttfPath, size, chars, fltr, padding, baseline, savePath, imgSavePath):
font = Font(ttfPath, size[0], size[1], fltr, padding, baseline)
font.addChars(chars)
font.saveHeader(savePath)
if(imgSavePath):
font.saveImage(imgSavePath)
def generateAsciiFont16(bdfPath, fltr, padding, baseline, savePath, imgSavePath):
font = Font(bdfPath, 8, 13, fltr, padding, baseline, True)
chars = [unichr(i) for i in range(0, 0x4FF)]
font.addChars(chars)
font.saveHeader(savePath)
if(imgSavePath):
font.saveImage(imgSavePath)
def generateUnicodeFont16(ttfPath, fltr, padding, baseline, savePath, imgSavePath):
font = Font(ttfPath, 16, 16, fltr, padding, baseline)
chars = []
chars.extend([unichr(i)
for i in range(0x3000, 0x9FFF)])
chars.extend([unichr(i)
for i in range(0xF900, 0xFFFF)])
font.addChars(chars)
font.saveHeader(savePath)
if(imgSavePath):
font.saveImage(imgSavePath)
def generateBinaryFont16(ttfPath, bdfPath, fltr, padding, baseline, savePath):
halfWidthFont = Font(bdfPath, 8, 13, fltr, padding, baseline, True)
halfWidthFont.addChars([unichr(i) for i in range(0, 0x4FF)])
fullWidthFont1 = Font(ttfPath, 16, 16, fltr, padding, baseline, False)
fullWidthFont1.addChars([unichr(i) for i in range(0x3000, 0x9FFF)])
fullWidthFont2 = Font(ttfPath, 16, 16, fltr, padding, baseline, False)
fullWidthFont2.addChars([unichr(i) for i in range(0xF900, 0xFFFF)])
with open(savePath, 'wb')as fnt:
w = fnt.write
w('FONT')
# Section count
w(struct.pack('HH', 3, 2))
w('\x00'*14*3)
keys1 = sorted(halfWidthFont.Chars.keys())
keys2 = sorted(fullWidthFont1.Chars.keys())
keys3 = sorted(fullWidthFont2.Chars.keys())
font1DataOffset = fnt.tell()
for c in keys1:
w(bytearray(halfWidthFont.Chars[c]))
font2DataOffset = fnt.tell()
for c in keys2:
w(bytearray(fullWidthFont1.Chars[c]))
font3DataOffset = fnt.tell()
for c in keys3:
w(bytearray(fullWidthFont2.Chars[c]))
fnt.seek(8, 0)
writeSection(w, 0, 0x4FF, 8, 13,
halfWidthFont.GlyphDataLength, font1DataOffset)
writeSection(w, 0x3000, 0x9FFF, 16, 16,
fullWidthFont1.GlyphDataLength, font2DataOffset)
writeSection(w, 0xF900, 0xFFFF, 16, 16,
fullWidthFont2.GlyphDataLength, font3DataOffset)
def writeSection(writer, codeStart, codeEnd, charWidth, charHeight, entrySize, dataOffset):
# Code range
writer(struct.pack('<HH', codeStart, codeEnd))
# Char width
writer(struct.pack('<B', charWidth))
# Char height
writer(struct.pack('<B', charHeight))
# Glyph data lenght
writer(struct.pack('<H', entrySize))
writer(struct.pack('<I', dataOffset))
def compressGlyphBmp(bs, fltr):
bits = 0
rotate = 0
result = []
for i in range(len(bs)):
b = bs[i]
if (b >= fltr):
bits |= 1 << rotate
else:
pass
rotate += 1
if(rotate >= 8):
result.append(bits)
rotate = 0
bits = 0
return result
def toImg(pixels1bpp, size):
img = Image.new('RGBA', (size[0], size[1]))
x = 0
y = 0
for bits in pixels1bpp:
mask = 1
while(mask < (1 << 8)):
bit = bits & mask
if(bit == mask):
img.putpixel((x, y), (255, 255, 255, 255))
mask <<= 1
x += 1
if(x == size[0]):
x = 0
y += 1
return img
def glyphsToImg(path, glyphs, glyphSize, fltr):
rows = len(glyphs) / 16 + 1
img = Image.new('RGBA', (16*glyphSize[0], rows*glyphSize[1]))
x = 0
y = 0
for g in glyphs:
i = toImg(g, glyphSize)
img.paste(i, (x*glyphSize[0], y*glyphSize[1]))
x += 1
if(x == 16):
x = 0
y += 1
img.save(path)
if __name__ == "__main__":
import argparse
def parse_options():
parser = argparse.ArgumentParser(
description="Nintendo CTR Font Converter Text Filter(xllt) Generator.")
parser.add_argument('-t', '--ttf', help="Set TrueType font file path.")
parser.add_argument(
'-d', '--bdf', help="Set Bitmap font file path for ascii chars.")
parser.add_argument(
'-s', '--size', help="Set font width.", nargs=2, type=int)
parser.add_argument(
'-f', '--files', help="Files to scan.", nargs='*', type=str)
parser.add_argument(
'-l', '--filter', help="Set bitmap grey filter.", default=90, type=int)
parser.add_argument(
'-p', '--padding', help="Set glyph padding.", default=3, type=int)
parser.add_argument(
'-b', '--baseline', help="Set baseline.", default=3, type=int)
parser.add_argument('-o', '--output', help="Set output file path.")
parser.add_argument(
'-x', '--charset', help="Set raw charset path. If not set, the charset will not save.", default=None)
parser.add_argument(
'-i', '--image', help="Set image path. If not set, the image will not save.", default=False)
parser.add_argument(
'-a', '--ascii', help="Generate ASCII font.", action='store_true')
parser.add_argument(
'-u', '--unicode', help="Generate Unicode font.", action='store_true')
parser.add_argument(
'-r', '--binary', help="Generate Unicode font.", action='store_true')
return parser.parse_args()
def main():
options = parse_options()
if not options:
return False
if options.ascii:
generateAsciiFont16(options.bdf, options.filter, options.padding,
options.baseline, options.output, options.image)
return
if options.unicode:
generateUnicodeFont16(options.ttf, options.filter, options.padding,
options.baseline, options.output, options.image)
return
if options.binary:
generateBinaryFont16(options.ttf, options.bdf, options.filter, options.padding,
options.baseline, options.output)
return
chars = scanFiles(options.files)
if(options.charset):
codecs.open(options.charset, 'w', 'utf-8').write(u''.join(chars))
generateFont(options.ttf, options.size, chars, options.filter,
options.padding, options.baseline, options.output, options.image)
main()