Merge branch 'worktree-agent-a9a25296deadb0eaa'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+455
@@ -0,0 +1,455 @@
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui';
|
||||
|
||||
/// Box drawing (U+2500-257F), block elements (U+2580-259F) and Powerline
|
||||
/// separators (U+E0B0-E0BF) drawn as geometry that fills the cell exactly,
|
||||
/// so frames, bars and prompt segments join across cells and rows whatever
|
||||
/// the font has (or lacks) and whatever the line height.
|
||||
bool isCellGlyph(int codePoint) =>
|
||||
(codePoint >= 0x2500 && codePoint <= 0x259F) ||
|
||||
(codePoint >= 0xE0B0 && codePoint <= 0xE0BF);
|
||||
|
||||
/// Paints [codePoint] (see [isCellGlyph]) in [color] over [cell]. Edges on
|
||||
/// the cell's sides overlap the neighbours by half a pixel so antialiased
|
||||
/// fractional cell edges leave no seam.
|
||||
void paintCellGlyph(Canvas canvas, Rect cell, int codePoint, Color color) {
|
||||
final paint = Paint()
|
||||
..color = color
|
||||
..isAntiAlias = true;
|
||||
if (codePoint < 0x2580) {
|
||||
_BoxDrawing(canvas, cell, paint).draw(codePoint);
|
||||
} else if (codePoint < 0x25A0) {
|
||||
_paintBlock(canvas, cell, codePoint, paint);
|
||||
} else {
|
||||
_paintPowerline(canvas, cell, codePoint, paint);
|
||||
}
|
||||
}
|
||||
|
||||
const _bleed = 0.5;
|
||||
|
||||
/// Widens the sides of [rect] that lie on [cell]'s sides by [_bleed].
|
||||
Rect _bled(Rect rect, Rect cell) => Rect.fromLTRB(
|
||||
rect.left <= cell.left ? rect.left - _bleed : rect.left,
|
||||
rect.top,
|
||||
rect.right >= cell.right ? rect.right + _bleed : rect.right,
|
||||
rect.bottom,
|
||||
);
|
||||
|
||||
/// The arms of U+2500-257F: up, right, down and left in two bits each
|
||||
/// (0 none, 1 light, 2 heavy, 3 double); 0 for the dashed, arc and diagonal
|
||||
/// characters, which are drawn on their own. Generated from the Unicode
|
||||
/// character names.
|
||||
const List<int> _boxArms = [
|
||||
0x44, 0x88, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, //
|
||||
0x00, 0x00, 0x00, 0x00, 0x14, 0x18, 0x24, 0x28, //
|
||||
0x50, 0x90, 0x60, 0xA0, 0x05, 0x09, 0x06, 0x0A, //
|
||||
0x41, 0x81, 0x42, 0x82, 0x15, 0x19, 0x16, 0x25, //
|
||||
0x26, 0x1A, 0x29, 0x2A, 0x51, 0x91, 0x52, 0x61, //
|
||||
0x62, 0x92, 0xA1, 0xA2, 0x54, 0x94, 0x58, 0x98, //
|
||||
0x64, 0xA4, 0x68, 0xA8, 0x45, 0x85, 0x49, 0x89, //
|
||||
0x46, 0x86, 0x4A, 0x8A, 0x55, 0x95, 0x59, 0x99, //
|
||||
0x56, 0x65, 0x66, 0x96, 0x5A, 0xA5, 0x69, 0x9A, //
|
||||
0xA9, 0xA6, 0x6A, 0xAA, 0x00, 0x00, 0x00, 0x00, //
|
||||
0xCC, 0x33, 0x1C, 0x34, 0x3C, 0xD0, 0x70, 0xF0, //
|
||||
0x0D, 0x07, 0x0F, 0xC1, 0x43, 0xC3, 0x1D, 0x37, //
|
||||
0x3F, 0xD1, 0x73, 0xF3, 0xDC, 0x74, 0xFC, 0xCD, //
|
||||
0x47, 0xCF, 0xDD, 0x77, 0xFF, 0x00, 0x00, 0x00, //
|
||||
0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x04, 0x10, //
|
||||
0x80, 0x02, 0x08, 0x20, 0x48, 0x21, 0x84, 0x12, //
|
||||
];
|
||||
|
||||
const _none = 0;
|
||||
const _light = 1;
|
||||
const _heavy = 2;
|
||||
const _double = 3;
|
||||
|
||||
class _BoxDrawing {
|
||||
_BoxDrawing(this.canvas, this.cell, this.paint)
|
||||
: light = math.max(1.0, (cell.width / 8).roundToDouble()),
|
||||
cx = (cell.left + cell.width / 2).floorToDouble(),
|
||||
cy = (cell.top + cell.height / 2).floorToDouble();
|
||||
|
||||
final Canvas canvas;
|
||||
final Rect cell;
|
||||
final Paint paint;
|
||||
|
||||
/// A light line's thickness; heavy is twice it, a double line two light
|
||||
/// lines a light line apart.
|
||||
final double light;
|
||||
|
||||
/// The pixel the lines' centres run through.
|
||||
final double cx;
|
||||
final double cy;
|
||||
|
||||
double get heavy => light * 2;
|
||||
|
||||
void draw(int codePoint) {
|
||||
final arms = _boxArms[codePoint - 0x2500];
|
||||
if (arms != 0) {
|
||||
_arms(arms & 3, arms >> 2 & 3, arms >> 4 & 3, arms >> 6 & 3);
|
||||
return;
|
||||
}
|
||||
switch (codePoint) {
|
||||
case 0x2504 || 0x2505:
|
||||
_dashes(3, horizontal: true, weight: (codePoint & 1) + 1);
|
||||
case 0x2506 || 0x2507:
|
||||
_dashes(3, horizontal: false, weight: (codePoint & 1) + 1);
|
||||
case 0x2508 || 0x2509:
|
||||
_dashes(4, horizontal: true, weight: (codePoint & 1) + 1);
|
||||
case 0x250A || 0x250B:
|
||||
_dashes(4, horizontal: false, weight: (codePoint & 1) + 1);
|
||||
case 0x254C || 0x254D:
|
||||
_dashes(2, horizontal: true, weight: (codePoint & 1) + 1);
|
||||
case 0x254E || 0x254F:
|
||||
_dashes(2, horizontal: false, weight: (codePoint & 1) + 1);
|
||||
case >= 0x256D && <= 0x2570:
|
||||
_arc(codePoint);
|
||||
case >= 0x2571 && <= 0x2573:
|
||||
_diagonals(codePoint);
|
||||
}
|
||||
}
|
||||
|
||||
double _thickness(int weight) => weight == _heavy ? heavy : light;
|
||||
|
||||
/// A horizontal stroke centred on [y] from [x0] to [x1].
|
||||
void _hLine(double x0, double x1, double y, double thickness) {
|
||||
final top = y - (thickness / 2).floorToDouble();
|
||||
canvas.drawRect(
|
||||
_bled(Rect.fromLTRB(x0, top, x1, top + thickness), cell),
|
||||
paint,
|
||||
);
|
||||
}
|
||||
|
||||
/// A vertical stroke centred on [x] from [y0] to [y1].
|
||||
void _vLine(double x, double y0, double y1, double thickness) {
|
||||
final left = x - (thickness / 2).floorToDouble();
|
||||
canvas.drawRect(Rect.fromLTRB(left, y0, left + thickness, y1), paint);
|
||||
}
|
||||
|
||||
/// The top (left) edge of a [thickness] stroke centred on [centre].
|
||||
static double _low(double centre, double thickness) =>
|
||||
centre - (thickness / 2).floorToDouble();
|
||||
|
||||
void _arms(int up, int right, int down, int left) {
|
||||
final gap = light;
|
||||
final verticalDouble = up == _double || down == _double;
|
||||
final horizontalDouble = left == _double || right == _double;
|
||||
|
||||
/// One arm from the cell's edge towards the centre. [before] and [after]
|
||||
/// are the arms across it (up and down for a horizontal arm), [opposite]
|
||||
/// the arm on the other side of the centre.
|
||||
void arm(
|
||||
int weight, {
|
||||
required bool horizontal,
|
||||
required bool positive,
|
||||
required int before,
|
||||
required int after,
|
||||
required int opposite,
|
||||
}) {
|
||||
if (weight == _none) return;
|
||||
final centre = horizontal ? cx : cy;
|
||||
final sign = positive ? 1.0 : -1.0;
|
||||
final edge = horizontal
|
||||
? (positive ? cell.right : cell.left)
|
||||
: (positive ? cell.bottom : cell.top);
|
||||
// Where the arm stops: the far side of a [thickness] line centred on
|
||||
// [target], seen from the cell edge the arm starts at.
|
||||
double reach(double target, double thickness) => positive
|
||||
? _low(target, thickness)
|
||||
: _low(target, thickness) + thickness;
|
||||
void stroke(double end, double across, double thickness) {
|
||||
final a = math.min(end, edge);
|
||||
final b = math.max(end, edge);
|
||||
if (horizontal) {
|
||||
_hLine(a, b, across, thickness);
|
||||
} else {
|
||||
_vLine(across, a, b, thickness);
|
||||
}
|
||||
}
|
||||
|
||||
final acrossDouble = horizontal ? verticalDouble : horizontalDouble;
|
||||
final mid = horizontal ? cy : cx;
|
||||
if (weight != _double) {
|
||||
double end;
|
||||
if (acrossDouble && opposite == _none) {
|
||||
// Against a double line: stop at its near line when it runs
|
||||
// through, else span both lines it hangs from.
|
||||
final through = before != _none && after != _none;
|
||||
end = reach(centre + sign * (through ? gap : -gap), light);
|
||||
} else {
|
||||
final across = math.max(before, after);
|
||||
end = reach(
|
||||
centre,
|
||||
across == _none || across == _double
|
||||
? _thickness(weight)
|
||||
: _thickness(across),
|
||||
);
|
||||
}
|
||||
stroke(end, mid, _thickness(weight));
|
||||
return;
|
||||
}
|
||||
for (final side in const [-1.0, 1.0]) {
|
||||
final near = side < 0 ? before : after;
|
||||
final far = side < 0 ? after : before;
|
||||
double end;
|
||||
if (near == _double) {
|
||||
end = reach(centre + sign * gap, light);
|
||||
} else if (far == _double) {
|
||||
end = reach(centre - sign * gap, light);
|
||||
} else if (near != _none || far != _none) {
|
||||
end = reach(centre, _thickness(math.max(near, far)));
|
||||
} else {
|
||||
end = reach(centre, light);
|
||||
}
|
||||
stroke(end, mid + side * gap, light);
|
||||
}
|
||||
}
|
||||
|
||||
arm(right,
|
||||
horizontal: true,
|
||||
positive: true,
|
||||
before: up,
|
||||
after: down,
|
||||
opposite: left);
|
||||
arm(left,
|
||||
horizontal: true,
|
||||
positive: false,
|
||||
before: up,
|
||||
after: down,
|
||||
opposite: right);
|
||||
arm(down,
|
||||
horizontal: false,
|
||||
positive: true,
|
||||
before: left,
|
||||
after: right,
|
||||
opposite: up);
|
||||
arm(up,
|
||||
horizontal: false,
|
||||
positive: false,
|
||||
before: left,
|
||||
after: right,
|
||||
opposite: down);
|
||||
}
|
||||
|
||||
void _dashes(int count, {required bool horizontal, required int weight}) {
|
||||
final thickness = _thickness(weight);
|
||||
final length = horizontal ? cell.width : cell.height;
|
||||
final step = length / count;
|
||||
final gap = math.max(1.0, (step * 0.4).roundToDouble());
|
||||
for (var i = 0; i < count; i++) {
|
||||
final start = i * step + gap / 2;
|
||||
final end = (i + 1) * step - gap / 2;
|
||||
if (horizontal) {
|
||||
final top = cy - (thickness / 2).floorToDouble();
|
||||
canvas.drawRect(
|
||||
Rect.fromLTRB(
|
||||
cell.left + start,
|
||||
top,
|
||||
cell.left + end,
|
||||
top + thickness,
|
||||
),
|
||||
paint,
|
||||
);
|
||||
} else {
|
||||
final left = cx - (thickness / 2).floorToDouble();
|
||||
canvas.drawRect(
|
||||
Rect.fromLTRB(
|
||||
left, cell.top + start, left + thickness, cell.top + end),
|
||||
paint,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ╭ ╮ ╯ ╰: a quarter circle joining the two arms' centre lines.
|
||||
void _arc(int codePoint) {
|
||||
final x = cx - (light / 2).floorToDouble() + light / 2;
|
||||
final y = cy - (light / 2).floorToDouble() + light / 2;
|
||||
final toRight = codePoint == 0x256D || codePoint == 0x2570;
|
||||
final toDown = codePoint == 0x256D || codePoint == 0x256E;
|
||||
final hEdge = toRight ? cell.right + _bleed : cell.left - _bleed;
|
||||
final vEdge = toDown ? cell.bottom : cell.top;
|
||||
final radius = math.min((hEdge - x).abs(), (vEdge - y).abs());
|
||||
final hx = toRight ? x + radius : x - radius;
|
||||
final vy = toDown ? y + radius : y - radius;
|
||||
final path = Path()
|
||||
..moveTo(hEdge, y)
|
||||
..lineTo(hx, y)
|
||||
..arcToPoint(
|
||||
Offset(x, vy),
|
||||
radius: Radius.circular(radius),
|
||||
clockwise: codePoint == 0x256E || codePoint == 0x2570,
|
||||
)
|
||||
..lineTo(x, vEdge);
|
||||
canvas.drawPath(
|
||||
path,
|
||||
Paint()
|
||||
..color = paint.color
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = light,
|
||||
);
|
||||
}
|
||||
|
||||
void _diagonals(int codePoint) {
|
||||
final stroke = Paint()
|
||||
..color = paint.color
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = light;
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
if (codePoint != 0x2572) {
|
||||
canvas.drawLine(cell.topRight, cell.bottomLeft, stroke);
|
||||
}
|
||||
if (codePoint != 0x2571) {
|
||||
canvas.drawLine(cell.topLeft, cell.bottomRight, stroke);
|
||||
}
|
||||
canvas.restore();
|
||||
}
|
||||
}
|
||||
|
||||
void _paintBlock(Canvas canvas, Rect cell, int codePoint, Paint paint) {
|
||||
final l = cell.left;
|
||||
final t = cell.top;
|
||||
final w = cell.width;
|
||||
final h = cell.height;
|
||||
// Edges inside the cell fall on whole pixels, so partial blocks are crisp.
|
||||
double x(double f) =>
|
||||
f == 0 || f == 1 ? l + w * f : (l + w * f).roundToDouble();
|
||||
double y(double f) =>
|
||||
f == 0 || f == 1 ? t + h * f : (t + h * f).roundToDouble();
|
||||
void fill(double x0, double y0, double x1, double y1) {
|
||||
canvas.drawRect(
|
||||
_bled(Rect.fromLTRB(x(x0), y(y0), x(x1), y(y1)), cell),
|
||||
paint,
|
||||
);
|
||||
}
|
||||
|
||||
switch (codePoint) {
|
||||
case 0x2580:
|
||||
fill(0, 0, 1, 0.5);
|
||||
case >= 0x2581 && <= 0x2588:
|
||||
fill(0, 1 - (codePoint - 0x2580) / 8, 1, 1);
|
||||
case >= 0x2589 && <= 0x258F:
|
||||
fill(0, 0, (0x2590 - codePoint) / 8, 1);
|
||||
case 0x2590:
|
||||
fill(0.5, 0, 1, 1);
|
||||
case >= 0x2591 && <= 0x2593:
|
||||
final alpha = (codePoint - 0x2590) / 4;
|
||||
canvas.drawRect(
|
||||
_bled(cell, cell),
|
||||
Paint()..color = paint.color.withValues(alpha: paint.color.a * alpha),
|
||||
);
|
||||
case 0x2594:
|
||||
fill(0, 0, 1, 1 / 8);
|
||||
case 0x2595:
|
||||
fill(7 / 8, 0, 1, 1);
|
||||
default:
|
||||
// Quadrants U+2596-259F: upper left, upper right, lower left, lower
|
||||
// right in bits 0-3.
|
||||
const quadrants = [4, 8, 1, 13, 9, 7, 11, 2, 6, 14];
|
||||
final bits = quadrants[codePoint - 0x2596];
|
||||
if (bits & 1 != 0) fill(0, 0, 0.5, 0.5);
|
||||
if (bits & 2 != 0) fill(0.5, 0, 1, 0.5);
|
||||
if (bits & 4 != 0) fill(0, 0.5, 0.5, 1);
|
||||
if (bits & 8 != 0) fill(0.5, 0.5, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void _paintPowerline(Canvas canvas, Rect cell, int codePoint, Paint paint) {
|
||||
final l = cell.left - _bleed;
|
||||
final r = cell.right + _bleed;
|
||||
final t = cell.top;
|
||||
final b = cell.bottom;
|
||||
final m = cell.center.dy;
|
||||
final stroke = Paint()
|
||||
..color = paint.color
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = math.max(1.0, (cell.width / 8).roundToDouble());
|
||||
|
||||
Path polygon(List<Offset> points) => Path()..addPolygon(points, true);
|
||||
|
||||
switch (codePoint) {
|
||||
case 0xE0B0:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(l, t), Offset(r, m), Offset(l, b)]), paint);
|
||||
case 0xE0B1:
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
canvas.drawPath(
|
||||
Path()
|
||||
..moveTo(cell.left, t)
|
||||
..lineTo(cell.right, m)
|
||||
..lineTo(cell.left, b),
|
||||
stroke,
|
||||
);
|
||||
canvas.restore();
|
||||
case 0xE0B2:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(r, t), Offset(l, m), Offset(r, b)]), paint);
|
||||
case 0xE0B3:
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
canvas.drawPath(
|
||||
Path()
|
||||
..moveTo(cell.right, t)
|
||||
..lineTo(cell.left, m)
|
||||
..lineTo(cell.right, b),
|
||||
stroke,
|
||||
);
|
||||
canvas.restore();
|
||||
case 0xE0B4 || 0xE0B5:
|
||||
// A half disc bulging right.
|
||||
final oval = Rect.fromLTRB(cell.left - cell.width, t, cell.right, b);
|
||||
final path = Path()..addArc(oval, -math.pi / 2, math.pi);
|
||||
if (codePoint == 0xE0B4) {
|
||||
path
|
||||
..lineTo(l, b)
|
||||
..lineTo(l, t)
|
||||
..close();
|
||||
canvas.drawPath(path, paint);
|
||||
} else {
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
canvas.drawPath(path, stroke);
|
||||
canvas.restore();
|
||||
}
|
||||
case 0xE0B6 || 0xE0B7:
|
||||
final oval = Rect.fromLTRB(cell.left, t, cell.right + cell.width, b);
|
||||
final path = Path()..addArc(oval, math.pi / 2, math.pi);
|
||||
if (codePoint == 0xE0B6) {
|
||||
path
|
||||
..lineTo(r, t)
|
||||
..lineTo(r, b)
|
||||
..close();
|
||||
canvas.drawPath(path, paint);
|
||||
} else {
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
canvas.drawPath(path, stroke);
|
||||
canvas.restore();
|
||||
}
|
||||
case 0xE0B8:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(l, t), Offset(l, b), Offset(r, b)]), paint);
|
||||
case 0xE0BA:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(r, t), Offset(r, b), Offset(l, b)]), paint);
|
||||
case 0xE0BC:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(l, t), Offset(r, t), Offset(l, b)]), paint);
|
||||
case 0xE0BE:
|
||||
canvas.drawPath(
|
||||
polygon([Offset(l, t), Offset(r, t), Offset(r, b)]), paint);
|
||||
case 0xE0B9 || 0xE0BF:
|
||||
_clippedLine(canvas, cell, cell.topLeft, cell.bottomRight, stroke);
|
||||
case 0xE0BB || 0xE0BD:
|
||||
_clippedLine(canvas, cell, cell.topRight, cell.bottomLeft, stroke);
|
||||
}
|
||||
}
|
||||
|
||||
void _clippedLine(Canvas canvas, Rect cell, Offset a, Offset b, Paint paint) {
|
||||
canvas.save();
|
||||
canvas.clipRect(cell);
|
||||
canvas.drawLine(a, b, paint);
|
||||
canvas.restore();
|
||||
}
|
||||
+284
-71
@@ -1,8 +1,11 @@
|
||||
import 'dart:ui';
|
||||
import 'package:flutter/painting.dart';
|
||||
|
||||
import 'package:xterm/src/core/buffer/graphemes.dart';
|
||||
import 'package:xterm/src/ui/cell_glyphs.dart';
|
||||
import 'package:xterm/src/ui/palette_builder.dart';
|
||||
import 'package:xterm/src/ui/paragraph_cache.dart';
|
||||
import 'package:xterm/src/ui/text_runs.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
/// Encapsulates the logic for painting various terminal elements.
|
||||
@@ -26,13 +29,76 @@ class TerminalPainter {
|
||||
/// [_textStyle] is changed, or when the system font changes.
|
||||
final _paragraphCache = ParagraphCache(10240);
|
||||
|
||||
/// Paragraphs for runs of same-styled cells ([paintLine]).
|
||||
final _runCache = TextRunCache(4096);
|
||||
|
||||
/// Changes whenever the same cells would paint differently (style, text
|
||||
/// scale, theme or fonts), so pictures of painted lines can be dropped.
|
||||
int get generation => _generation;
|
||||
var _generation = 0;
|
||||
|
||||
void _invalidate() {
|
||||
_paragraphCache.clear();
|
||||
_runCache.clear();
|
||||
for (final picture in _glyphPictures.values) {
|
||||
picture.dispose();
|
||||
}
|
||||
_glyphPictures.clear();
|
||||
_generation++;
|
||||
}
|
||||
|
||||
/// Recorded box drawing, block and Powerline glyphs by code point, colour,
|
||||
/// columns and the cell's sub-pixel position (which decides how lines snap
|
||||
/// to pixels), so each is built once rather than per cell.
|
||||
final _glyphPictures = <(int, int, int, int), Picture>{};
|
||||
|
||||
void _paintGlyph(
|
||||
Canvas canvas,
|
||||
Offset offset,
|
||||
int codePoint,
|
||||
int columns,
|
||||
Color color,
|
||||
) {
|
||||
final left = offset.dx.floorToDouble();
|
||||
final top = offset.dy.floorToDouble();
|
||||
// The fraction of a pixel the cell starts at, in 64ths.
|
||||
final dx = ((offset.dx - left) * 64).round();
|
||||
final dy = ((offset.dy - top) * 64).round();
|
||||
final key = (codePoint, color.toARGB32(), columns, dx << 8 | dy);
|
||||
var picture = _glyphPictures[key];
|
||||
if (picture == null) {
|
||||
if (_glyphPictures.length >= 1024) _invalidateGlyphs();
|
||||
final recorder = PictureRecorder();
|
||||
paintCellGlyph(
|
||||
Canvas(recorder),
|
||||
Offset(dx / 64, dy / 64) &
|
||||
Size(_cellSize.width * columns, _cellSize.height),
|
||||
codePoint,
|
||||
color,
|
||||
);
|
||||
picture = recorder.endRecording();
|
||||
_glyphPictures[key] = picture;
|
||||
}
|
||||
canvas.save();
|
||||
canvas.translate(left, top);
|
||||
canvas.drawPicture(picture);
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
void _invalidateGlyphs() {
|
||||
for (final picture in _glyphPictures.values) {
|
||||
picture.dispose();
|
||||
}
|
||||
_glyphPictures.clear();
|
||||
}
|
||||
|
||||
TerminalStyle get textStyle => _textStyle;
|
||||
TerminalStyle _textStyle;
|
||||
set textStyle(TerminalStyle value) {
|
||||
if (value == _textStyle) return;
|
||||
_textStyle = value;
|
||||
_cellSize = _measureCharSize();
|
||||
_paragraphCache.clear();
|
||||
_invalidate();
|
||||
}
|
||||
|
||||
TextScaler get textScaler => _textScaler;
|
||||
@@ -41,7 +107,7 @@ class TerminalPainter {
|
||||
if (value == _textScaler) return;
|
||||
_textScaler = value;
|
||||
_cellSize = _measureCharSize();
|
||||
_paragraphCache.clear();
|
||||
_invalidate();
|
||||
}
|
||||
|
||||
TerminalTheme get theme => _theme;
|
||||
@@ -50,7 +116,7 @@ class TerminalPainter {
|
||||
if (value == _theme) return;
|
||||
_theme = value;
|
||||
_colorPalette = PaletteBuilder(value).build();
|
||||
_paragraphCache.clear();
|
||||
_invalidate();
|
||||
}
|
||||
|
||||
Size _measureCharSize() {
|
||||
@@ -66,9 +132,12 @@ class TerminalPainter {
|
||||
final paragraph = builder.build();
|
||||
paragraph.layout(ParagraphConstraints(width: double.infinity));
|
||||
|
||||
// Whole-pixel rows: every row starts on a pixel, so backgrounds and box
|
||||
// drawing meet the next row without a seam. The width stays the font's
|
||||
// advance, which text runs rely on.
|
||||
final result = Size(
|
||||
paragraph.maxIntrinsicWidth / test.length,
|
||||
paragraph.height,
|
||||
paragraph.height.ceilToDouble(),
|
||||
);
|
||||
|
||||
paragraph.dispose();
|
||||
@@ -82,7 +151,7 @@ class TerminalPainter {
|
||||
/// clear cached state related to font rendering.
|
||||
void clearFontCache() {
|
||||
_cellSize = _measureCharSize();
|
||||
_paragraphCache.clear();
|
||||
_invalidate();
|
||||
}
|
||||
|
||||
/// Paints the cursor based on the current cursor type.
|
||||
@@ -169,28 +238,183 @@ class TerminalPainter {
|
||||
);
|
||||
}
|
||||
|
||||
/// Paints [line] to [canvas] at [offset]. The x offset of [offset] is usually
|
||||
/// 0, and the y offset is the top of the line.
|
||||
/// Paints [line] to [canvas] at [offset] (the top left of its first cell):
|
||||
/// every background, merged into runs of one colour, then the text. A
|
||||
/// line's plain text is one paragraph with a span per style ([RowText]);
|
||||
/// box drawing, block and Powerline characters are geometry
|
||||
/// ([paintCellGlyph]); anything else is drawn in its own cell.
|
||||
///
|
||||
/// With [paragraphs] false (a line just written, which may scroll away
|
||||
/// before it is drawn again; see `RowPictureCache`), or with less plain
|
||||
/// text than [minRowTextInk], the text is drawn cell by cell from the
|
||||
/// per-character cache instead: laying out a new paragraph costs more
|
||||
/// than drawing a few cached glyphs once, and less on every later frame.
|
||||
void paintLine(
|
||||
Canvas canvas,
|
||||
Offset offset,
|
||||
BufferLine line,
|
||||
) {
|
||||
BufferLine line, {
|
||||
bool paragraphs = true,
|
||||
}) {
|
||||
_paintBackgrounds(canvas, offset, line);
|
||||
|
||||
final cellData = CellData.empty();
|
||||
final cellWidth = _cellSize.width;
|
||||
final row = _rowText..clear();
|
||||
final length = line.length;
|
||||
|
||||
// Plain text into the row's paragraph (or straight to the canvas when
|
||||
// the line is not laid out); everything else drawn now.
|
||||
for (var i = 0; i < length; i++) {
|
||||
line.getCellData(i, cellData);
|
||||
final content = cellData.content;
|
||||
final cellFlags = cellData.flags;
|
||||
final width = content >> CellContent.widthShift;
|
||||
final cluster = content & CellContent.clusterFlag != 0;
|
||||
final code = content & CellContent.codepointMask;
|
||||
final hidden = cellFlags & CellFlags.invisible != 0;
|
||||
final decorated = cellFlags & _decorations != 0;
|
||||
|
||||
if (!cluster && (code == 0 || hidden || (code == 0x20 && !decorated))) {
|
||||
if (paragraphs) row.skip(1, _plainStyle);
|
||||
continue;
|
||||
}
|
||||
if (paragraphs &&
|
||||
!cluster &&
|
||||
width == 1 &&
|
||||
(code == 0x20 || isRunCharacter(code))) {
|
||||
final color = _foregroundColor(cellData).toARGB32();
|
||||
final flags = cellFlags & _textFlags;
|
||||
final key = color ^ (flags * 0x9E3779B1);
|
||||
TextStyle Function()? style;
|
||||
if (!row.continues(key)) {
|
||||
final styled = CellData(
|
||||
foreground: cellData.foreground,
|
||||
background: cellData.background,
|
||||
flags: cellFlags,
|
||||
content: 0,
|
||||
);
|
||||
style = () => _cellTextStyle(styled);
|
||||
}
|
||||
row.add(
|
||||
// Flutter draws no decoration under a plain space at the end of a
|
||||
// span; it does under a no-break space.
|
||||
code == 0x20 ? 0xA0 : code,
|
||||
key,
|
||||
style,
|
||||
decorated: decorated,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
paintCellForeground(canvas, offset.translate(i * cellWidth, 0), cellData);
|
||||
if (paragraphs) row.skip(width == 2 ? 2 : 1, _plainStyle);
|
||||
if (width == 2) i++;
|
||||
}
|
||||
|
||||
if (row.ink == 0) return;
|
||||
if (row.ink >= minRowTextInk) {
|
||||
final paragraph = row.layout(_runCache, _textScaler, cellWidth);
|
||||
if (paragraph != null) {
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Cell by cell: few characters, or a font whose glyphs are off the grid.
|
||||
for (var i = 0; i < length; i++) {
|
||||
line.getCellData(i, cellData);
|
||||
final content = cellData.content;
|
||||
if (content & CellContent.clusterFlag != 0) continue;
|
||||
final code = content & CellContent.codepointMask;
|
||||
if (content >> CellContent.widthShift != 1 || !isRunCharacter(code)) {
|
||||
if (code != 0x20 || cellData.flags & _decorations == 0) continue;
|
||||
}
|
||||
paintCellForeground(canvas, offset.translate(i * cellWidth, 0), cellData);
|
||||
}
|
||||
}
|
||||
|
||||
/// The least characters with ink a line needs to be drawn as one
|
||||
/// paragraph rather than cell by cell (see [paintLine]).
|
||||
static const minRowTextInk = 4;
|
||||
|
||||
final _rowText = RowText();
|
||||
|
||||
TextStyle _plainStyle() => _textStyle.toTextStyle();
|
||||
|
||||
static const _decorations =
|
||||
CellFlags.underline | CellFlags.strikethrough | CellFlags.overline;
|
||||
|
||||
/// The flags that change how text is drawn, besides its colour.
|
||||
static const _textFlags = CellFlags.bold |
|
||||
CellFlags.italic |
|
||||
CellFlags.underline |
|
||||
CellAttr.underlineStyleMask |
|
||||
CellFlags.strikethrough |
|
||||
CellFlags.overline;
|
||||
|
||||
/// Paints the backgrounds of [line]'s cells, one rectangle per run of a
|
||||
/// colour, with edges on whole pixels so neighbouring runs meet exactly.
|
||||
void _paintBackgrounds(Canvas canvas, Offset offset, BufferLine line) {
|
||||
final cellWidth = _cellSize.width;
|
||||
final cellData = CellData.empty();
|
||||
Color? runColor;
|
||||
var runStart = 0;
|
||||
|
||||
void flush(int end) {
|
||||
final color = runColor;
|
||||
if (color == null) return;
|
||||
final left = (offset.dx + runStart * cellWidth).roundToDouble();
|
||||
final right = (offset.dx + end * cellWidth).roundToDouble();
|
||||
canvas.drawRect(
|
||||
Rect.fromLTRB(left, offset.dy, right, offset.dy + _cellSize.height),
|
||||
Paint()..color = color,
|
||||
);
|
||||
}
|
||||
|
||||
for (var i = 0; i < line.length; i++) {
|
||||
line.getCellData(i, cellData);
|
||||
|
||||
final charWidth = cellData.content >> CellContent.widthShift;
|
||||
final cellOffset = offset.translate(i * cellWidth, 0);
|
||||
|
||||
paintCell(canvas, cellOffset, cellData);
|
||||
|
||||
if (charWidth == 2) {
|
||||
i++;
|
||||
final color = _backgroundColor(cellData);
|
||||
if (color != runColor) {
|
||||
flush(i);
|
||||
runColor = color;
|
||||
runStart = i;
|
||||
}
|
||||
}
|
||||
flush(line.length);
|
||||
}
|
||||
|
||||
/// The colour a cell's background is painted in; null for the theme's
|
||||
/// background, which is not painted.
|
||||
Color? _backgroundColor(CellData cellData) {
|
||||
if (cellData.flags & CellFlags.inverse != 0) {
|
||||
return resolveForegroundColor(cellData.foreground);
|
||||
}
|
||||
if (cellData.background & CellColor.typeMask == CellColor.normal) {
|
||||
return null;
|
||||
}
|
||||
return resolveBackgroundColor(cellData.background);
|
||||
}
|
||||
|
||||
/// The colour a cell's text is drawn in, inverse and faint applied.
|
||||
Color _foregroundColor(CellData cellData) {
|
||||
final color = cellData.flags & CellFlags.inverse == 0
|
||||
? resolveForegroundColor(cellData.foreground)
|
||||
: resolveBackgroundColor(cellData.background);
|
||||
if (cellData.flags & CellFlags.faint == 0) return color;
|
||||
return color.withValues(alpha: color.a * 0.5);
|
||||
}
|
||||
|
||||
TextStyle _cellTextStyle(CellData cellData) {
|
||||
final cellFlags = cellData.flags;
|
||||
final underline = cellFlags & CellFlags.underline != 0;
|
||||
return _textStyle.toTextStyle(
|
||||
color: _foregroundColor(cellData),
|
||||
bold: cellFlags & CellFlags.bold != 0,
|
||||
italic: cellFlags & CellFlags.italic != 0,
|
||||
underline: underline,
|
||||
strikethrough: cellFlags & CellFlags.strikethrough != 0,
|
||||
overline: cellFlags & CellFlags.overline != 0,
|
||||
decorationStyle: underline ? _underlineStyle(cellFlags) : null,
|
||||
);
|
||||
}
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@@ -199,63 +423,62 @@ class TerminalPainter {
|
||||
paintCellForeground(canvas, offset, cellData);
|
||||
}
|
||||
|
||||
/// Paints the character in the cell represented by [cellData] to [canvas] at
|
||||
/// [offset].
|
||||
@pragma('vm:prefer-inline')
|
||||
/// Paints the character in the cell represented by [cellData] to [canvas]
|
||||
/// at [offset]: its whole grapheme cluster, centred in the cell's columns
|
||||
/// when the glyph is narrower, or the geometry of a box drawing, block or
|
||||
/// Powerline character.
|
||||
void paintCellForeground(Canvas canvas, Offset offset, CellData cellData) {
|
||||
final charCode = cellData.content & CellContent.codepointMask;
|
||||
if (charCode == 0) return;
|
||||
final content = cellData.content;
|
||||
final cluster = content & CellContent.clusterFlag != 0;
|
||||
final charCode = content & CellContent.codepointMask;
|
||||
if (charCode == 0 && !cluster) return;
|
||||
|
||||
// SGR 8 (conceal): the cell keeps its background, the text is hidden.
|
||||
if (cellData.flags & CellFlags.invisible != 0) return;
|
||||
|
||||
final columns = content >> CellContent.widthShift == 2 ? 2 : 1;
|
||||
if (!cluster && isCellGlyph(charCode)) {
|
||||
_paintGlyph(
|
||||
canvas, offset, charCode, columns, _foregroundColor(cellData));
|
||||
return;
|
||||
}
|
||||
|
||||
final cacheKey = cellData.getHash() ^ _textScaler.hashCode;
|
||||
var paragraph = _paragraphCache.getLayoutFromCache(cacheKey);
|
||||
|
||||
if (paragraph == null) {
|
||||
final cellFlags = cellData.flags;
|
||||
|
||||
var color = cellFlags & CellFlags.inverse == 0
|
||||
? resolveForegroundColor(cellData.foreground)
|
||||
: resolveBackgroundColor(cellData.background);
|
||||
|
||||
if (cellData.flags & CellFlags.faint != 0) {
|
||||
color = color.withOpacity(0.5);
|
||||
var text = cellContentText(content);
|
||||
if (cellData.flags & _decorations != 0 && text == ' ') {
|
||||
// Flutter draws no decoration under a lone space; it does under a
|
||||
// no-break space.
|
||||
text = String.fromCharCode(0xA0);
|
||||
}
|
||||
|
||||
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: underline,
|
||||
strikethrough: cellFlags & CellFlags.strikethrough != 0,
|
||||
overline: cellFlags & CellFlags.overline != 0,
|
||||
decorationStyle: underline ? _underlineStyle(cellFlags) : null,
|
||||
);
|
||||
|
||||
// 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 decoration can
|
||||
// be drawn on it.
|
||||
var char = String.fromCharCode(charCode);
|
||||
if (cellFlags & decorated != 0 && charCode == 0x20) {
|
||||
char = String.fromCharCode(0xA0);
|
||||
}
|
||||
|
||||
paragraph = _paragraphCache.performAndCacheLayout(
|
||||
char,
|
||||
style,
|
||||
text,
|
||||
_cellTextStyle(cellData),
|
||||
_textScaler,
|
||||
cacheKey,
|
||||
);
|
||||
}
|
||||
|
||||
canvas.drawParagraph(paragraph, offset);
|
||||
final room = _cellSize.width * columns;
|
||||
final glyph = paragraph.maxIntrinsicWidth;
|
||||
if (glyph <= room + 0.5) {
|
||||
// Narrower (a fallback font's emoji or CJK): centred in its columns.
|
||||
final dx = glyph < room - 0.5 ? (room - glyph) / 2 : 0.0;
|
||||
canvas.drawParagraph(paragraph, offset.translate(dx, 0));
|
||||
return;
|
||||
}
|
||||
// Wider (a symbol or icon from a fallback font with a full-em advance):
|
||||
// scaled down to its columns and centred in the row, so it neither
|
||||
// covers the next cell nor shifts the grid.
|
||||
final scale = room / glyph;
|
||||
final dy = (_cellSize.height - paragraph.height * scale) / 2;
|
||||
canvas.save();
|
||||
canvas.translate(offset.dx, offset.dy + dy);
|
||||
canvas.scale(scale);
|
||||
canvas.drawParagraph(paragraph, Offset.zero);
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
/// The line style of the underline in [flags] (SGR 4:1-4:5, 21).
|
||||
@@ -275,22 +498,12 @@ class TerminalPainter {
|
||||
/// [offset].
|
||||
@pragma('vm:prefer-inline')
|
||||
void paintCellBackground(Canvas canvas, Offset offset, CellData cellData) {
|
||||
late Color color;
|
||||
final colorType = cellData.background & CellColor.typeMask;
|
||||
|
||||
if (cellData.flags & CellFlags.inverse != 0) {
|
||||
color = resolveForegroundColor(cellData.foreground);
|
||||
} else if (colorType == CellColor.normal) {
|
||||
return;
|
||||
} else {
|
||||
color = resolveBackgroundColor(cellData.background);
|
||||
}
|
||||
|
||||
final paint = Paint()..color = color;
|
||||
final color = _backgroundColor(cellData);
|
||||
if (color == null) return;
|
||||
final doubleWidth = cellData.content >> CellContent.widthShift == 2;
|
||||
final widthScale = doubleWidth ? 2 : 1;
|
||||
final size = Size(_cellSize.width * widthScale + 1, _cellSize.height);
|
||||
canvas.drawRect(offset & size, paint);
|
||||
canvas.drawRect(offset & size, Paint()..color = color);
|
||||
}
|
||||
|
||||
/// Get the effective foreground color for a cell from information encoded in
|
||||
|
||||
+30
-4
@@ -14,6 +14,7 @@ import 'package:xterm/src/terminal.dart';
|
||||
import 'package:xterm/src/ui/controller.dart';
|
||||
import 'package:xterm/src/ui/cursor_type.dart';
|
||||
import 'package:xterm/src/ui/painter.dart';
|
||||
import 'package:xterm/src/ui/row_pictures.dart';
|
||||
import 'package:xterm/src/ui/selection_mode.dart';
|
||||
import 'package:xterm/src/ui/terminal_size.dart';
|
||||
import 'package:xterm/src/ui/terminal_text_style.dart';
|
||||
@@ -176,6 +177,8 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
|
||||
final TerminalPainter _painter;
|
||||
|
||||
final _rows = RowPictureCache();
|
||||
|
||||
var _stickToBottom = true;
|
||||
|
||||
void _onScroll() {
|
||||
@@ -219,6 +222,13 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
_controller.removeListener(_onControllerUpdate);
|
||||
_focusNode.removeListener(_onFocusChange);
|
||||
_cursorBlinkVisible?.removeListener(markNeedsPaint);
|
||||
_rows.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_rows.clear();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -461,13 +471,29 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
final effectFirstLine = firstLine.clamp(0, lines.length - 1);
|
||||
final effectLastLine = lastLine.clamp(0, lines.length - 1);
|
||||
|
||||
// Each line is drawn from its picture, recorded again only when the
|
||||
// line changed, and settled into laid out text once it stays the same
|
||||
// (RowPictureCache).
|
||||
final generation = _painter.generation;
|
||||
_rows.beginFrame();
|
||||
for (var i = effectFirstLine; i <= effectLastLine; i++) {
|
||||
_painter.paintLine(
|
||||
canvas,
|
||||
offset.translate(0, (i * charHeight + _lineOffset).truncateToDouble()),
|
||||
lines[i],
|
||||
final line = lines[i];
|
||||
final picture = _rows.pictureOf(
|
||||
line,
|
||||
generation,
|
||||
(canvas, settled) =>
|
||||
_painter.paintLine(canvas, Offset.zero, line, paragraphs: settled),
|
||||
);
|
||||
final top = offset.translate(
|
||||
0,
|
||||
(i * charHeight + _lineOffset).truncateToDouble(),
|
||||
);
|
||||
canvas.save();
|
||||
canvas.translate(top.dx, top.dy);
|
||||
canvas.drawPicture(picture);
|
||||
canvas.restore();
|
||||
}
|
||||
_rows.trim((effectLastLine - effectFirstLine + 1) * 2 + 8);
|
||||
|
||||
if (_terminal.buffer.absoluteCursorY >= effectFirstLine &&
|
||||
_terminal.buffer.absoluteCursorY <= effectLastLine) {
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
import 'dart:collection';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:xterm/src/core/buffer/line.dart';
|
||||
|
||||
/// Paints a line at the origin; [settled] lines may lay out their text as
|
||||
/// paragraphs (`TerminalPainter.paintLine`).
|
||||
typedef RowPainter = void Function(Canvas canvas, bool settled);
|
||||
|
||||
/// Pictures of painted lines, kept while a line's cells and the painter's
|
||||
/// output stay the same, so a frame redraws only lines that changed: a
|
||||
/// scrolled line is drawn again from its picture, and a full-screen redraw
|
||||
/// of the same text records nothing new.
|
||||
///
|
||||
/// A line is first recorded cell by cell from cached glyphs, which costs
|
||||
/// little to record. Once it has been drawn unchanged in [settleAfter]
|
||||
/// frames it is recorded again as settled, laying its text out as
|
||||
/// paragraphs, which cost more to lay out but less to draw on every later
|
||||
/// frame. Output streaming past never settles, so it never pays for the
|
||||
/// layout; at most [settlePerFrame] lines settle in one frame.
|
||||
class RowPictureCache {
|
||||
RowPictureCache({this.settleAfter = 2, this.settlePerFrame = 8});
|
||||
|
||||
final int settleAfter;
|
||||
final int settlePerFrame;
|
||||
|
||||
final _entries = LinkedHashMap<BufferLine, _RowPicture>.identity();
|
||||
var _generation = -1;
|
||||
var _settleBudget = 0;
|
||||
|
||||
int get length => _entries.length;
|
||||
|
||||
/// Starts a frame: up to [settlePerFrame] lines may settle in it.
|
||||
void beginFrame() {
|
||||
_settleBudget = settlePerFrame;
|
||||
}
|
||||
|
||||
/// Whether [line]'s picture is settled.
|
||||
bool isSettled(BufferLine line) => _entries[line]?.settled ?? false;
|
||||
|
||||
/// The picture of [line] painted by [paint], reused while the line's
|
||||
/// version and [generation] (the painter's) are unchanged.
|
||||
Picture pictureOf(BufferLine line, int generation, RowPainter paint) {
|
||||
if (generation != _generation) {
|
||||
clear();
|
||||
_generation = generation;
|
||||
}
|
||||
final cached = _entries.remove(line);
|
||||
if (cached != null && cached.version == line.version) {
|
||||
// Most recently used last.
|
||||
_entries[line] = cached;
|
||||
if (cached.settled) return cached.picture;
|
||||
cached.reuses++;
|
||||
if (cached.reuses < settleAfter || _settleBudget <= 0) {
|
||||
return cached.picture;
|
||||
}
|
||||
_settleBudget--;
|
||||
cached.picture.dispose();
|
||||
final entry = _record(line, paint, settled: true);
|
||||
_entries[line] = entry;
|
||||
return entry.picture;
|
||||
}
|
||||
cached?.picture.dispose();
|
||||
final entry = _record(line, paint, settled: false);
|
||||
_entries[line] = entry;
|
||||
return entry.picture;
|
||||
}
|
||||
|
||||
_RowPicture _record(
|
||||
BufferLine line,
|
||||
RowPainter paint, {
|
||||
required bool settled,
|
||||
}) {
|
||||
final recorder = PictureRecorder();
|
||||
paint(Canvas(recorder), settled);
|
||||
return _RowPicture(line.version, recorder.endRecording(), settled);
|
||||
}
|
||||
|
||||
/// Drops the least recently used pictures beyond [keep].
|
||||
void trim(int keep) {
|
||||
while (_entries.length > keep) {
|
||||
final oldest = _entries.keys.first;
|
||||
_entries.remove(oldest)!.picture.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (final entry in _entries.values) {
|
||||
entry.picture.dispose();
|
||||
}
|
||||
_entries.clear();
|
||||
}
|
||||
}
|
||||
|
||||
class _RowPicture {
|
||||
_RowPicture(this.version, this.picture, this.settled);
|
||||
|
||||
final int version;
|
||||
final Picture picture;
|
||||
final bool settled;
|
||||
var reuses = 0;
|
||||
}
|
||||
+23
-3
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart' show listEquals;
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
const _kDefaultFontSize = 13.0;
|
||||
@@ -6,7 +7,8 @@ const _kDefaultHeight = 1.2;
|
||||
|
||||
const _kDefaultFontFamily = 'monospace';
|
||||
|
||||
const _kDefaultFontFamilyFallback = [
|
||||
/// The fallback families a [TerminalStyle] uses unless given its own.
|
||||
const kTerminalDefaultFontFamilyFallback = [
|
||||
'Menlo',
|
||||
'Monaco',
|
||||
'Consolas',
|
||||
@@ -28,7 +30,7 @@ class TerminalStyle {
|
||||
this.fontSize = _kDefaultFontSize,
|
||||
this.height = _kDefaultHeight,
|
||||
this.fontFamily = _kDefaultFontFamily,
|
||||
this.fontFamilyFallback = _kDefaultFontFamilyFallback,
|
||||
this.fontFamilyFallback = kTerminalDefaultFontFamilyFallback,
|
||||
});
|
||||
|
||||
factory TerminalStyle.fromTextStyle(TextStyle textStyle) {
|
||||
@@ -39,7 +41,7 @@ class TerminalStyle {
|
||||
textStyle.fontFamilyFallback?.first ??
|
||||
_kDefaultFontFamily,
|
||||
fontFamilyFallback:
|
||||
textStyle.fontFamilyFallback ?? _kDefaultFontFamilyFallback,
|
||||
textStyle.fontFamilyFallback ?? kTerminalDefaultFontFamilyFallback,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -92,4 +94,22 @@ class TerminalStyle {
|
||||
fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback,
|
||||
);
|
||||
}
|
||||
|
||||
// Equal styles keep the painter's caches: a host that builds its style
|
||||
// anew each frame does not make every line draw again.
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is TerminalStyle &&
|
||||
other.fontSize == fontSize &&
|
||||
other.height == height &&
|
||||
other.fontFamily == fontFamily &&
|
||||
listEquals(other.fontFamilyFallback, fontFamilyFallback);
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
fontSize,
|
||||
height,
|
||||
fontFamily,
|
||||
Object.hashAll(fontFamilyFallback),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -54,4 +54,47 @@ class TerminalTheme {
|
||||
final Color searchHitBackground;
|
||||
final Color searchHitBackgroundCurrent;
|
||||
final Color searchHitForeground;
|
||||
|
||||
List<Color> get _colors => [
|
||||
cursor,
|
||||
selection,
|
||||
foreground,
|
||||
background,
|
||||
black,
|
||||
red,
|
||||
green,
|
||||
yellow,
|
||||
blue,
|
||||
magenta,
|
||||
cyan,
|
||||
white,
|
||||
brightBlack,
|
||||
brightRed,
|
||||
brightGreen,
|
||||
brightYellow,
|
||||
brightBlue,
|
||||
brightMagenta,
|
||||
brightCyan,
|
||||
brightWhite,
|
||||
searchHitBackground,
|
||||
searchHitBackgroundCurrent,
|
||||
searchHitForeground,
|
||||
];
|
||||
|
||||
// Equal themes keep the painter's caches: a host that builds its theme
|
||||
// anew each frame does not make every line draw again.
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
if (other is! TerminalTheme) return false;
|
||||
final mine = _colors;
|
||||
final theirs = other._colors;
|
||||
for (var i = 0; i < mine.length; i++) {
|
||||
if (mine[i] != theirs[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hashAll(_colors);
|
||||
}
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user