Skip to content

Commit

Permalink
fix crash on export if a chamber or dungeon is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
H4kor committed Jul 3, 2024
1 parent 24f5405 commit fdc1a86
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 34 deletions.
33 changes: 33 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ impl BitAndAssign<BBox> for BBox {
}
}

impl BBox {
pub fn is_valid(&self) -> bool {
self.min.x.is_finite()
&& self.min.y.is_finite()
&& self.max.x.is_finite()
&& self.max.y.is_finite()
}
}

#[cfg(test)]
mod tests {
use super::{BBox, Vec2};
Expand Down Expand Up @@ -206,4 +215,28 @@ mod tests {
assert_eq!(a.max.x, 6.0);
assert_eq!(a.max.y, 7.0);
}

#[test]
fn test_invalid_bbox() {
let a = BBox {
min: Vec2 {
x: 1.0 / 0.0,
y: 1.0 / 0.0,
},
max: Vec2 {
x: 1.0 / 0.0,
y: 1.0 / 0.0,
},
};
assert_eq!(a.is_valid(), false)
}

#[test]
fn test_valid_bbox() {
let a = BBox {
min: Vec2 { x: 1.0, y: 1.0 },
max: Vec2 { x: 1.0, y: 1.0 },
};
assert_eq!(a.is_valid(), true)
}
}
120 changes: 86 additions & 34 deletions src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ fn prims_to_bbox(prims: &Vec<Box<dyn Primitive>>) -> BBox {
fn draw_full_dungeon(dungeon: &Dungeon, ctx: &Context, include_hidden: bool) {
let all_prims = dungeon_to_primitives(dungeon, include_hidden);
let bbox = prims_to_bbox(&all_prims);
// early abort on empty dungeon
if !bbox.is_valid() {
return;
}

let mut cur_h = START_H;
let (_, tl) = layout_title();
Expand Down Expand Up @@ -355,41 +359,43 @@ fn chamber_headline(chamber: &Chamber) -> PdfElement {
bbox.min = bbox.min - Vec2 { x: 50.0, y: 50.0 };
bbox.max = bbox.max + Vec2 { x: 50.0, y: 50.0 };

let size = bbox.max - bbox.min;
let scale = IMAGE_SIZE / f64::max(size.x, size.y);
ctx.translate(
-bbox.min.x * scale + LEFT_SPACE,
-bbox.min.y * scale + cur_h,
);
ctx.scale(scale, scale);

let mut grid = Grid::new();
grid.color = Rgb {
r: 0.5,
g: 0.5,
b: 0.5,
};
grid.width = 1.0;

// set clipping
ctx.rectangle(bbox.min.x, bbox.min.y, size.x, size.y);
ctx.clip();
ctx.new_path();

// draw grid
ctx.set_dash(&vec![10.0, 10.0], 0.0);
for prim in grid.draw(bbox.min.into(), bbox.max.into()) {
prim.draw(&ctx)
if bbox.is_valid() {
let size = bbox.max - bbox.min;
let scale = IMAGE_SIZE / f64::max(size.x, size.y);
ctx.translate(
-bbox.min.x * scale + LEFT_SPACE,
-bbox.min.y * scale + cur_h,
);
ctx.scale(scale, scale);

let mut grid = Grid::new();
grid.color = Rgb {
r: 0.5,
g: 0.5,
b: 0.5,
};
grid.width = 1.0;

// set clipping
ctx.rectangle(bbox.min.x, bbox.min.y, size.x, size.y);
ctx.clip();
ctx.new_path();

// draw grid
ctx.set_dash(&vec![10.0, 10.0], 0.0);
for prim in grid.draw(bbox.min.into(), bbox.max.into()) {
prim.draw(&ctx)
}
ctx.set_dash(&vec![], 0.0);

// draw chamber
for prim in prims.iter() {
prim.draw(&ctx)
}

ctx.reset_clip();
ctx.identity_matrix();
}
ctx.set_dash(&vec![], 0.0);

// draw chamber
for prim in prims.iter() {
prim.draw(&ctx)
}

ctx.reset_clip();
ctx.identity_matrix();
}
}),
}
Expand Down Expand Up @@ -593,7 +599,14 @@ pub fn to_pdf(dungeon: &Dungeon, path: String) {
pub fn to_full_player_map_pdf(dungeon: &Dungeon, path: String) {
// Draw entire dungeon
let all_prims = dungeon_to_primitives(dungeon, false);
// early abort of dungeon is empty (nothing to draw)
if all_prims.len() == 0 {
return;
}
let bbox = prims_to_bbox(&all_prims);
if !bbox.is_valid() {
return;
}

let size = bbox.max - bbox.min;
// determine if page should be horizontal or vertical
Expand Down Expand Up @@ -657,3 +670,42 @@ pub fn to_full_player_map_pdf(dungeon: &Dungeon, path: String) {
ctx.identity_matrix();
ctx.show_page().unwrap();
}

#[cfg(test)]
mod test {
use crate::{chamber::Chamber, dungeon::Dungeon};

use super::{to_full_player_map_pdf, to_pdf};

#[test]
fn test_to_full_player_map_pdf_empty() {
let dungeon = &Dungeon::new();
to_full_player_map_pdf(
dungeon,
"/tmp/test_to_full_player_map_pdf_empty.pdf".to_string(),
)
}
#[test]
fn test_to_full_player_map_pdf_empty_chamber() {
let mut dungeon = Dungeon::new();
let chamber = Chamber::new();
dungeon.add_chamber(chamber);
to_full_player_map_pdf(
&dungeon,
"/tmp/test_to_full_player_map_pdf_empty_chamber.pdf".to_string(),
)
}

#[test]
fn test_to_pdf_empty() {
let dungeon = &Dungeon::new();
to_pdf(dungeon, "/tmp/test_to_pdf_empty.pdf".to_string())
}
#[test]
fn test_to_pdf_empty_chamber() {
let mut dungeon = Dungeon::new();
let chamber = Chamber::new();
dungeon.add_chamber(chamber);
to_pdf(&dungeon, "/tmp/test_to_pdf_empty_chamber.pdf".to_string())
}
}

0 comments on commit fdc1a86

Please sign in to comment.