From d569aba4d731dd1f51ac0cb998e285a383309f08 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 27 Feb 2018 13:31:26 -0800 Subject: [PATCH] Use xterm's to calc terminal char dimensions if available Fixes #44608 --- src/typings/vscode-xterm.d.ts | 1 + .../electron-browser/terminalConfigHelper.ts | 27 ++++++++++++------- .../electron-browser/terminalInstance.ts | 8 +++--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/typings/vscode-xterm.d.ts b/src/typings/vscode-xterm.d.ts index 7ecbd8a013b..09252b3e5cb 100644 --- a/src/typings/vscode-xterm.d.ts +++ b/src/typings/vscode-xterm.d.ts @@ -606,5 +606,6 @@ declare module 'vscode-xterm' { webLinksInit(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions): void; winptyCompatInit(): void; + charMeasure?: { height: number, width: number } } } \ No newline at end of file diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalConfigHelper.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalConfigHelper.ts index 3c27b5e6719..0fd4de9e7a4 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalConfigHelper.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalConfigHelper.ts @@ -14,6 +14,7 @@ import { ITerminalConfiguration, ITerminalConfigHelper, ITerminalFont, IShellLau import Severity from 'vs/base/common/severity'; import { isFedora } from 'vs/workbench/parts/terminal/electron-browser/terminal'; import { IChoiceService } from 'vs/platform/dialogs/common/dialogs'; +import { Terminal as XTermTerminal } from 'vscode-xterm'; const DEFAULT_LINE_HEIGHT = 1.0; @@ -50,20 +51,12 @@ export class TerminalConfigHelper implements ITerminalConfigHelper { } private _measureFont(fontFamily: string, fontSize: number, lineHeight: number): ITerminalFont { - // Return cached font if no config changed - if (this._lastFontMeasurement && - this._lastFontMeasurement.fontFamily === fontFamily && - this._lastFontMeasurement.fontSize === fontSize && - this._lastFontMeasurement.lineHeight === lineHeight) { - return this._lastFontMeasurement; - } - // Create charMeasureElement if it hasn't been created or if it was orphaned by its parent if (!this._charMeasureElement || !this._charMeasureElement.parentElement) { this._charMeasureElement = document.createElement('div'); this.panelContainer.appendChild(this._charMeasureElement); } - // TODO: This should leverage CharMeasure + const style = this._charMeasureElement.style; style.display = 'block'; style.fontFamily = fontFamily; @@ -92,7 +85,7 @@ export class TerminalConfigHelper implements ITerminalConfigHelper { * Gets the font information based on the terminal.integrated.fontFamily * terminal.integrated.fontSize, terminal.integrated.lineHeight configuration properties */ - public getFont(excludeDimensions?: boolean): ITerminalFont { + public getFont(xterm?: XTermTerminal, excludeDimensions?: boolean): ITerminalFont { const editorConfig = this._configurationService.getValue('editor'); let fontFamily = this.config.fontFamily || editorConfig.fontFamily; @@ -115,6 +108,20 @@ export class TerminalConfigHelper implements ITerminalConfigHelper { }; } + // Get the character dimensions from xterm if it's available + if (xterm) { + if (xterm.charMeasure && xterm.charMeasure.width && xterm.charMeasure.height) { + return { + fontFamily, + fontSize, + lineHeight, + charHeight: xterm.charMeasure.height, + charWidth: xterm.charMeasure.width + }; + } + } + + // Fall back to measuring the font ourselves return this._measureFont(fontFamily, fontSize, lineHeight); } diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts index 4feea9a8bc9..4927ce7b8a2 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts @@ -223,7 +223,7 @@ export class TerminalInstance implements ITerminalInstance { return null; } - const font = this._configHelper.getFont(); + const font = this._configHelper.getFont(this._xterm); // Because xterm.js converts from CSS pixels to actual pixels through // the use of canvas, window.devicePixelRatio needs to be used here in @@ -243,7 +243,7 @@ export class TerminalInstance implements ITerminalInstance { private _getDimension(width: number, height: number): Dimension { // The font needs to have been initialized - const font = this._configHelper.getFont(); + const font = this._configHelper.getFont(this._xterm); if (!font || !font.charWidth || !font.charHeight) { return null; } @@ -285,7 +285,7 @@ export class TerminalInstance implements ITerminalInstance { Terminal.strings.tooMuchOutput = nls.localize('terminal.integrated.a11yTooMuchOutput', 'Too much output to announce, navigate to rows manually to read'); } const accessibilitySupport = this._configurationService.getValue('editor').accessibilitySupport; - const font = this._configHelper.getFont(true); + const font = this._configHelper.getFont(undefined, true); this._xterm = new Terminal({ scrollback: this._configHelper.config.scrollback, theme: this._getXtermTheme(), @@ -1096,7 +1096,7 @@ export class TerminalInstance implements ITerminalInstance { } if (this._xterm) { - const font = this._configHelper.getFont(); + const font = this._configHelper.getFont(this._xterm); // Only apply these settings when the terminal is visible so that // the characters are measured correctly. -- GitLab