[librsvg: 4/5] text: Don't create Spans for chars nodes that end up as empty strings after xml:space normalization




commit 21b8bc12e717143a9c99c785f1877ce6ac25fe3c
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Nov 4 19:40:26 2020 -0600

    text: Don't create Spans for chars nodes that end up as empty strings after xml:space normalization
    
    The new test fails without this commit; it creates inter-line spacings
    in the second <text> element.

 src/text.rs                                        |  30 ++++++++++++---------
 .../reftests/bugs/642-nested-tspan-dx-dy-ref.png   | Bin 0 -> 5885 bytes
 .../reftests/bugs/642-nested-tspan-dx-dy.svg       |  17 ++++++++++++
 3 files changed, 34 insertions(+), 13 deletions(-)
---
diff --git a/src/text.rs b/src/text.rs
index 9fc7ab7e..e94f2020 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -380,16 +380,20 @@ impl Chars {
         dx: f64,
         dy: f64,
         depth: usize,
-    ) -> Span {
+    ) -> Option<Span> {
         self.ensure_normalized_string(node, values);
 
-        Span::new(
-            self.space_normalized.borrow().as_ref().unwrap(),
-            values.clone(),
-            dx,
-            dy,
-            depth,
-        )
+        if self.space_normalized.borrow().as_ref().unwrap() == "" {
+            None
+        } else {
+            Some(Span::new(
+                self.space_normalized.borrow().as_ref().unwrap(),
+                values.clone(),
+                dx,
+                dy,
+                depth,
+            ))
+        }
     }
 
     fn to_chunks(
@@ -401,12 +405,12 @@ impl Chars {
         dy: f64,
         depth: usize,
     ) {
-        let span = self.make_span(&node, values, dx, dy, depth);
-
-        let num_chunks = chunks.len();
-        assert!(num_chunks > 0);
+        if let Some(span) = self.make_span(&node, values, dx, dy, depth) {
+            let num_chunks = chunks.len();
+            assert!(num_chunks > 0);
 
-        chunks[num_chunks - 1].spans.push(span);
+            chunks[num_chunks - 1].spans.push(span);
+        }
     }
 
     pub fn get_string(&self) -> String {
diff --git a/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy-ref.png 
b/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy-ref.png
new file mode 100644
index 00000000..1030c425
Binary files /dev/null and b/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy-ref.png differ
diff --git a/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy.svg 
b/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy.svg
new file mode 100644
index 00000000..175d00c8
--- /dev/null
+++ b/tests/fixtures/reftests/bugs/642-nested-tspan-dx-dy.svg
@@ -0,0 +1,17 @@
+<svg width="500" height="500" xmlns="http://www.w3.org/2000/svg"; version="1.1">
+  <rect x="0" y="0" width="500" height="500" fill="white"/>
+
+  <!-- with buggy #642 these will overlap -->
+  <text x="100" y="100" fill="black" style="font-family: sans-serif; font-size: 20px;">
+    <tspan x="100" y="100" dy="0"><tspan>one</tspan></tspan>
+    <tspan x="100" y="100" dy="20"><tspan>two</tspan></tspan>
+    <tspan x="100" y="100" dy="40"><tspan>three</tspan></tspan>
+  </text>
+
+  <!-- but these won't, note the whitespace inside the outermost tspans -->
+  <text x="100" y="200" fill="black" style="font-family: sans-serif; font-size: 20px;">
+    <tspan x="100" y="200" dy="0"> <tspan>one</tspan></tspan>
+    <tspan x="100" y="200" dy="20"> <tspan>two</tspan></tspan>
+    <tspan x="100" y="200" dy="40"> <tspan>three</tspan></tspan>
+  </text>
+</svg>


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