ParseSVG.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 12/18/2015 #
  6. # MIT Licence #
  7. # #
  8. # SVG Features supported: #
  9. # * Groups #
  10. # * Rectangles (w/ rounded corners) #
  11. # * Circles #
  12. # * Ellipses #
  13. # * Polygons #
  14. # * Polylines #
  15. # * Lines #
  16. # * Paths #
  17. # * All transformations #
  18. # #
  19. # Reference: www.w3.org/TR/SVG/Overview.html #
  20. # ##########################################################
  21. # import xml.etree.ElementTree as ET
  22. from svg.path import Line, Arc, CubicBezier, QuadraticBezier, parse_path
  23. # from svg.path.path import Move
  24. # from svg.path.path import Close
  25. import svg.path
  26. from shapely.geometry import LineString, MultiLineString
  27. from shapely.affinity import skew, affine_transform, rotate
  28. import numpy as np
  29. from appParsers.ParseFont import *
  30. log = logging.getLogger('base2')
  31. def svgparselength(lengthstr):
  32. """
  33. Parse an SVG length string into a float and a units
  34. string, if any.
  35. :param lengthstr: SVG length string.
  36. :return: Number and units pair.
  37. :rtype: tuple(float, str|None)
  38. """
  39. integer_re_str = r'[+-]?[0-9]+'
  40. number_re_str = r'(?:[+-]?[0-9]*\.[0-9]+(?:[Ee]' + integer_re_str + ')?' + r')|' + \
  41. r'(?:' + integer_re_str + r'(?:[Ee]' + integer_re_str + r')?)'
  42. length_re_str = r'(' + number_re_str + r')(em|ex|px|in|cm|mm|pt|pc|%)?'
  43. if lengthstr:
  44. match = re.search(length_re_str, lengthstr)
  45. if match:
  46. return float(match.group(1)), match.group(2)
  47. else:
  48. return 0, 0
  49. return
  50. def svgparse_viewbox(root):
  51. val = root.get('viewBox')
  52. if val is None:
  53. return 1.0
  54. res = [float(x) for x in val.split()] or [float(x) for x in val.split(',')]
  55. w = svgparselength(root.get('width'))[0]
  56. # h = svgparselength(root.get('height'))[0]
  57. v_w = res[2]
  58. # v_h = res[3]
  59. return w / v_w
  60. def path2shapely(path, object_type, res=1.0, units='MM', factor=1.0):
  61. """
  62. Converts an svg.path.Path into a Shapely
  63. Polygon or LinearString.
  64. :param path: svg.path.Path instance
  65. :param object_type:
  66. :param res: Resolution (minimum step along path)
  67. :param units: FlatCAM units
  68. :type units: str
  69. :param factor: correction factor due of virtual units
  70. :type factor: float
  71. :return: Shapely geometry object
  72. :rtype : Polygon
  73. :rtype : LineString
  74. """
  75. points = []
  76. geometry = []
  77. rings = []
  78. closed = False
  79. for component in path:
  80. # Line
  81. if isinstance(component, Line):
  82. start = component.start
  83. x, y = start.real, start.imag
  84. if len(points) == 0 or points[-1] != (x, y):
  85. points.append((x, y))
  86. end = component.end
  87. points.append((factor * end.real, factor * end.imag))
  88. continue
  89. # Arc, CubicBezier or QuadraticBezier
  90. if isinstance(component, Arc) or \
  91. isinstance(component, CubicBezier) or \
  92. isinstance(component, QuadraticBezier):
  93. # How many points to use in the discrete representation.
  94. length = component.length(res / 10.0)
  95. # steps = int(length / res + 0.5)
  96. steps = int(length) * 2
  97. if units == 'IN':
  98. steps *= 25
  99. # solve error when step is below 1,
  100. # it may cause other problems, but LineString needs at least two points
  101. # later edit: made the minimum nr of steps to be 10; left it like that to see that steps can be 0
  102. if steps == 0 or steps < 10:
  103. steps = 10
  104. frac = 1.0 / steps
  105. # print length, steps, frac
  106. for i in range(steps):
  107. point = component.point(i * frac)
  108. x, y = point.real, point.imag
  109. if len(points) == 0 or points[-1] != (x, y):
  110. points.append((factor * x, factor * y))
  111. end = component.point(1.0)
  112. points.append((factor * end.real, factor * end.imag))
  113. continue
  114. # Move
  115. if isinstance(component, svg.path.Move):
  116. if not points:
  117. continue
  118. else:
  119. rings.append(points)
  120. if closed is False:
  121. points = []
  122. else:
  123. closed = False
  124. start = component.start
  125. x, y = start.real, start.imag
  126. points = [(factor * x, factor * y)]
  127. continue
  128. closed = False
  129. # Close
  130. if isinstance(component, svg.path.Close):
  131. if not points:
  132. continue
  133. else:
  134. rings.append(points)
  135. points = []
  136. closed = True
  137. continue
  138. log.warning("I don't know what this is: %s" % str(component))
  139. continue
  140. # if there are still points in points then add them to the last ring
  141. if points:
  142. rings.append(points)
  143. try:
  144. rings = MultiLineString(rings)
  145. except Exception as e:
  146. log.debug("ParseSVG.path2shapely() MString --> %s" % str(e))
  147. return None
  148. if len(rings) > 0:
  149. if len(rings) == 1 and not isinstance(rings, MultiLineString):
  150. # Polygons are closed and require more than 2 points
  151. if Point(rings[0][0]).almost_equals(Point(rings[0][-1])) and len(rings[0]) > 2:
  152. geo_element = Polygon(rings[0])
  153. else:
  154. geo_element = LineString(rings[0])
  155. else:
  156. try:
  157. geo_element = Polygon(rings[0], rings[1:])
  158. except Exception:
  159. coords = []
  160. for line in rings:
  161. coords.append(line.coords[0])
  162. coords.append(line.coords[1])
  163. try:
  164. geo_element = Polygon(coords)
  165. except Exception:
  166. geo_element = LineString(coords)
  167. geometry.append(geo_element)
  168. return geometry
  169. def svgrect2shapely(rect, n_points=32, factor=1.0):
  170. """
  171. Converts an SVG rect into Shapely geometry.
  172. :param rect: Rect Element
  173. :type rect: xml.etree.ElementTree.Element
  174. :param n_points: number of points to approximate rectangles corners when having rounded corners
  175. :type n_points: int
  176. :param factor: correction factor due of virtual units
  177. :type factor: float
  178. :return: shapely.geometry.polygon.LinearRing
  179. """
  180. w = svgparselength(rect.get('width'))[0]
  181. h = svgparselength(rect.get('height'))[0]
  182. x_obj = rect.get('x')
  183. if x_obj is not None:
  184. x = svgparselength(x_obj)[0] * factor
  185. else:
  186. x = 0
  187. y_obj = rect.get('y')
  188. if y_obj is not None:
  189. y = svgparselength(y_obj)[0] * factor
  190. else:
  191. y = 0
  192. rxstr = rect.get('rx')
  193. rxstr = rxstr * factor if rxstr else rxstr
  194. rystr = rect.get('ry')
  195. rystr = rystr * factor if rystr else rystr
  196. if rxstr is None and rystr is None: # Sharp corners
  197. pts = [
  198. (x, y), (x + w, y), (x + w, y + h), (x, y + h), (x, y)
  199. ]
  200. else: # Rounded corners
  201. rx = 0.0 if rxstr is None else svgparselength(rxstr)[0]
  202. ry = 0.0 if rystr is None else svgparselength(rystr)[0]
  203. n_points = int(n_points / 4 + 0.5)
  204. t = np.arange(n_points, dtype=float) / n_points / 4
  205. x_ = (x + w - rx) + rx * np.cos(2 * np.pi * (t + 0.75))
  206. y_ = (y + ry) + ry * np.sin(2 * np.pi * (t + 0.75))
  207. lower_right = [(x_[i], y_[i]) for i in range(n_points)]
  208. x_ = (x + w - rx) + rx * np.cos(2 * np.pi * t)
  209. y_ = (y + h - ry) + ry * np.sin(2 * np.pi * t)
  210. upper_right = [(x_[i], y_[i]) for i in range(n_points)]
  211. x_ = (x + rx) + rx * np.cos(2 * np.pi * (t + 0.25))
  212. y_ = (y + h - ry) + ry * np.sin(2 * np.pi * (t + 0.25))
  213. upper_left = [(x_[i], y_[i]) for i in range(n_points)]
  214. x_ = (x + rx) + rx * np.cos(2 * np.pi * (t + 0.5))
  215. y_ = (y + ry) + ry * np.sin(2 * np.pi * (t + 0.5))
  216. lower_left = [(x_[i], y_[i]) for i in range(n_points)]
  217. pts = [(x + rx, y), (x - rx + w, y)] + \
  218. lower_right + \
  219. [(x + w, y + ry), (x + w, y + h - ry)] + \
  220. upper_right + \
  221. [(x + w - rx, y + h), (x + rx, y + h)] + \
  222. upper_left + \
  223. [(x, y + h - ry), (x, y + ry)] + \
  224. lower_left
  225. return Polygon(pts).buffer(0)
  226. # return LinearRing(pts)
  227. def svgcircle2shapely(circle, n_points=64, factor=1.0):
  228. """
  229. Converts an SVG circle into Shapely geometry.
  230. :param circle: Circle Element
  231. :type circle: xml.etree.ElementTree.Element
  232. :param n_points: circle resolution; nr of points to b e used to approximate a circle
  233. :type n_points: int
  234. :return: Shapely representation of the circle.
  235. :rtype: shapely.geometry.polygon.LinearRing
  236. """
  237. # cx = float(circle.get('cx'))
  238. # cy = float(circle.get('cy'))
  239. # r = float(circle.get('r'))
  240. cx = svgparselength(circle.get('cx'))[0] # TODO: No units support yet
  241. cx = cx * factor if cx else cx
  242. cy = svgparselength(circle.get('cy'))[0] # TODO: No units support yet
  243. cy = cy * factor if cy else cy
  244. r = svgparselength(circle.get('r'))[0] # TODO: No units support yet
  245. r = r * factor if r else r
  246. return Point(cx, cy).buffer(r, resolution=n_points)
  247. def svgellipse2shapely(ellipse, n_points=64, factor=1.0):
  248. """
  249. Converts an SVG ellipse into Shapely geometry
  250. :param ellipse: Ellipse Element
  251. :type ellipse: xml.etree.ElementTree.Element
  252. :param n_points: Number of discrete points in output.
  253. :type n_points: int
  254. :return: Shapely representation of the ellipse.
  255. :rtype: shapely.geometry.polygon.LinearRing
  256. """
  257. cx = svgparselength(ellipse.get('cx'))[0] # TODO: No units support yet
  258. cx = cx * factor if cx else cx
  259. cy = svgparselength(ellipse.get('cy'))[0] # TODO: No units support yet
  260. cy = cy * factor if cy else cy
  261. rx = svgparselength(ellipse.get('rx'))[0] # TODO: No units support yet
  262. rx = rx * factor if rx else rx
  263. ry = svgparselength(ellipse.get('ry'))[0] # TODO: No units support yet
  264. ry = ry * factor if ry else ry
  265. t = np.arange(n_points, dtype=float) / n_points
  266. x = cx + rx * np.cos(2 * np.pi * t)
  267. y = cy + ry * np.sin(2 * np.pi * t)
  268. pts = [(x[i], y[i]) for i in range(n_points)]
  269. return Polygon(pts).buffer(0)
  270. # return LinearRing(pts)
  271. def svgline2shapely(line, factor=1.0):
  272. """
  273. :param line: Line element
  274. :type line: xml.etree.ElementTree.Element
  275. :param factor: correction factor due of virtual units
  276. :type factor: float
  277. :return: Shapely representation on the line.
  278. :rtype: shapely.geometry.polygon.LineString
  279. """
  280. x1 = svgparselength(line.get('x1'))[0] * factor
  281. y1 = svgparselength(line.get('y1'))[0] * factor
  282. x2 = svgparselength(line.get('x2'))[0] * factor
  283. y2 = svgparselength(line.get('y2'))[0] * factor
  284. return LineString([(x1, y1), (x2, y2)])
  285. def svgpolyline2shapely(polyline, factor=1.0):
  286. """
  287. :param polyline: Polyline element
  288. :type polyline: xml.etree.ElementTree.Element
  289. :param factor: correction factor due of virtual units
  290. :type factor: float
  291. :return: Shapely representation of the PolyLine
  292. :rtype: shapely.geometry.polygon.LineString
  293. """
  294. ptliststr = polyline.get('points')
  295. points = parse_svg_point_list(ptliststr, factor)
  296. return LineString(points)
  297. def svgpolygon2shapely(polygon, n_points=64, factor=1.0):
  298. """
  299. Convert a SVG polygon to a Shapely Polygon.
  300. :param polygon:
  301. :type polygon:
  302. :param n_points: circle resolution; nr of points to b e used to approximate a circle
  303. :type n_points: int
  304. :param factor: correction factor due of virtual units
  305. :type factor: float
  306. :return: Shapely Polygon
  307. """
  308. ptliststr = polygon.get('points')
  309. points = parse_svg_point_list(ptliststr, factor)
  310. return Polygon(points).buffer(0, resolution=n_points)
  311. # return LinearRing(points)
  312. def getsvggeo(node, object_type, root=None, units='MM', res=64, factor=1.0):
  313. """
  314. Extracts and flattens all geometry from an SVG node
  315. into a list of Shapely geometry.
  316. :param node: xml.etree.ElementTree.Element
  317. :param object_type:
  318. :param root:
  319. :param units: FlatCAM units
  320. :param res: resolution to be used for circles buffering
  321. :param factor: correction factor due of virtual units
  322. :type factor: float
  323. :return: List of Shapely geometry
  324. :rtype: list
  325. """
  326. if root is None:
  327. root = node
  328. kind = re.search('(?:\{.*\})?(.*)$', node.tag).group(1)
  329. geo = []
  330. # Recurse
  331. if len(node) > 0:
  332. for child in node:
  333. subgeo = getsvggeo(child, object_type, root=root, units=units, res=res, factor=factor)
  334. if subgeo is not None:
  335. geo += subgeo
  336. # Parse
  337. elif kind == 'path':
  338. log.debug("***PATH***")
  339. P = parse_path(node.get('d'))
  340. P = path2shapely(P, object_type, units=units, factor=factor)
  341. # for path, the resulting geometry is already a list so no need to create a new one
  342. geo = P
  343. elif kind == 'rect':
  344. log.debug("***RECT***")
  345. R = svgrect2shapely(node, n_points=res, factor=factor)
  346. geo = [R]
  347. elif kind == 'circle':
  348. log.debug("***CIRCLE***")
  349. C = svgcircle2shapely(node, n_points=res, factor=factor)
  350. geo = [C]
  351. elif kind == 'ellipse':
  352. log.debug("***ELLIPSE***")
  353. E = svgellipse2shapely(node, n_points=res, factor=factor)
  354. geo = [E]
  355. elif kind == 'polygon':
  356. log.debug("***POLYGON***")
  357. poly = svgpolygon2shapely(node, n_points=res, factor=factor)
  358. geo = [poly]
  359. elif kind == 'line':
  360. log.debug("***LINE***")
  361. line = svgline2shapely(node, factor=factor)
  362. geo = [line]
  363. elif kind == 'polyline':
  364. log.debug("***POLYLINE***")
  365. pline = svgpolyline2shapely(node, factor=factor)
  366. geo = [pline]
  367. elif kind == 'use':
  368. log.debug('***USE***')
  369. # href= is the preferred name for this[1], but inkscape still generates xlink:href=.
  370. # [1] https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use#Attributes
  371. href = node.attrib['href'] if 'href' in node.attrib else node.attrib['{http://www.w3.org/1999/xlink}href']
  372. ref = root.find(".//*[@id='%s']" % href.replace('#', ''))
  373. if ref is not None:
  374. geo = getsvggeo(ref, object_type, root=root, units=units, res=res, factor=factor)
  375. else:
  376. log.warning("Unknown kind: " + kind)
  377. geo = None
  378. # ignore transformation for unknown kind
  379. if geo is not None:
  380. # Transformations
  381. if 'transform' in node.attrib:
  382. trstr = node.get('transform')
  383. trlist = parse_svg_transform(trstr)
  384. # log.debug(trlist)
  385. # Transformations are applied in reverse order
  386. for tr in trlist[::-1]:
  387. if tr[0] == 'translate':
  388. geo = [translate(geoi, tr[1], tr[2]) for geoi in geo]
  389. elif tr[0] == 'scale':
  390. geo = [scale(geoi, tr[1], tr[2], origin=(0, 0))
  391. for geoi in geo]
  392. elif tr[0] == 'rotate':
  393. geo = [rotate(geoi, tr[1], origin=(tr[2], tr[3]))
  394. for geoi in geo]
  395. elif tr[0] == 'skew':
  396. geo = [skew(geoi, tr[1], tr[2], origin=(0, 0))
  397. for geoi in geo]
  398. elif tr[0] == 'matrix':
  399. geo = [affine_transform(geoi, tr[1:]) for geoi in geo]
  400. else:
  401. raise Exception('Unknown transformation: %s', tr)
  402. return geo
  403. def getsvgtext(node, object_type, units='MM'):
  404. """
  405. Extracts and flattens all geometry from an SVG node
  406. into a list of Shapely geometry.
  407. :param node: xml.etree.ElementTree.Element
  408. :param object_type:
  409. :param units: FlatCAM units
  410. :return: List of Shapely geometry
  411. :rtype: list
  412. """
  413. kind = re.search('(?:\{.*\})?(.*)$', node.tag).group(1)
  414. geo = []
  415. # Recurse
  416. if len(node) > 0:
  417. for child in node:
  418. subgeo = getsvgtext(child, object_type, units=units)
  419. if subgeo is not None:
  420. geo += subgeo
  421. # Parse
  422. elif kind == 'tspan':
  423. current_attrib = node.attrib
  424. txt = node.text
  425. style_dict = {}
  426. parrent_attrib = node.getparent().attrib
  427. style = parrent_attrib['style']
  428. try:
  429. style_list = style.split(';')
  430. for css in style_list:
  431. style_dict[css.rpartition(':')[0]] = css.rpartition(':')[-1]
  432. pos_x = float(current_attrib['x'])
  433. pos_y = float(current_attrib['y'])
  434. # should have used the instance from FlatCAMApp.App but how? without reworking everything ...
  435. pf = ParseFont()
  436. pf.get_fonts_by_types()
  437. font_name = style_dict['font-family'].replace("'", '')
  438. if style_dict['font-style'] == 'italic' and style_dict['font-weight'] == 'bold':
  439. font_type = 'bi'
  440. elif style_dict['font-weight'] == 'bold':
  441. font_type = 'bold'
  442. elif style_dict['font-style'] == 'italic':
  443. font_type = 'italic'
  444. else:
  445. font_type = 'regular'
  446. # value of 2.2 should have been 2.83 (conversion value from pixels to points)
  447. # but the dimensions from Inkscape did not corelate with the ones after importing in FlatCAM
  448. # so I adjusted this
  449. font_size = svgparselength(style_dict['font-size'])[0] * 2.2
  450. geo = [pf.font_to_geometry(txt,
  451. font_name=font_name,
  452. font_size=font_size,
  453. font_type=font_type,
  454. units=units,
  455. coordx=pos_x,
  456. coordy=pos_y)
  457. ]
  458. geo = [(scale(g, 1.0, -1.0)) for g in geo]
  459. except Exception as e:
  460. log.debug(str(e))
  461. else:
  462. geo = None
  463. # ignore transformation for unknown kind
  464. if geo is not None:
  465. # Transformations
  466. if 'transform' in node.attrib:
  467. trstr = node.get('transform')
  468. trlist = parse_svg_transform(trstr)
  469. # log.debug(trlist)
  470. # Transformations are applied in reverse order
  471. for tr in trlist[::-1]:
  472. if tr[0] == 'translate':
  473. geo = [translate(geoi, tr[1], tr[2]) for geoi in geo]
  474. elif tr[0] == 'scale':
  475. geo = [scale(geoi, tr[1], tr[2], origin=(0, 0))
  476. for geoi in geo]
  477. elif tr[0] == 'rotate':
  478. geo = [rotate(geoi, tr[1], origin=(tr[2], tr[3]))
  479. for geoi in geo]
  480. elif tr[0] == 'skew':
  481. geo = [skew(geoi, tr[1], tr[2], origin=(0, 0))
  482. for geoi in geo]
  483. elif tr[0] == 'matrix':
  484. geo = [affine_transform(geoi, tr[1:]) for geoi in geo]
  485. else:
  486. raise Exception('Unknown transformation: %s', tr)
  487. return geo
  488. def parse_svg_point_list(ptliststr, factor):
  489. """
  490. Returns a list of coordinate pairs extracted from the "points"
  491. attribute in SVG polygons and polyline's.
  492. :param ptliststr: "points" attribute string in polygon or polyline.
  493. :param factor: correction factor due of virtual units
  494. :type factor: float
  495. :return: List of tuples with coordinates.
  496. """
  497. pairs = []
  498. last = None
  499. pos = 0
  500. i = 0
  501. for match in re.finditer(r'(\s*,\s*)|(\s+)', ptliststr.strip(' ')):
  502. val = float(ptliststr[pos:match.start()])
  503. if i % 2 == 1:
  504. pairs.append((factor * last, factor * val))
  505. else:
  506. last = val * factor
  507. pos = match.end()
  508. i += 1
  509. # Check for last element
  510. val = float(ptliststr[pos:])
  511. if i % 2 == 1:
  512. pairs.append((factor * last, factor * val))
  513. else:
  514. log.warning("Incomplete coordinates.")
  515. return pairs
  516. def parse_svg_transform(trstr):
  517. """
  518. Parses an SVG transform string into a list
  519. of transform names and their parameters.
  520. Possible transformations are:
  521. * Translate: translate(<tx> [<ty>]), which specifies
  522. a translation by tx and ty. If <ty> is not provided,
  523. it is assumed to be zero. Result is
  524. ['translate', tx, ty]
  525. * Scale: scale(<sx> [<sy>]), which specifies a scale operation
  526. by sx and sy. If <sy> is not provided, it is assumed to be
  527. equal to <sx>. Result is: ['scale', sx, sy]
  528. * Rotate: rotate(<rotate-angle> [<cx> <cy>]), which specifies
  529. a rotation by <rotate-angle> degrees about a given point.
  530. If optional parameters <cx> and <cy> are not supplied,
  531. the rotate is about the origin of the current user coordinate
  532. system. Result is: ['rotate', rotate-angle, cx, cy]
  533. * Skew: skewX(<skew-angle>), which specifies a skew
  534. transformation along the x-axis. skewY(<skew-angle>), which
  535. specifies a skew transformation along the y-axis.
  536. Result is ['skew', angle-x, angle-y]
  537. * Matrix: matrix(<a> <b> <c> <d> <e> <f>), which specifies a
  538. transformation in the form of a transformation matrix of six
  539. values. matrix(a,b,c,d,e,f) is equivalent to applying the
  540. transformation matrix [a b c d e f]. Result is
  541. ['matrix', a, b, c, d, e, f]
  542. Note: All parameters to the transformations are "numbers",
  543. i.e. no units present.
  544. :param trstr: SVG transform string.
  545. :type trstr: str
  546. :return: List of transforms.
  547. :rtype: list
  548. """
  549. trlist = []
  550. assert isinstance(trstr, str)
  551. trstr = trstr.strip(' ')
  552. integer_re_str = r'[+-]?[0-9]+'
  553. number_re_str = r'(?:[+-]?[0-9]*\.[0-9]+(?:[Ee]' + integer_re_str + ')?' + r')|' + \
  554. r'(?:' + integer_re_str + r'(?:[Ee]' + integer_re_str + r')?)'
  555. # num_re_str = r'[\+\-]?[0-9\.e]+' # TODO: Negative exponents missing
  556. comma_or_space_re_str = r'(?:(?:\s+)|(?:\s*,\s*))'
  557. translate_re_str = r'translate\s*\(\s*(' + \
  558. number_re_str + r')(?:' + \
  559. comma_or_space_re_str + \
  560. r'(' + number_re_str + r'))?\s*\)'
  561. scale_re_str = r'scale\s*\(\s*(' + \
  562. number_re_str + r')' + \
  563. r'(?:' + comma_or_space_re_str + \
  564. r'(' + number_re_str + r'))?\s*\)'
  565. skew_re_str = r'skew([XY])\s*\(\s*(' + \
  566. number_re_str + r')\s*\)'
  567. rotate_re_str = r'rotate\s*\(\s*(' + \
  568. number_re_str + r')' + \
  569. r'(?:' + comma_or_space_re_str + \
  570. r'(' + number_re_str + r')' + \
  571. comma_or_space_re_str + \
  572. r'(' + number_re_str + r'))?\s*\)'
  573. matrix_re_str = r'matrix\s*\(\s*' + \
  574. r'(' + number_re_str + r')' + comma_or_space_re_str + \
  575. r'(' + number_re_str + r')' + comma_or_space_re_str + \
  576. r'(' + number_re_str + r')' + comma_or_space_re_str + \
  577. r'(' + number_re_str + r')' + comma_or_space_re_str + \
  578. r'(' + number_re_str + r')' + comma_or_space_re_str + \
  579. r'(' + number_re_str + r')\s*\)'
  580. while len(trstr) > 0:
  581. match = re.search(r'^' + translate_re_str, trstr)
  582. if match:
  583. trlist.append([
  584. 'translate',
  585. float(match.group(1)),
  586. float(match.group(2)) if (match.group(2) is not None) else 0.0
  587. ])
  588. trstr = trstr[len(match.group(0)):].strip(' ')
  589. continue
  590. match = re.search(r'^' + scale_re_str, trstr)
  591. if match:
  592. trlist.append([
  593. 'scale',
  594. float(match.group(1)),
  595. float(match.group(2)) if (match.group(2) is not None) else float(match.group(1))
  596. ])
  597. trstr = trstr[len(match.group(0)):].strip(' ')
  598. continue
  599. match = re.search(r'^' + skew_re_str, trstr)
  600. if match:
  601. trlist.append([
  602. 'skew',
  603. float(match.group(2)) if match.group(1) == 'X' else 0.0,
  604. float(match.group(2)) if match.group(1) == 'Y' else 0.0
  605. ])
  606. trstr = trstr[len(match.group(0)):].strip(' ')
  607. continue
  608. match = re.search(r'^' + rotate_re_str, trstr)
  609. if match:
  610. trlist.append([
  611. 'rotate',
  612. float(match.group(1)),
  613. float(match.group(2)) if match.group(2) else 0.0,
  614. float(match.group(3)) if match.group(3) else 0.0
  615. ])
  616. trstr = trstr[len(match.group(0)):].strip(' ')
  617. continue
  618. match = re.search(r'^' + matrix_re_str, trstr)
  619. if match:
  620. trlist.append(['matrix'] + [float(x) for x in match.groups()])
  621. trstr = trstr[len(match.group(0)):].strip(' ')
  622. continue
  623. # raise Exception("Don't know how to parse: %s" % trstr)
  624. log.error("[ERROR] Don't know how to parse: %s" % trstr)
  625. return trlist
  626. # if __name__ == "__main__":
  627. # tree = ET.parse('tests/svg/drawing.svg')
  628. # root = tree.getroot()
  629. # ns = re.search(r'\{(.*)\}', root.tag).group(1)
  630. # print(ns)
  631. # for geo in getsvggeo(root):
  632. # print(geo)