-
-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add accessor to determine a TextElement size (#3130)
Add accessor to determine a `TextElement` size. Currently, there is no way to figure out the actual size of a `TextElement`. This is quite important as the element will have the lines laid out and broken down, so we need a way to determine the height of the resulting bounding box. Before: <img src="https://github.com/flame-engine/flame/assets/882703/5d69d9dd-068f-4deb-8dd6-19c90aca01db" width="30%" /> After: <img src="https://github.com/flame-engine/flame/assets/882703/a235badd-734f-4eb6-880a-3ebe0edc9433" width="30%" />
- Loading branch information
1 parent
7b706d5
commit 8a63a07
Showing
9 changed files
with
127 additions
and
12 deletions.
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
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,68 @@ | ||
import 'package:flame/src/text/elements/group_element.dart'; | ||
import 'package:flame/text.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('text elements', () { | ||
test('bounding box for empty group', () { | ||
final emptyGroup = GroupElement(width: 0, height: 0, children: []); | ||
expect(emptyGroup.boundingBox, Rect.zero); | ||
}); | ||
|
||
test('bounding box for inline elements', () { | ||
final document = DocumentRoot([ | ||
ParagraphNode.group([ | ||
PlainTextNode('Hello'), | ||
]), | ||
]); | ||
|
||
final element1 = document.format( | ||
DocumentStyle( | ||
paragraph: const BlockStyle( | ||
margin: EdgeInsets.zero, | ||
padding: EdgeInsets.zero, | ||
), | ||
), | ||
width: 80, | ||
height: 16, | ||
); | ||
const expected = Rect.fromLTWH(0, 0, 80, 16); | ||
|
||
expect(element1.boundingBox, expected); | ||
final element2 = element1.children.single as GroupElement; | ||
expect(element2.boundingBox, expected); | ||
final element3 = element2.children.single as InlineTextElement; | ||
expect(element3.boundingBox, expected); | ||
}); | ||
|
||
test('bounding box is composed', () { | ||
final document = DocumentRoot([ | ||
ParagraphNode.group([ | ||
PlainTextNode('Hello, '), | ||
PlainTextNode('there'), | ||
]), | ||
ParagraphNode.group([ | ||
ItalicTextNode.simple('General '), | ||
BoldTextNode.simple('Kenobi'), | ||
]), | ||
]); | ||
|
||
final element1 = document.format( | ||
DocumentStyle( | ||
paragraph: const BlockStyle( | ||
margin: EdgeInsets.zero, | ||
padding: EdgeInsets.zero, | ||
), | ||
), | ||
width: 600, | ||
height: 400, | ||
); | ||
expect(element1.boundingBox, const Rect.fromLTWH(0, 0, 224, 32)); | ||
final element2 = element1.children[0] as GroupElement; | ||
expect(element2.boundingBox, const Rect.fromLTWH(0, 0, 192, 16)); | ||
final element3 = element1.children[1] as GroupElement; | ||
expect(element3.boundingBox, const Rect.fromLTWH(0, 16, 224, 16)); | ||
}); | ||
}); | ||
} |