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>
148 lines
4.8 KiB
Dart
148 lines
4.8 KiB
Dart
import 'dart:ui';
|
|
|
|
import 'package:flutter/painting.dart';
|
|
import 'package:quiver/collection.dart';
|
|
|
|
/// The text of one line's plain cells (see [isRunCharacter]) as a single
|
|
/// paragraph with a style span per run of same-styled cells, so a line is
|
|
/// laid out and drawn once rather than cell by cell. Columns drawn some other
|
|
/// way (wide characters, clusters, symbols, empty cells) are spaces, which
|
|
/// keep the following text on the grid since every glyph of the terminal
|
|
/// font advances one cell.
|
|
class RowText {
|
|
final _text = StringBuffer();
|
|
final _starts = <int>[];
|
|
final _keys = <int>[];
|
|
final _styles = <TextStyle Function()>[];
|
|
final _decorated = <bool>[];
|
|
var _length = 0;
|
|
var _ink = 0;
|
|
|
|
/// Characters with ink added since [clear].
|
|
int get ink => _ink;
|
|
|
|
void clear() {
|
|
_text.clear();
|
|
_starts.clear();
|
|
_keys.clear();
|
|
_styles.clear();
|
|
_decorated.clear();
|
|
_length = 0;
|
|
_ink = 0;
|
|
}
|
|
|
|
/// Whether a character in the style [key] continues the current span.
|
|
bool continues(int key) => _keys.isNotEmpty && _keys.last == key;
|
|
|
|
/// Adds one cell's character [code] in the style [key] identifies:
|
|
/// [style] builds it, and is needed only when it starts a span (see
|
|
/// [continues]); [decorated] when it draws lines.
|
|
void add(
|
|
int code,
|
|
int key,
|
|
TextStyle Function()? style, {
|
|
required bool decorated,
|
|
}) {
|
|
if (!continues(key)) {
|
|
_starts.add(_text.length);
|
|
_keys.add(key);
|
|
_styles.add(style!);
|
|
_decorated.add(decorated);
|
|
}
|
|
_text.writeCharCode(code);
|
|
_length = _text.length;
|
|
if (code != 0x20 && code != 0xA0) _ink++;
|
|
}
|
|
|
|
/// Adds [columns] columns without ink. Inside a decorated span they get
|
|
/// a plain span, so the decoration does not run under them.
|
|
void skip(int columns, TextStyle Function() plain) {
|
|
if (_keys.isEmpty || _decorated.last) {
|
|
_starts.add(_text.length);
|
|
_keys.add(_plainKey);
|
|
_styles.add(plain);
|
|
_decorated.add(false);
|
|
}
|
|
for (var i = 0; i < columns; i++) {
|
|
_text.writeCharCode(0x20);
|
|
}
|
|
}
|
|
|
|
static const _plainKey = -1;
|
|
|
|
/// The laid out paragraph, from [cache] when the same text and spans were
|
|
/// laid out before; null when the font's glyphs do not fall on the grid
|
|
/// of [cellWidth] (a fallback font, a proportional family).
|
|
Paragraph? layout(TextRunCache cache, TextScaler scaler, double cellWidth) {
|
|
final text = _text.toString().substring(0, _length);
|
|
// Spans that start after the last character with ink are left out.
|
|
var spans = _starts.length;
|
|
while (spans > 0 && _starts[spans - 1] >= text.length) {
|
|
spans--;
|
|
}
|
|
final signature = StringBuffer();
|
|
for (var i = 0; i < spans; i++) {
|
|
signature
|
|
..write(_starts[i])
|
|
..write(':')
|
|
..write(_keys[i])
|
|
..write(',');
|
|
}
|
|
return cache.laidOut((text, signature.toString()), () {
|
|
final builder = ParagraphBuilder(_styles.first().getParagraphStyle());
|
|
for (var i = 0; i < spans; i++) {
|
|
final start = _starts[i];
|
|
final end = i + 1 < spans ? _starts[i + 1] : text.length;
|
|
builder
|
|
..pushStyle(_styles[i]().getTextStyle(textScaler: scaler))
|
|
..addText(text.substring(start, end))
|
|
..pop();
|
|
}
|
|
return builder.build()
|
|
..layout(const ParagraphConstraints(width: double.infinity));
|
|
}, text.length * cellWidth);
|
|
}
|
|
}
|
|
|
|
/// Laid out [RowText] paragraphs, most recently used kept, so lines that
|
|
/// repeat (a redrawn screen, `yes`) are laid out once.
|
|
class TextRunCache {
|
|
TextRunCache(int maximumSize)
|
|
: _cache = LruMap<(String, String), Paragraph?>(
|
|
maximumSize: maximumSize,
|
|
);
|
|
|
|
final LruMap<(String, String), Paragraph?> _cache;
|
|
|
|
/// The paragraph [build] makes for [key], or null when it is not
|
|
/// [expectedWidth] wide (to half a pixel).
|
|
Paragraph? laidOut(
|
|
(String, String) key,
|
|
Paragraph Function() build,
|
|
double expectedWidth,
|
|
) {
|
|
if (_cache.containsKey(key)) return _cache[key];
|
|
final paragraph = build();
|
|
final aligned = (paragraph.maxIntrinsicWidth - expectedWidth).abs() <= 0.5;
|
|
if (!aligned) paragraph.dispose();
|
|
final result = aligned ? paragraph : null;
|
|
_cache[key] = result;
|
|
return result;
|
|
}
|
|
|
|
void clear() {
|
|
_cache.clear();
|
|
}
|
|
|
|
int get length => _cache.length;
|
|
}
|
|
|
|
/// Whether [codePoint] may be drawn in a line's [RowText]: printable ASCII
|
|
/// and Latin-1 to Latin Extended-B, which the bundled monospace family draws
|
|
/// itself, one cell each. Everything else (symbols, other scripts, emoji) is
|
|
/// drawn in its own cell, where a fallback font's different advance cannot
|
|
/// shift the cells after it.
|
|
bool isRunCharacter(int codePoint) =>
|
|
(codePoint > 0x20 && codePoint < 0x7F) ||
|
|
(codePoint >= 0xA0 && codePoint < 0x250 && codePoint != 0xAD);
|