1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/readfont.py Wed May 23 23:57:09 2018 +0200
1.3 @@ -0,0 +1,244 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Convert GNU Unifont format definitions into the .tff font format used by L4Re.
1.8 +Note that this .tff format is *not* .ttf, the latter being TrueType format.
1.9 +Searching the Internet for .tff will yield lots of .ttf nonsense.
1.10 +
1.11 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
1.12 +
1.13 +This program is free software; you can redistribute it and/or modify it under
1.14 +the terms of the GNU General Public License as published by the Free Software
1.15 +Foundation; either version 3 of the License, or (at your option) any later
1.16 +version.
1.17 +
1.18 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
1.19 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.20 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.21 +
1.22 +You should have received a copy of the GNU General Public License along
1.23 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.24 +"""
1.25 +
1.26 +from os.path import split
1.27 +import sys
1.28 +
1.29 +def convert_font(fin, fout, points, missing=32):
1.30 +
1.31 + """
1.32 + Convert the font obtained via 'fin' to an assembly language representation,
1.33 + writing to 'fout' the characters for the chosen sequence of 'points'.
1.34 +
1.35 + A table of offsets is written to permit each character to be found in the
1.36 + generated data.
1.37 +
1.38 + If 'missing' is specified, the character of the given value will be used
1.39 + for missing character data.
1.40 + """
1.41 +
1.42 + points.sort()
1.43 + base = points[0]
1.44 + limit = points[-1]
1.45 +
1.46 + # Store the offset of each chosen character.
1.47 +
1.48 + table = []
1.49 +
1.50 + # Search for each character until no more remain to be found.
1.51 +
1.52 + index = 0
1.53 + end = len(points)
1.54 +
1.55 + # Record character widths and bitmaps.
1.56 +
1.57 + widths = []
1.58 + bitmaps = []
1.59 + height = 16
1.60 +
1.61 + line = fin.readline()
1.62 +
1.63 + while line and index < end:
1.64 + point, width, bytes = read_entry(line, height)
1.65 +
1.66 + # Add a null offset for unselected characters.
1.67 +
1.68 + if point < points[index]:
1.69 + if point >= base:
1.70 + widths.append(None)
1.71 + bitmaps.append(None)
1.72 + line = fin.readline()
1.73 + continue
1.74 +
1.75 + bitmaps.append(bytes)
1.76 + widths.append(width)
1.77 +
1.78 + index += 1
1.79 + line = fin.readline()
1.80 +
1.81 + # Generate the offsets.
1.82 +
1.83 + line_step = 0
1.84 +
1.85 + for width in widths:
1.86 + if width is None:
1.87 + width = widths[missing - base]
1.88 + table.append(line_step)
1.89 + line_step += width
1.90 +
1.91 + # Write the offset table.
1.92 +
1.93 + for offset in table:
1.94 + if offset is None:
1.95 + offset = table[missing - base]
1.96 + fout.write(uint32_out(offset))
1.97 +
1.98 + # Write the widths table.
1.99 +
1.100 + for width in widths:
1.101 + if width is None:
1.102 + width = widths[missing - base]
1.103 + fout.write(uint32_out(width))
1.104 +
1.105 + # Write the line data step size and common height.
1.106 +
1.107 + fout.write(uint32_out(line_step))
1.108 + fout.write(uint32_out(height))
1.109 +
1.110 + # Write the bitmaps.
1.111 +
1.112 + line = 0
1.113 + while line < height:
1.114 + for width, bitmap in zip(widths, bitmaps):
1.115 + if bitmap is None:
1.116 + bitmap = bitmaps[missing - base]
1.117 +
1.118 + bytes_per_line = width / 8
1.119 + start = line * bytes_per_line
1.120 +
1.121 + data = bitmap[start:start+bytes_per_line]
1.122 + fout.write(bits_out(data))
1.123 +
1.124 + line += 1
1.125 +
1.126 +def read_entry(line, height):
1.127 +
1.128 + # Obtain the code point and data.
1.129 +
1.130 + point, data = line.rstrip().split(":")
1.131 + point = int(point, 16)
1.132 +
1.133 + # Each data digit represents four bits.
1.134 +
1.135 + width = (len(data) * 4) / height
1.136 +
1.137 + # Obtain the byte values from the data.
1.138 +
1.139 + bytes = []
1.140 + i = 0
1.141 + while i < len(data):
1.142 + bytes.append(int(data[i:i+2], 16))
1.143 + i += 2
1.144 +
1.145 + return point, width, bytes
1.146 +
1.147 +# Utilities for writing .tff fonts.
1.148 +
1.149 +def bits_out(s):
1.150 + result = []
1.151 + for value in s[::-1]:
1.152 + i = 0
1.153 + while i < 8:
1.154 + result.insert(0, (value & 0x01) and "\xff" or "\x00")
1.155 + value >>= 1
1.156 + i += 1
1.157 + return "".join(result)
1.158 +
1.159 +def uint32_out(value):
1.160 + result = []
1.161 + i = 0
1.162 + while i < 4:
1.163 + result.append(chr(value & 0xff))
1.164 + value >>= 8
1.165 + i += 1
1.166 + return "".join(result)
1.167 +
1.168 +# Utilities for reading .tff fonts.
1.169 +
1.170 +def uint32(s):
1.171 + result = 0
1.172 + i = 3
1.173 + while i >= 0:
1.174 + result = (result << 8) + ord(s[i])
1.175 + i -= 1
1.176 + return result
1.177 +
1.178 +def words(s):
1.179 + l = []
1.180 + i = 0
1.181 + while i < len(s):
1.182 + l.append(s[i:i+4])
1.183 + i += 4
1.184 + return l
1.185 +
1.186 +class Font:
1.187 +
1.188 + "An abstraction for the .tff format."
1.189 +
1.190 + def __init__(self, filename):
1.191 + f = open(filename)
1.192 + try:
1.193 + self.otab = map(uint32, words(f.read(1024)))
1.194 + self.wtab = map(uint32, words(f.read(1024)))
1.195 + self.w = uint32(f.read(4))
1.196 + self.h = uint32(f.read(4))
1.197 + self.img = f.read()
1.198 + finally:
1.199 + f.close()
1.200 +
1.201 + def show(self, c):
1.202 + num = ord(c)
1.203 + offset = self.otab[num]
1.204 + row = 0
1.205 + while row < self.h:
1.206 + start = offset + row * self.w
1.207 + print self.img[start:start + self.wtab[num]].replace("\xff", "#").replace("\x00", ".")
1.208 + row += 1
1.209 +
1.210 +# Main program.
1.211 +
1.212 +if __name__ == "__main__":
1.213 +
1.214 + # Test options.
1.215 +
1.216 + if "--help" in sys.argv or len(sys.argv) < 3:
1.217 + basename = split(sys.argv[0])[1]
1.218 + print >>sys.stderr, """\
1.219 +Usage:
1.220 +
1.221 +%s <input filename> <output filename>
1.222 +""" % basename
1.223 + sys.exit(1)
1.224 +
1.225 + base = 0
1.226 + points = range(base, 256)
1.227 +
1.228 + filename_or_option, tff_filename = sys.argv[1:3]
1.229 +
1.230 + if filename_or_option == "--show":
1.231 + f = Font(tff_filename)
1.232 + for arg in sys.argv[3:]:
1.233 + for char in arg:
1.234 + f.show(char)
1.235 +
1.236 + # Convert from the input to the output file.
1.237 +
1.238 + else:
1.239 + fin = open(filename_or_option)
1.240 + fout = open(tff_filename, "w")
1.241 + try:
1.242 + convert_font(fin, fout, points, base)
1.243 + finally:
1.244 + fin.close()
1.245 + fout.close()
1.246 +
1.247 +# vim: tabstop=4 expandtab shiftwidth=4