[librsvg: 8/15] state: move stroke width to rust



commit 64dacac8409523c706e218e74b8090a133a2c720
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Apr 7 12:15:41 2018 +0200

    state: move stroke width to rust

 librsvg/rsvg-styles.c        | 17 -----------------
 librsvg/rsvg-styles.h        |  5 -----
 rsvg_internals/src/draw.rs   |  9 ++++++++-
 rsvg_internals/src/marker.rs |  9 ++++++---
 rsvg_internals/src/state.rs  | 28 +++++++++++++++++++++++-----
 5 files changed, 37 insertions(+), 31 deletions(-)
---
diff --git a/librsvg/rsvg-styles.c b/librsvg/rsvg-styles.c
index 6f18e008..bc031d2d 100644
--- a/librsvg/rsvg-styles.c
+++ b/librsvg/rsvg-styles.c
@@ -103,7 +103,6 @@ rsvg_state_init (RsvgState * state)
     state->fill = rsvg_paint_server_parse (NULL, "#000");
     state->fill_opacity = 0xff;
     state->stroke_opacity = 0xff;
-    state->stroke_width = rsvg_length_parse ("1", LENGTH_DIR_BOTH);
 
     /* The following two start as INHERIT, even though has_stop_color and
      * has_stop_opacity get initialized to FALSE below.  This is so that the
@@ -139,7 +138,6 @@ rsvg_state_init (RsvgState * state)
     state->has_clip_rule = FALSE;
     state->has_stroke_server = FALSE;
     state->has_stroke_opacity = FALSE;
-    state->has_stroke_width = FALSE;
     state->has_dash = FALSE;
     state->has_dashoffset = FALSE;
     state->has_visible = FALSE;
@@ -307,8 +305,6 @@ rsvg_state_inherit_run (RsvgState * dst, const RsvgState * src,
     }
     if (function (dst->has_stroke_opacity, src->has_stroke_opacity))
         dst->stroke_opacity = src->stroke_opacity;
-    if (function (dst->has_stroke_width, src->has_stroke_width))
-        dst->stroke_width = src->stroke_width;
     if (function (dst->has_stop_color, src->has_stop_color)) {
         if (dst->stop_color.kind == RSVG_CSS_COLOR_SPEC_INHERIT) {
             dst->has_stop_color = TRUE;
@@ -670,13 +666,6 @@ rsvg_parse_style_pair (RsvgState *state,
     }
     break;
 
-    case RSVG_ATTRIBUTE_STROKE_WIDTH:
-    {
-        state->stroke_width = rsvg_length_parse (value, LENGTH_DIR_BOTH);
-        state->has_stroke_width = TRUE;
-    }
-    break;
-
     case RSVG_ATTRIBUTE_STROKE_OPACITY:
     {
         RsvgOpacitySpec spec;
@@ -1474,12 +1463,6 @@ rsvg_state_get_stroke_opacity (RsvgState *state)
     return state->stroke_opacity;
 }
 
-RsvgLength
-rsvg_state_get_stroke_width (RsvgState *state)
-{
-    return state->stroke_width;
-}
-
 gboolean
 rsvg_state_get_cond_true (RsvgState *state)
 {
diff --git a/librsvg/rsvg-styles.h b/librsvg/rsvg-styles.h
index dc756ee6..8e70136a 100644
--- a/librsvg/rsvg-styles.h
+++ b/librsvg/rsvg-styles.h
@@ -67,8 +67,6 @@ struct _RsvgState {
     gboolean has_stroke_server;
     guint8 stroke_opacity;      /* 0..255 */
     gboolean has_stroke_opacity;
-    RsvgLength stroke_width;
-    gboolean has_stroke_width;
 
     PangoWeight font_weight;
     gboolean has_font_weight;
@@ -181,9 +179,6 @@ RsvgPaintServer *rsvg_state_get_stroke (RsvgState *state);
 G_GNUC_INTERNAL
 guint8 rsvg_state_get_stroke_opacity (RsvgState *state);
 
-G_GNUC_INTERNAL
-RsvgLength rsvg_state_get_stroke_width (RsvgState *state);
-
 G_GNUC_INTERNAL
 gboolean rsvg_state_get_cond_true (RsvgState *state);
 
diff --git a/rsvg_internals/src/draw.rs b/rsvg_internals/src/draw.rs
index 415fa24a..b148b7fd 100644
--- a/rsvg_internals/src/draw.rs
+++ b/rsvg_internals/src/draw.rs
@@ -19,6 +19,7 @@ use state::{
     StrokeLinecap,
     StrokeLinejoin,
     StrokeMiterlimit,
+    StrokeWidth,
     TextRendering,
 };
 use text;
