| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { CharData, ICellData, IExtendedAttrs } from 'common/Types'; |
| 7 |
import { stringFromCodePoint } from 'common/input/TextDecoder'; |
| 8 |
import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_ATTR_INDEX, Content } from 'common/buffer/Constants'; |
| 9 |
import { AttributeData, ExtendedAttrs } from 'common/buffer/AttributeData'; |
| 10 |
|
| 11 |
/** |
| 12 |
* CellData - represents a single Cell in the terminal buffer. |
| 13 |
*/ |
| 14 |
export class CellData extends AttributeData implements ICellData { |
| 15 |
/** Helper to create CellData from CharData. */ |
| 16 |
public static fromCharData(value: CharData): CellData { |
| 17 |
const obj = new CellData(); |
| 18 |
obj.setFromCharData(value); |
| 19 |
return obj; |
| 20 |
} |
| 21 |
/** Primitives from terminal buffer. */ |
| 22 |
public content = 0; |
| 23 |
public fg = 0; |
| 24 |
public bg = 0; |
| 25 |
public extended: IExtendedAttrs = new ExtendedAttrs(); |
| 26 |
public combinedData = ''; |
| 27 |
/** Whether cell contains a combined string. */ |
| 28 |
public isCombined(): number { |
| 29 |
return this.content & Content.IS_COMBINED_MASK; |
| 30 |
} |
| 31 |
/** Width of the cell. */ |
| 32 |
public getWidth(): number { |
| 33 |
return this.content >> Content.WIDTH_SHIFT; |
| 34 |
} |
| 35 |
/** JS string of the content. */ |
| 36 |
public getChars(): string { |
| 37 |
if (this.content & Content.IS_COMBINED_MASK) { |
| 38 |
return this.combinedData; |
| 39 |
} |
| 40 |
if (this.content & Content.CODEPOINT_MASK) { |
| 41 |
return stringFromCodePoint(this.content & Content.CODEPOINT_MASK); |
| 42 |
} |
| 43 |
return ''; |
| 44 |
} |
| 45 |
/** |
| 46 |
* Codepoint of cell |
| 47 |
* Note this returns the UTF32 codepoint of single chars, |
| 48 |
* if content is a combined string it returns the codepoint |
| 49 |
* of the last char in string to be in line with code in CharData. |
| 50 |
*/ |
| 51 |
public getCode(): number { |
| 52 |
return (this.isCombined()) |
| 53 |
? this.combinedData.charCodeAt(this.combinedData.length - 1) |
| 54 |
: this.content & Content.CODEPOINT_MASK; |
| 55 |
} |
| 56 |
/** Set data from CharData */ |
| 57 |
public setFromCharData(value: CharData): void { |
| 58 |
this.fg = value[CHAR_DATA_ATTR_INDEX]; |
| 59 |
this.bg = 0; |
| 60 |
let combined = false; |
| 61 |
// surrogates and combined strings need special treatment |
| 62 |
if (value[CHAR_DATA_CHAR_INDEX].length > 2) { |
| 63 |
combined = true; |
| 64 |
} |
| 65 |
else if (value[CHAR_DATA_CHAR_INDEX].length === 2) { |
| 66 |
const code = value[CHAR_DATA_CHAR_INDEX].charCodeAt(0); |
| 67 |
// if the 2-char string is a surrogate create single codepoint |
| 68 |
// everything else is combined |
| 69 |
if (0xD800 <= code && code <= 0xDBFF) { |
| 70 |
const second = value[CHAR_DATA_CHAR_INDEX].charCodeAt(1); |
| 71 |
if (0xDC00 <= second && second <= 0xDFFF) { |
| 72 |
this.content = ((code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000) | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); |
| 73 |
} |
| 74 |
else { |
| 75 |
combined = true; |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
combined = true; |
| 80 |
} |
| 81 |
} |
| 82 |
else { |
| 83 |
this.content = value[CHAR_DATA_CHAR_INDEX].charCodeAt(0) | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); |
| 84 |
} |
| 85 |
if (combined) { |
| 86 |
this.combinedData = value[CHAR_DATA_CHAR_INDEX]; |
| 87 |
this.content = Content.IS_COMBINED_MASK | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); |
| 88 |
} |
| 89 |
} |
| 90 |
/** Get data as CharData. */ |
| 91 |
public getAsCharData(): CharData { |
| 92 |
return [this.fg, this.getChars(), this.getWidth(), this.getCode()]; |
| 93 |
} |
| 94 |
} |