Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to center the image, or atleast a functiom to determine the area the image would be rendered to #74

Open
TadoTheMiner opened this issue Jan 9, 2025 · 3 comments

Comments

@TadoTheMiner
Copy link
Contributor

Thanks to the new resize scale option, the image gets resized as desired.
image
Though as you can notice, the alignment of the image to the left is aesthetically unpleasing. If you know the width and height of the resized image, it can be centered easily:

fn center_image(image_dimensions: Rect, available_area: Rect) -> Rect {
    Rect {
        x: available_area.x + (available_area.width - image_dimensions.width) / 2,
        y: available_area.y + (available_area.height - image_dimensions.height) / 2,
        width: image_dimensions.width,
        height: image_dimensions.height,
    }
}

I could determine the image dimensions, but that would involve doing what the library already does - calculating what the image dimensions are (while I can divide the image dimensions with font size, that would return the dimensions of the original image and not of the resized). Either there should be a function that makes the image centered or atleast a function where you give it available area and it tells what will be the image dimensions

@benjajaja
Copy link
Owner

https://docs.rs/ratatui-image/4.2.0/ratatui_image/protocol/enum.Protocol.html#method.area should be exactly that, right?

It was a somewhat hidden addition recently, but I did mention it in the changelog https://github.com/benjajaja/ratatui-image/blob/master/CHANGELOG.md#410---2024-12-23-

@TadoTheMiner
Copy link
Contributor Author

Yeah but that works only for fixed image. I thought it was implied that I asked for such a function for StatefulProtocol. It has area function aswell but it apparently always returns Rect::default() (I mean it obviously shouldn't work as the area depends on the available area). What I am asking for is a method of StatefulProtocol that takes a rect as an input, and returns a rect of what would the image render to

@benjajaja
Copy link
Owner

Okay I understand this a bit better now. I'm on the fence between providing some AreaAlignment (not to be confused with crop-alignment option) similar to the Resize option, or adding a method like resized_area and let the user do whatever with that.

In the meantime, you could actually hack it yourself already, but with some types that I now think shouldn't actually be public: 8c6bf46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants