import-svg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. #define _USE_MATH_DEFINES
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "import-svg.h"
  4. #ifndef MSDFGEN_DISABLE_SVG
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <tinyxml2.h>
  9. #ifdef MSDFGEN_USE_SKIA
  10. #include <skia/core/SkPath.h>
  11. #include <skia/utils/SkParsePath.h>
  12. #include <skia/pathops/SkPathOps.h>
  13. #endif
  14. #include "../core/arithmetics.hpp"
  15. #define ARC_SEGMENTS_PER_PI 2
  16. #define ENDPOINT_SNAP_RANGE_PROPORTION (1/16384.)
  17. namespace msdfgen {
  18. #if defined(_DEBUG) || !NDEBUG
  19. #define REQUIRE(cond) { if (!(cond)) { fprintf(stderr, "SVG Parse Error (%s:%d): " #cond "\n", __FILE__, __LINE__); return false; } }
  20. #else
  21. #define REQUIRE(cond) { if (!(cond)) return false; }
  22. #endif
  23. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_FAILURE = 0x00;
  24. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_SUCCESS_FLAG = 0x01;
  25. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_PARTIAL_FAILURE_FLAG = 0x02;
  26. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_INCOMPLETE_FLAG = 0x04;
  27. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG = 0x08;
  28. MSDFGEN_EXT_PUBLIC const int SVG_IMPORT_TRANSFORMATION_IGNORED_FLAG = 0x10;
  29. static void skipExtraChars(const char *&pathDef) {
  30. while (*pathDef == ',' || *pathDef == ' ' || *pathDef == '\t' || *pathDef == '\r' || *pathDef == '\n')
  31. ++pathDef;
  32. }
  33. static bool readNodeType(char &output, const char *&pathDef) {
  34. skipExtraChars(pathDef);
  35. char nodeType = *pathDef;
  36. if (nodeType && nodeType != '+' && nodeType != '-' && nodeType != '.' && nodeType != ',' && (nodeType < '0' || nodeType > '9')) {
  37. ++pathDef;
  38. output = nodeType;
  39. return true;
  40. }
  41. return false;
  42. }
  43. static bool readDouble(double &output, const char *&pathDef) {
  44. skipExtraChars(pathDef);
  45. char *end = NULL;
  46. output = strtod(pathDef, &end);
  47. if (end > pathDef) {
  48. pathDef = end;
  49. return true;
  50. }
  51. return false;
  52. }
  53. static bool readCoord(Point2 &output, const char *&pathDef) {
  54. return readDouble(output.x, pathDef) && readDouble(output.y, pathDef);
  55. }
  56. static bool readBool(bool &output, const char *&pathDef) {
  57. skipExtraChars(pathDef);
  58. char *end = NULL;
  59. long v = strtol(pathDef, &end, 10);
  60. if (end > pathDef) {
  61. pathDef = end;
  62. output = v != 0;
  63. return true;
  64. }
  65. return false;
  66. }
  67. static double arcAngle(Vector2 u, Vector2 v) {
  68. return nonZeroSign(crossProduct(u, v))*acos(clamp(dotProduct(u, v)/(u.length()*v.length()), -1., +1.));
  69. }
  70. static Vector2 rotateVector(Vector2 v, Vector2 direction) {
  71. return Vector2(direction.x*v.x-direction.y*v.y, direction.y*v.x+direction.x*v.y);
  72. }
  73. static void addArcApproximate(Contour &contour, Point2 startPoint, Point2 endPoint, Vector2 radius, double rotation, bool largeArc, bool sweep) {
  74. if (endPoint == startPoint)
  75. return;
  76. if (radius.x == 0 || radius.y == 0)
  77. return contour.addEdge(EdgeHolder(startPoint, endPoint));
  78. radius.x = fabs(radius.x);
  79. radius.y = fabs(radius.y);
  80. Vector2 axis(cos(rotation), sin(rotation));
  81. Vector2 rm = rotateVector(.5*(startPoint-endPoint), Vector2(axis.x, -axis.y));
  82. Vector2 rm2 = rm*rm;
  83. Vector2 radius2 = radius*radius;
  84. double radiusGap = rm2.x/radius2.x+rm2.y/radius2.y;
  85. if (radiusGap > 1) {
  86. radius *= sqrt(radiusGap);
  87. radius2 = radius*radius;
  88. }
  89. double dq = (radius2.x*rm2.y+radius2.y*rm2.x);
  90. double pq = radius2.x*radius2.y/dq-1;
  91. double q = (largeArc == sweep ? -1 : +1)*sqrt(max(pq, 0.));
  92. Vector2 rc(q*radius.x*rm.y/radius.y, -q*radius.y*rm.x/radius.x);
  93. Point2 center = .5*(startPoint+endPoint)+rotateVector(rc, axis);
  94. double angleStart = arcAngle(Vector2(1, 0), (rm-rc)/radius);
  95. double angleExtent = arcAngle((rm-rc)/radius, (-rm-rc)/radius);
  96. if (!sweep && angleExtent > 0)
  97. angleExtent -= 2*M_PI;
  98. else if (sweep && angleExtent < 0)
  99. angleExtent += 2*M_PI;
  100. int segments = (int) ceil(ARC_SEGMENTS_PER_PI/M_PI*fabs(angleExtent));
  101. double angleIncrement = angleExtent/segments;
  102. double cl = 4/3.*sin(.5*angleIncrement)/(1+cos(.5*angleIncrement));
  103. Point2 prevNode = startPoint;
  104. double angle = angleStart;
  105. for (int i = 0; i < segments; ++i) {
  106. Point2 controlPoint[2];
  107. Vector2 d(cos(angle), sin(angle));
  108. controlPoint[0] = center+rotateVector(Vector2(d.x-cl*d.y, d.y+cl*d.x)*radius, axis);
  109. angle += angleIncrement;
  110. d.set(cos(angle), sin(angle));
  111. controlPoint[1] = center+rotateVector(Vector2(d.x+cl*d.y, d.y-cl*d.x)*radius, axis);
  112. Point2 node = i == segments-1 ? endPoint : center+rotateVector(d*radius, axis);
  113. contour.addEdge(EdgeHolder(prevNode, controlPoint[0], controlPoint[1], node));
  114. prevNode = node;
  115. }
  116. }
  117. #define FLAGS_FINAL(flags) (((flags)&(SVG_IMPORT_SUCCESS_FLAG|SVG_IMPORT_INCOMPLETE_FLAG|SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG)) == (SVG_IMPORT_SUCCESS_FLAG|SVG_IMPORT_INCOMPLETE_FLAG|SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG))
  118. static void findPathByForwardIndex(tinyxml2::XMLElement *&path, int &flags, int &skips, tinyxml2::XMLElement *parent, bool hasTransformation) {
  119. for (tinyxml2::XMLElement *cur = parent->FirstChildElement(); cur && !FLAGS_FINAL(flags); cur = cur->NextSiblingElement()) {
  120. if (!strcmp(cur->Name(), "path")) {
  121. if (!skips--) {
  122. path = cur;
  123. flags |= SVG_IMPORT_SUCCESS_FLAG;
  124. if (hasTransformation || cur->Attribute("transform"))
  125. flags |= SVG_IMPORT_TRANSFORMATION_IGNORED_FLAG;
  126. } else if (flags&SVG_IMPORT_SUCCESS_FLAG)
  127. flags |= SVG_IMPORT_INCOMPLETE_FLAG;
  128. } else if (!strcmp(cur->Name(), "g"))
  129. findPathByForwardIndex(path, flags, skips, cur, hasTransformation || cur->Attribute("transform"));
  130. else if (!strcmp(cur->Name(), "rect") || !strcmp(cur->Name(), "circle") || !strcmp(cur->Name(), "ellipse") || !strcmp(cur->Name(), "polygon"))
  131. flags |= SVG_IMPORT_INCOMPLETE_FLAG;
  132. else if (!strcmp(cur->Name(), "mask") || !strcmp(cur->Name(), "use"))
  133. flags |= SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG;
  134. }
  135. }
  136. static void findPathByBackwardIndex(tinyxml2::XMLElement *&path, int &flags, int &skips, tinyxml2::XMLElement *parent, bool hasTransformation) {
  137. for (tinyxml2::XMLElement *cur = parent->LastChildElement(); cur && !FLAGS_FINAL(flags); cur = cur->PreviousSiblingElement()) {
  138. if (!strcmp(cur->Name(), "path")) {
  139. if (!skips--) {
  140. path = cur;
  141. flags |= SVG_IMPORT_SUCCESS_FLAG;
  142. if (hasTransformation || cur->Attribute("transform"))
  143. flags |= SVG_IMPORT_TRANSFORMATION_IGNORED_FLAG;
  144. } else if (flags&SVG_IMPORT_SUCCESS_FLAG)
  145. flags |= SVG_IMPORT_INCOMPLETE_FLAG;
  146. } else if (!strcmp(cur->Name(), "g"))
  147. findPathByBackwardIndex(path, flags, skips, cur, hasTransformation || cur->Attribute("transform"));
  148. else if (!strcmp(cur->Name(), "rect") || !strcmp(cur->Name(), "circle") || !strcmp(cur->Name(), "ellipse") || !strcmp(cur->Name(), "polygon"))
  149. flags |= SVG_IMPORT_INCOMPLETE_FLAG;
  150. else if (!strcmp(cur->Name(), "mask") || !strcmp(cur->Name(), "use"))
  151. flags |= SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG;
  152. }
  153. }
  154. bool buildShapeFromSvgPath(Shape &shape, const char *pathDef, double endpointSnapRange) {
  155. char nodeType = '\0';
  156. char prevNodeType = '\0';
  157. Point2 prevNode(0, 0);
  158. bool nodeTypePreread = false;
  159. while (nodeTypePreread || readNodeType(nodeType, pathDef)) {
  160. nodeTypePreread = false;
  161. Contour &contour = shape.addContour();
  162. bool contourStart = true;
  163. Point2 startPoint;
  164. Point2 controlPoint[2];
  165. Point2 node;
  166. while (*pathDef) {
  167. switch (nodeType) {
  168. case 'M': case 'm':
  169. if (!contourStart) {
  170. nodeTypePreread = true;
  171. goto NEXT_CONTOUR;
  172. }
  173. REQUIRE(readCoord(node, pathDef));
  174. if (nodeType == 'm')
  175. node += prevNode;
  176. startPoint = node;
  177. --nodeType; // to 'L' or 'l'
  178. break;
  179. case 'Z': case 'z':
  180. REQUIRE(!contourStart);
  181. goto NEXT_CONTOUR;
  182. case 'L': case 'l':
  183. REQUIRE(readCoord(node, pathDef));
  184. if (nodeType == 'l')
  185. node += prevNode;
  186. contour.addEdge(EdgeHolder(prevNode, node));
  187. break;
  188. case 'H': case 'h':
  189. REQUIRE(readDouble(node.x, pathDef));
  190. if (nodeType == 'h')
  191. node.x += prevNode.x;
  192. contour.addEdge(EdgeHolder(prevNode, node));
  193. break;
  194. case 'V': case 'v':
  195. REQUIRE(readDouble(node.y, pathDef));
  196. if (nodeType == 'v')
  197. node.y += prevNode.y;
  198. contour.addEdge(EdgeHolder(prevNode, node));
  199. break;
  200. case 'Q': case 'q':
  201. REQUIRE(readCoord(controlPoint[0], pathDef));
  202. REQUIRE(readCoord(node, pathDef));
  203. if (nodeType == 'q') {
  204. controlPoint[0] += prevNode;
  205. node += prevNode;
  206. }
  207. contour.addEdge(EdgeHolder(prevNode, controlPoint[0], node));
  208. break;
  209. case 'T': case 't':
  210. if (prevNodeType == 'Q' || prevNodeType == 'q' || prevNodeType == 'T' || prevNodeType == 't')
  211. controlPoint[0] = node+node-controlPoint[0];
  212. else
  213. controlPoint[0] = node;
  214. REQUIRE(readCoord(node, pathDef));
  215. if (nodeType == 't')
  216. node += prevNode;
  217. contour.addEdge(EdgeHolder(prevNode, controlPoint[0], node));
  218. break;
  219. case 'C': case 'c':
  220. REQUIRE(readCoord(controlPoint[0], pathDef));
  221. REQUIRE(readCoord(controlPoint[1], pathDef));
  222. REQUIRE(readCoord(node, pathDef));
  223. if (nodeType == 'c') {
  224. controlPoint[0] += prevNode;
  225. controlPoint[1] += prevNode;
  226. node += prevNode;
  227. }
  228. contour.addEdge(EdgeHolder(prevNode, controlPoint[0], controlPoint[1], node));
  229. break;
  230. case 'S': case 's':
  231. if (prevNodeType == 'C' || prevNodeType == 'c' || prevNodeType == 'S' || prevNodeType == 's')
  232. controlPoint[0] = node+node-controlPoint[1];
  233. else
  234. controlPoint[0] = node;
  235. REQUIRE(readCoord(controlPoint[1], pathDef));
  236. REQUIRE(readCoord(node, pathDef));
  237. if (nodeType == 's') {
  238. controlPoint[1] += prevNode;
  239. node += prevNode;
  240. }
  241. contour.addEdge(EdgeHolder(prevNode, controlPoint[0], controlPoint[1], node));
  242. break;
  243. case 'A': case 'a':
  244. {
  245. Vector2 radius;
  246. double angle;
  247. bool largeArg;
  248. bool sweep;
  249. REQUIRE(readCoord(radius, pathDef));
  250. REQUIRE(readDouble(angle, pathDef));
  251. REQUIRE(readBool(largeArg, pathDef));
  252. REQUIRE(readBool(sweep, pathDef));
  253. REQUIRE(readCoord(node, pathDef));
  254. if (nodeType == 'a')
  255. node += prevNode;
  256. angle *= M_PI/180.0;
  257. addArcApproximate(contour, prevNode, node, radius, angle, largeArg, sweep);
  258. }
  259. break;
  260. default:
  261. REQUIRE(!"Unknown node type");
  262. }
  263. contourStart &= nodeType == 'M' || nodeType == 'm';
  264. prevNode = node;
  265. prevNodeType = nodeType;
  266. readNodeType(nodeType, pathDef);
  267. }
  268. NEXT_CONTOUR:
  269. // Fix contour if it isn't properly closed
  270. if (!contour.edges.empty() && prevNode != startPoint) {
  271. if ((contour.edges.back()->point(1)-contour.edges[0]->point(0)).length() < endpointSnapRange)
  272. contour.edges.back()->moveEndPoint(contour.edges[0]->point(0));
  273. else
  274. contour.addEdge(EdgeHolder(prevNode, startPoint));
  275. }
  276. prevNode = startPoint;
  277. prevNodeType = '\0';
  278. }
  279. return true;
  280. }
  281. bool loadSvgShape(Shape &output, const char *filename, int pathIndex, Vector2 *dimensions) {
  282. tinyxml2::XMLDocument doc;
  283. if (doc.LoadFile(filename))
  284. return false;
  285. tinyxml2::XMLElement *root = doc.FirstChildElement("svg");
  286. if (!root)
  287. return false;
  288. tinyxml2::XMLElement *path = NULL;
  289. int flags = 0;
  290. int skippedPaths = abs(pathIndex)-(pathIndex != 0);
  291. if (pathIndex > 0)
  292. findPathByForwardIndex(path, flags, skippedPaths, root, false);
  293. else
  294. findPathByBackwardIndex(path, flags, skippedPaths, root, false);
  295. if (!path)
  296. return false;
  297. const char *pd = path->Attribute("d");
  298. if (!pd)
  299. return false;
  300. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  301. if (const char *viewBox = root->Attribute("viewBox")) {
  302. double left = 0, top = 0;
  303. readDouble(left, viewBox) && readDouble(top, viewBox) && readDouble(dims.x, viewBox) && readDouble(dims.y, viewBox);
  304. }
  305. if (dimensions)
  306. *dimensions = dims;
  307. output.contours.clear();
  308. output.inverseYAxis = true;
  309. return buildShapeFromSvgPath(output, pd, ENDPOINT_SNAP_RANGE_PROPORTION*dims.length());
  310. }
  311. #ifndef MSDFGEN_USE_SKIA
  312. int loadSvgShape(Shape &output, Shape::Bounds &viewBox, const char *filename) {
  313. tinyxml2::XMLDocument doc;
  314. if (doc.LoadFile(filename))
  315. return SVG_IMPORT_FAILURE;
  316. tinyxml2::XMLElement *root = doc.FirstChildElement("svg");
  317. if (!root)
  318. return SVG_IMPORT_FAILURE;
  319. tinyxml2::XMLElement *path = NULL;
  320. int flags = 0;
  321. int skippedPaths = 0;
  322. findPathByBackwardIndex(path, flags, skippedPaths, root, false);
  323. if (!(path && (flags&SVG_IMPORT_SUCCESS_FLAG)))
  324. return SVG_IMPORT_FAILURE;
  325. const char *pd = path->Attribute("d");
  326. if (!pd)
  327. return SVG_IMPORT_FAILURE;
  328. viewBox.l = 0, viewBox.b = 0;
  329. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  330. if (const char *viewBoxStr = root->Attribute("viewBox"))
  331. readDouble(viewBox.l, viewBoxStr) && readDouble(viewBox.b, viewBoxStr) && readDouble(dims.x, viewBoxStr) && readDouble(dims.y, viewBoxStr);
  332. viewBox.r = viewBox.l+dims.x;
  333. viewBox.t = viewBox.b+dims.y;
  334. output.contours.clear();
  335. output.inverseYAxis = true;
  336. if (!buildShapeFromSvgPath(output, pd, ENDPOINT_SNAP_RANGE_PROPORTION*dims.length()))
  337. return SVG_IMPORT_FAILURE;
  338. return flags;
  339. }
  340. #else
  341. void shapeFromSkiaPath(Shape &shape, const SkPath &skPath); // defined in resolve-shape-geometry.cpp
  342. static bool readTransformationOp(SkScalar dst[6], int &count, const char *&str, const char *name) {
  343. int nameLen = int(strlen(name));
  344. if (!memcmp(str, name, nameLen)) {
  345. const char *curStr = str+nameLen;
  346. skipExtraChars(curStr);
  347. if (*curStr == '(') {
  348. skipExtraChars(++curStr);
  349. count = 0;
  350. while (*curStr && *curStr != ')') {
  351. double x;
  352. if (!(count < 6 && readDouble(x, curStr)))
  353. return false;
  354. dst[count++] = SkScalar(x);
  355. skipExtraChars(curStr);
  356. }
  357. if (*curStr == ')') {
  358. str = curStr+1;
  359. return true;
  360. }
  361. }
  362. }
  363. return false;
  364. }
  365. static SkMatrix parseTransformation(int &flags, const char *str) {
  366. SkMatrix transformation;
  367. skipExtraChars(str);
  368. while (*str) {
  369. SkScalar values[6];
  370. int count;
  371. SkMatrix partial;
  372. if (readTransformationOp(values, count, str, "matrix") && count == 6) {
  373. partial.setAll(values[0], values[2], values[4], values[1], values[3], values[5], SkScalar(0), SkScalar(0), SkScalar(1));
  374. } else if (readTransformationOp(values, count, str, "translate") && (count == 1 || count == 2)) {
  375. if (count == 1)
  376. values[1] = SkScalar(0);
  377. partial.setTranslate(values[0], values[1]);
  378. } else if (readTransformationOp(values, count, str, "scale") && (count == 1 || count == 2)) {
  379. if (count == 1)
  380. values[1] = values[0];
  381. partial.setScale(values[0], values[1]);
  382. } else if (readTransformationOp(values, count, str, "rotate") && (count == 1 || count == 3)) {
  383. if (count == 3)
  384. partial.setRotate(values[0], values[1], values[2]);
  385. else
  386. partial.setRotate(values[0]);
  387. } else if (readTransformationOp(values, count, str, "skewX") && count == 1) {
  388. partial.setSkewX(SkScalar(tan(M_PI/180*values[0])));
  389. } else if (readTransformationOp(values, count, str, "skewY") && count == 1) {
  390. partial.setSkewY(SkScalar(tan(M_PI/180*values[0])));
  391. } else {
  392. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  393. break;
  394. }
  395. transformation = transformation*partial;
  396. skipExtraChars(str);
  397. }
  398. return transformation;
  399. }
  400. static SkMatrix combineTransformation(int &flags, const SkMatrix &parentTransformation, const char *transformationString, const char *transformationOriginString) {
  401. if (transformationString) {
  402. SkMatrix transformation = parseTransformation(flags, transformationString);
  403. if (transformationOriginString) {
  404. Point2 origin;
  405. if (readCoord(origin, transformationOriginString))
  406. transformation = SkMatrix::Translate(SkScalar(origin.x), SkScalar(origin.y))*transformation*SkMatrix::Translate(SkScalar(-origin.x), SkScalar(-origin.y));
  407. else
  408. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  409. }
  410. return parentTransformation*transformation;
  411. }
  412. return parentTransformation;
  413. }
  414. static void gatherPaths(SkPath &fullPath, int &flags, tinyxml2::XMLElement *parent, const SkMatrix &transformation) {
  415. for (tinyxml2::XMLElement *cur = parent->FirstChildElement(); cur && !FLAGS_FINAL(flags); cur = cur->NextSiblingElement()) {
  416. if (!strcmp(cur->Name(), "g"))
  417. gatherPaths(fullPath, flags, cur, combineTransformation(flags, transformation, cur->Attribute("transform"), cur->Attribute("transform-origin")));
  418. else if (!strcmp(cur->Name(), "mask") || !strcmp(cur->Name(), "use"))
  419. flags |= SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG;
  420. else {
  421. SkPath curPath;
  422. if (!strcmp(cur->Name(), "path")) {
  423. const char *pd = cur->Attribute("d");
  424. if (!(pd && SkParsePath::FromSVGString(pd, &curPath))) {
  425. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  426. continue;
  427. }
  428. } else if (!strcmp(cur->Name(), "rect")) {
  429. SkScalar x = SkScalar(cur->DoubleAttribute("x")), y = SkScalar(cur->DoubleAttribute("y"));
  430. SkScalar width = SkScalar(cur->DoubleAttribute("width")), height = SkScalar(cur->DoubleAttribute("height"));
  431. SkScalar rx = SkScalar(cur->DoubleAttribute("rx")), ry = SkScalar(cur->DoubleAttribute("ry"));
  432. if (!(width && height))
  433. continue;
  434. SkRect rect = SkRect::MakeLTRB(x, y, x+width, y+height);
  435. if (rx || ry) {
  436. SkScalar radii[] = { rx, ry, rx, ry, rx, ry, rx, ry };
  437. curPath.addRoundRect(rect, radii);
  438. } else
  439. curPath.addRect(rect);
  440. } else if (!strcmp(cur->Name(), "circle")) {
  441. SkScalar cx = SkScalar(cur->DoubleAttribute("cx")), cy = SkScalar(cur->DoubleAttribute("cy"));
  442. SkScalar r = SkScalar(cur->DoubleAttribute("r"));
  443. if (!r)
  444. continue;
  445. curPath.addCircle(cx, cy, r);
  446. } else if (!strcmp(cur->Name(), "ellipse")) {
  447. SkScalar cx = SkScalar(cur->DoubleAttribute("cx")), cy = SkScalar(cur->DoubleAttribute("cy"));
  448. SkScalar rx = SkScalar(cur->DoubleAttribute("rx")), ry = SkScalar(cur->DoubleAttribute("ry"));
  449. if (!(rx && ry))
  450. continue;
  451. curPath.addOval(SkRect::MakeLTRB(cx-rx, cy-ry, cx+rx, cy+ry));
  452. } else if (!strcmp(cur->Name(), "polygon")) {
  453. const char *pd = cur->Attribute("points");
  454. if (!pd) {
  455. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  456. continue;
  457. }
  458. Point2 point;
  459. if (!readCoord(point, pd))
  460. continue;
  461. curPath.moveTo(SkScalar(point.x), SkScalar(point.y));
  462. if (!readCoord(point, pd))
  463. continue;
  464. do {
  465. curPath.lineTo(SkScalar(point.x), SkScalar(point.y));
  466. } while (readCoord(point, pd));
  467. curPath.close();
  468. } else
  469. continue;
  470. const char *fillRule = cur->Attribute("fill-rule");
  471. if (fillRule && !strcmp(fillRule, "evenodd"))
  472. curPath.setFillType(SkPathFillType::kEvenOdd);
  473. curPath.transform(combineTransformation(flags, transformation, cur->Attribute("transform"), cur->Attribute("transform-origin")));
  474. if (Op(fullPath, curPath, kUnion_SkPathOp, &fullPath))
  475. flags |= SVG_IMPORT_SUCCESS_FLAG;
  476. else
  477. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  478. }
  479. }
  480. }
  481. int loadSvgShape(Shape &output, Shape::Bounds &viewBox, const char *filename) {
  482. tinyxml2::XMLDocument doc;
  483. if (doc.LoadFile(filename))
  484. return SVG_IMPORT_FAILURE;
  485. tinyxml2::XMLElement *root = doc.FirstChildElement("svg");
  486. if (!root)
  487. return SVG_IMPORT_FAILURE;
  488. SkPath fullPath;
  489. int flags = 0;
  490. gatherPaths(fullPath, flags, root, SkMatrix());
  491. if (!((flags&SVG_IMPORT_SUCCESS_FLAG) && Simplify(fullPath, &fullPath)))
  492. return SVG_IMPORT_FAILURE;
  493. shapeFromSkiaPath(output, fullPath);
  494. output.inverseYAxis = true;
  495. output.orientContours();
  496. viewBox.l = 0, viewBox.b = 0;
  497. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  498. if (const char *viewBoxStr = root->Attribute("viewBox"))
  499. readDouble(viewBox.l, viewBoxStr) && readDouble(viewBox.b, viewBoxStr) && readDouble(dims.x, viewBoxStr) && readDouble(dims.y, viewBoxStr);
  500. viewBox.r = viewBox.l+dims.x;
  501. viewBox.t = viewBox.b+dims.y;
  502. return flags;
  503. }
  504. #endif
  505. }
  506. #endif