018d37f103
Emulator (vendored xterm): - Widths follow glibc wcwidth on generated Unicode 16 tables (tool/gen_unicode_tables.py); combining marks, variation selectors, ZWJ sequences, skin tones, flags and Hangul jamo join the previous cell, stored as interned clusters (CellContent.clusterFlag) and drawn and copied whole. U+FE0F and flags take two columns, as modern terminals do; a wide character never starts in the last column. - Box drawing, block elements and Powerline separators are drawn as geometry filling the cell; rows are whole pixels high. - Backgrounds merge per colour; each line is a picture reused until the line changes (BufferLine.version); a line drawn unchanged settles into one laid out paragraph with a span per style. TerminalStyle and TerminalTheme compare by value. App: bundled Symbols Nerd Font Mono subset (568 KiB, MIT) as the terminal's first fallback, loaded on first use; line height 1.1; ?terminalUnicode=1 fixture showcase; bench gains a progress workload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
312 lines
12 KiB
Dart
312 lines
12 KiB
Dart
import 'package:flutter/painting.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:xterm/src/ui/painter.dart';
|
|
import 'package:xterm/src/ui/row_pictures.dart';
|
|
import 'package:xterm/xterm.dart';
|
|
|
|
import 'paint_fixture.dart';
|
|
|
|
/// Box drawing, block and Powerline characters are geometry that fills the
|
|
/// cell, so they join across cells and rows; text runs land on the grid.
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
final painter = fixturePainter();
|
|
final w = painter.cellSize.width.round();
|
|
final h = painter.cellSize.height.round();
|
|
|
|
/// A line of [cols] cells written with [text] by a terminal.
|
|
BufferLine lineOf(String text, {int cols = 6}) {
|
|
final terminal = Terminal(maxLines: 100)..resize(cols, 5);
|
|
terminal.write(text);
|
|
return terminal.buffer.lines[0];
|
|
}
|
|
|
|
/// [lines] painted one under the other.
|
|
Future<Pixels> paintLines(List<BufferLine> lines, {int cols = 6}) =>
|
|
Pixels.paint(cols * w, lines.length * h, (canvas) {
|
|
for (var i = 0; i < lines.length; i++) {
|
|
painter.paintLine(canvas, Offset(0, i * h.toDouble()), lines[i]);
|
|
}
|
|
});
|
|
|
|
Rect cellRect(int x, int y) => Rect.fromLTWH(
|
|
x * w.toDouble(),
|
|
y * h.toDouble(),
|
|
w.toDouble(),
|
|
h.toDouble(),
|
|
);
|
|
|
|
test('cells are whole pixels high', () {
|
|
final odd = TerminalPainter(
|
|
theme: fixtureTheme,
|
|
textStyle: const TerminalStyle(fontSize: 13, height: 1.15),
|
|
textScaler: TextScaler.noScaling,
|
|
);
|
|
final height = odd.cellSize.height;
|
|
expect(height, height.roundToDouble());
|
|
});
|
|
|
|
test('a full block fills its cell exactly', () async {
|
|
final pixels = await paintLines([lineOf('\u2588')]);
|
|
expect(pixels.count(foregroundColor, cellRect(0, 0)), w * h);
|
|
expect(pixels.count(foregroundColor, cellRect(1, 0)), 0);
|
|
});
|
|
|
|
test('half and eighth blocks fill their part of the cell', () async {
|
|
final pixels = await paintLines([lineOf('\u2580\u2584\u258C\u2581')]);
|
|
expect(pixels.count(foregroundColor, cellRect(0, 0)), w * h ~/ 2);
|
|
expect(pixels.at(w ~/ 2, 0), foregroundColor); // upper half
|
|
expect(pixels.at(w + w ~/ 2, h - 1), foregroundColor); // lower half
|
|
expect(pixels.at(w + w ~/ 2, 0).a, 0);
|
|
expect(pixels.at(2 * w, h ~/ 2), foregroundColor); // left half
|
|
expect(pixels.at(3 * w - 1, h ~/ 2).a, 0);
|
|
// The lower eighth: whole pixel rows, about an eighth of the height.
|
|
final rows = [
|
|
for (var y = 0; y < h; y++)
|
|
if (pixels.count(
|
|
foregroundColor,
|
|
Rect.fromLTWH(3.0 * w, y.toDouble(), w.toDouble(), 1),
|
|
) ==
|
|
w)
|
|
y,
|
|
];
|
|
expect(rows.length, closeTo(h / 8, 1));
|
|
expect(rows.last, h - 1);
|
|
for (var y = 0; y < h; y++) {
|
|
if (!rows.contains(y)) expect(pixels.at(3 * w + w ~/ 2, y).a, 0);
|
|
}
|
|
});
|
|
|
|
test('a horizontal line runs unbroken across cells', () async {
|
|
final pixels = await paintLines([lineOf('\u2500\u2500\u2500')]);
|
|
final row = [
|
|
for (var y = 0; y < h; y++)
|
|
if (pixels.at(1, y) == foregroundColor) y,
|
|
];
|
|
expect(row, isNotEmpty);
|
|
for (var x = 0; x < 3 * w; x++) {
|
|
expect(pixels.at(x, row.first), foregroundColor, reason: 'x=$x');
|
|
}
|
|
});
|
|
|
|
test('a vertical line runs unbroken across rows', () async {
|
|
final pixels = await paintLines([lineOf('\u2502'), lineOf('\u2502')]);
|
|
final column = [
|
|
for (var x = 0; x < w; x++)
|
|
if (pixels.at(x, 1) == foregroundColor) x,
|
|
];
|
|
expect(column, isNotEmpty);
|
|
for (var y = 0; y < 2 * h; y++) {
|
|
expect(pixels.at(column.first, y), foregroundColor, reason: 'y=$y');
|
|
}
|
|
});
|
|
|
|
test('corners and tees join the lines they meet', () async {
|
|
// \u250C\u2500\u2510 over \u2502 \u2502: the corner's arms reach the cell edges.
|
|
final pixels = await paintLines([
|
|
lineOf('\u250C\u2500\u2510'),
|
|
lineOf('\u2502 \u2502'),
|
|
]);
|
|
final lineY = [
|
|
for (var y = 0; y < h; y++)
|
|
if (pixels.at(w + 1, y) == foregroundColor) y,
|
|
].first;
|
|
final lineX = [
|
|
for (var x = 0; x < w; x++)
|
|
if (pixels.at(x, h + 1) == foregroundColor) x,
|
|
].first;
|
|
// The corner's right arm reaches its right edge, the down arm its bottom.
|
|
expect(pixels.at(w - 1, lineY), foregroundColor);
|
|
expect(pixels.at(lineX, h - 1), foregroundColor);
|
|
// Nothing left of or above the corner's elbow.
|
|
expect(pixels.at(0, lineY).a, 0);
|
|
expect(pixels.at(lineX, 0).a, 0);
|
|
});
|
|
|
|
test('double lines draw two strokes', () async {
|
|
final pixels = await paintLines([lineOf('\u2550')]);
|
|
final rows = [
|
|
for (var y = 0; y < h; y++)
|
|
if (pixels.at(w ~/ 2, y) == foregroundColor) y,
|
|
];
|
|
// Two separate strokes: a gap between the first and last painted rows.
|
|
expect(rows.length, lessThan(rows.last - rows.first + 1));
|
|
for (var x = 0; x < w; x++) {
|
|
expect(pixels.at(x, rows.first), foregroundColor);
|
|
expect(pixels.at(x, rows.last), foregroundColor);
|
|
}
|
|
});
|
|
|
|
test('a Powerline arrow fills the cell height at its base', () async {
|
|
final pixels = await paintLines([lineOf('\uE0B0\uE0B2')]);
|
|
// Right-pointing: the whole left column; left-pointing: the right (the
|
|
// top and bottom pixels are the antialiased points of the triangle).
|
|
for (var y = 1; y < h - 1; y++) {
|
|
expect(pixels.at(0, y), foregroundColor, reason: 'y=$y');
|
|
expect(pixels.at(2 * w - 1, y), foregroundColor, reason: 'y=$y');
|
|
}
|
|
expect(pixels.at(0, 0).a, greaterThan(0.4));
|
|
expect(pixels.at(0, h - 1).a, greaterThan(0.4));
|
|
// The tip reaches the far side only in the middle.
|
|
expect(pixels.at(w - 1, h ~/ 2).a, greaterThan(0));
|
|
expect(pixels.at(w - 1, 0).a, 0);
|
|
expect(pixels.at(w, 0).a, 0);
|
|
// About half the cell.
|
|
final filled = pixels.count(foregroundColor, cellRect(0, 0));
|
|
expect(filled, closeTo(w * h / 2, w * h * 0.1));
|
|
});
|
|
|
|
test('Powerline diagonals fill a triangle', () async {
|
|
final pixels = await paintLines([lineOf('\uE0B8\uE0BC')]);
|
|
expect(pixels.at(0, h - 1), foregroundColor); // lower left
|
|
expect(pixels.at(w - 2, 0).a, 0);
|
|
expect(pixels.at(w, 0), foregroundColor); // upper left
|
|
expect(pixels.at(2 * w - 1, h - 1).a, 0);
|
|
});
|
|
|
|
/// [line] drawn cell by cell with [TerminalPainter.paintCell].
|
|
Future<Pixels> paintCells(BufferLine line, int cols) =>
|
|
Pixels.paint(cols * w, h, (canvas) {
|
|
final cell = CellData.empty();
|
|
for (var x = 0; x < cols; x++) {
|
|
line.getCellData(x, cell);
|
|
painter.paintCell(canvas, Offset(x * w.toDouble(), 0), cell);
|
|
}
|
|
});
|
|
|
|
Future<void> expectSamePixels(Pixels a, Pixels b) async {
|
|
for (var y = 0; y < a.height; y++) {
|
|
for (var x = 0; x < a.width; x++) {
|
|
expect(a.at(x, y), b.at(x, y), reason: '($x, $y)');
|
|
}
|
|
}
|
|
}
|
|
|
|
test('a line of text paints exactly as cell by cell', () async {
|
|
// Long enough to be one paragraph, with spans of three styles.
|
|
const cols = 24;
|
|
final line = lineOf(
|
|
'plain \x1b[31mred\x1b[0m and \x1b[1;34mbold\x1b[0m text!',
|
|
cols: cols,
|
|
);
|
|
await expectSamePixels(
|
|
await paintLines([line], cols: cols),
|
|
await paintCells(line, cols),
|
|
);
|
|
});
|
|
|
|
test('a short line is drawn cell by cell, the same', () async {
|
|
final line = lineOf('Ab c-d');
|
|
await expectSamePixels(await paintLines([line]), await paintCells(line, 6));
|
|
});
|
|
|
|
test('text after a wide character and an emoji stays on the grid', () async {
|
|
const cols = 24;
|
|
final line = lineOf('\u4E2D\u{1F600}x text after them', cols: cols);
|
|
final pixels = await paintLines([line], cols: cols);
|
|
// Each plain character's cell is inked, the spaces are not.
|
|
for (var x = 4; x < 4 + 'x text after them'.length; x++) {
|
|
final blank = ' x text after them'[x - 3] == ' ';
|
|
final ink = pixels.count(foregroundColor, cellRect(x, 0));
|
|
expect(ink > 0, !blank, reason: 'cell $x');
|
|
}
|
|
});
|
|
|
|
test('an underline does not run under undecorated blanks', () async {
|
|
const cols = 24;
|
|
final line = lineOf(
|
|
'\x1b[4munderline\x1b[24m \x1b[4mmore text\x1b[0m',
|
|
cols: cols,
|
|
);
|
|
final pixels = await paintLines([line], cols: cols);
|
|
for (var x = 9; x < 12; x++) {
|
|
expect(pixels.count(foregroundColor, cellRect(x, 0)), 0, reason: 'x=$x');
|
|
}
|
|
await expectSamePixels(pixels, await paintCells(line, cols));
|
|
});
|
|
|
|
test('a styled run breaks where the colour changes', () async {
|
|
final pixels = await paintLines([lineOf('a\x1b[31mb\x1b[0mc')]);
|
|
expect(pixels.count(foregroundColor, cellRect(0, 0)), greaterThan(0));
|
|
expect(pixels.count(foregroundColor, cellRect(1, 0)), 0);
|
|
expect(pixels.count(fixtureTheme.red, cellRect(1, 0)), greaterThan(0));
|
|
expect(pixels.count(foregroundColor, cellRect(2, 0)), greaterThan(0));
|
|
});
|
|
|
|
test('backgrounds merge into runs that meet without a gap', () async {
|
|
final pixels = await paintLines([lineOf('\x1b[44m \x1b[0m')]);
|
|
for (var x = 0; x < 3 * w; x++) {
|
|
expect(pixels.at(x, h ~/ 2), fixtureTheme.blue, reason: 'x=$x');
|
|
}
|
|
expect(pixels.at(3 * w + 1, h ~/ 2).a, 0);
|
|
});
|
|
|
|
test('a grapheme cluster is drawn in its own cell', () async {
|
|
final pixels = await paintLines([lineOf('e\u0301\u{1F1F5}\u{1F1F9}x')]);
|
|
expect(pixels.count(foregroundColor, cellRect(0, 0)), greaterThan(0));
|
|
expect(pixels.count(foregroundColor, cellRect(3, 0)), greaterThan(0));
|
|
});
|
|
|
|
group('row pictures', () {
|
|
test('a line is recorded again only when it changes', () {
|
|
final cache = RowPictureCache(settleAfter: 100);
|
|
final line = lineOf('abc');
|
|
var recorded = 0;
|
|
void paint(Canvas canvas, bool settled) => recorded++;
|
|
final first = cache.pictureOf(line, 0, paint);
|
|
expect(cache.pictureOf(line, 0, paint), same(first));
|
|
expect(recorded, 1);
|
|
line.setCodePoint(0, 0x7A);
|
|
expect(cache.pictureOf(line, 0, paint), isNot(same(first)));
|
|
expect(recorded, 2);
|
|
// A new painter generation (theme, font) records every line again.
|
|
cache.pictureOf(line, 1, paint);
|
|
expect(recorded, 3);
|
|
cache.clear();
|
|
});
|
|
|
|
test('a line drawn unchanged settles into laid out text, within the '
|
|
'frame budget', () {
|
|
final cache = RowPictureCache(settleAfter: 2, settlePerFrame: 1);
|
|
final lines = [lineOf('one'), lineOf('two')];
|
|
final settledCalls = <bool>[];
|
|
void paint(Canvas canvas, bool settled) => settledCalls.add(settled);
|
|
for (var frame = 0; frame < 4; frame++) {
|
|
cache.beginFrame();
|
|
for (final line in lines) {
|
|
cache.pictureOf(line, 0, paint);
|
|
}
|
|
}
|
|
// Recorded new (not settled), then each settled in its own frame.
|
|
expect(settledCalls, [false, false, true, true]);
|
|
expect(cache.isSettled(lines[0]), isTrue);
|
|
expect(cache.isSettled(lines[1]), isTrue);
|
|
// A change starts the line over.
|
|
lines[0].setCodePoint(0, 0x7A);
|
|
cache.beginFrame();
|
|
cache.pictureOf(lines[0], 0, paint);
|
|
expect(settledCalls.last, isFalse);
|
|
expect(cache.isSettled(lines[0]), isFalse);
|
|
cache.clear();
|
|
});
|
|
|
|
test('trim keeps the most recently used lines', () {
|
|
final cache = RowPictureCache();
|
|
final lines = [for (var i = 0; i < 4; i++) lineOf('$i')];
|
|
for (final line in lines) {
|
|
cache.pictureOf(line, 0, (_, _) {});
|
|
}
|
|
cache.pictureOf(lines[0], 0, (_, _) {});
|
|
cache.trim(2);
|
|
expect(cache.length, 2);
|
|
var recorded = 0;
|
|
cache.pictureOf(lines[0], 0, (_, _) => recorded++);
|
|
expect(recorded, 0);
|
|
cache.pictureOf(lines[1], 0, (_, _) => recorded++);
|
|
expect(recorded, 1);
|
|
cache.clear();
|
|
});
|
|
});
|
|
}
|