2026-02-22 10:14:37 +01:00
|
|
|
import type { RenderParams, FontStyle } from './types.js';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render the overlay content into the container element.
|
|
|
|
|
*
|
|
|
|
|
* Creates per-character `<span>` elements positioned on an exact grid
|
|
|
|
|
* matching xterm.js's canvas renderer. This avoids sub-pixel drift that
|
|
|
|
|
* occurs with normal DOM text flow.
|
|
|
|
|
*/
|
|
|
|
|
export function renderOverlay(container: HTMLDivElement, params: RenderParams): void {
|
2026-03-01 16:27:40 +01:00
|
|
|
const { lines, startCol, totalCols, cellW, cellH, charTop, charHeight, promptRow, font, showCursor, cursorColor } = params;
|
2026-02-22 10:14:37 +01:00
|
|
|
|
2026-03-01 16:27:40 +01:00
|
|
|
// Position container at prompt row.
|
2026-02-22 10:14:37 +01:00
|
|
|
container.style.left = '0px';
|
|
|
|
|
container.style.top = (promptRow * cellH) + 'px';
|
|
|
|
|
|
|
|
|
|
// Clear and rebuild (typically 1-3 line divs, negligible cost)
|
|
|
|
|
container.innerHTML = '';
|
|
|
|
|
const fullWidthPx = totalCols * cellW;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
|
const leftPx = i === 0 ? startCol * cellW : 0;
|
|
|
|
|
const widthPx = i === 0 ? (fullWidthPx - leftPx) : fullWidthPx;
|
|
|
|
|
const topPx = i * cellH;
|
2026-03-01 16:27:40 +01:00
|
|
|
const lineEl = makeLine(lines[i], leftPx, topPx, widthPx, cellH, cellW, charTop, charHeight, font);
|
2026-02-22 10:14:37 +01:00
|
|
|
container.appendChild(lineEl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Block cursor at end of last line
|
|
|
|
|
if (showCursor) {
|
|
|
|
|
const lastLine = lines[lines.length - 1];
|
|
|
|
|
const lastLineLeft = lines.length === 1 ? startCol : 0;
|
|
|
|
|
const cursorCol = lastLineLeft + lastLine.length;
|
|
|
|
|
if (cursorCol < totalCols) {
|
|
|
|
|
const cursor = document.createElement('span');
|
|
|
|
|
cursor.style.cssText = 'position:absolute;display:inline-block';
|
|
|
|
|
cursor.style.left = (cursorCol * cellW) + 'px';
|
|
|
|
|
cursor.style.top = ((lines.length - 1) * cellH) + 'px';
|
|
|
|
|
cursor.style.width = cellW + 'px';
|
|
|
|
|
cursor.style.height = cellH + 'px';
|
|
|
|
|
cursor.style.backgroundColor = cursorColor;
|
|
|
|
|
container.appendChild(cursor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container.style.display = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a styled line `<div>` with per-character grid positioning.
|
|
|
|
|
*
|
|
|
|
|
* Each character gets its own `<span>` placed at `i * cellW` pixels.
|
|
|
|
|
* This matches xterm's canvas renderer where each glyph occupies exactly
|
|
|
|
|
* one cell width, regardless of the actual glyph metrics.
|
|
|
|
|
*/
|
|
|
|
|
function makeLine(
|
|
|
|
|
text: string,
|
|
|
|
|
leftPx: number,
|
|
|
|
|
topPx: number,
|
|
|
|
|
widthPx: number,
|
|
|
|
|
cellH: number,
|
|
|
|
|
cellW: number,
|
2026-03-01 16:27:40 +01:00
|
|
|
charTop: number,
|
|
|
|
|
charHeight: number,
|
2026-02-22 10:14:37 +01:00
|
|
|
font: FontStyle,
|
|
|
|
|
): HTMLDivElement {
|
|
|
|
|
const el = document.createElement('div');
|
|
|
|
|
el.style.cssText = 'position:absolute;pointer-events:none';
|
|
|
|
|
el.style.backgroundColor = font.backgroundColor;
|
|
|
|
|
el.style.left = leftPx + 'px';
|
|
|
|
|
el.style.top = topPx + 'px';
|
|
|
|
|
el.style.width = widthPx + 'px';
|
2026-03-01 16:27:40 +01:00
|
|
|
// Extend background 1px past cell boundary to cover the compositing
|
|
|
|
|
// seam between the overlay layer (z-index:7) and the canvas layer below.
|
|
|
|
|
// The extra 1px lands in the next row's charTop gap (empty area before
|
|
|
|
|
// text rendering starts), so no canvas content is obscured.
|
2026-02-22 10:14:37 +01:00
|
|
|
el.style.height = (cellH + 1) + 'px';
|
2026-03-01 16:27:40 +01:00
|
|
|
|
|
|
|
|
// Spans fill the full cell height with matching lineHeight for natural
|
|
|
|
|
// CSS vertical centering. No transform — any sub-pixel overhang past
|
|
|
|
|
// the line div causes visible anti-aliasing artifacts at the boundary.
|
|
|
|
|
// The ≤0.5px difference from canvas ceil() rounding is imperceptible.
|
2026-02-22 10:14:37 +01:00
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
|
const span = document.createElement('span');
|
2026-03-01 16:27:40 +01:00
|
|
|
// No ligatures — canvas renders each glyph independently.
|
2026-02-22 10:14:37 +01:00
|
|
|
span.style.cssText =
|
|
|
|
|
'position:absolute;display:inline-block;text-align:center;pointer-events:none;' +
|
2026-03-01 16:27:40 +01:00
|
|
|
"font-feature-settings:'liga' 0,'calt' 0";
|
2026-02-22 10:14:37 +01:00
|
|
|
span.style.left = (i * cellW) + 'px';
|
2026-03-01 16:27:40 +01:00
|
|
|
span.style.top = '0px';
|
2026-02-22 10:14:37 +01:00
|
|
|
span.style.width = cellW + 'px';
|
2026-03-01 16:27:40 +01:00
|
|
|
span.style.height = cellH + 'px';
|
|
|
|
|
span.style.lineHeight = cellH + 'px';
|
2026-02-22 10:14:37 +01:00
|
|
|
span.style.fontFamily = font.fontFamily;
|
|
|
|
|
span.style.fontSize = font.fontSize;
|
|
|
|
|
span.style.fontWeight = font.fontWeight;
|
|
|
|
|
span.style.color = font.color;
|
|
|
|
|
if (font.letterSpacing) span.style.letterSpacing = font.letterSpacing;
|
|
|
|
|
span.textContent = text[i];
|
|
|
|
|
el.appendChild(span);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return el;
|
|
|
|
|
}
|