This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
hterm_vt_character_map.js
446 lines (383 loc) · 12.7 KB
/
hterm_vt_character_map.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Character map object.
*
* Mapping from received to display character, used depending on the active
* VT character set.
*
* GR maps are not currently supported.
*
* @param {string} description A human readable description of this map.
* @param {?Object} glmap The GL mapping from input to output characters.
* @constructor
*/
hterm.VT.CharacterMap = function(description, glmap) {
/**
* Short description for this character set, useful for debugging.
*/
this.description = description;
/**
* The function to call to when this map is installed in GL.
*/
this.GL = null;
// Always keep an unmodified reference to the map.
// This allows us to sanely reset back to the original state.
this.glmapBase_ = glmap;
// Now sync the internal state as needed.
this.sync_();
};
/**
* Internal helper for resyncing internal state.
*
* Used when the mappings change.
*
* @param {!Object=} glmap Additional mappings to overlay on top of the
* base mapping.
*/
hterm.VT.CharacterMap.prototype.sync_ = function(glmap = undefined) {
// If there are no maps, then reset the state back.
if (!this.glmapBase_ && !glmap) {
this.GL = null;
delete this.glmap_;
delete this.glre_;
return;
}
// Set the the GL mapping. If we're given a custom mapping, then create a
// new object to hold the merged map. This way we can cleanly reset back.
if (glmap) {
this.glmap_ = Object.assign({}, this.glmapBase_, glmap);
} else {
this.glmap_ = this.glmapBase_;
}
const glchars = Object.keys(lib.notNull(this.glmap_)).map((key) =>
'\\x' + lib.f.zpad(key.charCodeAt(0).toString(16), 2));
this.glre_ = new RegExp('[' + glchars.join('') + ']', 'g');
this.GL = (str) => str.replace(this.glre_, (ch) => this.glmap_[ch]);
};
/**
* Reset map back to original mappings (discarding runtime updates).
*
* Specifically, any calls to setOverrides will be discarded.
*/
hterm.VT.CharacterMap.prototype.reset = function() {
// If we haven't been given a custom mapping, then there's nothing to reset.
if (this.glmap_ !== this.glmapBase_) {
this.sync_();
}
};
/**
* Merge custom changes to this map.
*
* The input map need not duplicate the existing mappings as it is merged with
* the existing base map (what was created with). Subsequent calls to this
* will throw away previous override settings.
*
* @param {!Object} glmap The custom map to override existing mappings.
*/
hterm.VT.CharacterMap.prototype.setOverrides = function(glmap) {
this.sync_(glmap);
};
/**
* Return a copy of this mapping.
*
* @return {!hterm.VT.CharacterMap} A new hterm.VT.CharacterMap instance.
*/
hterm.VT.CharacterMap.prototype.clone = function() {
const map = new hterm.VT.CharacterMap(this.description, this.glmapBase_);
if (this.glmap_ !== this.glmapBase_) {
map.setOverrides(this.glmap_);
}
return map;
};
/**
* Table of character maps.
*
* @constructor
*/
hterm.VT.CharacterMaps = function() {
this.maps_ = hterm.VT.CharacterMaps.DefaultMaps;
// Always keep an unmodified reference to the map.
// This allows us to sanely reset back to the original state.
this.mapsBase_ = this.maps_;
};
/**
* Look up a previously registered map.
*
* @param {string} name The name of the map to lookup.
* @return {!hterm.VT.CharacterMap|undefined} The map, if it's been registered
* or undefined.
*/
hterm.VT.CharacterMaps.prototype.getMap = function(name) {
if (this.maps_.hasOwnProperty(name)) {
return this.maps_[name];
} else {
return undefined;
}
};
/**
* Register a new map.
*
* Any previously registered maps by this name will be discarded.
*
* @param {string} name The name of the map.
* @param {!hterm.VT.CharacterMap} map The map to register.
*/
hterm.VT.CharacterMaps.prototype.addMap = function(name, map) {
if (this.maps_ === this.mapsBase_) {
this.maps_ = Object.assign({}, this.mapsBase_);
}
this.maps_[name] = map;
};
/**
* Reset the table and all its maps back to original state.
*/
hterm.VT.CharacterMaps.prototype.reset = function() {
if (this.maps_ !== hterm.VT.CharacterMaps.DefaultMaps) {
this.maps_ = hterm.VT.CharacterMaps.DefaultMaps;
}
};
/**
* Merge custom changes to this table.
*
* @param {!Object} maps A set of hterm.VT.CharacterMap objects.
*/
hterm.VT.CharacterMaps.prototype.setOverrides = function(maps) {
if (this.maps_ === this.mapsBase_) {
this.maps_ = Object.assign({}, this.mapsBase_);
}
for (const name in maps) {
const map = this.getMap(name);
if (map !== undefined) {
this.maps_[name] = map.clone();
this.maps_[name].setOverrides(maps[name]);
} else {
this.addMap(name, new hterm.VT.CharacterMap('user ' + name, maps[name]));
}
}
};
/**
* The default set of supported character maps.
*/
hterm.VT.CharacterMaps.DefaultMaps = {};
/**
* VT100 Graphic character map.
* http://vt100.net/docs/vt220-rm/table2-4.html
*/
hterm.VT.CharacterMaps.DefaultMaps['0'] = new hterm.VT.CharacterMap(
'graphic', {
'\x60':'\u25c6', // ` -> diamond
'\x61':'\u2592', // a -> grey-box
'\x62':'\u2409', // b -> h/t
'\x63':'\u240c', // c -> f/f
'\x64':'\u240d', // d -> c/r
'\x65':'\u240a', // e -> l/f
'\x66':'\u00b0', // f -> degree
'\x67':'\u00b1', // g -> +/-
'\x68':'\u2424', // h -> n/l
'\x69':'\u240b', // i -> v/t
'\x6a':'\u2518', // j -> bottom-right
'\x6b':'\u2510', // k -> top-right
'\x6c':'\u250c', // l -> top-left
'\x6d':'\u2514', // m -> bottom-left
'\x6e':'\u253c', // n -> line-cross
'\x6f':'\u23ba', // o -> scan1
'\x70':'\u23bb', // p -> scan3
'\x71':'\u2500', // q -> scan5
'\x72':'\u23bc', // r -> scan7
'\x73':'\u23bd', // s -> scan9
'\x74':'\u251c', // t -> left-tee
'\x75':'\u2524', // u -> right-tee
'\x76':'\u2534', // v -> bottom-tee
'\x77':'\u252c', // w -> top-tee
'\x78':'\u2502', // x -> vertical-line
'\x79':'\u2264', // y -> less-equal
'\x7a':'\u2265', // z -> greater-equal
'\x7b':'\u03c0', // { -> pi
'\x7c':'\u2260', // | -> not-equal
'\x7d':'\u00a3', // } -> british-pound
'\x7e':'\u00b7', // ~ -> dot
});
/**
* British character map.
* http://vt100.net/docs/vt220-rm/table2-5.html
*/
hterm.VT.CharacterMaps.DefaultMaps['A'] = new hterm.VT.CharacterMap(
'british', {
'\x23': '\u00a3', // # -> british-pound
});
/**
* US ASCII map, no changes.
*/
hterm.VT.CharacterMaps.DefaultMaps['B'] = new hterm.VT.CharacterMap(
'us', null);
/**
* Dutch character map.
* http://vt100.net/docs/vt220-rm/table2-6.html
*/
hterm.VT.CharacterMaps.DefaultMaps['4'] = new hterm.VT.CharacterMap(
'dutch', {
'\x23': '\u00a3', // # -> british-pound
'\x40': '\u00be', // @ -> 3/4
'\x5b': '\u0132', // [ -> 'ij' ligature (xterm goes with \u00ff?)
'\x5c': '\u00bd', // \ -> 1/2
'\x5d': '\u007c', // ] -> vertical bar
'\x7b': '\u00a8', // { -> two dots
'\x7c': '\u0066', // | -> f
'\x7d': '\u00bc', // } -> 1/4
'\x7e': '\u00b4', // ~ -> acute
});
/**
* Finnish character map.
* http://vt100.net/docs/vt220-rm/table2-7.html
*/
hterm.VT.CharacterMaps.DefaultMaps['C'] =
hterm.VT.CharacterMaps.DefaultMaps['5'] = new hterm.VT.CharacterMap(
'finnish', {
'\x5b': '\u00c4', // [ -> 'A' umlaut
'\x5c': '\u00d6', // \ -> 'O' umlaut
'\x5d': '\u00c5', // ] -> 'A' ring
'\x5e': '\u00dc', // ~ -> 'u' umlaut
'\x60': '\u00e9', // ` -> 'e' acute
'\x7b': '\u00e4', // { -> 'a' umlaut
'\x7c': '\u00f6', // | -> 'o' umlaut
'\x7d': '\u00e5', // } -> 'a' ring
'\x7e': '\u00fc', // ~ -> 'u' umlaut
});
/**
* French character map.
* http://vt100.net/docs/vt220-rm/table2-8.html
*/
hterm.VT.CharacterMaps.DefaultMaps['R'] = new hterm.VT.CharacterMap(
'french', {
'\x23': '\u00a3', // # -> british-pound
'\x40': '\u00e0', // @ -> 'a' grave
'\x5b': '\u00b0', // [ -> ring
'\x5c': '\u00e7', // \ -> 'c' cedilla
'\x5d': '\u00a7', // ] -> section symbol (double s)
'\x7b': '\u00e9', // { -> 'e' acute
'\x7c': '\u00f9', // | -> 'u' grave
'\x7d': '\u00e8', // } -> 'e' grave
'\x7e': '\u00a8', // ~ -> umlaut
});
/**
* French Canadian character map.
* http://vt100.net/docs/vt220-rm/table2-9.html
*/
hterm.VT.CharacterMaps.DefaultMaps['Q'] = new hterm.VT.CharacterMap(
'french canadian', {
'\x40': '\u00e0', // @ -> 'a' grave
'\x5b': '\u00e2', // [ -> 'a' circumflex
'\x5c': '\u00e7', // \ -> 'c' cedilla
'\x5d': '\u00ea', // ] -> 'e' circumflex
'\x5e': '\u00ee', // ^ -> 'i' circumflex
'\x60': '\u00f4', // ` -> 'o' circumflex
'\x7b': '\u00e9', // { -> 'e' acute
'\x7c': '\u00f9', // | -> 'u' grave
'\x7d': '\u00e8', // } -> 'e' grave
'\x7e': '\u00fb', // ~ -> 'u' circumflex
});
/**
* German character map.
* http://vt100.net/docs/vt220-rm/table2-10.html
*/
hterm.VT.CharacterMaps.DefaultMaps['K'] = new hterm.VT.CharacterMap(
'german', {
'\x40': '\u00a7', // @ -> section symbol (double s)
'\x5b': '\u00c4', // [ -> 'A' umlaut
'\x5c': '\u00d6', // \ -> 'O' umlaut
'\x5d': '\u00dc', // ] -> 'U' umlaut
'\x7b': '\u00e4', // { -> 'a' umlaut
'\x7c': '\u00f6', // | -> 'o' umlaut
'\x7d': '\u00fc', // } -> 'u' umlaut
'\x7e': '\u00df', // ~ -> eszett
});
/**
* Italian character map.
* http://vt100.net/docs/vt220-rm/table2-11.html
*/
hterm.VT.CharacterMaps.DefaultMaps['Y'] = new hterm.VT.CharacterMap(
'italian', {
'\x23': '\u00a3', // # -> british-pound
'\x40': '\u00a7', // @ -> section symbol (double s)
'\x5b': '\u00b0', // [ -> ring
'\x5c': '\u00e7', // \ -> 'c' cedilla
'\x5d': '\u00e9', // ] -> 'e' acute
'\x60': '\u00f9', // ` -> 'u' grave
'\x7b': '\u00e0', // { -> 'a' grave
'\x7c': '\u00f2', // | -> 'o' grave
'\x7d': '\u00e8', // } -> 'e' grave
'\x7e': '\u00ec', // ~ -> 'i' grave
});
/**
* Norwegian/Danish character map.
* http://vt100.net/docs/vt220-rm/table2-12.html
*/
hterm.VT.CharacterMaps.DefaultMaps['E'] =
hterm.VT.CharacterMaps.DefaultMaps['6'] = new hterm.VT.CharacterMap(
'norwegian/danish', {
'\x40': '\u00c4', // @ -> 'A' umlaut
'\x5b': '\u00c6', // [ -> 'AE' ligature
'\x5c': '\u00d8', // \ -> 'O' stroke
'\x5d': '\u00c5', // ] -> 'A' ring
'\x5e': '\u00dc', // ^ -> 'U' umlaut
'\x60': '\u00e4', // ` -> 'a' umlaut
'\x7b': '\u00e6', // { -> 'ae' ligature
'\x7c': '\u00f8', // | -> 'o' stroke
'\x7d': '\u00e5', // } -> 'a' ring
'\x7e': '\u00fc', // ~ -> 'u' umlaut
});
/**
* Spanish character map.
* http://vt100.net/docs/vt220-rm/table2-13.html
*/
hterm.VT.CharacterMaps.DefaultMaps['Z'] = new hterm.VT.CharacterMap(
'spanish', {
'\x23': '\u00a3', // # -> british-pound
'\x40': '\u00a7', // @ -> section symbol (double s)
'\x5b': '\u00a1', // [ -> '!' inverted
'\x5c': '\u00d1', // \ -> 'N' tilde
'\x5d': '\u00bf', // ] -> '?' inverted
'\x7b': '\u00b0', // { -> ring
'\x7c': '\u00f1', // | -> 'n' tilde
'\x7d': '\u00e7', // } -> 'c' cedilla
});
/**
* Swedish character map.
* http://vt100.net/docs/vt220-rm/table2-14.html
*/
hterm.VT.CharacterMaps.DefaultMaps['7'] =
hterm.VT.CharacterMaps.DefaultMaps['H'] = new hterm.VT.CharacterMap(
'swedish', {
'\x40': '\u00c9', // @ -> 'E' acute
'\x5b': '\u00c4', // [ -> 'A' umlaut
'\x5c': '\u00d6', // \ -> 'O' umlaut
'\x5d': '\u00c5', // ] -> 'A' ring
'\x5e': '\u00dc', // ^ -> 'U' umlaut
'\x60': '\u00e9', // ` -> 'e' acute
'\x7b': '\u00e4', // { -> 'a' umlaut
'\x7c': '\u00f6', // | -> 'o' umlaut
'\x7d': '\u00e5', // } -> 'a' ring
'\x7e': '\u00fc', // ~ -> 'u' umlaut
});
/**
* Swiss character map.
* http://vt100.net/docs/vt220-rm/table2-15.html
*/
hterm.VT.CharacterMaps.DefaultMaps['='] = new hterm.VT.CharacterMap(
'swiss', {
'\x23': '\u00f9', // # -> 'u' grave
'\x40': '\u00e0', // @ -> 'a' grave
'\x5b': '\u00e9', // [ -> 'e' acute
'\x5c': '\u00e7', // \ -> 'c' cedilla
'\x5d': '\u00ea', // ] -> 'e' circumflex
'\x5e': '\u00ee', // ^ -> 'i' circumflex
'\x5f': '\u00e8', // _ -> 'e' grave
'\x60': '\u00f4', // ` -> 'o' circumflex
'\x7b': '\u00e4', // { -> 'a' umlaut
'\x7c': '\u00f6', // | -> 'o' umlaut
'\x7d': '\u00fc', // } -> 'u' umlaut
'\x7e': '\u00fb', // ~ -> 'u' circumflex
});