Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dimmed characters (faint) #1043

Merged
merged 8 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { C0 } from './EscapeSequences';
import { DEFAULT_CHARSET } from './Charsets';
import { CharData } from './Types';
import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX } from './Buffer';
import { FLAGS } from './renderer/Types';

/**
* The terminal's standard implementation of IInputHandler, this handles all
Expand Down Expand Up @@ -1107,6 +1108,7 @@ export class InputHandler implements IInputHandler {
* CSI Pm m Character Attributes (SGR).
* Ps = 0 -> Normal (default).
* Ps = 1 -> Bold.
* Ps = 2 -> Faint, decreased intensity (ISO 6429).
* Ps = 4 -> Underlined.
* Ps = 5 -> Blink (appears as Bold).
* Ps = 7 -> Inverse.
Expand Down Expand Up @@ -1206,35 +1208,39 @@ export class InputHandler implements IInputHandler {
// bg = 0x1ff;
} else if (p === 1) {
// bold text
flags |= 1;
flags |= FLAGS.BOLD;
} else if (p === 4) {
// underlined text
flags |= 2;
flags |= FLAGS.UNDERLINE;
} else if (p === 5) {
// blink
flags |= 4;
flags |= FLAGS.BLINK;
} else if (p === 7) {
// inverse and positive
// test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
flags |= 8;
flags |= FLAGS.INVERSE;
} else if (p === 8) {
// invisible
flags |= 16;
flags |= FLAGS.INVISIBLE;
} else if (p === 2) {
// dimmed text
flags |= FLAGS.DIM;
} else if (p === 22) {
// not bold
flags &= ~1;
// not bold nor faint
flags &= ~FLAGS.BOLD;
flags &= ~FLAGS.DIM;
} else if (p === 24) {
// not underlined
flags &= ~2;
flags &= ~FLAGS.UNDERLINE;
} else if (p === 25) {
// not blink
flags &= ~4;
flags &= ~FLAGS.BLINK;
} else if (p === 27) {
// not inverse
flags &= ~8;
flags &= ~FLAGS.INVERSE;
} else if (p === 28) {
// not invisible
flags &= ~16;
flags &= ~FLAGS.INVISIBLE;
} else if (p === 39) {
// reset fg
fg = (this._terminal.defAttr >> 9) & 0x1ff;
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/BaseRenderLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CharData } from '../Types';
import { CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CHAR_INDEX } from '../Buffer';

export const INVERTED_DEFAULT_COLOR = -1;
const DIM_OPACITY = 0.5;

export abstract class BaseRenderLayer implements IRenderLayer {
private _canvas: HTMLCanvasElement;
Expand Down Expand Up @@ -223,7 +224,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
* This is used to validate whether a cached image can be used.
* @param bold Whether the text is bold.
*/
protected drawChar(terminal: ITerminal, char: string, code: number, width: number, x: number, y: number, fg: number, bg: number, bold: boolean): void {
protected drawChar(terminal: ITerminal, char: string, code: number, width: number, x: number, y: number, fg: number, bg: number, bold: boolean, dim: boolean): void {
let colorIndex = 0;
if (fg < 256) {
colorIndex = fg + 2;
Expand All @@ -241,11 +242,15 @@ export abstract class BaseRenderLayer implements IRenderLayer {
// ImageBitmap's draw about twice as fast as from a canvas
const charAtlasCellWidth = this._scaledCharWidth + CHAR_ATLAS_CELL_SPACING;
const charAtlasCellHeight = this._scaledCharHeight + CHAR_ATLAS_CELL_SPACING;
// Apply alpha to dim the character
if (dim) {
this._ctx.globalAlpha = DIM_OPACITY;
}
this._ctx.drawImage(this._charAtlas,
code * charAtlasCellWidth, colorIndex * charAtlasCellHeight, charAtlasCellWidth, this._scaledCharHeight,
x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY, this._scaledCharWidth, this._scaledCharHeight);
} else {
this._drawUncachedChar(terminal, char, width, fg, x, y, bold);
this._drawUncachedChar(terminal, char, width, fg, x, y, bold, dim);
}
// This draws the atlas (for debugging purposes)
// this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
Expand All @@ -263,7 +268,7 @@ export abstract class BaseRenderLayer implements IRenderLayer {
* @param x The column to draw at.
* @param y The row to draw at.
*/
private _drawUncachedChar(terminal: ITerminal, char: string, width: number, fg: number, x: number, y: number, bold: boolean): void {
private _drawUncachedChar(terminal: ITerminal, char: string, width: number, fg: number, x: number, y: number, bold: boolean, dim: boolean): void {
this._ctx.save();
this._ctx.font = `${terminal.options.fontSize * window.devicePixelRatio}px ${terminal.options.fontFamily}`;
if (bold) {
Expand All @@ -288,6 +293,10 @@ export abstract class BaseRenderLayer implements IRenderLayer {
this._ctx.rect(0, y * this._scaledLineHeight + this._scaledLineDrawY, terminal.cols * this._scaledCharWidth, this._scaledCharHeight);
this._ctx.clip();

// Apply alpha to dim the character
if (dim) {
this._ctx.globalAlpha = DIM_OPACITY;
}
// Draw the character
this._ctx.fillText(char, x * this._scaledCharWidth, y * this._scaledLineHeight + this._scaledLineDrawY);
this._ctx.restore();
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/TextRenderLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class TextRenderLayer extends BaseRenderLayer {
this.fillBottomLineAtCells(x, y);
}

this.drawChar(terminal, char, code, width, x, y, fg, bg, !!(flags & FLAGS.BOLD));
this.drawChar(terminal, char, code, width, x, y, fg, bg, !!(flags & FLAGS.BOLD), !!(flags & FLAGS.DIM));

this._ctx.restore();
}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export enum FLAGS {
UNDERLINE = 2,
BLINK = 4,
INVERSE = 8,
INVISIBLE = 16
INVISIBLE = 16,
DIM = 32
};