The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1524 lines
52KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. class SVGState
  20. {
  21. public:
  22. //==============================================================================
  23. explicit SVGState (const XmlElement* topLevel) : topLevelXml (topLevel, nullptr)
  24. {
  25. }
  26. struct XmlPath
  27. {
  28. XmlPath (const XmlElement* e, const XmlPath* p) noexcept : xml (e), parent (p) {}
  29. const XmlElement& operator*() const noexcept { jassert (xml != nullptr); return *xml; }
  30. const XmlElement* operator->() const noexcept { return xml; }
  31. XmlPath getChild (const XmlElement* e) const noexcept { return XmlPath (e, this); }
  32. template <typename OperationType>
  33. bool applyOperationToChildWithID (const String& id, OperationType& op) const
  34. {
  35. forEachXmlChildElement (*xml, e)
  36. {
  37. XmlPath child (e, this);
  38. if (e->compareAttribute ("id", id))
  39. {
  40. op (child);
  41. return true;
  42. }
  43. if (child.applyOperationToChildWithID (id, op))
  44. return true;
  45. }
  46. return false;
  47. }
  48. const XmlElement* xml;
  49. const XmlPath* parent;
  50. };
  51. //==============================================================================
  52. struct UsePathOp
  53. {
  54. const SVGState* state;
  55. Path* targetPath;
  56. void operator() (const XmlPath& xmlPath) const
  57. {
  58. state->parsePathElement (xmlPath, *targetPath);
  59. }
  60. };
  61. struct GetClipPathOp
  62. {
  63. const SVGState* state;
  64. Drawable* target;
  65. void operator() (const XmlPath& xmlPath) const
  66. {
  67. state->applyClipPath (*target, xmlPath);
  68. }
  69. };
  70. struct SetGradientStopsOp
  71. {
  72. const SVGState* state;
  73. ColourGradient* gradient;
  74. void operator() (const XmlPath& xml) const
  75. {
  76. state->addGradientStopsIn (*gradient, xml);
  77. }
  78. };
  79. struct GetFillTypeOp
  80. {
  81. const SVGState* state;
  82. const Path* path;
  83. float opacity;
  84. FillType fillType;
  85. void operator() (const XmlPath& xml)
  86. {
  87. if (xml->hasTagNameIgnoringNamespace ("linearGradient")
  88. || xml->hasTagNameIgnoringNamespace ("radialGradient"))
  89. fillType = state->getGradientFillType (xml, *path, opacity);
  90. }
  91. };
  92. //==============================================================================
  93. Drawable* parseSVGElement (const XmlPath& xml)
  94. {
  95. auto drawable = new DrawableComposite();
  96. setCommonAttributes (*drawable, xml);
  97. SVGState newState (*this);
  98. if (xml->hasAttribute ("transform"))
  99. newState.addTransform (xml);
  100. newState.width = getCoordLength (xml->getStringAttribute ("width", String (newState.width)), viewBoxW);
  101. newState.height = getCoordLength (xml->getStringAttribute ("height", String (newState.height)), viewBoxH);
  102. if (newState.width <= 0) newState.width = 100;
  103. if (newState.height <= 0) newState.height = 100;
  104. Point<float> viewboxXY;
  105. if (xml->hasAttribute ("viewBox"))
  106. {
  107. auto viewBoxAtt = xml->getStringAttribute ("viewBox");
  108. auto viewParams = viewBoxAtt.getCharPointer();
  109. Point<float> vwh;
  110. if (parseCoords (viewParams, viewboxXY, true)
  111. && parseCoords (viewParams, vwh, true)
  112. && vwh.x > 0
  113. && vwh.y > 0)
  114. {
  115. newState.viewBoxW = vwh.x;
  116. newState.viewBoxH = vwh.y;
  117. auto placementFlags = parsePlacementFlags (xml->getStringAttribute ("preserveAspectRatio").trim());
  118. if (placementFlags != 0)
  119. newState.transform = RectanglePlacement (placementFlags)
  120. .getTransformToFit (Rectangle<float> (viewboxXY.x, viewboxXY.y, vwh.x, vwh.y),
  121. Rectangle<float> (newState.width, newState.height))
  122. .followedBy (newState.transform);
  123. }
  124. }
  125. else
  126. {
  127. if (viewBoxW == 0.0f) newState.viewBoxW = newState.width;
  128. if (viewBoxH == 0.0f) newState.viewBoxH = newState.height;
  129. }
  130. newState.parseSubElements (xml, *drawable);
  131. drawable->setContentArea (RelativeRectangle (RelativeCoordinate (viewboxXY.x),
  132. RelativeCoordinate (viewboxXY.x + newState.viewBoxW),
  133. RelativeCoordinate (viewboxXY.y),
  134. RelativeCoordinate (viewboxXY.y + newState.viewBoxH)));
  135. drawable->resetBoundingBoxToContentArea();
  136. return drawable;
  137. }
  138. //==============================================================================
  139. void parsePathString (Path& path, const String& pathString) const
  140. {
  141. auto d = pathString.getCharPointer().findEndOfWhitespace();
  142. Point<float> subpathStart, last, last2, p1, p2, p3;
  143. juce_wchar currentCommand = 0, previousCommand = 0;
  144. bool isRelative = true;
  145. bool carryOn = true;
  146. while (! d.isEmpty())
  147. {
  148. if (CharPointer_ASCII ("MmLlHhVvCcSsQqTtAaZz").indexOf (*d) >= 0)
  149. {
  150. currentCommand = d.getAndAdvance();
  151. isRelative = currentCommand >= 'a';
  152. }
  153. switch (currentCommand)
  154. {
  155. case 'M':
  156. case 'm':
  157. case 'L':
  158. case 'l':
  159. if (parseCoordsOrSkip (d, p1, false))
  160. {
  161. if (isRelative)
  162. p1 += last;
  163. if (currentCommand == 'M' || currentCommand == 'm')
  164. {
  165. subpathStart = p1;
  166. path.startNewSubPath (p1);
  167. currentCommand = 'l';
  168. }
  169. else
  170. path.lineTo (p1);
  171. last2 = last;
  172. last = p1;
  173. }
  174. break;
  175. case 'H':
  176. case 'h':
  177. if (parseCoord (d, p1.x, false, true))
  178. {
  179. if (isRelative)
  180. p1.x += last.x;
  181. path.lineTo (p1.x, last.y);
  182. last2.x = last.x;
  183. last.x = p1.x;
  184. }
  185. else
  186. {
  187. ++d;
  188. }
  189. break;
  190. case 'V':
  191. case 'v':
  192. if (parseCoord (d, p1.y, false, false))
  193. {
  194. if (isRelative)
  195. p1.y += last.y;
  196. path.lineTo (last.x, p1.y);
  197. last2.y = last.y;
  198. last.y = p1.y;
  199. }
  200. else
  201. {
  202. ++d;
  203. }
  204. break;
  205. case 'C':
  206. case 'c':
  207. if (parseCoordsOrSkip (d, p1, false)
  208. && parseCoordsOrSkip (d, p2, false)
  209. && parseCoordsOrSkip (d, p3, false))
  210. {
  211. if (isRelative)
  212. {
  213. p1 += last;
  214. p2 += last;
  215. p3 += last;
  216. }
  217. path.cubicTo (p1, p2, p3);
  218. last2 = p2;
  219. last = p3;
  220. }
  221. break;
  222. case 'S':
  223. case 's':
  224. if (parseCoordsOrSkip (d, p1, false)
  225. && parseCoordsOrSkip (d, p3, false))
  226. {
  227. if (isRelative)
  228. {
  229. p1 += last;
  230. p3 += last;
  231. }
  232. p2 = last + (last - last2);
  233. path.cubicTo (p2, p1, p3);
  234. last2 = p1;
  235. last = p3;
  236. }
  237. break;
  238. case 'Q':
  239. case 'q':
  240. if (parseCoordsOrSkip (d, p1, false)
  241. && parseCoordsOrSkip (d, p2, false))
  242. {
  243. if (isRelative)
  244. {
  245. p1 += last;
  246. p2 += last;
  247. }
  248. path.quadraticTo (p1, p2);
  249. last2 = p1;
  250. last = p2;
  251. }
  252. break;
  253. case 'T':
  254. case 't':
  255. if (parseCoordsOrSkip (d, p1, false))
  256. {
  257. if (isRelative)
  258. p1 += last;
  259. p2 = CharPointer_ASCII ("QqTt").indexOf (previousCommand) >= 0 ? last + (last - last2)
  260. : p1;
  261. path.quadraticTo (p2, p1);
  262. last2 = p2;
  263. last = p1;
  264. }
  265. break;
  266. case 'A':
  267. case 'a':
  268. if (parseCoordsOrSkip (d, p1, false))
  269. {
  270. String num;
  271. if (parseNextNumber (d, num, false))
  272. {
  273. const float angle = degreesToRadians (num.getFloatValue());
  274. if (parseNextNumber (d, num, false))
  275. {
  276. const bool largeArc = num.getIntValue() != 0;
  277. if (parseNextNumber (d, num, false))
  278. {
  279. const bool sweep = num.getIntValue() != 0;
  280. if (parseCoordsOrSkip (d, p2, false))
  281. {
  282. if (isRelative)
  283. p2 += last;
  284. if (last != p2)
  285. {
  286. double centreX, centreY, startAngle, deltaAngle;
  287. double rx = p1.x, ry = p1.y;
  288. endpointToCentreParameters (last.x, last.y, p2.x, p2.y,
  289. angle, largeArc, sweep,
  290. rx, ry, centreX, centreY,
  291. startAngle, deltaAngle);
  292. path.addCentredArc ((float) centreX, (float) centreY,
  293. (float) rx, (float) ry,
  294. angle, (float) startAngle, (float) (startAngle + deltaAngle),
  295. false);
  296. path.lineTo (p2);
  297. }
  298. last2 = last;
  299. last = p2;
  300. }
  301. }
  302. }
  303. }
  304. }
  305. break;
  306. case 'Z':
  307. case 'z':
  308. path.closeSubPath();
  309. last = last2 = subpathStart;
  310. d = d.findEndOfWhitespace();
  311. currentCommand = 'M';
  312. break;
  313. default:
  314. carryOn = false;
  315. break;
  316. }
  317. if (! carryOn)
  318. break;
  319. previousCommand = currentCommand;
  320. }
  321. // paths that finish back at their start position often seem to be
  322. // left without a 'z', so need to be closed explicitly..
  323. if (path.getCurrentPosition() == subpathStart)
  324. path.closeSubPath();
  325. }
  326. private:
  327. //==============================================================================
  328. const XmlPath topLevelXml;
  329. float width = 512, height = 512, viewBoxW = 0, viewBoxH = 0;
  330. AffineTransform transform;
  331. String cssStyleText;
  332. static bool isNone (const String& s) noexcept
  333. {
  334. return s.equalsIgnoreCase ("none");
  335. }
  336. static void setCommonAttributes (Drawable& d, const XmlPath& xml)
  337. {
  338. auto compID = xml->getStringAttribute ("id");
  339. d.setName (compID);
  340. d.setComponentID (compID);
  341. if (isNone (xml->getStringAttribute ("display")))
  342. d.setVisible (false);
  343. }
  344. //==============================================================================
  345. void parseSubElements (const XmlPath& xml, DrawableComposite& parentDrawable)
  346. {
  347. forEachXmlChildElement (*xml, e)
  348. {
  349. const XmlPath child (xml.getChild (e));
  350. if (auto* drawable = parseSubElement (child))
  351. {
  352. parentDrawable.addChildComponent (drawable);
  353. if (! isNone (getStyleAttribute (child, "display")))
  354. drawable->setVisible (true);
  355. }
  356. }
  357. }
  358. Drawable* parseSubElement (const XmlPath& xml)
  359. {
  360. {
  361. Path path;
  362. if (parsePathElement (xml, path))
  363. return parseShape (xml, path);
  364. }
  365. auto tag = xml->getTagNameWithoutNamespace();
  366. if (tag == "g") return parseGroupElement (xml);
  367. if (tag == "svg") return parseSVGElement (xml);
  368. if (tag == "text") return parseText (xml, true);
  369. if (tag == "switch") return parseSwitch (xml);
  370. if (tag == "a") return parseLinkElement (xml);
  371. if (tag == "style") parseCSSStyle (xml);
  372. if (tag == "defs") parseDefs (xml);
  373. return nullptr;
  374. }
  375. bool parsePathElement (const XmlPath& xml, Path& path) const
  376. {
  377. const String tag (xml->getTagNameWithoutNamespace());
  378. if (tag == "path") { parsePath (xml, path); return true; }
  379. if (tag == "rect") { parseRect (xml, path); return true; }
  380. if (tag == "circle") { parseCircle (xml, path); return true; }
  381. if (tag == "ellipse") { parseEllipse (xml, path); return true; }
  382. if (tag == "line") { parseLine (xml, path); return true; }
  383. if (tag == "polyline") { parsePolygon (xml, true, path); return true; }
  384. if (tag == "polygon") { parsePolygon (xml, false, path); return true; }
  385. if (tag == "use") { parseUse (xml, path); return true; }
  386. return false;
  387. }
  388. DrawableComposite* parseSwitch (const XmlPath& xml)
  389. {
  390. if (auto* group = xml->getChildByName ("g"))
  391. return parseGroupElement (xml.getChild (group));
  392. return nullptr;
  393. }
  394. DrawableComposite* parseGroupElement (const XmlPath& xml)
  395. {
  396. auto drawable = new DrawableComposite();
  397. setCommonAttributes (*drawable, xml);
  398. if (xml->hasAttribute ("transform"))
  399. {
  400. SVGState newState (*this);
  401. newState.addTransform (xml);
  402. newState.parseSubElements (xml, *drawable);
  403. }
  404. else
  405. {
  406. parseSubElements (xml, *drawable);
  407. }
  408. drawable->resetContentAreaAndBoundingBoxToFitChildren();
  409. return drawable;
  410. }
  411. DrawableComposite* parseLinkElement (const XmlPath& xml)
  412. {
  413. return parseGroupElement (xml); // TODO: support for making this clickable
  414. }
  415. //==============================================================================
  416. void parsePath (const XmlPath& xml, Path& path) const
  417. {
  418. parsePathString (path, xml->getStringAttribute ("d"));
  419. if (getStyleAttribute (xml, "fill-rule").trim().equalsIgnoreCase ("evenodd"))
  420. path.setUsingNonZeroWinding (false);
  421. }
  422. void parseRect (const XmlPath& xml, Path& rect) const
  423. {
  424. const bool hasRX = xml->hasAttribute ("rx");
  425. const bool hasRY = xml->hasAttribute ("ry");
  426. if (hasRX || hasRY)
  427. {
  428. float rx = getCoordLength (xml, "rx", viewBoxW);
  429. float ry = getCoordLength (xml, "ry", viewBoxH);
  430. if (! hasRX)
  431. rx = ry;
  432. else if (! hasRY)
  433. ry = rx;
  434. rect.addRoundedRectangle (getCoordLength (xml, "x", viewBoxW),
  435. getCoordLength (xml, "y", viewBoxH),
  436. getCoordLength (xml, "width", viewBoxW),
  437. getCoordLength (xml, "height", viewBoxH),
  438. rx, ry);
  439. }
  440. else
  441. {
  442. rect.addRectangle (getCoordLength (xml, "x", viewBoxW),
  443. getCoordLength (xml, "y", viewBoxH),
  444. getCoordLength (xml, "width", viewBoxW),
  445. getCoordLength (xml, "height", viewBoxH));
  446. }
  447. }
  448. void parseCircle (const XmlPath& xml, Path& circle) const
  449. {
  450. auto cx = getCoordLength (xml, "cx", viewBoxW);
  451. auto cy = getCoordLength (xml, "cy", viewBoxH);
  452. auto radius = getCoordLength (xml, "r", viewBoxW);
  453. circle.addEllipse (cx - radius, cy - radius, radius * 2.0f, radius * 2.0f);
  454. }
  455. void parseEllipse (const XmlPath& xml, Path& ellipse) const
  456. {
  457. auto cx = getCoordLength (xml, "cx", viewBoxW);
  458. auto cy = getCoordLength (xml, "cy", viewBoxH);
  459. auto radiusX = getCoordLength (xml, "rx", viewBoxW);
  460. auto radiusY = getCoordLength (xml, "ry", viewBoxH);
  461. ellipse.addEllipse (cx - radiusX, cy - radiusY, radiusX * 2.0f, radiusY * 2.0f);
  462. }
  463. void parseLine (const XmlPath& xml, Path& line) const
  464. {
  465. auto x1 = getCoordLength (xml, "x1", viewBoxW);
  466. auto y1 = getCoordLength (xml, "y1", viewBoxH);
  467. auto x2 = getCoordLength (xml, "x2", viewBoxW);
  468. auto y2 = getCoordLength (xml, "y2", viewBoxH);
  469. line.startNewSubPath (x1, y1);
  470. line.lineTo (x2, y2);
  471. }
  472. void parsePolygon (const XmlPath& xml, const bool isPolyline, Path& path) const
  473. {
  474. auto pointsAtt = xml->getStringAttribute ("points");
  475. auto points = pointsAtt.getCharPointer();
  476. Point<float> p;
  477. if (parseCoords (points, p, true))
  478. {
  479. Point<float> first (p), last;
  480. path.startNewSubPath (first);
  481. while (parseCoords (points, p, true))
  482. {
  483. last = p;
  484. path.lineTo (p);
  485. }
  486. if ((! isPolyline) || first == last)
  487. path.closeSubPath();
  488. }
  489. }
  490. void parseUse (const XmlPath& xml, Path& path) const
  491. {
  492. auto link = xml->getStringAttribute ("xlink:href");
  493. if (link.startsWithChar ('#'))
  494. {
  495. auto linkedID = link.substring (1);
  496. UsePathOp op = { this, &path };
  497. topLevelXml.applyOperationToChildWithID (linkedID, op);
  498. }
  499. }
  500. static String parseURL (const String& str)
  501. {
  502. if (str.startsWithIgnoreCase ("url"))
  503. return str.fromFirstOccurrenceOf ("#", false, false)
  504. .upToLastOccurrenceOf (")", false, false).trim();
  505. return {};
  506. }
  507. //==============================================================================
  508. Drawable* parseShape (const XmlPath& xml, Path& path,
  509. const bool shouldParseTransform = true) const
  510. {
  511. if (shouldParseTransform && xml->hasAttribute ("transform"))
  512. {
  513. SVGState newState (*this);
  514. newState.addTransform (xml);
  515. return newState.parseShape (xml, path, false);
  516. }
  517. auto dp = new DrawablePath();
  518. setCommonAttributes (*dp, xml);
  519. dp->setFill (Colours::transparentBlack);
  520. path.applyTransform (transform);
  521. dp->setPath (path);
  522. dp->setFill (getPathFillType (path, xml, "fill",
  523. getStyleAttribute (xml, "fill-opacity"),
  524. getStyleAttribute (xml, "opacity"),
  525. pathContainsClosedSubPath (path) ? Colours::black
  526. : Colours::transparentBlack));
  527. auto strokeType = getStyleAttribute (xml, "stroke");
  528. if (strokeType.isNotEmpty() && ! isNone (strokeType))
  529. {
  530. dp->setStrokeFill (getPathFillType (path, xml, "stroke",
  531. getStyleAttribute (xml, "stroke-opacity"),
  532. getStyleAttribute (xml, "opacity"),
  533. Colours::transparentBlack));
  534. dp->setStrokeType (getStrokeFor (xml));
  535. }
  536. auto strokeDashArray = getStyleAttribute (xml, "stroke-dasharray");
  537. if (strokeDashArray.isNotEmpty())
  538. parseDashArray (strokeDashArray, *dp);
  539. parseClipPath (xml, *dp);
  540. return dp;
  541. }
  542. static bool pathContainsClosedSubPath (const Path& path) noexcept
  543. {
  544. for (Path::Iterator iter (path); iter.next();)
  545. if (iter.elementType == Path::Iterator::closePath)
  546. return true;
  547. return false;
  548. }
  549. void parseDashArray (const String& dashList, DrawablePath& dp) const
  550. {
  551. if (dashList.equalsIgnoreCase ("null") || isNone (dashList))
  552. return;
  553. Array<float> dashLengths;
  554. for (auto t = dashList.getCharPointer();;)
  555. {
  556. float value;
  557. if (! parseCoord (t, value, true, true))
  558. break;
  559. dashLengths.add (value);
  560. t = t.findEndOfWhitespace();
  561. if (*t == ',')
  562. ++t;
  563. }
  564. if (dashLengths.size() > 0)
  565. {
  566. auto* dashes = dashLengths.getRawDataPointer();
  567. for (int i = 0; i < dashLengths.size(); ++i)
  568. {
  569. if (dashes[i] <= 0) // SVG uses zero-length dashes to mean a dotted line
  570. {
  571. if (dashLengths.size() == 1)
  572. return;
  573. const float nonZeroLength = 0.001f;
  574. dashes[i] = nonZeroLength;
  575. const int pairedIndex = i ^ 1;
  576. if (isPositiveAndBelow (pairedIndex, dashLengths.size())
  577. && dashes[pairedIndex] > nonZeroLength)
  578. dashes[pairedIndex] -= nonZeroLength;
  579. }
  580. }
  581. dp.setDashLengths (dashLengths);
  582. }
  583. }
  584. void parseClipPath (const XmlPath& xml, Drawable& d) const
  585. {
  586. const String clipPath (getStyleAttribute (xml, "clip-path"));
  587. if (clipPath.isNotEmpty())
  588. {
  589. auto urlID = parseURL (clipPath);
  590. if (urlID.isNotEmpty())
  591. {
  592. GetClipPathOp op = { this, &d };
  593. topLevelXml.applyOperationToChildWithID (urlID, op);
  594. }
  595. }
  596. }
  597. void applyClipPath (Drawable& target, const XmlPath& xmlPath) const
  598. {
  599. if (xmlPath->hasTagNameIgnoringNamespace ("clipPath"))
  600. {
  601. // TODO: implement clipping..
  602. ignoreUnused (target);
  603. }
  604. }
  605. void addGradientStopsIn (ColourGradient& cg, const XmlPath& fillXml) const
  606. {
  607. if (fillXml.xml != nullptr)
  608. {
  609. forEachXmlChildElementWithTagName (*fillXml, e, "stop")
  610. {
  611. auto col = parseColour (fillXml.getChild (e), "stop-color", Colours::black);
  612. auto opacity = getStyleAttribute (fillXml.getChild (e), "stop-opacity", "1");
  613. col = col.withMultipliedAlpha (jlimit (0.0f, 1.0f, opacity.getFloatValue()));
  614. double offset = e->getDoubleAttribute ("offset");
  615. if (e->getStringAttribute ("offset").containsChar ('%'))
  616. offset *= 0.01;
  617. cg.addColour (jlimit (0.0, 1.0, offset), col);
  618. }
  619. }
  620. }
  621. FillType getGradientFillType (const XmlPath& fillXml,
  622. const Path& path,
  623. const float opacity) const
  624. {
  625. ColourGradient gradient;
  626. {
  627. auto link = fillXml->getStringAttribute ("xlink:href");
  628. if (link.startsWithChar ('#'))
  629. {
  630. SetGradientStopsOp op = { this, &gradient, };
  631. topLevelXml.applyOperationToChildWithID (link.substring (1), op);
  632. }
  633. }
  634. addGradientStopsIn (gradient, fillXml);
  635. if (int numColours = gradient.getNumColours())
  636. {
  637. if (gradient.getColourPosition (0) > 0)
  638. gradient.addColour (0.0, gradient.getColour (0));
  639. if (gradient.getColourPosition (numColours - 1) < 1.0)
  640. gradient.addColour (1.0, gradient.getColour (numColours - 1));
  641. }
  642. else
  643. {
  644. gradient.addColour (0.0, Colours::black);
  645. gradient.addColour (1.0, Colours::black);
  646. }
  647. if (opacity < 1.0f)
  648. gradient.multiplyOpacity (opacity);
  649. jassert (gradient.getNumColours() > 0);
  650. gradient.isRadial = fillXml->hasTagNameIgnoringNamespace ("radialGradient");
  651. float gradientWidth = viewBoxW;
  652. float gradientHeight = viewBoxH;
  653. float dx = 0.0f;
  654. float dy = 0.0f;
  655. const bool userSpace = fillXml->getStringAttribute ("gradientUnits").equalsIgnoreCase ("userSpaceOnUse");
  656. if (! userSpace)
  657. {
  658. auto bounds = path.getBounds();
  659. dx = bounds.getX();
  660. dy = bounds.getY();
  661. gradientWidth = bounds.getWidth();
  662. gradientHeight = bounds.getHeight();
  663. }
  664. if (gradient.isRadial)
  665. {
  666. if (userSpace)
  667. gradient.point1.setXY (dx + getCoordLength (fillXml->getStringAttribute ("cx", "50%"), gradientWidth),
  668. dy + getCoordLength (fillXml->getStringAttribute ("cy", "50%"), gradientHeight));
  669. else
  670. gradient.point1.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("cx", "50%"), 1.0f),
  671. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("cy", "50%"), 1.0f));
  672. auto radius = getCoordLength (fillXml->getStringAttribute ("r", "50%"), gradientWidth);
  673. gradient.point2 = gradient.point1 + Point<float> (radius, 0.0f);
  674. //xxx (the fx, fy focal point isn't handled properly here..)
  675. }
  676. else
  677. {
  678. if (userSpace)
  679. {
  680. gradient.point1.setXY (dx + getCoordLength (fillXml->getStringAttribute ("x1", "0%"), gradientWidth),
  681. dy + getCoordLength (fillXml->getStringAttribute ("y1", "0%"), gradientHeight));
  682. gradient.point2.setXY (dx + getCoordLength (fillXml->getStringAttribute ("x2", "100%"), gradientWidth),
  683. dy + getCoordLength (fillXml->getStringAttribute ("y2", "0%"), gradientHeight));
  684. }
  685. else
  686. {
  687. gradient.point1.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("x1", "0%"), 1.0f),
  688. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("y1", "0%"), 1.0f));
  689. gradient.point2.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("x2", "100%"), 1.0f),
  690. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("y2", "0%"), 1.0f));
  691. }
  692. if (gradient.point1 == gradient.point2)
  693. return Colour (gradient.getColour (gradient.getNumColours() - 1));
  694. }
  695. FillType type (gradient);
  696. auto gradientTransform = parseTransform (fillXml->getStringAttribute ("gradientTransform"))
  697. .followedBy (transform);
  698. if (gradient.isRadial)
  699. {
  700. type.transform = gradientTransform;
  701. }
  702. else
  703. {
  704. // Transform the perpendicular vector into the new coordinate space for the gradient.
  705. // This vector is now the slope of the linear gradient as it should appear in the new coord space
  706. auto perpendicular = Point<float> (gradient.point2.y - gradient.point1.y,
  707. gradient.point1.x - gradient.point2.x)
  708. .transformedBy (gradientTransform.withAbsoluteTranslation (0, 0));
  709. auto newGradPoint1 = gradient.point1.transformedBy (gradientTransform);
  710. auto newGradPoint2 = gradient.point2.transformedBy (gradientTransform);
  711. // Project the transformed gradient vector onto the transformed slope of the linear
  712. // gradient as it should appear in the new coordinate space
  713. const float scale = perpendicular.getDotProduct (newGradPoint2 - newGradPoint1)
  714. / perpendicular.getDotProduct (perpendicular);
  715. type.gradient->point1 = newGradPoint1;
  716. type.gradient->point2 = newGradPoint2 - perpendicular * scale;
  717. }
  718. return type;
  719. }
  720. FillType getPathFillType (const Path& path,
  721. const XmlPath& xml,
  722. StringRef fillAttribute,
  723. const String& fillOpacity,
  724. const String& overallOpacity,
  725. const Colour defaultColour) const
  726. {
  727. float opacity = 1.0f;
  728. if (overallOpacity.isNotEmpty())
  729. opacity = jlimit (0.0f, 1.0f, overallOpacity.getFloatValue());
  730. if (fillOpacity.isNotEmpty())
  731. opacity *= (jlimit (0.0f, 1.0f, fillOpacity.getFloatValue()));
  732. String fill (getStyleAttribute (xml, fillAttribute));
  733. String urlID = parseURL (fill);
  734. if (urlID.isNotEmpty())
  735. {
  736. GetFillTypeOp op = { this, &path, opacity, FillType() };
  737. if (topLevelXml.applyOperationToChildWithID (urlID, op))
  738. return op.fillType;
  739. }
  740. if (isNone (fill))
  741. return Colours::transparentBlack;
  742. return parseColour (xml, fillAttribute, defaultColour).withMultipliedAlpha (opacity);
  743. }
  744. static PathStrokeType::JointStyle getJointStyle (const String& join) noexcept
  745. {
  746. if (join.equalsIgnoreCase ("round")) return PathStrokeType::curved;
  747. if (join.equalsIgnoreCase ("bevel")) return PathStrokeType::beveled;
  748. return PathStrokeType::mitered;
  749. }
  750. static PathStrokeType::EndCapStyle getEndCapStyle (const String& cap) noexcept
  751. {
  752. if (cap.equalsIgnoreCase ("round")) return PathStrokeType::rounded;
  753. if (cap.equalsIgnoreCase ("square")) return PathStrokeType::square;
  754. return PathStrokeType::butt;
  755. }
  756. float getStrokeWidth (const String& strokeWidth) const noexcept
  757. {
  758. return transform.getScaleFactor() * getCoordLength (strokeWidth, viewBoxW);
  759. }
  760. PathStrokeType getStrokeFor (const XmlPath& xml) const
  761. {
  762. return PathStrokeType (getStrokeWidth (getStyleAttribute (xml, "stroke-width", "1")),
  763. getJointStyle (getStyleAttribute (xml, "stroke-linejoin")),
  764. getEndCapStyle (getStyleAttribute (xml, "stroke-linecap")));
  765. }
  766. //==============================================================================
  767. Drawable* parseText (const XmlPath& xml, bool shouldParseTransform)
  768. {
  769. if (shouldParseTransform && xml->hasAttribute ("transform"))
  770. {
  771. SVGState newState (*this);
  772. newState.addTransform (xml);
  773. return newState.parseText (xml, false);
  774. }
  775. Array<float> xCoords, yCoords, dxCoords, dyCoords;
  776. getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true);
  777. getCoordList (yCoords, getInheritedAttribute (xml, "y"), true, false);
  778. getCoordList (dxCoords, getInheritedAttribute (xml, "dx"), true, true);
  779. getCoordList (dyCoords, getInheritedAttribute (xml, "dy"), true, false);
  780. auto font = getFont (xml);
  781. auto anchorStr = getStyleAttribute(xml, "text-anchor");
  782. auto dc = new DrawableComposite();
  783. setCommonAttributes (*dc, xml);
  784. forEachXmlChildElement (*xml, e)
  785. {
  786. if (e->isTextElement())
  787. {
  788. auto text = e->getText().trim();
  789. auto dt = new DrawableText();
  790. dc->addAndMakeVisible (dt);
  791. dt->setText (text);
  792. dt->setFont (font, true);
  793. dt->setTransform (transform);
  794. dt->setColour (parseColour (xml, "fill", Colours::black)
  795. .withMultipliedAlpha (getStyleAttribute (xml, "fill-opacity", "1").getFloatValue()));
  796. Rectangle<float> bounds (xCoords[0], yCoords[0] - font.getAscent(),
  797. font.getStringWidthFloat (text), font.getHeight());
  798. if (anchorStr == "middle") bounds.setX (bounds.getX() - bounds.getWidth() / 2.0f);
  799. else if (anchorStr == "end") bounds.setX (bounds.getX() - bounds.getWidth());
  800. dt->setBoundingBox (bounds);
  801. }
  802. else if (e->hasTagNameIgnoringNamespace ("tspan"))
  803. {
  804. dc->addAndMakeVisible (parseText (xml.getChild (e), true));
  805. }
  806. }
  807. return dc;
  808. }
  809. Font getFont (const XmlPath& xml) const
  810. {
  811. Font f;
  812. auto family = getStyleAttribute (xml, "font-family").unquoted();
  813. if (family.isNotEmpty())
  814. f.setTypefaceName (family);
  815. if (getStyleAttribute (xml, "font-style").containsIgnoreCase ("italic"))
  816. f.setItalic (true);
  817. if (getStyleAttribute (xml, "font-weight").containsIgnoreCase ("bold"))
  818. f.setBold (true);
  819. return f.withPointHeight (getCoordLength (getStyleAttribute (xml, "font-size"), 1.0f));
  820. }
  821. //==============================================================================
  822. void addTransform (const XmlPath& xml)
  823. {
  824. transform = parseTransform (xml->getStringAttribute ("transform"))
  825. .followedBy (transform);
  826. }
  827. //==============================================================================
  828. bool parseCoord (String::CharPointerType& s, float& value, const bool allowUnits, const bool isX) const
  829. {
  830. String number;
  831. if (! parseNextNumber (s, number, allowUnits))
  832. {
  833. value = 0;
  834. return false;
  835. }
  836. value = getCoordLength (number, isX ? viewBoxW : viewBoxH);
  837. return true;
  838. }
  839. bool parseCoords (String::CharPointerType& s, Point<float>& p, const bool allowUnits) const
  840. {
  841. return parseCoord (s, p.x, allowUnits, true)
  842. && parseCoord (s, p.y, allowUnits, false);
  843. }
  844. bool parseCoordsOrSkip (String::CharPointerType& s, Point<float>& p, const bool allowUnits) const
  845. {
  846. if (parseCoords (s, p, allowUnits))
  847. return true;
  848. if (! s.isEmpty()) ++s;
  849. return false;
  850. }
  851. float getCoordLength (const String& s, const float sizeForProportions) const noexcept
  852. {
  853. float n = s.getFloatValue();
  854. const int len = s.length();
  855. if (len > 2)
  856. {
  857. const float dpi = 96.0f;
  858. const juce_wchar n1 = s [len - 2];
  859. const juce_wchar n2 = s [len - 1];
  860. if (n1 == 'i' && n2 == 'n') n *= dpi;
  861. else if (n1 == 'm' && n2 == 'm') n *= dpi / 25.4f;
  862. else if (n1 == 'c' && n2 == 'm') n *= dpi / 2.54f;
  863. else if (n1 == 'p' && n2 == 'c') n *= 15.0f;
  864. else if (n2 == '%') n *= 0.01f * sizeForProportions;
  865. }
  866. return n;
  867. }
  868. float getCoordLength (const XmlPath& xml, const char* attName, const float sizeForProportions) const noexcept
  869. {
  870. return getCoordLength (xml->getStringAttribute (attName), sizeForProportions);
  871. }
  872. void getCoordList (Array<float>& coords, const String& list, bool allowUnits, const bool isX) const
  873. {
  874. auto text = list.getCharPointer();
  875. float value;
  876. while (parseCoord (text, value, allowUnits, isX))
  877. coords.add (value);
  878. }
  879. //==============================================================================
  880. void parseCSSStyle (const XmlPath& xml)
  881. {
  882. cssStyleText = xml->getAllSubText() + "\n" + cssStyleText;
  883. }
  884. void parseDefs (const XmlPath& xml)
  885. {
  886. if (auto* style = xml->getChildByName ("style"))
  887. parseCSSStyle (xml.getChild (style));
  888. }
  889. static String::CharPointerType findStyleItem (String::CharPointerType source, String::CharPointerType name)
  890. {
  891. auto nameLength = (int) name.length();
  892. while (! source.isEmpty())
  893. {
  894. if (source.getAndAdvance() == '.'
  895. && CharacterFunctions::compareIgnoreCaseUpTo (source, name, nameLength) == 0)
  896. {
  897. auto endOfName = (source + nameLength).findEndOfWhitespace();
  898. if (*endOfName == '{')
  899. return endOfName;
  900. if (*endOfName == ',')
  901. return CharacterFunctions::find (endOfName, (juce_wchar) '{');
  902. }
  903. }
  904. return source;
  905. }
  906. String getStyleAttribute (const XmlPath& xml, StringRef attributeName, const String& defaultValue = String()) const
  907. {
  908. if (xml->hasAttribute (attributeName))
  909. return xml->getStringAttribute (attributeName, defaultValue);
  910. auto styleAtt = xml->getStringAttribute ("style");
  911. if (styleAtt.isNotEmpty())
  912. {
  913. auto value = getAttributeFromStyleList (styleAtt, attributeName, {});
  914. if (value.isNotEmpty())
  915. return value;
  916. }
  917. else if (xml->hasAttribute ("class"))
  918. {
  919. for (auto i = cssStyleText.getCharPointer();;)
  920. {
  921. auto openBrace = findStyleItem (i, xml->getStringAttribute ("class").getCharPointer());
  922. if (openBrace.isEmpty())
  923. break;
  924. auto closeBrace = CharacterFunctions::find (openBrace, (juce_wchar) '}');
  925. if (closeBrace.isEmpty())
  926. break;
  927. auto value = getAttributeFromStyleList (String (openBrace + 1, closeBrace),
  928. attributeName, defaultValue);
  929. if (value.isNotEmpty())
  930. return value;
  931. i = closeBrace + 1;
  932. }
  933. }
  934. if (xml.parent != nullptr)
  935. return getStyleAttribute (*xml.parent, attributeName, defaultValue);
  936. return defaultValue;
  937. }
  938. String getInheritedAttribute (const XmlPath& xml, StringRef attributeName) const
  939. {
  940. if (xml->hasAttribute (attributeName))
  941. return xml->getStringAttribute (attributeName);
  942. if (xml.parent != nullptr)
  943. return getInheritedAttribute (*xml.parent, attributeName);
  944. return {};
  945. }
  946. static int parsePlacementFlags (const String& align) noexcept
  947. {
  948. if (align.isEmpty())
  949. return 0;
  950. if (isNone (align))
  951. return RectanglePlacement::stretchToFit;
  952. return (align.containsIgnoreCase ("slice") ? RectanglePlacement::fillDestination : 0)
  953. | (align.containsIgnoreCase ("xMin") ? RectanglePlacement::xLeft
  954. : (align.containsIgnoreCase ("xMax") ? RectanglePlacement::xRight
  955. : RectanglePlacement::xMid))
  956. | (align.containsIgnoreCase ("yMin") ? RectanglePlacement::yTop
  957. : (align.containsIgnoreCase ("yMax") ? RectanglePlacement::yBottom
  958. : RectanglePlacement::yMid));
  959. }
  960. //==============================================================================
  961. static bool isIdentifierChar (const juce_wchar c)
  962. {
  963. return CharacterFunctions::isLetter (c) || c == '-';
  964. }
  965. static String getAttributeFromStyleList (const String& list, StringRef attributeName, const String& defaultValue)
  966. {
  967. int i = 0;
  968. for (;;)
  969. {
  970. i = list.indexOf (i, attributeName);
  971. if (i < 0)
  972. break;
  973. if ((i == 0 || (i > 0 && ! isIdentifierChar (list [i - 1])))
  974. && ! isIdentifierChar (list [i + attributeName.length()]))
  975. {
  976. i = list.indexOfChar (i, ':');
  977. if (i < 0)
  978. break;
  979. int end = list.indexOfChar (i, ';');
  980. if (end < 0)
  981. end = 0x7ffff;
  982. return list.substring (i + 1, end).trim();
  983. }
  984. ++i;
  985. }
  986. return defaultValue;
  987. }
  988. //==============================================================================
  989. static bool isStartOfNumber (juce_wchar c) noexcept
  990. {
  991. return CharacterFunctions::isDigit (c) || c == '-' || c == '+';
  992. }
  993. static bool parseNextNumber (String::CharPointerType& text, String& value, const bool allowUnits)
  994. {
  995. String::CharPointerType s (text);
  996. while (s.isWhitespace() || *s == ',')
  997. ++s;
  998. auto start = s;
  999. if (isStartOfNumber (*s))
  1000. ++s;
  1001. while (s.isDigit())
  1002. ++s;
  1003. if (*s == '.')
  1004. {
  1005. ++s;
  1006. while (s.isDigit())
  1007. ++s;
  1008. }
  1009. if ((*s == 'e' || *s == 'E') && isStartOfNumber (s[1]))
  1010. {
  1011. s += 2;
  1012. while (s.isDigit())
  1013. ++s;
  1014. }
  1015. if (allowUnits)
  1016. while (s.isLetter())
  1017. ++s;
  1018. if (s == start)
  1019. {
  1020. text = s;
  1021. return false;
  1022. }
  1023. value = String (start, s);
  1024. while (s.isWhitespace() || *s == ',')
  1025. ++s;
  1026. text = s;
  1027. return true;
  1028. }
  1029. //==============================================================================
  1030. Colour parseColour (const XmlPath& xml, StringRef attributeName, const Colour defaultColour) const
  1031. {
  1032. auto text = getStyleAttribute (xml, attributeName);
  1033. if (text.startsWithChar ('#'))
  1034. {
  1035. uint32 hex[6] = { 0 };
  1036. int numChars = 0;
  1037. auto s = text.getCharPointer();
  1038. while (numChars < 6)
  1039. {
  1040. auto hexValue = CharacterFunctions::getHexDigitValue (*++s);
  1041. if (hexValue >= 0)
  1042. hex [numChars++] = (uint32) hexValue;
  1043. else
  1044. break;
  1045. }
  1046. if (numChars <= 3)
  1047. return Colour ((uint8) (hex[0] * 0x11),
  1048. (uint8) (hex[1] * 0x11),
  1049. (uint8) (hex[2] * 0x11));
  1050. return Colour ((uint8) ((hex[0] << 4) + hex[1]),
  1051. (uint8) ((hex[2] << 4) + hex[3]),
  1052. (uint8) ((hex[4] << 4) + hex[5]));
  1053. }
  1054. if (text.startsWith ("rgb"))
  1055. {
  1056. auto openBracket = text.indexOfChar ('(');
  1057. auto closeBracket = text.indexOfChar (openBracket, ')');
  1058. if (openBracket >= 3 && closeBracket > openBracket)
  1059. {
  1060. StringArray tokens;
  1061. tokens.addTokens (text.substring (openBracket + 1, closeBracket), ",", "");
  1062. tokens.trim();
  1063. tokens.removeEmptyStrings();
  1064. if (tokens[0].containsChar ('%'))
  1065. return Colour ((uint8) roundToInt (2.55 * tokens[0].getDoubleValue()),
  1066. (uint8) roundToInt (2.55 * tokens[1].getDoubleValue()),
  1067. (uint8) roundToInt (2.55 * tokens[2].getDoubleValue()));
  1068. return Colour ((uint8) tokens[0].getIntValue(),
  1069. (uint8) tokens[1].getIntValue(),
  1070. (uint8) tokens[2].getIntValue());
  1071. }
  1072. }
  1073. if (text == "inherit")
  1074. {
  1075. for (const XmlPath* p = xml.parent; p != nullptr; p = p->parent)
  1076. if (getStyleAttribute (*p, attributeName).isNotEmpty())
  1077. return parseColour (*p, attributeName, defaultColour);
  1078. }
  1079. return Colours::findColourForName (text, defaultColour);
  1080. }
  1081. static AffineTransform parseTransform (String t)
  1082. {
  1083. AffineTransform result;
  1084. while (t.isNotEmpty())
  1085. {
  1086. StringArray tokens;
  1087. tokens.addTokens (t.fromFirstOccurrenceOf ("(", false, false)
  1088. .upToFirstOccurrenceOf (")", false, false),
  1089. ", ", "");
  1090. tokens.removeEmptyStrings (true);
  1091. float numbers[6];
  1092. for (int i = 0; i < numElementsInArray (numbers); ++i)
  1093. numbers[i] = tokens[i].getFloatValue();
  1094. AffineTransform trans;
  1095. if (t.startsWithIgnoreCase ("matrix"))
  1096. {
  1097. trans = AffineTransform (numbers[0], numbers[2], numbers[4],
  1098. numbers[1], numbers[3], numbers[5]);
  1099. }
  1100. else if (t.startsWithIgnoreCase ("translate"))
  1101. {
  1102. trans = AffineTransform::translation (numbers[0], numbers[1]);
  1103. }
  1104. else if (t.startsWithIgnoreCase ("scale"))
  1105. {
  1106. trans = AffineTransform::scale (numbers[0], numbers[tokens.size() > 1 ? 1 : 0]);
  1107. }
  1108. else if (t.startsWithIgnoreCase ("rotate"))
  1109. {
  1110. trans = AffineTransform::rotation (degreesToRadians (numbers[0]), numbers[1], numbers[2]);
  1111. }
  1112. else if (t.startsWithIgnoreCase ("skewX"))
  1113. {
  1114. trans = AffineTransform::shear (std::tan (degreesToRadians (numbers[0])), 0.0f);
  1115. }
  1116. else if (t.startsWithIgnoreCase ("skewY"))
  1117. {
  1118. trans = AffineTransform::shear (0.0f, std::tan (degreesToRadians (numbers[0])));
  1119. }
  1120. result = trans.followedBy (result);
  1121. t = t.fromFirstOccurrenceOf (")", false, false).trimStart();
  1122. }
  1123. return result;
  1124. }
  1125. static void endpointToCentreParameters (const double x1, const double y1,
  1126. const double x2, const double y2,
  1127. const double angle,
  1128. const bool largeArc, const bool sweep,
  1129. double& rx, double& ry,
  1130. double& centreX, double& centreY,
  1131. double& startAngle, double& deltaAngle) noexcept
  1132. {
  1133. const double midX = (x1 - x2) * 0.5;
  1134. const double midY = (y1 - y2) * 0.5;
  1135. const double cosAngle = std::cos (angle);
  1136. const double sinAngle = std::sin (angle);
  1137. const double xp = cosAngle * midX + sinAngle * midY;
  1138. const double yp = cosAngle * midY - sinAngle * midX;
  1139. const double xp2 = xp * xp;
  1140. const double yp2 = yp * yp;
  1141. double rx2 = rx * rx;
  1142. double ry2 = ry * ry;
  1143. const double s = (xp2 / rx2) + (yp2 / ry2);
  1144. double c;
  1145. if (s <= 1.0)
  1146. {
  1147. c = std::sqrt (jmax (0.0, ((rx2 * ry2) - (rx2 * yp2) - (ry2 * xp2))
  1148. / (( rx2 * yp2) + (ry2 * xp2))));
  1149. if (largeArc == sweep)
  1150. c = -c;
  1151. }
  1152. else
  1153. {
  1154. const double s2 = std::sqrt (s);
  1155. rx *= s2;
  1156. ry *= s2;
  1157. c = 0;
  1158. }
  1159. const double cpx = ((rx * yp) / ry) * c;
  1160. const double cpy = ((-ry * xp) / rx) * c;
  1161. centreX = ((x1 + x2) * 0.5) + (cosAngle * cpx) - (sinAngle * cpy);
  1162. centreY = ((y1 + y2) * 0.5) + (sinAngle * cpx) + (cosAngle * cpy);
  1163. const double ux = (xp - cpx) / rx;
  1164. const double uy = (yp - cpy) / ry;
  1165. const double vx = (-xp - cpx) / rx;
  1166. const double vy = (-yp - cpy) / ry;
  1167. const double length = juce_hypot (ux, uy);
  1168. startAngle = acos (jlimit (-1.0, 1.0, ux / length));
  1169. if (uy < 0)
  1170. startAngle = -startAngle;
  1171. startAngle += double_Pi * 0.5;
  1172. deltaAngle = acos (jlimit (-1.0, 1.0, ((ux * vx) + (uy * vy))
  1173. / (length * juce_hypot (vx, vy))));
  1174. if ((ux * vy) - (uy * vx) < 0)
  1175. deltaAngle = -deltaAngle;
  1176. if (sweep)
  1177. {
  1178. if (deltaAngle < 0)
  1179. deltaAngle += double_Pi * 2.0;
  1180. }
  1181. else
  1182. {
  1183. if (deltaAngle > 0)
  1184. deltaAngle -= double_Pi * 2.0;
  1185. }
  1186. deltaAngle = fmod (deltaAngle, double_Pi * 2.0);
  1187. }
  1188. SVGState& operator= (const SVGState&) JUCE_DELETED_FUNCTION;
  1189. };
  1190. //==============================================================================
  1191. Drawable* Drawable::createFromSVG (const XmlElement& svgDocument)
  1192. {
  1193. if (! svgDocument.hasTagNameIgnoringNamespace ("svg"))
  1194. return nullptr;
  1195. SVGState state (&svgDocument);
  1196. return state.parseSVGElement (SVGState::XmlPath (&svgDocument, nullptr));
  1197. }
  1198. Path Drawable::parseSVGPath (const String& svgPath)
  1199. {
  1200. SVGState state (nullptr);
  1201. Path p;
  1202. state.parsePathString (p, svgPath);
  1203. return p;
  1204. }