as a usecase, i want to resize SVG to match desired bounds while preserving ratio and respecting its viewbox/content area.
and this is how i achieve proper placement and scale, where the scale “float s” is sort of a magnify level
AffineTransform getTransformWith(int w, int h, float s, RectanglePlacement::Flags p = RectanglePlacement::fillDestination) {
if(viewBox.getWidth() == 0 || viewBox.getHeight() == 0) return AffineTransform();
float newX = 0.f;
float newY = 0.f;
float scaleX = w / viewBox.getWidth();
float scaleY = (h > 0.f ? h : w) / viewBox.getHeight();
scaleX *= s;
scaleY *= s;
if(p != RectanglePlacement::stretchToFit) {
scaleX = (p == RectanglePlacement::Flags::centred) ? 1.f : jmax(jmin(scaleX, scaleY), 0.0f);
scaleY = scaleX;
newX += (w - viewBox.getWidth() * scaleX) / 2.0f; // centre
newY += ((h > 0.f ? h : w) - viewBox.getHeight() * scaleX) / 2.0f; // centre
}
auto at = AffineTransform::scale(scaleX, scaleY).translated(newX, newY);
return at.isSingularity() ? AffineTransform() : at;
}
