import-svg.cpp 23 KB

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