2
0

import-svg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 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 readDouble(double &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 double arcAngle(Vector2 u, Vector2 v) {
  78. return nonZeroSign(crossProduct(u, v))*acos(clamp(dotProduct(u, v)/(u.length()*v.length()), -1., +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, double rotation, bool largeArc, bool sweep) {
  84. if (endPoint == startPoint)
  85. return;
  86. if (radius.x == 0 || radius.y == 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(.5*(startPoint-endPoint), Vector2(axis.x, -axis.y));
  92. Vector2 rm2 = rm*rm;
  93. Vector2 radius2 = radius*radius;
  94. double radiusGap = rm2.x/radius2.x+rm2.y/radius2.y;
  95. if (radiusGap > 1) {
  96. radius *= sqrt(radiusGap);
  97. radius2 = radius*radius;
  98. }
  99. double dq = (radius2.x*rm2.y+radius2.y*rm2.x);
  100. double pq = radius2.x*radius2.y/dq-1;
  101. double q = (largeArc == sweep ? -1 : +1)*sqrt(max(pq, 0.));
  102. Vector2 rc(q*radius.x*rm.y/radius.y, -q*radius.y*rm.x/radius.x);
  103. Point2 center = .5*(startPoint+endPoint)+rotateVector(rc, axis);
  104. double angleStart = arcAngle(Vector2(1, 0), (rm-rc)/radius);
  105. double angleExtent = arcAngle((rm-rc)/radius, (-rm-rc)/radius);
  106. if (!sweep && angleExtent > 0)
  107. angleExtent -= 2*M_PI;
  108. else if (sweep && angleExtent < 0)
  109. angleExtent += 2*M_PI;
  110. int segments = (int) ceil(ARC_SEGMENTS_PER_PI/M_PI*fabs(angleExtent));
  111. double angleIncrement = angleExtent/segments;
  112. double cl = 4/3.*sin(.5*angleIncrement)/(1+cos(.5*angleIncrement));
  113. Point2 prevNode = startPoint;
  114. double 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, double 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(readDouble(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(readDouble(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. double angle;
  257. bool largeArg;
  258. bool sweep;
  259. REQUIRE(readCoord(radius, pathDef));
  260. REQUIRE(readDouble(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 *= M_PI/180.0;
  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. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  311. double left, top;
  312. const char *viewBox = root->Attribute("viewBox");
  313. if (viewBox)
  314. sscanf(viewBox, "%lf %lf %lf %lf", &left, &top, &dims.x, &dims.y);
  315. if (dimensions)
  316. *dimensions = dims;
  317. output.contours.clear();
  318. output.inverseYAxis = true;
  319. return buildShapeFromSvgPath(output, pd, ENDPOINT_SNAP_RANGE_PROPORTION*dims.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. viewBox.l = 0, viewBox.b = 0;
  339. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  340. const char *viewBoxStr = root->Attribute("viewBox");
  341. if (viewBoxStr)
  342. sscanf(viewBoxStr, "%lf %lf %lf %lf", &viewBox.l, &viewBox.b, &dims.x, &dims.y);
  343. viewBox.r = viewBox.l+dims.x;
  344. viewBox.t = viewBox.b+dims.y;
  345. output.contours.clear();
  346. output.inverseYAxis = true;
  347. if (!buildShapeFromSvgPath(output, pd, ENDPOINT_SNAP_RANGE_PROPORTION*dims.length()))
  348. return SVG_IMPORT_FAILURE;
  349. return flags;
  350. }
  351. #else
  352. void shapeFromSkiaPath(Shape &shape, const SkPath &skPath); // defined in resolve-shape-geometry.cpp
  353. static bool readTransformationOp(SkScalar dst[6], int &count, const char *&str, const char *name) {
  354. int nameLen = int(strlen(name));
  355. if (!memcmp(str, name, nameLen)) {
  356. const char *curStr = str+nameLen;
  357. skipExtraChars(curStr);
  358. if (*curStr == '(') {
  359. skipExtraChars(++curStr);
  360. count = 0;
  361. while (*curStr && *curStr != ')') {
  362. double x;
  363. if (!(count < 6 && readDouble(x, curStr)))
  364. return false;
  365. dst[count++] = SkScalar(x);
  366. skipExtraChars(curStr);
  367. }
  368. if (*curStr == ')') {
  369. str = curStr+1;
  370. return true;
  371. }
  372. }
  373. }
  374. return false;
  375. }
  376. static SkMatrix parseTransformation(int &flags, const char *str) {
  377. SkMatrix transformation;
  378. skipExtraChars(str);
  379. while (*str) {
  380. SkScalar values[6];
  381. int count;
  382. SkMatrix partial;
  383. if (readTransformationOp(values, count, str, "matrix") && count == 6) {
  384. partial.setAll(values[0], values[2], values[4], values[1], values[3], values[5], SkScalar(0), SkScalar(0), SkScalar(1));
  385. } else if (readTransformationOp(values, count, str, "translate") && (count == 1 || count == 2)) {
  386. if (count == 1)
  387. values[1] = SkScalar(0);
  388. partial.setTranslate(values[0], values[1]);
  389. } else if (readTransformationOp(values, count, str, "scale") && (count == 1 || count == 2)) {
  390. if (count == 1)
  391. values[1] = values[0];
  392. partial.setScale(values[0], values[1]);
  393. } else if (readTransformationOp(values, count, str, "rotate") && (count == 1 || count == 3)) {
  394. if (count == 3)
  395. partial.setRotate(values[0], values[1], values[2]);
  396. else
  397. partial.setRotate(values[0]);
  398. } else if (readTransformationOp(values, count, str, "skewX") && count == 1) {
  399. partial.setSkewX(SkScalar(tan(M_PI/180*values[0])));
  400. } else if (readTransformationOp(values, count, str, "skewY") && count == 1) {
  401. partial.setSkewY(SkScalar(tan(M_PI/180*values[0])));
  402. } else {
  403. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  404. break;
  405. }
  406. transformation = transformation*partial;
  407. skipExtraChars(str);
  408. }
  409. return transformation;
  410. }
  411. static SkMatrix combineTransformation(int &flags, const SkMatrix &parentTransformation, const char *transformationString, const char *transformationOriginString) {
  412. if (transformationString) {
  413. SkMatrix transformation = parseTransformation(flags, transformationString);
  414. if (transformationOriginString) {
  415. Point2 origin;
  416. if (readCoord(origin, transformationOriginString))
  417. transformation = SkMatrix::Translate(SkScalar(origin.x), SkScalar(origin.y))*transformation*SkMatrix::Translate(SkScalar(-origin.x), SkScalar(-origin.y));
  418. else
  419. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  420. }
  421. return parentTransformation*transformation;
  422. }
  423. return parentTransformation;
  424. }
  425. static void gatherPaths(SkPath &fullPath, int &flags, tinyxml2::XMLElement *parent, const SkMatrix &transformation) {
  426. for (tinyxml2::XMLElement *cur = parent->FirstChildElement(); cur && !FLAGS_FINAL(flags); cur = cur->NextSiblingElement()) {
  427. if (!strcmp(cur->Name(), "g"))
  428. gatherPaths(fullPath, flags, cur, combineTransformation(flags, transformation, cur->Attribute("transform"), cur->Attribute("transform-origin")));
  429. else if (!strcmp(cur->Name(), "mask") || !strcmp(cur->Name(), "use"))
  430. flags |= SVG_IMPORT_UNSUPPORTED_FEATURE_FLAG;
  431. else {
  432. SkPath curPath;
  433. if (!strcmp(cur->Name(), "path")) {
  434. const char *pd = cur->Attribute("d");
  435. if (!(pd && SkParsePath::FromSVGString(pd, &curPath))) {
  436. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  437. continue;
  438. }
  439. } else if (!strcmp(cur->Name(), "rect")) {
  440. SkScalar x = SkScalar(cur->DoubleAttribute("x")), y = SkScalar(cur->DoubleAttribute("y"));
  441. SkScalar width = SkScalar(cur->DoubleAttribute("width")), height = SkScalar(cur->DoubleAttribute("height"));
  442. SkScalar rx = SkScalar(cur->DoubleAttribute("rx")), ry = SkScalar(cur->DoubleAttribute("ry"));
  443. if (!(width && height))
  444. continue;
  445. SkRect rect = SkRect::MakeLTRB(x, y, x+width, y+height);
  446. if (rx || ry) {
  447. SkScalar radii[] = { rx, ry, rx, ry, rx, ry, rx, ry };
  448. curPath.addRoundRect(rect, radii);
  449. } else
  450. curPath.addRect(rect);
  451. } else if (!strcmp(cur->Name(), "circle")) {
  452. SkScalar cx = SkScalar(cur->DoubleAttribute("cx")), cy = SkScalar(cur->DoubleAttribute("cy"));
  453. SkScalar r = SkScalar(cur->DoubleAttribute("r"));
  454. if (!r)
  455. continue;
  456. curPath.addCircle(cx, cy, r);
  457. } else if (!strcmp(cur->Name(), "ellipse")) {
  458. SkScalar cx = SkScalar(cur->DoubleAttribute("cx")), cy = SkScalar(cur->DoubleAttribute("cy"));
  459. SkScalar rx = SkScalar(cur->DoubleAttribute("rx")), ry = SkScalar(cur->DoubleAttribute("ry"));
  460. if (!(rx && ry))
  461. continue;
  462. curPath.addOval(SkRect::MakeLTRB(cx-rx, cy-ry, cx+rx, cy+ry));
  463. } else if (!strcmp(cur->Name(), "polygon")) {
  464. const char *pd = cur->Attribute("points");
  465. if (!pd) {
  466. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  467. continue;
  468. }
  469. Point2 point;
  470. if (!readCoord(point, pd))
  471. continue;
  472. curPath.moveTo(SkScalar(point.x), SkScalar(point.y));
  473. if (!readCoord(point, pd))
  474. continue;
  475. do {
  476. curPath.lineTo(SkScalar(point.x), SkScalar(point.y));
  477. } while (readCoord(point, pd));
  478. curPath.close();
  479. } else
  480. continue;
  481. curPath.transform(combineTransformation(flags, transformation, cur->Attribute("transform"), cur->Attribute("transform-origin")));
  482. if (Op(fullPath, curPath, kUnion_SkPathOp, &fullPath))
  483. flags |= SVG_IMPORT_SUCCESS_FLAG;
  484. else
  485. flags |= SVG_IMPORT_PARTIAL_FAILURE_FLAG;
  486. }
  487. }
  488. }
  489. int loadSvgShape(Shape &output, Shape::Bounds &viewBox, const char *filename) {
  490. tinyxml2::XMLDocument doc;
  491. if (doc.LoadFile(filename))
  492. return SVG_IMPORT_FAILURE;
  493. tinyxml2::XMLElement *root = doc.FirstChildElement("svg");
  494. if (!root)
  495. return SVG_IMPORT_FAILURE;
  496. SkPath fullPath;
  497. int flags = 0;
  498. gatherPaths(fullPath, flags, root, SkMatrix());
  499. if (!((flags&SVG_IMPORT_SUCCESS_FLAG) && Simplify(fullPath, &fullPath)))
  500. return SVG_IMPORT_FAILURE;
  501. shapeFromSkiaPath(output, fullPath);
  502. output.inverseYAxis = true;
  503. output.orientContours();
  504. viewBox.l = 0, viewBox.b = 0;
  505. Vector2 dims(root->DoubleAttribute("width"), root->DoubleAttribute("height"));
  506. const char *viewBoxStr = root->Attribute("viewBox");
  507. if (viewBoxStr)
  508. sscanf(viewBoxStr, "%lf %lf %lf %lf", &viewBox.l, &viewBox.b, &dims.x, &dims.y);
  509. viewBox.r = viewBox.l+dims.x;
  510. viewBox.t = viewBox.b+dims.y;
  511. return flags;
  512. }
  513. #endif
  514. }
  515. #endif