Skip to content

Commit

Permalink
[examples][rasterize] support for specifying custom bounding box
Browse files Browse the repository at this point in the history
  • Loading branch information
aslpavel committed Mar 17, 2024
1 parent 598fb40 commit d43100f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rasterize"
version = "0.3.7"
version = "0.3.8"
authors = ["Pavel Aslanov <[email protected]>"]
description = "Simple and small 2D rendering library"
edition = "2021"
Expand Down
23 changes: 18 additions & 5 deletions examples/rasterize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Args {
tr: Transform,
fg: Option<LinColor>,
bg: Option<LinColor>,
bbox: Option<BBox>,
}

impl Args {
Expand All @@ -54,6 +55,7 @@ impl Args {
tr: Transform::identity(),
fg: None,
bg: None,
bbox: None,
};
let mut positional = 0;
let mut args = env::args();
Expand All @@ -68,6 +70,10 @@ impl Args {
let width = args.next().ok_or("-w requires argument")?;
result.width = Some(width.parse()?);
}
"-b" => {
let bbox = args.next().ok_or("-b requires argument")?;
result.bbox = Some(bbox.parse()?);
}
"-t" => {
result.tr = args.next().ok_or("-t requires argument")?.parse()?;
}
Expand Down Expand Up @@ -118,11 +124,12 @@ impl Args {
);
eprintln!("\nUSAGE:");
eprintln!(
" {} [-w <width>] [-s <stroke>] [-f <flatness>] [-o] [-a] [-fg <color>] [-bg <color>] <file.path> <out.bmp>",
" {} [-w <width>] [-b <bbox>] [-s <stroke>] [-f <flatness>] [-o] [-a] [-fg <color>] [-bg <color>] <file.path> <out.bmp>",
cmd
);
eprintln!("\nARGS:");
eprintln!(" -w <width> width in pixels of the output image");
eprintln!(" -b <bbox> custom bounding box");
eprintln!(" -t <transform> apply transform");
eprintln!(" -s <stroke_width> stroke path before rendering");
eprintln!(" -o show outline with control points instead of filling");
Expand Down Expand Up @@ -246,7 +253,10 @@ fn main() -> Result<(), Error> {
// transform if needed
let tr = match args.width {
Some(width) if width > 2 => {
let src_bbox = path.bbox(args.tr).ok_or("path is empty")?;
let src_bbox = match args.bbox {
Some(bbox) => bbox.transform(args.tr),
None => path.bbox(args.tr).ok_or("path is empty")?,
};
let width = width as Scalar;
let height = src_bbox.height() * width / src_bbox.width();
let dst_bbox = BBox::new(Point::new(1.0, 1.0), Point::new(width - 1.0, height - 1.0));
Expand Down Expand Up @@ -274,9 +284,12 @@ fn main() -> Result<(), Error> {
let scene = Scene::group(group);

// add background or checkerboard
let bbox = scene
.bbox(Transform::identity())
.ok_or("nothing to render")?;
let bbox = match args.bbox {
Some(bbox) => bbox.transform(tr),
None => scene
.bbox(Transform::identity())
.ok_or("nothing to render")?,
};
let bbox = BBox::new((bbox.x().round(), bbox.y().round()), bbox.max());
let (scene, bg) = match args.bg {
None => {
Expand Down
9 changes: 9 additions & 0 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,15 @@ impl BBox {
pub fn unit_transform(&self) -> Transform {
Transform::new_translate(self.x(), self.y()).pre_scale(self.width(), self.height())
}

/// Compute new bounding box such that it will include original bounding box after transformation
pub fn transform(&self, tr: Transform) -> BBox {
let p00 = tr.apply(self.min);
let p01 = tr.apply(Point::new(self.min.x(), self.max.y()));
let p10 = tr.apply(Point::new(self.max.x(), self.min.y()));
let p11 = tr.apply(self.max);
BBox::new(p00, p11).extend(p10).extend(p01)
}
}

/// Find intersection of two ranges
Expand Down
2 changes: 1 addition & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Default for LineCap {
}

/// Style used to generate stroke
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StrokeStyle {
/// Width of the stroke
Expand Down
4 changes: 2 additions & 2 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<I: Read> SvgPathParser<I> {
self.parser.unparse_byte(byte);
match self.prev_op {
Some(op) => Ok(Some(op)),
None => Err(SvgParserError::InvalidCmd(op)),
None => Err(SvgParserError::InvalidCmd(op.into())),
}
}
}
Expand Down Expand Up @@ -561,7 +561,7 @@ impl FromStr for Transform {
#[derive(Debug)]
pub enum SvgParserError {
/// Failed to parse SVG command
InvalidCmd(u8),
InvalidCmd(char),
/// Failed to parse scalar value
InvalidScalar,
/// Failed to parse flag value
Expand Down

0 comments on commit d43100f

Please sign in to comment.