pub struct Loader {
unlimited_size: bool,
keep_image_data: bool,
session: Session,
}
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
.
Fields§
§unlimited_size: bool
§keep_image_data: bool
§session: Session
Implementations§
source§impl Loader
impl Loader
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a Loader
with the default flags.
-
unlimited_size
defaults tofalse
, as malicious SVG documents could cause the XML parser to consume very large amounts of memory. -
keep_image_data
defaults tofalse
. 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();
sourcepub fn new_with_session(session: Session) -> Self
pub fn new_with_session(session: Session) -> Self
Creates a Loader
from a pre-created Session
.
This is useful when a Loader
must be created by the C API, which should already
have created a session for logging.
sourcepub fn with_unlimited_size(self, unlimited: bool) -> Self
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();
sourcepub fn keep_image_data(self, keep: bool) -> Self
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),
)?;
sourcepub fn read_path<P: AsRef<Path>>(
self,
path: P,
) -> Result<SvgHandle, LoadingError>
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();
sourcepub fn read_file<F: IsA<File>, P: IsA<Cancellable>>(
self,
file: &F,
cancellable: Option<&P>,
) -> Result<SvgHandle, LoadingError>
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();
sourcepub 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>
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§
impl Freeze for Loader
impl RefUnwindSafe for Loader
impl Send for Loader
impl Sync for Loader
impl Unpin for Loader
impl UnwindSafe for Loader
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.