| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { UnicodeV6 } from 'common/input/UnicodeV6'; |
| 7 |
import { IUnicodeService, IUnicodeVersionProvider, UnicodeCharProperties, UnicodeCharWidth } from 'common/services/Services'; |
| 8 |
import { Emitter } from 'vs/base/common/event'; |
| 9 |
|
| 10 |
export class UnicodeService implements IUnicodeService { |
| 11 |
public serviceBrand: any; |
| 12 |
|
| 13 |
private _providers: {[key: string]: IUnicodeVersionProvider} = Object.create(null); |
| 14 |
private _active: string = ''; |
| 15 |
private _activeProvider: IUnicodeVersionProvider; |
| 16 |
|
| 17 |
private readonly _onChange = new Emitter<string>(); |
| 18 |
public readonly onChange = this._onChange.event; |
| 19 |
|
| 20 |
public static extractShouldJoin(value: UnicodeCharProperties): boolean { |
| 21 |
return (value & 1) !== 0; |
| 22 |
} |
| 23 |
public static extractWidth(value: UnicodeCharProperties): UnicodeCharWidth { |
| 24 |
return ((value >> 1) & 0x3) as UnicodeCharWidth; |
| 25 |
} |
| 26 |
public static extractCharKind(value: UnicodeCharProperties): number { |
| 27 |
return value >> 3; |
| 28 |
} |
| 29 |
public static createPropertyValue(state: number, width: number, shouldJoin: boolean = false): UnicodeCharProperties { |
| 30 |
return ((state & 0xffffff) << 3) | ((width & 3) << 1) | (shouldJoin?1:0); |
| 31 |
} |
| 32 |
|
| 33 |
constructor() { |
| 34 |
const defaultProvider = new UnicodeV6(); |
| 35 |
this.register(defaultProvider); |
| 36 |
this._active = defaultProvider.version; |
| 37 |
this._activeProvider = defaultProvider; |
| 38 |
} |
| 39 |
|
| 40 |
public dispose(): void { |
| 41 |
this._onChange.dispose(); |
| 42 |
} |
| 43 |
|
| 44 |
public get versions(): string[] { |
| 45 |
return Object.keys(this._providers); |
| 46 |
} |
| 47 |
|
| 48 |
public get activeVersion(): string { |
| 49 |
return this._active; |
| 50 |
} |
| 51 |
|
| 52 |
public set activeVersion(version: string) { |
| 53 |
if (!this._providers[version]) { |
| 54 |
throw new Error(`unknown Unicode version "${version}"`); |
| 55 |
} |
| 56 |
this._active = version; |
| 57 |
this._activeProvider = this._providers[version]; |
| 58 |
this._onChange.fire(version); |
| 59 |
} |
| 60 |
|
| 61 |
public register(provider: IUnicodeVersionProvider): void { |
| 62 |
this._providers[provider.version] = provider; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Unicode version dependent interface. |
| 67 |
*/ |
| 68 |
public wcwidth(num: number): UnicodeCharWidth { |
| 69 |
return this._activeProvider.wcwidth(num); |
| 70 |
} |
| 71 |
|
| 72 |
public getStringCellWidth(s: string): number { |
| 73 |
let result = 0; |
| 74 |
let precedingInfo = 0; |
| 75 |
const length = s.length; |
| 76 |
for (let i = 0; i < length; ++i) { |
| 77 |
let code = s.charCodeAt(i); |
| 78 |
// surrogate pair first |
| 79 |
if (0xD800 <= code && code <= 0xDBFF) { |
| 80 |
if (++i >= length) { |
| 81 |
// this should not happen with strings retrieved from |
| 82 |
// Buffer.translateToString as it converts from UTF-32 |
| 83 |
// and therefore always should contain the second part |
| 84 |
// for any other string we still have to handle it somehow: |
| 85 |
// simply treat the lonely surrogate first as a single char (UCS-2 behavior) |
| 86 |
return result + this.wcwidth(code); |
| 87 |
} |
| 88 |
const second = s.charCodeAt(i); |
| 89 |
// convert surrogate pair to high codepoint only for valid second part (UTF-16) |
| 90 |
// otherwise treat them independently (UCS-2 behavior) |
| 91 |
if (0xDC00 <= second && second <= 0xDFFF) { |
| 92 |
code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; |
| 93 |
} else { |
| 94 |
result += this.wcwidth(second); |
| 95 |
} |
| 96 |
} |
| 97 |
const currentInfo = this.charProperties(code, precedingInfo); |
| 98 |
let chWidth = UnicodeService.extractWidth(currentInfo); |
| 99 |
if (UnicodeService.extractShouldJoin(currentInfo)) { |
| 100 |
chWidth -= UnicodeService.extractWidth(precedingInfo); |
| 101 |
} |
| 102 |
result += chWidth; |
| 103 |
precedingInfo = currentInfo; |
| 104 |
} |
| 105 |
return result; |
| 106 |
} |
| 107 |
|
| 108 |
public charProperties(codepoint: number, preceding: UnicodeCharProperties): UnicodeCharProperties { |
| 109 |
return this._activeProvider.charProperties(codepoint, preceding); |
| 110 |
} |
| 111 |
} |