summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Mena Quintero <federico@gnome.org>2020-11-03 18:44:09 -0600
committerFederico Mena Quintero <federico@gnome.org>2020-11-05 12:49:57 -0600
commit5d10a047a215d748f4487765b32570613ba4c1a0 (patch)
treee3f09768f954ab0587ea5a23fcc53757d89a019a
parentab069926f683eda6413f658c4d98606609827ac6 (diff)
downloadlibrsvg-5d10a047a215d748f4487765b32570613ba4c1a0.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--rsvg_internals/src/text.rs94
1 files changed, 46 insertions, 48 deletions
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index b4452e90..32223ea2 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -36,15 +36,15 @@ use crate::space::{xml_space_normalize, NormalizeDefault, XmlSpaceNormalize};
/// [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, &params);
let offset = baseline + baseline_shift;
- let dx = measured
- .dx
- .map(|l| l.normalize(&values, &params))
- .unwrap_or(0.0);
- let dy = measured
- .dy
- .map(|l| l.normalize(&values, &params))
- .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, &params));
+ let dy = self.dy.map_or(0.0, |l| l.normalize(&values, &params));
+
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, &params);
let mut y = self.y.normalize(values, &params);
- 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, &params));
- let chunk_y = chunk
- .y
- .map_or_else(|| y, |l| l.normalize(&chunk.values, &params));
+ 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, &params));
+ let y = self.y.map(|l| l.normalize(&values, &params));
+
+ let dx = self.dx.map_or(0.0, |l| l.normalize(&values, &params));
+ let dy = self.dy.map_or(0.0, |l| l.normalize(&values, &params));
+
+ 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,
);
}