1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
//! The `image` element.
use markup5ever::{expanded_name, local_name, namespace_url, ns};
use crate::aspect_ratio::AspectRatio;
use crate::bbox::BoundingBox;
use crate::document::AcquiredNodes;
use crate::drawing_ctx::DrawingCtx;
use crate::element::{set_attribute, ElementTrait};
use crate::error::*;
use crate::href::{is_href, set_href};
use crate::layout::{self, Layer, LayerKind, StackingContext};
use crate::length::*;
use crate::node::{CascadedValues, Node, NodeBorrow};
use crate::parsers::ParseValue;
use crate::rect::Rect;
use crate::session::Session;
use crate::xml::Attributes;
/// The `<image>` element.
///
/// Note that its x/y/width/height are properties in SVG2, so they are
/// defined as part of [the properties machinery](properties.rs).
#[derive(Default)]
pub struct Image {
aspect: AspectRatio,
href: Option<String>,
}
impl ElementTrait for Image {
fn set_attributes(&mut self, attrs: &Attributes, session: &Session) {
for (attr, value) in attrs.iter() {
match attr.expanded() {
expanded_name!("", "preserveAspectRatio") => {
set_attribute(&mut self.aspect, attr.parse(value), session)
}
// "path" is used by some older Adobe Illustrator versions
ref a if is_href(a) || *a == expanded_name!("", "path") => {
set_href(a, &mut self.href, Some(value.to_string()))
}
_ => (),
}
}
}
fn draw(
&self,
node: &Node,
acquired_nodes: &mut AcquiredNodes<'_>,
cascaded: &CascadedValues<'_>,
draw_ctx: &mut DrawingCtx,
clipping: bool,
) -> Result<BoundingBox, RenderingError> {
let surface = match self.href {
Some(ref url) => match acquired_nodes.lookup_image(url) {
Ok(surf) => surf,
Err(e) => {
rsvg_log!(
draw_ctx.session(),
"could not load image \"{}\": {}",
url,
e
);
return Ok(draw_ctx.empty_bbox());
}
},
None => return Ok(draw_ctx.empty_bbox()),
};
let values = cascaded.get();
let view_params = draw_ctx.get_view_params();
let params = NormalizeParams::new(values, &view_params);
let x = values.x().0.to_user(¶ms);
let y = values.y().0.to_user(¶ms);
let w = match values.width().0 {
LengthOrAuto::Length(l) => l.to_user(¶ms),
LengthOrAuto::Auto => surface.width() as f64,
};
let h = match values.height().0 {
LengthOrAuto::Length(l) => l.to_user(¶ms),
LengthOrAuto::Auto => surface.height() as f64,
};
let is_visible = values.is_visible();
let rect = Rect::new(x, y, x + w, y + h);
let overflow = values.overflow();
let image = Box::new(layout::Image {
surface,
is_visible,
rect,
aspect: self.aspect,
overflow,
});
let elt = node.borrow_element();
let stacking_ctx = StackingContext::new(
draw_ctx.session(),
acquired_nodes,
&elt,
values.transform(),
values,
);
let layer = Layer {
kind: LayerKind::Image(image),
stacking_ctx,
};
draw_ctx.draw_layer(&layer, acquired_nodes, values, clipping)
}
}
|