Skip to content

Commit

Permalink
Use charData and ctx.save/restore, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mofux committed Sep 15, 2017
1 parent 7b5d0dd commit a141c42
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/renderer/TextRenderLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class TextRenderLayer extends BaseRenderLayer {
if (code === 32 /*' '*/) {
if (x > 0) {
const previousChar: CharData = line[x - 1];
if (this._isOverlapping(previousChar[CHAR_DATA_CHAR_INDEX])) {
if (this._isOverlapping(previousChar)) {
continue;
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ export class TextRenderLayer extends BaseRenderLayer {

// If the character is an overlapping char and the character to the right is a
// space, take ownership of the cell to the right.
if (width !== 0 && this._isOverlapping(char)) {
if (width !== 0 && this._isOverlapping(charData)) {
// If the character is overlapping, we want to force a re-render on every
// frame. This is specifically to work around the case where two
// overlaping chars `a` and `b` are adjacent, the cursor is moved to b and a
Expand Down Expand Up @@ -184,32 +184,35 @@ export class TextRenderLayer extends BaseRenderLayer {
* Whether a character is overlapping to the
* next cell.
*/
private _isOverlapping(char: string): boolean {

// Deliver from cache if available
if (this._characterOverlapCache.hasOwnProperty(char)) {
return this._characterOverlapCache[char];
}

// Set the correct character font for the current context
let font;
if (this._ctx.font !== this._characterFont) {
font = this._ctx.font;
this._ctx.font = this._characterFont;
}
private _isOverlapping(charData: CharData): boolean {
// We assume that any ascii character will not overlap
const code = charData[CHAR_DATA_CODE_INDEX];
if (code < 256) {
return false;
}

// Deliver from cache if available
const char = charData[CHAR_DATA_CHAR_INDEX];
if (this._characterOverlapCache.hasOwnProperty(char)) {
return this._characterOverlapCache[char];
}

// Measure the width of the character, but Math.floor it
// because that is what the renderer does when it calculates
// the character dimensions we are comparing against
const overlaps = Math.floor(this._ctx.measureText(char).width) > this._characterWidth;
// Setup the font
this._ctx.save();
this._ctx.font = this._characterFont;

// Restore the original font to the context
if (font) this._ctx.font = font;
// Measure the width of the character, but Math.floor it
// because that is what the renderer does when it calculates
// the character dimensions we are comparing against
const overlaps = Math.floor(this._ctx.measureText(char).width) > this._characterWidth;

// Cache and return
return this._characterOverlapCache[char] = overlaps;
// Restore the original context
this._ctx.restore();

}
// Cache and return
this._characterOverlapCache[char] = overlaps;
return overlaps;
}

/**
* Clear the charcater at the cell specified.
Expand Down

0 comments on commit a141c42

Please sign in to comment.