xterm: draw strikethrough, concealed text, overline and underline styles

SGR 9 (strikethrough) and 53 (overline) were stored but never drawn, and
SGR 8 (conceal) still showed the text. They are drawn now, and underline
styles use the matching line style: double (21, 4:2), curly (4:3, wavy),
dotted (4:4) and dashed (4:5). Concealed cells keep their background.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:38:35 +00:00
parent 822d77b1df
commit 2bcb62b5d8
5 changed files with 123 additions and 6 deletions
@@ -0,0 +1,76 @@
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'paint_fixture.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final size = fixturePainter().cellSize;
final w = size.width.ceil();
final h = size.height.ceil();
Future<Pixels> cell(CellData data) => Pixels.paint(
w,
h * 2, // room for decorations drawn below the cell
(canvas) => fixturePainter().paintCell(canvas, Offset.zero, data),
);
/// The rows of the cell that contain painted pixels.
Future<List<int>> inkRows(CellData data) async {
final pixels = await cell(data);
return [
for (var y = 0; y < h * 2; y++)
if (pixels.rowHasInk(y)) y,
];
}
test('a plain space draws nothing', () async {
expect(await inkRows(cellOf(' ')), isEmpty);
});
test('strikethrough (SGR 9) draws a line through the middle', () async {
final rows = await inkRows(cellOf(' ', flags: CellAttr.strikethrough));
expect(rows, isNotEmpty);
expect(rows.every((y) => y > h * 0.2 && y < h * 0.8), isTrue);
});
test('overline (SGR 53) draws at the top, underline at the bottom', () async {
final over = await inkRows(cellOf(' ', flags: CellAttr.overline));
expect(over, isNotEmpty);
expect(over.first, lessThan(h * 0.3));
final under = await inkRows(cellOf(' ', flags: CellAttr.underline));
expect(under, isNotEmpty);
expect(under.last, greaterThan(h * 0.6));
});
test('double underline (SGR 21) draws two lines', () async {
const doubled = CellAttr.underline | (2 << CellAttr.underlineStyleShift);
final single = await inkRows(cellOf(' ', flags: CellAttr.underline));
final two = await inkRows(cellOf(' ', flags: doubled));
expect(two.length, greaterThan(single.length));
});
test(
'concealed text (SGR 8) keeps its background, hides the glyph',
() async {
final shown = await cell(cellOf('M'));
expect(shown.count(foregroundColor), greaterThan(0));
final hidden = await cell(cellOf('M', flags: CellAttr.invisible));
expect(hidden.count(foregroundColor), 0);
final inverse = await cell(
cellOf('M', flags: CellAttr.invisible | CellAttr.inverse),
);
expect(
inverse.count(foregroundColor),
greaterThan(0),
reason: 'background',
);
expect(inverse.count(backgroundColor), 0, reason: 'no glyph');
},
);
}
@@ -75,6 +75,15 @@ class Pixels {
);
}
/// Whether row [y] has any painted (non-transparent) pixel; decorations
/// are antialiased, so their exact colour varies.
bool rowHasInk(int y) {
for (var x = 0; x < width; x++) {
if (_rgba.getUint8((y * width + x) * 4 + 3) != 0) return true;
}
return false;
}
/// How many pixels in [rect] are exactly [color].
int count(Color color, [Rect? rect]) {
final area =
@@ -6,4 +6,6 @@ abstract class CellFlags {
static const blink = 1 << 4;
static const inverse = 1 << 5;
static const invisible = 1 << 6;
static const strikethrough = 1 << 7;
static const overline = 1 << 11;
}
+27 -5
View File
@@ -206,6 +206,9 @@ class TerminalPainter {
final charCode = cellData.content & CellContent.codepointMask;
if (charCode == 0) return;
// SGR 8 (conceal): the cell keeps its background, the text is hidden.
if (cellData.flags & CellFlags.invisible != 0) return;
final cacheKey = cellData.getHash() ^ _textScaler.hashCode;
var paragraph = _paragraphCache.getLayoutFromCache(cacheKey);
@@ -220,21 +223,27 @@ class TerminalPainter {
color = color.withOpacity(0.5);
}
final underline = cellFlags & CellFlags.underline != 0;
const decorated =
CellFlags.underline | CellFlags.strikethrough | CellFlags.overline;
final style = _textStyle.toTextStyle(
color: color,
bold: cellFlags & CellFlags.bold != 0,
italic: cellFlags & CellFlags.italic != 0,
underline: cellFlags & CellFlags.underline != 0,
underline: underline,
strikethrough: cellFlags & CellFlags.strikethrough != 0,
overline: cellFlags & CellFlags.overline != 0,
decorationStyle: underline ? _underlineStyle(cellFlags) : null,
);
// Flutter does not draw an underline below a space which is not between
// Flutter does not draw a decoration on a space which is not between
// other regular characters. As only single characters are drawn, this
// will never produce an underline below a space in the terminal. As a
// workaround the regular space CodePoint 0x20 is replaced with
// the CodePoint 0xA0. This is a non breaking space and a underline can be
// drawn below it.
// the CodePoint 0xA0. This is a non breaking space and a decoration can
// be drawn on it.
var char = String.fromCharCode(charCode);
if (cellFlags & CellFlags.underline != 0 && charCode == 0x20) {
if (cellFlags & decorated != 0 && charCode == 0x20) {
char = String.fromCharCode(0xA0);
}
@@ -249,6 +258,19 @@ class TerminalPainter {
canvas.drawParagraph(paragraph, offset);
}
/// The line style of the underline in [flags] (SGR 4:1-4:5, 21).
static TextDecorationStyle _underlineStyle(int flags) {
final style =
(flags & CellAttr.underlineStyleMask) >> CellAttr.underlineStyleShift;
return switch (style) {
2 => TextDecorationStyle.double,
3 => TextDecorationStyle.wavy,
4 => TextDecorationStyle.dotted,
5 => TextDecorationStyle.dashed,
_ => TextDecorationStyle.solid,
};
}
/// Paints the background of a cell represented by [cellData] to [canvas] at
/// [offset].
@pragma('vm:prefer-inline')
+9 -1
View File
@@ -57,8 +57,12 @@ class TerminalStyle {
bool bold = false,
bool italic = false,
bool underline = false,
bool strikethrough = false,
bool overline = false,
TextDecorationStyle? decorationStyle,
}) {
return TextStyle(
decorationStyle: decorationStyle,
fontSize: fontSize,
height: height,
fontFamily: fontFamily,
@@ -67,7 +71,11 @@ class TerminalStyle {
backgroundColor: backgroundColor,
fontWeight: bold ? FontWeight.bold : FontWeight.normal,
fontStyle: italic ? FontStyle.italic : FontStyle.normal,
decoration: underline ? TextDecoration.underline : TextDecoration.none,
decoration: TextDecoration.combine([
if (underline) TextDecoration.underline,
if (strikethrough) TextDecoration.lineThrough,
if (overline) TextDecoration.overline,
]),
);
}