@xterm/addon-image 0.10.0-beta.25 → 0.10.0-beta.250
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 +5 -3
- package/lib/addon-image.js +1 -1
- package/lib/addon-image.js.map +1 -1
- package/lib/addon-image.mjs +1 -22
- package/lib/addon-image.mjs.map +4 -4
- package/package.json +5 -5
- package/src/IIPHandler.ts +53 -39
- package/src/IIPHeaderParser.ts +3 -2
- package/src/IIPImageStorage.ts +26 -0
- package/src/IIPMetrics.ts +9 -1
- package/src/ImageAddon.ts +73 -7
- package/src/ImageRenderer.ts +99 -53
- package/src/ImageStorage.ts +128 -64
- package/src/SixelHandler.ts +4 -4
- package/src/SixelImageStorage.ts +50 -0
- package/src/Types.ts +8 -2
- package/src/kitty/KittyGraphicsHandler.ts +819 -0
- package/src/kitty/KittyGraphicsTypes.ts +195 -0
- package/src/kitty/KittyImageStorage.ts +151 -0
- package/typings/addon-image.d.ts +15 -1
package/src/ImageRenderer.ts
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
|
|
6
6
|
import { toRGBA8888 } from 'sixel/lib/Colors';
|
|
7
7
|
import { IDisposable } from '@xterm/xterm';
|
|
8
|
-
import { ICellSize, ITerminalExt, IImageSpec, IRenderDimensions, IRenderService } from './Types';
|
|
9
|
-
import { Disposable, MutableDisposable, toDisposable } from '
|
|
8
|
+
import { ICellSize, ImageLayer, ITerminalExt, IImageSpec, IRenderDimensions, IRenderService } from './Types';
|
|
9
|
+
import { Disposable, MutableDisposable, toDisposable } from 'common/Lifecycle';
|
|
10
10
|
|
|
11
|
-
const
|
|
12
|
-
|
|
11
|
+
const enum Constants {
|
|
12
|
+
PLACEHOLDER_LENGTH = 4096,
|
|
13
|
+
PLACEHOLDER_HEIGHT = 24
|
|
14
|
+
}
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* ImageRenderer - terminal frontend extension:
|
|
@@ -18,8 +20,9 @@ const PLACEHOLDER_HEIGHT = 24;
|
|
|
18
20
|
* - draw image tiles onRender
|
|
19
21
|
*/
|
|
20
22
|
export class ImageRenderer extends Disposable implements IDisposable {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
/** @deprecated Kept for backward compat — points to top layer canvas. */
|
|
24
|
+
public get canvas(): HTMLCanvasElement | undefined { return this._layers.get('top')?.canvas; }
|
|
25
|
+
private _layers = new Map<ImageLayer, CanvasRenderingContext2D>();
|
|
23
26
|
private _placeholder: HTMLCanvasElement | undefined;
|
|
24
27
|
private _placeholderBitmap: ImageBitmap | undefined;
|
|
25
28
|
private _optionsRefresh = this._register(new MutableDisposable());
|
|
@@ -38,7 +41,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
38
41
|
* Only the DOM output canvas should be on the terminal's document,
|
|
39
42
|
* which gets explicitly checked in `insertLayerToDom`.
|
|
40
43
|
*/
|
|
41
|
-
const canvas = (localDocument
|
|
44
|
+
const canvas = (localDocument ?? document).createElement('canvas');
|
|
42
45
|
canvas.width = width | 0;
|
|
43
46
|
canvas.height = height | 0;
|
|
44
47
|
return canvas;
|
|
@@ -86,6 +89,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
86
89
|
});
|
|
87
90
|
this._register(toDisposable(() => {
|
|
88
91
|
this.removeLayerFromDom();
|
|
92
|
+
this.removeLayerFromDom('bottom');
|
|
89
93
|
if (this._terminal._core && this._oldOpen) {
|
|
90
94
|
this._terminal._core.open = this._oldOpen;
|
|
91
95
|
this._oldOpen = undefined;
|
|
@@ -95,8 +99,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
95
99
|
this._oldSetRenderer = undefined;
|
|
96
100
|
}
|
|
97
101
|
this._renderService = undefined;
|
|
98
|
-
this.
|
|
99
|
-
this._ctx = undefined;
|
|
102
|
+
this._layers.clear();
|
|
100
103
|
this._placeholderBitmap?.close();
|
|
101
104
|
this._placeholderBitmap = undefined;
|
|
102
105
|
this._placeholder = undefined;
|
|
@@ -109,7 +112,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
109
112
|
public showPlaceholder(value: boolean): void {
|
|
110
113
|
if (value) {
|
|
111
114
|
if (!this._placeholder && this.cellSize.height !== -1) {
|
|
112
|
-
this._createPlaceHolder(Math.max(this.cellSize.height + 1, PLACEHOLDER_HEIGHT));
|
|
115
|
+
this._createPlaceHolder(Math.max(this.cellSize.height + 1, Constants.PLACEHOLDER_HEIGHT));
|
|
113
116
|
}
|
|
114
117
|
} else {
|
|
115
118
|
this._placeholderBitmap?.close();
|
|
@@ -124,7 +127,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
124
127
|
* Forwarded from internal render service.
|
|
125
128
|
*/
|
|
126
129
|
public get dimensions(): IRenderDimensions | undefined {
|
|
127
|
-
return this.
|
|
130
|
+
return this._terminal.dimensions;
|
|
128
131
|
}
|
|
129
132
|
|
|
130
133
|
/**
|
|
@@ -140,27 +143,38 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
140
143
|
/**
|
|
141
144
|
* Clear a region of the image layer canvas.
|
|
142
145
|
*/
|
|
143
|
-
public clearLines(start: number, end: number): void {
|
|
144
|
-
this.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
(
|
|
149
|
-
|
|
146
|
+
public clearLines(start: number, end: number, layer?: ImageLayer): void {
|
|
147
|
+
const y = start * (this.dimensions?.css.cell.height || 0);
|
|
148
|
+
const w = this.dimensions?.css.canvas.width || 0;
|
|
149
|
+
const h = (++end - start) * (this.dimensions?.css.cell.height || 0);
|
|
150
|
+
if (!layer || layer === 'top') {
|
|
151
|
+
this._layers.get('top')?.clearRect(0, y, w, h);
|
|
152
|
+
}
|
|
153
|
+
if (!layer || layer === 'bottom') {
|
|
154
|
+
this._layers.get('bottom')?.clearRect(0, y, w, h);
|
|
155
|
+
}
|
|
150
156
|
}
|
|
151
157
|
|
|
152
158
|
/**
|
|
153
159
|
* Clear whole image canvas.
|
|
154
160
|
*/
|
|
155
|
-
public clearAll(): void {
|
|
156
|
-
|
|
161
|
+
public clearAll(layer?: ImageLayer): void {
|
|
162
|
+
if (!layer || layer === 'top') {
|
|
163
|
+
const ctx = this._layers.get('top');
|
|
164
|
+
ctx?.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
165
|
+
}
|
|
166
|
+
if (!layer || layer === 'bottom') {
|
|
167
|
+
const ctx = this._layers.get('bottom');
|
|
168
|
+
ctx?.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
169
|
+
}
|
|
157
170
|
}
|
|
158
171
|
|
|
159
172
|
/**
|
|
160
173
|
* Draw neighboring tiles on the image layer canvas.
|
|
161
174
|
*/
|
|
162
175
|
public draw(imgSpec: IImageSpec, tileId: number, col: number, row: number, count: number = 1): void {
|
|
163
|
-
|
|
176
|
+
const ctx = this._layers.get(imgSpec.layer);
|
|
177
|
+
if (!ctx) {
|
|
164
178
|
return;
|
|
165
179
|
}
|
|
166
180
|
const { width, height } = this.cellSize;
|
|
@@ -187,7 +201,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
187
201
|
// Note: For not pixel perfect aligned cells like in the DOM renderer
|
|
188
202
|
// this will move a tile slightly to the top/left (subpixel range, thus ignore it).
|
|
189
203
|
// FIX #34: avoid striping on displays with pixelDeviceRatio != 1 by ceiling height and width
|
|
190
|
-
|
|
204
|
+
ctx.drawImage(
|
|
191
205
|
img,
|
|
192
206
|
Math.floor(sx), Math.floor(sy), Math.ceil(finalWidth), Math.ceil(finalHeight),
|
|
193
207
|
Math.floor(dx), Math.floor(dy), Math.ceil(finalWidth), Math.ceil(finalHeight)
|
|
@@ -227,7 +241,8 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
227
241
|
* Draw a line with placeholder on the image layer canvas.
|
|
228
242
|
*/
|
|
229
243
|
public drawPlaceholder(col: number, row: number, count: number = 1): void {
|
|
230
|
-
|
|
244
|
+
const ctx = this._layers.get('top');
|
|
245
|
+
if (ctx) {
|
|
231
246
|
const { width, height } = this.cellSize;
|
|
232
247
|
|
|
233
248
|
// Don't try to draw anything, if we cannot get valid renderer metrics.
|
|
@@ -236,13 +251,13 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
236
251
|
}
|
|
237
252
|
|
|
238
253
|
if (!this._placeholder) {
|
|
239
|
-
this._createPlaceHolder(Math.max(height + 1, PLACEHOLDER_HEIGHT));
|
|
254
|
+
this._createPlaceHolder(Math.max(height + 1, Constants.PLACEHOLDER_HEIGHT));
|
|
240
255
|
} else if (height >= this._placeholder!.height) {
|
|
241
256
|
this._createPlaceHolder(height + 1);
|
|
242
257
|
}
|
|
243
258
|
if (!this._placeholder) return;
|
|
244
|
-
|
|
245
|
-
this._placeholderBitmap
|
|
259
|
+
ctx.drawImage(
|
|
260
|
+
this._placeholderBitmap ?? this._placeholder!,
|
|
246
261
|
col * width,
|
|
247
262
|
(row * height) % 2 ? 0 : 1, // needs %2 offset correction
|
|
248
263
|
width * count,
|
|
@@ -260,12 +275,13 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
260
275
|
* Checked once from `ImageStorage.render`.
|
|
261
276
|
*/
|
|
262
277
|
public rescaleCanvas(): void {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
278
|
+
const w = this.dimensions?.css.canvas.width || 0;
|
|
279
|
+
const h = this.dimensions?.css.canvas.height || 0;
|
|
280
|
+
for (const ctx of this._layers.values()) {
|
|
281
|
+
if (ctx.canvas.width !== w || ctx.canvas.height !== h) {
|
|
282
|
+
ctx.canvas.width = w;
|
|
283
|
+
ctx.canvas.height = h;
|
|
284
|
+
}
|
|
269
285
|
}
|
|
270
286
|
}
|
|
271
287
|
|
|
@@ -304,38 +320,68 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
304
320
|
this._renderService = this._terminal._core._renderService;
|
|
305
321
|
this._oldSetRenderer = this._renderService.setRenderer.bind(this._renderService);
|
|
306
322
|
this._renderService.setRenderer = (renderer: any) => {
|
|
307
|
-
this.
|
|
323
|
+
for (const key of [...this._layers.keys()]) {
|
|
324
|
+
this.removeLayerFromDom(key);
|
|
325
|
+
}
|
|
308
326
|
this._oldSetRenderer?.call(this._renderService, renderer);
|
|
309
327
|
};
|
|
310
328
|
}
|
|
311
329
|
|
|
312
|
-
public insertLayerToDom(): void {
|
|
330
|
+
public insertLayerToDom(layer: ImageLayer = 'top'): void {
|
|
313
331
|
// make sure that the terminal is attached to a document and to DOM
|
|
314
|
-
if (this.document
|
|
315
|
-
if (!this.canvas) {
|
|
316
|
-
this.canvas = ImageRenderer.createCanvas(
|
|
317
|
-
this.document, this.dimensions?.css.canvas.width || 0,
|
|
318
|
-
this.dimensions?.css.canvas.height || 0
|
|
319
|
-
);
|
|
320
|
-
this.canvas.classList.add('xterm-image-layer');
|
|
321
|
-
this._terminal._core.screenElement.appendChild(this.canvas);
|
|
322
|
-
this._ctx = this.canvas.getContext('2d', { alpha: true, desynchronized: true });
|
|
323
|
-
this.clearAll();
|
|
324
|
-
}
|
|
325
|
-
} else {
|
|
332
|
+
if (!this.document || !this._terminal._core.screenElement) {
|
|
326
333
|
console.warn('image addon: cannot insert output canvas to DOM, missing document or screenElement');
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (this._layers.has(layer)) {
|
|
337
|
+
return;
|
|
327
338
|
}
|
|
339
|
+
const canvas = ImageRenderer.createCanvas(
|
|
340
|
+
this.document, this.dimensions?.css.canvas.width || 0,
|
|
341
|
+
this.dimensions?.css.canvas.height || 0
|
|
342
|
+
);
|
|
343
|
+
canvas.classList.add(`xterm-image-layer-${layer}`);
|
|
344
|
+
const screenElement = this._terminal._core.screenElement;
|
|
345
|
+
// Use isolation to create a stacking context without overriding z-index,
|
|
346
|
+
// which would conflict with integrators (e.g. VS Code) that set their
|
|
347
|
+
// own z-index on the screen element.
|
|
348
|
+
screenElement.style.isolation = 'isolate';
|
|
349
|
+
if (layer === 'bottom') {
|
|
350
|
+
// Use z-index:-1 so it paints behind non-positioned text elements.
|
|
351
|
+
// The screen element needs to be a stacking context (via isolation)
|
|
352
|
+
// to contain the negative z-index, otherwise it would go behind the
|
|
353
|
+
// entire terminal.
|
|
354
|
+
canvas.style.zIndex = '-1';
|
|
355
|
+
screenElement.insertBefore(canvas, screenElement.firstChild);
|
|
356
|
+
} else {
|
|
357
|
+
// Explicit z-index ensures the image canvas reliably stacks above
|
|
358
|
+
// the text layer (DOM renderer rows). z-index: 0 is below the
|
|
359
|
+
// selection overlay (z-index: 1).
|
|
360
|
+
canvas.style.zIndex = '0';
|
|
361
|
+
screenElement.appendChild(canvas);
|
|
362
|
+
}
|
|
363
|
+
const ctx = canvas.getContext('2d', { alpha: true });
|
|
364
|
+
if (!ctx) {
|
|
365
|
+
canvas.remove();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
this._layers.set(layer, ctx);
|
|
369
|
+
this.clearAll(layer);
|
|
328
370
|
}
|
|
329
371
|
|
|
330
|
-
public removeLayerFromDom(): void {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
this.
|
|
372
|
+
public removeLayerFromDom(layer: ImageLayer = 'top'): void {
|
|
373
|
+
const ctx = this._layers.get(layer);
|
|
374
|
+
if (ctx) {
|
|
375
|
+
ctx.canvas.remove();
|
|
376
|
+
this._layers.delete(layer);
|
|
335
377
|
}
|
|
336
378
|
}
|
|
337
379
|
|
|
338
|
-
|
|
380
|
+
public hasLayer(layer: ImageLayer): boolean {
|
|
381
|
+
return this._layers.has(layer);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private _createPlaceHolder(height: number = Constants.PLACEHOLDER_HEIGHT): void {
|
|
339
385
|
this._placeholderBitmap?.close();
|
|
340
386
|
this._placeholderBitmap = undefined;
|
|
341
387
|
|
|
@@ -359,7 +405,7 @@ export class ImageRenderer extends Disposable implements IDisposable {
|
|
|
359
405
|
ctx.putImageData(imgData, 0, 0);
|
|
360
406
|
|
|
361
407
|
// create placeholder line, width aligned to blueprint width
|
|
362
|
-
const width = (screen.width + bWidth - 1) & ~(bWidth - 1) || PLACEHOLDER_LENGTH;
|
|
408
|
+
const width = (screen.width + bWidth - 1) & ~(bWidth - 1) || Constants.PLACEHOLDER_LENGTH;
|
|
363
409
|
this._placeholder = ImageRenderer.createCanvas(this.document, width, height);
|
|
364
410
|
const ctx2 = this._placeholder.getContext('2d', { alpha: false });
|
|
365
411
|
if (!ctx2) {
|
package/src/ImageStorage.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { IDisposable } from '@xterm/xterm';
|
|
7
7
|
import { ImageRenderer } from './ImageRenderer';
|
|
8
|
-
import { ITerminalExt, IExtendedAttrsImage, IImageAddonOptions, IImageSpec, IBufferLineExt, BgFlags, Cell, Content, ICellSize, ExtFlags, Attributes, UnderlineStyle } from './Types';
|
|
8
|
+
import { ITerminalExt, IExtendedAttrsImage, IImageAddonOptions, IImageSpec, IBufferLineExt, BgFlags, Cell, Content, ICellSize, ExtFlags, Attributes, UnderlineStyle, ImageLayer } from './Types';
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
// fallback default cell size
|
|
@@ -124,6 +124,8 @@ export class ImageStorage implements IDisposable {
|
|
|
124
124
|
private _pixelLimit: number = 2500000;
|
|
125
125
|
|
|
126
126
|
private _viewportMetrics: { cols: number, rows: number };
|
|
127
|
+
public onImageAdded: (() => void) | undefined;
|
|
128
|
+
public onImageDeleted: ((storageId: number) => void) | undefined;
|
|
127
129
|
|
|
128
130
|
constructor(
|
|
129
131
|
private _terminal: ITerminalExt,
|
|
@@ -132,8 +134,10 @@ export class ImageStorage implements IDisposable {
|
|
|
132
134
|
) {
|
|
133
135
|
try {
|
|
134
136
|
this.setLimit(this._opts.storageLimit);
|
|
135
|
-
} catch (e:
|
|
136
|
-
|
|
137
|
+
} catch (e: unknown) {
|
|
138
|
+
if (e instanceof Error) {
|
|
139
|
+
console.error(e.message);
|
|
140
|
+
}
|
|
137
141
|
console.warn(`storageLimit is set to ${this.getLimit()} MB`);
|
|
138
142
|
}
|
|
139
143
|
this._viewportMetrics = {
|
|
@@ -187,11 +191,13 @@ export class ImageStorage implements IDisposable {
|
|
|
187
191
|
|
|
188
192
|
private _delImg(id: number): void {
|
|
189
193
|
const spec = this._images.get(id);
|
|
194
|
+
if (!spec) return;
|
|
190
195
|
this._images.delete(id);
|
|
191
196
|
// FIXME: really ugly workaround to get bitmaps deallocated :(
|
|
192
|
-
if (
|
|
197
|
+
if (window.ImageBitmap && spec.orig instanceof ImageBitmap) {
|
|
193
198
|
spec.orig.close();
|
|
194
199
|
}
|
|
200
|
+
this.onImageDeleted?.(id);
|
|
195
201
|
}
|
|
196
202
|
|
|
197
203
|
/**
|
|
@@ -215,27 +221,27 @@ export class ImageStorage implements IDisposable {
|
|
|
215
221
|
}
|
|
216
222
|
|
|
217
223
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* Partially fixes https://github.com/jerch/xterm-addon-image/issues/37.
|
|
224
|
+
* Delete an image by its internal storage ID.
|
|
225
|
+
* Used by protocols that support explicit deletion (e.g. Kitty a=d).
|
|
221
226
|
*/
|
|
222
|
-
public
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
const rows = Math.ceil(height / cellSize.height);
|
|
229
|
-
for (let i = 1; i < rows; ++i) {
|
|
230
|
-
this._terminal._core._inputHandler.lineFeed();
|
|
231
|
-
}
|
|
227
|
+
public deleteImage(id: number): void {
|
|
228
|
+
const spec = this._images.get(id);
|
|
229
|
+
if (spec) {
|
|
230
|
+
spec.marker?.dispose();
|
|
231
|
+
this._delImg(id);
|
|
232
232
|
}
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
/**
|
|
236
236
|
* Method to add an image to the storage.
|
|
237
|
+
* @param img - The image to add (canvas or bitmap).
|
|
238
|
+
* @param scrolling - When true, cursor advances with the image (lineFeed per row).
|
|
239
|
+
* When false, image is placed at (0,0) and cursor is restored (DECSET 80 / sixel origin mode).
|
|
240
|
+
* @param layer - Which canvas layer to render on ('top' or 'bottom').
|
|
241
|
+
* @param zIndex - Z-index for image layering within the same layer.
|
|
242
|
+
* @returns The internal image ID assigned to the stored image.
|
|
237
243
|
*/
|
|
238
|
-
public addImage(img: HTMLCanvasElement | ImageBitmap):
|
|
244
|
+
public addImage(img: HTMLCanvasElement | ImageBitmap, scrolling: boolean, layer: ImageLayer = 'top', zIndex: number = 0): number {
|
|
239
245
|
// never allow storage to exceed memory limit
|
|
240
246
|
this._evictOldest(img.width * img.height);
|
|
241
247
|
|
|
@@ -257,7 +263,7 @@ export class ImageStorage implements IDisposable {
|
|
|
257
263
|
let offset = originX;
|
|
258
264
|
let tileCount = 0;
|
|
259
265
|
|
|
260
|
-
if (!
|
|
266
|
+
if (!scrolling) {
|
|
261
267
|
buffer.x = 0;
|
|
262
268
|
buffer.y = 0;
|
|
263
269
|
offset = 0;
|
|
@@ -271,7 +277,7 @@ export class ImageStorage implements IDisposable {
|
|
|
271
277
|
this._writeToCell(line as IBufferLineExt, offset + col, imageId, row * cols + col);
|
|
272
278
|
tileCount++;
|
|
273
279
|
}
|
|
274
|
-
if (
|
|
280
|
+
if (scrolling) {
|
|
275
281
|
if (row < rows - 1) this._terminal._core._inputHandler.lineFeed();
|
|
276
282
|
} else {
|
|
277
283
|
if (++buffer.y >= termRows) break;
|
|
@@ -281,7 +287,7 @@ export class ImageStorage implements IDisposable {
|
|
|
281
287
|
this._terminal._core._inputHandler._dirtyRowTracker.markDirty(buffer.y);
|
|
282
288
|
|
|
283
289
|
// cursor positioning modes
|
|
284
|
-
if (
|
|
290
|
+
if (scrolling) {
|
|
285
291
|
buffer.x = offset;
|
|
286
292
|
} else {
|
|
287
293
|
buffer.x = originX;
|
|
@@ -324,11 +330,15 @@ export class ImageStorage implements IDisposable {
|
|
|
324
330
|
actualCellSize: { ...cellSize }, // clone needed, since later modified
|
|
325
331
|
marker: endMarker || undefined,
|
|
326
332
|
tileCount,
|
|
327
|
-
bufferType: this._terminal.buffer.active.type
|
|
333
|
+
bufferType: this._terminal.buffer.active.type,
|
|
334
|
+
layer,
|
|
335
|
+
zIndex
|
|
328
336
|
};
|
|
329
337
|
|
|
330
338
|
// finally add the image
|
|
331
339
|
this._images.set(imageId, imgSpec);
|
|
340
|
+
this.onImageAdded?.();
|
|
341
|
+
return imageId;
|
|
332
342
|
}
|
|
333
343
|
|
|
334
344
|
|
|
@@ -338,16 +348,30 @@ export class ImageStorage implements IDisposable {
|
|
|
338
348
|
*/
|
|
339
349
|
// TODO: Should we move this to the ImageRenderer?
|
|
340
350
|
public render(range: { start: number, end: number }): void {
|
|
341
|
-
//
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (
|
|
346
|
-
|
|
351
|
+
// Determine which layers have images
|
|
352
|
+
let hasTopImages = false;
|
|
353
|
+
let hasBottomImages = false;
|
|
354
|
+
for (const spec of this._images.values()) {
|
|
355
|
+
if (spec.layer === 'bottom') {
|
|
356
|
+
hasBottomImages = true;
|
|
357
|
+
} else {
|
|
358
|
+
hasTopImages = true;
|
|
347
359
|
}
|
|
360
|
+
if (hasTopImages && hasBottomImages) break;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Lazily insert layers that are needed
|
|
364
|
+
if (hasTopImages && !this._renderer.hasLayer('top')) {
|
|
365
|
+
this._renderer.insertLayerToDom('top');
|
|
366
|
+
if (!this._renderer.hasLayer('top')) return;
|
|
367
|
+
}
|
|
368
|
+
if (hasBottomImages && !this._renderer.hasLayer('bottom')) {
|
|
369
|
+
this._renderer.insertLayerToDom('bottom');
|
|
348
370
|
}
|
|
371
|
+
|
|
349
372
|
// rescale if needed
|
|
350
373
|
this._renderer.rescaleCanvas();
|
|
374
|
+
|
|
351
375
|
// exit early if we dont have any images to test for
|
|
352
376
|
if (!this._images.size) {
|
|
353
377
|
if (!this._fullyCleared) {
|
|
@@ -355,12 +379,25 @@ export class ImageStorage implements IDisposable {
|
|
|
355
379
|
this._fullyCleared = true;
|
|
356
380
|
this._needsFullClear = false;
|
|
357
381
|
}
|
|
358
|
-
if (this._renderer.
|
|
359
|
-
this._renderer.removeLayerFromDom();
|
|
382
|
+
if (this._renderer.hasLayer('top')) {
|
|
383
|
+
this._renderer.removeLayerFromDom('top');
|
|
384
|
+
}
|
|
385
|
+
if (this._renderer.hasLayer('bottom')) {
|
|
386
|
+
this._renderer.removeLayerFromDom('bottom');
|
|
360
387
|
}
|
|
361
388
|
return;
|
|
362
389
|
}
|
|
363
390
|
|
|
391
|
+
// Remove layers no longer needed
|
|
392
|
+
if (!hasTopImages && this._renderer.hasLayer('top')) {
|
|
393
|
+
this._renderer.clearAll('top');
|
|
394
|
+
this._renderer.removeLayerFromDom('top');
|
|
395
|
+
}
|
|
396
|
+
if (!hasBottomImages && this._renderer.hasLayer('bottom')) {
|
|
397
|
+
this._renderer.clearAll('bottom');
|
|
398
|
+
this._renderer.removeLayerFromDom('bottom');
|
|
399
|
+
}
|
|
400
|
+
|
|
364
401
|
// buffer switches force a full clear
|
|
365
402
|
if (this._needsFullClear) {
|
|
366
403
|
this._renderer.clearAll();
|
|
@@ -375,50 +412,77 @@ export class ImageStorage implements IDisposable {
|
|
|
375
412
|
// clear drawing area
|
|
376
413
|
this._renderer.clearLines(start, end);
|
|
377
414
|
|
|
378
|
-
//
|
|
415
|
+
// Collect draw calls so we can sort by z-index (lower z drawn first).
|
|
416
|
+
const drawCalls: { imgSpec: IImageSpec, tileId: number, col: number, row: number, count: number }[] = [];
|
|
417
|
+
const placeholderCalls: { col: number, row: number, count: number }[] = [];
|
|
418
|
+
|
|
419
|
+
// walk all cells in viewport and collect tiles found
|
|
420
|
+
// Note: We check _extendedAttrs directly (not just HAS_EXTENDED flag)
|
|
421
|
+
// because text writes clear the BG flag but leave image tile data intact.
|
|
422
|
+
// This lets top-layer images survive text overwrites (kitty C=1 behavior).
|
|
379
423
|
for (let row = start; row <= end; ++row) {
|
|
380
424
|
const line = buffer.lines.get(row + buffer.ydisp) as IBufferLineExt;
|
|
381
425
|
if (!line) return;
|
|
382
426
|
for (let col = 0; col < cols; ++col) {
|
|
427
|
+
let e: IExtendedAttrsImage;
|
|
383
428
|
if (line.getBg(col) & BgFlags.HAS_EXTENDED) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
429
|
+
e = line._extendedAttrs[col] ?? EMPTY_ATTRS;
|
|
430
|
+
} else {
|
|
431
|
+
const maybeImg = line._extendedAttrs[col] as IExtendedAttrsImage | undefined;
|
|
432
|
+
if (!maybeImg || maybeImg.imageId === undefined || maybeImg.imageId === -1) {
|
|
387
433
|
continue;
|
|
388
434
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
435
|
+
e = maybeImg;
|
|
436
|
+
}
|
|
437
|
+
const imageId = e.imageId;
|
|
438
|
+
if (imageId === undefined || imageId === -1) {
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
const imgSpec = this._images.get(imageId);
|
|
442
|
+
if (e.tileId !== -1) {
|
|
443
|
+
const startTile = e.tileId;
|
|
444
|
+
const startCol = col;
|
|
445
|
+
let count = 1;
|
|
446
|
+
/**
|
|
447
|
+
* merge tiles to the right into a single draw call, if:
|
|
448
|
+
* - not at end of line
|
|
449
|
+
* - cell has same image id
|
|
450
|
+
* - cell has consecutive tile id
|
|
451
|
+
* Also check _extendedAttrs directly for cells where text cleared HAS_EXTENDED.
|
|
452
|
+
*/
|
|
453
|
+
while (++col < cols) {
|
|
454
|
+
const nextE = line._extendedAttrs[col] as IExtendedAttrsImage | undefined;
|
|
455
|
+
if (!nextE || nextE.imageId !== imageId || nextE.tileId !== startTile + count) {
|
|
456
|
+
break;
|
|
408
457
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
458
|
+
e = nextE;
|
|
459
|
+
count++;
|
|
460
|
+
}
|
|
461
|
+
col--;
|
|
462
|
+
if (imgSpec) {
|
|
463
|
+
if (imgSpec.actual) {
|
|
464
|
+
drawCalls.push({ imgSpec, tileId: startTile, col: startCol, row, count });
|
|
416
465
|
}
|
|
417
|
-
|
|
466
|
+
} else if (this._opts.showPlaceholder) {
|
|
467
|
+
placeholderCalls.push({ col: startCol, row, count });
|
|
418
468
|
}
|
|
469
|
+
this._fullyCleared = false;
|
|
419
470
|
}
|
|
420
471
|
}
|
|
421
472
|
}
|
|
473
|
+
|
|
474
|
+
// Sort by z-index so lower z draws first (higher z renders on top)
|
|
475
|
+
drawCalls.sort((a, b) => a.imgSpec.zIndex - b.imgSpec.zIndex);
|
|
476
|
+
|
|
477
|
+
// Draw placeholders first (lowest priority)
|
|
478
|
+
for (const call of placeholderCalls) {
|
|
479
|
+
this._renderer.drawPlaceholder(call.col, call.row, call.count);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Draw images in z-index order
|
|
483
|
+
for (const call of drawCalls) {
|
|
484
|
+
this._renderer.draw(call.imgSpec, call.tileId, call.col, call.row, call.count);
|
|
485
|
+
}
|
|
422
486
|
}
|
|
423
487
|
|
|
424
488
|
public viewportResize(metrics: { cols: number, rows: number }): void {
|
|
@@ -442,7 +506,7 @@ export class ImageStorage implements IDisposable {
|
|
|
442
506
|
for (let row = 0; row < rows; ++row) {
|
|
443
507
|
const line = buffer.lines.get(row) as IBufferLineExt;
|
|
444
508
|
if (line.getBg(oldCol) & BgFlags.HAS_EXTENDED) {
|
|
445
|
-
const e: IExtendedAttrsImage = line._extendedAttrs[oldCol]
|
|
509
|
+
const e: IExtendedAttrsImage = line._extendedAttrs[oldCol] ?? EMPTY_ATTRS;
|
|
446
510
|
const imageId = e.imageId;
|
|
447
511
|
if (imageId === undefined || imageId === -1) {
|
|
448
512
|
continue;
|
|
@@ -487,7 +551,7 @@ export class ImageStorage implements IDisposable {
|
|
|
487
551
|
const buffer = this._terminal._core.buffer;
|
|
488
552
|
const line = buffer.lines.get(y) as IBufferLineExt;
|
|
489
553
|
if (line && line.getBg(x) & BgFlags.HAS_EXTENDED) {
|
|
490
|
-
const e: IExtendedAttrsImage = line._extendedAttrs[x]
|
|
554
|
+
const e: IExtendedAttrsImage = line._extendedAttrs[x] ?? EMPTY_ATTRS;
|
|
491
555
|
if (e.imageId && e.imageId !== -1) {
|
|
492
556
|
const orig = this._images.get(e.imageId)?.orig;
|
|
493
557
|
if (window.ImageBitmap && orig instanceof ImageBitmap) {
|
|
@@ -507,7 +571,7 @@ export class ImageStorage implements IDisposable {
|
|
|
507
571
|
const buffer = this._terminal._core.buffer;
|
|
508
572
|
const line = buffer.lines.get(y) as IBufferLineExt;
|
|
509
573
|
if (line && line.getBg(x) & BgFlags.HAS_EXTENDED) {
|
|
510
|
-
const e: IExtendedAttrsImage = line._extendedAttrs[x]
|
|
574
|
+
const e: IExtendedAttrsImage = line._extendedAttrs[x] ?? EMPTY_ATTRS;
|
|
511
575
|
if (e.imageId && e.imageId !== -1 && e.tileId !== -1) {
|
|
512
576
|
const spec = this._images.get(e.imageId);
|
|
513
577
|
if (spec) {
|
package/src/SixelHandler.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { SixelImageStorage } from './SixelImageStorage';
|
|
7
7
|
import { IDcsHandler, IParams, IImageAddonOptions, ITerminalExt, AttributeData, IResetHandler, ReadonlyColorSet } from './Types';
|
|
8
8
|
import { toRGBA8888, BIG_ENDIAN, PALETTE_ANSI_256, PALETTE_VT340_COLOR } from 'sixel/lib/Colors';
|
|
9
9
|
import { RGBA8888 } from 'sixel/lib/Types';
|
|
@@ -26,7 +26,7 @@ export class SixelHandler implements IDcsHandler, IResetHandler {
|
|
|
26
26
|
|
|
27
27
|
constructor(
|
|
28
28
|
private readonly _opts: IImageAddonOptions,
|
|
29
|
-
private readonly _storage:
|
|
29
|
+
private readonly _storage: SixelImageStorage,
|
|
30
30
|
private readonly _coreTerminal: ITerminalExt
|
|
31
31
|
) {
|
|
32
32
|
DecoderAsync({
|
|
@@ -91,7 +91,7 @@ export class SixelHandler implements IDcsHandler, IResetHandler {
|
|
|
91
91
|
const height = this._dec.height;
|
|
92
92
|
|
|
93
93
|
// partial fix for https://github.com/jerch/xterm-addon-image/issues/37
|
|
94
|
-
if (!width || !
|
|
94
|
+
if (!width || !height) {
|
|
95
95
|
if (height) {
|
|
96
96
|
this._storage.advanceCursor(height);
|
|
97
97
|
}
|
|
@@ -99,7 +99,7 @@ export class SixelHandler implements IDcsHandler, IResetHandler {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const canvas = ImageRenderer.createCanvas(undefined, width, height);
|
|
102
|
-
canvas.getContext('2d')?.putImageData(new ImageData(this._dec.data8
|
|
102
|
+
canvas.getContext('2d')?.putImageData(new ImageData(this._dec.data8 as Uint8ClampedArray<ArrayBuffer>, width, height), 0, 0);
|
|
103
103
|
if (this._dec.memoryUsage > MEM_PERMA_LIMIT) {
|
|
104
104
|
this._dec.release();
|
|
105
105
|
}
|