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.

1507 lines
51KB

  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 void setCommonAttributes (Drawable& d, const XmlPath& xml)
  333. {
  334. auto compID = xml->getStringAttribute ("id");
  335. d.setName (compID);
  336. d.setComponentID (compID);
  337. if (xml->getStringAttribute ("display") == "none")
  338. d.setVisible (false);
  339. }
  340. //==============================================================================
  341. void parseSubElements (const XmlPath& xml, DrawableComposite& parentDrawable)
  342. {
  343. forEachXmlChildElement (*xml, e)
  344. parentDrawable.addAndMakeVisible (parseSubElement (xml.getChild (e)));
  345. }
  346. Drawable* parseSubElement (const XmlPath& xml)
  347. {
  348. {
  349. Path path;
  350. if (parsePathElement (xml, path))
  351. return parseShape (xml, path);
  352. }
  353. auto tag = xml->getTagNameWithoutNamespace();
  354. if (tag == "g") return parseGroupElement (xml);
  355. if (tag == "svg") return parseSVGElement (xml);
  356. if (tag == "text") return parseText (xml, true);
  357. if (tag == "switch") return parseSwitch (xml);
  358. if (tag == "a") return parseLinkElement (xml);
  359. if (tag == "style") parseCSSStyle (xml);
  360. if (tag == "defs") parseDefs (xml);
  361. return nullptr;
  362. }
  363. bool parsePathElement (const XmlPath& xml, Path& path) const
  364. {
  365. const String tag (xml->getTagNameWithoutNamespace());
  366. if (tag == "path") { parsePath (xml, path); return true; }
  367. if (tag == "rect") { parseRect (xml, path); return true; }
  368. if (tag == "circle") { parseCircle (xml, path); return true; }
  369. if (tag == "ellipse") { parseEllipse (xml, path); return true; }
  370. if (tag == "line") { parseLine (xml, path); return true; }
  371. if (tag == "polyline") { parsePolygon (xml, true, path); return true; }
  372. if (tag == "polygon") { parsePolygon (xml, false, path); return true; }
  373. if (tag == "use") { parseUse (xml, path); return true; }
  374. return false;
  375. }
  376. DrawableComposite* parseSwitch (const XmlPath& xml)
  377. {
  378. if (auto* group = xml->getChildByName ("g"))
  379. return parseGroupElement (xml.getChild (group));
  380. return nullptr;
  381. }
  382. DrawableComposite* parseGroupElement (const XmlPath& xml)
  383. {
  384. auto drawable = new DrawableComposite();
  385. setCommonAttributes (*drawable, xml);
  386. if (xml->hasAttribute ("transform"))
  387. {
  388. SVGState newState (*this);
  389. newState.addTransform (xml);
  390. newState.parseSubElements (xml, *drawable);
  391. }
  392. else
  393. {
  394. parseSubElements (xml, *drawable);
  395. }
  396. drawable->resetContentAreaAndBoundingBoxToFitChildren();
  397. return drawable;
  398. }
  399. DrawableComposite* parseLinkElement (const XmlPath& xml)
  400. {
  401. return parseGroupElement (xml); // TODO: support for making this clickable
  402. }
  403. //==============================================================================
  404. void parsePath (const XmlPath& xml, Path& path) const
  405. {
  406. parsePathString (path, xml->getStringAttribute ("d"));
  407. if (getStyleAttribute (xml, "fill-rule").trim().equalsIgnoreCase ("evenodd"))
  408. path.setUsingNonZeroWinding (false);
  409. }
  410. void parseRect (const XmlPath& xml, Path& rect) const
  411. {
  412. const bool hasRX = xml->hasAttribute ("rx");
  413. const bool hasRY = xml->hasAttribute ("ry");
  414. if (hasRX || hasRY)
  415. {
  416. float rx = getCoordLength (xml, "rx", viewBoxW);
  417. float ry = getCoordLength (xml, "ry", viewBoxH);
  418. if (! hasRX)
  419. rx = ry;
  420. else if (! hasRY)
  421. ry = rx;
  422. rect.addRoundedRectangle (getCoordLength (xml, "x", viewBoxW),
  423. getCoordLength (xml, "y", viewBoxH),
  424. getCoordLength (xml, "width", viewBoxW),
  425. getCoordLength (xml, "height", viewBoxH),
  426. rx, ry);
  427. }
  428. else
  429. {
  430. rect.addRectangle (getCoordLength (xml, "x", viewBoxW),
  431. getCoordLength (xml, "y", viewBoxH),
  432. getCoordLength (xml, "width", viewBoxW),
  433. getCoordLength (xml, "height", viewBoxH));
  434. }
  435. }
  436. void parseCircle (const XmlPath& xml, Path& circle) const
  437. {
  438. const float cx = getCoordLength (xml, "cx", viewBoxW);
  439. const float cy = getCoordLength (xml, "cy", viewBoxH);
  440. const float radius = getCoordLength (xml, "r", viewBoxW);
  441. circle.addEllipse (cx - radius, cy - radius, radius * 2.0f, radius * 2.0f);
  442. }
  443. void parseEllipse (const XmlPath& xml, Path& ellipse) const
  444. {
  445. const float cx = getCoordLength (xml, "cx", viewBoxW);
  446. const float cy = getCoordLength (xml, "cy", viewBoxH);
  447. const float radiusX = getCoordLength (xml, "rx", viewBoxW);
  448. const float radiusY = getCoordLength (xml, "ry", viewBoxH);
  449. ellipse.addEllipse (cx - radiusX, cy - radiusY, radiusX * 2.0f, radiusY * 2.0f);
  450. }
  451. void parseLine (const XmlPath& xml, Path& line) const
  452. {
  453. const float x1 = getCoordLength (xml, "x1", viewBoxW);
  454. const float y1 = getCoordLength (xml, "y1", viewBoxH);
  455. const float x2 = getCoordLength (xml, "x2", viewBoxW);
  456. const float y2 = getCoordLength (xml, "y2", viewBoxH);
  457. line.startNewSubPath (x1, y1);
  458. line.lineTo (x2, y2);
  459. }
  460. void parsePolygon (const XmlPath& xml, const bool isPolyline, Path& path) const
  461. {
  462. auto pointsAtt = xml->getStringAttribute ("points");
  463. auto points = pointsAtt.getCharPointer();
  464. Point<float> p;
  465. if (parseCoords (points, p, true))
  466. {
  467. Point<float> first (p), last;
  468. path.startNewSubPath (first);
  469. while (parseCoords (points, p, true))
  470. {
  471. last = p;
  472. path.lineTo (p);
  473. }
  474. if ((! isPolyline) || first == last)
  475. path.closeSubPath();
  476. }
  477. }
  478. void parseUse (const XmlPath& xml, Path& path) const
  479. {
  480. auto link = xml->getStringAttribute ("xlink:href");
  481. if (link.startsWithChar ('#'))
  482. {
  483. auto linkedID = link.substring (1);
  484. UsePathOp op = { this, &path };
  485. topLevelXml.applyOperationToChildWithID (linkedID, op);
  486. }
  487. }
  488. static String parseURL (const String& str)
  489. {
  490. if (str.startsWithIgnoreCase ("url"))
  491. return str.fromFirstOccurrenceOf ("#", false, false)
  492. .upToLastOccurrenceOf (")", false, false).trim();
  493. return {};
  494. }
  495. //==============================================================================
  496. Drawable* parseShape (const XmlPath& xml, Path& path,
  497. const bool shouldParseTransform = true) const
  498. {
  499. if (shouldParseTransform && xml->hasAttribute ("transform"))
  500. {
  501. SVGState newState (*this);
  502. newState.addTransform (xml);
  503. return newState.parseShape (xml, path, false);
  504. }
  505. auto dp = new DrawablePath();
  506. setCommonAttributes (*dp, xml);
  507. dp->setFill (Colours::transparentBlack);
  508. path.applyTransform (transform);
  509. dp->setPath (path);
  510. dp->setFill (getPathFillType (path, xml, "fill",
  511. getStyleAttribute (xml, "fill-opacity"),
  512. getStyleAttribute (xml, "opacity"),
  513. pathContainsClosedSubPath (path) ? Colours::black
  514. : Colours::transparentBlack));
  515. auto strokeType = getStyleAttribute (xml, "stroke");
  516. if (strokeType.isNotEmpty() && ! strokeType.equalsIgnoreCase ("none"))
  517. {
  518. dp->setStrokeFill (getPathFillType (path, xml, "stroke",
  519. getStyleAttribute (xml, "stroke-opacity"),
  520. getStyleAttribute (xml, "opacity"),
  521. Colours::transparentBlack));
  522. dp->setStrokeType (getStrokeFor (xml));
  523. }
  524. auto strokeDashArray = getStyleAttribute (xml, "stroke-dasharray");
  525. if (strokeDashArray.isNotEmpty())
  526. parseDashArray (strokeDashArray, *dp);
  527. parseClipPath (xml, *dp);
  528. return dp;
  529. }
  530. static bool pathContainsClosedSubPath (const Path& path) noexcept
  531. {
  532. for (Path::Iterator iter (path); iter.next();)
  533. if (iter.elementType == Path::Iterator::closePath)
  534. return true;
  535. return false;
  536. }
  537. void parseDashArray (const String& dashList, DrawablePath& dp) const
  538. {
  539. if (dashList.equalsIgnoreCase ("null") || dashList.equalsIgnoreCase ("none"))
  540. return;
  541. Array<float> dashLengths;
  542. for (auto t = dashList.getCharPointer();;)
  543. {
  544. float value;
  545. if (! parseCoord (t, value, true, true))
  546. break;
  547. dashLengths.add (value);
  548. t = t.findEndOfWhitespace();
  549. if (*t == ',')
  550. ++t;
  551. }
  552. if (dashLengths.size() > 0)
  553. {
  554. auto* dashes = dashLengths.getRawDataPointer();
  555. for (int i = 0; i < dashLengths.size(); ++i)
  556. {
  557. if (dashes[i] <= 0) // SVG uses zero-length dashes to mean a dotted line
  558. {
  559. if (dashLengths.size() == 1)
  560. return;
  561. const float nonZeroLength = 0.001f;
  562. dashes[i] = nonZeroLength;
  563. const int pairedIndex = i ^ 1;
  564. if (isPositiveAndBelow (pairedIndex, dashLengths.size())
  565. && dashes[pairedIndex] > nonZeroLength)
  566. dashes[pairedIndex] -= nonZeroLength;
  567. }
  568. }
  569. dp.setDashLengths (dashLengths);
  570. }
  571. }
  572. void parseClipPath (const XmlPath& xml, Drawable& d) const
  573. {
  574. const String clipPath (getStyleAttribute (xml, "clip-path"));
  575. if (clipPath.isNotEmpty())
  576. {
  577. auto urlID = parseURL (clipPath);
  578. if (urlID.isNotEmpty())
  579. {
  580. GetClipPathOp op = { this, &d };
  581. topLevelXml.applyOperationToChildWithID (urlID, op);
  582. }
  583. }
  584. }
  585. void applyClipPath (Drawable& target, const XmlPath& xmlPath) const
  586. {
  587. if (xmlPath->hasTagNameIgnoringNamespace ("clipPath"))
  588. {
  589. // TODO: implement clipping..
  590. ignoreUnused (target);
  591. }
  592. }
  593. void addGradientStopsIn (ColourGradient& cg, const XmlPath& fillXml) const
  594. {
  595. if (fillXml.xml != nullptr)
  596. {
  597. forEachXmlChildElementWithTagName (*fillXml, e, "stop")
  598. {
  599. auto col = parseColour (fillXml.getChild (e), "stop-color", Colours::black);
  600. auto opacity = getStyleAttribute (fillXml.getChild (e), "stop-opacity", "1");
  601. col = col.withMultipliedAlpha (jlimit (0.0f, 1.0f, opacity.getFloatValue()));
  602. double offset = e->getDoubleAttribute ("offset");
  603. if (e->getStringAttribute ("offset").containsChar ('%'))
  604. offset *= 0.01;
  605. cg.addColour (jlimit (0.0, 1.0, offset), col);
  606. }
  607. }
  608. }
  609. FillType getGradientFillType (const XmlPath& fillXml,
  610. const Path& path,
  611. const float opacity) const
  612. {
  613. ColourGradient gradient;
  614. {
  615. auto link = fillXml->getStringAttribute ("xlink:href");
  616. if (link.startsWithChar ('#'))
  617. {
  618. SetGradientStopsOp op = { this, &gradient, };
  619. topLevelXml.applyOperationToChildWithID (link.substring (1), op);
  620. }
  621. }
  622. addGradientStopsIn (gradient, fillXml);
  623. if (int numColours = gradient.getNumColours())
  624. {
  625. if (gradient.getColourPosition (0) > 0)
  626. gradient.addColour (0.0, gradient.getColour (0));
  627. if (gradient.getColourPosition (numColours - 1) < 1.0)
  628. gradient.addColour (1.0, gradient.getColour (numColours - 1));
  629. }
  630. else
  631. {
  632. gradient.addColour (0.0, Colours::black);
  633. gradient.addColour (1.0, Colours::black);
  634. }
  635. if (opacity < 1.0f)
  636. gradient.multiplyOpacity (opacity);
  637. jassert (gradient.getNumColours() > 0);
  638. gradient.isRadial = fillXml->hasTagNameIgnoringNamespace ("radialGradient");
  639. float gradientWidth = viewBoxW;
  640. float gradientHeight = viewBoxH;
  641. float dx = 0.0f;
  642. float dy = 0.0f;
  643. const bool userSpace = fillXml->getStringAttribute ("gradientUnits").equalsIgnoreCase ("userSpaceOnUse");
  644. if (! userSpace)
  645. {
  646. auto bounds = path.getBounds();
  647. dx = bounds.getX();
  648. dy = bounds.getY();
  649. gradientWidth = bounds.getWidth();
  650. gradientHeight = bounds.getHeight();
  651. }
  652. if (gradient.isRadial)
  653. {
  654. if (userSpace)
  655. gradient.point1.setXY (dx + getCoordLength (fillXml->getStringAttribute ("cx", "50%"), gradientWidth),
  656. dy + getCoordLength (fillXml->getStringAttribute ("cy", "50%"), gradientHeight));
  657. else
  658. gradient.point1.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("cx", "50%"), 1.0f),
  659. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("cy", "50%"), 1.0f));
  660. auto radius = getCoordLength (fillXml->getStringAttribute ("r", "50%"), gradientWidth);
  661. gradient.point2 = gradient.point1 + Point<float> (radius, 0.0f);
  662. //xxx (the fx, fy focal point isn't handled properly here..)
  663. }
  664. else
  665. {
  666. if (userSpace)
  667. {
  668. gradient.point1.setXY (dx + getCoordLength (fillXml->getStringAttribute ("x1", "0%"), gradientWidth),
  669. dy + getCoordLength (fillXml->getStringAttribute ("y1", "0%"), gradientHeight));
  670. gradient.point2.setXY (dx + getCoordLength (fillXml->getStringAttribute ("x2", "100%"), gradientWidth),
  671. dy + getCoordLength (fillXml->getStringAttribute ("y2", "0%"), gradientHeight));
  672. }
  673. else
  674. {
  675. gradient.point1.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("x1", "0%"), 1.0f),
  676. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("y1", "0%"), 1.0f));
  677. gradient.point2.setXY (dx + gradientWidth * getCoordLength (fillXml->getStringAttribute ("x2", "100%"), 1.0f),
  678. dy + gradientHeight * getCoordLength (fillXml->getStringAttribute ("y2", "0%"), 1.0f));
  679. }
  680. if (gradient.point1 == gradient.point2)
  681. return Colour (gradient.getColour (gradient.getNumColours() - 1));
  682. }
  683. FillType type (gradient);
  684. auto gradientTransform = parseTransform (fillXml->getStringAttribute ("gradientTransform"))
  685. .followedBy (transform);
  686. if (gradient.isRadial)
  687. {
  688. type.transform = gradientTransform;
  689. }
  690. else
  691. {
  692. // Transform the perpendicular vector into the new coordinate space for the gradient.
  693. // This vector is now the slope of the linear gradient as it should appear in the new coord space
  694. auto perpendicular = Point<float> (gradient.point2.y - gradient.point1.y,
  695. gradient.point1.x - gradient.point2.x)
  696. .transformedBy (gradientTransform.withAbsoluteTranslation (0, 0));
  697. auto newGradPoint1 = gradient.point1.transformedBy (gradientTransform);
  698. auto newGradPoint2 = gradient.point2.transformedBy (gradientTransform);
  699. // Project the transformed gradient vector onto the transformed slope of the linear
  700. // gradient as it should appear in the new coordinate space
  701. const float scale = perpendicular.getDotProduct (newGradPoint2 - newGradPoint1)
  702. / perpendicular.getDotProduct (perpendicular);
  703. type.gradient->point1 = newGradPoint1;
  704. type.gradient->point2 = newGradPoint2 - perpendicular * scale;
  705. }
  706. return type;
  707. }
  708. FillType getPathFillType (const Path& path,
  709. const XmlPath& xml,
  710. StringRef fillAttribute,
  711. const String& fillOpacity,
  712. const String& overallOpacity,
  713. const Colour defaultColour) const
  714. {
  715. float opacity = 1.0f;
  716. if (overallOpacity.isNotEmpty())
  717. opacity = jlimit (0.0f, 1.0f, overallOpacity.getFloatValue());
  718. if (fillOpacity.isNotEmpty())
  719. opacity *= (jlimit (0.0f, 1.0f, fillOpacity.getFloatValue()));
  720. String fill (getStyleAttribute (xml, fillAttribute));
  721. String urlID = parseURL (fill);
  722. if (urlID.isNotEmpty())
  723. {
  724. GetFillTypeOp op = { this, &path, opacity, FillType() };
  725. if (topLevelXml.applyOperationToChildWithID (urlID, op))
  726. return op.fillType;
  727. }
  728. if (fill.equalsIgnoreCase ("none"))
  729. return Colours::transparentBlack;
  730. return parseColour (xml, fillAttribute, defaultColour).withMultipliedAlpha (opacity);
  731. }
  732. static PathStrokeType::JointStyle getJointStyle (const String& join) noexcept
  733. {
  734. if (join.equalsIgnoreCase ("round")) return PathStrokeType::curved;
  735. if (join.equalsIgnoreCase ("bevel")) return PathStrokeType::beveled;
  736. return PathStrokeType::mitered;
  737. }
  738. static PathStrokeType::EndCapStyle getEndCapStyle (const String& cap) noexcept
  739. {
  740. if (cap.equalsIgnoreCase ("round")) return PathStrokeType::rounded;
  741. if (cap.equalsIgnoreCase ("square")) return PathStrokeType::square;
  742. return PathStrokeType::butt;
  743. }
  744. float getStrokeWidth (const String& strokeWidth) const noexcept
  745. {
  746. return transform.getScaleFactor() * getCoordLength (strokeWidth, viewBoxW);
  747. }
  748. PathStrokeType getStrokeFor (const XmlPath& xml) const
  749. {
  750. return PathStrokeType (getStrokeWidth (getStyleAttribute (xml, "stroke-width", "1")),
  751. getJointStyle (getStyleAttribute (xml, "stroke-linejoin")),
  752. getEndCapStyle (getStyleAttribute (xml, "stroke-linecap")));
  753. }
  754. //==============================================================================
  755. Drawable* parseText (const XmlPath& xml, bool shouldParseTransform)
  756. {
  757. if (shouldParseTransform && xml->hasAttribute ("transform"))
  758. {
  759. SVGState newState (*this);
  760. newState.addTransform (xml);
  761. return newState.parseText (xml, false);
  762. }
  763. Array<float> xCoords, yCoords, dxCoords, dyCoords;
  764. getCoordList (xCoords, getInheritedAttribute (xml, "x"), true, true);
  765. getCoordList (yCoords, getInheritedAttribute (xml, "y"), true, false);
  766. getCoordList (dxCoords, getInheritedAttribute (xml, "dx"), true, true);
  767. getCoordList (dyCoords, getInheritedAttribute (xml, "dy"), true, false);
  768. auto font = getFont (xml);
  769. auto anchorStr = getStyleAttribute(xml, "text-anchor");
  770. auto dc = new DrawableComposite();
  771. setCommonAttributes (*dc, xml);
  772. forEachXmlChildElement (*xml, e)
  773. {
  774. if (e->isTextElement())
  775. {
  776. auto text = e->getText().trim();
  777. auto dt = new DrawableText();
  778. dc->addAndMakeVisible (dt);
  779. dt->setText (text);
  780. dt->setFont (font, true);
  781. dt->setTransform (transform);
  782. dt->setColour (parseColour (xml, "fill", Colours::black)
  783. .withMultipliedAlpha (getStyleAttribute (xml, "fill-opacity", "1").getFloatValue()));
  784. Rectangle<float> bounds (xCoords[0], yCoords[0] - font.getAscent(),
  785. font.getStringWidthFloat (text), font.getHeight());
  786. if (anchorStr == "middle") bounds.setX (bounds.getX() - bounds.getWidth() / 2.0f);
  787. else if (anchorStr == "end") bounds.setX (bounds.getX() - bounds.getWidth());
  788. dt->setBoundingBox (bounds);
  789. }
  790. else if (e->hasTagNameIgnoringNamespace ("tspan"))
  791. {
  792. dc->addAndMakeVisible (parseText (xml.getChild (e), true));
  793. }
  794. }
  795. return dc;
  796. }
  797. Font getFont (const XmlPath& xml) const
  798. {
  799. auto fontSize = getCoordLength (getStyleAttribute (xml, "font-size"), 1.0f);
  800. int style = getStyleAttribute (xml, "font-style").containsIgnoreCase ("italic") ? Font::italic : Font::plain;
  801. if (getStyleAttribute (xml, "font-weight").containsIgnoreCase ("bold"))
  802. style |= Font::bold;
  803. auto family = getStyleAttribute (xml, "font-family");
  804. return family.isEmpty() ? Font (fontSize, style)
  805. : Font (family, fontSize, style);
  806. }
  807. //==============================================================================
  808. void addTransform (const XmlPath& xml)
  809. {
  810. transform = parseTransform (xml->getStringAttribute ("transform"))
  811. .followedBy (transform);
  812. }
  813. //==============================================================================
  814. bool parseCoord (String::CharPointerType& s, float& value, const bool allowUnits, const bool isX) const
  815. {
  816. String number;
  817. if (! parseNextNumber (s, number, allowUnits))
  818. {
  819. value = 0;
  820. return false;
  821. }
  822. value = getCoordLength (number, isX ? viewBoxW : viewBoxH);
  823. return true;
  824. }
  825. bool parseCoords (String::CharPointerType& s, Point<float>& p, const bool allowUnits) const
  826. {
  827. return parseCoord (s, p.x, allowUnits, true)
  828. && parseCoord (s, p.y, allowUnits, false);
  829. }
  830. bool parseCoordsOrSkip (String::CharPointerType& s, Point<float>& p, const bool allowUnits) const
  831. {
  832. if (parseCoords (s, p, allowUnits))
  833. return true;
  834. if (! s.isEmpty()) ++s;
  835. return false;
  836. }
  837. float getCoordLength (const String& s, const float sizeForProportions) const noexcept
  838. {
  839. float n = s.getFloatValue();
  840. const int len = s.length();
  841. if (len > 2)
  842. {
  843. const float dpi = 96.0f;
  844. const juce_wchar n1 = s [len - 2];
  845. const juce_wchar n2 = s [len - 1];
  846. if (n1 == 'i' && n2 == 'n') n *= dpi;
  847. else if (n1 == 'm' && n2 == 'm') n *= dpi / 25.4f;
  848. else if (n1 == 'c' && n2 == 'm') n *= dpi / 2.54f;
  849. else if (n1 == 'p' && n2 == 'c') n *= 15.0f;
  850. else if (n2 == '%') n *= 0.01f * sizeForProportions;
  851. }
  852. return n;
  853. }
  854. float getCoordLength (const XmlPath& xml, const char* attName, const float sizeForProportions) const noexcept
  855. {
  856. return getCoordLength (xml->getStringAttribute (attName), sizeForProportions);
  857. }
  858. void getCoordList (Array<float>& coords, const String& list, bool allowUnits, const bool isX) const
  859. {
  860. auto text = list.getCharPointer();
  861. float value;
  862. while (parseCoord (text, value, allowUnits, isX))
  863. coords.add (value);
  864. }
  865. //==============================================================================
  866. void parseCSSStyle (const XmlPath& xml)
  867. {
  868. cssStyleText = xml->getAllSubText() + "\n" + cssStyleText;
  869. }
  870. void parseDefs (const XmlPath& xml)
  871. {
  872. if (auto* style = xml->getChildByName ("style"))
  873. parseCSSStyle (xml.getChild (style));
  874. }
  875. static String::CharPointerType findStyleItem (String::CharPointerType source, String::CharPointerType name)
  876. {
  877. auto nameLength = (int) name.length();
  878. while (! source.isEmpty())
  879. {
  880. if (source.getAndAdvance() == '.'
  881. && CharacterFunctions::compareIgnoreCaseUpTo (source, name, nameLength) == 0)
  882. {
  883. auto endOfName = (source + nameLength).findEndOfWhitespace();
  884. if (*endOfName == '{')
  885. return endOfName;
  886. if (*endOfName == ',')
  887. return CharacterFunctions::find (endOfName, (juce_wchar) '{');
  888. }
  889. }
  890. return source;
  891. }
  892. String getStyleAttribute (const XmlPath& xml, StringRef attributeName, const String& defaultValue = String()) const
  893. {
  894. if (xml->hasAttribute (attributeName))
  895. return xml->getStringAttribute (attributeName, defaultValue);
  896. auto styleAtt = xml->getStringAttribute ("style");
  897. if (styleAtt.isNotEmpty())
  898. {
  899. auto value = getAttributeFromStyleList (styleAtt, attributeName, {});
  900. if (value.isNotEmpty())
  901. return value;
  902. }
  903. else if (xml->hasAttribute ("class"))
  904. {
  905. for (auto i = cssStyleText.getCharPointer();;)
  906. {
  907. auto openBrace = findStyleItem (i, xml->getStringAttribute ("class").getCharPointer());
  908. if (openBrace.isEmpty())
  909. break;
  910. auto closeBrace = CharacterFunctions::find (openBrace, (juce_wchar) '}');
  911. if (closeBrace.isEmpty())
  912. break;
  913. auto value = getAttributeFromStyleList (String (openBrace + 1, closeBrace),
  914. attributeName, defaultValue);
  915. if (value.isNotEmpty())
  916. return value;
  917. i = closeBrace + 1;
  918. }
  919. }
  920. if (xml.parent != nullptr)
  921. return getStyleAttribute (*xml.parent, attributeName, defaultValue);
  922. return defaultValue;
  923. }
  924. String getInheritedAttribute (const XmlPath& xml, StringRef attributeName) const
  925. {
  926. if (xml->hasAttribute (attributeName))
  927. return xml->getStringAttribute (attributeName);
  928. if (xml.parent != nullptr)
  929. return getInheritedAttribute (*xml.parent, attributeName);
  930. return {};
  931. }
  932. static int parsePlacementFlags (const String& align) noexcept
  933. {
  934. if (align.isEmpty())
  935. return 0;
  936. if (align.containsIgnoreCase ("none"))
  937. return RectanglePlacement::stretchToFit;
  938. return (align.containsIgnoreCase ("slice") ? RectanglePlacement::fillDestination : 0)
  939. | (align.containsIgnoreCase ("xMin") ? RectanglePlacement::xLeft
  940. : (align.containsIgnoreCase ("xMax") ? RectanglePlacement::xRight
  941. : RectanglePlacement::xMid))
  942. | (align.containsIgnoreCase ("yMin") ? RectanglePlacement::yTop
  943. : (align.containsIgnoreCase ("yMax") ? RectanglePlacement::yBottom
  944. : RectanglePlacement::yMid));
  945. }
  946. //==============================================================================
  947. static bool isIdentifierChar (const juce_wchar c)
  948. {
  949. return CharacterFunctions::isLetter (c) || c == '-';
  950. }
  951. static String getAttributeFromStyleList (const String& list, StringRef attributeName, const String& defaultValue)
  952. {
  953. int i = 0;
  954. for (;;)
  955. {
  956. i = list.indexOf (i, attributeName);
  957. if (i < 0)
  958. break;
  959. if ((i == 0 || (i > 0 && ! isIdentifierChar (list [i - 1])))
  960. && ! isIdentifierChar (list [i + attributeName.length()]))
  961. {
  962. i = list.indexOfChar (i, ':');
  963. if (i < 0)
  964. break;
  965. int end = list.indexOfChar (i, ';');
  966. if (end < 0)
  967. end = 0x7ffff;
  968. return list.substring (i + 1, end).trim();
  969. }
  970. ++i;
  971. }
  972. return defaultValue;
  973. }
  974. //==============================================================================
  975. static bool isStartOfNumber (juce_wchar c) noexcept
  976. {
  977. return CharacterFunctions::isDigit (c) || c == '-' || c == '+';
  978. }
  979. static bool parseNextNumber (String::CharPointerType& text, String& value, const bool allowUnits)
  980. {
  981. String::CharPointerType s (text);
  982. while (s.isWhitespace() || *s == ',')
  983. ++s;
  984. auto start = s;
  985. if (isStartOfNumber (*s))
  986. ++s;
  987. while (s.isDigit())
  988. ++s;
  989. if (*s == '.')
  990. {
  991. ++s;
  992. while (s.isDigit())
  993. ++s;
  994. }
  995. if ((*s == 'e' || *s == 'E') && isStartOfNumber (s[1]))
  996. {
  997. s += 2;
  998. while (s.isDigit())
  999. ++s;
  1000. }
  1001. if (allowUnits)
  1002. while (s.isLetter())
  1003. ++s;
  1004. if (s == start)
  1005. {
  1006. text = s;
  1007. return false;
  1008. }
  1009. value = String (start, s);
  1010. while (s.isWhitespace() || *s == ',')
  1011. ++s;
  1012. text = s;
  1013. return true;
  1014. }
  1015. //==============================================================================
  1016. Colour parseColour (const XmlPath& xml, StringRef attributeName, const Colour defaultColour) const
  1017. {
  1018. auto text = getStyleAttribute (xml, attributeName);
  1019. if (text.startsWithChar ('#'))
  1020. {
  1021. uint32 hex[6] = { 0 };
  1022. int numChars = 0;
  1023. auto s = text.getCharPointer();
  1024. while (numChars < 6)
  1025. {
  1026. auto hexValue = CharacterFunctions::getHexDigitValue (*++s);
  1027. if (hexValue >= 0)
  1028. hex [numChars++] = (uint32) hexValue;
  1029. else
  1030. break;
  1031. }
  1032. if (numChars <= 3)
  1033. return Colour ((uint8) (hex[0] * 0x11),
  1034. (uint8) (hex[1] * 0x11),
  1035. (uint8) (hex[2] * 0x11));
  1036. return Colour ((uint8) ((hex[0] << 4) + hex[1]),
  1037. (uint8) ((hex[2] << 4) + hex[3]),
  1038. (uint8) ((hex[4] << 4) + hex[5]));
  1039. }
  1040. if (text.startsWith ("rgb"))
  1041. {
  1042. auto openBracket = text.indexOfChar ('(');
  1043. auto closeBracket = text.indexOfChar (openBracket, ')');
  1044. if (openBracket >= 3 && closeBracket > openBracket)
  1045. {
  1046. StringArray tokens;
  1047. tokens.addTokens (text.substring (openBracket + 1, closeBracket), ",", "");
  1048. tokens.trim();
  1049. tokens.removeEmptyStrings();
  1050. if (tokens[0].containsChar ('%'))
  1051. return Colour ((uint8) roundToInt (2.55 * tokens[0].getDoubleValue()),
  1052. (uint8) roundToInt (2.55 * tokens[1].getDoubleValue()),
  1053. (uint8) roundToInt (2.55 * tokens[2].getDoubleValue()));
  1054. return Colour ((uint8) tokens[0].getIntValue(),
  1055. (uint8) tokens[1].getIntValue(),
  1056. (uint8) tokens[2].getIntValue());
  1057. }
  1058. }
  1059. if (text == "inherit")
  1060. {
  1061. for (const XmlPath* p = xml.parent; p != nullptr; p = p->parent)
  1062. if (getStyleAttribute (*p, attributeName).isNotEmpty())
  1063. return parseColour (*p, attributeName, defaultColour);
  1064. }
  1065. return Colours::findColourForName (text, defaultColour);
  1066. }
  1067. static AffineTransform parseTransform (String t)
  1068. {
  1069. AffineTransform result;
  1070. while (t.isNotEmpty())
  1071. {
  1072. StringArray tokens;
  1073. tokens.addTokens (t.fromFirstOccurrenceOf ("(", false, false)
  1074. .upToFirstOccurrenceOf (")", false, false),
  1075. ", ", "");
  1076. tokens.removeEmptyStrings (true);
  1077. float numbers[6];
  1078. for (int i = 0; i < numElementsInArray (numbers); ++i)
  1079. numbers[i] = tokens[i].getFloatValue();
  1080. AffineTransform trans;
  1081. if (t.startsWithIgnoreCase ("matrix"))
  1082. {
  1083. trans = AffineTransform (numbers[0], numbers[2], numbers[4],
  1084. numbers[1], numbers[3], numbers[5]);
  1085. }
  1086. else if (t.startsWithIgnoreCase ("translate"))
  1087. {
  1088. trans = AffineTransform::translation (numbers[0], numbers[1]);
  1089. }
  1090. else if (t.startsWithIgnoreCase ("scale"))
  1091. {
  1092. trans = AffineTransform::scale (numbers[0], numbers[tokens.size() > 1 ? 1 : 0]);
  1093. }
  1094. else if (t.startsWithIgnoreCase ("rotate"))
  1095. {
  1096. trans = AffineTransform::rotation (degreesToRadians (numbers[0]), numbers[1], numbers[2]);
  1097. }
  1098. else if (t.startsWithIgnoreCase ("skewX"))
  1099. {
  1100. trans = AffineTransform::shear (std::tan (degreesToRadians (numbers[0])), 0.0f);
  1101. }
  1102. else if (t.startsWithIgnoreCase ("skewY"))
  1103. {
  1104. trans = AffineTransform::shear (0.0f, std::tan (degreesToRadians (numbers[0])));
  1105. }
  1106. result = trans.followedBy (result);
  1107. t = t.fromFirstOccurrenceOf (")", false, false).trimStart();
  1108. }
  1109. return result;
  1110. }
  1111. static void endpointToCentreParameters (const double x1, const double y1,
  1112. const double x2, const double y2,
  1113. const double angle,
  1114. const bool largeArc, const bool sweep,
  1115. double& rx, double& ry,
  1116. double& centreX, double& centreY,
  1117. double& startAngle, double& deltaAngle) noexcept
  1118. {
  1119. const double midX = (x1 - x2) * 0.5;
  1120. const double midY = (y1 - y2) * 0.5;
  1121. const double cosAngle = std::cos (angle);
  1122. const double sinAngle = std::sin (angle);
  1123. const double xp = cosAngle * midX + sinAngle * midY;
  1124. const double yp = cosAngle * midY - sinAngle * midX;
  1125. const double xp2 = xp * xp;
  1126. const double yp2 = yp * yp;
  1127. double rx2 = rx * rx;
  1128. double ry2 = ry * ry;
  1129. const double s = (xp2 / rx2) + (yp2 / ry2);
  1130. double c;
  1131. if (s <= 1.0)
  1132. {
  1133. c = std::sqrt (jmax (0.0, ((rx2 * ry2) - (rx2 * yp2) - (ry2 * xp2))
  1134. / (( rx2 * yp2) + (ry2 * xp2))));
  1135. if (largeArc == sweep)
  1136. c = -c;
  1137. }
  1138. else
  1139. {
  1140. const double s2 = std::sqrt (s);
  1141. rx *= s2;
  1142. ry *= s2;
  1143. c = 0;
  1144. }
  1145. const double cpx = ((rx * yp) / ry) * c;
  1146. const double cpy = ((-ry * xp) / rx) * c;
  1147. centreX = ((x1 + x2) * 0.5) + (cosAngle * cpx) - (sinAngle * cpy);
  1148. centreY = ((y1 + y2) * 0.5) + (sinAngle * cpx) + (cosAngle * cpy);
  1149. const double ux = (xp - cpx) / rx;
  1150. const double uy = (yp - cpy) / ry;
  1151. const double vx = (-xp - cpx) / rx;
  1152. const double vy = (-yp - cpy) / ry;
  1153. const double length = juce_hypot (ux, uy);
  1154. startAngle = acos (jlimit (-1.0, 1.0, ux / length));
  1155. if (uy < 0)
  1156. startAngle = -startAngle;
  1157. startAngle += double_Pi * 0.5;
  1158. deltaAngle = acos (jlimit (-1.0, 1.0, ((ux * vx) + (uy * vy))
  1159. / (length * juce_hypot (vx, vy))));
  1160. if ((ux * vy) - (uy * vx) < 0)
  1161. deltaAngle = -deltaAngle;
  1162. if (sweep)
  1163. {
  1164. if (deltaAngle < 0)
  1165. deltaAngle += double_Pi * 2.0;
  1166. }
  1167. else
  1168. {
  1169. if (deltaAngle > 0)
  1170. deltaAngle -= double_Pi * 2.0;
  1171. }
  1172. deltaAngle = fmod (deltaAngle, double_Pi * 2.0);
  1173. }
  1174. SVGState& operator= (const SVGState&) JUCE_DELETED_FUNCTION;
  1175. };
  1176. //==============================================================================
  1177. Drawable* Drawable::createFromSVG (const XmlElement& svgDocument)
  1178. {
  1179. if (! svgDocument.hasTagNameIgnoringNamespace ("svg"))
  1180. return nullptr;
  1181. SVGState state (&svgDocument);
  1182. return state.parseSVGElement (SVGState::XmlPath (&svgDocument, nullptr));
  1183. }
  1184. Path Drawable::parseSVGPath (const String& svgPath)
  1185. {
  1186. SVGState state (nullptr);
  1187. Path p;
  1188. state.parsePathString (p, svgPath);
  1189. return p;
  1190. }