Add crop ratio presets for embedded presentation borders

This commit is contained in:
Dandaedre 2026-11-17 15:00:37 +01:00
parent 328bb722ff
commit 86762435cd
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
9 changed files with 277 additions and 3 deletions

2
Cargo.lock generated
View File

@ -548,7 +548,7 @@ dependencies = [
[[package]]
name = "canvas-pilot"
version = "0.9.28"
version = "0.9.29"
dependencies = [
"base64",
"craft-dwg",

View File

@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"]
resolver = "3"
[workspace.package]
version = "0.9.28"
version = "0.9.29"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot"

View File

@ -539,6 +539,11 @@ impl Editor {
}
if self.property_tab == PropertyTab::Crop {
ui.heading(tr(ui.ctx(), "ui.layer-crop-zoom-position"));
ui.add_enabled_ui(!d.pending(&self.layout), |ui| {
ui.group(|ui| {
if self.crop_preset_buttons(ui) { d = FormatDraft::new(&self.layout, i); }
});
});
let mut advanced_fit = self.layout.formats().layer_fit[i].is_some();
if ui.checkbox(&mut advanced_fit, tr(ui.ctx(), "ui.advanced-layer-fit")).changed() {
let mode = advanced_fit.then_some(canvas_pilot::formats::LayerFit::Centered);
@ -981,6 +986,43 @@ mod tests {
use super::super::tests::{click_text, frame, mouse};
use super::*;
#[test]
fn crop_properties_presets_resize_and_crop_together_and_respect_pending_edits() {
let ctx = egui::Context::default();
let mut app = Editor::default();
app.layout.select(Some(0));
app.sync();
app.property_tab = PropertyTab::Crop;
app.format_draft = Some(FormatDraft::new(&app.layout, 0));
let before = canvas_pilot::project::Project::from_layout(&app.layout);
click_text(&mut app, &ctx, "4:3");
let g = app.layout.layers()[0];
assert!((g.width / g.height - 4.0 / 3.0).abs() < 1e-9);
assert!(app.layout.formats().advanced[0].left > 0.0);
let after = canvas_pilot::project::Project::from_layout(&app.layout);
click_text(&mut app, &ctx, "4:3");
assert_eq!(
canvas_pilot::project::Project::from_layout(&app.layout),
after
);
app.layout.undo();
assert_eq!(
canvas_pilot::project::Project::from_layout(&app.layout),
before
);
let mut draft = FormatDraft::new(&app.layout, 0);
draft.source.native = [1280, 720];
app.format_draft = Some(draft);
click_text(&mut app, &ctx, "4:3");
assert_eq!(
canvas_pilot::project::Project::from_layout(&app.layout),
before
);
assert_eq!(
app.format_draft.as_ref().unwrap().source.native,
[1280, 720]
);
}
#[test]
fn enter_from_crop_or_mask_reviews_pending_properties_without_discarding() {
for tab in [PropertyTab::Crop, PropertyTab::Mask] {
let ctx = egui::Context::default();

View File

@ -2355,6 +2355,11 @@ impl Editor {
}
let selected = self.layout.selected_indices();
if !selected.is_empty() {
ui.menu_button("Crop presets", |ui| {
if self.crop_preset_buttons(ui) {
ui.close();
}
});
if ui.button(tr(ui.ctx(), "ui.copy-layer-values")).clicked() {
self.copy(ui.ctx());
ui.close();

View File

@ -103,3 +103,61 @@ impl Layout {
self.commit(next)
}
}
impl Layout {
/// Trim symmetric borders from the current picture and shrink its box around
/// its center. Matching ratios are exact no-ops, including existing crops.
pub fn apply_crop_preset(&mut self, aspect: crate::formats::Aspect) -> Result<(), String> {
use crate::formats::Aspect;
let ratio = match aspect {
Aspect::FourThree | Aspect::SixteenNine | Aspect::SixteenTen => aspect.ratio().unwrap(),
_ => return Err("Choose a 4:3, 16:9 or 16:10 crop preset.".into()),
};
let selected = self.selected_indices();
if selected.is_empty() {
return Err("Select at least one layer for a crop preset.".into());
}
let mut layers = self.layers.clone();
let mut formats = self.formats.clone();
for i in selected {
if !self.selectable(i) {
return Err("Unlock and show selected layers before cropping them.".into());
}
let old = layers[i];
if (old.width / old.height - ratio).abs() <= ratio * 1e-10 {
continue;
}
let width = old.width.min(old.height * ratio);
let height = old.height.min(old.width / ratio);
let next = Geometry {
x: old.x + (old.width - width) / 2.0,
y: old.y + (old.height - height) / 2.0,
width,
height,
}
.validate()?;
let input = formats.input(i)?;
let retained = formats.advanced[i].remaining(input)?;
let image = match formats.layer_fit[i] {
Some(mode) => mode.image(retained, old)?,
None => old,
};
let crop = &mut formats.advanced[i];
crop.left += ((next.x - image.x) / image.width * retained[0]).max(0.0);
crop.right +=
((image.x + image.width - next.x - width) / image.width * retained[0]).max(0.0);
crop.top += ((next.y - image.y) / image.height * retained[1]).max(0.0);
crop.bottom +=
((image.y + image.height - next.y - height) / image.height * retained[1]).max(0.0);
crop.remaining(input)?;
formats.mapping(i, next)?;
layers[i] = next;
}
self.finish_drag();
let before = self.snapshot();
self.layers = layers;
self.formats = formats;
self.remember(before);
Ok(())
}
}

View File

@ -79,7 +79,7 @@ fn paint_icon(ui: &egui::Ui, rect: Rect, icon: Icon, enabled: bool) {
}
impl Editor {
fn size_presets_enabled(&self) -> bool {
pub(super) fn size_presets_enabled(&self) -> bool {
!self.layout.selected_indices().is_empty()
&& !self.layout.dragging()
&& !self
@ -89,6 +89,27 @@ impl Editor {
&& !(self.fields_edited && self.fields_pending())
&& !self.ratio_edit.dirty
}
pub(super) fn crop_preset_buttons(&mut self, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label("Crop borders to ratio");
ui.horizontal(|ui| {
for aspect in [canvas_pilot::formats::Aspect::FourThree, canvas_pilot::formats::Aspect::SixteenNine, canvas_pilot::formats::Aspect::SixteenTen] {
if ui.add_enabled(self.size_presets_enabled(), egui::Button::new(aspect.label()))
.on_hover_text("Trim centered borders and shrink the layer; keep its center. Matching ratios do nothing.").clicked() {
match self.layout.apply_crop_preset(aspect) {
Ok(()) => {
if self.format_draft.is_some() {
self.format_draft = self.layout.selected().map(|i| FormatDraft::new(&self.layout, i));
}
self.sync(); changed = true;
}
Err(error) => self.error = error,
}
}
}
});
changed
}
fn apply_size_preset(&mut self, preset: SizePreset) {
if !self.size_presets_enabled() {
return;

View File

@ -219,3 +219,121 @@ fn preset_validation_and_hidden_locked_layers_cannot_partially_change_document()
.is_err()
);
}
#[test]
fn crop_presets_trim_current_picture_without_scaling_and_round_trip() {
use canvas_pilot::formats::{Aspect, Framing, LayerFit};
for fit in [
None,
Some(LayerFit::Fullscreen),
Some(LayerFit::Centered),
Some(LayerFit::Cropped),
Some(LayerFit::OneToOne),
] {
for (aspect, width, edge) in [
(Aspect::FourThree, 1440.0, 240.0),
(Aspect::SixteenTen, 1728.0, 96.0),
] {
let mut layout = Layout::default();
layout.select(Some(0));
layout.apply_size_preset(SizePreset::Fullscreen).unwrap();
layout.set_layer_fit(0, fit, Default::default()).unwrap();
let before = Project::from_layout(&layout);
layout.apply_crop_preset(aspect).unwrap();
let g = layout.layers()[0];
close(g.x, edge);
close(g.y, 0.0);
close(g.width, width);
close(g.height, 1080.0);
let f = layout.formats();
close(f.advanced[0].left, edge);
close(f.advanced[0].right, edge);
close(f.advanced[0].top, 0.0);
assert_eq!(f.layer_fit[0], fit);
let after = Project::from_layout(&layout);
assert_eq!(
Project::from_json(&after.to_json().unwrap()).unwrap(),
after
);
layout.apply_crop_preset(aspect).unwrap();
assert_eq!(Project::from_layout(&layout), after);
layout.undo();
assert_eq!(Project::from_layout(&layout), before);
layout.redo();
assert_eq!(Project::from_layout(&layout), after);
}
}
// Existing off-center framing is trimmed in place, not reset or recentered.
let mut layout = Layout::default();
layout.select(Some(0));
layout
.set_framing(
0,
Framing {
zoom: 2.0,
offset: [30.0, -10.0],
},
)
.unwrap();
let old = layout.layers()[0];
let crop = layout.formats().advanced[0];
let scale = layout.formats().mapping(0, old).unwrap().scale;
layout.apply_crop_preset(Aspect::FourThree).unwrap();
let new = layout.layers()[0];
close(layout.formats().mapping(0, new).unwrap().scale, scale);
close(
layout.formats().advanced[0].left,
crop.left + (old.width - new.width) / 2.0 / scale,
);
close(layout.formats().advanced[0].top, crop.top);
}
#[test]
fn crop_presets_letterbox_multi_selection_matching_ratio_and_errors() {
use canvas_pilot::formats::Aspect;
let mut layout = Layout::default();
layout.select(Some(0));
layout
.apply(
0,
Geometry {
x: 13.25,
y: 27.5,
width: 800.0,
height: 600.0,
},
)
.unwrap();
layout.set_selection([true, true]);
let before = Project::from_layout(&layout);
let same = layout.layers()[1];
let same_crop = layout.formats().advanced[1];
let old_crop = layout.formats().advanced[0];
let old_scale = layout
.formats()
.mapping(0, layout.layers()[0])
.unwrap()
.scale;
layout.apply_crop_preset(Aspect::SixteenNine).unwrap();
assert_eq!(
layout.layers()[0],
Geometry {
x: 13.25,
y: 102.5,
width: 800.0,
height: 450.0
}
);
close(
layout.formats().advanced[0].top,
old_crop.top + 75.0 / old_scale,
);
assert_eq!(layout.layers()[1], same);
assert_eq!(layout.formats().advanced[1], same_crop);
layout.undo();
assert_eq!(Project::from_layout(&layout), before);
assert!(layout.apply_crop_preset(Aspect::Custom).is_err());
assert_eq!(Project::from_layout(&layout), before);
layout.select(None);
assert!(layout.apply_crop_preset(Aspect::FourThree).is_err());
}

View File

@ -4,6 +4,13 @@
## Unreleased
### v0.9.29
- Added 4:3, 16:9 and 16:10 crop presets in Crop properties and the selected-layer
context menu. Trim symmetric embedded borders and shrink the layer around its
center together; matching ratios are no-ops. Multi-layer changes undo atomically,
preserve existing source/input settings and fit modes, and send no device writes.
### v0.9.28
- Added a toolbar icon box for selected-layer Fullscreen, Quarter, X/Y Tiling,

View File

@ -78,6 +78,29 @@ for equations and worked examples.
*The cyan helper frames the content while the visible layer box stays fixed. The header still uses the former Midra Layout name.*
## Crop ratio presets for embedded black borders
In **Layer properties → Crop**, use the **Crop borders to ratio** box: **4:3**,
**16:9** or **16:10**. These presets trim symmetric borders baked into a signal,
such as a 4:3 PowerPoint slide sent inside a 16:9 video resolution. They shrink
the visible layer around its center to the requested ratio and crop its picture
at the same time, rather than stretching the slide. The largest rectangle of
the chosen ratio that fits inside the current layer is retained.
For example, 4:3 on a full 1920×1080 layer removes 240 pixels from each side and
leaves 1440×1080. Conversely, 16:9 on an 800×600 layer removes 75 pixels from the
top and bottom and leaves 800×450. Already matching layers are unchanged,
including existing crop, and repeated clicks do not crop again.
Right-click selected layers in the viewport or layer list and choose **Crop
presets** to apply the same correction to several layers in one Undo step.
Existing crop is trimmed further; the presets do not detect black pixels or
restore previously removed content. Use Undo to compare a different preset, and
manual crop controls for asymmetric borders. Explicit layer-fit modes are retained
and fit the cropped picture into the new box. Shared input settings, source routing
and masks are unchanged. No device command is sent. Finish pending numeric or
property edits before applying a preset.
## Display/export precision and midpoint handles
Geometry in the viewport, inspector and calculation view is shown to the nearest