Fix Canvas.drawParagraph should assert that Paragraph.layout has been called (flutter/engine#33776)

This commit is contained in:
Bruno Leroux
2022-06-02 20:13:02 +02:00
committed by GitHub
parent aace477a3f
commit 1a7f78e201
3 changed files with 36 additions and 1 deletions

View File

@@ -4784,6 +4784,7 @@ class Canvas extends NativeFieldWrapperClass1 {
void drawParagraph(Paragraph paragraph, Offset offset) {
assert(paragraph != null);
assert(_offsetIsValid(offset));
assert(!paragraph._needsLayout);
paragraph._paint(this, offset.dx, offset.dy);
}

View File

@@ -2669,6 +2669,8 @@ class Paragraph extends NativeFieldWrapperClass1 {
@pragma('vm:entry-point')
Paragraph._();
bool _needsLayout = true;
/// The amount of horizontal space this paragraph occupies.
///
/// Valid only after [layout] has been called.
@@ -2717,7 +2719,13 @@ class Paragraph extends NativeFieldWrapperClass1 {
/// Computes the size and position of each glyph in the paragraph.
///
/// The [ParagraphConstraints] control how wide the text is allowed to be.
void layout(ParagraphConstraints constraints) => _layout(constraints.width);
void layout(ParagraphConstraints constraints) {
_layout(constraints.width);
assert(() {
_needsLayout = false;
return true;
}());
}
void _layout(double width) native 'Paragraph_layout';
List<TextBox> _decodeTextBoxes(Float32List encoded) {

View File

@@ -409,4 +409,30 @@ void main() {
await fuzzyGoldenImageCompare(image, 'text_with_gradient_with_matrix.png');
expect(areEqual, true);
}, skip: !Platform.isLinux); // https://github.com/flutter/flutter/issues/53784
test('Canvas.drawParagraph throws when Paragraph.layout was not called', () async {
// Regression test for https://github.com/flutter/flutter/issues/97172
bool assertsEnabled = false;
assert(() {
assertsEnabled = true;
return true;
}());
Object? error;
try {
await toImage((Canvas canvas) {
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle());
builder.addText('Woodstock!');
final Paragraph woodstock = builder.build();
canvas.drawParagraph(woodstock, const Offset(0, 50));
}, 100, 100);
} catch (e) {
error = e;
}
if (assertsEnabled) {
expect(error, isNotNull);
} else {
expect(error, isNull);
}
});
}