Skip to content

Commit 2db891d

Browse files
committed
Add basic error handling for images
1 parent 30463c6 commit 2db891d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

crates/krilla/src/chunk_container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct ChunkContainer {
5757
pub(crate) shading_functions: Vec<Chunk>,
5858
pub(crate) patterns: Vec<Chunk>,
5959
pub(crate) pages: Vec<Deferred<Chunk>>,
60-
pub(crate) images: Vec<Deferred<Chunk>>,
60+
pub(crate) images: Vec<Deferred<KrillaResult<Chunk>>>,
6161

6262
pub(crate) metadata: Option<Metadata>,
6363
}
@@ -149,7 +149,7 @@ impl ChunkContainer {
149149
($remapper:expr, $pdf:expr; $($field:expr),+) => {
150150
$(
151151
for chunk in $field {
152-
let chunk = chunk.wait();
152+
let chunk = chunk.wait().res()?;
153153
chunk.renumber_into($pdf, |old| *$remapper.get(&old).unwrap());
154154
}
155155
)+

crates/krilla/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! invalid fonts are provided. This module provides the basic error types krilla uses.
55
66
use crate::font::Font;
7+
use crate::image::Image;
78
use crate::validation::ValidationError;
89

910
/// A wrapper type for krilla errors.
@@ -22,4 +23,6 @@ pub enum KrillaError {
2223
///
2324
/// [`SerializeSettings`]: crate::SerializeSettings
2425
ValidationError(Vec<ValidationError>),
26+
/// An image couldn't be processed properly.
27+
ImageError(Image),
2528
}

crates/krilla/src/object/image.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// TODO: CLean up and update docs
1111

1212
use crate::color::{ICCBasedColorSpace, ICCProfile, ICCProfileWrapper, DEVICE_CMYK, DEVICE_RGB};
13+
use crate::error::{KrillaError, KrillaResult};
1314
use crate::object::color::DEVICE_GRAY;
1415
use crate::resource::RegisterableResource;
1516
use crate::serialize::SerializerContext;
@@ -233,7 +234,11 @@ impl Image {
233234
self.0.color_space()
234235
}
235236

236-
pub(crate) fn serialize(self, sc: &mut SerializerContext, root_ref: Ref) -> Deferred<Chunk> {
237+
pub(crate) fn serialize(
238+
self,
239+
sc: &mut SerializerContext,
240+
root_ref: Ref,
241+
) -> Deferred<KrillaResult<Chunk>> {
237242
let soft_mask_id = sc.new_ref();
238243
let icc_ref = self.icc().and_then(|ic| {
239244
if sc
@@ -260,7 +265,12 @@ impl Image {
260265
Deferred::new(move || {
261266
let mut chunk = Chunk::new();
262267

263-
let repr = self.0.inner.wait().as_ref().unwrap();
268+
let repr = self
269+
.0
270+
.inner
271+
.wait()
272+
.as_ref()
273+
.ok_or(KrillaError::ImageError(self.clone()))?;
264274

265275
let alpha_mask = match repr {
266276
Repr::Sampled(sampled) => sampled.mask_data.as_ref().map(|mask_data| {
@@ -323,7 +333,7 @@ impl Image {
323333
}
324334
image_x_object.finish();
325335

326-
chunk
336+
Ok(chunk)
327337
})
328338
}
329339
}

0 commit comments

Comments
 (0)