This repository has been archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Paragraph.getBoxesForRange uses LineMetrics #16625
Merged
+280
−19
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -426,6 +426,209 @@ void main() async { | |
expect(paragraph.getBoxesForRange(0, 0), isEmpty); | ||
}); | ||
|
||
testEachMeasurement('getBoxesForRange multi-line', () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding a test for a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good corner case to test. I'd like to merge this PR while the tree is green though. So I'll add the |
||
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( | ||
fontFamily: 'Ahem', | ||
fontStyle: FontStyle.normal, | ||
fontWeight: FontWeight.normal, | ||
fontSize: 10, | ||
textDirection: TextDirection.ltr, | ||
)); | ||
builder.addText('abcd\n'); | ||
builder.addText('abcdefg\n'); | ||
builder.addText('ab'); | ||
final Paragraph paragraph = builder.build(); | ||
paragraph.layout(const ParagraphConstraints(width: 100)); | ||
|
||
// In the dom-based measurement (except Firefox), there will be some | ||
// discrepancies around line ends. | ||
final isDiscrepancyExpected = | ||
!TextMeasurementService.enableExperimentalCanvasImplementation && | ||
browserEngine != BrowserEngine.firefox; | ||
|
||
// First line: "abcd\n" | ||
|
||
// At the beginning of the first line. | ||
expect( | ||
paragraph.getBoxesForRange(0, 0), | ||
<TextBox>[], | ||
); | ||
// At the end of the first line. | ||
expect( | ||
paragraph.getBoxesForRange(4, 4), | ||
<TextBox>[], | ||
); | ||
// Between "b" and "c" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(2, 2), | ||
<TextBox>[], | ||
); | ||
// The range "ab" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(0, 2), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(0.0, 0.0, 20.0, 10.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "bc" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(1, 3), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(10.0, 0.0, 30.0, 10.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "d" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(3, 4), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(30.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "\n" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(4, 5), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "cd\n" in the first line. | ||
expect( | ||
paragraph.getBoxesForRange(2, 5), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(20.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// Second line: "abcdefg\n" | ||
|
||
// At the beginning of the second line. | ||
expect( | ||
paragraph.getBoxesForRange(5, 5), | ||
<TextBox>[], | ||
); | ||
// At the end of the second line. | ||
expect( | ||
paragraph.getBoxesForRange(12, 12), | ||
<TextBox>[], | ||
); | ||
// The range "efg" in the second line. | ||
expect( | ||
paragraph.getBoxesForRange(9, 12), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(40.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "bcde" in the second line. | ||
expect( | ||
paragraph.getBoxesForRange(6, 10), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(10.0, 10.0, 50.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "fg\n" in the second line. | ||
expect( | ||
paragraph.getBoxesForRange(10, 13), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(50.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(70.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// Last (third) line: "ab" | ||
|
||
// At the beginning of the last line. | ||
expect( | ||
paragraph.getBoxesForRange(13, 13), | ||
<TextBox>[], | ||
); | ||
// At the end of the last line. | ||
expect( | ||
paragraph.getBoxesForRange(15, 15), | ||
<TextBox>[], | ||
); | ||
// The range "a" in the last line. | ||
expect( | ||
paragraph.getBoxesForRange(14, 15), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(10.0, 20.0, 20.0, 30.0, TextDirection.ltr), | ||
], | ||
); | ||
// The range "ab" in the last line. | ||
expect( | ||
paragraph.getBoxesForRange(13, 15), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(0.0, 20.0, 20.0, 30.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
|
||
// Combine multiple lines | ||
|
||
// The range "cd\nabc". | ||
expect( | ||
paragraph.getBoxesForRange(2, 8), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(20.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 10.0, 30.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// The range "\nabcd". | ||
expect( | ||
paragraph.getBoxesForRange(4, 9), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 10.0, 40.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// The range "d\nabcdefg\na". | ||
expect( | ||
paragraph.getBoxesForRange(3, 14), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(30.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(70.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 20.0, 10.0, 30.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// The range "abcd\nabcdefg\n". | ||
expect( | ||
paragraph.getBoxesForRange(0, 13), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(0.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(70.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
], | ||
); | ||
|
||
// The range "abcd\nabcdefg\nab". | ||
expect( | ||
paragraph.getBoxesForRange(0, 15), | ||
<TextBox>[ | ||
TextBox.fromLTRBD(0.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(40.0, 0.0, 40.0, 10.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
if (isDiscrepancyExpected) | ||
TextBox.fromLTRBD(70.0, 10.0, 70.0, 20.0, TextDirection.ltr), | ||
TextBox.fromLTRBD(0.0, 20.0, 20.0, 30.0, TextDirection.ltr), | ||
], | ||
); | ||
}); | ||
|
||
test('longestLine', () { | ||
// [Paragraph.longestLine] is only supported by canvas-based measurement. | ||
TextMeasurementService.enableExperimentalCanvasImplementation = true; | ||
|
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.
Love the ASCII art docs 👍