lineRects static method
List<Rect>
lineRects(
- TextPainter painter,
- String text,
- int start,
- int end, {
- required double fontSize,
One rectangle per visual line for [start, end), trimmed of the spaces at
line wraps and shrunk from the full line height to the glyph band, so
highlights look the same as in the PDF viewer.
Implementation
static List<Rect> lineRects(TextPainter painter, String text, int start, int end, {required double fontSize}) {
final List<Rect> rects = <Rect>[];
final int length = text.length;
int position = start.clamp(0, length);
final int stop = end.clamp(0, length);
int guard = 0;
bool isSpace(int index) => index >= 0 && index < length && (text.codeUnitAt(index) == 0x20 || text.codeUnitAt(index) == 0x0A || text.codeUnitAt(index) == 0x09 || text.codeUnitAt(index) == 0xA0);
while (position < stop && guard++ < 10000) {
final TextRange line = painter.getLineBoundary(TextPosition(offset: position));
final int lineEnd = line.end <= position ? stop : min(line.end, stop);
int a = position;
int b = lineEnd;
while (a < b && isSpace(a)) {
a++;
}
while (b > a && isSpace(b - 1)) {
b--;
}
if (b > a) {
Rect? union;
for (final TextBox box in painter.getBoxesForSelection(TextSelection(baseOffset: a, extentOffset: b))) {
final Rect rect = box.toRect();
if (rect.width <= 0.5) continue;
union = union == null ? rect : union.expandToInclude(rect);
}
if (union != null) {
final double band = fontSize * 1.3;
if (union.height > band) {
final double center = union.center.dy + fontSize * 0.04;
union = Rect.fromLTRB(union.left, center - band / 2, union.right, center + band / 2);
}
rects.add(union);
}
}
position = max(lineEnd, position + 1);
while (position < stop && text.codeUnitAt(position) == 0x0A) {
position++;
}
}
return rects;
}