Better spacing and positioning. Remove the logo

This commit is contained in:
Alexis Métaireau 2025-04-04 14:05:47 +02:00
parent e0adb40880
commit 8789709aec
No known key found for this signature in database
GPG key ID: 1C21B876828E5FF2
3 changed files with 1590 additions and 153 deletions

1678
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@ name = "jardiflore"
path = "main.rs" path = "main.rs"
[dependencies] [dependencies]
Inflector = "0.11.4"
ab_glyph = "0.2.29" ab_glyph = "0.2.29"
calamine = "0.26.1" calamine = "0.26.1"
image = "0.25.6" image = "0.25.6"

64
main.rs
View file

@ -11,6 +11,8 @@ use ab_glyph::{FontRef, PxScale};
use image::{ImageBuffer, Rgba}; use image::{ImageBuffer, Rgba};
use imageproc::drawing::draw_text_mut; use imageproc::drawing::draw_text_mut;
use inflector::Inflector;
// A4 dimensions // A4 dimensions
const A4_WIDTH: f32 = 210.0; const A4_WIDTH: f32 = 210.0;
const A4_HEIGHT: f32 = 297.0; const A4_HEIGHT: f32 = 297.0;
@ -18,28 +20,28 @@ const A4_HEIGHT: f32 = 297.0;
// Configuration for the label // Configuration for the label
const DPI: f32 = 300.0; const DPI: f32 = 300.0;
const MM_TO_PX: f32 = DPI / 25.4; const MM_TO_PX: f32 = DPI / 25.4;
const PX_TO_PT: f32 = 0.75;
const LABEL_WIDTH: f32 = 210.0 * MM_TO_PX; const LABEL_WIDTH: f32 = 210.0 * MM_TO_PX;
const LABEL_HEIGHT: f32 = 16.0 * MM_TO_PX; const LABEL_HEIGHT: f32 = 16.0 * MM_TO_PX;
// Elements positionning // Elements positionning
const MARGIN_LEFT: f32 = 72.0 * MM_TO_PX; const MARGIN_LEFT: f32 = 80.0 * MM_TO_PX;
const LOGO_WIDTH: u32 = 151; const MARGIN_RIGHT: f32 = 10.0 * MM_TO_PX;
const LOGO_HEIGHT: u32 = 64; // const LOGO_WIDTH: u32 = 151;
const LOGO_POS_X: u32 = LABEL_WIDTH as u32 - LOGO_WIDTH - 10; // const LOGO_HEIGHT: u32 = 64;
const LOGO_POS_Y: u32 = 10; // const LOGO_POS_X: u32 = LABEL_WIDTH as u32 - LOGO_WIDTH - 10;
// const LOGO_POS_Y: u32 = 10;
const CLIENT_CODE_POS_X: i32 = MARGIN_LEFT as i32 + 10; const CLIENT_CODE_POS_X: i32 = MARGIN_LEFT as i32;
const CLIENT_CODE_POS_Y: i32 = 10; const CLIENT_CODE_POS_Y: i32 = 10;
const CLIENT_CODE_SCALE: f32 = 24.0; const CLIENT_CODE_SCALE: f32 = 100.0;
const SPECIES_VARIETY_POS_X: i32 = MARGIN_LEFT as i32 + 10; const SPECIES_VARIETY_POS_X: i32 = MARGIN_LEFT as i32 + 220;
const SPECIES_VARIETY_POS_Y: i32 = 80; const SPECIES_VARIETY_POS_Y: i32 = 20;
const SPECIES_VARIETY_SCALE: f32 = 24.0; const SPECIES_VARIETY_SCALE: f32 = 80.0;
const DATE_POS_X: i32 = MARGIN_LEFT as i32 + 10; const DATE_POS_X: i32 = LABEL_WIDTH as i32 - MARGIN_RIGHT as i32 - 150;
const DATE_POS_Y: i32 = 40; const DATE_POS_Y: i32 = 10;
const DATE_SCALE: f32 = 32.0; const DATE_SCALE: f32 = 75.0;
const COLOR_TEXT: Rgba<u8> = Rgba([0, 0, 0, 255]); const COLOR_TEXT: Rgba<u8> = Rgba([0, 0, 0, 255]);
const COLOR_BACKGROUND: Rgba<u8> = Rgba([255, 255, 255, 255]); const COLOR_BACKGROUND: Rgba<u8> = Rgba([255, 255, 255, 255]);
@ -95,10 +97,10 @@ fn parse_xlsx(delivery_week_filter: &str) -> Result<Vec<Record>, Box<dyn std::er
} }
Some(Record { Some(Record {
client_code: get_cell_value(row, "client_code"), client_code: get_cell_value(row, "client_code").to_uppercase(),
species: get_cell_value(row, "species"), species: get_cell_value(row, "species").to_title_case(),
variety: get_cell_value(row, "variety"), variety: get_cell_value(row, "variety").to_title_case(),
delivery_week, delivery_week: delivery_week.to_uppercase(),
}) })
}) })
.collect(); .collect();
@ -109,7 +111,6 @@ fn parse_xlsx(delivery_week_filter: &str) -> Result<Vec<Record>, Box<dyn std::er
fn generate_label(record: Record) -> ImageBuffer<Rgba<u8>, Vec<u8>> { fn generate_label(record: Record) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
let font_data = include_bytes!("./assets/DejaVuSans.ttf"); let font_data = include_bytes!("./assets/DejaVuSans.ttf");
let font = FontRef::try_from_slice(font_data).unwrap(); let font = FontRef::try_from_slice(font_data).unwrap();
let logo = image::open("./assets/logo.png").unwrap();
let mut label: ImageBuffer<Rgba<u8>, Vec<u8>> = let mut label: ImageBuffer<Rgba<u8>, Vec<u8>> =
ImageBuffer::new(LABEL_WIDTH as u32, LABEL_HEIGHT as u32); ImageBuffer::new(LABEL_WIDTH as u32, LABEL_HEIGHT as u32);
@ -120,19 +121,20 @@ fn generate_label(record: Record) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
} }
// Logo // Logo
let resized_logo = image::imageops::resize( // let logo = image::open("./assets/logo.png").unwrap();
&logo, // let resized_logo = image::imageops::resize(
LOGO_WIDTH, // &logo,
LOGO_HEIGHT, // LOGO_WIDTH,
image::imageops::FilterType::Lanczos3, // LOGO_HEIGHT,
); // image::imageops::FilterType::Lanczos3,
// );
image::imageops::overlay( // image::imageops::overlay(
&mut label, // &mut label,
&resized_logo, // &resized_logo,
LOGO_POS_X as i64, // LOGO_POS_X as i64,
LOGO_POS_Y as i64, // LOGO_POS_Y as i64,
); // );
draw_text_mut( draw_text_mut(
&mut label, &mut label,