chiark / gitweb /
networkd: Begin with serial number 1 for netlink requests
[elogind.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
35 write = getattr(sys.stdout, 'buffer', sys.stdout).write
36
37 def write_bin_entry(entry):
38     l = len(entry)
39     if l != 32 and l != 64:
40         entry = "0" * 64
41         l = 0
42     elif l < 64:
43         entry += "0" * (64 - l)
44
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
49
50     i = 0
51     for j in range(0, 16):
52         for k in range(0, 2):
53             if l <= k * 16 * 2:
54                 c = 0
55             else:
56                 c = int(entry[i:i+2], 16)
57                 i += 2
58
59             write(struct.pack('B', c))
60
61 def write_bin(bits):
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
76
77     # write glyphs
78     for idx in range(len(bits)):
79         write_bin_entry(bits[idx])
80
81 #
82 # Parse hex file into "bits" array
83 #
84
85 def parse_hex_line(bits, line):
86     m = re.match(r"^([0-9A-Fa-f]+):([0-9A-Fa-f]+)$", line)
87     if m == None:
88         return
89
90     idx = int(m.group(1), 16)
91     val = m.group(2)
92
93     # insert skipped lines
94     for i in range(len(bits), idx):
95         bits.append("")
96
97     bits.insert(idx, val)
98
99 def parse_hex():
100     bits = []
101
102     for line in sys.stdin:
103         if not line:
104             continue
105         if line.startswith("#"):
106             continue
107
108         parse_hex_line(bits, line)
109
110     return bits
111
112 #
113 # In normal mode we simply read line by line from standard-input and write the
114 # binary-file to standard-output.
115 #
116
117 if __name__ == "__main__":
118     bits = parse_hex()
119     write_bin(bits)