-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdf2csv.py
More file actions
100 lines (75 loc) · 2.5 KB
/
bdf2csv.py
File metadata and controls
100 lines (75 loc) · 2.5 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
from bdflib import reader
from curses import ascii
import sys
def get_hex(number):
"""
This function return number as hex byte string.
:number: Number to return as string
:return: String with number
"""
hex_string = hex(number).upper()
if len(hex_string) == 3:
hex_string = hex_string[0:2] + "0" + hex_string[2]
return hex_string.replace("X", "x")
def get_letter(letter):
"""
This function return description as letter.
:letter: Number of letter in ASCII
:return: String with description for comment
"""
if letter == 0x20:
return "' ' SP"
if letter < len(ascii.controlnames):
return "\"" + ascii.unctrl(letter) + "\" " + ascii.controlnames[letter]
if ascii.isprint(letter):
if letter == ord("'"): return "'\\''"
if letter == ord("\\"): return "'\\\\'"
return "'" + chr(letter) + "'"
return ascii.unctrl(letter)
def get_comment(letter):
"""
This function return description line for letter.
:letter: Letter to create description for
:return: Description line
"""
return "# " + get_hex(letter) + " " + get_letter(letter)
def get_bitmap(letter):
"""
This function create bitmat in CSV format.
:ltter: Letter as bool array.
:return: CSV format bitmap
"""
result = ""
for line in letter:
for pixel in line:
result += str(int(pixel)) + ","
result += "\n"
return result
def main():
if len(sys.argv) <= 1:
print("Run bdf2csv.py [bdf filename] > [csv filename]")
exit(0)
filename = sys.argv[1]
with open(filename, "rb") as font_file:
font = reader.read_bdf(font_file)
width = font.glyphs[0].bbW
height = font.glyphs[0].bbH
for letter_number in range(256):
try:
letter = font[letter_number]
letter_array = list()
for line in letter.iter_pixels():
letter_array.append(list())
for pixel in line:
letter_array[-1].append(pixel)
while len(letter_array) < height:
letter_array.append(list())
for count in range(len(letter_array)):
while len(letter_array[count]) < width:
letter_array[count].append(False)
except:
letter_array = list([list([False] * width)] * height)
print(get_comment(letter_number))
print(get_bitmap(letter_array))
if __name__ == "__main__":
main()