SVG viewport/viewBox question


#1

Hello,

I’m pretty new to Juce and I’d like to know the best way to preserve the empty space in an SVG Drawable’s SVG viewport/viewBox when attempting to scale it.

I’m using a JUCE git checkout from Aug 18 2012 on Linux if that matters.

I’m basically trying to reproduce the pretty chess diagrams in the Wikipedia chess article http://en.wikipedia.org/wiki/Chess, using Wikimedia’s SVG pieces at http://commons.wikimedia.org/wiki/Category:SVG_chess_pieces – but specifically the pieces with no background square (so that I can customize the square if desired)

When I use Drawable::createFromSVG() to create these pieces, and then try Drawable::setTransformToFit() on them with a custom square component, the piece is scaled and translated so that its bottom and top touch the edges of the custom component. This makes it look too big, especially the pawns.

[attachment=1]screenbad.png[/attachment]
The current kludge/workaround I’m using is to patch JUCE’s juce_SVGparser.cpp:parseSVGElement(), by changing

to

drawable->setContentArea(RelativeRectangle(Rectangle<float>(0.0, 0.0, newState.viewBoxW, newState.viewBoxH))); drawable->resetBoundingBoxToContentArea();
And then in my custom square component, which owns the ScopedPointer piece, instead of scaling the piece by doing

/* doesn't work because piece's getDrawableBounds() returns the bounds of the visible part of the piece, not the entire viewport. getBounds(), getHeight(), and getWidth() fail in similar fashion */ piece->setTransformToFit(getLocalBounds().toFloat(), 0);
I do something like:

DrawableComposite *dc = dynamic_cast<DrawableComposite *>(piece.get()); if (dc != nullptr) { RectanglePlacement placement(0); dc->setTransform (placement.getTransformToFit(dc->getContentArea().resolve(nullptr), getLocalBounds().toFloat())); }
Which gives me what I want:
[attachment=0]screengood.png[/attachment]
Is there a nicer way to do this? Particularly one where I don’t have to maintain a private (and probably wrong) patch to the JUCE source, and don’t have to change the SVG pieces I’m using?

Thanks for any help, --buck

(and feel free to ignore the weird fill on the knights, that’s some unrelated issue I’ll try to track down better before I inquire about it)


#2

Hmm. That does actually make a lot of sense… I’m not sure why it doesn’t already use the bounding box like that…

Thanks, I’ll have a look at this later and will probably change it.


#3

Jules, thanks very much for the response, for having a look, and of course, for the fine library. There are probably also cases where the current behavior is preferred; I just haven’t run into them yet.

Edit: Man, you are quick.


#4

Thanks, I checked in the changes earlier today if you want to have a look.