From 609794ceaed0dc5dfa474b2eb1a8ae03ce2f2771 Mon Sep 17 00:00:00 2001 From: jules Date: Sun, 24 Aug 2014 20:05:38 +0100 Subject: [PATCH] SVG parser fix for default stroke widths --- .../drawables/juce_SVGParser.cpp | 97 +++++++++---------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index f5dbcd9ffd..e0214d87f1 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -560,24 +560,12 @@ private: path.applyTransform (transform); dp->setPath (path); - Path::Iterator iter (path); - - bool containsClosedSubPath = false; - while (iter.next()) - { - if (iter.elementType == Path::Iterator::closePath) - { - containsClosedSubPath = true; - break; - } - } - dp->setFill (getPathFillType (path, getStyleAttribute (xml, "fill"), getStyleAttribute (xml, "fill-opacity"), getStyleAttribute (xml, "opacity"), - containsClosedSubPath ? Colours::black - : Colours::transparentBlack)); + pathContainsClosedSubPath (path) ? Colours::black + : Colours::transparentBlack)); const String strokeType (getStyleAttribute (xml, "stroke")); @@ -594,6 +582,15 @@ private: return dp; } + static bool pathContainsClosedSubPath (const Path& path) noexcept + { + for (Path::Iterator iter (path); iter.next();) + if (iter.elementType == Path::Iterator::closePath) + return true; + + return false; + } + struct SetGradientStopsOp { const SVGState* state; @@ -796,44 +793,41 @@ private: return parseColour (fill, i, defaultColour).withMultipliedAlpha (opacity); } + static PathStrokeType::JointStyle getJointStyle (const String& join) noexcept + { + if (join.equalsIgnoreCase ("round")) return PathStrokeType::curved; + if (join.equalsIgnoreCase ("bevel")) return PathStrokeType::beveled; + + return PathStrokeType::mitered; + } + + static PathStrokeType::EndCapStyle getEndCapStyle (const String& cap) noexcept + { + if (cap.equalsIgnoreCase ("round")) return PathStrokeType::rounded; + if (cap.equalsIgnoreCase ("square")) return PathStrokeType::square; + + return PathStrokeType::butt; + } + + float getStrokeWidth (const String& strokeWidth) const noexcept + { + return transform.getScaleFactor() * getCoordLength (strokeWidth, viewBoxW); + } + PathStrokeType getStrokeFor (const XmlPath& xml) const { - const String strokeWidth (getStyleAttribute (xml, "stroke-width")); - const String cap (getStyleAttribute (xml, "stroke-linecap")); - const String join (getStyleAttribute (xml, "stroke-linejoin")); - - //const String mitreLimit (getStyleAttribute (xml, "stroke-miterlimit")); - //const String dashArray (getStyleAttribute (xml, "stroke-dasharray")); - //const String dashOffset (getStyleAttribute (xml, "stroke-dashoffset")); - - PathStrokeType::JointStyle joinStyle = PathStrokeType::mitered; - PathStrokeType::EndCapStyle capStyle = PathStrokeType::butt; - - if (join.equalsIgnoreCase ("round")) - joinStyle = PathStrokeType::curved; - else if (join.equalsIgnoreCase ("bevel")) - joinStyle = PathStrokeType::beveled; - - if (cap.equalsIgnoreCase ("round")) - capStyle = PathStrokeType::rounded; - else if (cap.equalsIgnoreCase ("square")) - capStyle = PathStrokeType::square; - - float ox = 0.0f, oy = 0.0f; - float x = getCoordLength (strokeWidth, viewBoxW), y = 0.0f; - transform.transformPoints (ox, oy, x, y); - - return PathStrokeType (strokeWidth.isNotEmpty() ? juce_hypot (x - ox, y - oy) : 1.0f, - joinStyle, capStyle); + return PathStrokeType (getStrokeWidth (getStyleAttribute (xml, "stroke-width", "1")), + getJointStyle (getStyleAttribute (xml, "stroke-linejoin")), + getEndCapStyle (getStyleAttribute (xml, "stroke-linecap"))); } //============================================================================== Drawable* parseText (const XmlPath& xml) { - Array xCoords, yCoords, dxCoords, dyCoords; + Array xCoords, yCoords, dxCoords, dyCoords; - getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true); - getCoordList (yCoords, getInheritedAttribute (xml, "y"), true, false); + getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true); + getCoordList (yCoords, getInheritedAttribute (xml, "y"), true, false); getCoordList (dxCoords, getInheritedAttribute (xml, "dx"), true, true); getCoordList (dyCoords, getInheritedAttribute (xml, "dy"), true, false); @@ -898,7 +892,7 @@ private: return false; } - float getCoordLength (const String& s, const float sizeForProportions) const + float getCoordLength (const String& s, const float sizeForProportions) const noexcept { float n = s.getFloatValue(); const int len = s.length(); @@ -920,13 +914,12 @@ private: return n; } - float getCoordLength (const XmlPath& xml, const char* attName, const float sizeForProportions) const + float getCoordLength (const XmlPath& xml, const char* attName, const float sizeForProportions) const noexcept { return getCoordLength (xml->getStringAttribute (attName), sizeForProportions); } - void getCoordList (Array & coords, const String& list, - const bool allowUnits, const bool isX) const + void getCoordList (Array& coords, const String& list, bool allowUnits, const bool isX) const { String::CharPointerType text (list.getCharPointer()); float value; @@ -960,7 +953,7 @@ private: return source; } - String getStyleAttribute (const XmlPath& xml, const String& attributeName, + String getStyleAttribute (const XmlPath& xml, StringRef attributeName, const String& defaultValue = String()) const { if (xml->hasAttribute (attributeName)) @@ -1000,7 +993,7 @@ private: return defaultValue; } - String getInheritedAttribute (const XmlPath& xml, const String& attributeName) const + String getInheritedAttribute (const XmlPath& xml, StringRef attributeName) const { if (xml->hasAttribute (attributeName)) return xml->getStringAttribute (attributeName); @@ -1017,7 +1010,7 @@ private: return CharacterFunctions::isLetter (c) || c == '-'; } - static String getAttributeFromStyleList (const String& list, const String& attributeName, const String& defaultValue) + static String getAttributeFromStyleList (const String& list, StringRef attributeName, const String& defaultValue) { int i = 0; @@ -1221,7 +1214,7 @@ private: const bool largeArc, const bool sweep, double& rx, double& ry, double& centreX, double& centreY, - double& startAngle, double& deltaAngle) + double& startAngle, double& deltaAngle) noexcept { const double midX = (x1 - x2) * 0.5; const double midY = (y1 - y2) * 0.5;