Struct rsvg::Loader

source ·
pub struct Loader { /* private fields */ }
Expand description

Builder for loading an SvgHandle.

This is the starting point for using librsvg. This struct implements a builder pattern for configuring an SvgHandle’s options, and then loading the SVG data. You can call the methods of Loader in sequence to configure how SVG data should be loaded, and finally use one of the loading functions to load an SvgHandle.

Implementations§

source§

impl Loader

source

pub fn new() -> Self

Creates a Loader with the default flags.

  • unlimited_size defaults to false, as malicious SVG documents could cause the XML parser to consume very large amounts of memory.

  • keep_image_data defaults to false. You may only need this if rendering to Cairo surfaces that support including image data in compressed formats, like PDF.

§Example:
use rsvg;

let svg_handle = rsvg::Loader::new()
    .read_path("example.svg")
    .unwrap();
source

pub fn with_unlimited_size(self, unlimited: bool) -> Self

Controls safety limits used in the XML parser.

Internally, librsvg uses libxml2, which has set limits for things like the maximum length of XML element names, the size of accumulated buffers using during parsing of deeply-nested XML files, and the maximum size of embedded XML entities.

Set this to true only if loading a trusted SVG fails due to size limits.

§Example:
use rsvg;

let svg_handle = rsvg::Loader::new()
    .with_unlimited_size(true)
    .read_path("example.svg")    // presumably a trusted huge file
    .unwrap();
source

pub fn keep_image_data(self, keep: bool) -> Self

Controls embedding of compressed image data into the renderer.

Normally, Cairo expects one to pass it uncompressed (decoded) images as surfaces. However, when using a PDF rendering context to render SVG documents that reference raster images (e.g. those which include a bitmap as part of the SVG image), it may be more efficient to embed the original, compressed raster images into the PDF.

Set this to true if you are using a Cairo PDF context, or any other type of context which allows embedding compressed images.

§Example:
let svg_handle = rsvg::Loader::new()
    .keep_image_data(true)
    .read_path("example.svg")
    .unwrap();

let mut output = env::temp_dir();
output.push("output.pdf");
let surface = cairo::PdfSurface::new(640.0, 480.0, output)?;
let cr = cairo::Context::new(&surface).expect("Failed to create a cairo context");

let renderer = rsvg::CairoRenderer::new(&svg_handle);
renderer.render_document(
    &cr,
    &cairo::Rectangle::new(0.0, 0.0, 640.0, 480.0),
)?;
source

pub fn read_path<P: AsRef<Path>>( self, path: P ) -> Result<SvgHandle, LoadingError>

Reads an SVG document from path.

§Example:
let svg_handle = rsvg::Loader::new()
    .read_path("example.svg")
    .unwrap();
source

pub fn read_file<F: IsA<File>, P: IsA<Cancellable>>( self, file: &F, cancellable: Option<&P> ) -> Result<SvgHandle, LoadingError>

Reads an SVG document from a gio::File.

The cancellable can be used to cancel loading from another thread.

§Example:
let svg_handle = rsvg::Loader::new()
    .read_file(&gio::File::for_path("example.svg"), None::<&gio::Cancellable>)
    .unwrap();
source

pub fn read_stream<S: IsA<InputStream>, F: IsA<File>, P: IsA<Cancellable>>( self, stream: &S, base_file: Option<&F>, cancellable: Option<&P> ) -> Result<SvgHandle, LoadingError>

Reads an SVG stream from a gio::InputStream.

The base_file, if it is not None, is used to extract the base URL for this stream.

Reading an SVG document may involve resolving relative URLs if the SVG references things like raster images, or other SVG files. In this case, pass the base_file that correspondds to the URL where this SVG got loaded from.

The cancellable can be used to cancel loading from another thread.

§Example
use gio::prelude::*;

let file = gio::File::for_path("example.svg");

let stream = file.read(None::<&gio::Cancellable>).unwrap();

let svg_handle = rsvg::Loader::new()
    .read_stream(&stream, Some(&file), None::<&gio::Cancellable>)
    .unwrap();

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.