Skip to content

Commit

Permalink
aded FreeSize for cut marks, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Apr 27, 2020
1 parent 5caef56 commit 934da2b
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd_examples/prep-help.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
..\pprep prep -h
pause
11 changes: 11 additions & 0 deletions cmd_examples_dev/prep-frame.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
..\target\release\pprep ^
--input ../test_data/*.png ^
--debug ^
prep ^
--output ../test_data/out/*-frame.png ^
--format 10cm/15cm ^
--padding 5mm/5mm/1cm/5mm ^
--margins 2mm ^
--cut-frame ./2mm ^
--dpi 300
pause
11 changes: 11 additions & 0 deletions cmd_examples_dev/prep-framed-size.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
..\target\release\pprep ^
--input ../test_data/*.png ^
--debug ^
prep ^
--output ../test_data/out/*-frame.png ^
--format 15cm/10cm ^
--framed-size 12cm/6cm ^
--padding 5mm/5mm/5mm/5mm ^
--cut-frame ./. ^
--dpi 300
pause
11 changes: 11 additions & 0 deletions cmd_examples_dev/prep-marks.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
..\target\release\pprep ^
--input ../test_data/*.png ^
--debug ^
prep ^
--output ../test_data/out/*-marks.png ^
--format 10cm/15cm ^
--padding 5mm/5mm/1cm/5mm ^
--margins 2mm ^
--cut-marks ./1mm ^
--dpi 300
pause
8 changes: 4 additions & 4 deletions src/op/prep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::cli::parse;
use crate::op::{ImageIoOperation, ImageOperation};
use crate::units::color::Color;
use crate::units::{format, LengthUnit, ScaleMode, Size};
use crate::units::{format, FreeSize, LengthUnit, ScaleMode};
use crate::units::{Borders, FixSize};
use crate::util::ImageUtil;
use image::imageops::FilterType;
Expand Down Expand Up @@ -37,11 +37,11 @@ pub struct PrepareImage {

/// Cut marks with offset. Format <line-width>/<offset>. Use alternative to `--cut-frame`.
#[structopt(name = "cut-marks", long)]
pub cut_marks: Option<Size>,
pub cut_marks: Option<FreeSize>,

/// Cut frame. Format <line-width>/<extend>. Use alternative to `--cut-marks`.
#[structopt(name = "cut-frame", long)]
pub cut_frame: Option<Size>,
pub cut_frame: Option<FreeSize>,

/// Cut marks or frame color. Default: black.
#[structopt(name = "cut-color", long)]
Expand Down Expand Up @@ -298,7 +298,7 @@ impl ImageIoOperation for PrepareImage {
};

// Calculates sizes, etc.
let (img, frame, padding, margins) =
let (img, _frame, padding, margins) =
self.calc_sizes(width, height, image.width(), image.height(), rotate, dpi);
let x_img = (margins.left().value() + padding.left().value()) as u32;
let y_img = (margins.top().value() + padding.top().value()) as u32;
Expand Down
1 change: 1 addition & 0 deletions src/units/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use length::LengthUnit;
pub use length::ToLength;

pub use size::FixSize;
pub use size::FreeSize;
pub use size::Size;

pub use scale::Scale;
Expand Down
88 changes: 88 additions & 0 deletions src/units/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,94 @@ impl fmt::Display for Size {
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct FreeSize {
width: Option<Length>,
height: Option<Length>,
}

impl FreeSize {
pub fn new(width: Option<Length>, height: Option<Length>) -> Result<Self, ParseStructError> {
Ok(FreeSize { width, height })
}
/// Width of this size.
pub fn width(&self) -> &Option<Length> {
&self.width
}
/// Height of this size.
pub fn height(&self) -> &Option<Length> {
&self.height
}
/// Converts this size to pixels.
pub fn to_px(&self, dpi: f64) -> Size {
self.to(&LengthUnit::Px, dpi)
}
/// Converts this size to another unit.
pub fn to(&self, unit: &LengthUnit, dpi: f64) -> Size {
Size {
width: self.width.as_ref().and_then(|w| Some(w.to(unit, dpi))),
height: self.height.as_ref().and_then(|w| Some(w.to(unit, dpi))),
}
}
/// Rotates this size by 90° clockwise (i.e. swaps width and height).
pub fn rotate_90(&self) -> Size {
Size::new(self.height.clone(), self.width.clone()).unwrap()
}
/// Does this size require a dpi value for conversion to px?
pub fn needs_dpi(&self) -> bool {
let mut needs = false;
if let Some(w) = &self.width {
if w.needs_dpi() {
needs = true;
}
};
if let Some(h) = &self.height {
if h.needs_dpi() {
needs = true;
}
};
needs
}
}

impl FromStr for FreeSize {
type Err = Box<dyn Error>;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<_> = s.split("/").collect();
if parts.len() != 2 {
return Err(Box::new(ParseStructError(format!(
"Unexpected size format in {}, expects `width/height`",
s
))));
}
let width = if parts[0] == "." {
None
} else {
Some(parts[0].parse()?)
};
let height = if parts[1] == "." {
None
} else {
Some(parts[1].parse()?)
};
Ok(FreeSize { width, height })
}
}
impl fmt::Display for FreeSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let str1 = self
.width
.as_ref()
.map_or(".".to_string(), |l| l.to_string());
let str2 = self
.height
.as_ref()
.map_or(".".to_string(), |l| l.to_string());
write!(f, "{}/{}", str1, str2)
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct FixSize {
width: Length,
Expand Down

0 comments on commit 934da2b

Please sign in to comment.