#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Letterbox {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
pub fn fit_fractional(window_w: u32, window_h: u32, logical_w: u32, logical_h: u32) -> Letterbox {
let win_w = window_w.max(1) as f32;
let win_h = window_h.max(1) as f32;
let log_w = logical_w.max(1) as f32;
let log_h = logical_h.max(1) as f32;
let scale = (win_w / log_w).min(win_h / log_h);
let width = log_w * scale;
let height = log_h * scale;
Letterbox {
x: (win_w - width) * 0.5,
y: (win_h - height) * 0.5,
width,
height,
}
}
#[cfg(test)]
mod tests {
use super::*;
use wasm_bindgen_test::wasm_bindgen_test;
const W: u32 = 960;
const H: u32 = 540;
fn assert_close(a: f32, b: f32) {
assert!((a - b).abs() < 1e-3, "expected {b}, got {a}");
}
#[wasm_bindgen_test(unsupported = test)]
fn exact_fit_has_no_bars() {
let lb = fit_fractional(W, H, W, H);
assert_close(lb.x, 0.0);
assert_close(lb.y, 0.0);
assert_close(lb.width, 960.0);
assert_close(lb.height, 540.0);
}
#[wasm_bindgen_test(unsupported = test)]
fn double_size_fills_exactly() {
let lb = fit_fractional(1920, 1080, W, H);
assert_close(lb.x, 0.0);
assert_close(lb.y, 0.0);
assert_close(lb.width, 1920.0);
assert_close(lb.height, 1080.0);
}
#[wasm_bindgen_test(unsupported = test)]
fn wide_window_pillarboxes() {
let lb = fit_fractional(2000, 540, W, H);
assert_close(lb.height, 540.0);
assert_close(lb.width, 960.0);
assert_close(lb.x, (2000.0 - 960.0) * 0.5);
assert_close(lb.y, 0.0);
}
#[wasm_bindgen_test(unsupported = test)]
fn tall_window_letterboxes() {
let lb = fit_fractional(960, 1000, W, H);
assert_close(lb.width, 960.0);
assert_close(lb.height, 540.0);
assert_close(lb.x, 0.0);
assert_close(lb.y, (1000.0 - 540.0) * 0.5);
}
#[wasm_bindgen_test(unsupported = test)]
fn fractional_scale_fills_constraining_axis() {
let lb = fit_fractional(1000, 1000, W, H);
assert_close(lb.width, 1000.0);
assert_close(lb.height, 1000.0 * 540.0 / 960.0);
assert_close(lb.x, 0.0);
}
#[wasm_bindgen_test(unsupported = test)]
fn zero_size_window_is_safe() {
let lb = fit_fractional(0, 0, W, H);
assert!(lb.width > 0.0 && lb.height > 0.0);
}
}