[web] Initial support for LineMetrics (flutter/engine#13985)

This commit is contained in:
Mouad Debbar
2019-11-27 12:56:44 -08:00
committed by GitHub
parent 12fc2661cc
commit 92b7f5f264
6 changed files with 571 additions and 113 deletions

View File

@@ -614,7 +614,9 @@ double _measureSubstring(
int start,
int end,
) {
assert(0 <= start && start <= end && end <= text.length);
assert(0 <= start);
assert(start <= end);
assert(end <= text.length);
if (start == end) {
return 0;
@@ -624,6 +626,7 @@ double _measureSubstring(
end == _lastEnd &&
text == _lastText &&
_lastStyle == style) {
// TODO(mdebbar): Explore caching all widths in a map, not only the last one.
return _lastWidth;
}
_lastStart = start;
@@ -654,7 +657,9 @@ double _roundWidth(double width) {
/// The return value is the new end of the substring after excluding the
/// trailing characters.
int _excludeTrailing(String text, int start, int end, CharPredicate predicate) {
assert(0 <= start && start <= end && end <= text.length);
assert(0 <= start);
assert(start <= end);
assert(end <= text.length);
while (start < end && predicate(text.codeUnitAt(end - 1))) {
end--;
@@ -676,7 +681,7 @@ class LinesCalculator {
final double _maxWidth;
/// The lines that have been consumed so far.
List<String> lines = <String>[];
List<EngineLineMetrics> lines = <EngineLineMetrics>[];
int _lineStart = 0;
int _chunkStart = 0;
@@ -703,13 +708,8 @@ class LinesCalculator {
// A single chunk of text could be force-broken into multiple lines if it
// doesn't fit in a single line. That's why we need a loop.
while (!_reachedMaxLines) {
final double lineWidth = _measureSubstring(
_canvasContext,
_style,
_text,
_lineStart,
chunkEndWithoutSpace,
);
final double lineWidth =
measureSubstring(_lineStart, chunkEndWithoutSpace);
// The current chunk doesn't reach the maximum width, so we stop here and
// wait for the next line break.
@@ -731,30 +731,37 @@ class LinesCalculator {
// When there's an ellipsis, truncate text to leave enough space for
// the ellipsis.
final double availableWidth = _maxWidth - _ellipsisWidth;
final int breakingPoint = _forceBreak(
availableWidth,
_text,
_lineStart,
chunkEndWithoutSpace,
final int breakingPoint = forceBreakSubstring(
maxWidth: availableWidth,
start: _lineStart,
end: chunkEndWithoutSpace,
);
lines.add(_text.substring(_lineStart, breakingPoint) + _style.ellipsis);
lines.add(EngineLineMetrics.withText(
_text.substring(_lineStart, breakingPoint) + _style.ellipsis,
hardBreak: false,
width: measureSubstring(_lineStart, breakingPoint) + _ellipsisWidth,
lineNumber: lines.length,
));
} else if (isChunkTooLong) {
final int breakingPoint =
_forceBreak(_maxWidth, _text, _lineStart, chunkEndWithoutSpace);
final int breakingPoint = forceBreakSubstring(
maxWidth: _maxWidth,
start: _lineStart,
end: chunkEndWithoutSpace,
);
if (breakingPoint == chunkEndWithoutSpace) {
// We could force-break the chunk any further which means we reached
// We couldn't force-break the chunk any further which means we reached
// the last character and there isn't enough space for it to fit in
// its own line. Since this is the last character in the chunk, we
// don't do anything here and we rely on the next iteration (or the
// [isHardBreak] check below) to break the line.
break;
}
_addLineBreak(lineEnd: breakingPoint);
_addLineBreak(lineEnd: breakingPoint, isHardBreak: false);
_chunkStart = breakingPoint;
} else {
// The control case of current line exceeding [_maxWidth], we break the
// line.
_addLineBreak(lineEnd: _chunkStart);
_addLineBreak(lineEnd: _chunkStart, isHardBreak: false);
}
}
@@ -763,39 +770,71 @@ class LinesCalculator {
}
if (isHardBreak) {
_addLineBreak(lineEnd: chunkEnd);
_addLineBreak(lineEnd: chunkEnd, isHardBreak: true);
}
_chunkStart = chunkEnd;
}
void _addLineBreak({@required int lineEnd}) {
final int indexWithoutNewlines = _excludeTrailing(
void _addLineBreak({
@required int lineEnd,
@required bool isHardBreak,
}) {
final int endWithoutNewlines = _excludeTrailing(
_text,
_lineStart,
lineEnd,
_newlinePredicate,
);
lines.add(_text.substring(_lineStart, indexWithoutNewlines));
final int endWithoutSpace = _excludeTrailing(
_text,
_lineStart,
endWithoutNewlines,
_whitespacePredicate,
);
final int lineNumber = lines.length;
final EngineLineMetrics metrics = EngineLineMetrics.withText(
_text.substring(_lineStart, endWithoutNewlines),
hardBreak: isHardBreak,
width: measureSubstring(_lineStart, endWithoutSpace),
lineNumber: lineNumber,
);
lines.add(metrics);
_lineStart = lineEnd;
if (lines.length == _style.maxLines) {
_reachedMaxLines = true;
}
}
/// Measures the width of a substring of [_text] starting from the index
/// [start] (inclusive) to [end] (exclusive).
///
/// This method uses [_text], [_style] and [_canvasContext] to perform the
/// measurement.
double measureSubstring(int start, int end) {
return _measureSubstring(_canvasContext, _style, _text, start, end);
}
/// In a continuous block of text, finds the point where text can be broken to
/// fit in the given constraint [maxWidth].
///
/// This always returns at least one character even if there isn't enough
/// space for it.
int _forceBreak(double maxWidth, String text, int start, int end) {
assert(0 <= start && start < end && end <= text.length);
int forceBreakSubstring({
@required double maxWidth,
@required int start,
@required int end,
}) {
assert(0 <= start);
assert(start < end);
assert(end <= _text.length);
// When there's no ellipsis, the breaking point should be at least one
// character away from [start].
int low = hasEllipsis ? start : start + 1;
int high = end;
do {
final int mid = (low + high) ~/ 2;
final double width =
_measureSubstring(_canvasContext, _style, text, start, mid);
final double width = measureSubstring(start, mid);
if (width < maxWidth) {
low = mid;
} else if (width > maxWidth) {
@@ -805,7 +844,6 @@ class LinesCalculator {
}
} while (high - low > 1);
// The breaking point should be at least one character away from [start].
return low;
}
}

View File

@@ -4,6 +4,102 @@
part of engine;
class EngineLineMetrics implements ui.LineMetrics {
EngineLineMetrics({
this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
this.width,
this.left,
this.baseline,
this.lineNumber,
}) : text = null;
EngineLineMetrics.withText(
this.text, {
@required this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
@required this.width,
this.left,
this.baseline,
@required this.lineNumber,
}) : assert(text != null),
assert(hardBreak != null),
assert(width != null),
assert(lineNumber != null && lineNumber >= 0);
/// The textual content representing this line.
final String text;
@override
final bool hardBreak;
@override
final double ascent;
@override
final double descent;
@override
final double unscaledAscent;
@override
final double height;
@override
final double width;
@override
final double left;
@override
final double baseline;
@override
final int lineNumber;
@override
int get hashCode => ui.hashValues(
text,
hardBreak,
ascent,
descent,
unscaledAscent,
height,
width,
left,
baseline,
lineNumber,
);
@override
bool operator ==(dynamic other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
final EngineLineMetrics typedOther = other;
return text == typedOther.text &&
hardBreak == typedOther.hardBreak &&
ascent == typedOther.ascent &&
descent == typedOther.descent &&
unscaledAscent == typedOther.unscaledAscent &&
height == typedOther.height &&
width == typedOther.width &&
left == typedOther.left &&
baseline == typedOther.baseline &&
lineNumber == typedOther.lineNumber;
}
}
/// The web implementation of [ui.Paragraph].
class EngineParagraph implements ui.Paragraph {
/// This class is created by the engine, and should not be instantiated
@@ -72,9 +168,28 @@ class EngineParagraph implements ui.Paragraph {
/// Valid only after [layout] has been called.
double get _lineHeight => _measurementResult?.lineHeight ?? 0;
// TODO(flutter_web): see https://github.com/flutter/flutter/issues/33613.
@override
double get longestLine => 0;
double get longestLine {
if (_measurementResult.lines != null) {
double maxWidth = 0.0;
for (ui.LineMetrics metrics in _measurementResult.lines) {
if (maxWidth < metrics.width) {
maxWidth = metrics.width;
}
}
return maxWidth;
}
// In the single-line case, the longest line is equal to the maximum
// intrinsic width of the paragraph.
if (_measurementResult.isSingleLine) {
return _measurementResult.maxIntrinsicWidth;
}
// If we don't have any line metrics information, there's no way to know the
// longest line in a multi-line paragraph.
return 0.0;
}
@override
double get minIntrinsicWidth => _measurementResult?.minIntrinsicWidth ?? 0;
@@ -101,7 +216,25 @@ class EngineParagraph implements ui.Paragraph {
/// If not null, this list would contain the strings representing each line
/// in the paragraph.
List<String> get _lines => _measurementResult?.lines;
///
/// Avoid repetitively accessing this field as it generates a new list every
/// time.
List<String> get _lines {
if (_plainText == null) {
return null;
}
final List<EngineLineMetrics> metricsList = _measurementResult.lines;
if (metricsList == null) {
return null;
}
final List<String> lines = <String>[];
for (EngineLineMetrics metrics in metricsList) {
lines.add(metrics.text);
}
return lines;
}
@override
void layout(ui.ParagraphConstraints constraints) {
@@ -161,7 +294,7 @@ class EngineParagraph implements ui.Paragraph {
bool get _drawOnCanvas {
bool canDrawTextOnCanvas;
if (TextMeasurementService.enableExperimentalCanvasImplementation) {
canDrawTextOnCanvas = _lines != null;
canDrawTextOnCanvas = _measurementResult.lines != null;
} else {
canDrawTextOnCanvas = _measurementResult.isSingleLine &&
_plainText != null &&
@@ -303,8 +436,7 @@ class EngineParagraph implements ui.Paragraph {
@override
List<ui.LineMetrics> computeLineMetrics() {
// TODO(flutter_web): https://github.com/flutter/flutter/issues/39537
return null;
return _measurementResult.lines;
}
}

View File

@@ -838,9 +838,9 @@ class MeasurementResult {
/// {@macro dart.ui.paragraph.ideographicBaseline}
final double ideographicBaseline;
/// Substrings that represent how the text should wrap into multiple lines to
/// satisfy [constraintWidth],
final List<String> lines;
/// The full list of [EngineLineMetrics] that describe in detail the various metrics
/// of each laid out line.
final List<EngineLineMetrics> lines;
const MeasurementResult(
this.constraintWidth, {

View File

@@ -1149,48 +1149,101 @@ enum BoxWidthStyle {
max,
}
class LineMetrics {
LineMetrics({
this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
this.width,
this.left,
this.baseline,
this.lineNumber,
});
@pragma('vm:entry-point')
LineMetrics._(
this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
this.width,
this.left,
this.baseline,
this.lineNumber,
);
abstract class LineMetrics {
factory LineMetrics({
bool hardBreak,
double ascent,
double descent,
double unscaledAscent,
double height,
double width,
double left,
double baseline,
int lineNumber,
}) = engine.EngineLineMetrics;
/// {@template dart.ui.LineMetrics.hardBreak}
/// True if this line ends with an explicit line break (e.g. '\n') or is the end
/// of the paragraph. False otherwise.
/// {@endtemplate}
final bool hardBreak;
/// {@template dart.ui.LineMetrics.ascent}
/// The rise from the [baseline] as calculated from the font and style for this line.
///
/// This is the final computed ascent and can be impacted by the strut, height, scaling,
/// as well as outlying runs that are very tall.
///
/// The [ascent] is provided as a positive value, even though it is typically defined
/// in fonts as negative. This is to ensure the signage of operations with these
/// metrics directly reflects the intended signage of the value. For example,
/// the y coordinate of the top edge of the line is `baseline - ascent`.
/// {@endtemplate}
final double ascent;
/// {@template dart.ui.LineMetrics.descent}
/// The drop from the [baseline] as calculated from the font and style for this line.
///
/// This is the final computed ascent and can be impacted by the strut, height, scaling,
/// as well as outlying runs that are very tall.
///
/// The y coordinate of the bottom edge of the line is `baseline + descent`.
/// {@endtemplate}
final double descent;
/// {@template dart.ui.LineMetrics.unscaledAscent}
/// The rise from the [baseline] as calculated from the font and style for this line
/// ignoring the [TextStyle.height].
///
/// The [unscaledAscent] is provided as a positive value, even though it is typically
/// defined in fonts as negative. This is to ensure the signage of operations with
/// these metrics directly reflects the intended signage of the value.
/// {@endtemplate}
final double unscaledAscent;
/// {@template dart.ui.LineMetrics.height}
/// Total height of the line from the top edge to the bottom edge.
///
/// This is equivalent to `round(ascent + descent)`. This value is provided
/// separately due to rounding causing sub-pixel differences from the unrounded
/// values.
/// {@endtemplate}
final double height;
/// {@template dart.ui.LineMetrics.width}
/// Width of the line from the left edge of the leftmost glyph to the right
/// edge of the rightmost glyph.
///
/// This is not the same as the width of the pargraph.
///
/// See also:
///
/// * [Paragraph.width], the max width passed in during layout.
/// * [Paragraph.longestLine], the width of the longest line in the paragraph.
/// {@endtemplate}
final double width;
/// {@template dart.ui.LineMetrics.left}
/// The x coordinate of left edge of the line.
///
/// The right edge can be obtained with `left + width`.
/// {@endtemplate}
final double left;
/// {@template dart.ui.LineMetrics.baseline}
/// The y coordinate of the baseline for this line from the top of the paragraph.
///
/// The bottom edge of the paragraph up to and including this line may be obtained
/// through `baseline + descent`.
/// {@endtemplate}
final double baseline;
/// {@template dart.ui.LineMetrics.lineNumber}
/// The number of this line in the overall paragraph, with the first line being
/// index zero.
///
/// For example, the first line is line 0, second line is line 1.
/// {@endtemplate}
final int lineNumber;
}

View File

@@ -164,4 +164,24 @@ void main() async {
paragraph.layout(const ParagraphConstraints(width: 1000));
expect(paragraph.getBoxesForRange(0, 0), isEmpty);
});
test('longestLine', () {
// [Paragraph.longestLine] is only supported by canvas-based measurement.
TextMeasurementService.enableExperimentalCanvasImplementation = true;
TextMeasurementService.initialize(rulerCacheCapacity: 2);
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
fontFamily: 'Ahem',
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
fontSize: 10,
));
builder.addText('abcd\nabcde abc');
final Paragraph paragraph = builder.build();
paragraph.layout(const ParagraphConstraints(width: 80.0));
expect(paragraph.longestLine, 50.0);
TextMeasurementService.clearCache();
TextMeasurementService.enableExperimentalCanvasImplementation = false;
});
}

View File

@@ -155,7 +155,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 60);
expect(result.minIntrinsicWidth, 30);
expect(result.height, 10);
expectLines(instance, result, <String>[' abc']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line(' abc', hardBreak: true, width: 60.0, lineNumber: 0),
]);
}
// trailing whitespaces
text = build(ahemStyle, 'abc ');
@@ -163,7 +167,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 60);
expect(result.minIntrinsicWidth, 30);
expect(result.height, 10);
expectLines(instance, result, <String>['abc ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: true, width: 30.0, lineNumber: 0),
]);
}
// mixed whitespaces
text = build(ahemStyle, ' ab c ');
@@ -171,7 +179,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 100);
expect(result.minIntrinsicWidth, 20);
expect(result.height, 10);
expectLines(instance, result, <String>[' ab c ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line(' ab c ', hardBreak: true, width: 80.0, lineNumber: 0),
]);
}
// single whitespace
text = build(ahemStyle, ' ');
@@ -179,7 +191,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 10);
expect(result.minIntrinsicWidth, 0);
expect(result.height, 10);
expectLines(instance, result, <String>[' ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line(' ', hardBreak: true, width: 0.0, lineNumber: 0),
]);
}
// whitespace only
text = build(ahemStyle, ' ');
@@ -187,7 +203,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 50);
expect(result.minIntrinsicWidth, 0);
expect(result.height, 10);
expectLines(instance, result, <String>[' ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line(' ', hardBreak: true, width: 0.0, lineNumber: 0),
]);
}
},
);
@@ -203,7 +223,11 @@ void main() async {
expect(result.minIntrinsicWidth, 50);
expect(result.width, 50);
expect(result.height, 10);
expectLines(instance, result, <String>['12345']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('12345', hardBreak: true, width: 50.0, lineNumber: 0),
]);
}
},
);
@@ -221,7 +245,12 @@ void main() async {
expect(result.minIntrinsicWidth, 30);
expect(result.width, 70);
expect(result.height, 20);
expectLines(instance, result, <String>['foo bar ', 'baz']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('foo bar ', hardBreak: false, width: 70.0, lineNumber: 0),
line('baz', hardBreak: true, width: 30.0, lineNumber: 1),
]);
}
},
);
@@ -237,7 +266,12 @@ void main() async {
expect(result.minIntrinsicWidth, 100);
expect(result.width, 50);
expect(result.height, 20);
expectLines(instance, result, <String>['12345', '67890']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('12345', hardBreak: false, width: 50.0, lineNumber: 0),
line('67890', hardBreak: true, width: 50.0, lineNumber: 1),
]);
}
// The first word is force-broken twice.
result =
@@ -247,7 +281,13 @@ void main() async {
expect(result.minIntrinsicWidth, 110);
expect(result.width, 50);
expect(result.height, 30);
expectLines(instance, result, <String>['abcde', 'fghij', 'k lm']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcde', hardBreak: false, width: 50.0, lineNumber: 0),
line('fghij', hardBreak: false, width: 50.0, lineNumber: 1),
line('k lm', hardBreak: true, width: 40.0, lineNumber: 2),
]);
}
// Constraints aren't enough even for a single character. In this case,
// we show a minimum of one character per line.
@@ -259,7 +299,12 @@ void main() async {
expect(result.minIntrinsicWidth, 20);
expect(result.width, 8);
expect(result.height, 20);
expectLines(instance, result, <String>['A', 'A']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('A', hardBreak: false, width: 10.0, lineNumber: 0),
line('A', hardBreak: true, width: 10.0, lineNumber: 1),
]);
}
// Extremely narrow constraints with new line in the middle.
result = instance.measure(build(ahemStyle, 'AA\nA'), narrowConstraints);
@@ -267,11 +312,16 @@ void main() async {
expect(result.maxIntrinsicWidth, 20);
expect(result.minIntrinsicWidth, 20);
expect(result.width, 8);
// This can only be done correctly by the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
// This can only be done correctly by the canvas-based implementation.
expect(result.height, 30);
expect(result.lines, <EngineLineMetrics>[
line('A', hardBreak: false, width: 10.0, lineNumber: 0),
line('A', hardBreak: true, width: 10.0, lineNumber: 1),
line('A', hardBreak: true, width: 10.0, lineNumber: 2),
]);
}
expectLines(instance, result, <String>['A', 'A', 'A']);
// Extremely narrow constraints with new line in the end.
result = instance.measure(build(ahemStyle, 'AAA\n'), narrowConstraints);
@@ -280,7 +330,14 @@ void main() async {
expect(result.minIntrinsicWidth, 30);
expect(result.width, 8);
expect(result.height, 40);
expectLines(instance, result, <String>['A', 'A', 'A', '']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('A', hardBreak: false, width: 10.0, lineNumber: 0),
line('A', hardBreak: false, width: 10.0, lineNumber: 1),
line('A', hardBreak: true, width: 10.0, lineNumber: 2),
line('', hardBreak: true, width: 0.0, lineNumber: 3),
]);
}
},
);
@@ -296,7 +353,12 @@ void main() async {
expect(result.minIntrinsicWidth, 20);
expect(result.width, 50);
expect(result.height, 20);
expectLines(instance, result, <String>['12', '34']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('12', hardBreak: true, width: 20.0, lineNumber: 0),
line('34', hardBreak: true, width: 20.0, lineNumber: 1),
]);
}
},
);
@@ -308,14 +370,26 @@ void main() async {
expect(result.maxIntrinsicWidth, 40);
expect(result.minIntrinsicWidth, 40);
expect(result.height, 30);
expectLines(instance, result, <String>['', '', '1234']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('', hardBreak: true, width: 0.0, lineNumber: 0),
line('', hardBreak: true, width: 0.0, lineNumber: 1),
line('1234', hardBreak: true, width: 40.0, lineNumber: 2),
]);
}
// Empty lines in the middle.
result = instance.measure(build(ahemStyle, '12\n\n345'), constraints);
expect(result.maxIntrinsicWidth, 30);
expect(result.minIntrinsicWidth, 30);
expect(result.height, 30);
expectLines(instance, result, <String>['12', '', '345']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('12', hardBreak: true, width: 20.0, lineNumber: 0),
line('', hardBreak: true, width: 0.0, lineNumber: 1),
line('345', hardBreak: true, width: 30.0, lineNumber: 2),
]);
}
// Empty lines in the end.
result = instance.measure(build(ahemStyle, '1234\n\n'), constraints);
@@ -324,7 +398,11 @@ void main() async {
if (instance is CanvasTextMeasurementService) {
// This can only be done correctly in the canvas-based implementation.
expect(result.height, 30);
expectLines(instance, result, <String>['1234', '', '']);
expect(result.lines, <EngineLineMetrics>[
line('1234', hardBreak: true, width: 40.0, lineNumber: 0),
line('', hardBreak: true, width: 0.0, lineNumber: 1),
line('', hardBreak: true, width: 0.0, lineNumber: 2),
]);
}
});
@@ -380,27 +458,55 @@ void main() async {
// Simple case.
result = instance.measure(build(ahemStyle, 'abc de fghi'), constraints);
expect(result.minIntrinsicWidth, 40);
expectLines(instance, result, <String>['abc ', 'de ', 'fghi']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: false, width: 30.0, lineNumber: 0),
line('de ', hardBreak: false, width: 20.0, lineNumber: 1),
line('fghi', hardBreak: true, width: 40.0, lineNumber: 2),
]);
}
// With new lines.
result = instance.measure(build(ahemStyle, 'abcd\nef\nghi'), constraints);
expect(result.minIntrinsicWidth, 40);
expectLines(instance, result, <String>['abcd', 'ef', 'ghi']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcd', hardBreak: true, width: 40.0, lineNumber: 0),
line('ef', hardBreak: true, width: 20.0, lineNumber: 1),
line('ghi', hardBreak: true, width: 30.0, lineNumber: 2),
]);
}
// With trailing whitespace.
result = instance.measure(build(ahemStyle, 'abcd efg'), constraints);
expect(result.minIntrinsicWidth, 40);
expectLines(instance, result, <String>['abcd ', 'efg']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0),
line('efg', hardBreak: true, width: 30.0, lineNumber: 1),
]);
}
// With trailing whitespace and new lines.
result = instance.measure(build(ahemStyle, 'abc \ndefg'), constraints);
expect(result.minIntrinsicWidth, 40);
expectLines(instance, result, <String>['abc ', 'defg']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: true, width: 30.0, lineNumber: 0),
line('defg', hardBreak: true, width: 40.0, lineNumber: 1),
]);
}
// Very long text.
result = instance.measure(build(ahemStyle, 'AAAAAAAAAAAA'), constraints);
expect(result.minIntrinsicWidth, 120);
expectLines(instance, result, <String>['AAAAA', 'AAAAA', 'AA']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 0),
line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1),
line('AA', hardBreak: true, width: 20.0, lineNumber: 2),
]);
}
});
testMeasurements('maxIntrinsicWidth', (TextMeasurementService instance) {
@@ -409,32 +515,65 @@ void main() async {
// Simple case.
result = instance.measure(build(ahemStyle, 'abc de fghi'), constraints);
expect(result.maxIntrinsicWidth, 110);
expectLines(instance, result, <String>['abc ', 'de ', 'fghi']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: false, width: 30.0, lineNumber: 0),
line('de ', hardBreak: false, width: 20.0, lineNumber: 1),
line('fghi', hardBreak: true, width: 40.0, lineNumber: 2),
]);
}
// With new lines.
result = instance.measure(build(ahemStyle, 'abcd\nef\nghi'), constraints);
expect(result.maxIntrinsicWidth, 40);
expectLines(instance, result, <String>['abcd', 'ef', 'ghi']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcd', hardBreak: true, width: 40.0, lineNumber: 0),
line('ef', hardBreak: true, width: 20.0, lineNumber: 1),
line('ghi', hardBreak: true, width: 30.0, lineNumber: 2),
]);
}
// With long whitespace.
result = instance.measure(build(ahemStyle, 'abcd efg'), constraints);
expect(result.maxIntrinsicWidth, 100);
expectLines(instance, result, <String>['abcd ', 'efg']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0),
line('efg', hardBreak: true, width: 30.0, lineNumber: 1),
]);
}
// With trailing whitespace.
result = instance.measure(build(ahemStyle, 'abc def '), constraints);
expect(result.maxIntrinsicWidth, 100);
expectLines(instance, result, <String>['abc ', 'def ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: false, width: 30.0, lineNumber: 0),
line('def ', hardBreak: true, width: 30.0, lineNumber: 1),
]);
}
// With trailing whitespace and new lines.
result = instance.measure(build(ahemStyle, 'abc \ndef '), constraints);
expect(result.maxIntrinsicWidth, 60);
expectLines(instance, result, <String>['abc ', 'def ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc ', hardBreak: true, width: 30.0, lineNumber: 0),
line('def ', hardBreak: true, width: 30.0, lineNumber: 1),
]);
}
// Very long text.
result = instance.measure(build(ahemStyle, 'AAAAAAAAAAAA'), constraints);
expect(result.maxIntrinsicWidth, 120);
expectLines(instance, result, <String>['AAAAA', 'AAAAA', 'AA']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 0),
line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1),
line('AA', hardBreak: true, width: 20.0, lineNumber: 2),
]);
}
});
testMeasurements(
@@ -458,7 +597,11 @@ void main() async {
expect(result.minIntrinsicWidth, 480);
expect(result.maxIntrinsicWidth, 480);
expect(result.height, 10);
expectLines(instance, result, <String>['AA...']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('AA...', hardBreak: false, width: 50.0, lineNumber: 0),
]);
}
// The short prefix should make the text break into two lines, but the
// second line should remain unbroken.
@@ -470,7 +613,12 @@ void main() async {
expect(result.minIntrinsicWidth, 450);
expect(result.maxIntrinsicWidth, 450);
expect(result.height, 20);
expectLines(instance, result, <String>['AAA', 'AA...']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('AAA', hardBreak: true, width: 30.0, lineNumber: 0),
line('AA...', hardBreak: false, width: 50.0, lineNumber: 1),
]);
}
// Tiny constraints.
const ui.ParagraphConstraints tinyConstraints =
@@ -480,7 +628,11 @@ void main() async {
expect(result.minIntrinsicWidth, 40);
expect(result.maxIntrinsicWidth, 40);
expect(result.height, 10);
expectLines(instance, result, <String>['...']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('...', hardBreak: false, width: 30.0, lineNumber: 0),
]);
}
// Tinier constraints (not enough for the ellipsis).
const ui.ParagraphConstraints tinierConstraints =
@@ -490,7 +642,11 @@ void main() async {
expect(result.maxIntrinsicWidth, 40);
expect(result.height, 10);
// TODO(flutter_web): https://github.com/flutter/flutter/issues/34346
// expectLines(instance, result, <String>['.']);
// if (instance is CanvasTextMeasurementService) {
// expect(result.lines, <EngineLineMetrics>[
// line('.', hardBreak: false, width: 10.0, lineNumber: 0),
// ]);
// }
},
);
@@ -507,14 +663,23 @@ void main() async {
final ui.Paragraph oneline = build(maxlinesStyle, 'One line');
result = instance.measure(oneline, infiniteConstraints);
expect(result.height, 10);
expectLines(instance, result, <String>['One line']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('One line', hardBreak: true, width: 80.0, lineNumber: 0),
]);
}
// The height should respect max lines and be limited to two lines here.
final ui.Paragraph threelines =
build(maxlinesStyle, 'First\nSecond\nThird');
result = instance.measure(threelines, infiniteConstraints);
expect(result.height, 20);
expectLines(instance, result, <String>['First', 'Second']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('First', hardBreak: true, width: 50.0, lineNumber: 0),
line('Second', hardBreak: true, width: 60.0, lineNumber: 1),
]);
}
// The height should respect max lines and be limited to two lines here.
final ui.Paragraph veryLong = build(
@@ -523,7 +688,12 @@ void main() async {
);
result = instance.measure(veryLong, constraints);
expect(result.height, 20);
expectLines(instance, result, <String>['Lorem ', 'ipsum ']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('Lorem ', hardBreak: false, width: 50.0, lineNumber: 0),
line('ipsum ', hardBreak: false, width: 50.0, lineNumber: 1),
]);
}
// Case when last line is a long unbreakable word.
final ui.Paragraph veryLongLastLine = build(
@@ -532,7 +702,12 @@ void main() async {
);
result = instance.measure(veryLongLastLine, constraints);
expect(result.height, 20);
expectLines(instance, result, <String>['AAA ', 'AAAAA']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('AAA ', hardBreak: false, width: 30.0, lineNumber: 0),
line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1),
]);
}
});
testMeasurements(
@@ -560,19 +735,31 @@ void main() async {
p = build(onelineStyle, 'abcdef');
result = instance.measure(p, constraints);
expect(result.height, 10);
expectLines(instance, result, <String>['abcdef']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abcdef', hardBreak: true, width: 60.0, lineNumber: 0),
]);
}
// Simple overflow case.
p = build(onelineStyle, 'abcd efg');
result = instance.measure(p, constraints);
expect(result.height, 10);
expectLines(instance, result, <String>['abc...']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('abc...', hardBreak: false, width: 60.0, lineNumber: 0),
]);
}
// Another simple overflow case.
p = build(onelineStyle, 'a bcde fgh');
result = instance.measure(p, constraints);
expect(result.height, 10);
expectLines(instance, result, <String>['a b...']);
if (instance is CanvasTextMeasurementService) {
expect(result.lines, <EngineLineMetrics>[
line('a b...', hardBreak: false, width: 60.0, lineNumber: 0),
]);
}
// The ellipsis is supposed to go on the second line, but because the
// 2nd line doesn't overflow, no ellipsis is shown.
@@ -581,8 +768,12 @@ void main() async {
// This can only be done correctly in the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
expect(result.height, 20);
expect(result.lines, <EngineLineMetrics>[
line('abcdef ', hardBreak: false, width: 60.0, lineNumber: 0),
line('ghijkl', hardBreak: true, width: 60.0, lineNumber: 1),
]);
}
expectLines(instance, result, <String>['abcdef ', 'ghijkl']);
// But when the 2nd line is long enough, the ellipsis is shown.
p = build(multilineStyle, 'abcd efghijkl');
@@ -590,8 +781,12 @@ void main() async {
// This can only be done correctly in the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
expect(result.height, 20);
expect(result.lines, <EngineLineMetrics>[
line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0),
line('efg...', hardBreak: false, width: 60.0, lineNumber: 1),
]);
}
expectLines(instance, result, <String>['abcd ', 'efg...']);
// Even if the second line can be broken, we don't break it, we just
// insert the ellipsis.
@@ -600,8 +795,12 @@ void main() async {
// This can only be done correctly in the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
expect(result.height, 20);
expect(result.lines, <EngineLineMetrics>[
line('abcde ', hardBreak: false, width: 50.0, lineNumber: 0),
line('f g...', hardBreak: false, width: 60.0, lineNumber: 1),
]);
}
expectLines(instance, result, <String>['abcde ', 'f g...']);
// First line overflows but second line doesn't.
p = build(multilineStyle, 'abcdefg hijk');
@@ -609,8 +808,12 @@ void main() async {
// This can only be done correctly in the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
expect(result.height, 20);
expect(result.lines, <EngineLineMetrics>[
line('abcdef', hardBreak: false, width: 60.0, lineNumber: 0),
line('g hijk', hardBreak: true, width: 60.0, lineNumber: 1),
]);
}
expectLines(instance, result, <String>['abcdef', 'g hijk']);
// Both first and second lines overflow.
p = build(multilineStyle, 'abcdefg hijklmnop');
@@ -618,16 +821,28 @@ void main() async {
// This can only be done correctly in the canvas-based implementation.
if (instance is CanvasTextMeasurementService) {
expect(result.height, 20);
expect(result.lines, <EngineLineMetrics>[
line('abcdef', hardBreak: false, width: 60.0, lineNumber: 0),
line('g h...', hardBreak: false, width: 60.0, lineNumber: 1),
]);
}
expectLines(instance, result, <String>['abcdef', 'g h...']);
},
);
});
}
void expectLines(TextMeasurementService instance, MeasurementResult result,
List<String> lines) {
if (instance is CanvasTextMeasurementService) {
expect(result.lines, lines);
}
/// Shortcut to avoid many line wraps in the tests above.
EngineLineMetrics line(
String text, {
double width,
int lineNumber,
bool hardBreak,
}) {
return EngineLineMetrics.withText(
text,
hardBreak: hardBreak,
width: width,
lineNumber: lineNumber,
);
}