Add generate stickers per page
parent
d80952ac92
commit
ac26294893
|
|
@ -1,38 +1,76 @@
|
|||
use crate::config::Config;
|
||||
use crate::stickers::Sticker;
|
||||
use crate::stickers::*;
|
||||
|
||||
pub fn stickers_placement() -> Vec<Sticker> {
|
||||
#[derive(Debug)]
|
||||
pub struct Page {
|
||||
stickers: Vec<Sticker>,
|
||||
}
|
||||
|
||||
pub fn generate_page(sticker_details: Vec<StickerDetail>) -> Vec<Page> {
|
||||
let config = Config::load_a4();
|
||||
let mut pages: Vec<Page> = vec![];
|
||||
|
||||
let mut positions: Vec<Sticker> = vec![];
|
||||
let mut current_page = Page {
|
||||
stickers: vec![]
|
||||
};
|
||||
|
||||
let sticker_width = 50.0;
|
||||
let sticker_height = 50.0;
|
||||
for sticker_detail in sticker_details {
|
||||
let total_width = total_width(&config, sticker_detail.width);
|
||||
let total_height = total_height(&config, sticker_detail.height);
|
||||
|
||||
let usable_width: f64 = config.page_width_mm - 2.0 * config.bleed_mm;
|
||||
let usable_height: f64 = config.page_height_mm - 2.0 * config.bleed_mm;
|
||||
let total_width = sticker_width + config.spacing_mm;
|
||||
let total_height = sticker_height + config.spacing_mm;
|
||||
let rows = (usable_width(&config) / total_width).floor() as usize;
|
||||
let cols = (usable_height(&config) / total_height ).floor() as usize;
|
||||
|
||||
let stickers_per_page = rows * cols;
|
||||
|
||||
let row = (usable_width / total_width).floor() as usize;
|
||||
let col = (usable_height / total_height).floor() as usize;
|
||||
let mut remaining = sticker_detail.number;
|
||||
|
||||
for i in 0..col {
|
||||
for j in 0..row {
|
||||
let x = config.bleed_mm + (j as f64) * total_width;
|
||||
let y = config.bleed_mm + (i as f64) * total_height;
|
||||
while remaining > 0 {
|
||||
let index = current_page.stickers.len();
|
||||
if index == stickers_per_page {
|
||||
pages.push(current_page);
|
||||
current_page = Page { stickers: vec![] };
|
||||
}
|
||||
|
||||
positions.push(
|
||||
Sticker {
|
||||
path: String::from("Bonjour"),
|
||||
position: (x, y),
|
||||
width: sticker_width,
|
||||
height: sticker_height
|
||||
}
|
||||
);
|
||||
let i = index / cols;
|
||||
let j = index % cols;
|
||||
|
||||
let position_x = config.bleed_mm + (j as f64) * total_width;
|
||||
let position_y = config.bleed_mm + (i as f64) * total_height;
|
||||
|
||||
current_page.stickers.push(create_sticker(sticker_detail.clone(), position_x, position_y));
|
||||
remaining -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
positions
|
||||
if !current_page.stickers.is_empty() {
|
||||
pages.push(current_page);
|
||||
}
|
||||
|
||||
pages
|
||||
}
|
||||
|
||||
fn create_sticker(sticker_detail: StickerDetail, x: f64, y: f64) -> Sticker {
|
||||
Sticker {
|
||||
path: sticker_detail.path,
|
||||
position: (x, y),
|
||||
width: sticker_detail.width,
|
||||
height: sticker_detail.width
|
||||
}
|
||||
}
|
||||
|
||||
fn usable_width(config: &Config) -> f64 {
|
||||
config.page_width_mm - 2.0 * config.bleed_mm
|
||||
}
|
||||
|
||||
fn usable_height(config: &Config) -> f64 {
|
||||
config.page_height_mm - 2.0 * config.bleed_mm
|
||||
}
|
||||
|
||||
fn total_width(config: &Config, width: f64) -> f64 {
|
||||
width + config.spacing_mm
|
||||
}
|
||||
|
||||
fn total_height(config: &Config, height: f64) -> f64 {
|
||||
height + config.spacing_mm
|
||||
}
|
||||
|
|
|
|||
24
src/main.rs
24
src/main.rs
|
|
@ -1,8 +1,24 @@
|
|||
use printstick::layout::stickers_placement;
|
||||
use printstick::stickers::Sticker;
|
||||
use printstick::layout::generate_page;
|
||||
use printstick::stickers::StickerDetail;
|
||||
use printstick::layout::Page;
|
||||
|
||||
fn main() {
|
||||
let positions: Vec<Sticker> = stickers_placement();
|
||||
let stickers_order: Vec<StickerDetail> = vec![
|
||||
StickerDetail {
|
||||
number: 52,
|
||||
path: String::from("/test"),
|
||||
height: 50.0,
|
||||
width: 50.0
|
||||
},
|
||||
StickerDetail {
|
||||
number: 10,
|
||||
path: String::from("/nouveau_sticker"),
|
||||
height: 40.0,
|
||||
width: 30.0
|
||||
},
|
||||
];
|
||||
|
||||
println!("Generated positions: {:?}", positions);
|
||||
let pages: Vec<Page> = generate_page(stickers_order);
|
||||
|
||||
println!("Generated positions in one page: {:?}", pages);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
// Use for one stickers
|
||||
#[derive(Debug)]
|
||||
pub struct Sticker {
|
||||
pub path: String,
|
||||
|
|
@ -5,3 +6,12 @@ pub struct Sticker {
|
|||
pub width: f64,
|
||||
pub height: f64
|
||||
}
|
||||
|
||||
// User for many different sticker
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StickerDetail {
|
||||
pub number: u32,
|
||||
pub path: String,
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue