Drawable::setTransformToFit broken for SVGs

Hi Jules,

when using setTransformToFit on a Drawable that has been created from a SVG image it doesn’t seem to work. It works fine with Drawables created from a png image.

Adding the following code to the Juce Demo shows this:

// WidgetsDemo.cpp / ButtonPage (~ line 459)

ButtonPage()
[ // add this to constructor
  // load svg from desktop, only zipped as binary resource available
  d = Drawable::createFromImageFile (File::getSpecialLocation(File::userDesktopDirectory).getChildFile("svgfile.svg")); 
]

void resized()
{
  if (d != nullptr)
    d->setTransformToFit(juce::Rectangle<float> (0.5f*getWidth(), 0.5f*getHeight(), 2.5f*getWidth(), 2.5f*getHeight()), RectanglePlacement::centred);
}

void paint(Graphics& g)
{
  if (d != nullptr)
    d->draw(g, 0.5f);
}

ScopedPointer<Drawable> d;

Using Drawable::drawWithin works fine.

Chris

Most likely that method does work, but the SVG’s bounding box isn’t where you expect it to be?

I updated JUCE from 2.0.18 and it worked there.
I’m currently using this as a work around:

void resize()
[
  RectanglePlacement placement(RectanglePlacement::centred);
  svgTransform = placement.getTransformToFit (svg->getDrawableBounds(), r); // AffineTransform svgTransform;
]

void paint()
[
  svg->draw(g, 1.f, svgTransform);
]

Generally setting a transform (like Drawable::setTransform(svgTransform)) doesn’t seem to work with svg Drawables.

Chris

You’re misunderstanding the meaning of setTransform - it’s actually Component::setTransform() that gets called, so if you bypass the normal component painting mechanism by directly calling the old draw() method, then any components transforms will be ignored.

setTransformToFit() is a Drawable member and calls Component::setTransform(), but I assume it’s now only meant for changing child bounds. I’m quite sure that draw used the Component’s transform some time ago (as the above code worked with the older JUCE). What would be the correct approach now to display a Drawable? I think adding as child component won’t work when variable transparency is a requirement.

Chris

Yes, you can make it a child comp. Alpha should work just fine.