JPC-RR r11.7
[jpcrr.git] / lua / convert-rawimg-to-font.lua
blob3d41c7b4655052a8125e4d66febd2f4a8893e5d8
1 lines = {};
2 linelen = nil;
3 linecount = 0;
5 component_encode = function(str)
6 mult = 1;
7 value = 0;
8 for i = 1,#str do
9 if i % 8 == 1 then
10 mult = 1;
11 value = 256 * value;
12 end
13 if string.sub(str, i, i) == "1" then
14 value = value + mult;
15 end
16 mult = mult * 2;
17 end
18 if #str < 9 then
19 c2 = value % 93;
20 value = (value - c2) / 93;
21 c1 = value % 93;
22 return string.char(34 + c1, 34 + c2);
23 elseif #str < 17 then
24 c3 = value % 93;
25 value = (value - c3) / 93;
26 c2 = value % 93;
27 value = (value - c2) / 93;
28 c1 = value % 93;
29 return string.char(37 + c1, 34 + c2, 34 + c3);
30 elseif #str < 25 then
31 c4 = value % 93;
32 value = (value - c4) / 93;
33 c3 = value % 93;
34 value = (value - c3) / 93;
35 c2 = value % 93;
36 value = (value - c2) / 93;
37 c1 = value % 93;
38 return string.char(45 + c1, 34 + c2, 34 + c3, 34 + c4);
39 elseif #str < 32 then
40 c5 = value % 93;
41 value = (value - c5) / 93;
42 c4 = value % 93;
43 value = (value - c4) / 93;
44 c3 = value % 93;
45 value = (value - c3) / 93;
46 c2 = value % 93;
47 value = (value - c2) / 93;
48 c1 = value % 93;
49 return string.char(66 + c1, 34 + c2, 34 + c3, 34 + c4, 34 + c5);
50 end
51 end
53 encode_letter = function(x, y, name)
54 letter = prefix;
55 for i = 1,cellheight do
56 letter = letter .. component_encode(string.sub(lines[y + i - 1], x, x + cellwidth));
57 end
58 print("!BEGIN " .. name);
59 print("+" .. letter);
60 end
62 for line in io.stdin:lines() do
63 table.insert(lines, line);
64 if linelen and #line ~= linelen then
65 error("Inconsistent line length");
66 end
67 linelen = #line;
68 linecount = linecount + 1;
69 end
71 if linecount == 0 or linecount % 6 ~= 0 then
72 error("Line count must be positive and divisible by 6");
73 end
75 if (linelen or 0) % 16 ~= 0 then
76 error("Line length must be positive and divisible by 16");
77 end
79 cellwidth = linelen / 16;
80 cellheight = linecount / 6;
82 if cellwidth > 31 then
83 error("Font too wide (max 31 pels)");
84 end
86 prefix = string.char(34, 34 + cellwidth);
88 print("JRSR");
89 for j=0,95 do
90 x = j % 16;
91 y = (j - x) / 16;
92 encode_letter(cellwidth * x + 1, cellheight * y + 1, j + 32);
93 end
94 print("!END");