]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/compile-unifont.py
remove unneeded libgen.h includes
[thirdparty/systemd.git] / tools / compile-unifont.py
1 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2 #
3 # This file is part of systemd.
4 #
5 # Copyright 2013-2014 David Herrmann <dh.herrmann@gmail.com>
6 #
7 # systemd is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # systemd is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 #
21 # Parse a unifont.hex file and produce a compressed binary-format.
22 #
23
24 from __future__ import print_function
25 import re
26 import sys
27 import fileinput
28 import struct
29
30 #
31 # Write "bits" array as binary output.
32 #
33
34 def write_bin_entry(entry):
35 l = len(entry)
36 if l != 32 and l != 64:
37 entry = "0" * 64
38 l = 0
39 elif l < 64:
40 entry += "0" * (64 - l)
41
42 sys.stdout.buffer.write(struct.pack('B', int(l / 32))) # width
43 sys.stdout.buffer.write(struct.pack('B', 0)) # padding
44 sys.stdout.buffer.write(struct.pack('H', 0)) # padding
45 sys.stdout.buffer.write(struct.pack('I', 0)) # padding
46
47 i = 0
48 for j in range(0, 16):
49 for k in range(0, 2):
50 if l <= k * 16 * 2:
51 c = 0
52 else:
53 c = int(entry[i:i+2], 16)
54 i += 2
55
56 sys.stdout.buffer.write(struct.pack('B', c))
57
58 def write_bin(bits):
59 sys.stdout.buffer.write(struct.pack('B', 0x44)) # ASCII: 'D'
60 sys.stdout.buffer.write(struct.pack('B', 0x56)) # ASCII: 'V'
61 sys.stdout.buffer.write(struct.pack('B', 0x44)) # ASCII: 'D'
62 sys.stdout.buffer.write(struct.pack('B', 0x48)) # ASCII: 'H'
63 sys.stdout.buffer.write(struct.pack('B', 0x52)) # ASCII: 'R'
64 sys.stdout.buffer.write(struct.pack('B', 0x4d)) # ASCII: 'M'
65 sys.stdout.buffer.write(struct.pack('B', 0x55)) # ASCII: 'U'
66 sys.stdout.buffer.write(struct.pack('B', 0x46)) # ASCII: 'F'
67 sys.stdout.buffer.write(struct.pack('<I', 0)) # compatible-flags
68 sys.stdout.buffer.write(struct.pack('<I', 0)) # incompatible-flags
69 sys.stdout.buffer.write(struct.pack('<I', 32)) # header-size
70 sys.stdout.buffer.write(struct.pack('<H', 8)) # glyph-header-size
71 sys.stdout.buffer.write(struct.pack('<H', 2)) # glyph-stride
72 sys.stdout.buffer.write(struct.pack('<Q', 32)) # glyph-body-size
73
74 # write glyphs
75 for idx in range(len(bits)):
76 write_bin_entry(bits[idx])
77
78 #
79 # Parse hex file into "bits" array
80 #
81
82 def parse_hex_line(bits, line):
83 m = re.match(r"^([0-9A-Fa-f]+):([0-9A-Fa-f]+)$", line)
84 if m == None:
85 return
86
87 idx = int(m.group(1), 16)
88 val = m.group(2)
89
90 # insert skipped lines
91 for i in range(len(bits), idx):
92 bits.append("")
93
94 bits.insert(idx, val)
95
96 def parse_hex():
97 bits = []
98
99 for line in sys.stdin:
100 if not line:
101 continue
102 if line.startswith("#"):
103 continue
104
105 parse_hex_line(bits, line)
106
107 return bits
108
109 #
110 # In normal mode we simply read line by line from standard-input and write the
111 # binary-file to standard-output.
112 #
113
114 if __name__ == "__main__":
115 bits = parse_hex()
116 write_bin(bits)