Modify variantOffset calculation logic

This commit is contained in:
tisilent
2023-08-25 14:17:36 +08:00
parent 08f7b4c53c
commit bec1642123
14 changed files with 51 additions and 89 deletions
@@ -1,9 +1,8 @@
import { computeVarinatOffset } from 'browser/renderer/shared/RendererUtils';
import { ISelectionRenderModel } from 'browser/renderer/shared/Types';
import { ICoreBrowserService, IThemeService } from 'browser/services/Services';
import { ReadonlyColorSet } from 'browser/Types';
import { Attributes, BgFlags, ExtFlags, FgFlags, NULL_CELL_CODE, UnderlineStyle } from 'common/buffer/Constants';
import { IDecorationService, IUnicodeService } from 'common/services/Services';
import { IDecorationService, IOptionsService } from 'common/services/Services';
import { ICellData } from 'common/Types';
import { Terminal } from 'xterm';
@@ -14,7 +13,6 @@ let $hasFg = false;
let $hasBg = false;
let $isSelected = false;
let $colors: ReadonlyColorSet | undefined;
let $y = -1;
let $variantOffset = 0;
export class CellColorResolver {
@@ -30,11 +28,11 @@ export class CellColorResolver {
constructor(
private readonly _terminal: Terminal,
private readonly _optionService: IOptionsService,
private readonly _selectionRenderModel: ISelectionRenderModel,
private readonly _decorationService: IDecorationService,
private readonly _coreBrowserService: ICoreBrowserService,
private readonly _themeService: IThemeService,
private readonly _unicodeService: IUnicodeService
private readonly _themeService: IThemeService
) {
}
@@ -56,15 +54,12 @@ export class CellColorResolver {
$hasFg = false;
$isSelected = false;
$colors = this._themeService.colors;
if ($y !== y) {
$variantOffset = 0;
}
$y = y;
$variantOffset = 0;
const code = cell.getCode();
if (code === NULL_CELL_CODE && cell.extended.underlineStyle !== UnderlineStyle.DOTTED) {
$variantOffset = 0;
if (code !== NULL_CELL_CODE && cell.extended.underlineStyle === UnderlineStyle.DOTTED) {
const lineWidth = Math.max(1, Math.floor(this._optionService.rawOptions.fontSize * this._coreBrowserService.dpr / 15));
$variantOffset = x * deviceCellWidth % (Math.round(lineWidth) * 2);
}
// Apply decorations on the bottom layer
@@ -151,22 +146,5 @@ export class CellColorResolver {
// Reset overrides variantOffset
this.result.ext &= ~ExtFlags.VARIANT_OFFSET;
this.result.ext |= ($variantOffset << 29) & ExtFlags.VARIANT_OFFSET;
// Compute next variantOffset
if (cell.extended.underlineStyle === UnderlineStyle.DOTTED) {
if (code !== NULL_CELL_CODE) {
const fontSize = this._terminal.options.fontSize;
const lineWidth = Math.max(1, Math.floor(fontSize! * this._coreBrowserService.dpr / 15));
let chWidth: number;
if (typeof code === 'number') {
chWidth = this._unicodeService.wcwidth(code);
} else {
chWidth = this._unicodeService.getStringCellWidth(code);
}
$variantOffset = computeVarinatOffset(deviceCellWidth * chWidth, lineWidth, $variantOffset);
}
} else {
$variantOffset = 0;
}
}
}
@@ -3,11 +3,11 @@
* @license MIT
*/
import { computeVarinatOffset } from 'browser/renderer/shared/RendererUtils';
import { computeNextVarinatOffset } from 'browser/renderer/shared/RendererUtils';
import { assert } from 'chai';
describe('RendererUtils', () => {
it('computeVarinatOffset', () => {
it('computeNextVarinatOffset', () => {
const cellWidth = 11;
const doubleCellWidth = 22;
let line = 1;
@@ -19,7 +19,7 @@ describe('RendererUtils', () => {
let result = [1, 0, 0, 0];
for (let index = 0; index < cells.length; index++) {
const cell = cells[index];
varinatOffset = computeVarinatOffset(cell, line, varinatOffset);
varinatOffset = computeNextVarinatOffset(cell, line, varinatOffset);
assert.equal(varinatOffset, result[index]);
}
@@ -31,7 +31,7 @@ describe('RendererUtils', () => {
result = [3, 2, 0 ,2];
for (let index = 0; index < cells.length; index++) {
const cell = cells[index];
varinatOffset = computeVarinatOffset(cell, line, varinatOffset);
varinatOffset = computeNextVarinatOffset(cell, line, varinatOffset);
assert.equal(varinatOffset, result[index]);
}
@@ -43,7 +43,7 @@ describe('RendererUtils', () => {
result = [5, 4, 2, 0];
for (let index = 0; index < cells.length; index++) {
const cell = cells[index];
varinatOffset = computeVarinatOffset(cell, line, varinatOffset);
varinatOffset = computeNextVarinatOffset(cell, line, varinatOffset);
assert.equal(varinatOffset, result[index]);
}
});
+1 -1
View File
@@ -57,6 +57,6 @@ function createDimension(): IDimensions {
};
}
export function computeVarinatOffset(cellWidth: number, lineWidth: number, currentOffset: number = 0): number {
export function computeNextVarinatOffset(cellWidth: number, lineWidth: number, currentOffset: number = 0): number {
return (cellWidth - (Math.round(lineWidth) * 2 - currentOffset)) % (Math.round(lineWidth) * 2);
}
+11 -17
View File
@@ -6,7 +6,7 @@
import { IColorContrastCache } from 'browser/Types';
import { DIM_OPACITY, TEXT_BASELINE } from 'browser/renderer/shared/Constants';
import { tryDrawCustomChar } from 'browser/renderer/shared/CustomGlyphs';
import { computeVarinatOffset, excludeFromContrastRatioDemands, isPowerlineGlyph, isRestrictedPowerlineGlyph, throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
import { computeNextVarinatOffset, excludeFromContrastRatioDemands, isPowerlineGlyph, isRestrictedPowerlineGlyph, throwIfFalsy } from 'browser/renderer/shared/RendererUtils';
import { IBoundingBox, ICharAtlasConfig, IRasterizedGlyph, ITextureAtlas } from 'browser/renderer/shared/Types';
import { NULL_COLOR, color, rgba } from 'common/Color';
import { EventEmitter } from 'common/EventEmitter';
@@ -599,26 +599,20 @@ export class TextureAtlas implements ITextureAtlas {
case UnderlineStyle.DOTTED:
const offsetWidth = nextOffset === 0 ? 0 :
(nextOffset >= lineWidth ? lineWidth * 2 - nextOffset : lineWidth - nextOffset);
if (offsetWidth === 0) {
// a line and a gap.
const isLineStart = nextOffset >= lineWidth ? false : true;
if (isLineStart === false || offsetWidth === 0) {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft, yTop);
this._tmpCtx.moveTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
} else {
// a line and a gap.
const isLineStart = nextOffset >= lineWidth ? false : true;
if (isLineStart === false) {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
} else {
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft, yTop);
this._tmpCtx.lineTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.moveTo(xChLeft + offsetWidth + lineWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
}
this._tmpCtx.setLineDash([Math.round(lineWidth), Math.round(lineWidth)]);
this._tmpCtx.moveTo(xChLeft, yTop);
this._tmpCtx.lineTo(xChLeft + offsetWidth, yTop);
this._tmpCtx.moveTo(xChLeft + offsetWidth + lineWidth, yTop);
this._tmpCtx.lineTo(xChRight, yTop);
}
nextOffset = computeVarinatOffset(xChRight - xChLeft, lineWidth, nextOffset);
nextOffset = computeNextVarinatOffset(xChRight - xChLeft, lineWidth, nextOffset);
break;
case UnderlineStyle.DASHED:
this._tmpCtx.setLineDash([this._config.devicePixelRatio * 4, this._config.devicePixelRatio * 3]);
+1 -1
View File
@@ -180,7 +180,7 @@ export class ExtendedAttrs implements IExtendedAttrs {
public get underlineVarinatOffset(): number {
const val = (this._ext & ExtFlags.VARIANT_OFFSET) >> 29;
if (val < 0) {
return val ^ 4294967288;
return val ^ 0xFFFFFFF8;
}
return val;
}