| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IBufferLine, ICellData, CharData } from 'common/Types'; |
| 7 |
import { ICharacterJoiner } from 'browser/Types'; |
| 8 |
import { AttributeData } from 'common/buffer/AttributeData'; |
| 9 |
import { WHITESPACE_CELL_CHAR, Content } from 'common/buffer/Constants'; |
| 10 |
import { CellData } from 'common/buffer/CellData'; |
| 11 |
import { IBufferService } from 'common/services/Services'; |
| 12 |
import { ICharacterJoinerService } from 'browser/services/Services'; |
| 13 |
|
| 14 |
export class JoinedCellData extends AttributeData implements ICellData { |
| 15 |
private _width: number; |
| 16 |
// .content carries no meaning for joined CellData, simply nullify it |
| 17 |
// thus we have to overload all other .content accessors |
| 18 |
public content: number = 0; |
| 19 |
public fg: number; |
| 20 |
public bg: number; |
| 21 |
public combinedData: string = ''; |
| 22 |
|
| 23 |
constructor(firstCell: ICellData, chars: string, width: number) { |
| 24 |
super(); |
| 25 |
this.fg = firstCell.fg; |
| 26 |
this.bg = firstCell.bg; |
| 27 |
this.combinedData = chars; |
| 28 |
this._width = width; |
| 29 |
} |
| 30 |
|
| 31 |
public isCombined(): number { |
| 32 |
// always mark joined cell data as combined |
| 33 |
return Content.IS_COMBINED_MASK; |
| 34 |
} |
| 35 |
|
| 36 |
public getWidth(): number { |
| 37 |
return this._width; |
| 38 |
} |
| 39 |
|
| 40 |
public getChars(): string { |
| 41 |
return this.combinedData; |
| 42 |
} |
| 43 |
|
| 44 |
public getCode(): number { |
| 45 |
// code always gets the highest possible fake codepoint (read as -1) |
| 46 |
// this is needed as code is used by caches as identifier |
| 47 |
return 0x1FFFFF; |
| 48 |
} |
| 49 |
|
| 50 |
public setFromCharData(value: CharData): void { |
| 51 |
throw new Error('not implemented'); |
| 52 |
} |
| 53 |
|
| 54 |
public getAsCharData(): CharData { |
| 55 |
return [this.fg, this.getChars(), this.getWidth(), this.getCode()]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
export class CharacterJoinerService implements ICharacterJoinerService { |
| 60 |
public serviceBrand: undefined; |
| 61 |
|
| 62 |
private _characterJoiners: ICharacterJoiner[] = []; |
| 63 |
private _nextCharacterJoinerId: number = 0; |
| 64 |
private _workCell: CellData = new CellData(); |
| 65 |
|
| 66 |
constructor( |
| 67 |
@IBufferService private _bufferService: IBufferService |
| 68 |
) { } |
| 69 |
|
| 70 |
public register(handler: (text: string) => [number, number][]): number { |
| 71 |
const joiner: ICharacterJoiner = { |
| 72 |
id: this._nextCharacterJoinerId++, |
| 73 |
handler |
| 74 |
}; |
| 75 |
|
| 76 |
this._characterJoiners.push(joiner); |
| 77 |
return joiner.id; |
| 78 |
} |
| 79 |
|
| 80 |
public deregister(joinerId: number): boolean { |
| 81 |
for (let i = 0; i < this._characterJoiners.length; i++) { |
| 82 |
if (this._characterJoiners[i].id === joinerId) { |
| 83 |
this._characterJoiners.splice(i, 1); |
| 84 |
return true; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
public getJoinedCharacters(row: number): [number, number][] { |
| 92 |
if (this._characterJoiners.length === 0) { |
| 93 |
return []; |
| 94 |
} |
| 95 |
|
| 96 |
const line = this._bufferService.buffer.lines.get(row); |
| 97 |
if (!line || line.length === 0) { |
| 98 |
return []; |
| 99 |
} |
| 100 |
|
| 101 |
const ranges: [number, number][] = []; |
| 102 |
const lineStr = line.translateToString(true); |
| 103 |
|
| 104 |
// Because some cells can be represented by multiple javascript characters, |
| 105 |
// we track the cell and the string indexes separately. This allows us to |
| 106 |
// translate the string ranges we get from the joiners back into cell ranges |
| 107 |
// for use when rendering |
| 108 |
let rangeStartColumn = 0; |
| 109 |
let currentStringIndex = 0; |
| 110 |
let rangeStartStringIndex = 0; |
| 111 |
let rangeAttrFG = line.getFg(0); |
| 112 |
let rangeAttrBG = line.getBg(0); |
| 113 |
|
| 114 |
for (let x = 0; x < line.getTrimmedLength(); x++) { |
| 115 |
line.loadCell(x, this._workCell); |
| 116 |
|
| 117 |
if (this._workCell.getWidth() === 0) { |
| 118 |
// If this character is of width 0, skip it. |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
// End of range |
| 123 |
if (this._workCell.fg !== rangeAttrFG || this._workCell.bg !== rangeAttrBG) { |
| 124 |
// If we ended up with a sequence of more than one character, |
| 125 |
// look for ranges to join. |
| 126 |
if (x - rangeStartColumn > 1) { |
| 127 |
const joinedRanges = this._getJoinedRanges( |
| 128 |
lineStr, |
| 129 |
rangeStartStringIndex, |
| 130 |
currentStringIndex, |
| 131 |
line, |
| 132 |
rangeStartColumn |
| 133 |
); |
| 134 |
for (let i = 0; i < joinedRanges.length; i++) { |
| 135 |
ranges.push(joinedRanges[i]); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// Reset our markers for a new range. |
| 140 |
rangeStartColumn = x; |
| 141 |
rangeStartStringIndex = currentStringIndex; |
| 142 |
rangeAttrFG = this._workCell.fg; |
| 143 |
rangeAttrBG = this._workCell.bg; |
| 144 |
} |
| 145 |
|
| 146 |
currentStringIndex += this._workCell.getChars().length || WHITESPACE_CELL_CHAR.length; |
| 147 |
} |
| 148 |
|
| 149 |
// Process any trailing ranges. |
| 150 |
if (this._bufferService.cols - rangeStartColumn > 1) { |
| 151 |
const joinedRanges = this._getJoinedRanges( |
| 152 |
lineStr, |
| 153 |
rangeStartStringIndex, |
| 154 |
currentStringIndex, |
| 155 |
line, |
| 156 |
rangeStartColumn |
| 157 |
); |
| 158 |
for (let i = 0; i < joinedRanges.length; i++) { |
| 159 |
ranges.push(joinedRanges[i]); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return ranges; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Given a segment of a line of text, find all ranges of text that should be |
| 168 |
* joined in a single rendering unit. Ranges are internally converted to |
| 169 |
* column ranges, rather than string ranges. |
| 170 |
* @param line String representation of the full line of text |
| 171 |
* @param startIndex Start position of the range to search in the string (inclusive) |
| 172 |
* @param endIndex End position of the range to search in the string (exclusive) |
| 173 |
*/ |
| 174 |
private _getJoinedRanges(line: string, startIndex: number, endIndex: number, lineData: IBufferLine, startCol: number): [number, number][] { |
| 175 |
const text = line.substring(startIndex, endIndex); |
| 176 |
// At this point we already know that there is at least one joiner so |
| 177 |
// we can just pull its value and assign it directly rather than |
| 178 |
// merging it into an empty array, which incurs unnecessary writes. |
| 179 |
let allJoinedRanges: [number, number][] = []; |
| 180 |
try { |
| 181 |
allJoinedRanges = this._characterJoiners[0].handler(text); |
| 182 |
} catch (error) { |
| 183 |
console.error(error); |
| 184 |
} |
| 185 |
for (let i = 1; i < this._characterJoiners.length; i++) { |
| 186 |
// We merge any overlapping ranges across the different joiners |
| 187 |
try { |
| 188 |
const joinerRanges = this._characterJoiners[i].handler(text); |
| 189 |
for (let j = 0; j < joinerRanges.length; j++) { |
| 190 |
CharacterJoinerService._mergeRanges(allJoinedRanges, joinerRanges[j]); |
| 191 |
} |
| 192 |
} catch (error) { |
| 193 |
console.error(error); |
| 194 |
} |
| 195 |
} |
| 196 |
this._stringRangesToCellRanges(allJoinedRanges, lineData, startCol); |
| 197 |
return allJoinedRanges; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Modifies the provided ranges in-place to adjust for variations between |
| 202 |
* string length and cell width so that the range represents a cell range, |
| 203 |
* rather than the string range the joiner provides. |
| 204 |
* @param ranges String ranges containing start (inclusive) and end (exclusive) index |
| 205 |
* @param line Cell data for the relevant line in the terminal |
| 206 |
* @param startCol Offset within the line to start from |
| 207 |
*/ |
| 208 |
private _stringRangesToCellRanges(ranges: [number, number][], line: IBufferLine, startCol: number): void { |
| 209 |
let currentRangeIndex = 0; |
| 210 |
let currentRangeStarted = false; |
| 211 |
let currentStringIndex = 0; |
| 212 |
let currentRange = ranges[currentRangeIndex]; |
| 213 |
|
| 214 |
// If we got through all of the ranges, stop searching |
| 215 |
if (!currentRange) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
for (let x = startCol; x < this._bufferService.cols; x++) { |
| 220 |
const width = line.getWidth(x); |
| 221 |
const length = line.getString(x).length || WHITESPACE_CELL_CHAR.length; |
| 222 |
|
| 223 |
// We skip zero-width characters when creating the string to join the text |
| 224 |
// so we do the same here |
| 225 |
if (width === 0) { |
| 226 |
continue; |
| 227 |
} |
| 228 |
|
| 229 |
// Adjust the start of the range |
| 230 |
if (!currentRangeStarted && currentRange[0] <= currentStringIndex) { |
| 231 |
currentRange[0] = x; |
| 232 |
currentRangeStarted = true; |
| 233 |
} |
| 234 |
|
| 235 |
// Adjust the end of the range |
| 236 |
if (currentRange[1] <= currentStringIndex) { |
| 237 |
currentRange[1] = x; |
| 238 |
|
| 239 |
// We're finished with this range, so we move to the next one |
| 240 |
currentRange = ranges[++currentRangeIndex]; |
| 241 |
|
| 242 |
// If there are no more ranges left, stop searching |
| 243 |
if (!currentRange) { |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
// Ranges can be on adjacent characters. Because the end index of the |
| 248 |
// ranges are exclusive, this means that the index for the start of a |
| 249 |
// range can be the same as the end index of the previous range. To |
| 250 |
// account for the start of the next range, we check here just in case. |
| 251 |
if (currentRange[0] <= currentStringIndex) { |
| 252 |
currentRange[0] = x; |
| 253 |
currentRangeStarted = true; |
| 254 |
} else { |
| 255 |
currentRangeStarted = false; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Adjust the string index based on the character length to line up with |
| 260 |
// the column adjustment |
| 261 |
currentStringIndex += length; |
| 262 |
} |
| 263 |
|
| 264 |
// If there is still a range left at the end, it must extend all the way to |
| 265 |
// the end of the line. |
| 266 |
if (currentRange) { |
| 267 |
currentRange[1] = this._bufferService.cols; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Merges the range defined by the provided start and end into the list of |
| 273 |
* existing ranges. The merge is done in place on the existing range for |
| 274 |
* performance and is also returned. |
| 275 |
* @param ranges Existing range list |
| 276 |
* @param newRange Tuple of two numbers representing the new range to merge in. |
| 277 |
* @returns The ranges input with the new range merged in place |
| 278 |
*/ |
| 279 |
private static _mergeRanges(ranges: [number, number][], newRange: [number, number]): [number, number][] { |
| 280 |
let inRange = false; |
| 281 |
for (let i = 0; i < ranges.length; i++) { |
| 282 |
const range = ranges[i]; |
| 283 |
if (!inRange) { |
| 284 |
if (newRange[1] <= range[0]) { |
| 285 |
// Case 1: New range is before the search range |
| 286 |
ranges.splice(i, 0, newRange); |
| 287 |
return ranges; |
| 288 |
} |
| 289 |
|
| 290 |
if (newRange[1] <= range[1]) { |
| 291 |
// Case 2: New range is either wholly contained within the |
| 292 |
// search range or overlaps with the front of it |
| 293 |
range[0] = Math.min(newRange[0], range[0]); |
| 294 |
return ranges; |
| 295 |
} |
| 296 |
|
| 297 |
if (newRange[0] < range[1]) { |
| 298 |
// Case 3: New range either wholly contains the search range |
| 299 |
// or overlaps with the end of it |
| 300 |
range[0] = Math.min(newRange[0], range[0]); |
| 301 |
inRange = true; |
| 302 |
} |
| 303 |
|
| 304 |
// Case 4: New range starts after the search range |
| 305 |
continue; |
| 306 |
} else { |
| 307 |
if (newRange[1] <= range[0]) { |
| 308 |
// Case 5: New range extends from previous range but doesn't |
| 309 |
// reach the current one |
| 310 |
ranges[i - 1][1] = newRange[1]; |
| 311 |
return ranges; |
| 312 |
} |
| 313 |
|
| 314 |
if (newRange[1] <= range[1]) { |
| 315 |
// Case 6: New range extends from prvious range into the |
| 316 |
// current range |
| 317 |
ranges[i - 1][1] = Math.max(newRange[1], range[1]); |
| 318 |
ranges.splice(i, 1); |
| 319 |
return ranges; |
| 320 |
} |
| 321 |
|
| 322 |
// Case 7: New range extends from previous range past the |
| 323 |
// end of the current range |
| 324 |
ranges.splice(i, 1); |
| 325 |
i--; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
if (inRange) { |
| 330 |
// Case 8: New range extends past the last existing range |
| 331 |
ranges[ranges.length - 1][1] = newRange[1]; |
| 332 |
} else { |
| 333 |
// Case 9: New range starts after the last existing range |
| 334 |
ranges.push(newRange); |
| 335 |
} |
| 336 |
|
| 337 |
return ranges; |
| 338 |
} |
| 339 |
} |