@@ -189,7 +190,13 @@ fn setup_cr_for_stroke(
 ) {
     let rstate = state::get_state_rust(state);
 
-    cr.set_line_width(state::get_stroke_width(state).normalize(draw_ctx));
+    cr.set_line_width(
+        rstate
+            .stroke_width
+            .as_ref()
+            .map_or_else(|| StrokeWidth::default().0, |w| w.0)
+            .normalize(draw_ctx),
+    );
     cr.set_miter_limit(
         rstate
             .stroke_miterlimit
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index 9fd9afbb..baeb6fd1 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -20,7 +20,7 @@ use parsers::{parse, Parse};
 use parsers::ParseError;
 use path_builder::*;
 use property_bag::PropertyBag;
-use state;
+use state::{self, StrokeWidth};
 use util::utf8_cstr;
 use viewbox::*;
 
@@ -676,8 +676,11 @@ pub fn render_markers_for_path_builder(
     clipping: bool,
 ) {
     let state = drawing_ctx::get_current_state(draw_ctx);
-
-    let line_width = state::get_stroke_width(state).normalize(draw_ctx);
+    let line_width = state::get_state_rust(state)
+        .stroke_width
+        .as_ref()
+        .map_or_else(|| StrokeWidth::default().0, |w| w.0)
+        .normalize(draw_ctx);
 
     if line_width.approx_eq_cairo(&0.0) {
         return;
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index a6ded039..39c78a97 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -49,6 +49,7 @@ pub struct State {
     pub stroke_line_cap: Option<StrokeLinecap>,
     pub stroke_line_join: Option<StrokeLinejoin>,
     pub stroke_miterlimit: Option<StrokeMiterlimit>,
+    pub stroke_width: Option<StrokeWidth>,
     pub text_anchor: Option<TextAnchor>,
     pub text_decoration: Option<TextDecoration>,
     pub text_rendering: Option<TextRendering>,
@@ -77,6 +78,7 @@ impl State {
             stroke_line_cap: Default::default(),
             stroke_line_join: Default::default(),
             stroke_miterlimit: Default::default(),
+            stroke_width: Default::default(),
             text_anchor: Default::default(),
             text_decoration: Default::default(),
             text_rendering: Default::default(),
@@ -145,6 +147,10 @@ impl State {
                 self.stroke_miterlimit = parse_property(value, ())?;
             }
 
+            Attribute::StrokeWidth => {
+                self.stroke_width = parse_property(value, LengthDir::Both)?;
+            }
+
             Attribute::TextAnchor => {
                 self.text_anchor = parse_property(value, ())?;
             }
@@ -217,7 +223,6 @@ extern "C" {
     fn rsvg_state_get_current_color(state: *const RsvgState) -> u32;
     fn rsvg_state_get_stroke(state: *const RsvgState) -> *const PaintServer;
     fn rsvg_state_get_stroke_opacity(state: *const RsvgState) -> u8;
-    fn rsvg_state_get_stroke_width(state: *const RsvgState) -> RsvgLength;
     fn rsvg_state_get_text_dir(state: *const RsvgState) -> pango_sys::PangoDirection;
     fn rsvg_state_get_text_gravity(state: *const RsvgState) -> pango_sys::PangoGravity;
     fn rsvg_state_get_font_weight(state: *const RsvgState) -> pango_sys::PangoWeight;
@@ -358,10 +363,6 @@ pub fn get_stroke_opacity(state: *const RsvgState) -> u8 {
     unsafe { rsvg_state_get_stroke_opacity(state) }
 }
 
-pub fn get_stroke_width(state: *const RsvgState) -> RsvgLength {
-    unsafe { rsvg_state_get_stroke_width(state) }
-}
-
 pub fn get_text_dir(state: *const RsvgState) -> pango::Direction {
     unsafe { from_glib(rsvg_state_get_text_dir(state)) }
 }
@@ -611,6 +612,22 @@ make_property!(
     newtype_from_str: f64
 );
 
+make_property!(
+    StrokeWidth,
+    default: RsvgLength::parse("1.0", LengthDir::Both).unwrap(),
+    inherits_automatically: true,
+    newtype: RsvgLength
+);
+
+impl Parse for StrokeWidth {
+    type Data = LengthDir;
+    type Err = AttributeError;
+
+    fn parse(s: &str, dir: LengthDir) -> Result<StrokeWidth, AttributeError> {
+        Ok(StrokeWidth(RsvgLength::parse(s, dir)?))
+    }
+}
+
 make_property!(
     TextAnchor,
     default: Start,
@@ -786,6 +803,7 @@ pub extern "C" fn rsvg_state_rust_inherit_run(
         &mut dst.stroke_miterlimit,
         &src.stroke_miterlimit,
     );
+    inherit(inherit_fn, &mut dst.stroke_width, &src.stroke_width);
     inherit(inherit_fn, &mut dst.text_anchor, &src.text_anchor);
     inherit(inherit_fn, &mut dst.text_decoration, &src.text_decoration);
     inherit(inherit_fn, &mut dst.text_rendering, &src.text_rendering);


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