Skip to content

Commit 44feecf

Browse files
authored
Merge pull request #4997 from Tyriar/4969
Introduce opt-in glyph scaling to achieve GB18030 compliance
2 parents 0658719 + df559e3 commit 44feecf

File tree

9 files changed

+89
-12
lines changed

9 files changed

+89
-12
lines changed

addons/addon-canvas/src/BaseRenderLayer.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CellColorResolver } from 'browser/renderer/shared/CellColorResolver';
88
import { acquireTextureAtlas } from 'browser/renderer/shared/CharAtlasCache';
99
import { TEXT_BASELINE } from 'browser/renderer/shared/Constants';
1010
import { tryDrawCustomChar } from 'browser/renderer/shared/CustomGlyphs';
11-
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
11+
import { isEmoji, throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
1212
import { createSelectionRenderModel } from 'browser/renderer/shared/SelectionRenderModel';
1313
import { IRasterizedGlyph, IRenderDimensions, ISelectionRenderModel, ITextureAtlas } from 'browser/renderer/shared/Types';
1414
import { ICoreBrowserService, IThemeService } from 'browser/services/Services';
@@ -365,6 +365,8 @@ export abstract class BaseRenderLayer extends Disposable implements IRenderLayer
365365
*/
366366
protected _drawChars(cell: ICellData, x: number, y: number): void {
367367
const chars = cell.getChars();
368+
const code = cell.getCode();
369+
const width = cell.getWidth();
368370
this._cellColorResolver.resolve(cell, x, this._bufferService.buffer.ydisp + y, this._deviceCellWidth);
369371

370372
if (!this._charAtlas) {
@@ -400,6 +402,23 @@ export abstract class BaseRenderLayer extends Disposable implements IRenderLayer
400402
this._bitmapGenerator[glyph.texturePage]!.refresh();
401403
this._bitmapGenerator[glyph.texturePage]!.version = this._charAtlas.pages[glyph.texturePage].version;
402404
}
405+
406+
// Reduce scale horizontally for wide glyphs printed in cells that would overlap with the
407+
// following cell (ie. the width is not 2).
408+
let renderWidth = glyph.size.x;
409+
if (this._optionsService.rawOptions.rescaleOverlappingGlyphs) {
410+
if (
411+
// Is single cell width
412+
width === 1 &&
413+
// Glyph exceeds cell bounds, + 1 to avoid hurting readability
414+
glyph.size.x > this._deviceCellWidth + 1 &&
415+
// Never rescale emoji
416+
code && !isEmoji(code)
417+
) {
418+
renderWidth = this._deviceCellWidth - 1; // - 1 to improve readability
419+
}
420+
}
421+
403422
this._ctx.drawImage(
404423
this._bitmapGenerator[glyph.texturePage]?.bitmap || this._charAtlas!.pages[glyph.texturePage].canvas,
405424
glyph.texturePosition.x,
@@ -408,7 +427,7 @@ export abstract class BaseRenderLayer extends Disposable implements IRenderLayer
408427
glyph.size.y,
409428
x * this._deviceCellWidth + this._deviceCharLeft - glyph.offset.x,
410429
y * this._deviceCellHeight + this._deviceCharTop - glyph.offset.y,
411-
glyph.size.x,
430+
renderWidth,
412431
glyph.size.y
413432
);
414433
this._ctx.restore();

addons/addon-webgl/src/GlyphRenderer.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @license MIT
44
*/
55

6-
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
6+
import { isEmoji, throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
77
import { TextureAtlas } from 'browser/renderer/shared/TextureAtlas';
88
import { IRasterizedGlyph, IRenderDimensions, ITextureAtlas } from 'browser/renderer/shared/Types';
99
import { NULL_CELL_CODE } from 'common/buffer/Constants';
1010
import { Disposable, toDisposable } from 'common/Lifecycle';
1111
import { Terminal } from '@xterm/xterm';
1212
import { IRenderModel, IWebGL2RenderingContext, IWebGLVertexArrayObject } from './Types';
1313
import { createProgram, GLTexture, PROJECTION_MATRIX } from './WebglUtils';
14+
import type { IOptionsService } from 'common/services/Services';
1415

1516
interface IVertices {
1617
attributes: Float32Array;
@@ -111,7 +112,8 @@ export class GlyphRenderer extends Disposable {
111112
constructor(
112113
private readonly _terminal: Terminal,
113114
private readonly _gl: IWebGL2RenderingContext,
114-
private _dimensions: IRenderDimensions
115+
private _dimensions: IRenderDimensions,
116+
private readonly _optionsService: IOptionsService
115117
) {
116118
super();
117119

@@ -212,15 +214,15 @@ export class GlyphRenderer extends Disposable {
212214
return this._atlas ? this._atlas.beginFrame() : true;
213215
}
214216

215-
public updateCell(x: number, y: number, code: number, bg: number, fg: number, ext: number, chars: string, lastBg: number): void {
217+
public updateCell(x: number, y: number, code: number, bg: number, fg: number, ext: number, chars: string, width: number, lastBg: number): void {
216218
// Since this function is called for every cell (`rows*cols`), it must be very optimized. It
217219
// should not instantiate any variables unless a new glyph is drawn to the cache where the
218220
// slight slowdown is acceptable for the developer ergonomics provided as it's a once of for
219221
// each glyph.
220-
this._updateCell(this._vertices.attributes, x, y, code, bg, fg, ext, chars, lastBg);
222+
this._updateCell(this._vertices.attributes, x, y, code, bg, fg, ext, chars, width, lastBg);
221223
}
222224

223-
private _updateCell(array: Float32Array, x: number, y: number, code: number | undefined, bg: number, fg: number, ext: number, chars: string, lastBg: number): void {
225+
private _updateCell(array: Float32Array, x: number, y: number, code: number | undefined, bg: number, fg: number, ext: number, chars: string, width: number, lastBg: number): void {
224226
$i = (y * this._terminal.cols + x) * INDICES_PER_CELL;
225227

226228
// Exit early if this is a null character, allow space character to continue as it may have
@@ -275,6 +277,21 @@ export class GlyphRenderer extends Disposable {
275277
array[$i + 8] = $glyph.sizeClipSpace.y;
276278
}
277279
// a_cellpos only changes on resize
280+
281+
// Reduce scale horizontally for wide glyphs printed in cells that would overlap with the
282+
// following cell (ie. the width is not 2).
283+
if (this._optionsService.rawOptions.rescaleOverlappingGlyphs) {
284+
if (
285+
// Is single cell width
286+
width === 1 &&
287+
// Glyph exceeds cell bounds, + 1 to avoid hurting readability
288+
$glyph.size.x > this._dimensions.device.cell.width + 1 &&
289+
// Never rescale emoji
290+
code && !isEmoji(code)
291+
) {
292+
array[$i + 2] = (this._dimensions.device.cell.width - 1) / this._dimensions.device.canvas.width; // - 1 to improve readability
293+
}
294+
}
278295
}
279296

280297
public clear(): void {

addons/addon-webgl/src/WebglRenderer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class WebglRenderer extends Disposable implements IRenderer {
3636
private _observerDisposable = this.register(new MutableDisposable());
3737

3838
private _model: RenderModel = new RenderModel();
39-
private _workCell: CellData = new CellData();
39+
private _workCell: ICellData = new CellData();
40+
private _workCell2: ICellData = new CellData();
4041
private _cellColorResolver: CellColorResolver;
4142

4243
private _canvas: HTMLCanvasElement;
@@ -245,7 +246,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
245246
*/
246247
private _initializeWebGLState(): [RectangleRenderer, GlyphRenderer] {
247248
this._rectangleRenderer.value = new RectangleRenderer(this._terminal, this._gl, this.dimensions, this._themeService);
248-
this._glyphRenderer.value = new GlyphRenderer(this._terminal, this._gl, this.dimensions);
249+
this._glyphRenderer.value = new GlyphRenderer(this._terminal, this._gl, this.dimensions, this._optionsService);
249250

250251
// Update dimensions and acquire char atlas
251252
this.handleCharSizeChanged();
@@ -388,6 +389,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
388389
let range: [number, number];
389390
let chars: string;
390391
let code: number;
392+
let width: number;
391393
let i: number;
392394
let x: number;
393395
let j: number;
@@ -500,7 +502,8 @@ export class WebglRenderer extends Disposable implements IRenderer {
500502
this._model.cells[i + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;
501503
this._model.cells[i + RENDER_MODEL_EXT_OFFSET] = this._cellColorResolver.result.ext;
502504

503-
this._glyphRenderer.value!.updateCell(x, y, code, this._cellColorResolver.result.bg, this._cellColorResolver.result.fg, this._cellColorResolver.result.ext, chars, lastBg);
505+
width = cell.getWidth();
506+
this._glyphRenderer.value!.updateCell(x, y, code, this._cellColorResolver.result.bg, this._cellColorResolver.result.fg, this._cellColorResolver.result.ext, chars, width, lastBg);
504507

505508
if (isJoined) {
506509
// Restore work cell
@@ -509,7 +512,7 @@ export class WebglRenderer extends Disposable implements IRenderer {
509512
// Null out non-first cells
510513
for (x++; x < lastCharX; x++) {
511514
j = ((y * terminal.cols) + x) * RENDER_MODEL_INDICIES_PER_CELL;
512-
this._glyphRenderer.value!.updateCell(x, y, NULL_CELL_CODE, 0, 0, 0, NULL_CELL_CHAR, 0);
515+
this._glyphRenderer.value!.updateCell(x, y, NULL_CELL_CODE, 0, 0, 0, NULL_CELL_CHAR, 0, 0);
513516
this._model.cells[j] = NULL_CELL_CODE;
514517
this._model.cells[j + RENDER_MODEL_BG_OFFSET] = this._cellColorResolver.result.bg;
515518
this._model.cells[j + RENDER_MODEL_FG_OFFSET] = this._cellColorResolver.result.fg;

src/browser/renderer/shared/RendererUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ function isBoxOrBlockGlyph(codepoint: number): boolean {
2727
return 0x2500 <= codepoint && codepoint <= 0x259F;
2828
}
2929

30+
export function isEmoji(codepoint: number): boolean {
31+
return (
32+
codepoint >= 0x1F600 && codepoint <= 0x1F64F || // Emoticons
33+
codepoint >= 0x1F300 && codepoint <= 0x1F5FF || // Misc Symbols and Pictographs
34+
codepoint >= 0x1F680 && codepoint <= 0x1F6FF || // Transport and Map
35+
codepoint >= 0x2600 && codepoint <= 0x26FF || // Misc symbols
36+
codepoint >= 0x2700 && codepoint <= 0x27BF || // Dingbats
37+
codepoint >= 0xFE00 && codepoint <= 0xFE0F || // Variation Selectors
38+
codepoint >= 0x1F900 && codepoint <= 0x1F9FF || // Supplemental Symbols and Pictographs
39+
codepoint >= 0x1F1E6 && codepoint <= 0x1F1FF
40+
);
41+
}
42+
3043
export function treatGlyphAsBackgroundColor(codepoint: number): boolean {
3144
return isPowerlineGlyph(codepoint) || isBoxOrBlockGlyph(codepoint);
3245
}

src/browser/services/RenderService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export class RenderService extends Disposable implements IRenderService {
8787
'fontSize',
8888
'fontWeight',
8989
'fontWeightBold',
90-
'minimumContrastRatio'
90+
'minimumContrastRatio',
91+
'rescaleOverlappingGlyphs'
9192
], () => {
9293
this.clear();
9394
this.handleResize(bufferService.cols, bufferService.rows);

src/common/services/OptionsService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = {
4444
allowTransparency: false,
4545
tabStopWidth: 8,
4646
theme: {},
47+
rescaleOverlappingGlyphs: false,
4748
rightClickSelectsWord: isMac,
4849
windowOptions: {},
4950
windowsMode: false,

src/common/services/Services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export interface ITerminalOptions {
234234
macOptionIsMeta?: boolean;
235235
macOptionClickForcesSelection?: boolean;
236236
minimumContrastRatio?: number;
237+
rescaleOverlappingGlyphs?: boolean;
237238
rightClickSelectsWord?: boolean;
238239
rows?: number;
239240
screenReaderMode?: boolean;

typings/xterm-headless.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ declare module '@xterm/headless' {
140140
*/
141141
minimumContrastRatio?: number;
142142

143+
/**
144+
* Whether to rescale glyphs horizontally that are a single cell wide but
145+
* have glyphs that would overlap following cell(s). This typically happens
146+
* for ambiguous width characters (eg. the roman numeral characters U+2160+)
147+
* which aren't featured in monospace fonts. Emoji glyphs are never
148+
* rescaled. This is an important feature for achieving GB18030 compliance.
149+
*
150+
* Note that this doesn't work with the DOM renderer. The default is false.
151+
*/
152+
rescaleOverlappingGlyphs?: boolean;
153+
143154
/**
144155
* Whether to select the word under the cursor on right click, this is
145156
* standard behavior in a lot of macOS applications.

typings/xterm.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ declare module '@xterm/xterm' {
209209
*/
210210
minimumContrastRatio?: number;
211211

212+
/**
213+
* Whether to rescale glyphs horizontally that are a single cell wide but
214+
* have glyphs that would overlap following cell(s). This typically happens
215+
* for ambiguous width characters (eg. the roman numeral characters U+2160+)
216+
* which aren't featured in monospace fonts. Emoji glyphs are never
217+
* rescaled. This is an important feature for achieving GB18030 compliance.
218+
*
219+
* Note that this doesn't work with the DOM renderer. The default is false.
220+
*/
221+
rescaleOverlappingGlyphs?: boolean;
222+
212223
/**
213224
* Whether to select the word under the cursor on right click, this is
214225
* standard behavior in a lot of macOS applications.

0 commit comments

Comments
 (0)