Add built-in VGA font
[jpcrr.git] / lua / converthex.lua
blob9020cce97b38c00a05191667ec177cf0948a8bef
1 invalid_data = "007E7E7E4E367A7A76766E6E7E6E7E00";
2 nonexistent_data = "00540240024002400240024002402A00";
4 local hexes = {};
5 hexes[48] = 0;
6 hexes[49] = 8;
7 hexes[50] = 4;
8 hexes[51] = 12;
9 hexes[52] = 2;
10 hexes[53] = 10;
11 hexes[54] = 6;
12 hexes[55] = 14;
13 hexes[56] = 1;
14 hexes[57] = 9;
15 hexes[65] = 5;
16 hexes[66] = 11;
17 hexes[67] = 3;
18 hexes[68] = 13;
19 hexes[69] = 7;
20 hexes[70] = 15;
21 hexes[97] = 5;
22 hexes[98] = 13;
23 hexes[99] = 3;
24 hexes[100] = 11;
25 hexes[101] = 7;
26 hexes[102] = 15;
28 known_args = {};
29 known_args["input"] = true;
30 known_args["output"] = true;
31 known_args["spacehack"] = true;
33 args = args or {};
35 spacehack=false;
37 for k, v in ipairs(arg) do
38 name, value = string.match(v, "([%w_-]+)=(.*)");
39 if not name or not value then
40 error("Bad argument: '" .. v .. "'.");
41 end
42 args[name] = value;
43 end
45 for k, v in pairs(args) do
46 if not known_args[k] then
47 print("Warning: Unknown option '" .. k .. "'.");
48 end
49 end
51 if jpcrr then
52 -- This is JPC-RR embedded Selfstanding Lua interpretter.
53 print("Detected environment: JPC-RR embedded Lua interpretter");
54 if not args["input"] or not args["output"] then
55 error("Syntax: input=<input> and output=<output> required");
56 end
57 tmpfile = io.open_read(args["input"]);
58 infile = tmpfile:text();
59 outfile = io.open_write(args["output"]);
60 else
61 -- Assume reference implementation or compatible.
62 print("Detected environment: Reference Lua interpretter or compatible");
63 if not args["input"] or not args["output"] then
64 error("Syntax: input=<input> and output=<output> required");
65 end
66 infile = io.open(args["input"], "r");
67 outfile = io.open(args["output"], "w");
68 end
70 if args["spacehack"] then
71 spacehack = true;
72 end
74 write_data_to_file = function(data)
75 outfile:write(data);
76 end
78 read_data_from_file = function()
79 return infile:read();
80 end
82 parsenumber = function(hexnumber)
83 local n = tonumber("0x" .. hexnumber);
84 if not n then
85 error("Bad number " .. hexnumber);
86 end
87 return n;
88 end
90 hex_to_member_data = function(codepoint, hex)
91 local s = "!BEGIN " .. tostring(codepoint) .. "\n";
92 if #hex == 32 then
93 s = s .. "+\"*" -- width 8
94 elseif #hex == 64 then
95 s = s .. "+\"2" -- width 16
96 else
97 error("Unknown font format " .. (#hex) .. ".");
98 end
99 local i;
100 for i = 0,(#hex - 1),8 do
101 local c1, c2, c3, c4, c5, c6, c7, c8;
102 c1 = hexes[string.byte(hex, i + 1)];
103 c2 = hexes[string.byte(hex, i + 2)];
104 c3 = hexes[string.byte(hex, i + 3)];
105 c4 = hexes[string.byte(hex, i + 4)];
106 c5 = hexes[string.byte(hex, i + 5)];
107 c6 = hexes[string.byte(hex, i + 6)];
108 c7 = hexes[string.byte(hex, i + 7)];
109 c8 = hexes[string.byte(hex, i + 8)];
110 local val;
111 val = c2 * 268435456 + c1 * 16777216 + c4 * 1048576 + c3 * 65536 +
112 c6 * 4096 + c5 * 256 + c8 * 16 + c7;
113 local x1, x2, x3, x4, x5;
114 x5 = val % 93;
115 val = (val - x5) / 93;
116 x4 = val % 93;
117 val = (val - x4) / 93;
118 x3 = val % 93;
119 val = (val - x3) / 93;
120 x2 = val % 93;
121 val = (val - x2) / 93;
122 x1 = val % 93;
123 s = s .. string.char(x1 + 66, x2 + 34, x3 + 34, x4 + 34, x5 + 34);
125 return s .. "\n";
128 local codepoints = 0;
130 write_codepoint = function(codepoint, hex)
131 write_data_to_file(hex_to_member_data(codepoint, hex));
132 codepoints = codepoints + 1;
133 if codepoints % 1000 == 0 then
134 print("Wrote " .. codepoints .. " codepoints");
139 write_data_to_file("JRSR\n");
140 write_codepoint("invalid", invalid_data);
141 write_codepoint("nonexistent", nonexistent_data);
142 if spacehack then
143 write_codepoint(32, "00000000000000000000000000000000");
146 local line = read_data_from_file();
147 while line do
148 if #line > 0 then
149 sep = string.find(line, ":");
150 if not sep then
151 error("Badly formatted line (" .. line .. ")");
153 local code, hexes;
154 code = parsenumber(string.sub(line, 1, sep - 1));
155 hexes = string.sub(line, sep + 1);
156 if code ~= 32 or not spacehack then
157 write_codepoint(code, hexes);
160 line = read_data_from_file();
164 write_data_to_file("!END\n");
165 print("Converted " .. (codepoints - 2) .. " codepoints");