[librsvg: 8/12] layout::TextSpan - put a PositionedSpan's parameters here




commit cba52dd04e4313277b570707e5e3e75bef4ab592
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jun 3 16:33:27 2021 -0500

    layout::TextSpan - put a PositionedSpan's parameters here
    
    Renamed DrawingCtx::draw_text() to draw_text_span() accordingly.

 src/drawing_ctx.rs | 72 +++++++++++++++++++++---------------------------------
 src/layout.rs      | 14 ++++++++++-
 src/text.rs        | 37 ++++++++++++++++++++++++++--
 3 files changed, 76 insertions(+), 47 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 1d10d4e8..648a93b7 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -22,7 +22,7 @@ use crate::error::{AcquireError, ImplementationLimit, RenderingError};
 use crate::filters::{self, FilterSpec};
 use crate::float_eq_cairo::ApproxEqCairo;
 use crate::gradient::{GradientVariant, SpreadMethod, UserSpaceGradient};
-use crate::layout::{Image, Shape, StackingContext, Stroke};
+use crate::layout::{Image, Shape, StackingContext, Stroke, TextSpan};
 use crate::length::*;
 use crate::marker;
 use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
@@ -1352,21 +1352,19 @@ impl DrawingCtx {
         }
     }
 
-    // TODO: just like we have Shape with all its parameters, do the same for a layout::Text.
-    pub fn draw_text(
+    pub fn draw_text_span(
         &mut self,
-        layout: &pango::Layout,
-        x: f64,
-        y: f64,
+        view_params: &ViewParams,
+        span: &TextSpan,
         acquired_nodes: &mut AcquiredNodes<'_>,
         values: &ComputedValues,
         clipping: bool,
     ) -> Result<BoundingBox, RenderingError> {
         let transform = self.get_transform();
 
-        let gravity = layout.get_context().unwrap().get_gravity();
+        let gravity = span.layout.get_context().unwrap().get_gravity();
 
-        let bbox = compute_text_box(layout, x, y, transform, gravity);
+        let bbox = compute_text_box(&span.layout, span.x, span.y, transform, gravity);
         if bbox.is_none() {
             return Ok(self.empty_bbox());
         }
@@ -1377,17 +1375,11 @@ impl DrawingCtx {
 
         let cr = saved_cr.draw_ctx.cr.clone();
 
-        cr.set_antialias(cairo::Antialias::from(values.text_rendering()));
-
-        let view_params = saved_cr.draw_ctx.get_view_params();
-        {
-            let params = NormalizeParams::new(values, &view_params);
-            let stroke = Stroke::new(values, &params);
+        cr.set_antialias(cairo::Antialias::from(span.text_rendering));
 
-            setup_cr_for_stroke(&cr, &stroke);
-        }
+        setup_cr_for_stroke(&cr, &span.stroke);
 
-        cr.move_to(x, y);
+        cr.move_to(span.x, span.y);
 
         let rotation = gravity.to_rotation();
         if !rotation.approx_eq_cairo(0.0) {
@@ -1395,46 +1387,38 @@ impl DrawingCtx {
         }
 
         if clipping {
-            pangocairo::functions::update_layout(&cr, &layout);
-            pangocairo::functions::layout_path(&cr, &layout);
+            pangocairo::functions::update_layout(&cr, &span.layout);
+            pangocairo::functions::layout_path(&cr, &span.layout);
             return Ok(saved_cr.draw_ctx.empty_bbox());
         }
 
         // Fill
 
-        let paint_source = values
-            .fill()
-            .0
-            .resolve(acquired_nodes, values.fill_opacity().0, values.color().0)
-            .to_user_space(&bbox, &view_params, values);
+        if span.is_visible {
+            let fill_paint = span.fill_paint.to_user_space(&bbox, &view_params, values);
 
-        saved_cr
-            .draw_ctx
-            .set_paint_source(&paint_source, acquired_nodes)
-            .map(|had_paint_server| {
-                if had_paint_server {
-                    pangocairo::functions::update_layout(&cr, &layout);
-                    if values.is_visible() {
-                        pangocairo::functions::show_layout(&cr, &layout);
-                    }
-                };
-            })?;
+            saved_cr
+                .draw_ctx
+                .set_paint_source(&fill_paint, acquired_nodes)
+                .map(|had_paint_server| {
+                    if had_paint_server {
+                        pangocairo::functions::update_layout(&cr, &span.layout);
+                        pangocairo::functions::show_layout(&cr, &span.layout);
+                    };
+                })?;
+        }
 
         // Stroke
 
-        let paint_source = values
-            .stroke()
-            .0
-            .resolve(acquired_nodes, values.stroke_opacity().0, values.color().0)
-            .to_user_space(&bbox, &view_params, values);
+        let stroke_paint = span.stroke_paint.to_user_space(&bbox, &view_params, values);
 
         saved_cr
             .draw_ctx
-            .set_paint_source(&paint_source, acquired_nodes)
+            .set_paint_source(&stroke_paint, acquired_nodes)
             .map(|had_paint_server| {
                 if had_paint_server {
-                    pangocairo::functions::update_layout(&cr, &layout);
-                    pangocairo::functions::layout_path(&cr, &layout);
+                    pangocairo::functions::update_layout(&cr, &span.layout);
+                    pangocairo::functions::layout_path(&cr, &span.layout);
 
                     let (x0, y0, x1, y1) = cr.stroke_extents();
                     let r = Rect::new(x0, y0, x1, y1);
@@ -1442,7 +1426,7 @@ impl DrawingCtx {
                         .with_transform(transform)
                         .with_ink_rect(r);
                     bbox.insert(&ib);
-                    if values.is_visible() {
+                    if span.is_visible {
                         cr.stroke();
                     }
                 }
diff --git a/src/layout.rs b/src/layout.rs
index 958fbd54..23835d1e 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -16,7 +16,7 @@ use crate::path_builder::Path;
 use crate::properties::ComputedValues;
 use crate::property_defs::{
     ClipRule, FillRule, Filter, MixBlendMode, Opacity, Overflow, PaintOrder, ShapeRendering,
-    StrokeDasharray, StrokeLinecap, StrokeLinejoin, StrokeMiterlimit,
+    StrokeDasharray, StrokeLinecap, StrokeLinejoin, StrokeMiterlimit, TextRendering,
 };
 use crate::rect::Rect;
 use crate::surface_utils::shared_surface::SharedImageSurface;
@@ -86,6 +86,18 @@ pub struct Image {
     pub overflow: Overflow,
 }
 
+/// A single text span in user-space coordinates.
+pub struct TextSpan {
+    pub layout: pango::Layout,
+    pub is_visible: bool,
+    pub x: f64,
+    pub y: f64,
+    pub stroke: Stroke,
+    pub stroke_paint: PaintSource,
+    pub fill_paint: PaintSource,
+    pub text_rendering: TextRendering,
+}
+
 impl StackingContext {
     pub fn new(
         acquired_nodes: &mut AcquiredNodes<'_>,
diff --git a/src/text.rs b/src/text.rs
index 71b52e5b..58d1806d 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -10,7 +10,7 @@ use crate::drawing_ctx::DrawingCtx;
 use crate::element::{Draw, Element, ElementResult, SetAttributes};
 use crate::error::*;
 use crate::font_props::FontWeight;
-use crate::layout::StackingContext;
+use crate::layout::{self, StackingContext, Stroke};
 use crate::length::*;
 use crate::node::{CascadedValues, Node, NodeBorrow};
 use crate::parsers::ParseValue;
@@ -255,8 +255,41 @@ impl PositionedSpan {
         draw_ctx: &mut DrawingCtx,
         clipping: bool,
     ) -> Result<BoundingBox, RenderingError> {
+        let view_params = draw_ctx.get_view_params();
+        let params = NormalizeParams::new(&self.values, &view_params);
+
+        let layout = self.layout.clone();
+        let is_visible = self.values.is_visible();
         let (x, y) = self.rendered_position;
-        draw_ctx.draw_text(&self.layout, x, y, acquired_nodes, &self.values, clipping)
+
+        let stroke = Stroke::new(&self.values, &params);
+
+        let stroke_paint = self.values.stroke().0.resolve(
+            acquired_nodes,
+            self.values.stroke_opacity().0,
+            self.values.color().0,
+        );
+
+        let fill_paint = self.values.fill().0.resolve(
+            acquired_nodes,
+            self.values.fill_opacity().0,
+            self.values.color().0,
+        );
+
+        let text_rendering = self.values.text_rendering();
+
+        let span = layout::TextSpan {
+            layout,
+            is_visible,
+            x,
+            y,
+            stroke,
+            stroke_paint,
+            fill_paint,
+            text_rendering,
+        };
+
+        draw_ctx.draw_text_span(&view_params, &span, acquired_nodes, &self.values, clipping)
     }
 }
 


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