[librsvg: 21/22] (#791): Handle text direction while laying out spans inside chunks




commit 2b33cb1a971551b136418cf5f2955ff7fe541209
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Sep 23 12:05:15 2021 -0500

    (#791): Handle text direction while laying out spans inside chunks
    
    The test reference file sucks!  It shows Unicode hex boxes because
    librsvg's Roboto test font probably doesn't have Persian glyphs.  But
    at least the reference file shows the correct anchoring.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/791
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/593>

 src/text.rs                               |  18 ++++++++++++++----
 tests/fixtures/reftests/rtl-tspan-ref.png | Bin 0 -> 3961 bytes
 tests/fixtures/reftests/rtl-tspan.svg     |   9 +++++++++
 3 files changed, 23 insertions(+), 4 deletions(-)
---
diff --git a/src/text.rs b/src/text.rs
index 4994227b..df9148d7 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -147,10 +147,20 @@ impl PositionedChunk {
             let dx = mspan.dx;
             let dy = mspan.dy;
 
+            let start_pos = match values.direction() {
+                Direction::Ltr => (x, y),
+                Direction::Rtl => (x - mspan.advance.0, y),
+            };
+
+            let span_advance = match values.direction() {
+                Direction::Ltr => (mspan.advance.0, mspan.advance.1),
+                Direction::Rtl => (-mspan.advance.0, mspan.advance.1),
+            };
+
             let rendered_position = if values.writing_mode().is_horizontal() {
-                (x + dx, y - baseline_offset + dy)
+                (start_pos.0 + dx, start_pos.1 - baseline_offset + dy)
             } else {
-                (x + baseline_offset + dx, y + dy)
+                (start_pos.0 + baseline_offset + dx, start_pos.1 + dy)
             };
 
             let positioned_span = PositionedSpan {
@@ -161,8 +171,8 @@ impl PositionedChunk {
 
             positioned.push(positioned_span);
 
-            x = x + mspan.advance.0 + dx;
-            y = y + mspan.advance.1 + dy;
+            x = x + span_advance.0 + dx;
+            y = y + span_advance.1 + dy;
         }
 
         PositionedChunk {
diff --git a/tests/fixtures/reftests/rtl-tspan-ref.png b/tests/fixtures/reftests/rtl-tspan-ref.png
new file mode 100644
index 00000000..722c6745
Binary files /dev/null and b/tests/fixtures/reftests/rtl-tspan-ref.png differ
diff --git a/tests/fixtures/reftests/rtl-tspan.svg b/tests/fixtures/reftests/rtl-tspan.svg
new file mode 100644
index 00000000..510d6391
--- /dev/null
+++ b/tests/fixtures/reftests/rtl-tspan.svg
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<svg version="1.1" xmlns="http://www.w3.org/2000/svg"; xml:lang="fa" direction="rtl" width="300" height="100">
+    <line x1="50%" y1="0" x2="50%" y2="100%" stroke-width="2" stroke="blue"/>
+    <g font-family="Dana-FaNum" font-size="16" fill="black">
+        <text x="50%" y="30" text-anchor="start"><tspan font-weight="bold">نام: </tspan><tspan>مهدی 
</tspan></text>
+        <text x="50%" y="60" text-anchor="middle"><tspan font-weight="bold">نام: </tspan><tspan>مهدی 
</tspan></text>
+        <text x="50%" y="90" text-anchor="end"><tspan font-weight="bold">نام: </tspan><tspan>مهدی 
</tspan></text>
+    </g>
+</svg>


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