From 8e7fbe9976fabce136571386f9c8a097b29b11b9 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 11 Mar 2014 22:56:42 +0000 Subject: [PATCH] Fix for SVG view box parsing. Improved introjucer SVG viewer to show the image's content inside the view-box as defined in the file. --- .../Application/jucer_FilePreviewComponent.h | 26 +++++++++++++------ .../drawables/juce_SVGParser.cpp | 13 +++++++--- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/extras/Introjucer/Source/Application/jucer_FilePreviewComponent.h b/extras/Introjucer/Source/Application/jucer_FilePreviewComponent.h index 61820b54eb..881c9b717d 100644 --- a/extras/Introjucer/Source/Application/jucer_FilePreviewComponent.h +++ b/extras/Introjucer/Source/Application/jucer_FilePreviewComponent.h @@ -32,8 +32,7 @@ class ItemPreviewComponent : public Component { public: - ItemPreviewComponent (const File& f) - : file (f) + ItemPreviewComponent (const File& f) : file (f) { setOpaque (true); tryToLoadImage(); @@ -45,17 +44,28 @@ public: if (drawable != nullptr) { - Rectangle area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize) - .appliedTo (drawable->getBounds(), Rectangle (4, 22, getWidth() - 8, getHeight() - 26)); + Rectangle contentBounds (drawable->getDrawableBounds()); + + if (DrawableComposite* dc = dynamic_cast (drawable.get())) + { + Rectangle r (dc->getContentArea().resolve (nullptr)); + + if (! r.isEmpty()) + contentBounds = r; + } + + Rectangle area = RectanglePlacement (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize) + .appliedTo (contentBounds, Rectangle (4.0f, 22.0f, getWidth() - 8.0f, getHeight() - 26.0f)); Path p; p.addRectangle (area); DropShadow (Colours::black.withAlpha (0.5f), 6, Point (0, 1)).drawForPath (g, p); - g.fillCheckerBoard (area, 24, 24, Colour (0xffffffff), Colour (0xffeeeeee)); + g.fillCheckerBoard (area.getSmallestIntegerContainer(), 24, 24, + Colour (0xffffffff), Colour (0xffeeeeee)); - g.setOpacity (1.0f); - drawable->drawWithin (g, area.toFloat(), RectanglePlacement::stretchToFit, 1.0f); + drawable->draw (g, 1.0f, RectanglePlacement (RectanglePlacement::stretchToFit) + .getTransformToFit (contentBounds, area.toFloat())); } g.setFont (Font (14.0f, Font::bold)); @@ -75,7 +85,7 @@ private: drawable = nullptr; { - ScopedPointer input (file.createInputStream()); + ScopedPointer input (file.createInputStream()); if (input != nullptr) { diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index 198bfdbaac..b301a9d54e 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -69,13 +69,15 @@ public: if (newState.width <= 0) newState.width = 100; if (newState.height <= 0) newState.height = 100; + Point viewboxXY; + if (xml->hasAttribute ("viewBox")) { const String viewBoxAtt (xml->getStringAttribute ("viewBox")); String::CharPointerType viewParams (viewBoxAtt.getCharPointer()); - Point vxy, vwh; + Point vwh; - if (parseCoords (viewParams, vxy, true) + if (parseCoords (viewParams, viewboxXY, true) && parseCoords (viewParams, vwh, true) && vwh.x > 0 && vwh.y > 0) @@ -105,7 +107,7 @@ public: } newState.transform = RectanglePlacement (placementFlags) - .getTransformToFit (Rectangle (vxy.x, vxy.y, vwh.x, vwh.y), + .getTransformToFit (Rectangle (viewboxXY.x, viewboxXY.y, vwh.x, vwh.y), Rectangle (newState.width, newState.height)) .followedBy (newState.transform); } @@ -118,7 +120,10 @@ public: newState.parseSubElements (xml, *drawable); - drawable->setContentArea (RelativeRectangle (Rectangle (newState.viewBoxW, newState.viewBoxH))); + drawable->setContentArea (RelativeRectangle (RelativeCoordinate (viewboxXY.x), + RelativeCoordinate (viewboxXY.x + newState.viewBoxW), + RelativeCoordinate (viewboxXY.y), + RelativeCoordinate (viewboxXY.y + newState.viewBoxH))); drawable->resetBoundingBoxToContentArea(); return drawable;