Lines
100 %
Functions
Branches
45.1 %
use rsvg::tests_only::{SharedImageSurface, SurfaceType};
use rsvg::{CairoRenderer, RenderingError};
use rsvg::test_utils::load_svg;
use rsvg::test_utils::reference_utils::{Compare, Evaluate, Reference};
#[test]
fn has_element_with_id_works() {
let svg = load_svg(
br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
<rect id="foo" x="10" y="10" width="30" height="30"/>
</svg>
"#,
)
.unwrap();
assert!(svg.has_element_with_id("#foo").unwrap());
assert!(!svg.has_element_with_id("#bar").unwrap());
assert!(matches!(
svg.has_element_with_id(""),
Err(RenderingError::InvalidId(_))
));
svg.has_element_with_id("not a fragment"),
svg.has_element_with_id("notfragment#fragment"),
}
fn render_layer() {
br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<rect id="foo" x="10" y="10" width="30" height="30" fill="#00ff00"/>
<rect id="bar" x="20" y="20" width="30" height="30" fill="#0000ff"/>
"##,
let renderer = CairoRenderer::new(&svg);
let output = cairo::ImageSurface::create(cairo::Format::ARgb32, 300, 300).unwrap();
let res = {
let cr = cairo::Context::new(&output).expect("Failed to create cairo context");
let viewport = cairo::Rectangle::new(100.0, 100.0, 100.0, 100.0);
renderer.render_layer(&cr, Some("#bar"), &viewport)
};
let output_surf = res
.map(|_| SharedImageSurface::wrap(output, SurfaceType::SRgb).unwrap())
let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 300, 300).unwrap();
{
let cr = cairo::Context::new(&reference_surf).expect("Failed to create a cairo context");
cr.translate(100.0, 100.0);
cr.rectangle(20.0, 20.0, 30.0, 30.0);
cr.set_source_rgba(0.0, 0.0, 1.0, 1.0);
cr.fill().unwrap();
Reference::from_surface(reference_surf)
.compare(&output_surf)
.evaluate(&output_surf, "render_layer");
fn untransformed_element() {
// This has a rectangle inside a transformed group. The rectangle
// inherits its stroke-width from the group.
//
// The idea is that we'll be able to extract the geometry of the rectangle
// as if it were not transformed by its ancestors, but still retain the
// cascade from the ancestors.
<g transform="rotate(45)" stroke-width="10" stroke="#000000">
<rect id="foo" x="10" y="20" width="30" height="40" fill="#0000ff"/>
</g>
/* Measuring */
let (ink_r, logical_r) = renderer.geometry_for_element(Some("#foo")).unwrap();
assert_eq!(ink_r, cairo::Rectangle::new(0.0, 0.0, 40.0, 50.0));
assert_eq!(logical_r, cairo::Rectangle::new(5.0, 5.0, 30.0, 40.0));
/* Rendering */
renderer.render_element(&cr, Some("#foo"), &viewport)
cr.rectangle(10.0, 10.0, 60.0, 80.0);
cr.fill_preserve().unwrap();
cr.set_line_width(20.0);
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
cr.stroke().unwrap();
.evaluate(&output_surf, "untransformed_element");
fn set_stylesheet() {
// This has a rectangle which we style from a user-supplied stylesheet.
let mut svg = load_svg(
<rect id="foo" x="10" y="20" width="30" height="40" fill="black"/>
svg.set_stylesheet("rect { fill: #00ff00; }")
.expect("should be a valid stylesheet");
let output = cairo::ImageSurface::create(cairo::Format::ARgb32, 100, 100).unwrap();
let viewport = cairo::Rectangle::new(0.0, 0.0, 100.0, 100.0);
renderer.render_document(&cr, &viewport)
let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 100, 100).unwrap();
cr.rectangle(10.0, 20.0, 30.0, 40.0);
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
.evaluate(&output_surf, "set_stylesheet");
// https://gitlab.gnome.org/GNOME/librsvg/-/issues/799
fn text_doesnt_leave_points_in_current_path() {
<text>Hello world!</text>
let cr = cairo::Context::new(&output).unwrap();
assert!(!cr.has_current_point().unwrap());
renderer.render_document(&cr, &viewport).unwrap();