setTransformToFit documentation suggestion

I had to do this to position my SVG …

Even if you take out the centering stuff it’s still pretty bizarre unless you have a fairly deep understanding of what’s going on.

Could we get a simple example added to the docs for Drawable::setTransformToFit ?

static void positionDrawable(Drawable& drawable, float x, float centerY, float height, bool alignRight)
{
    auto b = drawable.getDrawableBounds();
    auto aspect = b.getAspectRatio();

    auto r = Rectangle(aspect * height, height).withCentre({0.0f, centerY});

    if (alignRight)
        r = r.withRightX(x);
    else
        r = r.withX(x);

    drawable.setTransformToFit(r, RectanglePlacement::centred);
    drawable.setBounds(b.getSmallestIntegerContainer());
}

Honestly there should be a way to do this in one step using the RectanglePlacement flags, but I can never figure it out without trial and error:

What is the opposite to RectanglePlacement::fillDestination i.e. letterboxed, maximum size without cropping while keeping the aspect ratio?
If possible it would be great to have an alias, like RectanglePlacement::centred is an alias.

Yeah, it needs a whole new function though, as the setTransformToFit/setBounds dance is really non-standard for something that ought to be simple!

Ok, interesting.

I have to admit, I only use drawable.setTransformToFit(Rectangle(x, y, width, height), Rectangleplacement) when I want to position an SVG.

Is it necessary to additionally use setBounds()?

But yes, I have seen that my SVGs are sometimes positioned a bit strangely (They can be a bit too small or slightly too big sometimes). So I have some trial and error, too.

Oh right, maybe I didn’t need setBounds at all…?

You don’t need setBounds to display the SVG.
But maybe it makes it easier to position it?
Hmmm…

IIUC you need the setBounds to control the drawables paint() method, while for the draw() you might not need it.
But in the past I had often unexpected results because of this duality between Drawable and Component. Seems the setBoundsToFit and other methods that modify the transformation moves the result out of the setBounds() area. I know that is to be expected, but I didn’t understand how it is used in a controlled manner (and unusally I have no trouble understanding matrices and transformations).

This is the second reason why I filed this feature request, feel free to support it here:

I agree.
Drawables are a bit confusing to position.

1 Like