-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a few issues rendering text with Skwasm. #51074
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:test/bootstrap/browser.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:ui/ui.dart' as ui; | ||
|
||
import '../common/test_initialization.dart'; | ||
import 'utils.dart'; | ||
|
||
void main() { | ||
internalBootstrapBrowserTest(() => testMain); | ||
} | ||
|
||
Future<void> testMain() async { | ||
setUpUnitTests( | ||
withImplicitView: true, | ||
setUpTestViewDimensions: false, | ||
); | ||
|
||
test('empty paragraph', () { | ||
const double fontSize = 10.0; | ||
final ui.Paragraph paragraph = ui.ParagraphBuilder(ui.ParagraphStyle( | ||
fontSize: fontSize, | ||
)).build(); | ||
paragraph.layout(const ui.ParagraphConstraints(width: double.infinity)); | ||
|
||
expect(paragraph.getLineMetricsAt(0), isNull); | ||
expect(paragraph.numberOfLines, 0); | ||
expect(paragraph.getLineNumberAt(0), isNull); | ||
}, skip: isHtml); // https://github.com/flutter/flutter/issues/144412 | ||
|
||
test('Basic line related metrics', () { | ||
const double fontSize = 10; | ||
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle( | ||
fontStyle: ui.FontStyle.normal, | ||
fontWeight: ui.FontWeight.normal, | ||
fontSize: fontSize, | ||
maxLines: 1, | ||
ellipsis: 'BBB', | ||
))..addText('A' * 100); | ||
final ui.Paragraph paragraph = builder.build(); | ||
paragraph.layout(const ui.ParagraphConstraints(width: 100.0)); | ||
|
||
expect(paragraph.numberOfLines, 1); | ||
|
||
expect(paragraph.getLineMetricsAt(-1), isNull); | ||
expect(paragraph.getLineMetricsAt(0)?.lineNumber, 0); | ||
expect(paragraph.getLineMetricsAt(1), isNull); | ||
|
||
expect(paragraph.getLineNumberAt(-1), isNull); | ||
expect(paragraph.getLineNumberAt(0), 0); | ||
expect(paragraph.getLineNumberAt(6), 0); | ||
// The last 3 characters on the first line are ellipsized with BBB. | ||
expect(paragraph.getLineMetricsAt(7), isNull); | ||
}); | ||
|
||
test('Basic glyph metrics', () { | ||
const double fontSize = 10; | ||
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle( | ||
fontStyle: ui.FontStyle.normal, | ||
fontWeight: ui.FontWeight.normal, | ||
fontFamily: 'FlutterTest', | ||
fontSize: fontSize, | ||
maxLines: 1, | ||
ellipsis: 'BBB', | ||
))..addText('A' * 100); | ||
final ui.Paragraph paragraph = builder.build(); | ||
paragraph.layout(const ui.ParagraphConstraints(width: 100.0)); | ||
|
||
expect(paragraph.getGlyphInfoAt(-1), isNull); | ||
|
||
// The last 3 characters on the first line are ellipsized with BBB. | ||
expect(paragraph.getGlyphInfoAt(0)?.graphemeClusterCodeUnitRange, const ui.TextRange(start: 0, end: 1)); | ||
expect(paragraph.getGlyphInfoAt(6)?.graphemeClusterCodeUnitRange, const ui.TextRange(start: 6, end: 7)); | ||
expect(paragraph.getGlyphInfoAt(7), isNull); | ||
expect(paragraph.getGlyphInfoAt(200), isNull); | ||
}); | ||
|
||
test('Basic glyph metrics - hit test', () { | ||
const double fontSize = 10.0; | ||
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle( | ||
fontSize: fontSize, | ||
fontFamily: 'FlutterTest', | ||
))..addText('Test\nTest'); | ||
final ui.Paragraph paragraph = builder.build(); | ||
paragraph.layout(const ui.ParagraphConstraints(width: double.infinity)); | ||
|
||
final ui.GlyphInfo? bottomRight = paragraph.getClosestGlyphInfoForOffset(const ui.Offset(99.0, 99.0)); | ||
final ui.GlyphInfo? last = paragraph.getGlyphInfoAt(8); | ||
expect(bottomRight, equals(last)); | ||
expect(bottomRight, isNot(paragraph.getGlyphInfoAt(0))); | ||
|
||
expect(bottomRight?.graphemeClusterLayoutBounds, const ui.Rect.fromLTWH(30, 10, 10, 10)); | ||
expect(bottomRight?.graphemeClusterCodeUnitRange, const ui.TextRange(start: 8, end: 9)); | ||
expect(bottomRight?.writingDirection, ui.TextDirection.ltr); | ||
}); | ||
|
||
test('rounding hack disabled by default', () { | ||
expect(ui.ParagraphBuilder.shouldDisableRoundingHack, isTrue); | ||
|
||
const double fontSize = 1.25; | ||
const String text = '12345'; | ||
assert((fontSize * text.length).truncate() != fontSize * text.length); | ||
final ui.ParagraphBuilder builder = ui.ParagraphBuilder( | ||
ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'), | ||
); | ||
builder.addText(text); | ||
final ui.Paragraph paragraph = builder.build() | ||
..layout(const ui.ParagraphConstraints(width: text.length * fontSize)); | ||
|
||
expect(paragraph.maxIntrinsicWidth, text.length * fontSize); | ||
switch (paragraph.computeLineMetrics()) { | ||
case [ui.LineMetrics(width: final double width)]: | ||
expect(width, text.length * fontSize); | ||
case final List<ui.LineMetrics> metrics: | ||
expect(metrics, hasLength(1)); | ||
} | ||
}, skip: isHtml); // The rounding hack doesn't apply to the html renderer | ||
|
||
test('setDisableRoundingHack to false works in tests', () { | ||
bool assertsEnabled = false; | ||
assert(() { | ||
assertsEnabled = true; | ||
return true; | ||
}()); | ||
if (!assertsEnabled){ | ||
return; | ||
} | ||
|
||
if (ui.ParagraphBuilder.shouldDisableRoundingHack) { | ||
ui.ParagraphBuilder.setDisableRoundingHack(false); | ||
addTearDown(() => ui.ParagraphBuilder.setDisableRoundingHack(true)); | ||
} | ||
|
||
assert(!ui.ParagraphBuilder.shouldDisableRoundingHack); | ||
const double fontSize = 1.25; | ||
const String text = '12345'; | ||
assert((fontSize * text.length).truncate() != fontSize * text.length); | ||
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest')); | ||
builder.addText(text); | ||
final ui.Paragraph paragraph = builder.build() | ||
..layout(const ui.ParagraphConstraints(width: text.length * fontSize)); | ||
expect(paragraph.computeLineMetrics().length, greaterThan(1)); | ||
}, skip: isHtml); // The rounding hack doesn't apply to the html renderer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand this. Why not:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because there is no reason to invoke this API when
shoudlDisableRoundingHack
is false, since the rounding hack is true by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth putting a comment above this code?