Skip to content

Commit

Permalink
Fix reflow larger with wide chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Jan 21, 2019
1 parent 68197ed commit 4843ca5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/Buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,43 @@ describe('Buffer', () => {
assert.equal(secondMarker.line, 1, 'second marker should be restored');
assert.equal(thirdMarker.line, 2, 'third marker should be restored');
});
it('should wrap wide characters correctly when reflowing larger', () => {
buffer.fillViewportRows();
buffer.resize(12, 10);
for (let i = 0; i < 12; i += 4) {
buffer.lines.get(0).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]);
buffer.lines.get(1).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]);
}
for (let i = 2; i < 12; i += 4) {
buffer.lines.get(0).set(i, [null, '语', 2, '语'.charCodeAt(0)]);
buffer.lines.get(1).set(i, [null, '语', 2, '语'.charCodeAt(0)]);
}
for (let i = 1; i < 12; i += 2) {
buffer.lines.get(0).set(i, [null, '', 0, undefined]);
buffer.lines.get(1).set(i, [null, '', 0, undefined]);
}
buffer.lines.get(1).isWrapped = true;
// Buffer:
// 汉语汉语汉语 (wrapped)
// 汉语汉语汉语
assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语');
assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语汉语');
buffer.resize(13, 10);
assert.equal(buffer.ybase, 0);
assert.equal(buffer.lines.length, 10);
assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语');
assert.equal(buffer.lines.get(0).translateToString(false), '汉语汉语汉语 ');
assert.equal(buffer.lines.get(1).translateToString(true), '汉语汉语汉语');
assert.equal(buffer.lines.get(1).translateToString(false), '汉语汉语汉语 ');
buffer.resize(14, 10);
assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉语汉');
assert.equal(buffer.lines.get(0).translateToString(false), '汉语汉语汉语汉');
assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语');
assert.equal(buffer.lines.get(1).translateToString(false), '语汉语汉语 ');
});
it('should wrap wide characters correctly when reflowing smaller', () => {
// TODO: ..
});

describe('reflowLarger cases', () => {
beforeEach(() => {
Expand Down
18 changes: 15 additions & 3 deletions src/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,24 +259,36 @@ export class Buffer implements IBuffer {

// Copy buffer data to new locations
let destLineIndex = 0;
let destCol = this._cols;
let destCol = wrappedLines[destLineIndex].getTrimmedLength();
let srcLineIndex = 1;
let srcCol = 0;
while (srcLineIndex < wrappedLines.length) {
const srcRemainingCells = this._cols - srcCol;
const srcTrimmedTineLength = wrappedLines[srcLineIndex].getTrimmedLength();
const srcRemainingCells = srcTrimmedTineLength - srcCol;
const destRemainingCells = newCols - destCol;
const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells);

wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false);

destCol += cellsToCopy;
if (destCol === newCols) {
destLineIndex++;
destCol = 0;
}
srcCol += cellsToCopy;
if (srcCol === this._cols) {
if (srcCol === srcTrimmedTineLength) {
srcLineIndex++;
srcCol = 0;
}

// Make sure the last cell isn't wide, if it is copy it to the current dest
if (destCol === 0) {
if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) {
wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false);
// Null out the end of the last row
wrappedLines[destLineIndex - 1].set(newCols - 1, FILL_CHAR_DATA);
}
}
}

// Clear out remaining cells or fragments could remain;
Expand Down
4 changes: 4 additions & 0 deletions src/BufferLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class BufferLine implements IBufferLine {
];
}

public getWidth(index: number): number {
return this._data[index * CELL_SIZE + Cell.WIDTH];
}

public set(index: number, value: CharData): void {
this._data[index * CELL_SIZE + Cell.FLAGS] = value[0];
if (value[1].length > 1) {
Expand Down

0 comments on commit 4843ca5

Please sign in to comment.