-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathRendererUtils.ts
164 lines (149 loc) · 5.35 KB
/
RendererUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
import { IDimensions, IRenderDimensions, UnderlineCurlyJoinOrLine, UnderlineCurlyLineType, UnderlineDrawCurlyOp } from 'browser/renderer/shared/Types';
import { TwoKeyMap } from 'common/MultiKeyMap';
export function throwIfFalsy<T>(value: T | undefined | null): T {
if (!value) {
throw new Error('value must not be falsy');
}
return value;
}
export function isPowerlineGlyph(codepoint: number): boolean {
// Only return true for Powerline symbols which require
// different padding and should be excluded from minimum contrast
// ratio standards
return 0xE0A4 <= codepoint && codepoint <= 0xE0D6;
}
export function isRestrictedPowerlineGlyph(codepoint: number): boolean {
return 0xE0B0 <= codepoint && codepoint <= 0xE0B7;
}
function isBoxOrBlockGlyph(codepoint: number): boolean {
return 0x2500 <= codepoint && codepoint <= 0x259F;
}
export function excludeFromContrastRatioDemands(codepoint: number): boolean {
return isPowerlineGlyph(codepoint) || isBoxOrBlockGlyph(codepoint);
}
export function createRenderDimensions(): IRenderDimensions {
return {
css: {
canvas: createDimension(),
cell: createDimension()
},
device: {
canvas: createDimension(),
cell: createDimension(),
char: {
width: 0,
height: 0,
left: 0,
top: 0
}
}
};
}
function createDimension(): IDimensions {
return {
width: 0,
height: 0
};
}
export function computeNextVariantOffset(cellWidth: number, lineWidth: number, currentOffset: number = 0): number {
return (cellWidth - (Math.round(lineWidth) * 2 - currentOffset)) % (Math.round(lineWidth) * 2);
}
// TwoKeyMap
const curlyVariantCache = new TwoKeyMap<number, number, string[]>();
export function getCurlyVariant(cellWidth: number, lineWidth: number, offset: number): string {
if (curlyVariantCache.get(cellWidth, lineWidth)) {
const curlyVariants = curlyVariantCache.get(cellWidth, lineWidth);
if (curlyVariants && curlyVariants.length > 0) {
if (!curlyVariants[offset]) {
return curlyVariants[0];
}
return curlyVariants[offset];
}
}
return '';
}
export function getCurlyVariantOffset(x: number, cellWidth: number, lineWidth: number): number {
if (curlyVariantCache.get(cellWidth, lineWidth)) {
const curlyVariants = curlyVariantCache.get(cellWidth, lineWidth) as any[];
return x % curlyVariants.length;
}
if (!curlyVariantCache.get(cellWidth, lineWidth)) {
const curlyVariants = createDrawCurlyPlan(cellWidth, lineWidth);
curlyVariantCache.set(cellWidth, lineWidth, curlyVariants);
return x % curlyVariants.length;
}
return 0;
}
const defaultCurlyLinePixels = 3;
export function createDrawCurlyPlan(cellWidth: number, lineWidth: number): string[] {
return createVariantSequences(cellWidth, lineWidth, defaultCurlyLinePixels + (lineWidth > 1 ? 1 : 0));
}
function createVariantSequences(cellWidth: number, joinPixels: number, linePixels: number): string[] {
const result: string[] = [];
let totalPixels = cellWidth * ((joinPixels + linePixels) * 2);
let joinOrLine: UnderlineCurlyJoinOrLine = 'join';
let upOrDown: UnderlineCurlyLineType = 'up';
let lastUpOrDown: UnderlineCurlyLineType = 'up';
// Split between cells to be processed
let waitHandlePixels = 0;
while (totalPixels > 0) {
const cellResult: any[] = [];
let cellCurrentWidth = cellWidth;
while (cellCurrentWidth > 0) {
if (joinOrLine === 'join') {
let token: UnderlineDrawCurlyOp = upOrDown === 'up' ? 'Y' : 'B';
if (waitHandlePixels > 0) {
// right
token = lastUpOrDown === 'up' ? 'M' : 'P';
cellResult.push(`${token}${waitHandlePixels}`);
cellCurrentWidth -= waitHandlePixels;
waitHandlePixels = 0;
joinOrLine = 'line';
} else {
// left
const usingWidth = joinPixels;
if (usingWidth > cellCurrentWidth) {
token = lastUpOrDown === 'up' ? 'Z' : 'Q';
cellResult.push(`${token}${cellCurrentWidth}`);
waitHandlePixels = usingWidth - cellCurrentWidth;
cellCurrentWidth = 0;
} else {
cellResult.push(`${token}${joinPixels}`);
cellCurrentWidth -= joinPixels;
joinOrLine = 'line';
}
}
} else if (joinOrLine === 'line') {
const token: UnderlineDrawCurlyOp = upOrDown === 'up' ? 'U' : 'D';
if (waitHandlePixels > 0) {
cellResult.push(`${token}${waitHandlePixels}`);
cellCurrentWidth -= waitHandlePixels;
waitHandlePixels = 0;
joinOrLine = 'join';
lastUpOrDown = upOrDown;
upOrDown = upOrDown === 'up' ? 'down' : 'up';
} else {
const usingWidth = linePixels;
if (usingWidth > cellCurrentWidth) {
cellResult.push(`${token}${cellCurrentWidth}`);
waitHandlePixels = usingWidth - cellCurrentWidth;
cellCurrentWidth = 0;
} else {
cellResult.push(`${token}${linePixels}`);
cellCurrentWidth -= linePixels;
joinOrLine = 'join';
lastUpOrDown = upOrDown;
upOrDown = upOrDown === 'up' ? 'down' : 'up';
}
}
}
}
totalPixels -= cellWidth;
result.push(cellResult.join(' '));
}
return result;
}