[librsvg: 17/32] Move the HandleIsNotLoaded error to c_api
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 17/32] Move the HandleIsNotLoaded error to c_api
- Date: Fri, 4 Dec 2020 21:11:31 +0000 (UTC)
commit b55a5b1cb18678e5d9df09627a67ffc89d949aab
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Nov 26 15:48:23 2020 -0600
Move the HandleIsNotLoaded error to c_api
This is just for RsvgHandle's state keeping.
src/c_api/handle.rs | 57 +++++++++++++++++++++++++++++++++++++----------------
src/error.rs | 4 ----
2 files changed, 40 insertions(+), 21 deletions(-)
---
diff --git a/src/c_api/handle.rs b/src/c_api/handle.rs
index 140f84fe..c17fb73c 100644
--- a/src/c_api/handle.rs
+++ b/src/c_api/handle.rs
@@ -24,6 +24,7 @@
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::ffi::{CStr, CString};
+use std::fmt;
use std::ops;
use std::path::PathBuf;
use std::ptr;
@@ -53,9 +54,7 @@ use glib::types::instance_of;
use gobject_sys::{GEnumValue, GFlagsValue};
-use crate::api::{
- CairoRenderer, IntrinsicDimensions, Loader, LoadingError, RenderingError, SvgHandle,
-};
+use crate::api::{self, CairoRenderer, IntrinsicDimensions, Loader, LoadingError, SvgHandle};
use crate::{
length::RsvgLength,
@@ -70,6 +69,30 @@ use super::sizing::LegacySize;
include!(concat!(env!("OUT_DIR"), "/version.rs"));
+// This is basically the same as api::RenderingError but with extra cases for
+// the peculiarities of the C API.
+enum RenderingError {
+ RenderingError(api::RenderingError),
+
+ // The RsvgHandle is created, but hasn't been loaded yet.
+ HandleIsNotLoaded,
+}
+
+impl<T: Into<api::RenderingError>> From<T> for RenderingError {
+ fn from(e: T) -> RenderingError {
+ RenderingError::RenderingError(e.into())
+ }
+}
+
+impl fmt::Display for RenderingError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match *self {
+ RenderingError::RenderingError(ref e) => e.fmt(f),
+ RenderingError::HandleIsNotLoaded => write!(f, "SVG data is not loaded into handle"),
+ }
+ }
+}
+
mod handle_flags {
// The following is entirely stolen from the auto-generated code
// for GBindingFlags, from gtk-rs/glib/src/gobject/auto/flags.rs
@@ -859,7 +882,7 @@ impl CHandle {
fn has_sub(&self, id: &str) -> Result<bool, RenderingError> {
let handle = self.get_handle_ref()?;
- handle.has_element_with_id(id)
+ Ok(handle.has_element_with_id(id)?)
}
fn get_dimensions_or_empty(&self) -> RsvgDimensionData {
@@ -954,12 +977,12 @@ impl CHandle {
let renderer = self.make_renderer(&handle);
match id {
- Some(id) => renderer.geometry_for_layer(Some(id), &unit_rectangle()),
+ Some(id) => Ok(renderer.geometry_for_layer(Some(id), &unit_rectangle())?),
- None => renderer.legacy_document_size_in_pixels().map(|(w, h)| {
+ None => Ok(renderer.legacy_document_size_in_pixels().map(|(w, h)| {
let rect = cairo::Rectangle::from_size(w, h);
(rect, rect)
- }),
+ })?),
}
}
@@ -1004,7 +1027,7 @@ impl CHandle {
let dimensions = self.get_dimensions_sub(None)?;
if dimensions.width == 0 || dimensions.height == 0 {
- return empty_pixbuf();
+ return Ok(empty_pixbuf()?);
}
let surface = cairo::ImageSurface::create(
@@ -1020,7 +1043,7 @@ impl CHandle {
let surface = SharedImageSurface::wrap(surface, SurfaceType::SRgb)?;
- pixbuf_from_surface(&surface)
+ Ok(pixbuf_from_surface(&surface)?)
}
fn render_document(
@@ -1033,7 +1056,7 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let renderer = self.make_renderer(&handle);
- renderer.render_document(cr, viewport)
+ Ok(renderer.render_document(cr, viewport)?)
}
fn get_geometry_for_layer(
@@ -1044,9 +1067,9 @@ impl CHandle {
let handle = self.get_handle_ref()?;
let renderer = self.make_renderer(&handle);
- renderer
+ Ok(renderer
.geometry_for_layer(id, viewport)
- .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
+ .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))?)
}
fn render_layer(
@@ -1061,7 +1084,7 @@ impl CHandle {
let renderer = self.make_renderer(&handle);
- renderer.render_layer(cr, id, viewport)
+ Ok(renderer.render_layer(cr, id, viewport)?)
}
fn get_geometry_for_element(
@@ -1072,9 +1095,9 @@ impl CHandle {
let renderer = self.make_renderer(&handle);
- renderer
+ Ok(renderer
.geometry_for_element(id)
- .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))
+ .map(|(i, l)| (RsvgRectangle::from(i), RsvgRectangle::from(l)))?)
}
fn render_element(
@@ -1089,7 +1112,7 @@ impl CHandle {
let renderer = self.make_renderer(&handle);
- renderer.render_element(cr, id, element_viewport)
+ Ok(renderer.render_element(cr, id, element_viewport)?)
}
fn get_intrinsic_dimensions(&self) -> Result<IntrinsicDimensions, RenderingError> {
@@ -2157,7 +2180,7 @@ fn check_cairo_context(cr: &cairo::Context) -> Result<(), RenderingError> {
);
rsvg_g_warning(&msg);
- Err(RenderingError::Cairo(status))
+ Err(RenderingError::from(status))
}
}
diff --git a/src/error.rs b/src/error.rs
index 7708c357..c535998c 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -147,9 +147,6 @@ pub enum RenderingError {
/// Not enough memory was available for rendering.
// FIXME: right now this is only returned from pixbuf_utils.rs
OutOfMemory,
-
- /// Cannot occur from librsvg_crate; this is just for the C API.
- HandleIsNotLoaded,
}
impl From<DefsLookupErrorKind> for RenderingError {
@@ -168,7 +165,6 @@ impl fmt::Display for RenderingError {
match *self {
RenderingError::LimitExceeded(ref s) => write!(f, "limit exceeded: {}", s),
RenderingError::OutOfMemory => write!(f, "out of memory"),
- RenderingError::HandleIsNotLoaded => write!(f, "SVG data is not loaded into handle"),
RenderingError::Cairo(ref status) => write!(f, "cairo error: {:?}", status),
RenderingError::IdNotFound => write!(f, "element id not found"),
RenderingError::InvalidId(ref s) => write!(f, "invalid id: {:?}", s),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]