Skip to content

Commit

Permalink
A few more simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jun 13, 2023
1 parent e1cc04b commit 7721ed3
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions src/bin/spreet/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ fn main() {
// store them in a map. The keys are the SVG filenames without the `.svg` extension. The
// bitmapped SVGs will be added to the spritesheet, and the keys will be used as the unique
// sprite ids in the JSON index file.
let sprites = BTreeMap::from_iter(
get_svg_input_paths(&args.input, args.recursive)
.par_iter()
.map(|svg_path| {
if let Ok(svg) = load_svg(svg_path) {
(
sprite::sprite_name(svg_path, args.input.as_path()),
sprite::generate_pixmap_from_svg(&svg, pixel_ratio).unwrap(),
)
} else {
eprintln!("{svg_path:?}: not a valid SVG image");
std::process::exit(exitcode::DATAERR);
}
})
.collect::<Vec<(String, Pixmap)>>(),
);
let sprites = get_svg_input_paths(&args.input, args.recursive)
.par_iter()
.map(|svg_path| {
if let Ok(svg) = load_svg(svg_path) {
(
sprite::sprite_name(svg_path, args.input.as_path()),
sprite::generate_pixmap_from_svg(&svg, pixel_ratio).unwrap(),
)
} else {
eprintln!("{svg_path:?}: not a valid SVG image");
std::process::exit(exitcode::DATAERR);
}
})
.collect::<BTreeMap<String, Pixmap>>();

if sprites.is_empty() {
eprintln!("Error: no valid SVGs found in {:?}", args.input);
std::process::exit(exitcode::NOINPUT);
Expand All @@ -49,25 +48,24 @@ fn main() {
if args.unique {
spritesheet_builder.make_unique();
};
// Save the spritesheet and index file if the sprites could be packed successfully, or exit with
// an error code if not.
if let Some(spritesheet) = spritesheet_builder.generate() {
let spritesheet_path = format!("{}.png", args.output);
// Save the bitmapped spritesheet to a local PNG.
if let Err(e) = spritesheet.save_spritesheet(&spritesheet_path) {
eprintln!("Error: could not save spritesheet to {spritesheet_path} ({e})");
std::process::exit(exitcode::IOERR);
};
// Save the index file to a local JSON file with the same name as the spritesheet.
if let Err(e) = spritesheet.save_index(&args.output, args.minify_index_file) {
eprintln!(
"Error: could not save sprite index to {} ({e})",
args.output
);
std::process::exit(exitcode::IOERR);
};
} else {

// Generate sprite sheet
let Some(spritesheet) = spritesheet_builder.generate() else {
eprintln!("Error: could not pack the sprites within an area fifty times their size.");
std::process::exit(exitcode::DATAERR);
};

// Save the bitmapped spritesheet to a local PNG.
let file_prefix = args.output;
let spritesheet_path = format!("{file_prefix}.png");
if let Err(e) = spritesheet.save_spritesheet(&spritesheet_path) {
eprintln!("Error: could not save spritesheet to {spritesheet_path} ({e})");
std::process::exit(exitcode::IOERR);
};

// Save the index file to a local JSON file with the same name as the spritesheet.
if let Err(e) = spritesheet.save_index(&file_prefix, args.minify_index_file) {
eprintln!("Error: could not save sprite index to {file_prefix} ({e})");
std::process::exit(exitcode::IOERR);
};
}

0 comments on commit 7721ed3

Please sign in to comment.