[librsvg] drawing_ctx: move set_source_paint_server method
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] drawing_ctx: move set_source_paint_server method
- Date: Tue, 1 Jan 2019 00:03:34 +0000 (UTC)
commit 790ec5b9113738e4469b02bdfa729f9e73931604
Author: Paolo Borelli <pborelli gnome org>
Date: Sun Dec 30 21:54:56 2018 +0100
drawing_ctx: move set_source_paint_server method
It is a bit unfortunate that we move more code in the
drawing_ctx module, but I think having the code as a
method is more clear
rsvg_internals/src/drawing_ctx.rs | 159 ++++++++++++++++++++++++----------
rsvg_internals/src/filters/context.rs | 28 +++---
rsvg_internals/src/paint_server.rs | 85 +-----------------
3 files changed, 127 insertions(+), 145 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 72a4c18b..21fe5315 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -16,11 +16,13 @@ use dpi::Dpi;
use error::RenderingError;
use filters;
use float_eq_cairo::ApproxEqCairo;
+use gradient::NodeGradient;
use handle::{self, RsvgHandle};
use length::Dasharray;
use mask::NodeMask;
use node::{CascadedValues, NodeType, RsvgNode};
-use paint_server::{self, PaintServer};
+use paint_server::{PaintServer, PaintSource};
+use pattern::NodePattern;
use rect::RectangleExt;
use state::{
ClipRule,
@@ -519,6 +521,82 @@ impl DrawingCtx {
}
}
+ fn set_color(
+ &self,
+ color: &cssparser::Color,
+ opacity: &UnitInterval,
+ current_color: &cssparser::RGBA,
+ ) {
+ let rgba = match *color {
+ cssparser::Color::RGBA(ref rgba) => rgba,
+ cssparser::Color::CurrentColor => current_color,
+ };
+
+ let &UnitInterval(o) = opacity;
+ self.get_cairo_context().set_source_rgba(
+ f64::from(rgba.red_f32()),
+ f64::from(rgba.green_f32()),
+ f64::from(rgba.blue_f32()),
+ f64::from(rgba.alpha_f32()) * o,
+ );
+ }
+
+ pub fn set_source_paint_server(
+ &mut self,
+ ps: &PaintServer,
+ opacity: &UnitInterval,
+ bbox: &BoundingBox,
+ current_color: &cssparser::RGBA,
+ ) -> Result<bool, RenderingError> {
+ let mut had_paint_server;
+
+ match *ps {
+ PaintServer::Iri {
+ ref iri,
+ ref alternate,
+ } => {
+ had_paint_server = false;
+
+ if let Some(acquired) = self.get_acquired_node(iri) {
+ let node = acquired.get();
+
+ if node.get_type() == NodeType::LinearGradient
+ || node.get_type() == NodeType::RadialGradient
+ {
+ had_paint_server = node.with_impl(|n: &NodeGradient| {
+ n.resolve_fallbacks_and_set_pattern(&node, self, opacity, bbox)
+ })?;
+ } else if node.get_type() == NodeType::Pattern {
+ had_paint_server = node.with_impl(|n: &NodePattern| {
+ n.resolve_fallbacks_and_set_pattern(&node, self, opacity, bbox)
+ })?;
+ }
+ }
+
+ if !had_paint_server && alternate.is_some() {
+ self.set_color(alternate.as_ref().unwrap(), opacity, current_color);
+ had_paint_server = true;
+ } else {
+ rsvg_log!(
+ "pattern \"{}\" was not found and there was no fallback alternate",
+ iri
+ );
+ }
+ }
+
+ PaintServer::SolidColor(color) => {
+ self.set_color(&color, opacity, current_color);
+ had_paint_server = true;
+ }
+
+ PaintServer::None => {
+ had_paint_server = false;
+ }
+ };
+
+ Ok(had_paint_server)
+ }
+
pub fn get_pango_context(&self) -> pango::Context {
let font_map = pangocairo::FontMap::get_default().unwrap();
let context = font_map.create_context().unwrap();
@@ -600,20 +678,14 @@ impl DrawingCtx {
let fill_opacity = &values.fill_opacity.0;
let res = if !clipping {
- paint_server::set_source_paint_server(
- self,
- &values.fill.0,
- fill_opacity,
- &bbox,
- current_color,
- )
- .and_then(|had_paint_server| {
- if had_paint_server {
- pangocairo::functions::update_layout(&cr, layout);
- pangocairo::functions::show_layout(&cr, layout);
- };
- Ok(())
- })
+ self.set_source_paint_server(&values.fill.0, fill_opacity, &bbox, current_color)
+ .and_then(|had_paint_server| {
+ if had_paint_server {
+ pangocairo::functions::update_layout(&cr, layout);
+ pangocairo::functions::show_layout(&cr, layout);
+ };
+ Ok(())
+ })
} else {
Ok(())
};
@@ -624,8 +696,7 @@ impl DrawingCtx {
let mut need_layout_path = clipping;
let res = if !clipping {
- paint_server::set_source_paint_server(
- self,
+ self.set_source_paint_server(
&values.stroke.0,
stroke_opacity,
&bbox,
@@ -705,41 +776,35 @@ impl DrawingCtx {
let fill_opacity = &values.fill_opacity.0;
- let res = paint_server::set_source_paint_server(
- self,
- &values.fill.0,
- fill_opacity,
- &bbox,
- current_color,
- )
- .and_then(|had_paint_server| {
- if had_paint_server {
- if values.stroke.0 == PaintServer::None {
- cr.fill();
- } else {
- cr.fill_preserve();
- }
- }
-
- Ok(())
- })
- .and_then(|_| {
- let stroke_opacity = values.stroke_opacity.0;
-
- paint_server::set_source_paint_server(
- self,
- &values.stroke.0,
- &stroke_opacity,
- &bbox,
- ¤t_color,
- )
+ let res = self
+ .set_source_paint_server(&values.fill.0, fill_opacity, &bbox, current_color)
.and_then(|had_paint_server| {
if had_paint_server {
- cr.stroke();
+ if values.stroke.0 == PaintServer::None {
+ cr.fill();
+ } else {
+ cr.fill_preserve();
+ }
}
+
Ok(())
})
- });
+ .and_then(|_| {
+ let stroke_opacity = values.stroke_opacity.0;
+
+ self.set_source_paint_server(
+ &values.stroke.0,
+ &stroke_opacity,
+ &bbox,
+ ¤t_color,
+ )
+ .and_then(|had_paint_server| {
+ if had_paint_server {
+ cr.stroke();
+ }
+ Ok(())
+ })
+ });
// clear the path in case stroke == fill == None; otherwise
// we leave it around from computing the bounding box
diff --git a/rsvg_internals/src/filters/context.rs b/rsvg_internals/src/filters/context.rs
index 5c31c48d..33f425d3 100644
--- a/rsvg_internals/src/filters/context.rs
+++ b/rsvg_internals/src/filters/context.rs
@@ -9,7 +9,7 @@ use coord_units::CoordUnits;
use drawing_ctx::DrawingCtx;
use length::Length;
use node::RsvgNode;
-use paint_server::{self, PaintServer};
+use paint_server::PaintServer;
use rect::IRect;
use state::ComputedValues;
use surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
@@ -438,19 +438,19 @@ impl FilterContext {
let bbox = draw_ctx.get_bbox().clone();
// FIXME: we are ignoring the following error; propagate it upstream
- let _ = paint_server::set_source_paint_server(
- draw_ctx,
- paint_server,
- &opacity,
- &bbox,
- &self.computed_from_node_being_filtered.color.0,
- )
- .and_then(|had_paint_server| {
- if had_paint_server {
- cr.paint();
- }
- Ok(())
- });
+ let _ = draw_ctx
+ .set_source_paint_server(
+ paint_server,
+ &opacity,
+ &bbox,
+ &self.computed_from_node_being_filtered.color.0,
+ )
+ .and_then(|had_paint_server| {
+ if had_paint_server {
+ cr.paint();
+ }
+ Ok(())
+ });
draw_ctx.set_cairo_context(&cr_save);
Ok(surface)
diff --git a/rsvg_internals/src/paint_server.rs b/rsvg_internals/src/paint_server.rs
index 34ff0d4d..366c2b13 100644
--- a/rsvg_internals/src/paint_server.rs
+++ b/rsvg_internals/src/paint_server.rs
@@ -4,10 +4,8 @@ use bbox::BoundingBox;
use defs::Fragment;
use drawing_ctx::DrawingCtx;
use error::*;
-use gradient::NodeGradient;
-use node::{NodeType, RsvgNode};
+use node::RsvgNode;
use parsers::Parse;
-use pattern::NodePattern;
use state::ComputedValues;
use unit_interval::UnitInterval;
@@ -51,26 +49,6 @@ impl Parse for PaintServer {
}
}
-fn set_color(
- draw_ctx: &mut DrawingCtx,
- color: &cssparser::Color,
- opacity: &UnitInterval,
- current_color: &cssparser::RGBA,
-) {
- let rgba = match *color {
- cssparser::Color::RGBA(ref rgba) => rgba,
- cssparser::Color::CurrentColor => current_color,
- };
-
- let &UnitInterval(o) = opacity;
- draw_ctx.get_cairo_context().set_source_rgba(
- f64::from(rgba.red_f32()),
- f64::from(rgba.green_f32()),
- f64::from(rgba.blue_f32()),
- f64::from(rgba.alpha_f32()) * o,
- );
-}
-
pub trait PaintSource<T> {
fn resolve(&self, node: &RsvgNode, draw_ctx: &mut DrawingCtx, bbox: &BoundingBox) -> Option<T>;
@@ -100,67 +78,6 @@ pub trait PaintSource<T> {
}
}
-pub fn set_source_paint_server(
- draw_ctx: &mut DrawingCtx,
- ps: &PaintServer,
- opacity: &UnitInterval,
- bbox: &BoundingBox,
- current_color: &cssparser::RGBA,
-) -> Result<bool, RenderingError> {
- let mut had_paint_server;
-
- match *ps {
- PaintServer::Iri {
- ref iri,
- ref alternate,
- } => {
- had_paint_server = false;
-
- if let Some(acquired) = draw_ctx.get_acquired_node(iri) {
- let node = acquired.get();
-
- if node.get_type() == NodeType::LinearGradient
- || node.get_type() == NodeType::RadialGradient
- {
- had_paint_server = node.with_impl(|n: &NodeGradient| {
- n.resolve_fallbacks_and_set_pattern(&node, draw_ctx, opacity, bbox)
- })?;
- } else if node.get_type() == NodeType::Pattern {
- had_paint_server = node.with_impl(|n: &NodePattern| {
- n.resolve_fallbacks_and_set_pattern(&node, draw_ctx, opacity, bbox)
- })?;
- }
- }
-
- if !had_paint_server && alternate.is_some() {
- set_color(
- draw_ctx,
- alternate.as_ref().unwrap(),
- opacity,
- current_color,
- );
- had_paint_server = true;
- } else {
- rsvg_log!(
- "pattern \"{}\" was not found and there was no fallback alternate",
- iri
- );
- }
- }
-
- PaintServer::SolidColor(color) => {
- set_color(draw_ctx, &color, opacity, current_color);
- had_paint_server = true;
- }
-
- PaintServer::None => {
- had_paint_server = false;
- }
- };
-
- Ok(had_paint_server)
-}
-
#[cfg(test)]
mod tests {
use super::*;
[Date Prev][
Date Next] [Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]