Terminal program for MailStation devices
1#
2# hexfont2inc
3# convert a hex-exported bitmap font (like from gbdfed) to an asm include file
4# msTERM
5#
6# Copyright (c) 2019 joshua stein <jcs@jcs.org>
7#
8# Permission to use, copy, modify, and distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19#
20
21chars = []
22char_size = 0
23all_bytes = []
24
25if !ARGV[0]
26 puts "usage: #{$0} <hex file converted from bdf>"
27 exit 1
28end
29
30File.open(ARGV[0], "r") do |f|
31 char = 0
32
33 while f && !f.eof?
34 line = f.gets
35
36 # 0023:A0E0A0E0A000
37 if !(m = line.match(/^(....):(.+)$/))
38 raise "unexpected format: #{line.inspect}"
39 end
40
41 char = m[1].to_i(16)
42 char_size = (m[2].length / 2)
43
44 # A0E0A0E0A000
45 # -> [ "A0", "e0", "A0", "e0", "A0", "00", "00" ]
46 bytes = m[2].scan(/(..)/).flatten
47
48 # -> [ 0xa0, 0xe0, 0xa0, 0xe0, 0xa0, 0x00, 0x00 ]
49 bytes = bytes.map{|c| c.to_i(16) }
50
51 # -> [ 101000000, 11100000, ... ]
52 # -> [ [ 1, 0, 1, 0, 0, 0, 0, 0 ], [ 1, 1, 1, 0, 0, 0, 0, 0 ], ... ]
53 bytes = bytes.map{|c| sprintf("%08b", c).split(//).map{|z| z.to_i } }
54
55 # -> [ [ 0, 0, 0, 0, 0, 1, 0, 1 ], [ 0, 0, 0, 0, 0, 1, 1, 1 ], ... ]
56 bytes = bytes.map{|a| a.reverse }
57
58 # -> [ 0x5, 0x7, ... ]
59 bytes = bytes.map{|a| a.join.to_i(2) }
60
61 chars[char] = bytes
62 end
63end
64
65(0 .. 255).each do |c|
66 if chars[c] && chars[c].any?
67 print ".db " << chars[c].map{|c| sprintf("#0x%02x", c) }.join(", ")
68 if c >= 32 && c <= 126
69 print "\t; #{sprintf("%.3d", c)} - #{c.chr}"
70 end
71 print "\n"
72 else
73 puts ".db " << char_size.times.map{|c| "#0x00" }.join(", ")
74 end
75end