fitText function
- TextSegmenter segmenter,
- String text,
- int width, {
- TextOverflow overflow = TextOverflow.ellipsis,
- String ellipsis = '\u2026',
text cut off to fit width columns as segmenter measures them
(TextSegmenter.width), … in place of what is cut: text itself
where it fits; else the clusters that fit and the ellipsis, which takes
the columns of an East Asian Ambiguous character
(TextSegmenter.ambiguousWidth), as on a line that RenderParagraph
cuts with the same overflow; where the ellipsis does not fit either,
the clusters that fit, with none, as there; and nothing at all for a
width of 0 or less. No cluster is split, and no blank is left next to
the ellipsis, as the paragraph trims them (TextOverflow.clip keeps them).
overflow says what is cut: the end (TextOverflow.ellipsis, as
Text has it by default here), the start, keeping the end
(TextOverflow.ellipsisStart), or the middle, keeping both ends, the
start half the room or more (TextOverflow.ellipsisMiddle; a path, a
name). TextOverflow.clip cuts the end with no ellipsis.
ellipsis is the mark put in place of what is cut, … by default; one
of several columns (...) takes them from the room, whatever it is made
of, and is left out where it does not fit.
final narrow = TextSegmenter();
print(fitText(narrow, 'Warnings (12)', 8)); // Warning…
print(fitText(narrow, '/usr/local/bin', 8,
overflow: TextOverflow.ellipsisStart)); // …cal/bin
print(fitText(narrow, '/usr/local/bin', 8,
overflow: TextOverflow.ellipsisMiddle)); // /usr…bin
final cjk = TextSegmenter(ambiguousWidth: 2);
print(fitText(cjk, 'Warnings (12)', 8)); // Warnin…
print(fitText(cjk, 'Warnings (12)', 1)); // W
print(fitText(narrow, 'Warnings (12)', 8, ellipsis: '...')); // Warni...
Implementation
String fitText(
TextSegmenter segmenter,
String text,
int width, {
TextOverflow overflow = TextOverflow.ellipsis,
String ellipsis = '\u2026',
}) {
final total = _columns(segmenter, text);
if (total <= width) {
return text;
}
if (width <= 0) {
return '';
}
final ellipsisWidth = _columns(segmenter, ellipsis);
final clip = overflow == TextOverflow.clip;
final room = clip || width < ellipsisWidth ? width : width - ellipsisWidth;
final mark = clip || width < ellipsisWidth ? '' : ellipsis;
final cut = _Cut();
switch (overflow) {
case TextOverflow.ellipsisStart:
var t = _tail(segmenter, text, cut, room, 0, total);
if (mark.isNotEmpty) {
t = _skipBlanks(segmenter, text, cut, t);
}
return '$mark${text.substring(t)}';
case TextOverflow.ellipsisMiddle when !clip && width >= ellipsisWidth:
final first = _head(
segmenter,
text,
cut,
(room + 1) >> 1,
text.length,
true,
);
final used = cut.cells;
final t = _skipBlanks(
segmenter,
text,
cut,
_tail(segmenter, text, cut, room - used, first, total - used),
);
final h = _head(segmenter, text, cut, room - cut.cells, t, true);
return '${text.substring(0, h)}$ellipsis${text.substring(t)}';
case TextOverflow.clip:
case TextOverflow.ellipsis:
case TextOverflow.ellipsisMiddle:
final h = _head(
segmenter,
text,
cut,
room,
text.length,
mark.isNotEmpty,
);
return '${text.substring(0, h)}$mark';
}
}