-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadfont.js
214 lines (194 loc) · 5.42 KB
/
loadfont.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Loads a font to the remote system
/*
* Supported arguments:
* -P shows progress indicator.
* -S### sets the first font slot to ### default is 256 - number of fonts
* -H causes the next font on the command-line to NOT be made active. (Default is activate font)
* -A# activates the next font font into alternate font #.
* Multiple files can be sent at the same time. A -H or -A# only affects
* the next font file.
*
* ie: load("loadfonts.js", "-P", "-H", "/path/to/unused/font",
* "/path/to/default/font", "-A1", "/path/to/bright/font",
* "-A2", "/path/to/blink/font", "-A3", "/path/to/bright/blink/font");
*/
load("sbbsdefs.js");
if(bbs.mods.CTerm_Version==undefined)
bbs.mods.CTerm_Version=null;
loadfont();
function loadfont()
{
var filenames=[];
var activate=[undefined, undefined, undefined, undefined];
var showprogress=false;
var firstslot=-1;
var i;
var next_set = -1;
for(i=0; i<argc; i++) {
if(argv[i].toString().substr(0,1)=="-") {
switch(argv[i].substr(1,1).toUpperCase()) {
case 'H': /* Hidden update */
showfont=false;
next_set = -1;
break;
case 'S': /* First font slot */
firstslot=parseInt(argv[i].substr(2));
break;
case 'P': /* Show progress indicator */
showprogress=true;
break;
case 'A': /* Load alternate font */
if(!(next_set=parseInt(argv[i].substr(2))))
next_set=2;
break;
}
}
else {
if (next_set != -1) {
activate[next_set] = filenames.length;
next_set = 0;
}
if(argv[i].constructor==Array)
filenames=filenames.concat(argv[i]);
else
filenames.push(argv[i].toString());
}
}
if(filenames.length==0) {
alert("No font file specified!");
log(LOG_ERR, "!ERROR No font file specified!");
return(0);
}
if(firstslot==-1) {
firstslot=256-filenames.length;
}
if(firstslot+filenames.length > 256) {
alert("No room for "+filenames.length+" fonts if first slot is "+firstslot);
log(LOG_ERR, "!ERROR No room for "+filenames.length+" fonts if first slot is "+firstslot);
return(-1);
}
if(firstslot < 43) {
alert("Cannot overwrite default fonts!");
log(LOG_ERR, "!ERROR Cannot overwrite default fonts!");
return(-1);
}
if(showprogress) {
writeln();
write("Detecting font support... ");
}
// Check if it's CTerm and supports font loading...
var ver=new Array(0,0);
if(bbs.mods.CTerm_Version == undefined || bbs.mods.CTerm_Version == null) {
// Disable parsed input... we need to do ESC processing ourselves here.
var oldctrl=console.ctrlkey_passthru;
console.ctrlkey_passthru=-1;
write("\x1b[c");
var response='';
while(1) {
var ch=console.inkey(0, 5000);
if(ch=="")
break;
response += ch;
if(ch != '\x1b' && ch != '[' && (ch < ' ' || ch > '/') && (ch<'0' || ch > '?'))
break;
}
// var printable=response;
// printable=printable.replace(/\x1b/g,"");
// alert("Response: "+printable);
if(response.substr(0,21) != "\x1b[=67;84;101;114;109;") { // Not CTerm
bbs.mods.CTerm_Version='0';
console.ctrlkey_passthru=oldctrl;
if(showprogress)
writeln("Not detected.");
return(0);
}
if(response.substr(-1) != "c") { // Not a DA
bbs.mods.CTerm_Version='0';
console.ctrlkey_passthru=oldctrl;
if(showprogress)
writeln("Not detected.");
return(0);
}
var version=response.substr(21);
version=version.replace(/c/,"");
bbs.mods.CTerm_Version=version;
ver=version.split(/;/);
console.ctrlkey_passthru=oldctrl;
}
else {
ver=bbs.mods.CTerm_Version.split(/;/);
if(parseInt(ver[0]) < 1 || (parseInt(ver[0])==1 && parseInt(ver[1]) < 61)) {
// Too old for dynamic fonts
if(showprogress)
writeln("Not detected.");
return(0);
}
}
if(parseInt(ver[0]) < 1 || (parseInt(ver[0])==1 && parseInt(ver[1]) < 61)) {
// Too old for dynamic fonts
if(showprogress)
writeln("Not detected.");
return(0);
}
if(showprogress) {
writeln("Detected!");
write("Loading fonts...");
}
for (i in filenames) {
var font=new File(filenames[i]);
if(!font.exists) {
log(LOG_ERR, "!ERROR Cannot load font "+filenames[i]+"!");
continue;
}
var fontsize;
switch(font.length) {
case 4096:
fontsize=0;
break;
case 3586:
fontsize=1;
break;
case 2048:
fontsize=2;
break;
default:
log(LOG_ERR, "!ERROR Illegal font file: "+filenames[i]+"!");
continue;
}
if(!font.open("rb",true,4096)) {
log(LOG_ERR,"!ERROR "+font.error+" Unable to open "+filenames[i]+"! errno="+errno);
continue;
}
// Doesn't work on Win32.. Win32 sucks.
var fontdata=font.read(font.length);
var fonterr=font.error;
if(fontdata.length != font.length) {
log(LOG_ERR,"!ERROR Error "+fonterr+" reading font data (read "+fontdata.length+", expected "+font.length+") errno="+errno);
font.close();
continue;
}
font.close();
if (parseInt(ver[1]) > 212) {
console.write("\x1bPCTerm:Font:"+(firstslot+parseInt(i))+":"+base64_encode(fontdata)+"\x1b\\");
}
else {
console.write("\x1b[="+(firstslot+parseInt(i))+";"+fontsize+"{");
console.write(fontdata);
}
if(showprogress)
console.write(".");
}
if(showprogress)
console.write("done.\r\n");
for (i in activate) {
if (activate[i] !== undefined) {
console.write("\x1b[" + i +";"+(firstslot+activate[i])+" D");
if(i&2) /* Alternate font for Blink attribute */
console.write("\x1b[?34h\x1b[?35h");
if(i&1) /* Alternate font for High-intensity attribute */
console.write("\x1b[?31h");
}
}
console.ctrlkey_passthru=oldctrl;
return(0);
}