[librsvg/wip/rust-api: 6/11] librsvg_crate/examples/render.rs - Basic rendering example with the Rust API
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg/wip/rust-api: 6/11] librsvg_crate/examples/render.rs - Basic rendering example with the Rust API
- Date: Sat, 16 Feb 2019 01:34:55 +0000 (UTC)
commit 3939feb08142665b01d0eb18580af65202bb5c08
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Feb 13 17:48:26 2019 -0600
librsvg_crate/examples/render.rs - Basic rendering example with the Rust API
Doesn't build yet; we need to link the C dependencies by hand :(
librsvg_crate/Cargo.toml | 3 +++
librsvg_crate/examples/render.rs | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+)
---
diff --git a/librsvg_crate/Cargo.toml b/librsvg_crate/Cargo.toml
index 041f270b..16309072 100644
--- a/librsvg_crate/Cargo.toml
+++ b/librsvg_crate/Cargo.toml
@@ -13,3 +13,6 @@ glib = "0.6.0"
gio = { version="0.5.1", features=["v2_48"] } # per configure.ac
rsvg_internals = { path = "../rsvg_internals" }
url = "1.7.2"
+
+[dev-dependencies]
+cairo-rs = { version = "0.5.0", features = ["png"] }
diff --git a/librsvg_crate/examples/render.rs b/librsvg_crate/examples/render.rs
new file mode 100644
index 00000000..cefb2f3a
--- /dev/null
+++ b/librsvg_crate/examples/render.rs
@@ -0,0 +1,34 @@
+extern crate cairo;
+extern crate librsvg;
+
+use std::fs::File;
+use std::io::BufWriter;
+use std::process;
+
+fn main() {
+ let args = std::env::args_os();
+
+ if args.len() != 3 {
+ eprintln!("usage: render <input.svg> <output.png>");
+ process::exit(1);
+ }
+
+ let mut args = args.skip(1);
+
+ let input = args.next().unwrap();
+ let output = args.next().unwrap();
+
+ let handle = librsvg::LoadOptions::new().read_path(input).unwrap();
+
+ let renderer = handle.get_cairo_renderer();
+
+ let (w, h) = renderer.get_dimensions().unwrap();
+
+ let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, w, h).unwrap();
+ let cr = cairo::Context::new(&surface);
+ renderer.render(&cr).unwrap();
+
+ let mut file = BufWriter::new(File::create(output).unwrap());
+
+ surface.write_to_png(&mut file).unwrap();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]