Workaround for TextPainter.computeDistanceToActualBaseline when the text is empty (#13305)
This commit is contained in:
@@ -285,6 +285,29 @@ class TextPainter {
|
||||
return new Size(width, height);
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/flutter/flutter/issues/13303
|
||||
double _workaroundBaselineBug(double value, TextBaseline baseline) {
|
||||
if (value >= 0.0)
|
||||
return value;
|
||||
|
||||
final ui.ParagraphBuilder builder = new ui.ParagraphBuilder(
|
||||
_createParagraphStyle(TextDirection.ltr),
|
||||
);
|
||||
if (text?.style != null)
|
||||
builder.pushStyle(text.style.getTextStyle(textScaleFactor: textScaleFactor));
|
||||
builder.addText(_kZeroWidthSpace);
|
||||
final ui.Paragraph paragraph = builder.build()
|
||||
..layout(new ui.ParagraphConstraints(width: double.INFINITY));
|
||||
|
||||
switch (baseline) {
|
||||
case TextBaseline.alphabetic:
|
||||
return paragraph.alphabeticBaseline;
|
||||
case TextBaseline.ideographic:
|
||||
return paragraph.ideographicBaseline;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns the distance from the top of the text to the first baseline of the
|
||||
/// given type.
|
||||
///
|
||||
@@ -294,9 +317,9 @@ class TextPainter {
|
||||
assert(baseline != null);
|
||||
switch (baseline) {
|
||||
case TextBaseline.alphabetic:
|
||||
return _paragraph.alphabeticBaseline;
|
||||
return _workaroundBaselineBug(_paragraph.alphabeticBaseline, baseline);
|
||||
case TextBaseline.ideographic:
|
||||
return _paragraph.ideographicBaseline;
|
||||
return _workaroundBaselineBug(_paragraph.ideographicBaseline, baseline);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -658,6 +658,21 @@ void main() {
|
||||
skip: skipExpectsWithKnownBugs,
|
||||
);
|
||||
}, skip: skipTestsWithKnownBugs);
|
||||
|
||||
test('TextPainter - empty text baseline', () {
|
||||
final TextPainter painter = new TextPainter()
|
||||
..textDirection = TextDirection.ltr;
|
||||
painter.text = const TextSpan(
|
||||
text: '',
|
||||
style: const TextStyle(fontFamily: 'Ahem', fontSize: 100.0, height: 1.0),
|
||||
);
|
||||
painter.layout();
|
||||
expect(
|
||||
// Returns -1
|
||||
painter.computeDistanceToActualBaseline(TextBaseline.alphabetic), 80.0,
|
||||
skip: skipExpectsWithKnownBugs,
|
||||
);
|
||||
}, skip: skipTestsWithKnownBugs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user