Provide a minimal loading API in librsvg_crate

parent cd848439
......@@ -590,6 +590,7 @@ version = "0.0.1"
dependencies = [
"cairo-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rsvg_internals 0.0.1",
"url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
......
......@@ -9,6 +9,7 @@ name = "librsvg"
[dependencies]
cairo-rs = "0.5.0"
glib = "0.6.0"
gio = { version="0.5.1", features=["v2_48"] } # per configure.ac
rsvg_internals = { path = "../rsvg_internals" }
url = "1.7.2"
#![warn(unused)]
extern crate gio;
extern crate glib;
extern crate rsvg_internals;
extern crate url;
use std::path::Path;
use gio::FileExt;
use glib::object::Cast;
use rsvg_internals::{Handle, LoadFlags};
use url::Url;
pub use rsvg_internals::{LoadingError, RenderingError};
/// Full configuration for loading an [`SvgHandle`][SvgHandle]
///
/// This struct implements a builder pattern for configuring an
/// [`SvgHandle`][SvgHandle]'s options, and then loading the SVG data.
/// You can call the methods of `LoadOptions` in sequence to configure
/// how SVG data should be loaded, and finally use one of the loading
/// functions to load an [`SvgHandle`][SvgHandle].
///
/// # Example:
///
/// ```ignore
/// extern crate librsvg;
///
/// use librsvg::LoadOptions;
///
/// let svg_handle = LoadOptions::new()
/// .unlimited_size()
/// .read_path("example.svg")
/// .unwrap();
/// ```
///
/// [SvgHandle]: struct.SvgHandle.html
pub struct LoadOptions {
unlimited_size: bool,
......@@ -36,7 +65,24 @@ impl LoadOptions {
self.keep_image_data = keep;
self
}
}
pub struct SvgHandle {
fn load_flags(&self) -> LoadFlags {
LoadFlags {
unlimited_size: self.unlimited_size,
keep_image_data: self.keep_image_data,
}
}
pub fn read_path<P: AsRef<Path>>(self, path: P) -> Result<SvgHandle, LoadingError> {
let file = gio::File::new_for_path(path);
let stream = file.read(None)?;
let mut handle = Handle::new_with_flags(self.load_flags());
handle.construct_read_stream_sync(&stream.upcast(), Some(&file), None)?;
Ok(SvgHandle(handle))
}
}
pub struct SvgHandle(Handle);
......@@ -172,6 +172,12 @@ impl Handle {
}
}
pub fn new_with_flags(load_flags: LoadFlags) -> Handle {
let handle = Handle::new();
handle.load_flags.set(load_flags);
handle
}
fn set_base_url(&self, url: &str) {
if self.load_state.get() != LoadState::Start {
rsvg_g_warning(
......@@ -523,7 +529,7 @@ impl Handle {
self.construct_read_stream_sync(&stream.upcast(), Some(file), cancellable)
}
fn construct_read_stream_sync(
pub fn construct_read_stream_sync(
&mut self,
stream: &gio::InputStream,
base_file: Option<&gio::File>,
......
......@@ -44,6 +44,8 @@ pub use color::{rsvg_css_parse_color, ColorKind, ColorSpec};
pub use dpi::rsvg_rust_set_default_dpi_x_y;
pub use error::{LoadingError, RenderingError};
pub use handle::{
rsvg_handle_rust_close,
rsvg_handle_rust_free,
......@@ -74,6 +76,9 @@ pub use handle::{
rsvg_handle_rust_set_size_callback,
rsvg_handle_rust_set_testing,
rsvg_handle_rust_write,
Handle,
LoadFlags,
};
pub use pixbuf_utils::{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment