@trismegisto/terminal 3.3.13 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/dist/addons/attach.d.ts +16 -0
- package/dist/addons/attach.js +58 -0
- package/dist/addons/attach.js.map +1 -0
- package/dist/addons/clipboard.d.ts +31 -0
- package/dist/addons/clipboard.js +77 -0
- package/dist/addons/clipboard.js.map +1 -0
- package/dist/addons/fit.d.ts +19 -0
- package/dist/addons/fit.js +69 -0
- package/dist/addons/fit.js.map +1 -0
- package/dist/addons/image/iip.d.ts +38 -0
- package/dist/addons/image/iip.js +158 -0
- package/dist/addons/image/iip.js.map +1 -0
- package/dist/addons/image/index.d.ts +91 -0
- package/dist/addons/image/index.js +783 -0
- package/dist/addons/image/index.js.map +1 -0
- package/dist/addons/image/kitty.d.ts +57 -0
- package/dist/addons/image/kitty.js +92 -0
- package/dist/addons/image/kitty.js.map +1 -0
- package/dist/addons/image/qoi.d.ts +5 -0
- package/dist/addons/image/qoi.js +70 -0
- package/dist/addons/image/qoi.js.map +1 -0
- package/dist/addons/image/sixel.d.ts +30 -0
- package/dist/addons/image/sixel.js +251 -0
- package/dist/addons/image/sixel.js.map +1 -0
- package/dist/addons/image/storage.d.ts +68 -0
- package/dist/addons/image/storage.js +87 -0
- package/dist/addons/image/storage.js.map +1 -0
- package/dist/addons/ligatures.d.ts +17 -0
- package/dist/addons/ligatures.js +66 -0
- package/dist/addons/ligatures.js.map +1 -0
- package/dist/addons/program-status.d.ts +32 -0
- package/dist/addons/program-status.js +196 -0
- package/dist/addons/program-status.js.map +1 -0
- package/dist/addons/progress.d.ts +21 -0
- package/dist/addons/progress.js +56 -0
- package/dist/addons/progress.js.map +1 -0
- package/dist/addons/search.d.ts +46 -0
- package/dist/addons/search.js +188 -0
- package/dist/addons/search.js.map +1 -0
- package/dist/addons/serialize.d.ts +35 -0
- package/dist/addons/serialize.js +313 -0
- package/dist/addons/serialize.js.map +1 -0
- package/dist/addons/unicode.d.ts +19 -0
- package/dist/addons/unicode.js +44 -0
- package/dist/addons/unicode.js.map +1 -0
- package/dist/addons/web-fonts.d.ts +22 -0
- package/dist/addons/web-fonts.js +88 -0
- package/dist/addons/web-fonts.js.map +1 -0
- package/dist/addons/web-links.d.ts +22 -0
- package/dist/addons/web-links.js +89 -0
- package/dist/addons/web-links.js.map +1 -0
- package/dist/addons/webgl/atlas.d.ts +57 -0
- package/dist/addons/webgl/atlas.js +90 -0
- package/dist/addons/webgl/atlas.js.map +1 -0
- package/dist/addons/webgl/box.d.ts +12 -0
- package/dist/addons/webgl/box.js +105 -0
- package/dist/addons/webgl/box.js.map +1 -0
- package/dist/addons/webgl/frame-model.d.ts +28 -0
- package/dist/addons/webgl/frame-model.js +105 -0
- package/dist/addons/webgl/frame-model.js.map +1 -0
- package/dist/addons/webgl/gl-renderer.d.ts +24 -0
- package/dist/addons/webgl/gl-renderer.js +211 -0
- package/dist/addons/webgl/gl-renderer.js.map +1 -0
- package/dist/addons/webgl/webgl-addon.d.ts +42 -0
- package/dist/addons/webgl/webgl-addon.js +185 -0
- package/dist/addons/webgl/webgl-addon.js.map +1 -0
- package/dist/core/attributes.d.ts +53 -0
- package/dist/core/attributes.js +75 -0
- package/dist/core/attributes.js.map +1 -0
- package/dist/core/buffer-line.d.ts +55 -0
- package/dist/core/buffer-line.js +242 -0
- package/dist/core/buffer-line.js.map +1 -0
- package/dist/core/buffer.d.ts +74 -0
- package/dist/core/buffer.js +277 -0
- package/dist/core/buffer.js.map +1 -0
- package/dist/core/charsets.d.ts +8 -0
- package/dist/core/charsets.js +79 -0
- package/dist/core/charsets.js.map +1 -0
- package/dist/core/circular-list.d.ts +22 -0
- package/dist/core/circular-list.js +84 -0
- package/dist/core/circular-list.js.map +1 -0
- package/dist/core/colors.d.ts +25 -0
- package/dist/core/colors.js +111 -0
- package/dist/core/colors.js.map +1 -0
- package/dist/core/event.d.ts +19 -0
- package/dist/core/event.js +39 -0
- package/dist/core/event.js.map +1 -0
- package/dist/core/index.d.ts +11 -0
- package/dist/core/index.js +13 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/keyboard.d.ts +76 -0
- package/dist/core/keyboard.js +249 -0
- package/dist/core/keyboard.js.map +1 -0
- package/dist/core/parser.d.ts +73 -0
- package/dist/core/parser.js +483 -0
- package/dist/core/parser.js.map +1 -0
- package/dist/core/terminal-core.d.ts +314 -0
- package/dist/core/terminal-core.js +1514 -0
- package/dist/core/terminal-core.js.map +1 -0
- package/dist/core/unicode.d.ts +36 -0
- package/dist/core/unicode.js +185 -0
- package/dist/core/unicode.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/terminal/buffer-api.d.ts +59 -0
- package/dist/terminal/buffer-api.js +174 -0
- package/dist/terminal/buffer-api.js.map +1 -0
- package/dist/terminal/glyphs.d.ts +7 -0
- package/dist/terminal/glyphs.js +135 -0
- package/dist/terminal/glyphs.js.map +1 -0
- package/dist/terminal/i18n/terminal-catalogs.d.ts +15 -0
- package/dist/terminal/i18n/terminal-catalogs.js +22 -0
- package/dist/terminal/i18n/terminal-catalogs.js.map +1 -0
- package/dist/terminal/i18n/terminal-messages.d.ts +32 -0
- package/dist/terminal/i18n/terminal-messages.en.d.ts +2 -0
- package/dist/terminal/i18n/terminal-messages.en.js +23 -0
- package/dist/terminal/i18n/terminal-messages.en.js.map +1 -0
- package/dist/terminal/i18n/terminal-messages.es.d.ts +2 -0
- package/dist/terminal/i18n/terminal-messages.es.js +23 -0
- package/dist/terminal/i18n/terminal-messages.es.js.map +1 -0
- package/dist/terminal/i18n/terminal-messages.js +23 -0
- package/dist/terminal/i18n/terminal-messages.js.map +1 -0
- package/dist/terminal/renderer.d.ts +120 -0
- package/dist/terminal/renderer.js +585 -0
- package/dist/terminal/renderer.js.map +1 -0
- package/dist/terminal/styles.d.ts +2 -0
- package/dist/terminal/styles.js +117 -0
- package/dist/terminal/styles.js.map +1 -0
- package/dist/terminal/terminal-messages.d.ts +2 -0
- package/dist/terminal/terminal-messages.js +5 -0
- package/dist/terminal/terminal-messages.js.map +1 -0
- package/dist/terminal/terminal-view.d.ts +118 -0
- package/dist/terminal/terminal-view.js +388 -0
- package/dist/terminal/terminal-view.js.map +1 -0
- package/dist/terminal/terminal.d.ts +198 -0
- package/dist/terminal/terminal.js +1468 -0
- package/dist/terminal/terminal.js.map +1 -0
- package/dist/terminal/token-theme.d.ts +12 -0
- package/dist/terminal/token-theme.js +66 -0
- package/dist/terminal/token-theme.js.map +1 -0
- package/dist/terminal/types.d.ts +364 -0
- package/dist/terminal/types.js +2 -0
- package/dist/terminal/types.js.map +1 -0
- package/package.json +31 -23
- package/fesm2022/trismegisto-terminal.mjs +0 -9717
- package/fesm2022/trismegisto-terminal.mjs.map +0 -1
- package/types/trismegisto-terminal.d.ts +0 -1952
- package/types/trismegisto-terminal.d.ts.map +0 -1
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
import { DisposableStore, Emitter, toDisposable } from '../../core/event';
|
|
2
|
+
import { decodeBase64Strict, iipDisplaySize, imageHeader, parseIip } from './iip';
|
|
3
|
+
import { KittyAssembler, inflate, kittyReply } from './kitty';
|
|
4
|
+
import { decodeQoi } from './qoi';
|
|
5
|
+
import { MAX_PALETTE, decodeSixel, defaultSixelPalette } from './sixel';
|
|
6
|
+
import { ImageStorage } from './storage';
|
|
7
|
+
const DEFAULT_OPTIONS = {
|
|
8
|
+
enableSizeReports: true,
|
|
9
|
+
// 4096 × 4096 pixels.
|
|
10
|
+
pixelLimit: 16777216,
|
|
11
|
+
storageLimit: 128,
|
|
12
|
+
showPlaceholder: true,
|
|
13
|
+
sixelSupport: true,
|
|
14
|
+
sixelScrolling: true,
|
|
15
|
+
sixelPaletteLimit: 256,
|
|
16
|
+
sixelSizeLimit: 25000000,
|
|
17
|
+
iipSupport: true,
|
|
18
|
+
iipSizeLimit: 20000000,
|
|
19
|
+
kittySupport: true,
|
|
20
|
+
kittySizeLimit: 20000000,
|
|
21
|
+
};
|
|
22
|
+
const first = (value) => (Array.isArray(value) ? value[0] : value ?? 0);
|
|
23
|
+
export class ImageAddon {
|
|
24
|
+
options;
|
|
25
|
+
storage;
|
|
26
|
+
store = new DisposableStore();
|
|
27
|
+
imageAddedEmitter = new Emitter();
|
|
28
|
+
onImageAdded = this.imageAddedEmitter.event;
|
|
29
|
+
terminal;
|
|
30
|
+
core;
|
|
31
|
+
sixelScrolling;
|
|
32
|
+
paletteLimit;
|
|
33
|
+
palette;
|
|
34
|
+
placeholder;
|
|
35
|
+
kitty;
|
|
36
|
+
kittyImages = new Map();
|
|
37
|
+
layer;
|
|
38
|
+
canvas;
|
|
39
|
+
frame;
|
|
40
|
+
originalPrint;
|
|
41
|
+
wrappedPrint;
|
|
42
|
+
constructor(options = {}) {
|
|
43
|
+
this.options = { ...DEFAULT_OPTIONS, ...stripUndefined(options) };
|
|
44
|
+
this.options.sixelPaletteLimit = Math.max(1, Math.min(MAX_PALETTE, this.options.sixelPaletteLimit));
|
|
45
|
+
this.storage = new ImageStorage(this.options.storageLimit);
|
|
46
|
+
this.sixelScrolling = this.options.sixelScrolling;
|
|
47
|
+
this.paletteLimit = this.options.sixelPaletteLimit;
|
|
48
|
+
this.palette = defaultSixelPalette(this.paletteLimit);
|
|
49
|
+
this.placeholder = this.options.showPlaceholder;
|
|
50
|
+
this.kitty = new KittyAssembler(this.options.kittySizeLimit);
|
|
51
|
+
}
|
|
52
|
+
// ---------------------------------------------------------------- ciclo de vida
|
|
53
|
+
activate(terminal) {
|
|
54
|
+
const term = terminal;
|
|
55
|
+
const core = term._internals.core;
|
|
56
|
+
this.terminal = term;
|
|
57
|
+
this.core = core;
|
|
58
|
+
const o = this.options;
|
|
59
|
+
if (o.enableSizeReports) {
|
|
60
|
+
core.options.windowOptions = { ...core.options.windowOptions, getWinSizePixels: true, getCellSizePixels: true, getWinSizeChars: true };
|
|
61
|
+
}
|
|
62
|
+
const p = core.parser;
|
|
63
|
+
this.store.add(p.registerDcsHandler({ final: 'q' }, (data, params) => this.onSixel(data, params)));
|
|
64
|
+
this.store.add(p.registerOscHandler(1337, (data) => this.onIip(data)));
|
|
65
|
+
this.store.add(p.registerApcHandler({ final: 'G' }, (data) => this.onKitty(data)));
|
|
66
|
+
this.store.add(p.registerCsiHandler({ prefix: '?', final: 'h' }, (ps) => this.decset(ps, true)));
|
|
67
|
+
this.store.add(p.registerCsiHandler({ prefix: '?', final: 'l' }, (ps) => this.decset(ps, false)));
|
|
68
|
+
this.store.add(p.registerCsiHandler({ prefix: '?', intermediates: '$', final: 'p' }, (ps) => this.decrqm(ps)));
|
|
69
|
+
this.store.add(p.registerCsiHandler({ prefix: '?', final: 'S' }, (ps) => this.xtsmgraphics(ps)));
|
|
70
|
+
// RIS limpa tudo; DECSTR restaura a paleta. Os dois seguem para o núcleo (false).
|
|
71
|
+
this.store.add(p.registerEscHandler({ final: 'c' }, () => {
|
|
72
|
+
this.resetRuntime();
|
|
73
|
+
return false;
|
|
74
|
+
}));
|
|
75
|
+
this.store.add(p.registerCsiHandler({ intermediates: '!', final: 'p' }, () => {
|
|
76
|
+
this.resetPalette();
|
|
77
|
+
return false;
|
|
78
|
+
}));
|
|
79
|
+
// ED 2 apaga as imagens da tela; ED 3, as do histórico.
|
|
80
|
+
this.store.add(p.registerCsiHandler({ final: 'J' }, (ps) => {
|
|
81
|
+
this.eraseInDisplay(first(ps[0]));
|
|
82
|
+
return false;
|
|
83
|
+
}));
|
|
84
|
+
// Texto escrito por cima de uma imagem apaga a imagem daquelas células.
|
|
85
|
+
this.originalPrint = p.printHandler;
|
|
86
|
+
this.wrappedPrint = (data, start, end) => this.print(data, start, end);
|
|
87
|
+
p.printHandler = this.wrappedPrint;
|
|
88
|
+
// Troca de tela apaga as imagens da alternativa.
|
|
89
|
+
this.store.add(core.onBufferChange(() => {
|
|
90
|
+
for (const placement of this.storage.placements.filter((pl) => pl.buffer === 'alternate'))
|
|
91
|
+
this.storage.removePlacement(placement);
|
|
92
|
+
this.refresh();
|
|
93
|
+
}));
|
|
94
|
+
this.store.add(core.onWriteParsed(() => this.attach()));
|
|
95
|
+
this.attach();
|
|
96
|
+
}
|
|
97
|
+
dispose() {
|
|
98
|
+
const core = this.core;
|
|
99
|
+
if (core && this.wrappedPrint && core.parser.printHandler === this.wrappedPrint && this.originalPrint)
|
|
100
|
+
core.parser.printHandler = this.originalPrint;
|
|
101
|
+
this.store.dispose();
|
|
102
|
+
this.frame?.dispose();
|
|
103
|
+
this.layer?.remove();
|
|
104
|
+
this.layer = this.canvas = undefined;
|
|
105
|
+
this.storage.clear();
|
|
106
|
+
this.kittyImages.clear();
|
|
107
|
+
this.imageAddedEmitter.dispose();
|
|
108
|
+
this.terminal = this.core = undefined;
|
|
109
|
+
}
|
|
110
|
+
/** Volta as opções de execução ao que o construtor recebeu e esvazia o armazenamento. */
|
|
111
|
+
reset() {
|
|
112
|
+
this.resetRuntime();
|
|
113
|
+
this.storage.limitMb = this.options.storageLimit;
|
|
114
|
+
this.placeholder = this.options.showPlaceholder;
|
|
115
|
+
this.refresh();
|
|
116
|
+
}
|
|
117
|
+
resetRuntime() {
|
|
118
|
+
this.sixelScrolling = this.options.sixelScrolling;
|
|
119
|
+
this.paletteLimit = this.options.sixelPaletteLimit;
|
|
120
|
+
this.palette = defaultSixelPalette(this.paletteLimit);
|
|
121
|
+
this.kitty.reset();
|
|
122
|
+
this.kittyImages.clear();
|
|
123
|
+
this.storage.clear();
|
|
124
|
+
}
|
|
125
|
+
resetPalette() {
|
|
126
|
+
this.paletteLimit = this.options.sixelPaletteLimit;
|
|
127
|
+
this.palette = defaultSixelPalette(this.paletteLimit);
|
|
128
|
+
}
|
|
129
|
+
// ---------------------------------------------------------------- propriedades públicas
|
|
130
|
+
get storageLimit() {
|
|
131
|
+
return this.storage.limitMb;
|
|
132
|
+
}
|
|
133
|
+
set storageLimit(limit) {
|
|
134
|
+
this.storage.limitMb = Math.max(0, limit);
|
|
135
|
+
this.storage.enforce();
|
|
136
|
+
this.refresh();
|
|
137
|
+
}
|
|
138
|
+
get storageUsage() {
|
|
139
|
+
return this.storage.usageMb;
|
|
140
|
+
}
|
|
141
|
+
get showPlaceholder() {
|
|
142
|
+
return this.placeholder;
|
|
143
|
+
}
|
|
144
|
+
set showPlaceholder(value) {
|
|
145
|
+
this.placeholder = value;
|
|
146
|
+
this.refresh();
|
|
147
|
+
}
|
|
148
|
+
/** O canvas com a imagem original (sem redimensionar) na posição do buffer (0-based, histórico incluído). */
|
|
149
|
+
getImageAtBufferCell(x, y) {
|
|
150
|
+
const hit = this.placementAt(x, y);
|
|
151
|
+
if (!hit || !hit.placement.image.source)
|
|
152
|
+
return undefined;
|
|
153
|
+
return this.toCanvas(hit.placement.image);
|
|
154
|
+
}
|
|
155
|
+
/** Um canvas novo com só o pedaço da imagem naquela célula (no tamanho da célula). */
|
|
156
|
+
extractTileAtBufferCell(x, y) {
|
|
157
|
+
const hit = this.placementAt(x, y);
|
|
158
|
+
if (!hit || !hit.placement.image.source)
|
|
159
|
+
return undefined;
|
|
160
|
+
const { placement, row, col } = hit;
|
|
161
|
+
const cell = this.cell();
|
|
162
|
+
const doc = this.document();
|
|
163
|
+
if (!doc)
|
|
164
|
+
return undefined;
|
|
165
|
+
const tile = doc.createElement('canvas');
|
|
166
|
+
tile.width = Math.max(1, Math.round(cell.width));
|
|
167
|
+
tile.height = Math.max(1, Math.round(cell.height));
|
|
168
|
+
const ctx = tile.getContext('2d');
|
|
169
|
+
const source = this.drawable(placement.image);
|
|
170
|
+
if (ctx && source) {
|
|
171
|
+
// Célula da exibição → retângulo na imagem original.
|
|
172
|
+
const scaleX = placement.image.width / placement.displayWidth;
|
|
173
|
+
const scaleY = placement.image.height / placement.displayHeight;
|
|
174
|
+
ctx.drawImage(source, col * placement.cellWidth * scaleX, row * placement.cellHeight * scaleY, placement.cellWidth * scaleX, placement.cellHeight * scaleY, 0, 0, tile.width, tile.height);
|
|
175
|
+
}
|
|
176
|
+
return tile;
|
|
177
|
+
}
|
|
178
|
+
// ---------------------------------------------------------------- SIXEL
|
|
179
|
+
onSixel(data, params) {
|
|
180
|
+
if (!this.options.sixelSupport || !this.core)
|
|
181
|
+
return false;
|
|
182
|
+
if (data.length > this.options.sixelSizeLimit)
|
|
183
|
+
return true;
|
|
184
|
+
const image = decodeSixel(data, { palette: this.palette, backgroundSelect: first(params[1]), pixelLimit: this.options.pixelLimit });
|
|
185
|
+
if (!image)
|
|
186
|
+
return true;
|
|
187
|
+
const entry = this.storage.add({ kind: 'pixels', width: image.width, height: image.height, data: image.data });
|
|
188
|
+
if (!entry)
|
|
189
|
+
return true;
|
|
190
|
+
this.place(entry, image.width, image.height, { scroll: this.sixelScrolling, cursor: 'sixel' });
|
|
191
|
+
this.imageAddedEmitter.fire();
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
/** DECSET/DECRST 80 (DECSDM): `h` desliga a rolagem do SIXEL; `l` liga. O resto segue para o núcleo. */
|
|
195
|
+
decset(params, on) {
|
|
196
|
+
if (params.some((v) => first(v) === 80))
|
|
197
|
+
this.sixelScrolling = !on;
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
decrqm(params) {
|
|
201
|
+
if (first(params[0]) !== 80)
|
|
202
|
+
return false;
|
|
203
|
+
this.reply(`\x1b[?80;${this.sixelScrolling ? 2 : 1}$y`);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
/** XTSMGRAPHICS (`CSI ? Pi ; Pa ; Pv S`): registradores de cor (1) e geometria do SIXEL (2). */
|
|
207
|
+
xtsmgraphics(params) {
|
|
208
|
+
const item = first(params[0]);
|
|
209
|
+
const action = first(params[1]);
|
|
210
|
+
const value = first(params[2]);
|
|
211
|
+
if (item === 1) {
|
|
212
|
+
if (action === 2)
|
|
213
|
+
this.resetPalette();
|
|
214
|
+
else if (action === 3) {
|
|
215
|
+
this.paletteLimit = Math.max(1, Math.min(MAX_PALETTE, value || 1));
|
|
216
|
+
const next = defaultSixelPalette(this.paletteLimit);
|
|
217
|
+
next.set(this.palette.subarray(0, Math.min(next.length, this.palette.length)));
|
|
218
|
+
this.palette = next;
|
|
219
|
+
}
|
|
220
|
+
else if (action !== 1 && action !== 4) {
|
|
221
|
+
this.reply('\x1b[?1;2;0S');
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
this.reply(`\x1b[?1;0;${action === 4 ? MAX_PALETTE : this.paletteLimit}S`);
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
if (item === 2) {
|
|
228
|
+
if (action === 1 || action === 4) {
|
|
229
|
+
const cell = this.cell();
|
|
230
|
+
const core = this.core;
|
|
231
|
+
let w = Math.round(core.cols * cell.width);
|
|
232
|
+
let h = Math.round(core.rows * cell.height);
|
|
233
|
+
if (w * h > this.options.pixelLimit) {
|
|
234
|
+
const scale = Math.sqrt(this.options.pixelLimit / (w * h));
|
|
235
|
+
w = Math.floor(w * scale);
|
|
236
|
+
h = Math.floor(h * scale);
|
|
237
|
+
}
|
|
238
|
+
this.reply(`\x1b[?2;0;${w};${h}S`);
|
|
239
|
+
}
|
|
240
|
+
else
|
|
241
|
+
this.reply('\x1b[?2;3;0S');
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
this.reply(`\x1b[?${item};1;0S`);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
// ---------------------------------------------------------------- IIP
|
|
248
|
+
onIip(data) {
|
|
249
|
+
if (!this.options.iipSupport || !this.core)
|
|
250
|
+
return false;
|
|
251
|
+
const request = parseIip(data, this.options.iipSizeLimit);
|
|
252
|
+
if (!request || !request.inline)
|
|
253
|
+
return true;
|
|
254
|
+
const header = imageHeader(request.data);
|
|
255
|
+
if (!header)
|
|
256
|
+
return true;
|
|
257
|
+
if (header.width * header.height > this.options.pixelLimit)
|
|
258
|
+
return true;
|
|
259
|
+
const placeDecoded = (source) => {
|
|
260
|
+
const cell = this.cell();
|
|
261
|
+
const core = this.core;
|
|
262
|
+
if (!core)
|
|
263
|
+
return true;
|
|
264
|
+
const size = iipDisplaySize(request, source, cell, { width: core.cols * cell.width, height: core.rows * cell.height });
|
|
265
|
+
if (!size || size.width * size.height > this.options.pixelLimit)
|
|
266
|
+
return true;
|
|
267
|
+
const entry = this.storage.add(source);
|
|
268
|
+
if (!entry)
|
|
269
|
+
return true;
|
|
270
|
+
this.place(entry, size.width, size.height, { scroll: true, cursor: 'after' });
|
|
271
|
+
this.imageAddedEmitter.fire();
|
|
272
|
+
return true;
|
|
273
|
+
};
|
|
274
|
+
if (header.format === 'qoi') {
|
|
275
|
+
const image = decodeQoi(request.data, this.options.pixelLimit);
|
|
276
|
+
return image ? placeDecoded({ kind: 'pixels', ...image }) : true;
|
|
277
|
+
}
|
|
278
|
+
const decoded = decodeBitmap(request.data);
|
|
279
|
+
if (!decoded)
|
|
280
|
+
return true;
|
|
281
|
+
return decoded.then((bitmap) => (bitmap ? placeDecoded({ kind: 'bitmap', width: bitmap.width, height: bitmap.height, bitmap }) : true));
|
|
282
|
+
}
|
|
283
|
+
// ---------------------------------------------------------------- kitty
|
|
284
|
+
onKitty(data) {
|
|
285
|
+
if (!this.options.kittySupport || !this.core)
|
|
286
|
+
return false;
|
|
287
|
+
const result = this.kitty.push(data);
|
|
288
|
+
if (!result)
|
|
289
|
+
return true;
|
|
290
|
+
if ('error' in result) {
|
|
291
|
+
this.kittyRespond(result.control, result.error);
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
const { control, payload } = result;
|
|
295
|
+
switch (control.a) {
|
|
296
|
+
case 'd':
|
|
297
|
+
this.kittyDelete(control);
|
|
298
|
+
return true;
|
|
299
|
+
case 'p': {
|
|
300
|
+
const entry = this.kittyImages.get(control.i);
|
|
301
|
+
if (!entry || !entry.source) {
|
|
302
|
+
this.kittyRespond(control, 'ENOENT:imagem não encontrada');
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
this.kittyPlace(entry, control);
|
|
306
|
+
this.kittyRespond(control, 'OK');
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
case 't':
|
|
310
|
+
case 'T':
|
|
311
|
+
case 'q':
|
|
312
|
+
return this.kittyTransmit(control, payload);
|
|
313
|
+
default:
|
|
314
|
+
this.kittyRespond(control, 'EINVAL:ação desconhecida');
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
kittyTransmit(control, payload) {
|
|
319
|
+
if (control.t !== 'd') {
|
|
320
|
+
this.kittyRespond(control, 'ENOTSUPPORTED:só a transmissão direta (t=d)');
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
const bytes = decodeBase64Strict(payload);
|
|
324
|
+
if (!bytes) {
|
|
325
|
+
this.kittyRespond(control, 'EINVAL:base64 inválido');
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
const finish = (source, error = 'EINVAL:imagem inválida') => {
|
|
329
|
+
if (!source) {
|
|
330
|
+
this.kittyRespond(control, error);
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
if (source.width * source.height > this.options.pixelLimit) {
|
|
334
|
+
this.kittyRespond(control, 'EFBIG:imagem grande demais');
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
// a=q só confere se a imagem seria aceita.
|
|
338
|
+
if (control.a === 'q') {
|
|
339
|
+
this.kittyRespond(control, 'OK');
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
const entry = this.storage.add(source);
|
|
343
|
+
if (!entry) {
|
|
344
|
+
this.kittyRespond(control, 'ENOSPC:sem espaço para a imagem');
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
if (control.i) {
|
|
348
|
+
const previous = this.kittyImages.get(control.i);
|
|
349
|
+
if (previous && previous !== entry) {
|
|
350
|
+
previous.retain = false;
|
|
351
|
+
if (!this.storage.placements.some((pl) => pl.image === previous))
|
|
352
|
+
this.storage.removeImage(previous);
|
|
353
|
+
}
|
|
354
|
+
this.kittyImages.set(control.i, entry);
|
|
355
|
+
entry.retain = control.a === 't';
|
|
356
|
+
}
|
|
357
|
+
if (control.a === 'T')
|
|
358
|
+
this.kittyPlace(entry, control);
|
|
359
|
+
else if (!control.i)
|
|
360
|
+
this.storage.removeImage(entry);
|
|
361
|
+
this.kittyRespond(control, 'OK');
|
|
362
|
+
this.imageAddedEmitter.fire();
|
|
363
|
+
return true;
|
|
364
|
+
};
|
|
365
|
+
const decode = (raw) => {
|
|
366
|
+
if (control.f === 24 || control.f === 32) {
|
|
367
|
+
const channels = control.f === 24 ? 3 : 4;
|
|
368
|
+
const { s: width, v: height } = control;
|
|
369
|
+
if (!width || !height || raw.length < width * height * channels)
|
|
370
|
+
return finish(null, 'EINVAL:tamanho não confere com s e v');
|
|
371
|
+
if (width * height > this.options.pixelLimit)
|
|
372
|
+
return finish(null, 'EFBIG:imagem grande demais');
|
|
373
|
+
const data = new Uint8ClampedArray(width * height * 4);
|
|
374
|
+
for (let i = 0, j = 0; i < width * height; i++, j += channels) {
|
|
375
|
+
data[i * 4] = raw[j];
|
|
376
|
+
data[i * 4 + 1] = raw[j + 1];
|
|
377
|
+
data[i * 4 + 2] = raw[j + 2];
|
|
378
|
+
data[i * 4 + 3] = channels === 4 ? raw[j + 3] : 255;
|
|
379
|
+
}
|
|
380
|
+
return finish({ kind: 'pixels', width, height, data });
|
|
381
|
+
}
|
|
382
|
+
if (control.f === 100) {
|
|
383
|
+
const header = imageHeader(raw);
|
|
384
|
+
if (!header || header.format !== 'png')
|
|
385
|
+
return finish(null, 'EINVAL:PNG inválido');
|
|
386
|
+
if (header.width * header.height > this.options.pixelLimit)
|
|
387
|
+
return finish(null, 'EFBIG:imagem grande demais');
|
|
388
|
+
const decoded = decodeBitmap(raw);
|
|
389
|
+
if (!decoded)
|
|
390
|
+
return finish(null, 'ENOTSUPPORTED:o navegador não decodifica imagens aqui');
|
|
391
|
+
return decoded.then((bitmap) => finish(bitmap ? { kind: 'bitmap', width: bitmap.width, height: bitmap.height, bitmap } : null));
|
|
392
|
+
}
|
|
393
|
+
return finish(null, 'ENOTSUPPORTED:formato não suportado');
|
|
394
|
+
};
|
|
395
|
+
if (control.o === 'z') {
|
|
396
|
+
return inflate(bytes).then((raw) => (raw ? decode(raw) : finish(null, 'ENOTSUPPORTED:sem descompressão zlib aqui')));
|
|
397
|
+
}
|
|
398
|
+
if (control.o)
|
|
399
|
+
return finish(null, 'ENOTSUPPORTED:compressão desconhecida');
|
|
400
|
+
return decode(bytes);
|
|
401
|
+
}
|
|
402
|
+
kittyPlace(entry, control) {
|
|
403
|
+
const cell = this.cell();
|
|
404
|
+
// c/r pedem um tamanho em células; sem eles, o tamanho natural.
|
|
405
|
+
const width = control.c ? control.c * cell.width : control.r ? (control.r * cell.height * entry.width) / entry.height : entry.width;
|
|
406
|
+
const height = control.r ? control.r * cell.height : control.c ? (control.c * cell.width * entry.height) / entry.width : entry.height;
|
|
407
|
+
this.place(entry, Math.max(1, Math.round(width)), Math.max(1, Math.round(height)), {
|
|
408
|
+
scroll: control.C !== 1,
|
|
409
|
+
cursor: control.C === 1 ? 'none' : 'after',
|
|
410
|
+
cols: control.c || undefined,
|
|
411
|
+
rows: control.r || undefined,
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
kittyDelete(control) {
|
|
415
|
+
const target = control.d || 'a';
|
|
416
|
+
if (target === 'a' || target === 'A') {
|
|
417
|
+
for (const placement of [...this.storage.placements])
|
|
418
|
+
this.storage.removePlacement(placement);
|
|
419
|
+
if (target === 'A') {
|
|
420
|
+
for (const entry of this.kittyImages.values())
|
|
421
|
+
this.storage.removeImage(entry);
|
|
422
|
+
this.kittyImages.clear();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
else if ((target === 'i' || target === 'I') && control.i) {
|
|
426
|
+
const entry = this.kittyImages.get(control.i);
|
|
427
|
+
if (entry) {
|
|
428
|
+
for (const placement of this.storage.placements.filter((pl) => pl.image === entry))
|
|
429
|
+
this.storage.removePlacement(placement);
|
|
430
|
+
if (target === 'I') {
|
|
431
|
+
this.storage.removeImage(entry);
|
|
432
|
+
this.kittyImages.delete(control.i);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
this.refresh();
|
|
437
|
+
}
|
|
438
|
+
/** Respostas só quando a imagem tem id; `q=1` cala o OK, `q=2` cala tudo. */
|
|
439
|
+
kittyRespond(control, message) {
|
|
440
|
+
if (!control.i && !control.I)
|
|
441
|
+
return;
|
|
442
|
+
if (control.q === 2 || (control.q === 1 && message === 'OK'))
|
|
443
|
+
return;
|
|
444
|
+
this.reply(kittyReply(control, message));
|
|
445
|
+
}
|
|
446
|
+
// ---------------------------------------------------------------- posição no buffer
|
|
447
|
+
/**
|
|
448
|
+
* Põe a imagem no cursor. Com rolagem, o cursor desce as linhas da imagem (rolando a tela se precisar) e para na
|
|
449
|
+
* primeira coluna da última linha (SIXEL) ou na célula seguinte a ela (IIP e kitty). Sem rolagem, a imagem vai para o
|
|
450
|
+
* canto de cima da tela e o cursor fica onde estava.
|
|
451
|
+
*/
|
|
452
|
+
place(entry, displayWidth, displayHeight, options) {
|
|
453
|
+
const core = this.core;
|
|
454
|
+
const b = core.buffer;
|
|
455
|
+
const cell = this.cell();
|
|
456
|
+
const cols = options.cols ?? Math.max(1, Math.ceil(displayWidth / cell.width));
|
|
457
|
+
const rows = options.rows ?? Math.max(1, Math.ceil(displayHeight / cell.height));
|
|
458
|
+
const isNormal = b === core.normal;
|
|
459
|
+
if (b.wrapPending) {
|
|
460
|
+
// A quebra pendente acontece antes da imagem (ela começa na próxima linha).
|
|
461
|
+
b.wrapPending = false;
|
|
462
|
+
b.x = 0;
|
|
463
|
+
core.index();
|
|
464
|
+
}
|
|
465
|
+
const x = options.scroll ? b.x : 0;
|
|
466
|
+
const offset = options.scroll ? 0 : -b.y;
|
|
467
|
+
const marker = isNormal ? core.registerMarker(offset) : undefined;
|
|
468
|
+
const line = isNormal ? undefined : b.lines.get(b.ybase + b.y + offset);
|
|
469
|
+
if (isNormal && !marker)
|
|
470
|
+
return;
|
|
471
|
+
if (options.scroll && options.cursor !== 'none') {
|
|
472
|
+
for (let r = 1; r < rows; r++)
|
|
473
|
+
core.index();
|
|
474
|
+
if (options.cursor === 'sixel')
|
|
475
|
+
b.x = x;
|
|
476
|
+
else {
|
|
477
|
+
const next = x + cols;
|
|
478
|
+
if (next >= b.cols) {
|
|
479
|
+
b.x = b.cols - 1;
|
|
480
|
+
b.wrapPending = core.modes.wraparoundMode;
|
|
481
|
+
}
|
|
482
|
+
else
|
|
483
|
+
b.x = next;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const placement = {
|
|
487
|
+
image: entry,
|
|
488
|
+
x,
|
|
489
|
+
displayWidth,
|
|
490
|
+
displayHeight,
|
|
491
|
+
cellWidth: cell.width,
|
|
492
|
+
cellHeight: cell.height,
|
|
493
|
+
cols,
|
|
494
|
+
rows,
|
|
495
|
+
marker,
|
|
496
|
+
line,
|
|
497
|
+
buffer: isNormal ? 'normal' : 'alternate',
|
|
498
|
+
erased: new Set(),
|
|
499
|
+
};
|
|
500
|
+
// A imagem nova apaga as antigas nas células que cobre.
|
|
501
|
+
const top = this.topLine(placement);
|
|
502
|
+
if (top >= 0)
|
|
503
|
+
this.eraseRect(top, x, rows, cols, placement.buffer);
|
|
504
|
+
this.storage.addPlacement(placement);
|
|
505
|
+
marker?.onDispose(() => {
|
|
506
|
+
if (this.storage.placements.includes(placement))
|
|
507
|
+
this.storage.removePlacement(placement);
|
|
508
|
+
});
|
|
509
|
+
core.markAllDirty();
|
|
510
|
+
this.refresh();
|
|
511
|
+
}
|
|
512
|
+
/** Linha absoluta da primeira linha da imagem; -1 se ela saiu do buffer. */
|
|
513
|
+
topLine(placement) {
|
|
514
|
+
const core = this.core;
|
|
515
|
+
if (!core)
|
|
516
|
+
return -1;
|
|
517
|
+
if (placement.marker)
|
|
518
|
+
return placement.marker.isDisposed ? -1 : placement.marker.line;
|
|
519
|
+
const lines = core.alt.lines;
|
|
520
|
+
for (let i = 0; i < lines.length; i++)
|
|
521
|
+
if (lines.get(i) === placement.line)
|
|
522
|
+
return i;
|
|
523
|
+
return -1;
|
|
524
|
+
}
|
|
525
|
+
/** A imagem (e a célula dentro dela) na posição do buffer ativo, se a célula não foi apagada. */
|
|
526
|
+
placementAt(x, y) {
|
|
527
|
+
const core = this.core;
|
|
528
|
+
if (!core)
|
|
529
|
+
return undefined;
|
|
530
|
+
const type = core.buffer === core.alt ? 'alternate' : 'normal';
|
|
531
|
+
for (let i = this.storage.placements.length - 1; i >= 0; i--) {
|
|
532
|
+
const placement = this.storage.placements[i];
|
|
533
|
+
if (placement.buffer !== type)
|
|
534
|
+
continue;
|
|
535
|
+
const top = this.topLine(placement);
|
|
536
|
+
if (top < 0)
|
|
537
|
+
continue;
|
|
538
|
+
const row = y - top;
|
|
539
|
+
const col = x - placement.x;
|
|
540
|
+
if (row < 0 || row >= placement.rows || col < 0 || col >= placement.cols)
|
|
541
|
+
continue;
|
|
542
|
+
if (placement.erased.has(row * placement.cols + col))
|
|
543
|
+
continue;
|
|
544
|
+
return { placement, row, col };
|
|
545
|
+
}
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
/** Apaga as células de um retângulo (linhas absolutas) nas imagens que já estavam lá. */
|
|
549
|
+
eraseRect(top, x, rows, cols, buffer) {
|
|
550
|
+
for (const placement of [...this.storage.placements]) {
|
|
551
|
+
if (placement.buffer !== buffer)
|
|
552
|
+
continue;
|
|
553
|
+
const pTop = this.topLine(placement);
|
|
554
|
+
if (pTop < 0)
|
|
555
|
+
continue;
|
|
556
|
+
const r0 = Math.max(top, pTop);
|
|
557
|
+
const r1 = Math.min(top + rows, pTop + placement.rows);
|
|
558
|
+
const c0 = Math.max(x, placement.x);
|
|
559
|
+
const c1 = Math.min(x + cols, placement.x + placement.cols);
|
|
560
|
+
if (r0 >= r1 || c0 >= c1)
|
|
561
|
+
continue;
|
|
562
|
+
for (let r = r0; r < r1; r++)
|
|
563
|
+
for (let c = c0; c < c1; c++)
|
|
564
|
+
placement.erased.add((r - pTop) * placement.cols + (c - placement.x));
|
|
565
|
+
if (placement.erased.size >= placement.rows * placement.cols)
|
|
566
|
+
this.storage.removePlacement(placement);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/** Texto impresso: apaga a imagem nas células escritas (do cursor de antes até o de depois, em ordem de leitura). */
|
|
570
|
+
print(data, start, end) {
|
|
571
|
+
const core = this.core;
|
|
572
|
+
const original = this.originalPrint;
|
|
573
|
+
if (!this.storage.placements.length)
|
|
574
|
+
return original(data, start, end);
|
|
575
|
+
const before = core.buffer;
|
|
576
|
+
const cols = before.cols;
|
|
577
|
+
const from = (before.ybase + before.y) * cols + (before.wrapPending ? cols : before.x);
|
|
578
|
+
original(data, start, end);
|
|
579
|
+
const after = core.buffer;
|
|
580
|
+
if (after !== before)
|
|
581
|
+
return;
|
|
582
|
+
const to = (after.ybase + after.y) * cols + after.x + (after.wrapPending ? 1 : 0);
|
|
583
|
+
if (to <= from)
|
|
584
|
+
return;
|
|
585
|
+
const type = after === core.alt ? 'alternate' : 'normal';
|
|
586
|
+
const firstRow = Math.floor(from / cols);
|
|
587
|
+
const lastRow = Math.floor((to - 1) / cols);
|
|
588
|
+
let changed = false;
|
|
589
|
+
for (const placement of [...this.storage.placements]) {
|
|
590
|
+
if (placement.buffer !== type)
|
|
591
|
+
continue;
|
|
592
|
+
const top = this.topLine(placement);
|
|
593
|
+
if (top < 0 || top > lastRow || top + placement.rows <= firstRow)
|
|
594
|
+
continue;
|
|
595
|
+
for (let r = Math.max(top, firstRow); r <= Math.min(top + placement.rows - 1, lastRow); r++) {
|
|
596
|
+
const rowStart = r * cols;
|
|
597
|
+
const c0 = Math.max(placement.x, from - rowStart);
|
|
598
|
+
const c1 = Math.min(placement.x + placement.cols, to - rowStart, cols);
|
|
599
|
+
for (let c = c0; c < c1; c++) {
|
|
600
|
+
placement.erased.add((r - top) * placement.cols + (c - placement.x));
|
|
601
|
+
changed = true;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
if (placement.erased.size >= placement.rows * placement.cols)
|
|
605
|
+
this.storage.removePlacement(placement);
|
|
606
|
+
}
|
|
607
|
+
if (changed)
|
|
608
|
+
this.refresh();
|
|
609
|
+
}
|
|
610
|
+
eraseInDisplay(mode) {
|
|
611
|
+
const core = this.core;
|
|
612
|
+
if (!core)
|
|
613
|
+
return;
|
|
614
|
+
const b = core.buffer;
|
|
615
|
+
const type = b === core.alt ? 'alternate' : 'normal';
|
|
616
|
+
if (mode === 2)
|
|
617
|
+
this.eraseRect(b.ybase, 0, b.rows, b.cols, type);
|
|
618
|
+
else if (mode === 3) {
|
|
619
|
+
for (const placement of [...this.storage.placements]) {
|
|
620
|
+
if (placement.buffer === 'normal' && this.topLine(placement) + placement.rows <= b.ybase)
|
|
621
|
+
this.storage.removePlacement(placement);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
this.refresh();
|
|
625
|
+
}
|
|
626
|
+
// ---------------------------------------------------------------- desenho
|
|
627
|
+
/** Cria a camada assim que a tela existe (o complemento pode ser carregado antes do `open`). */
|
|
628
|
+
attach() {
|
|
629
|
+
if (this.layer || !this.terminal)
|
|
630
|
+
return;
|
|
631
|
+
const renderer = this.terminal._internals.renderer;
|
|
632
|
+
if (!renderer)
|
|
633
|
+
return;
|
|
634
|
+
this.layer = renderer.addLayer('hmt-images', 'above-rows');
|
|
635
|
+
const canvas = this.layer.ownerDocument.createElement('canvas');
|
|
636
|
+
canvas.style.position = 'absolute';
|
|
637
|
+
canvas.style.left = '0';
|
|
638
|
+
canvas.style.top = '0';
|
|
639
|
+
this.layer.appendChild(canvas);
|
|
640
|
+
this.canvas = canvas;
|
|
641
|
+
this.frame = this.terminal._internals.onFrame(() => this.draw());
|
|
642
|
+
this.store.add(toDisposable(() => this.frame?.dispose()));
|
|
643
|
+
}
|
|
644
|
+
refresh() {
|
|
645
|
+
this.attach();
|
|
646
|
+
this.terminal?._internals.refreshAll();
|
|
647
|
+
}
|
|
648
|
+
draw() {
|
|
649
|
+
const core = this.core;
|
|
650
|
+
const renderer = this.terminal?._internals.renderer;
|
|
651
|
+
const canvas = this.canvas;
|
|
652
|
+
if (!core || !renderer || !canvas)
|
|
653
|
+
return;
|
|
654
|
+
const ctx = canvas.getContext('2d');
|
|
655
|
+
if (!ctx)
|
|
656
|
+
return;
|
|
657
|
+
const cw = renderer.cellWidth;
|
|
658
|
+
const ch = renderer.cellHeight;
|
|
659
|
+
if (!cw || !ch)
|
|
660
|
+
return;
|
|
661
|
+
// As medidas da célula também servem aos relatórios de tamanho (CSI 14/16 t).
|
|
662
|
+
if (!core.pixelMetrics.cellWidth)
|
|
663
|
+
core.pixelMetrics = { cellWidth: cw, cellHeight: ch };
|
|
664
|
+
const width = core.cols * cw;
|
|
665
|
+
const height = core.rows * ch;
|
|
666
|
+
const dpr = canvas.ownerDocument.defaultView?.devicePixelRatio || 1;
|
|
667
|
+
const pw = Math.round(width * dpr);
|
|
668
|
+
const ph = Math.round(height * dpr);
|
|
669
|
+
if (canvas.width !== pw || canvas.height !== ph) {
|
|
670
|
+
canvas.width = pw;
|
|
671
|
+
canvas.height = ph;
|
|
672
|
+
canvas.style.width = `${width}px`;
|
|
673
|
+
canvas.style.height = `${height}px`;
|
|
674
|
+
}
|
|
675
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
676
|
+
ctx.clearRect(0, 0, width, height);
|
|
677
|
+
const b = core.buffer;
|
|
678
|
+
const type = b === core.alt ? 'alternate' : 'normal';
|
|
679
|
+
for (const placement of this.storage.placements) {
|
|
680
|
+
if (placement.buffer !== type)
|
|
681
|
+
continue;
|
|
682
|
+
const top = this.topLine(placement);
|
|
683
|
+
if (top < 0)
|
|
684
|
+
continue;
|
|
685
|
+
const screenRow = top - b.ydisp;
|
|
686
|
+
if (screenRow >= core.rows || screenRow + placement.rows <= 0)
|
|
687
|
+
continue;
|
|
688
|
+
// Na célula de agora (a fonte pode ter mudado): a imagem continua cobrindo as mesmas células.
|
|
689
|
+
const dx = placement.x * cw;
|
|
690
|
+
const dy = screenRow * ch;
|
|
691
|
+
const dw = placement.displayWidth * (cw / placement.cellWidth);
|
|
692
|
+
const dh = placement.displayHeight * (ch / placement.cellHeight);
|
|
693
|
+
const source = this.drawable(placement.image);
|
|
694
|
+
if (source)
|
|
695
|
+
ctx.drawImage(source, dx, dy, dw, dh);
|
|
696
|
+
else if (this.placeholder)
|
|
697
|
+
drawPlaceholder(ctx, dx, dy, placement.cols * cw, placement.rows * ch);
|
|
698
|
+
for (const index of placement.erased) {
|
|
699
|
+
const row = Math.floor(index / placement.cols);
|
|
700
|
+
const col = index % placement.cols;
|
|
701
|
+
ctx.clearRect(dx + col * cw, dy + row * ch, cw, ch);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
drawable(entry) {
|
|
706
|
+
const source = entry.source;
|
|
707
|
+
if (!source)
|
|
708
|
+
return undefined;
|
|
709
|
+
if (source.kind === 'bitmap')
|
|
710
|
+
return source.bitmap;
|
|
711
|
+
return this.toCanvas(entry);
|
|
712
|
+
}
|
|
713
|
+
/** O canvas da imagem original (criado uma vez). Sem contexto 2D (ambiente de teste), volta vazio no tamanho certo. */
|
|
714
|
+
toCanvas(entry) {
|
|
715
|
+
if (entry.canvas)
|
|
716
|
+
return entry.canvas;
|
|
717
|
+
const source = entry.source;
|
|
718
|
+
const doc = this.document();
|
|
719
|
+
if (!source || !doc)
|
|
720
|
+
return undefined;
|
|
721
|
+
const canvas = doc.createElement('canvas');
|
|
722
|
+
canvas.width = source.width;
|
|
723
|
+
canvas.height = source.height;
|
|
724
|
+
const ctx = canvas.getContext('2d');
|
|
725
|
+
if (ctx) {
|
|
726
|
+
if (source.kind === 'pixels' && typeof ImageData !== 'undefined')
|
|
727
|
+
ctx.putImageData(new ImageData(source.data, source.width, source.height), 0, 0);
|
|
728
|
+
else if (source.kind === 'bitmap')
|
|
729
|
+
ctx.drawImage(source.bitmap, 0, 0);
|
|
730
|
+
}
|
|
731
|
+
entry.canvas = canvas;
|
|
732
|
+
return canvas;
|
|
733
|
+
}
|
|
734
|
+
// ---------------------------------------------------------------- auxiliares
|
|
735
|
+
document() {
|
|
736
|
+
return this.terminal?.element?.ownerDocument ?? (typeof document !== 'undefined' ? document : undefined);
|
|
737
|
+
}
|
|
738
|
+
/** Tamanho da célula: o medido pela tela; antes de abrir, o informado ao núcleo; sem nada, 8×16. */
|
|
739
|
+
cell() {
|
|
740
|
+
const renderer = this.terminal?._internals.renderer;
|
|
741
|
+
if (renderer?.cellWidth && renderer.cellHeight)
|
|
742
|
+
return { width: renderer.cellWidth, height: renderer.cellHeight };
|
|
743
|
+
const metrics = this.core?.pixelMetrics;
|
|
744
|
+
if (metrics?.cellWidth && metrics.cellHeight)
|
|
745
|
+
return { width: metrics.cellWidth, height: metrics.cellHeight };
|
|
746
|
+
return { width: 8, height: 16 };
|
|
747
|
+
}
|
|
748
|
+
reply(data) {
|
|
749
|
+
const core = this.core;
|
|
750
|
+
if (core && !core.options.disableStdin)
|
|
751
|
+
core.triggerData(data, false);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/** Decodifica com o navegador (`createImageBitmap`); `null` sem suporte, ou uma promessa que vira `null` se falhar. */
|
|
755
|
+
function decodeBitmap(bytes) {
|
|
756
|
+
if (typeof createImageBitmap !== 'function' || typeof Blob === 'undefined')
|
|
757
|
+
return null;
|
|
758
|
+
return createImageBitmap(new Blob([bytes])).then((bitmap) => bitmap, () => null);
|
|
759
|
+
}
|
|
760
|
+
/** Marcador de posição de imagem despejada: listras diagonais. */
|
|
761
|
+
function drawPlaceholder(ctx, x, y, w, h) {
|
|
762
|
+
ctx.save();
|
|
763
|
+
ctx.beginPath();
|
|
764
|
+
ctx.rect(x, y, w, h);
|
|
765
|
+
ctx.clip();
|
|
766
|
+
ctx.globalAlpha = 0.25;
|
|
767
|
+
ctx.fillStyle = '#808080';
|
|
768
|
+
ctx.fillRect(x, y, w, h);
|
|
769
|
+
ctx.globalAlpha = 0.5;
|
|
770
|
+
ctx.strokeStyle = '#808080';
|
|
771
|
+
ctx.lineWidth = 2;
|
|
772
|
+
for (let d = -h; d < w; d += 8) {
|
|
773
|
+
ctx.beginPath();
|
|
774
|
+
ctx.moveTo(x + d, y + h);
|
|
775
|
+
ctx.lineTo(x + d + h, y);
|
|
776
|
+
ctx.stroke();
|
|
777
|
+
}
|
|
778
|
+
ctx.restore();
|
|
779
|
+
}
|
|
780
|
+
function stripUndefined(value) {
|
|
781
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
|
|
782
|
+
}
|
|
783
|
+
//# sourceMappingURL=index.js.map
|