common/box: add box_fit_within()

Factor out common math from ssd's get_scale_box() for use elsewhere.
This commit is contained in:
John Lindgren 2024-10-05 09:45:48 -04:00
parent 465aac5514
commit 22e50aa4e2
3 changed files with 46 additions and 28 deletions

View file

@ -47,3 +47,29 @@ box_union(struct wlr_box *box_dest, struct wlr_box *box_a, struct wlr_box *box_b
box_dest->width = x2 - x1;
box_dest->height = y2 - y1;
}
struct wlr_box
box_fit_within(int width, int height, int max_width, int max_height)
{
struct wlr_box box;
if (width <= max_width && height <= max_height) {
/* No downscaling needed */
box.width = width;
box.height = height;
} else if (width * max_height > height * max_width) {
/* Wider content, fit width */
box.width = max_width;
box.height = (height * max_width + (width / 2)) / width;
} else {
/* Taller content, fit height */
box.width = (width * max_height + (height / 2)) / height;
box.height = max_height;
}
/* Compute centered position */
box.x = (max_width - box.width) / 2;
box.y = (max_height - box.height) / 2;
return box;
}