-
Notifications
You must be signed in to change notification settings - Fork 52
Compute whitespace properties and line metrics for each line eagerly #420
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,6 +205,11 @@ impl LineItemData { | |
| self.kind == LayoutItemKind::TextRun | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn is_rtl(&self) -> bool { | ||
| self.bidi_level & 1 != 0 | ||
| } | ||
|
|
||
| pub(crate) fn compute_line_height<B: Brush>(&self, layout: &LayoutData<B>) -> f32 { | ||
| match self.kind { | ||
| LayoutItemKind::TextRun => { | ||
|
|
@@ -235,6 +240,41 @@ impl LineItemData { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /// If the item is a text run | ||
| /// - Determine if it consists entirely of whitespace (`is_whitespace` property) | ||
| /// - Determine if it has trailing whitespace (`has_trailing_whitespace` property) | ||
| pub(crate) fn compute_whitespace_properties<B: Brush>(&mut self, layout_data: &LayoutData<B>) { | ||
|
Contributor
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. It's probably a touch faster to run this as a running calculation during breaking (similar to #443) for the degenerate case of submitting thousands of whitespace, but I think we might be able to consider that an extremely unexpected / malicious input
Contributor
Author
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. Makes sense. Perhaps we could even compute it earlier in pipeline as part of "text analysis". Certainly that seems possible for the "entirely whitespace" flag, which would leave the cheaper trailing whitespace (in fact if we already knew whether a run was entirely whitespace then we'd only need to check one cluster to check trailing whitespace) |
||
| // Skip items which are not text runs | ||
| if self.kind != LayoutItemKind::TextRun { | ||
| return; | ||
| } | ||
|
|
||
| self.is_whitespace = true; | ||
| if self.is_rtl() { | ||
| // RTL runs check for "trailing" whitespace at the front. | ||
| for cluster in layout_data.clusters[self.cluster_range.clone()].iter() { | ||
| if cluster.info.is_whitespace() { | ||
| self.has_trailing_whitespace = true; | ||
| } else { | ||
| self.is_whitespace = false; | ||
| break; | ||
| } | ||
| } | ||
| } else { | ||
| for cluster in layout_data.clusters[self.cluster_range.clone()] | ||
| .iter() | ||
| .rev() | ||
| { | ||
| if cluster.info.is_whitespace() { | ||
| self.has_trailing_whitespace = true; | ||
| } else { | ||
| self.is_whitespace = false; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
|
|
||
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.
Should we add a test for this change? (I know it's changing existing functionality that I don't believe has a test (?), but it might be nice to add anyway to protect against future regression)
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.
Are you thinking a unit test for the function (probably doesn't make sense if we're going to change it again soon anyway) or an intergration test for the functionality (probably makes sense, but I'd need to work out what this actually affects).