shapes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import re
  2. def _prefer_non_zero(*args):
  3. for arg in args:
  4. if arg != 0:
  5. return arg
  6. return 0.0
  7. def _ntos(n):
  8. # %f likes to add unnecessary 0's, %g isn't consistent about # decimals
  9. return ("%.3f" % n).rstrip("0").rstrip(".")
  10. def _strip_xml_ns(tag):
  11. # ElementTree API doesn't provide a way to ignore XML namespaces in tags
  12. # so we here strip them ourselves: cf. https://bugs.python.org/issue18304
  13. return tag.split("}", 1)[1] if "}" in tag else tag
  14. def _transform(raw_value):
  15. # TODO assumes a 'matrix' transform.
  16. # No other transform functions are supported at the moment.
  17. # https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
  18. # start simple: if you aren't exactly matrix(...) then no love
  19. match = re.match(r"matrix\((.*)\)", raw_value)
  20. if not match:
  21. raise NotImplementedError
  22. matrix = tuple(float(p) for p in re.split(r"\s+|,", match.group(1)))
  23. if len(matrix) != 6:
  24. raise ValueError("wrong # of terms in %s" % raw_value)
  25. return matrix
  26. class PathBuilder(object):
  27. def __init__(self):
  28. self.paths = []
  29. self.transforms = []
  30. def _start_path(self, initial_path=""):
  31. self.paths.append(initial_path)
  32. self.transforms.append(None)
  33. def _end_path(self):
  34. self._add("z")
  35. def _add(self, path_snippet):
  36. path = self.paths[-1]
  37. if path:
  38. path += " " + path_snippet
  39. else:
  40. path = path_snippet
  41. self.paths[-1] = path
  42. def _move(self, c, x, y):
  43. self._add("%s%s,%s" % (c, _ntos(x), _ntos(y)))
  44. def M(self, x, y):
  45. self._move("M", x, y)
  46. def m(self, x, y):
  47. self._move("m", x, y)
  48. def _arc(self, c, rx, ry, x, y, large_arc):
  49. self._add(
  50. "%s%s,%s 0 %d 1 %s,%s"
  51. % (c, _ntos(rx), _ntos(ry), large_arc, _ntos(x), _ntos(y))
  52. )
  53. def A(self, rx, ry, x, y, large_arc=0):
  54. self._arc("A", rx, ry, x, y, large_arc)
  55. def a(self, rx, ry, x, y, large_arc=0):
  56. self._arc("a", rx, ry, x, y, large_arc)
  57. def _vhline(self, c, x):
  58. self._add("%s%s" % (c, _ntos(x)))
  59. def H(self, x):
  60. self._vhline("H", x)
  61. def h(self, x):
  62. self._vhline("h", x)
  63. def V(self, y):
  64. self._vhline("V", y)
  65. def v(self, y):
  66. self._vhline("v", y)
  67. def _line(self, c, x, y):
  68. self._add("%s%s,%s" % (c, _ntos(x), _ntos(y)))
  69. def L(self, x, y):
  70. self._line("L", x, y)
  71. def l(self, x, y):
  72. self._line("l", x, y)
  73. def _parse_line(self, line):
  74. x1 = float(line.attrib.get("x1", 0))
  75. y1 = float(line.attrib.get("y1", 0))
  76. x2 = float(line.attrib.get("x2", 0))
  77. y2 = float(line.attrib.get("y2", 0))
  78. self._start_path()
  79. self.M(x1, y1)
  80. self.L(x2, y2)
  81. def _parse_rect(self, rect):
  82. x = float(rect.attrib.get("x", 0))
  83. y = float(rect.attrib.get("y", 0))
  84. w = float(rect.attrib.get("width"))
  85. h = float(rect.attrib.get("height"))
  86. rx = float(rect.attrib.get("rx", 0))
  87. ry = float(rect.attrib.get("ry", 0))
  88. rx = _prefer_non_zero(rx, ry)
  89. ry = _prefer_non_zero(ry, rx)
  90. # TODO there are more rules for adjusting rx, ry
  91. self._start_path()
  92. self.M(x + rx, y)
  93. self.H(x + w - rx)
  94. if rx > 0:
  95. self.A(rx, ry, x + w, y + ry)
  96. self.V(y + h - ry)
  97. if rx > 0:
  98. self.A(rx, ry, x + w - rx, y + h)
  99. self.H(x + rx)
  100. if rx > 0:
  101. self.A(rx, ry, x, y + h - ry)
  102. self.V(y + ry)
  103. if rx > 0:
  104. self.A(rx, ry, x + rx, y)
  105. self._end_path()
  106. def _parse_path(self, path):
  107. if "d" in path.attrib:
  108. self._start_path(initial_path=path.attrib["d"])
  109. def _parse_polygon(self, poly):
  110. if "points" in poly.attrib:
  111. self._start_path("M" + poly.attrib["points"])
  112. self._end_path()
  113. def _parse_polyline(self, poly):
  114. if "points" in poly.attrib:
  115. self._start_path("M" + poly.attrib["points"])
  116. def _parse_circle(self, circle):
  117. cx = float(circle.attrib.get("cx", 0))
  118. cy = float(circle.attrib.get("cy", 0))
  119. r = float(circle.attrib.get("r"))
  120. # arc doesn't seem to like being a complete shape, draw two halves
  121. self._start_path()
  122. self.M(cx - r, cy)
  123. self.A(r, r, cx + r, cy, large_arc=1)
  124. self.A(r, r, cx - r, cy, large_arc=1)
  125. def _parse_ellipse(self, ellipse):
  126. cx = float(ellipse.attrib.get("cx", 0))
  127. cy = float(ellipse.attrib.get("cy", 0))
  128. rx = float(ellipse.attrib.get("rx"))
  129. ry = float(ellipse.attrib.get("ry"))
  130. # arc doesn't seem to like being a complete shape, draw two halves
  131. self._start_path()
  132. self.M(cx - rx, cy)
  133. self.A(rx, ry, cx + rx, cy, large_arc=1)
  134. self.A(rx, ry, cx - rx, cy, large_arc=1)
  135. def add_path_from_element(self, el):
  136. tag = _strip_xml_ns(el.tag)
  137. parse_fn = getattr(self, "_parse_%s" % tag.lower(), None)
  138. if not callable(parse_fn):
  139. return False
  140. parse_fn(el)
  141. if "transform" in el.attrib:
  142. self.transforms[-1] = _transform(el.attrib["transform"])
  143. return True