-
Notifications
You must be signed in to change notification settings - Fork 24
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
Tree style builder #76
Open
nicoburns
wants to merge
2
commits into
linebender:main
Choose a base branch
from
nicoburns:tree-styles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Copyright 2021 the Parley Authors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! Context for layout. | ||
|
||
use super::context::*; | ||
use super::style::*; | ||
use super::FontContext; | ||
|
||
#[cfg(feature = "std")] | ||
use super::layout::Layout; | ||
|
||
use core::ops::RangeBounds; | ||
|
||
use crate::inline_box::InlineBox; | ||
|
||
/// Builder for constructing a text layout with ranged attributes. | ||
pub struct RangedBuilder<'a, B: Brush> { | ||
pub(crate) scale: f32, | ||
pub(crate) lcx: &'a mut LayoutContext<B>, | ||
pub(crate) fcx: &'a mut FontContext, | ||
} | ||
|
||
impl<'a, B: Brush> RangedBuilder<'a, B> { | ||
pub fn push_default(&mut self, property: &StyleProperty<B>) { | ||
let resolved = self | ||
.lcx | ||
.rcx | ||
.resolve_property(self.fcx, property, self.scale); | ||
self.lcx.ranged_style_builder.push_default(resolved); | ||
} | ||
|
||
pub fn push(&mut self, property: &StyleProperty<B>, range: impl RangeBounds<usize>) { | ||
let resolved = self | ||
.lcx | ||
.rcx | ||
.resolve_property(self.fcx, property, self.scale); | ||
self.lcx.ranged_style_builder.push(resolved, range); | ||
} | ||
|
||
pub fn push_inline_box(&mut self, inline_box: InlineBox) { | ||
self.lcx.inline_boxes.push(inline_box); | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
pub fn build_into(&mut self, layout: &mut Layout<B>, text: impl AsRef<str>) { | ||
// Apply RangedStyleBuilder styles to LayoutContext | ||
self.lcx.ranged_style_builder.finish(&mut self.lcx.styles); | ||
|
||
// Call generic layout builder method | ||
build_into_layout(layout, self.scale, text.as_ref(), self.lcx, self.fcx); | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
pub fn build(&mut self, text: impl AsRef<str>) -> Layout<B> { | ||
let mut layout = Layout::default(); | ||
self.build_into(&mut layout, text); | ||
layout | ||
} | ||
} | ||
|
||
/// Builder for constructing a text layout with a tree of attributes. | ||
pub struct TreeBuilder<'a, B: Brush> { | ||
pub(crate) scale: f32, | ||
pub(crate) lcx: &'a mut LayoutContext<B>, | ||
pub(crate) fcx: &'a mut FontContext, | ||
} | ||
|
||
impl<'a, B: Brush> TreeBuilder<'a, B> { | ||
pub fn push_style_span(&mut self, style: TextStyle<B>) { | ||
let resolved = self | ||
.lcx | ||
.rcx | ||
.resolve_entire_style_set(self.fcx, &style, self.scale); | ||
self.lcx.tree_style_builder.push_style_span(resolved); | ||
} | ||
|
||
pub fn push_style_modification_span<'s, 'iter>( | ||
&mut self, | ||
properties: impl IntoIterator<Item = &'iter StyleProperty<'s, B>>, | ||
) where | ||
's: 'iter, | ||
B: 'iter, | ||
{ | ||
self.lcx.tree_style_builder.push_style_modification_span( | ||
properties | ||
.into_iter() | ||
.map(|p| self.lcx.rcx.resolve_property(self.fcx, p, self.scale)), | ||
); | ||
} | ||
|
||
pub fn pop_style_span(&mut self) { | ||
self.lcx.tree_style_builder.pop_style_span(); | ||
} | ||
|
||
pub fn push_text(&mut self, text: &str) { | ||
self.lcx.tree_style_builder.push_text(text); | ||
} | ||
|
||
pub fn push_inline_box(&mut self, mut inline_box: InlineBox) { | ||
self.lcx.tree_style_builder.push_uncommitted_text(false); | ||
// TODO: arrange type better here to factor out the index | ||
inline_box.index = self.lcx.tree_style_builder.current_text_len(); | ||
self.lcx.inline_boxes.push(inline_box); | ||
} | ||
|
||
pub fn set_white_space_mode(&mut self, white_space_collapse: WhiteSpaceCollapse) { | ||
self.lcx | ||
.tree_style_builder | ||
.set_white_space_mode(white_space_collapse); | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
pub fn build_into(&mut self, layout: &mut Layout<B>) -> String { | ||
// Apply TreeStyleBuilder styles to LayoutContext | ||
let text = self.lcx.tree_style_builder.finish(&mut self.lcx.styles); | ||
|
||
self.lcx.analyze_text(&text); | ||
|
||
// Call generic layout builder method | ||
build_into_layout(layout, self.scale, &text, self.lcx, self.fcx); | ||
|
||
text | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
pub fn build(&mut self) -> (Layout<B>, String) { | ||
let mut layout = Layout::default(); | ||
let text = self.build_into(&mut layout); | ||
(layout, text) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
fn build_into_layout<B: Brush>( | ||
layout: &mut Layout<B>, | ||
scale: f32, | ||
text: &str, | ||
lcx: &mut LayoutContext<B>, | ||
fcx: &mut FontContext, | ||
) { | ||
layout.data.clear(); | ||
layout.data.scale = scale; | ||
layout.data.has_bidi = !lcx.bidi.levels().is_empty(); | ||
layout.data.base_level = lcx.bidi.base_level(); | ||
layout.data.text_len = text.len(); | ||
|
||
// println!("BUILD INTO ({})", text.len()); | ||
// for span in &lcx.styles { | ||
// let stack = lcx.rcx.stack(span.style.font_stack); | ||
// println!( | ||
// "{:?} weight:{}, family: {:?}", | ||
// span.range, span.style.font_weight, stack | ||
// ); | ||
// } | ||
|
||
let mut char_index = 0; | ||
for (i, style) in lcx.styles.iter().enumerate() { | ||
for _ in text[style.range.clone()].chars() { | ||
lcx.info[char_index].1 = i as u16; | ||
char_index += 1; | ||
} | ||
} | ||
|
||
// Copy the visual styles into the layout | ||
layout | ||
.data | ||
.styles | ||
.extend(lcx.styles.iter().map(|s| s.style.as_layout_style())); | ||
|
||
// Sort the inline boxes as subsequent code assumes that they are in text index order. | ||
// Note: It's important that this is a stable sort to allow users to control the order of contiguous inline boxes | ||
lcx.inline_boxes.sort_by_key(|b| b.index); | ||
|
||
// dbg!(&lcx.inline_boxes); | ||
|
||
{ | ||
let query = fcx.collection.query(&mut fcx.source_cache); | ||
super::shape::shape_text( | ||
&lcx.rcx, | ||
query, | ||
&lcx.styles, | ||
&lcx.inline_boxes, | ||
&lcx.info, | ||
lcx.bidi.levels(), | ||
&mut lcx.scx, | ||
text, | ||
layout, | ||
); | ||
} | ||
|
||
// Move inline boxes into the layout | ||
layout.data.inline_boxes.clear(); | ||
core::mem::swap(&mut layout.data.inline_boxes, &mut lcx.inline_boxes); | ||
|
||
layout.data.finish(); | ||
} |
Oops, something went wrong.
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.