1 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 # This file is part of systemd.
5 # Copyright 2013-2014 David Herrmann <dh.herrmann@gmail.com>
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.
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.
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/>.
21 # Parse a unifont.hex file and produce a compressed binary-format.
24 from __future__ import print_function
31 # Write "bits" array as binary output.
35 write = getattr(sys.stdout, 'buffer', sys.stdout).write
37 def write_bin_entry(entry):
39 if l != 32 and l != 64:
43 entry += "0" * (64 - l)
45 write(struct.pack('B', int(l / 32))) # width
46 write(struct.pack('B', 0)) # padding
47 write(struct.pack('H', 0)) # padding
48 write(struct.pack('I', 0)) # padding
51 for j in range(0, 16):
56 c = int(entry[i:i+2], 16)
59 write(struct.pack('B', c))
62 write(struct.pack('B', 0x44)) # ASCII: 'D'
63 write(struct.pack('B', 0x56)) # ASCII: 'V'
64 write(struct.pack('B', 0x44)) # ASCII: 'D'
65 write(struct.pack('B', 0x48)) # ASCII: 'H'
66 write(struct.pack('B', 0x52)) # ASCII: 'R'
67 write(struct.pack('B', 0x4d)) # ASCII: 'M'
68 write(struct.pack('B', 0x55)) # ASCII: 'U'
69 write(struct.pack('B', 0x46)) # ASCII: 'F'
70 write(struct.pack('<I', 0)) # compatible-flags
71 write(struct.pack('<I', 0)) # incompatible-flags
72 write(struct.pack('<I', 32)) # header-size
73 write(struct.pack('<H', 8)) # glyph-header-size
74 write(struct.pack('<H', 2)) # glyph-stride
75 write(struct.pack('<Q', 32)) # glyph-body-size
78 for idx in range(len(bits)):
79 write_bin_entry(bits[idx])
82 # Parse hex file into "bits" array
85 def parse_hex_line(bits, line):
86 m = re.match(r"^([0-9A-Fa-f]+):([0-9A-Fa-f]+)$", line)
90 idx = int(m.group(1), 16)
93 # insert skipped lines
94 for i in range(len(bits), idx):
102 for line in sys.stdin:
105 if line.startswith("#"):
108 parse_hex_line(bits, line)
113 # In normal mode we simply read line by line from standard-input and write the
114 # binary-file to standard-output.
117 if __name__ == "__main__":