[librsvg: 14/37] PositionedChunk: implement the layout logic



commit 77b2e91f13d65520a615e8ec93f079c333df6621
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Oct 30 18:57:32 2018 -0600

    PositionedChunk: implement the layout logic

 rsvg_internals/src/text.rs | 45 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)
---
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 9627a7aa..b039dbb8 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -38,14 +38,16 @@ use state::{
 ///
 /// [text chunk]: https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction
 struct Chunk {
+    values: ComputedValues,
     x: Option<Length>,
     y: Option<Length>,
     spans: Vec<Span>,
 }
 
 impl Chunk {
-    fn new(x: Option<Length>, y: Option<Length>) -> Chunk {
+    fn new(values: &ComputedValues, x: Option<Length>, y: Option<Length>) -> Chunk {
         Chunk {
+            values: values.clone(),
             x,
             y,
             spans: Vec::new(),
@@ -54,11 +56,18 @@ impl Chunk {
 }
 
 struct MeasuredChunk {
+    values: ComputedValues,
     x: Option<Length>,
     y: Option<Length>,
     spans: Vec<MeasuredSpan>,
 }
 
+struct PositionedChunk {
+    next_chunk_x: f64,
+    next_chunk_y: f64,
+    spans: Vec<PositionedSpan>,
+}
+
 struct Span {
     values: ComputedValues,
     text: String,
@@ -88,8 +97,9 @@ impl Span {
 }
 
 impl MeasuredChunk {
-    fn from_chunk(chunk: &Chunk, draw_ctx: &DrawingCtx<'_>) -> MeasuredChunk {
+    fn from_chunk(chunk: &Chunk, draw_ctx: &DrawingCtx) -> MeasuredChunk {
         MeasuredChunk {
+            values: chunk.values.clone(),
             x: chunk.x,
             y: chunk.y,
             spans: chunk
@@ -126,6 +136,35 @@ impl MeasuredSpan {
     }
 }
 
+impl PositionedChunk {
+    fn from_measured(
+        measured: &MeasuredChunk,
+        draw_ctx: &DrawingCtx,
+        x: Length,
+        y: Length,
+    ) -> PositionedChunk {
+        let mut positioned = Vec::new();
+
+        let params = draw_ctx.get_view_params();
+
+        let mut x = x.normalize(&measured.values, &params);
+        let mut y = y.normalize(&measured.values, &params);
+
+        for measured_span in &measured.spans {
+            positioned.push(PositionedSpan::from_measured(measured_span, draw_ctx, x, y));
+
+            x += measured_span.advance.0;
+            y += measured_span.advance.1;
+        }
+
+        PositionedChunk {
+            next_chunk_x: x,
+            next_chunk_y: y,
+            spans: positioned,
+        }
+    }
+}
+
 impl PositionedSpan {
     fn from_measured(
         measured: &MeasuredSpan,
@@ -193,7 +232,7 @@ fn children_to_chunks(
                 if num_chunks > 0 {
                     chunks[num_chunks - 1].spans.push(span);
                 } else {
-                    let mut chunk = Chunk::new(x, y);
+                    let mut chunk = Chunk::new(values, x, y);
                     chunk.spans.push(span);
                     chunks.push(chunk);
                 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]