diff options
author | Federico Mena Quintero <federico@gnome.org> | 2020-11-03 18:44:09 -0600 |
---|---|---|
committer | Federico Mena Quintero <federico@gnome.org> | 2020-11-04 19:55:12 -0600 |
commit | 9dcb1520a97bafbcaff292567d8b3077cf56949c (patch) | |
tree | 836b170a8382bef6ecbbfcb309a764dc3214009c | |
parent | bd20a3339bab267943bfca0e2df0b6db3228a2fb (diff) | |
download | librsvg-9dcb1520a97bafbcaff292567d8b3077cf56949c.tar.gz |
text: normalize some lengths as early as possible
Don't keep dx/dy in the structs as Option<Length<...>>; keep them as
f64.
x/y need to be kept as Option<f64> because they affect chunking.
-rw-r--r-- | src/text.rs | 94 |
1 files changed, 46 insertions, 48 deletions
diff --git a/src/text.rs b/src/text.rs index 733c85e6..e73517b2 100644 --- a/src/text.rs +++ b/src/text.rs @@ -36,15 +36,15 @@ use crate::url_resolver::Fragment; /// [text chunk]: https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction struct Chunk { values: ComputedValues, - x: Option<Length<Horizontal>>, - y: Option<Length<Vertical>>, + x: Option<f64>, + y: Option<f64>, spans: Vec<Span>, } struct MeasuredChunk { values: ComputedValues, - x: Option<Length<Horizontal>>, - y: Option<Length<Vertical>>, + x: Option<f64>, + y: Option<f64>, advance: (f64, f64), spans: Vec<MeasuredSpan>, } @@ -58,8 +58,8 @@ struct PositionedChunk { struct Span { values: ComputedValues, text: String, - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, + dx: f64, + dy: f64, _depth: usize, } @@ -68,8 +68,8 @@ struct MeasuredSpan { layout: pango::Layout, _layout_size: (f64, f64), advance: (f64, f64), - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, + dx: f64, + dy: f64, } struct PositionedSpan { @@ -82,11 +82,7 @@ struct PositionedSpan { } impl Chunk { - fn new( - values: &ComputedValues, - x: Option<Length<Horizontal>>, - y: Option<Length<Vertical>>, - ) -> Chunk { + fn new(values: &ComputedValues, x: Option<f64>, y: Option<f64>) -> Chunk { Chunk { values: values.clone(), x, @@ -178,13 +174,7 @@ fn text_anchor_advance( } impl Span { - fn new( - text: &str, - values: ComputedValues, - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, - depth: usize, - ) -> Span { + fn new(text: &str, values: ComputedValues, dx: f64, dy: f64, depth: usize) -> Span { Span { values, text: text.to_string(), @@ -238,14 +228,8 @@ impl PositionedSpan { let baseline_shift = values.baseline_shift().0.normalize(&values, ¶ms); let offset = baseline + baseline_shift; - let dx = measured - .dx - .map(|l| l.normalize(&values, ¶ms)) - .unwrap_or(0.0); - let dy = measured - .dy - .map(|l| l.normalize(&values, ¶ms)) - .unwrap_or(0.0); + let dx = measured.dx; + let dy = measured.dy; let (render_x, render_y) = if values.writing_mode().is_vertical() { (x + offset + dx, y + dy) @@ -287,8 +271,8 @@ fn children_to_chunks( acquired_nodes: &mut AcquiredNodes<'_>, cascaded: &CascadedValues<'_>, draw_ctx: &mut DrawingCtx, - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, + dx: f64, + dy: f64, depth: usize, ) { for child in node.children() { @@ -391,8 +375,8 @@ impl Chars { &self, node: &Node, values: &ComputedValues, - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, + dx: f64, + dy: f64, depth: usize, ) -> Span { self.ensure_normalized_string(node, values); @@ -411,8 +395,8 @@ impl Chars { node: &Node, values: &ComputedValues, chunks: &mut Vec<Chunk>, - dx: Option<Length<Horizontal>>, - dy: Option<Length<Vertical>>, + dx: f64, + dy: f64, depth: usize, ) { let span = self.make_span(&node, values, dx, dy, depth); @@ -443,17 +427,27 @@ impl Text { acquired_nodes: &mut AcquiredNodes<'_>, cascaded: &CascadedValues<'_>, draw_ctx: &mut DrawingCtx, + x: f64, + y: f64, ) -> Vec<Chunk> { let mut chunks = Vec::new(); - chunks.push(Chunk::new(cascaded.get(), Some(self.x), Some(self.y))); + + let values = cascaded.get(); + let params = draw_ctx.get_view_params(); + + chunks.push(Chunk::new(&values, Some(x), Some(y))); + + let dx = self.dx.map_or(0.0, |l| l.normalize(&values, ¶ms)); + let dy = self.dy.map_or(0.0, |l| l.normalize(&values, ¶ms)); + children_to_chunks( &mut chunks, node, acquired_nodes, cascaded, draw_ctx, - self.dx, - self.dy, + dx, + dy, 0, ); chunks @@ -491,7 +485,7 @@ impl Draw for Text { let mut x = self.x.normalize(values, ¶ms); let mut y = self.y.normalize(values, ¶ms); - let chunks = self.make_chunks(node, acquired_nodes, cascaded, draw_ctx); + let chunks = self.make_chunks(node, acquired_nodes, cascaded, draw_ctx, x, y); let mut measured_chunks = Vec::new(); for chunk in &chunks { @@ -500,12 +494,8 @@ impl Draw for Text { let mut positioned_chunks = Vec::new(); for chunk in &measured_chunks { - let chunk_x = chunk - .x - .map_or_else(|| x, |l| l.normalize(&chunk.values, ¶ms)); - let chunk_y = chunk - .y - .map_or_else(|| y, |l| l.normalize(&chunk.values, ¶ms)); + let chunk_x = chunk.x.unwrap_or(x); + let chunk_y = chunk.y.unwrap_or(y); let positioned = PositionedChunk::from_measured(&chunk, draw_ctx, chunk_x, chunk_y); @@ -574,7 +564,7 @@ fn extract_chars_children_to_chunks_recursively( if child.is_chars() { child .borrow_chars() - .to_chunks(&child, values, chunks, None, None, depth) + .to_chunks(&child, values, chunks, 0.0, 0.0, depth) } else { extract_chars_children_to_chunks_recursively(chunks, &child, values, depth + 1) } @@ -616,7 +606,15 @@ impl TSpan { depth: usize, ) { let values = cascaded.get(); - chunks.push(Chunk::new(values, self.x, self.y)); + + let params = draw_ctx.get_view_params(); + let x = self.x.map(|l| l.normalize(&values, ¶ms)); + let y = self.y.map(|l| l.normalize(&values, ¶ms)); + + let dx = self.dx.map_or(0.0, |l| l.normalize(&values, ¶ms)); + let dy = self.dy.map_or(0.0, |l| l.normalize(&values, ¶ms)); + + chunks.push(Chunk::new(values, x, y)); children_to_chunks( chunks, @@ -624,8 +622,8 @@ impl TSpan { acquired_nodes, cascaded, draw_ctx, - self.dx, - self.dy, + dx, + dy, depth, ); } |