[librsvg: 9/14] Path::to_cairo - strip out paths composed only of MoveTo




commit cfb5dd8886231f28a092e8ec9ed436da81ea9df3
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Feb 4 21:33:30 2022 -0600

    Path::to_cairo - strip out paths composed only of MoveTo
    
    Otherwise, text-to-path set at a nonzero position will generate a lone
    point at that position.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/673>

 src/drawing_ctx.rs | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 11634e230..029b50583 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -2233,17 +2233,11 @@ impl Path {
     fn from_cairo(cairo_path: cairo::Path) -> Path {
         let mut builder = PathBuilder::default();
 
-        // First, see if the path is a single MoveTo(0, 0).  Cairo does this when the code that
-        // generated the path didn't include any commands, due to the way it appends a MoveTo to
-        // some paths.
-        //
-        // Only do the conversion if the path is not empty; otherwise
-        // really return a librsvg path with no commands.
+        // Cairo has the habit of appending a MoveTo to some paths, but we don't want a
+        // path for empty text to generate that lone point.  So, strip out paths composed
+        // only of MoveTo.
 
-        if !cairo_path
-            .iter()
-            .eq([cairo::PathSegment::MoveTo((0.0, 0.0))])
-        {
+        if !cairo_path_is_only_move_tos(&cairo_path) {
             for segment in cairo_path.iter() {
                 match segment {
                     cairo::PathSegment::MoveTo((x, y)) => builder.move_to(x, y),
@@ -2260,6 +2254,10 @@ impl Path {
     }
 }
 
+fn cairo_path_is_only_move_tos(path: &cairo::Path) -> bool {
+    path.iter().all(|seg| matches!(seg, cairo::PathSegment::MoveTo((_, _))))
+}
+
 impl PathCommand {
     fn to_cairo(&self, cr: &cairo::Context) {
         match *self {
@@ -2354,14 +2352,14 @@ mod tests {
         let layout = pango::Layout::new(&context);
         layout.set_text("");
 
-        let path = pango_layout_to_path(0.0, 0.0, &layout, pango::Gravity::Auto).unwrap();
+        let path = pango_layout_to_path(10.0, 20.0, &layout, pango::Gravity::Auto).unwrap();
         assert!(path.is_empty());
 
         // only whitespace
 
         layout.set_text(" ");
 
-        let path = pango_layout_to_path(0.0, 0.0, &layout, pango::Gravity::Auto).unwrap();
+        let path = pango_layout_to_path(10.0, 20.0, &layout, pango::Gravity::Auto).unwrap();
         assert!(path.is_empty());
     }
 }


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