# SVG and its boundaries

**URL:** <https://forum.juce.com/t/svg-and-its-boundaries/21922>\
**Category:** General JUCE discussion\
**Created:** [April 21, 2017, 10:54am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922 "2017-04-21T10:54:47Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 21, 2017, 10:54am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/1 "2017-04-21T10:54:47Z")

</div>

Hi and Hello

is there a way to let [Drawable] consider svg’s viewport/boundaries context while processing its drawing?

In this [topic](https://forum.juce.com/t/drawable-rotation-around-center/21888/2) im facing particular scenario.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 21, 2017, 11:02am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/2 "2017-04-21T11:02:57Z")

</div>

What do you mean by “consider”? If you have something specific in mind, could you suggest a code change?

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 21, 2017, 11:12am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/3 "2017-04-21T11:12:16Z")

</div>

to optionally let [Drawable] use svg’s viewport/size attributes in order to display it within its original context (with offsets/position)

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 21, 2017, 5:02pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/4 "2017-04-21T17:02:57Z")

</div>

With [DrawableShape] call to `pathChanged()`, path’s bounds (actuall visible shape bounds) are passed to `setBoundsToEnclose()` in order to calculate delta for `originRelativeToComponent` which is used to alter the transform at the drawn stage. That is totally fine for most of the cases but it doesnt give us an option to respect original boundaries of a given path container that comes with xml/svg .  
Would you be so keen to add adequate flag/option to ommit offset substraction while parent is a `nullptr` ?

* * *

> <https://github.com/WeAreROLI/JUCE/blob/3592d619795a7e00bab0a5541123e423cec1eff6/modules/juce_gui_basics/drawables/juce_DrawableShape.cpp>

  

> <https://github.com/WeAreROLI/JUCE/blob/3592d619795a7e00bab0a5541123e423cec1eff6/modules/juce_gui_basics/drawables/juce_Drawable.cpp>

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [April 22, 2017, 10:18pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/5 "2017-04-22T22:18:28Z")

</div>

ive been looking further into the parser + co, and i found that altering [DrawableComposite]'s `getDrawableBounds()` method could provide an option for using bounding box values as a unity for drawing and transformation of a given paths. This way shapes are drawn within its original **viewbox** boundaries that comes as an attribute specified inside their document file.

As such relation might be only related to svg parsing, an appropriate flag could be specified by the user while passing desired svg document to the Drawable’s parser i guess.  
However, a more adorable way would be respecting svg’s **enable-background** attribute. It could be used as a indicator for the parser, but obviously it would introduce visual side effects across ale the legacy projects.  
.

> <https://github.com/dowail/JUCE/blob/master/modules/juce_gui_basics/drawables/juce_SVGParser.cpp#L122>

> <https://github.com/dowail/JUCE/blob/master/modules/juce_gui_basics/drawables/juce_DrawableComposite.cpp#L59>

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 2, 2017, 1:26pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/6 "2017-05-02T13:26:20Z")

</div>

Maybe I’ve misunderstood what you’re asking, but isn’t this what DrawableComposite::resetBoundingBoxToContentArea() is for?

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 2, 2017, 2:03pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/7 "2017-05-02T14:03:12Z")

</div>

i havent seen the implementation, just checked the method on the fly.  
could not aquire what its name stands here for.  
does it work in relation with `setTransformToFit()` ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 2, 2017, 2:06pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/8 "2017-05-02T14:06:08Z")

</div>

I guess so, I think setTransformToFit uses the bounds that this would set up.

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 2, 2017, 2:12pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/9 "2017-05-02T14:12:48Z")

</div>

wait ,bounding box consist of content boundary already.  
this is how i retrieve them to calculate scale myself later on against custom size

`viewBox = image->getBoundingBox().getBounds(nullptr);`

otherwise, no luck

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 2, 2017, 2:14pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/10 "2017-05-02T14:14:51Z")

</div>

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;
}
```

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [May 15, 2017, 10:12am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/11 "2017-05-15T10:12:28Z")

</div>

Get struggling with this too…  
I have an non-symmetric sized svg (see picture), which is perfectly centred in its current svg document bounds.

How can use I drawWithin to draw it centred, and respect its svg bounds, so i can rotate it, so that the circle stays at the position, and does not rotate around the wrong center.

![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/c/cb3a2af3e8f9a0203a9be567e814a5098937bb60.png)

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [May 16, 2017, 6:45am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/12 "2017-05-16T06:45:40Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 16, 2017, 7:08am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/13 "2017-05-16T07:08:55Z")

</div>

im not sure if i could do this with the `drawWithin()` if your SVG itslef doesnt have helping container within.  
Perhaps you would have to translate whole path in your drawing procedure, similarry to what i did with `getTransformWith()` above .  
If you dont mind, you could provide invisible container inside your SVG document that has the desired boundaries and then the parser will work around its bounds relatively.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [May 16, 2017, 7:13am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/14 "2017-05-16T07:13:26Z")

</div>

Thank you very much, using invisible box is seems to be the easiest workaround to solve this problem. 🙂

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 16, 2017, 7:14am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/15 "2017-05-16T07:14:07Z")

</div>

unfortunatelly 😕  
have fun !

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [May 16, 2017, 7:49am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/16 "2017-05-16T07:49:27Z")

</div>

You can also name objects and groups with ids in the SVG (e.g., in Illustrator) and it’s reasonably trivial to filter out specific objects or groups of objects based on the ids at the XML level. Then you could have multiple items laid out in the same page with bounding boxes as the reference bounds for those objects.

---

<div class="post-metadata">

**Author:** ![uuich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/uuich/32/19970_2.png) [@uuich](https://forum.juce.com/u/uuich)\
**Post date:** [May 16, 2017, 7:51am UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/17 "2017-05-16T07:51:30Z")

</div>

ive done it too, its too much hassle for displaying scenarios, unless you are building svg editor 🙂

**edit:**  
you can as well strip down **DrawableComposite** into the **paths** and do the same without editing XML id’s.

---

<div class="post-metadata">

**Author:** ![jackmon](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@jackmon](https://forum.juce.com/u/jackmon)\
**Post date:** [May 23, 2018, 9:02pm UTC](https://forum.juce.com/t/svg-and-its-boundaries/21922/18 "2018-05-23T21:02:57Z")

</div>

I’m late to the party, but I think the problem with setTransformToFit() is that it uses  
getDrawableBounds(). In cases where there is intentional space between svg elements and the viewbox, the drawable bounds won’t be the same as the content bounds. You can call resetBoundingBoxToContentArea(), but that won’t change the drawable bounds, right?

The only way I’ve found so far to accomplish what is desired is to do something like this when drawing:

```
        Rectangle<float> svgstate_default_rect(0,0,512,512);
        auto transform = placement.getTransformToFit(svgstate_default_rect, dest_bounds);
        drawable->draw( graphics, 1.0f, transform );

```

But I’m sure this is a brittle hack.
