paul@40 | 1 | #!/usr/bin/env python |
paul@40 | 2 | |
paul@40 | 3 | """ |
paul@40 | 4 | Convert GNU Unifont format definitions into data statements for GNU as. |
paul@40 | 5 | |
paul@40 | 6 | Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk> |
paul@40 | 7 | |
paul@40 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@40 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@40 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@40 | 11 | version. |
paul@40 | 12 | |
paul@40 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@40 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@40 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@40 | 16 | |
paul@40 | 17 | You should have received a copy of the GNU General Public License along |
paul@40 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@40 | 19 | """ |
paul@40 | 20 | |
paul@40 | 21 | from os.path import split |
paul@40 | 22 | import sys |
paul@40 | 23 | |
paul@40 | 24 | def convert_font(fin, fout, points, missing=32): |
paul@40 | 25 | |
paul@40 | 26 | """ |
paul@40 | 27 | Convert the font obtained via 'fin' to an assembly language representation, |
paul@40 | 28 | writing to 'fout' the characters for the chosen sequence of 'points'. |
paul@40 | 29 | |
paul@40 | 30 | A table of offsets is written to permit each character to be found in the |
paul@40 | 31 | generated data. |
paul@40 | 32 | |
paul@40 | 33 | If 'missing' is specified, the character of the given value will be used |
paul@40 | 34 | for missing character data. |
paul@40 | 35 | """ |
paul@40 | 36 | |
paul@40 | 37 | points.sort() |
paul@40 | 38 | base = points[0] |
paul@40 | 39 | limit = points[-1] |
paul@40 | 40 | |
paul@40 | 41 | # Store the offset of each chosen character. |
paul@40 | 42 | |
paul@40 | 43 | table = [] |
paul@40 | 44 | offset = 0 |
paul@40 | 45 | |
paul@40 | 46 | # Search for each character until no more remain to be found. |
paul@40 | 47 | |
paul@40 | 48 | index = 0 |
paul@40 | 49 | end = len(points) |
paul@40 | 50 | |
paul@40 | 51 | print >>fout, """\ |
paul@40 | 52 | .globl fontchars |
paul@40 | 53 | .globl fonttable |
paul@40 | 54 | .globl fontbase |
paul@40 | 55 | .globl fontlimit |
paul@40 | 56 | |
paul@40 | 57 | .section .flash, "a" |
paul@40 | 58 | |
paul@40 | 59 | fontchars:""" |
paul@40 | 60 | |
paul@40 | 61 | line = fin.readline() |
paul@40 | 62 | |
paul@40 | 63 | while line and index < end: |
paul@40 | 64 | |
paul@40 | 65 | # Obtain the code point and data. |
paul@40 | 66 | |
paul@40 | 67 | point, data = line.rstrip().split(":") |
paul@40 | 68 | point = int(point, 16) |
paul@40 | 69 | |
paul@40 | 70 | # Add null offsets for missing characters. |
paul@40 | 71 | |
paul@40 | 72 | i = len(table) + base |
paul@40 | 73 | while i < point: |
paul@40 | 74 | table.append(None) |
paul@40 | 75 | i += 1 |
paul@40 | 76 | |
paul@40 | 77 | # Add a null offset for unselected characters. |
paul@40 | 78 | |
paul@40 | 79 | if point < points[index]: |
paul@40 | 80 | if point >= base: |
paul@40 | 81 | table.append(None) |
paul@40 | 82 | line = fin.readline() |
paul@40 | 83 | continue |
paul@40 | 84 | |
paul@40 | 85 | # Obtain the byte values from the data. |
paul@40 | 86 | |
paul@40 | 87 | bytes = [] |
paul@40 | 88 | i = 0 |
paul@40 | 89 | while i < len(data): |
paul@40 | 90 | bytes.append("0x%s" % data[i:i+2]) |
paul@40 | 91 | i += 2 |
paul@40 | 92 | |
paul@40 | 93 | # Add an offset for the character. |
paul@40 | 94 | |
paul@40 | 95 | table.append(offset) |
paul@40 | 96 | offset += len(bytes) |
paul@40 | 97 | |
paul@40 | 98 | # Write the character details. |
paul@40 | 99 | |
paul@40 | 100 | print >>fout, "/* %s */" % point |
paul@40 | 101 | print >>fout, ".byte", ", ".join(bytes) |
paul@40 | 102 | |
paul@40 | 103 | index += 1 |
paul@40 | 104 | line = fin.readline() |
paul@40 | 105 | |
paul@40 | 106 | # Add a final offset to mark the end of the data. |
paul@40 | 107 | |
paul@40 | 108 | table.append(offset) |
paul@40 | 109 | |
paul@40 | 110 | # Write the offset table. |
paul@40 | 111 | |
paul@40 | 112 | print >>fout, """ |
paul@40 | 113 | fonttable:""" |
paul@40 | 114 | |
paul@40 | 115 | for offset in table: |
paul@40 | 116 | if offset is None: |
paul@40 | 117 | offset = table[missing - base] |
paul@40 | 118 | print >>fout, ".word %d" % offset |
paul@40 | 119 | |
paul@40 | 120 | print >>fout, """ |
paul@40 | 121 | fontbase: |
paul@40 | 122 | .byte %d |
paul@40 | 123 | |
paul@40 | 124 | fontlimit: |
paul@40 | 125 | .byte %d""" % (base, limit) |
paul@40 | 126 | |
paul@40 | 127 | # Main program. |
paul@40 | 128 | |
paul@40 | 129 | if __name__ == "__main__": |
paul@40 | 130 | |
paul@40 | 131 | # Test options. |
paul@40 | 132 | |
paul@40 | 133 | if "--help" in sys.argv or len(sys.argv) < 3: |
paul@40 | 134 | basename = split(sys.argv[0])[1] |
paul@40 | 135 | print >>sys.stderr, """\ |
paul@40 | 136 | Usage: |
paul@40 | 137 | |
paul@40 | 138 | %s <input filename> <output filename> |
paul@40 | 139 | """ % basename |
paul@40 | 140 | sys.exit(1) |
paul@40 | 141 | |
paul@40 | 142 | base = 32 |
paul@40 | 143 | points = range(32, 127) |
paul@40 | 144 | |
paul@40 | 145 | input_filename, output_filename = sys.argv[1:3] |
paul@40 | 146 | fin = open(input_filename) |
paul@40 | 147 | fout = open(output_filename, "w") |
paul@40 | 148 | try: |
paul@40 | 149 | convert_font(fin, fout, points, base) |
paul@40 | 150 | finally: |
paul@40 | 151 | fin.close() |
paul@40 | 152 | fout.close() |
paul@40 | 153 | |
paul@40 | 154 | # vim: tabstop=4 expandtab shiftwidth=4 |