Merge branch 'updated_wcwidth' of github.com:jerch/xterm.js into updated_wcwidth

This commit is contained in:
Jörg Breitbart
2019-11-26 02:04:36 +01:00
25 changed files with 1203 additions and 125 deletions
+3
View File
@@ -37,6 +37,7 @@ export const DEFAULT_OPTIONS: ITerminalOptions = Object.freeze({
screenReaderMode: false,
macOptionIsMeta: false,
macOptionClickForcesSelection: false,
minimumContrastRatio: 1,
disableStdin: false,
allowTransparency: false,
tabStopWidth: 8,
@@ -117,6 +118,8 @@ export class OptionsService implements IOptionsService {
throw new Error(`${key} cannot be less than 1, value: ${value}`);
}
break;
case 'minimumContrastRatio':
value = Math.max(1, Math.min(21, Math.round(value * 10) / 10));
case 'scrollback':
value = Math.min(value, 4294967295);
if (value < 0) {
+1
View File
@@ -237,6 +237,7 @@ export interface ITerminalOptions {
logLevel: LogLevel;
macOptionIsMeta: boolean;
macOptionClickForcesSelection: boolean;
minimumContrastRatio: number;
rendererType: RendererType;
rightClickSelectsWord: boolean;
rows: number;
+75
View File
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { IUnicodeService, IUnicodeVersionProvider } from 'common/services/Services';
import { EventEmitter, IEvent } from 'common/EventEmitter';
export class UnicodeService implements IUnicodeService {
private _providers: {[key: string]: IUnicodeVersionProvider} = Object.create(null);
private _active: string = '';
private _onRegister = new EventEmitter<string>();
public get onRegister(): IEvent<string> { return this._onRegister.event; }
private _onChange = new EventEmitter<string>();
public get onChange(): IEvent<string> { return this._onChange.event; }
public get versions(): string[] {
return Object.keys(this._providers);
}
public get activeVersion(): string {
return this._active;
}
public set activeVersion(version: string) {
if (!this._providers[version]) {
throw new Error(`unknown Unicode version "${version}"`);
}
this._active = version;
this.wcwidth = (num: number) => this._providers[version].wcwidth(num); // perf an issue here?
this._onChange.fire(version);
}
public registerVersion(provider: IUnicodeVersionProvider): void {
this._providers[provider.version] = provider;
this._onRegister.fire(provider.version);
if (!this.activeVersion) {
this.activeVersion = provider.version;
}
}
/**
* Unicode version dependent interface.
*/
public wcwidth: (num: number) => number = () => { throw new Error('no Unicode version registered and activated'); }
public getStringCellWidth(s: string): number {
let result = 0;
const length = s.length;
for (let i = 0; i < length; ++i) {
let code = s.charCodeAt(i);
// surrogate pair first
if (0xD800 <= code && code <= 0xDBFF) {
if (++i >= length) {
// this should not happen with strings retrieved from
// Buffer.translateToString as it converts from UTF-32
// and therefore always should contain the second part
// for any other string we still have to handle it somehow:
// simply treat the lonely surrogate first as a single char (UCS-2 behavior)
return result + this.wcwidth(code);
}
const second = s.charCodeAt(i);
// convert surrogate pair to high codepoint only for valid second part (UTF-16)
// otherwise treat them independently (UCS-2 behavior)
if (0xDC00 <= second && second <= 0xDFFF) {
code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
} else {
result += this.wcwidth(second);
}
}
result += this.wcwidth(code);
}
return result;
}
}